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