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