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