Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/vfs.h> |
| 24 | #include <unistd.h> |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 25 | #include <vector> |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 26 | #include <dirent.h> |
| 27 | #include <time.h> |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 30 | #include <iostream> |
| 31 | #include <iomanip> |
Ethan Yonker | 8613dc0 | 2014-09-11 09:28:20 -0500 | [diff] [blame] | 32 | #include <sys/wait.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 33 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 34 | #include "twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 35 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 36 | #include "data.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 37 | #include "twrp-functions.hpp" |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 38 | #include "fixPermissions.hpp" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 39 | #include "twrpDigest.hpp" |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 40 | #include "twrpDU.hpp" |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 41 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 42 | #ifdef TW_HAS_MTP |
| 43 | #include "mtp/mtp_MtpServer.hpp" |
| 44 | #include "mtp/twrpMtp.hpp" |
| 45 | #endif |
| 46 | |
Dees Troy | 6f6441d | 2014-01-23 02:07:03 +0000 | [diff] [blame] | 47 | extern "C" { |
| 48 | #include "cutils/properties.h" |
| 49 | } |
| 50 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 51 | #ifdef TW_INCLUDE_CRYPTO |
| 52 | #ifdef TW_INCLUDE_JB_CRYPTO |
| 53 | #include "crypto/jb/cryptfs.h" |
| 54 | #else |
| 55 | #include "crypto/ics/cryptfs.h" |
| 56 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 57 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 59 | extern bool datamedia; |
| 60 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 61 | TWPartitionManager::TWPartitionManager(void) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 62 | mtp_was_enabled = false; |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 65 | int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 66 | FILE *fstabFile; |
| 67 | char fstab_line[MAX_FSTAB_LINE_LENGTH]; |
Dees Troy | 02a6453 | 2014-03-19 15:23:32 +0000 | [diff] [blame] | 68 | TWPartition* settings_partition = NULL; |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 69 | TWPartition* andsec_partition = NULL; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 70 | |
| 71 | fstabFile = fopen(Fstab_Filename.c_str(), "rt"); |
| 72 | if (fstabFile == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 73 | LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | |
| 77 | while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) { |
| 78 | if (fstab_line[0] != '/') |
| 79 | continue; |
| 80 | |
Dees_Troy | 2a92358 | 2012-09-20 12:13:34 -0400 | [diff] [blame] | 81 | if (fstab_line[strlen(fstab_line) - 1] != '\n') |
| 82 | fstab_line[strlen(fstab_line)] = '\n'; |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 83 | TWPartition* partition = new TWPartition(); |
Dees_Troy | 2a92358 | 2012-09-20 12:13:34 -0400 | [diff] [blame] | 84 | string line = fstab_line; |
Dees_Troy | ab10ee2 | 2012-09-21 14:27:30 -0400 | [diff] [blame] | 85 | memset(fstab_line, 0, sizeof(fstab_line)); |
Dees_Troy | 2a92358 | 2012-09-20 12:13:34 -0400 | [diff] [blame] | 86 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 87 | if (partition->Process_Fstab_Line(line, Display_Error)) { |
Matt Mower | ed71fa3 | 2014-04-16 13:21:47 -0500 | [diff] [blame] | 88 | if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) { |
Dees Troy | 02a6453 | 2014-03-19 15:23:32 +0000 | [diff] [blame] | 89 | settings_partition = partition; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 90 | } else { |
| 91 | partition->Is_Settings_Storage = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 92 | } |
Matt Mower | ed71fa3 | 2014-04-16 13:21:47 -0500 | [diff] [blame] | 93 | if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) { |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 94 | andsec_partition = partition; |
| 95 | } else { |
| 96 | partition->Has_Android_Secure = false; |
| 97 | } |
Ethan Yonker | c05c598 | 2014-03-13 09:19:56 -0500 | [diff] [blame] | 98 | Partitions.push_back(partition); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 99 | } else { |
| 100 | delete partition; |
| 101 | } |
| 102 | } |
| 103 | fclose(fstabFile); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 104 | if (!datamedia && !settings_partition && Find_Partition_By_Path("/sdcard") == NULL && Find_Partition_By_Path("/internal_sd") == NULL && Find_Partition_By_Path("/internal_sdcard") == NULL && Find_Partition_By_Path("/emmc") == NULL) { |
| 105 | // Attempt to automatically identify /data/media emulated storage devices |
| 106 | TWPartition* Dat = Find_Partition_By_Path("/data"); |
| 107 | if (Dat) { |
| 108 | LOGINFO("Using automatic handling for /data/media emulated storage device.\n"); |
| 109 | datamedia = true; |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 110 | Dat->Setup_Data_Media(); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 111 | settings_partition = Dat; |
| 112 | } |
| 113 | } |
Dees Troy | 02a6453 | 2014-03-19 15:23:32 +0000 | [diff] [blame] | 114 | if (!settings_partition) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 115 | std::vector<TWPartition*>::iterator iter; |
| 116 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 117 | if ((*iter)->Is_Storage) { |
Dees Troy | 02a6453 | 2014-03-19 15:23:32 +0000 | [diff] [blame] | 118 | settings_partition = (*iter); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | } |
Dees Troy | 02a6453 | 2014-03-19 15:23:32 +0000 | [diff] [blame] | 122 | if (!settings_partition) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 123 | LOGERR("Unable to locate storage partition for storing settings file.\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 124 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 125 | if (!Write_Fstab()) { |
| 126 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 127 | LOGERR("Error creating fstab\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 128 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 129 | LOGINFO("Error creating fstab\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 130 | } |
Matt Mower | ed71fa3 | 2014-04-16 13:21:47 -0500 | [diff] [blame] | 131 | |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 132 | if (andsec_partition) { |
| 133 | Setup_Android_Secure_Location(andsec_partition); |
Matt Mower | ed71fa3 | 2014-04-16 13:21:47 -0500 | [diff] [blame] | 134 | } else if (settings_partition) { |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 135 | Setup_Android_Secure_Location(settings_partition); |
| 136 | } |
Matt Mower | ed71fa3 | 2014-04-16 13:21:47 -0500 | [diff] [blame] | 137 | if (settings_partition) { |
| 138 | Setup_Settings_Storage_Partition(settings_partition); |
| 139 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 140 | Update_System_Details(); |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 141 | UnMount_Main_Partitions(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | |
| 145 | int TWPartitionManager::Write_Fstab(void) { |
| 146 | FILE *fp; |
| 147 | std::vector<TWPartition*>::iterator iter; |
| 148 | string Line; |
| 149 | |
| 150 | fp = fopen("/etc/fstab", "w"); |
| 151 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 152 | LOGINFO("Can not open /etc/fstab.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 153 | return false; |
| 154 | } |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 155 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 156 | if ((*iter)->Can_Be_Mounted) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 157 | Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 158 | fputs(Line.c_str(), fp); |
Dees_Troy | 91862e6 | 2013-04-04 23:48:21 +0000 | [diff] [blame] | 159 | } |
| 160 | // Handle subpartition tracking |
| 161 | if ((*iter)->Is_SubPartition) { |
| 162 | TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of); |
| 163 | if (ParentPartition) |
| 164 | ParentPartition->Has_SubPartition = true; |
| 165 | else |
| 166 | LOGERR("Unable to locate parent partition '%s' of '%s'\n", (*iter)->SubPartition_Of.c_str(), (*iter)->Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | fclose(fp); |
| 170 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Ethan Yonker | c05c598 | 2014-03-13 09:19:56 -0500 | [diff] [blame] | 173 | void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) { |
Ethan Yonker | c05c598 | 2014-03-13 09:19:56 -0500 | [diff] [blame] | 174 | DataManager::SetValue("tw_settings_path", Part->Storage_Path); |
| 175 | DataManager::SetValue("tw_storage_path", Part->Storage_Path); |
| 176 | LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str()); |
| 177 | } |
| 178 | |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 179 | void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) { |
| 180 | if (Part->Has_Android_Secure) |
| 181 | Part->Setup_AndSec(); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 182 | else if (!datamedia) |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 183 | Part->Setup_AndSec(); |
Matt Mower | bf4efa3 | 2014-04-14 23:25:26 -0500 | [diff] [blame] | 184 | } |
| 185 | |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 186 | void TWPartitionManager::Output_Partition_Logging(void) { |
| 187 | std::vector<TWPartition*>::iterator iter; |
| 188 | |
| 189 | printf("\n\nPartition Logs:\n"); |
| 190 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) |
| 191 | Output_Partition((*iter)); |
| 192 | } |
| 193 | |
| 194 | void TWPartitionManager::Output_Partition(TWPartition* Part) { |
| 195 | unsigned long long mb = 1048576; |
| 196 | |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 197 | printf("%s | %s | Size: %iMB", Part->Mount_Point.c_str(), Part->Actual_Block_Device.c_str(), (int)(Part->Size / mb)); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 198 | if (Part->Can_Be_Mounted) { |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 199 | printf(" Used: %iMB Free: %iMB Backup Size: %iMB", (int)(Part->Used / mb), (int)(Part->Free / mb), (int)(Part->Backup_Size / mb)); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 200 | } |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 201 | printf("\n Flags: "); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 202 | if (Part->Can_Be_Mounted) |
| 203 | printf("Can_Be_Mounted "); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 204 | if (Part->Can_Be_Wiped) |
| 205 | printf("Can_Be_Wiped "); |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 206 | if (Part->Use_Rm_Rf) |
| 207 | printf("Use_Rm_Rf "); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 208 | if (Part->Can_Be_Backed_Up) |
| 209 | printf("Can_Be_Backed_Up "); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 210 | if (Part->Wipe_During_Factory_Reset) |
| 211 | printf("Wipe_During_Factory_Reset "); |
| 212 | if (Part->Wipe_Available_in_GUI) |
| 213 | printf("Wipe_Available_in_GUI "); |
| 214 | if (Part->Is_SubPartition) |
| 215 | printf("Is_SubPartition "); |
| 216 | if (Part->Has_SubPartition) |
| 217 | printf("Has_SubPartition "); |
| 218 | if (Part->Removable) |
| 219 | printf("Removable "); |
| 220 | if (Part->Is_Present) |
| 221 | printf("IsPresent "); |
| 222 | if (Part->Can_Be_Encrypted) |
| 223 | printf("Can_Be_Encrypted "); |
| 224 | if (Part->Is_Encrypted) |
| 225 | printf("Is_Encrypted "); |
| 226 | if (Part->Is_Decrypted) |
| 227 | printf("Is_Decrypted "); |
| 228 | if (Part->Has_Data_Media) |
| 229 | printf("Has_Data_Media "); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 230 | if (Part->Can_Encrypt_Backup) |
| 231 | printf("Can_Encrypt_Backup "); |
| 232 | if (Part->Use_Userdata_Encryption) |
| 233 | printf("Use_Userdata_Encryption "); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 234 | if (Part->Has_Android_Secure) |
| 235 | printf("Has_Android_Secure "); |
| 236 | if (Part->Is_Storage) |
| 237 | printf("Is_Storage "); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 238 | if (Part->Is_Settings_Storage) |
| 239 | printf("Is_Settings_Storage "); |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 240 | if (Part->Ignore_Blkid) |
| 241 | printf("Ignore_Blkid "); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 242 | if (Part->Retain_Layout_Version) |
| 243 | printf("Retain_Layout_Version "); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 244 | printf("\n"); |
| 245 | if (!Part->SubPartition_Of.empty()) |
| 246 | printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str()); |
| 247 | if (!Part->Symlink_Path.empty()) |
| 248 | printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str()); |
| 249 | if (!Part->Symlink_Mount_Point.empty()) |
| 250 | printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str()); |
| 251 | if (!Part->Primary_Block_Device.empty()) |
| 252 | printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str()); |
| 253 | if (!Part->Alternate_Block_Device.empty()) |
| 254 | printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str()); |
| 255 | if (!Part->Decrypted_Block_Device.empty()) |
| 256 | printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str()); |
| 257 | if (Part->Length != 0) |
| 258 | printf(" Length: %i\n", Part->Length); |
| 259 | if (!Part->Display_Name.empty()) |
| 260 | printf(" Display_Name: %s\n", Part->Display_Name.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 261 | if (!Part->Storage_Name.empty()) |
| 262 | printf(" Storage_Name: %s\n", Part->Storage_Name.c_str()); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 263 | if (!Part->Backup_Path.empty()) |
| 264 | printf(" Backup_Path: %s\n", Part->Backup_Path.c_str()); |
| 265 | if (!Part->Backup_Name.empty()) |
| 266 | printf(" Backup_Name: %s\n", Part->Backup_Name.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 267 | if (!Part->Backup_Display_Name.empty()) |
| 268 | printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str()); |
Gary Peck | 004d48b | 2012-11-21 16:28:18 -0800 | [diff] [blame] | 269 | if (!Part->Backup_FileName.empty()) |
| 270 | printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str()); |
| 271 | if (!Part->Storage_Path.empty()) |
| 272 | printf(" Storage_Path: %s\n", Part->Storage_Path.c_str()); |
| 273 | if (!Part->Current_File_System.empty()) |
| 274 | printf(" Current_File_System: %s\n", Part->Current_File_System.c_str()); |
| 275 | if (!Part->Fstab_File_System.empty()) |
| 276 | printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str()); |
| 277 | if (Part->Format_Block_Size != 0) |
| 278 | printf(" Format_Block_Size: %i\n", Part->Format_Block_Size); |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 279 | if (!Part->MTD_Name.empty()) |
| 280 | printf(" MTD_Name: %s\n", Part->MTD_Name.c_str()); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 281 | string back_meth = Part->Backup_Method_By_Name(); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 282 | printf(" Backup_Method: %s\n", back_meth.c_str()); |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 283 | if (Part->Mount_Flags || !Part->Mount_Options.empty()) |
| 284 | printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str()); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 285 | printf("\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 288 | int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 289 | std::vector<TWPartition*>::iterator iter; |
| 290 | int ret = false; |
| 291 | bool found = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 292 | string Local_Path = TWFunc::Get_Root_Path(Path); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 293 | |
Dees_Troy | d93bda5 | 2013-07-03 19:55:19 +0000 | [diff] [blame] | 294 | if (Local_Path == "/tmp" || Local_Path == "/") |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 295 | return true; |
| 296 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 297 | // Iterate through all partitions |
| 298 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 299 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 300 | ret = (*iter)->Mount(Display_Error); |
| 301 | found = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 302 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 303 | (*iter)->Mount(Display_Error); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 304 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 305 | } |
| 306 | if (found) { |
| 307 | return ret; |
| 308 | } else if (Display_Error) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 309 | LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 310 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 311 | LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 312 | } |
| 313 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 316 | int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 317 | std::vector<TWPartition*>::iterator iter; |
| 318 | int ret = false; |
| 319 | bool found = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 320 | string Local_Path = TWFunc::Get_Root_Path(Path); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 321 | |
| 322 | // Iterate through all partitions |
| 323 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 324 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 325 | ret = (*iter)->UnMount(Display_Error); |
| 326 | found = true; |
| 327 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
| 328 | (*iter)->UnMount(Display_Error); |
| 329 | } |
| 330 | } |
| 331 | if (found) { |
| 332 | return ret; |
| 333 | } else if (Display_Error) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 334 | LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 335 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 336 | LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 337 | } |
| 338 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 341 | int TWPartitionManager::Is_Mounted_By_Path(string Path) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 342 | TWPartition* Part = Find_Partition_By_Path(Path); |
| 343 | |
| 344 | if (Part) |
| 345 | return Part->Is_Mounted(); |
| 346 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 347 | LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 348 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 351 | int TWPartitionManager::Mount_Current_Storage(bool Display_Error) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 352 | string current_storage_path = DataManager::GetCurrentStoragePath(); |
| 353 | |
| 354 | if (Mount_By_Path(current_storage_path, Display_Error)) { |
| 355 | TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path); |
| 356 | if (FreeStorage) |
| 357 | DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU)); |
| 358 | return true; |
| 359 | } |
| 360 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 361 | } |
| 362 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 363 | int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) { |
| 364 | return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error); |
| 365 | } |
| 366 | |
| 367 | TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) { |
| 368 | std::vector<TWPartition*>::iterator iter; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 369 | string Local_Path = TWFunc::Get_Root_Path(Path); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 370 | |
| 371 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 372 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 373 | return (*iter); |
| 374 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 375 | return NULL; |
| 376 | } |
| 377 | |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 378 | int TWPartitionManager::Check_Backup_Name(bool Display_Error) { |
| 379 | // Check the backup name to ensure that it is the correct size and contains only valid characters |
| 380 | // and that a backup with that name doesn't already exist |
| 381 | char backup_name[MAX_BACKUP_NAME_LEN]; |
| 382 | char backup_loc[255], tw_image_dir[255]; |
| 383 | int copy_size; |
| 384 | int index, cur_char; |
| 385 | string Backup_Name, Backup_Loc; |
| 386 | |
| 387 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
| 388 | copy_size = Backup_Name.size(); |
| 389 | // Check size |
| 390 | if (copy_size > MAX_BACKUP_NAME_LEN) { |
| 391 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 392 | LOGERR("Backup name is too long.\n"); |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 393 | return -2; |
| 394 | } |
| 395 | |
| 396 | // Check each character |
| 397 | strncpy(backup_name, Backup_Name.c_str(), copy_size); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 398 | if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0) |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 399 | return 0; // A "0" (zero) means to use the current timestamp for the backup name |
| 400 | for (index=0; index<copy_size; index++) { |
| 401 | cur_char = (int)backup_name[index]; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 402 | if (cur_char == 32 || (cur_char >= 48 && cur_char <= 57) || (cur_char >= 65 && cur_char <= 91) || cur_char == 93 || cur_char == 95 || (cur_char >= 97 && cur_char <= 123) || cur_char == 125 || cur_char == 45 || cur_char == 46) { |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 403 | // These are valid characters |
| 404 | // Numbers |
| 405 | // Upper case letters |
| 406 | // Lower case letters |
| 407 | // Space |
| 408 | // and -_.{}[] |
| 409 | } else { |
| 410 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 411 | LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char); |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 412 | return -3; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | // Check to make sure that a backup with this name doesn't already exist |
| 417 | DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc); |
| 418 | strcpy(backup_loc, Backup_Loc.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 419 | sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str()); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 420 | if (TWFunc::Path_Exists(tw_image_dir)) { |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 421 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 422 | LOGERR("A backup with this name already exists.\n"); |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 423 | return -4; |
| 424 | } |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 425 | // No problems found, return 0 |
| 426 | return 0; |
| 427 | } |
| 428 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 429 | bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename) |
| 430 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 431 | string command; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 432 | string Full_File = Backup_Folder + Backup_Filename; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 433 | string result; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 434 | twrpDigest md5sum; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 435 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 436 | if (!generate_md5) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 437 | return true; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 438 | |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 439 | TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 440 | gui_print(" * Generating md5...\n"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 441 | |
| 442 | if (TWFunc::Path_Exists(Full_File)) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 443 | md5sum.setfn(Backup_Folder + Backup_Filename); |
| 444 | if (md5sum.computeMD5() == 0) |
| 445 | if (md5sum.write_md5digest() == 0) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 446 | gui_print(" * MD5 Created.\n"); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 447 | else |
| 448 | return -1; |
| 449 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 450 | gui_print(" * MD5 Error!\n"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 451 | } else { |
| 452 | char filename[512]; |
| 453 | int index = 0; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 454 | string strfn; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 455 | sprintf(filename, "%s%03i", Full_File.c_str(), index); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 456 | strfn = filename; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 457 | while (index < 1000) { |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 458 | md5sum.setfn(filename); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 459 | if (TWFunc::Path_Exists(filename)) { |
| 460 | if (md5sum.computeMD5() == 0) { |
| 461 | if (md5sum.write_md5digest() != 0) |
| 462 | { |
| 463 | gui_print(" * MD5 Error.\n"); |
| 464 | return false; |
| 465 | } |
| 466 | } else { |
| 467 | gui_print(" * Error computing MD5.\n"); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 468 | return false; |
| 469 | } |
| 470 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 471 | index++; |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 472 | sprintf(filename, "%s%03i", Full_File.c_str(), index); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 473 | strfn = filename; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 474 | } |
| 475 | if (index == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 476 | LOGERR("Backup file: '%s' not found!\n", filename); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 477 | return false; |
| 478 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 479 | gui_print(" * MD5 Created.\n"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 480 | } |
| 481 | return true; |
| 482 | } |
| 483 | |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 484 | bool TWPartitionManager::Backup_Partition(TWPartition* Part, string Backup_Folder, bool generate_md5, unsigned long long* img_bytes_remaining, unsigned long long* file_bytes_remaining, unsigned long *img_time, unsigned long *file_time, unsigned long long *img_bytes, unsigned long long *file_bytes) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 485 | time_t start, stop; |
bigbiff bigbiff | 2c57d78 | 2013-02-19 10:09:21 -0500 | [diff] [blame] | 486 | int img_bps; |
| 487 | unsigned long long file_bps; |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 488 | unsigned long total_time, remain_time, section_time; |
| 489 | int use_compression, backup_time; |
| 490 | float pos; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 491 | unsigned long long total_size, current_size; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 492 | |
| 493 | if (Part == NULL) |
| 494 | return true; |
| 495 | |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 496 | DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps); |
| 497 | |
| 498 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
| 499 | if (use_compression) |
| 500 | DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps); |
| 501 | else |
| 502 | DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps); |
| 503 | |
| 504 | // We know the speed for both, how far into the whole backup are we, based on time |
| 505 | total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps); |
| 506 | remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps); |
| 507 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 508 | //pos = (total_time - remain_time) / (float) total_time; |
| 509 | total_size = *file_bytes + *img_bytes; |
| 510 | current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining; |
| 511 | pos = ((float)(current_size) / (float)(total_size)); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 512 | DataManager::SetProgress(pos); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 513 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 514 | LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 515 | |
| 516 | // And get the time |
| 517 | if (Part->Backup_Method == 1) |
| 518 | section_time = Part->Backup_Size / file_bps; |
| 519 | else |
| 520 | section_time = Part->Backup_Size / img_bps; |
| 521 | |
| 522 | // Set the position |
| 523 | pos = section_time / (float) total_time; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 524 | //DataManager::ShowProgress(pos, section_time); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 525 | |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 526 | TWFunc::SetPerformanceMode(true); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 527 | time(&start); |
| 528 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 529 | if (Part->Backup(Backup_Folder, &total_size, ¤t_size)) { |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 530 | bool md5Success = false; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 531 | current_size += Part->Backup_Size; |
| 532 | pos = (float)((float)(current_size) / (float)(total_size)); |
| 533 | DataManager::SetProgress(pos); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 534 | if (Part->Has_SubPartition) { |
| 535 | std::vector<TWPartition*>::iterator subpart; |
| 536 | |
| 537 | for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 538 | if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) { |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 539 | if (!(*subpart)->Backup(Backup_Folder, &total_size, ¤t_size)) { |
| 540 | TWFunc::SetPerformanceMode(false); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 541 | return false; |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 542 | } |
Dees_Troy | 2727b99 | 2013-08-14 20:09:30 +0000 | [diff] [blame] | 543 | sync(); |
| 544 | sync(); |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 545 | if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) { |
| 546 | TWFunc::SetPerformanceMode(false); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 547 | return false; |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 548 | } |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 549 | if (Part->Backup_Method == 1) { |
| 550 | *file_bytes_remaining -= (*subpart)->Backup_Size; |
| 551 | } else { |
| 552 | *img_bytes_remaining -= (*subpart)->Backup_Size; |
| 553 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 554 | current_size += Part->Backup_Size; |
| 555 | pos = (float)(current_size / total_size); |
| 556 | DataManager::SetProgress(pos); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 560 | time(&stop); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 561 | backup_time = (int) difftime(stop, start); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 562 | LOGINFO("Partition Backup time: %d\n", backup_time); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 563 | if (Part->Backup_Method == 1) { |
| 564 | *file_bytes_remaining -= Part->Backup_Size; |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 565 | *file_time += backup_time; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 566 | } else { |
| 567 | *img_bytes_remaining -= Part->Backup_Size; |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 568 | *img_time += backup_time; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 569 | } |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 570 | |
| 571 | md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName); |
| 572 | TWFunc::SetPerformanceMode(false); |
| 573 | return md5Success; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 574 | } else { |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 575 | TWFunc::SetPerformanceMode(false); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 576 | return false; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | int TWPartitionManager::Run_Backup(void) { |
| 581 | int check, do_md5, partition_count = 0; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 582 | string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 583 | unsigned long long total_bytes = 0, file_bytes = 0, img_bytes = 0, free_space = 0, img_bytes_remaining, file_bytes_remaining, subpart_size; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 584 | unsigned long img_time = 0, file_time = 0; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 585 | TWPartition* backup_part = NULL; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 586 | TWPartition* storage = NULL; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 587 | std::vector<TWPartition*>::iterator subpart; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 588 | struct tm *t; |
| 589 | time_t start, stop, seconds, total_start, total_stop; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 590 | size_t start_pos = 0, end_pos = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 591 | seconds = time(0); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 592 | t = localtime(&seconds); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 593 | |
| 594 | time(&total_start); |
| 595 | |
| 596 | Update_System_Details(); |
| 597 | |
| 598 | if (!Mount_Current_Storage(true)) |
| 599 | return false; |
| 600 | |
| 601 | DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 602 | if (do_md5 == 0) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 603 | do_md5 = true; |
Dees_Troy | c5865ab | 2012-09-24 15:08:04 -0400 | [diff] [blame] | 604 | else |
| 605 | do_md5 = false; |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 606 | |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 607 | DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder); |
| 608 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
Dees Troy | b21cc64 | 2013-09-10 17:36:41 +0000 | [diff] [blame] | 609 | if (Backup_Name == "(Current Date)") { |
| 610 | Backup_Name = TWFunc::Get_Current_Date(); |
| 611 | } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) { |
| 612 | TWFunc::Auto_Generate_Backup_Name(); |
| 613 | DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 614 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 615 | LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 616 | Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/"; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 617 | LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 618 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 619 | LOGINFO("Calculating backup details...\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 620 | DataManager::GetValue("tw_backup_list", Backup_List); |
| 621 | if (!Backup_List.empty()) { |
| 622 | end_pos = Backup_List.find(";", start_pos); |
| 623 | while (end_pos != string::npos && start_pos < Backup_List.size()) { |
| 624 | backup_path = Backup_List.substr(start_pos, end_pos - start_pos); |
| 625 | backup_part = Find_Partition_By_Path(backup_path); |
| 626 | if (backup_part != NULL) { |
| 627 | partition_count++; |
| 628 | if (backup_part->Backup_Method == 1) |
| 629 | file_bytes += backup_part->Backup_Size; |
| 630 | else |
| 631 | img_bytes += backup_part->Backup_Size; |
| 632 | if (backup_part->Has_SubPartition) { |
| 633 | std::vector<TWPartition*>::iterator subpart; |
| 634 | |
| 635 | for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) { |
Dees_Troy | 9e0b71c | 2013-04-08 13:35:37 +0000 | [diff] [blame] | 636 | if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_part->Mount_Point) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 637 | partition_count++; |
| 638 | if ((*subpart)->Backup_Method == 1) |
| 639 | file_bytes += (*subpart)->Backup_Size; |
| 640 | else |
| 641 | img_bytes += (*subpart)->Backup_Size; |
| 642 | } |
| 643 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 644 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 645 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 646 | LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str()); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 647 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 648 | start_pos = end_pos + 1; |
| 649 | end_pos = Backup_List.find(";", start_pos); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 650 | } |
| 651 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 652 | |
| 653 | if (partition_count == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 654 | gui_print("No partitions selected for backup.\n"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 655 | return false; |
| 656 | } |
| 657 | total_bytes = file_bytes + img_bytes; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 658 | gui_print(" * Total number of partitions to back up: %d\n", partition_count); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 659 | gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 660 | storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath()); |
| 661 | if (storage != NULL) { |
| 662 | free_space = storage->Free; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 663 | gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 664 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 665 | LOGERR("Unable to locate storage device.\n"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 666 | return false; |
| 667 | } |
Dees_Troy | d4b22b0 | 2013-01-18 17:17:58 +0000 | [diff] [blame] | 668 | if (free_space - (32 * 1024 * 1024) < total_bytes) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 669 | // We require an extra 32MB just in case |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 670 | LOGERR("Not enough free space on storage.\n"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 671 | return false; |
| 672 | } |
| 673 | img_bytes_remaining = img_bytes; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 674 | file_bytes_remaining = file_bytes; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 675 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 676 | gui_print("\n[BACKUP STARTED]\n"); |
| 677 | gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str()); |
Dees_Troy | d4b22b0 | 2013-01-18 17:17:58 +0000 | [diff] [blame] | 678 | if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 679 | LOGERR("Failed to make backup folder.\n"); |
Dees_Troy | d4b22b0 | 2013-01-18 17:17:58 +0000 | [diff] [blame] | 680 | return false; |
| 681 | } |
| 682 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 683 | DataManager::SetProgress(0.0); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 684 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 685 | start_pos = 0; |
| 686 | end_pos = Backup_List.find(";", start_pos); |
| 687 | while (end_pos != string::npos && start_pos < Backup_List.size()) { |
| 688 | backup_path = Backup_List.substr(start_pos, end_pos - start_pos); |
| 689 | backup_part = Find_Partition_By_Path(backup_path); |
| 690 | if (backup_part != NULL) { |
| 691 | if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes)) |
| 692 | return false; |
| 693 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 694 | LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 695 | } |
| 696 | start_pos = end_pos + 1; |
| 697 | end_pos = Backup_List.find(";", start_pos); |
| 698 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 699 | |
| 700 | // Average BPS |
| 701 | if (img_time == 0) |
| 702 | img_time = 1; |
| 703 | if (file_time == 0) |
| 704 | file_time = 1; |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 705 | int img_bps = (int)img_bytes / (int)img_time; |
bigbiff bigbiff | 2c57d78 | 2013-02-19 10:09:21 -0500 | [diff] [blame] | 706 | unsigned long long file_bps = file_bytes / (int)file_time; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 707 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 708 | gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024))); |
| 709 | gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024))); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 710 | |
| 711 | time(&total_stop); |
| 712 | int total_time = (int) difftime(total_stop, total_start); |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 713 | uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 714 | actual_backup_size /= (1024LLU * 1024LLU); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 715 | |
bigbiff bigbiff | 2c57d78 | 2013-02-19 10:09:21 -0500 | [diff] [blame] | 716 | int prev_img_bps, use_compression; |
| 717 | unsigned long long prev_file_bps; |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 718 | DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps); |
| 719 | img_bps += (prev_img_bps * 4); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 720 | img_bps /= 5; |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 721 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 722 | DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 723 | if (use_compression) |
| 724 | DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 725 | else |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 726 | DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps); |
| 727 | file_bps += (prev_file_bps * 4); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 728 | file_bps /= 5; |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 729 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 730 | DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps); |
Dees_Troy | 093b764 | 2012-09-21 15:59:38 -0400 | [diff] [blame] | 731 | if (use_compression) |
| 732 | DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps); |
| 733 | else |
| 734 | DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps); |
| 735 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 736 | gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 737 | Update_System_Details(); |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 738 | UnMount_Main_Partitions(); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 739 | gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 740 | string backup_log = Full_Backup_Path + "recovery.log"; |
| 741 | TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644); |
| 742 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 743 | } |
| 744 | |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 745 | bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) { |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 746 | time_t Start, Stop; |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 747 | TWFunc::SetPerformanceMode(true); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 748 | time(&Start); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 749 | //DataManager::ShowProgress(1.0 / (float)partition_count, 150); |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 750 | if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) { |
| 751 | TWFunc::SetPerformanceMode(false); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 752 | return false; |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 753 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 754 | if (Part->Has_SubPartition) { |
| 755 | std::vector<TWPartition*>::iterator subpart; |
| 756 | |
| 757 | for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) { |
| 758 | if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) { |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 759 | if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) { |
| 760 | TWFunc::SetPerformanceMode(false); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 761 | return false; |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 762 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 766 | time(&Stop); |
Tom Hite | 5a92672 | 2014-09-15 01:31:03 +0000 | [diff] [blame] | 767 | TWFunc::SetPerformanceMode(false); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 768 | gui_print("[%s done (%d seconds)]\n\n", Part->Backup_Display_Name.c_str(), (int)difftime(Stop, Start)); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 769 | return true; |
| 770 | } |
| 771 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 772 | int TWPartitionManager::Run_Restore(string Restore_Name) { |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 773 | int check_md5, check, partition_count = 0; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 774 | TWPartition* restore_part = NULL; |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 775 | time_t rStart, rStop; |
| 776 | time(&rStart); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 777 | string Restore_List, restore_path; |
| 778 | size_t start_pos = 0, end_pos; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 779 | unsigned long long total_restore_size = 0, already_restored_size = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 780 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 781 | gui_print("\n[RESTORE STARTED]\n\n"); |
| 782 | gui_print("Restore folder: '%s'\n", Restore_Name.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 783 | |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 784 | if (!Mount_Current_Storage(true)) |
| 785 | return false; |
| 786 | |
| 787 | DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 788 | if (check_md5 > 0) { |
| 789 | // Check MD5 files first before restoring to ensure that all of them match before starting a restore |
| 790 | TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 791 | gui_print("Verifying MD5...\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 792 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 793 | gui_print("Skipping MD5 check based on user setting.\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 794 | } |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 795 | gui_print("Calculating restore details...\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 796 | DataManager::GetValue("tw_restore_selected", Restore_List); |
| 797 | if (!Restore_List.empty()) { |
| 798 | end_pos = Restore_List.find(";", start_pos); |
| 799 | while (end_pos != string::npos && start_pos < Restore_List.size()) { |
| 800 | restore_path = Restore_List.substr(start_pos, end_pos - start_pos); |
| 801 | restore_part = Find_Partition_By_Path(restore_path); |
| 802 | if (restore_part != NULL) { |
| 803 | partition_count++; |
| 804 | if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name)) |
| 805 | return false; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 806 | total_restore_size += restore_part->Get_Restore_Size(Restore_Name); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 807 | if (restore_part->Has_SubPartition) { |
| 808 | std::vector<TWPartition*>::iterator subpart; |
| 809 | |
| 810 | for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) { |
| 811 | if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) { |
bigbiff bigbiff | 0733881 | 2014-03-30 14:56:41 -0400 | [diff] [blame] | 812 | if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name)) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 813 | return false; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 814 | total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | } |
| 818 | } else { |
Dees_Troy | 59df926 | 2013-06-19 14:53:57 -0500 | [diff] [blame] | 819 | LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 820 | } |
| 821 | start_pos = end_pos + 1; |
| 822 | end_pos = Restore_List.find(";", start_pos); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 823 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 824 | } |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 825 | |
| 826 | if (partition_count == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 827 | LOGERR("No partitions selected for restore.\n"); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 828 | return false; |
| 829 | } |
| 830 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 831 | gui_print("Restoring %i partitions...\n", partition_count); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 832 | gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 833 | DataManager::SetProgress(0.0); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 834 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 835 | start_pos = 0; |
| 836 | if (!Restore_List.empty()) { |
| 837 | end_pos = Restore_List.find(";", start_pos); |
| 838 | while (end_pos != string::npos && start_pos < Restore_List.size()) { |
| 839 | restore_path = Restore_List.substr(start_pos, end_pos - start_pos); |
| 840 | restore_part = Find_Partition_By_Path(restore_path); |
| 841 | if (restore_part != NULL) { |
| 842 | partition_count++; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 843 | if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size)) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 844 | return false; |
| 845 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 846 | LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str()); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 847 | } |
| 848 | start_pos = end_pos + 1; |
| 849 | end_pos = Restore_List.find(";", start_pos); |
| 850 | } |
| 851 | } |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 852 | TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details"); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 853 | Update_System_Details(); |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 854 | UnMount_Main_Partitions(); |
Dees_Troy | 4a2a126 | 2012-09-18 09:33:47 -0400 | [diff] [blame] | 855 | time(&rStop); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 856 | gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart)); |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 857 | DataManager::SetValue("tw_file_progress", ""); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 858 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | void TWPartitionManager::Set_Restore_Files(string Restore_Name) { |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 862 | // Start with the default values |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 863 | string Restore_List; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 864 | bool get_date = true, check_encryption = true; |
| 865 | |
| 866 | DataManager::SetValue("tw_restore_encrypted", 0); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 867 | |
| 868 | DIR* d; |
| 869 | d = opendir(Restore_Name.c_str()); |
| 870 | if (d == NULL) |
| 871 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 872 | LOGERR("Error opening %s\n", Restore_Name.c_str()); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 873 | return; |
| 874 | } |
| 875 | |
| 876 | struct dirent* de; |
| 877 | while ((de = readdir(d)) != NULL) |
| 878 | { |
| 879 | // Strip off three components |
| 880 | char str[256]; |
| 881 | char* label; |
| 882 | char* fstype = NULL; |
| 883 | char* extn = NULL; |
| 884 | char* ptr; |
| 885 | |
| 886 | strcpy(str, de->d_name); |
| 887 | if (strlen(str) <= 2) |
| 888 | continue; |
| 889 | |
| 890 | if (get_date) { |
| 891 | char file_path[255]; |
| 892 | struct stat st; |
| 893 | |
| 894 | strcpy(file_path, Restore_Name.c_str()); |
| 895 | strcat(file_path, "/"); |
| 896 | strcat(file_path, str); |
| 897 | stat(file_path, &st); |
| 898 | string backup_date = ctime((const time_t*)(&st.st_mtime)); |
| 899 | DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date); |
| 900 | get_date = false; |
| 901 | } |
| 902 | |
| 903 | label = str; |
| 904 | ptr = label; |
| 905 | while (*ptr && *ptr != '.') ptr++; |
| 906 | if (*ptr == '.') |
| 907 | { |
| 908 | *ptr = 0x00; |
| 909 | ptr++; |
| 910 | fstype = ptr; |
| 911 | } |
| 912 | while (*ptr && *ptr != '.') ptr++; |
| 913 | if (*ptr == '.') |
| 914 | { |
| 915 | *ptr = 0x00; |
| 916 | ptr++; |
| 917 | extn = ptr; |
| 918 | } |
| 919 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 920 | if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 921 | int extnlength = strlen(extn); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 922 | if (extnlength != 3 && extnlength != 6) continue; |
| 923 | if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue; |
| 924 | //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue; |
| 925 | |
| 926 | if (check_encryption) { |
| 927 | string filename = Restore_Name + "/"; |
| 928 | filename += de->d_name; |
| 929 | if (TWFunc::Get_File_Type(filename) == 2) { |
| 930 | LOGINFO("'%s' is encrypted\n", filename.c_str()); |
| 931 | DataManager::SetValue("tw_restore_encrypted", 1); |
| 932 | } |
| 933 | } |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 934 | if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 935 | |
| 936 | TWPartition* Part = Find_Partition_By_Path(label); |
| 937 | if (Part == NULL) |
| 938 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 939 | LOGERR(" Unable to locate partition by backup name: '%s'\n", label); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 940 | continue; |
| 941 | } |
| 942 | |
| 943 | Part->Backup_FileName = de->d_name; |
| 944 | if (strlen(extn) > 3) { |
| 945 | Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3); |
| 946 | } |
| 947 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 948 | Restore_List += Part->Backup_Path + ";"; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 949 | } |
| 950 | closedir(d); |
| 951 | |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 952 | // Set the final value |
| 953 | DataManager::SetValue("tw_restore_list", Restore_List); |
| 954 | DataManager::SetValue("tw_restore_selected", Restore_List); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 955 | return; |
| 956 | } |
| 957 | |
| 958 | int TWPartitionManager::Wipe_By_Path(string Path) { |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 959 | std::vector<TWPartition*>::iterator iter; |
| 960 | int ret = false; |
| 961 | bool found = false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 962 | string Local_Path = TWFunc::Get_Root_Path(Path); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 963 | |
| 964 | // Iterate through all partitions |
| 965 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 966 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 967 | if (Path == "/and-sec") |
| 968 | ret = (*iter)->Wipe_AndSec(); |
| 969 | else |
| 970 | ret = (*iter)->Wipe(); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 971 | found = true; |
| 972 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
| 973 | (*iter)->Wipe(); |
| 974 | } |
| 975 | } |
| 976 | if (found) { |
| 977 | return ret; |
| 978 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 979 | LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 980 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 981 | } |
| 982 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 983 | int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) { |
| 984 | std::vector<TWPartition*>::iterator iter; |
| 985 | int ret = false; |
| 986 | bool found = false; |
| 987 | string Local_Path = TWFunc::Get_Root_Path(Path); |
| 988 | |
| 989 | // Iterate through all partitions |
| 990 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 991 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
| 992 | if (Path == "/and-sec") |
| 993 | ret = (*iter)->Wipe_AndSec(); |
| 994 | else |
| 995 | ret = (*iter)->Wipe(New_File_System); |
| 996 | found = true; |
| 997 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
| 998 | (*iter)->Wipe(New_File_System); |
| 999 | } |
| 1000 | } |
| 1001 | if (found) { |
| 1002 | return ret; |
| 1003 | } else |
| 1004 | LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
| 1005 | return false; |
| 1006 | } |
| 1007 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1008 | int TWPartitionManager::Factory_Reset(void) { |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1009 | std::vector<TWPartition*>::iterator iter; |
| 1010 | int ret = true; |
| 1011 | |
| 1012 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1013 | if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) { |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1014 | if (!(*iter)->Wipe()) |
| 1015 | ret = false; |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 1016 | } else if ((*iter)->Has_Android_Secure) { |
| 1017 | if (!(*iter)->Wipe_AndSec()) |
| 1018 | ret = false; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1022 | } |
| 1023 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1024 | int TWPartitionManager::Wipe_Dalvik_Cache(void) { |
| 1025 | struct stat st; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1026 | vector <string> dir; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1027 | |
| 1028 | if (!Mount_By_Path("/data", true)) |
| 1029 | return false; |
| 1030 | |
| 1031 | if (!Mount_By_Path("/cache", true)) |
| 1032 | return false; |
| 1033 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1034 | dir.push_back("/data/dalvik-cache"); |
| 1035 | dir.push_back("/cache/dalvik-cache"); |
| 1036 | dir.push_back("/cache/dc"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1037 | gui_print("\nWiping Dalvik Cache Directories...\n"); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1038 | for (unsigned i = 0; i < dir.size(); ++i) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1039 | if (stat(dir.at(i).c_str(), &st) == 0) { |
| 1040 | TWFunc::removeDir(dir.at(i), false); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1041 | gui_print("Cleaned: %s...\n", dir.at(i).c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1042 | } |
| 1043 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1044 | TWPartition* sdext = Find_Partition_By_Path("/sd-ext"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1045 | if (sdext && sdext->Is_Present && sdext->Mount(false)) |
| 1046 | { |
| 1047 | if (stat("/sd-ext/dalvik-cache", &st) == 0) |
| 1048 | { |
| 1049 | TWFunc::removeDir("/sd-ext/dalvik-cache", false); |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1050 | gui_print("Cleaned: /sd-ext/dalvik-cache...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1051 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1052 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1053 | gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1054 | return true; |
| 1055 | } |
| 1056 | |
| 1057 | int TWPartitionManager::Wipe_Rotate_Data(void) { |
| 1058 | if (!Mount_By_Path("/data", true)) |
| 1059 | return false; |
| 1060 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1061 | unlink("/data/misc/akmd*"); |
| 1062 | unlink("/data/misc/rild*"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1063 | gui_print("Rotation data wiped.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1064 | return true; |
| 1065 | } |
| 1066 | |
| 1067 | int TWPartitionManager::Wipe_Battery_Stats(void) { |
| 1068 | struct stat st; |
| 1069 | |
| 1070 | if (!Mount_By_Path("/data", true)) |
| 1071 | return false; |
| 1072 | |
| 1073 | if (0 != stat("/data/system/batterystats.bin", &st)) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1074 | gui_print("No Battery Stats Found. No Need To Wipe.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1075 | } else { |
| 1076 | remove("/data/system/batterystats.bin"); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1077 | gui_print("Cleared battery stats.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1078 | } |
| 1079 | return true; |
| 1080 | } |
| 1081 | |
Dees_Troy | 2ff5a8d | 2012-09-26 14:53:02 -0400 | [diff] [blame] | 1082 | int TWPartitionManager::Wipe_Android_Secure(void) { |
| 1083 | std::vector<TWPartition*>::iterator iter; |
| 1084 | int ret = false; |
| 1085 | bool found = false; |
| 1086 | |
| 1087 | // Iterate through all partitions |
| 1088 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1089 | if ((*iter)->Has_Android_Secure) { |
| 1090 | ret = (*iter)->Wipe_AndSec(); |
| 1091 | found = true; |
| 1092 | } |
| 1093 | } |
| 1094 | if (found) { |
| 1095 | return ret; |
| 1096 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1097 | LOGERR("No android secure partitions found.\n"); |
Dees_Troy | 2ff5a8d | 2012-09-26 14:53:02 -0400 | [diff] [blame] | 1098 | } |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1102 | int TWPartitionManager::Format_Data(void) { |
| 1103 | TWPartition* dat = Find_Partition_By_Path("/data"); |
| 1104 | |
| 1105 | if (dat != NULL) { |
| 1106 | if (!dat->UnMount(true)) |
| 1107 | return false; |
| 1108 | |
| 1109 | return dat->Wipe_Encryption(); |
| 1110 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1111 | LOGERR("Unable to locate /data.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1112 | return false; |
| 1113 | } |
| 1114 | return false; |
| 1115 | } |
| 1116 | |
| 1117 | int TWPartitionManager::Wipe_Media_From_Data(void) { |
| 1118 | TWPartition* dat = Find_Partition_By_Path("/data"); |
| 1119 | |
| 1120 | if (dat != NULL) { |
| 1121 | if (!dat->Has_Data_Media) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1122 | LOGERR("This device does not have /data/media\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1123 | return false; |
| 1124 | } |
| 1125 | if (!dat->Mount(true)) |
| 1126 | return false; |
| 1127 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1128 | gui_print("Wiping internal storage -- /data/media...\n"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1129 | mtp_was_enabled = TWFunc::Toggle_MTP(false); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1130 | TWFunc::removeDir("/data/media", false); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1131 | if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) { |
| 1132 | if (mtp_was_enabled) { |
| 1133 | if (!Enable_MTP()) |
| 1134 | Disable_MTP(); |
| 1135 | } |
| 1136 | return false; |
| 1137 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1138 | if (dat->Has_Data_Media) { |
| 1139 | dat->Recreate_Media_Folder(); |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 1140 | // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping |
| 1141 | dat->UnMount(false); |
| 1142 | dat->Mount(false); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1143 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1144 | if (mtp_was_enabled) { |
| 1145 | if (!Enable_MTP()) |
| 1146 | Disable_MTP(); |
| 1147 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1148 | return true; |
| 1149 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1150 | LOGERR("Unable to locate /data.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1151 | return false; |
| 1152 | } |
| 1153 | return false; |
| 1154 | } |
| 1155 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1156 | int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) { |
| 1157 | std::vector<TWPartition*>::iterator iter; |
| 1158 | int ret = false; |
| 1159 | bool found = false; |
| 1160 | string Local_Path = TWFunc::Get_Root_Path(Path); |
| 1161 | |
| 1162 | if (Local_Path == "/tmp" || Local_Path == "/") |
| 1163 | return true; |
| 1164 | |
| 1165 | // Iterate through all partitions |
| 1166 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1167 | if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { |
| 1168 | ret = (*iter)->Repair(); |
| 1169 | found = true; |
| 1170 | } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) { |
| 1171 | (*iter)->Repair(); |
| 1172 | } |
| 1173 | } |
| 1174 | if (found) { |
| 1175 | return ret; |
| 1176 | } else if (Display_Error) { |
| 1177 | LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
| 1178 | } else { |
| 1179 | LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str()); |
| 1180 | } |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1184 | void TWPartitionManager::Update_System_Details(void) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1185 | std::vector<TWPartition*>::iterator iter; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1186 | int data_size = 0; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1187 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1188 | gui_print("Updating partition details...\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1189 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1190 | if ((*iter)->Can_Be_Mounted) { |
| 1191 | (*iter)->Update_Size(true); |
| 1192 | if ((*iter)->Mount_Point == "/system") { |
| 1193 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1194 | DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size); |
| 1195 | } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") { |
| 1196 | data_size += (int)((*iter)->Backup_Size / 1048576LLU); |
| 1197 | } else if ((*iter)->Mount_Point == "/cache") { |
| 1198 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1199 | DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size); |
| 1200 | } else if ((*iter)->Mount_Point == "/sd-ext") { |
| 1201 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1202 | DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size); |
| 1203 | if ((*iter)->Backup_Size == 0) { |
| 1204 | DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0); |
| 1205 | DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0); |
| 1206 | } else |
| 1207 | DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1208 | } else if ((*iter)->Has_Android_Secure) { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1209 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1210 | DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1211 | if ((*iter)->Backup_Size == 0) { |
| 1212 | DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0); |
| 1213 | DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0); |
| 1214 | } else |
| 1215 | DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1); |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 1216 | } else if ((*iter)->Mount_Point == "/boot") { |
| 1217 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1218 | DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size); |
| 1219 | if ((*iter)->Backup_Size == 0) { |
| 1220 | DataManager::SetValue("tw_has_boot_partition", 0); |
| 1221 | DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0); |
| 1222 | } else |
| 1223 | DataManager::SetValue("tw_has_boot_partition", 1); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1224 | } |
Dees_Troy | ab10ee2 | 2012-09-21 14:27:30 -0400 | [diff] [blame] | 1225 | #ifdef SP1_NAME |
| 1226 | if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) { |
| 1227 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1228 | DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size); |
| 1229 | } |
| 1230 | #endif |
| 1231 | #ifdef SP2_NAME |
| 1232 | if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) { |
| 1233 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1234 | DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size); |
| 1235 | } |
| 1236 | #endif |
| 1237 | #ifdef SP3_NAME |
| 1238 | if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) { |
| 1239 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1240 | DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size); |
| 1241 | } |
| 1242 | #endif |
Dees_Troy | aa9cc40 | 2012-10-13 12:14:05 -0400 | [diff] [blame] | 1243 | } else { |
| 1244 | // Handle unmountable partitions in case we reset defaults |
| 1245 | if ((*iter)->Mount_Point == "/boot") { |
| 1246 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1247 | DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size); |
| 1248 | if ((*iter)->Backup_Size == 0) { |
| 1249 | DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0); |
| 1250 | DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0); |
| 1251 | } else |
| 1252 | DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1); |
| 1253 | } else if ((*iter)->Mount_Point == "/recovery") { |
| 1254 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1255 | DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size); |
| 1256 | if ((*iter)->Backup_Size == 0) { |
| 1257 | DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0); |
| 1258 | DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0); |
| 1259 | } else |
| 1260 | DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1); |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1261 | } else if ((*iter)->Mount_Point == "/data") { |
| 1262 | data_size += (int)((*iter)->Backup_Size / 1048576LLU); |
Dees_Troy | aa9cc40 | 2012-10-13 12:14:05 -0400 | [diff] [blame] | 1263 | } |
| 1264 | #ifdef SP1_NAME |
| 1265 | if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) { |
| 1266 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1267 | DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size); |
| 1268 | } |
| 1269 | #endif |
| 1270 | #ifdef SP2_NAME |
| 1271 | if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) { |
| 1272 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1273 | DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size); |
| 1274 | } |
| 1275 | #endif |
| 1276 | #ifdef SP3_NAME |
| 1277 | if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) { |
| 1278 | int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU); |
| 1279 | DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size); |
| 1280 | } |
| 1281 | #endif |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1282 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1283 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1284 | DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size); |
| 1285 | string current_storage_path = DataManager::GetCurrentStoragePath(); |
| 1286 | TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1287 | if (FreeStorage != NULL) { |
| 1288 | // Attempt to mount storage |
| 1289 | if (!FreeStorage->Mount(false)) { |
| 1290 | // We couldn't mount storage... check to see if we have dual storage |
| 1291 | int has_dual_storage; |
| 1292 | DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage); |
| 1293 | if (has_dual_storage == 1) { |
| 1294 | // We have dual storage, see if we're using the internal storage that should always be present |
| 1295 | if (current_storage_path == DataManager::GetSettingsStoragePath()) { |
Dees_Troy | ab10ee2 | 2012-09-21 14:27:30 -0400 | [diff] [blame] | 1296 | if (!FreeStorage->Is_Encrypted) { |
| 1297 | // Not able to use internal, so error! |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1298 | LOGERR("Unable to mount internal storage.\n"); |
Dees_Troy | ab10ee2 | 2012-09-21 14:27:30 -0400 | [diff] [blame] | 1299 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1300 | DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0); |
| 1301 | } else { |
| 1302 | // We were using external, flip to internal |
| 1303 | DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 1304 | current_storage_path = DataManager::GetCurrentStoragePath(); |
| 1305 | FreeStorage = Find_Partition_By_Path(current_storage_path); |
| 1306 | if (FreeStorage != NULL) { |
| 1307 | DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU)); |
| 1308 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1309 | LOGERR("Unable to locate internal storage partition.\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1310 | DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0); |
| 1311 | } |
| 1312 | } |
| 1313 | } else { |
| 1314 | // No dual storage and unable to mount storage, error! |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1315 | LOGERR("Unable to mount storage.\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1316 | DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0); |
| 1317 | } |
| 1318 | } else { |
| 1319 | DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU)); |
| 1320 | } |
| 1321 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1322 | LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str()); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1323 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1324 | if (!Write_Fstab()) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1325 | LOGERR("Error creating fstab\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1326 | return; |
| 1327 | } |
| 1328 | |
| 1329 | int TWPartitionManager::Decrypt_Device(string Password) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1330 | #ifdef TW_INCLUDE_CRYPTO |
| 1331 | int ret_val, password_len; |
| 1332 | char crypto_blkdev[255], cPassword[255]; |
| 1333 | size_t result; |
| 1334 | |
| 1335 | property_set("ro.crypto.state", "encrypted"); |
| 1336 | #ifdef TW_INCLUDE_JB_CRYPTO |
| 1337 | // No extra flags needed |
| 1338 | #else |
| 1339 | property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE); |
| 1340 | property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV); |
| 1341 | property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT); |
| 1342 | property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS); |
| 1343 | property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS); |
| 1344 | property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC); |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1345 | |
| 1346 | #ifdef CRYPTO_SD_FS_TYPE |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 1347 | property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE); |
| 1348 | property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV); |
| 1349 | property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH)); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1350 | #endif |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1351 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 1352 | property_set("rw.km_fips_status", "ready"); |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1353 | |
| 1354 | #endif |
| 1355 | |
| 1356 | // some samsung devices store "footer" on efs partition |
| 1357 | TWPartition *efs = Find_Partition_By_Path("/efs"); |
| 1358 | if(efs && !efs->Is_Mounted()) |
| 1359 | efs->Mount(false); |
| 1360 | else |
| 1361 | efs = 0; |
| 1362 | #ifdef TW_EXTERNAL_STORAGE_PATH |
Dees_Troy | 20c02c0 | 2013-01-10 14:14:10 +0000 | [diff] [blame] | 1363 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1364 | TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH)); |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1365 | if (sdcard && sdcard->Mount(false)) { |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1366 | property_set("ro.crypto.external_encrypted", "1"); |
| 1367 | property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str()); |
| 1368 | } else { |
| 1369 | property_set("ro.crypto.external_encrypted", "0"); |
| 1370 | } |
| 1371 | #endif |
Dees_Troy | 20c02c0 | 2013-01-10 14:14:10 +0000 | [diff] [blame] | 1372 | #endif |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1373 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1374 | strcpy(cPassword, Password.c_str()); |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1375 | int pwret = cryptfs_check_passwd(cPassword); |
| 1376 | |
| 1377 | if (pwret != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1378 | LOGERR("Failed to decrypt data.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1379 | return -1; |
| 1380 | } |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1381 | |
| 1382 | if(efs) |
| 1383 | efs->UnMount(false); |
| 1384 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1385 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 1386 | if (strcmp(crypto_blkdev, "error") == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1387 | LOGERR("Error retrieving decrypted data block device.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1388 | } else { |
| 1389 | TWPartition* dat = Find_Partition_By_Path("/data"); |
| 1390 | if (dat != NULL) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1391 | DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1392 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 1393 | dat->Is_Decrypted = true; |
| 1394 | dat->Decrypted_Block_Device = crypto_blkdev; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1395 | dat->Setup_File_System(false); |
Dees_Troy | 74fb2e9 | 2013-04-15 14:35:47 +0000 | [diff] [blame] | 1396 | dat->Current_File_System = dat->Fstab_File_System; // Needed if we're ignoring blkid because encrypted devices start out as emmc |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1397 | gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev); |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1398 | |
| 1399 | #ifdef CRYPTO_SD_FS_TYPE |
| 1400 | char crypto_blkdev_sd[255]; |
| 1401 | property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error"); |
| 1402 | if (strcmp(crypto_blkdev_sd, "error") == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1403 | LOGERR("Error retrieving decrypted data block device.\n"); |
Dees_Troy | c8bafa1 | 2013-01-10 15:43:00 +0000 | [diff] [blame] | 1404 | } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){ |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1405 | emmc->Is_Decrypted = true; |
| 1406 | emmc->Decrypted_Block_Device = crypto_blkdev_sd; |
| 1407 | emmc->Setup_File_System(false); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1408 | gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd); |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1409 | } |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1410 | #endif //ifdef CRYPTO_SD_FS_TYPE |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1411 | #ifdef TW_EXTERNAL_STORAGE_PATH |
Dees_Troy | 20c02c0 | 2013-01-10 14:14:10 +0000 | [diff] [blame] | 1412 | #ifdef TW_INCLUDE_CRYPTO_SAMSUNG |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1413 | char is_external_decrypted[255]; |
| 1414 | property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0"); |
| 1415 | if (strcmp(is_external_decrypted, "1") == 0) { |
| 1416 | sdcard->Is_Decrypted = true; |
| 1417 | sdcard->EcryptFS_Password = Password; |
| 1418 | sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device; |
Dees_Troy | 999b39d | 2013-01-14 15:36:13 +0000 | [diff] [blame] | 1419 | string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH); |
| 1420 | MetaEcfsFile += "/.MetaEcfsFile"; |
| 1421 | if (!TWFunc::Path_Exists(MetaEcfsFile)) { |
| 1422 | // External storage isn't actually encrypted so unmount and remount without ecryptfs |
| 1423 | sdcard->UnMount(false); |
| 1424 | sdcard->Mount(false); |
| 1425 | } |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1426 | } else { |
Dees_Troy | 066eb30 | 2013-08-23 17:20:32 +0000 | [diff] [blame] | 1427 | LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str()); |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1428 | sdcard->Is_Decrypted = false; |
| 1429 | sdcard->Decrypted_Block_Device = ""; |
| 1430 | } |
Dees_Troy | 20c02c0 | 2013-01-10 14:14:10 +0000 | [diff] [blame] | 1431 | #endif |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1432 | #endif //ifdef TW_EXTERNAL_STORAGE_PATH |
a3955269 | 6ff55ce | 2013-01-08 16:14:56 +0000 | [diff] [blame] | 1433 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1434 | // Sleep for a bit so that the device will be ready |
| 1435 | sleep(1); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1436 | if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) { |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1437 | dat->Storage_Path = "/data/media/0"; |
| 1438 | dat->Symlink_Path = dat->Storage_Path; |
Dees Troy | 98fb46c | 2013-12-04 16:56:45 +0000 | [diff] [blame] | 1439 | DataManager::SetValue("tw_storage_path", "/data/media/0"); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1440 | dat->UnMount(false); |
Dees_Troy | dc8bc1b | 2013-01-17 01:39:28 +0000 | [diff] [blame] | 1441 | Output_Partition(dat); |
Dees_Troy | 16b7435 | 2012-11-14 22:27:31 +0000 | [diff] [blame] | 1442 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1443 | Update_System_Details(); |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 1444 | UnMount_Main_Partitions(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1445 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1446 | LOGERR("Unable to locate data partition.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1447 | } |
| 1448 | return 0; |
| 1449 | #else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1450 | LOGERR("No crypto support was compiled into this build.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1451 | return -1; |
| 1452 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1453 | return 1; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1454 | } |
| 1455 | |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 1456 | int TWPartitionManager::Fix_Permissions(void) { |
| 1457 | int result = 0; |
| 1458 | if (!Mount_By_Path("/data", true)) |
| 1459 | return false; |
| 1460 | |
| 1461 | if (!Mount_By_Path("/system", true)) |
| 1462 | return false; |
| 1463 | |
| 1464 | Mount_By_Path("/sd-ext", false); |
| 1465 | |
| 1466 | fixPermissions perms; |
| 1467 | result = perms.fixPerms(true, false); |
Dees_Troy | 1a650e6 | 2012-10-19 20:54:32 -0400 | [diff] [blame] | 1468 | UnMount_Main_Partitions(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1469 | gui_print("Done.\n\n"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 1470 | return result; |
| 1471 | } |
| 1472 | |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1473 | TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) { |
| 1474 | std::vector<TWPartition*>::iterator iter = Partitions.begin(); |
| 1475 | |
| 1476 | if (!Path.empty()) { |
| 1477 | string Search_Path = TWFunc::Get_Root_Path(Path); |
| 1478 | for (; iter != Partitions.end(); iter++) { |
| 1479 | if ((*iter)->Mount_Point == Search_Path) { |
| 1480 | iter++; |
| 1481 | break; |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | for (; iter != Partitions.end(); iter++) { |
| 1487 | if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) { |
| 1488 | return (*iter); |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | return NULL; |
| 1493 | } |
| 1494 | |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1495 | int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) { |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1496 | TWPartition* Part = Find_Partition_By_Path(Partition_Path); |
| 1497 | |
| 1498 | if (Part == NULL) { |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1499 | LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str()); |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1500 | return false; |
| 1501 | } |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1502 | LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str()); |
| 1503 | if (!Part->UnMount(true) || !Part->Is_Present) |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1504 | return false; |
| 1505 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1506 | if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) { |
| 1507 | LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno)); |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1508 | return false; |
| 1509 | } |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1510 | return true; |
| 1511 | } |
| 1512 | |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1513 | int TWPartitionManager::usb_storage_enable(void) { |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1514 | int has_dual, has_data_media; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1515 | char lun_file[255]; |
Dees_Troy | d21618c | 2012-10-14 18:48:49 -0400 | [diff] [blame] | 1516 | bool has_multiple_lun = false; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1517 | |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1518 | DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media); |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1519 | string Lun_File_str = CUSTOM_LUN_FILE; |
| 1520 | size_t found = Lun_File_str.find("%"); |
| 1521 | if (found != string::npos) { |
| 1522 | sprintf(lun_file, CUSTOM_LUN_FILE, 1); |
| 1523 | if (TWFunc::Path_Exists(lun_file)) |
| 1524 | has_multiple_lun = true; |
| 1525 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1526 | mtp_was_enabled = TWFunc::Toggle_MTP(false); |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1527 | if (!has_multiple_lun) { |
| 1528 | LOGINFO("Device doesn't have multiple lun files, mount current storage\n"); |
| 1529 | sprintf(lun_file, CUSTOM_LUN_FILE, 0); |
| 1530 | if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") { |
| 1531 | TWPartition* Mount = Find_Next_Storage("", "/data"); |
| 1532 | if (Mount) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1533 | if (!Open_Lun_File(Mount->Mount_Point, lun_file)) { |
| 1534 | goto error_handle; |
| 1535 | } |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1536 | } else { |
| 1537 | LOGERR("Unable to find storage partition to mount to USB\n"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1538 | goto error_handle; |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1539 | } |
| 1540 | } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1541 | goto error_handle; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1542 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1543 | } else { |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1544 | LOGINFO("Device has multiple lun files\n"); |
| 1545 | TWPartition* Mount1; |
| 1546 | TWPartition* Mount2; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1547 | sprintf(lun_file, CUSTOM_LUN_FILE, 0); |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1548 | Mount1 = Find_Next_Storage("", "/data"); |
| 1549 | if (Mount1) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1550 | if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) { |
| 1551 | goto error_handle; |
| 1552 | } |
Matt Mower | ee71706 | 2014-04-26 01:46:46 -0500 | [diff] [blame] | 1553 | sprintf(lun_file, CUSTOM_LUN_FILE, 1); |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1554 | Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data"); |
| 1555 | if (Mount2) { |
Matt Mower | ee71706 | 2014-04-26 01:46:46 -0500 | [diff] [blame] | 1556 | Open_Lun_File(Mount2->Mount_Point, lun_file); |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1557 | } |
| 1558 | } else { |
| 1559 | LOGERR("Unable to find storage partition to mount to USB\n"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1560 | goto error_handle; |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1561 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1562 | } |
Ethan Yonker | 47360be | 2014-04-01 10:34:34 -0500 | [diff] [blame] | 1563 | property_set("sys.storage.ums_enabled", "1"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1564 | return true; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1565 | error_handle: |
| 1566 | if (mtp_was_enabled) |
| 1567 | if (!Enable_MTP()) |
| 1568 | Disable_MTP(); |
| 1569 | return false; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | int TWPartitionManager::usb_storage_disable(void) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1573 | int index, ret; |
| 1574 | char lun_file[255], ch[2] = {0, 0}; |
| 1575 | string str = ch; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1576 | |
| 1577 | for (index=0; index<2; index++) { |
| 1578 | sprintf(lun_file, CUSTOM_LUN_FILE, index); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1579 | ret = TWFunc::write_file(lun_file, str); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1580 | if (ret < 0) { |
| 1581 | break; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1582 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1583 | } |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1584 | Mount_All_Storage(); |
| 1585 | Update_System_Details(); |
Dees_Troy | cfd73ef | 2012-10-12 16:52:00 -0400 | [diff] [blame] | 1586 | UnMount_Main_Partitions(); |
Matt Mower | d9cb906 | 2013-12-14 20:34:45 -0600 | [diff] [blame] | 1587 | property_set("sys.storage.ums_enabled", "0"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1588 | if (mtp_was_enabled) |
| 1589 | if (!Enable_MTP()) |
| 1590 | Disable_MTP(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1591 | if (ret < 0 && index == 0) { |
| 1592 | LOGERR("Unable to write to ums lunfile '%s'.", lun_file); |
| 1593 | return false; |
| 1594 | } else { |
| 1595 | return true; |
| 1596 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 1597 | return true; |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | void TWPartitionManager::Mount_All_Storage(void) { |
| 1601 | std::vector<TWPartition*>::iterator iter; |
| 1602 | |
| 1603 | for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { |
| 1604 | if ((*iter)->Is_Storage) |
| 1605 | (*iter)->Mount(false); |
| 1606 | } |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 1607 | } |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1608 | |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 1609 | void TWPartitionManager::UnMount_Main_Partitions(void) { |
| 1610 | // Unmounts system and data if data is not data/media |
| 1611 | // Also unmounts boot if boot is mountable |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1612 | LOGINFO("Unmounting main partitions...\n"); |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 1613 | |
| 1614 | TWPartition* Boot_Partition = Find_Partition_By_Path("/boot"); |
| 1615 | |
| 1616 | UnMount_By_Path("/system", true); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1617 | if (!datamedia) |
| 1618 | UnMount_By_Path("/data", true); |
| 1619 | |
Dees_Troy | d0384ef | 2012-10-12 12:15:42 -0400 | [diff] [blame] | 1620 | if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted) |
| 1621 | Boot_Partition->UnMount(true); |
| 1622 | } |
| 1623 | |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1624 | int TWPartitionManager::Partition_SDCard(void) { |
| 1625 | char mkdir_path[255], temp[255], line[512]; |
| 1626 | string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice; |
| 1627 | int ext, swap, total_size = 0, fat_size; |
| 1628 | FILE* fp; |
| 1629 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1630 | gui_print("Partitioning SD Card...\n"); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1631 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 1632 | TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH)); |
| 1633 | #else |
| 1634 | TWPartition* SDCard = Find_Partition_By_Path("/sdcard"); |
| 1635 | #endif |
Ethan Yonker | 1eff6cd | 2014-09-15 13:30:42 -0500 | [diff] [blame] | 1636 | if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1637 | LOGERR("Unable to locate device to partition.\n"); |
Dees_Troy | 9350b8d | 2012-09-27 12:38:38 -0400 | [diff] [blame] | 1638 | return false; |
| 1639 | } |
| 1640 | if (!SDCard->UnMount(true)) |
| 1641 | return false; |
| 1642 | TWPartition* SDext = Find_Partition_By_Path("/sd-ext"); |
| 1643 | if (SDext != NULL) { |
| 1644 | if (!SDext->UnMount(true)) |
| 1645 | return false; |
| 1646 | } |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1647 | |
| 1648 | TWFunc:: |