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