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