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