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