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