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