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