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