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