blob: 9e23ccb9425163c6dd0074b9b0943b309a5aebc1 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
Ethan Yonker472f5062016-02-25 13:47:30 -06002 Copyright 2014 to 2016 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 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_Troy51a0e822012-09-05 15:24:24 -040018
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_Troy5bf43922012-09-07 16:07:55 -040025#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040026#include <dirent.h>
27#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040028#include <errno.h>
29#include <fcntl.h>
bigbiffce8f83c2015-12-12 18:30:21 -050030#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031#include <iostream>
32#include <iomanip>
Ethan Yonker8613dc02014-09-11 09:28:20 -050033#include <sys/wait.h>
Ethan Yonker483e9f42016-01-11 22:21:18 -060034#include <linux/fs.h>
35#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "twrp-functions.hpp"
Ethan Yonkerb5fab762016-01-28 14:03:33 -060041#include "fixContexts.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050042#include "twrpDigest.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060043#include "exclude.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060044#include "set_metadata.h"
bigbiff7abc5fe2015-01-17 16:53:12 -050045#include "tw_atomic.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050046#include "gui/gui.hpp"
Ethan Yonker472f5062016-02-25 13:47:30 -060047#include "progresstracking.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050048#include "adbbu/libtwadbbu.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040049
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040050#ifdef TW_HAS_MTP
51#include "mtp/mtp_MtpServer.hpp"
52#include "mtp/twrpMtp.hpp"
Ethan Yonker726a0202014-12-16 20:01:38 -060053#include "mtp/MtpMessage.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040054#endif
55
Dees Troy6f6441d2014-01-23 02:07:03 +000056extern "C" {
57 #include "cutils/properties.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050058 #include "gui/gui.h"
Dees Troy6f6441d2014-01-23 02:07:03 +000059}
60
Dees_Troy5bf43922012-09-07 16:07:55 -040061#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060062 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060063 #include "gui/rapidxml.hpp"
64 #include "gui/pages.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040065#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040066
Ethan Yonker1b190162016-12-05 15:25:19 -060067#ifdef AB_OTA_UPDATER
68#include <hardware/hardware.h>
69#include <hardware/boot_control.h>
70#endif
71
Ethan Yonker6277c792014-09-15 14:54:30 -050072extern bool datamedia;
73
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050074TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040075 mtp_was_enabled = false;
Ethan Yonker726a0202014-12-16 20:01:38 -060076 mtp_write_fd = -1;
bigbiff7abc5fe2015-01-17 16:53:12 -050077 stop_backup.set_value(0);
Ethan Yonker1b190162016-12-05 15:25:19 -060078#ifdef AB_OTA_UPDATER
79 char slot_suffix[PROPERTY_VALUE_MAX];
80 property_get("ro.boot.slot_suffix", slot_suffix, "_a");
81 Active_Slot_Display = "";
82 if (strcmp(slot_suffix, "_a") == 0)
83 Set_Active_Slot("A");
84 else
85 Set_Active_Slot("B");
86#endif
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050087}
88
Dees_Troy51a0e822012-09-05 15:24:24 -040089int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040090 FILE *fstabFile;
91 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000092 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050093 TWPartition* andsec_partition = NULL;
Ethan Yonker726a0202014-12-16 20:01:38 -060094 unsigned int storageid = 1 << 16; // upper 16 bits are for physical storage device, we pretend to have only one
Dees_Troy5bf43922012-09-07 16:07:55 -040095
96 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
97 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000098 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040099 return false;
100 }
101
102 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
103 if (fstab_line[0] != '/')
104 continue;
105
Dees_Troy2a923582012-09-20 12:13:34 -0400106 if (fstab_line[strlen(fstab_line) - 1] != '\n')
107 fstab_line[strlen(fstab_line)] = '\n';
Dees_Troy2a923582012-09-20 12:13:34 -0400108
Matt Mower2b2dd152016-04-26 11:24:08 -0500109 TWPartition* partition = new TWPartition();
Matt Mower72c87ce2016-04-26 14:34:56 -0500110 if (partition->Process_Fstab_Line(fstab_line, Display_Error))
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500111 Partitions.push_back(partition);
Matt Mower72c87ce2016-04-26 14:34:56 -0500112 else
Dees_Troy5bf43922012-09-07 16:07:55 -0400113 delete partition;
Matt Mower2b2dd152016-04-26 11:24:08 -0500114
115 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy5bf43922012-09-07 16:07:55 -0400116 }
117 fclose(fstabFile);
Matt Mower72c87ce2016-04-26 14:34:56 -0500118
119 std::vector<TWPartition*>::iterator iter;
120 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
121 (*iter)->Partition_Post_Processing(Display_Error);
122
123 if ((*iter)->Is_Storage) {
124 ++storageid;
125 (*iter)->MTP_Storage_ID = storageid;
126 }
127
128 if (!settings_partition && (*iter)->Is_Settings_Storage && (*iter)->Is_Present)
129 settings_partition = (*iter);
130 else
131 (*iter)->Is_Settings_Storage = false;
132
133 if (!andsec_partition && (*iter)->Has_Android_Secure && (*iter)->Is_Present)
134 andsec_partition = (*iter);
135 else
136 (*iter)->Has_Android_Secure = false;
137 }
138
Ethan Yonker6277c792014-09-15 14:54:30 -0500139 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) {
140 // Attempt to automatically identify /data/media emulated storage devices
141 TWPartition* Dat = Find_Partition_By_Path("/data");
142 if (Dat) {
143 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
144 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200145 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500146 settings_partition = Dat;
Ethan Yonker726a0202014-12-16 20:01:38 -0600147 // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID
148 ++storageid;
149 Dat->MTP_Storage_ID = storageid;
Ethan Yonker6277c792014-09-15 14:54:30 -0500150 }
151 }
Dees Troy02a64532014-03-19 15:23:32 +0000152 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500153 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
154 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000155 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500156 break;
157 }
158 }
Dees Troy02a64532014-03-19 15:23:32 +0000159 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000160 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500161 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400162 if (!Write_Fstab()) {
163 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000164 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400165 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000166 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 }
Matt Mowered71fa32014-04-16 13:21:47 -0500168
Matt Mowerbf4efa32014-04-14 23:25:26 -0500169 if (andsec_partition) {
170 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500171 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500172 Setup_Android_Secure_Location(settings_partition);
173 }
Matt Mowered71fa32014-04-16 13:21:47 -0500174 if (settings_partition) {
175 Setup_Settings_Storage_Partition(settings_partition);
176 }
Ethan Yonker253368a2014-11-25 15:00:52 -0600177#ifdef TW_INCLUDE_CRYPTO
Ethan Yonkercceebb82014-11-18 10:17:59 -0600178 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
179 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
180 int password_type = cryptfs_get_password_type();
181 if (password_type == CRYPT_TYPE_DEFAULT) {
182 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
183 if (Decrypt_Device("default_password") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500184 gui_msg("decrypt_success=Successfully decrypted with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600185 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
186 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500187 gui_err("unable_to_decrypt=Unable to decrypt with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600188 }
189 } else {
190 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
191 }
192 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600193 if (Decrypt_Data && (!Decrypt_Data->Is_Encrypted || Decrypt_Data->Is_Decrypted) && Decrypt_Data->Mount(false)) {
194 Decrypt_Adopted();
195 }
Ethan Yonkercceebb82014-11-18 10:17:59 -0600196#endif
Dees_Troy51127312012-09-08 13:08:49 -0400197 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400198 UnMount_Main_Partitions();
Ethan Yonker1b190162016-12-05 15:25:19 -0600199#ifdef AB_OTA_UPDATER
200 DataManager::SetValue("tw_active_slot", Get_Active_Slot_Display());
201#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400202 return true;
203}
204
205int TWPartitionManager::Write_Fstab(void) {
206 FILE *fp;
207 std::vector<TWPartition*>::iterator iter;
208 string Line;
209
210 fp = fopen("/etc/fstab", "w");
211 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000212 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400213 return false;
214 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400215 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400216 if ((*iter)->Can_Be_Mounted) {
dianlujitao7d304c72016-01-02 12:15:50 +0800217 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw 0 0\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000219 }
220 // Handle subpartition tracking
221 if ((*iter)->Is_SubPartition) {
222 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
223 if (ParentPartition)
224 ParentPartition->Has_SubPartition = true;
225 else
226 LOGERR("Unable to locate parent partition '%s' of '%s'\n", (*iter)->SubPartition_Of.c_str(), (*iter)->Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 }
228 }
229 fclose(fp);
230 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400231}
232
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500233void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500234 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
235 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
236 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
237}
238
Matt Mowerbf4efa32014-04-14 23:25:26 -0500239void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
240 if (Part->Has_Android_Secure)
241 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500242 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500243 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500244}
245
Dees_Troy8170a922012-09-18 15:40:25 -0400246void TWPartitionManager::Output_Partition_Logging(void) {
247 std::vector<TWPartition*>::iterator iter;
248
249 printf("\n\nPartition Logs:\n");
250 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
251 Output_Partition((*iter));
252}
253
254void TWPartitionManager::Output_Partition(TWPartition* Part) {
255 unsigned long long mb = 1048576;
256
Gary Peck004d48b2012-11-21 16:28:18 -0800257 printf("%s | %s | Size: %iMB", Part->Mount_Point.c_str(), Part->Actual_Block_Device.c_str(), (int)(Part->Size / mb));
Dees_Troy8170a922012-09-18 15:40:25 -0400258 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800259 printf(" Used: %iMB Free: %iMB Backup Size: %iMB", (int)(Part->Used / mb), (int)(Part->Free / mb), (int)(Part->Backup_Size / mb));
Dees_Troy8170a922012-09-18 15:40:25 -0400260 }
Gary Peck004d48b2012-11-21 16:28:18 -0800261 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500262 if (Part->Can_Be_Mounted)
263 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800264 if (Part->Can_Be_Wiped)
265 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700266 if (Part->Use_Rm_Rf)
267 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500268 if (Part->Can_Be_Backed_Up)
269 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800270 if (Part->Wipe_During_Factory_Reset)
271 printf("Wipe_During_Factory_Reset ");
272 if (Part->Wipe_Available_in_GUI)
273 printf("Wipe_Available_in_GUI ");
274 if (Part->Is_SubPartition)
275 printf("Is_SubPartition ");
276 if (Part->Has_SubPartition)
277 printf("Has_SubPartition ");
278 if (Part->Removable)
279 printf("Removable ");
280 if (Part->Is_Present)
281 printf("IsPresent ");
282 if (Part->Can_Be_Encrypted)
283 printf("Can_Be_Encrypted ");
284 if (Part->Is_Encrypted)
285 printf("Is_Encrypted ");
286 if (Part->Is_Decrypted)
287 printf("Is_Decrypted ");
288 if (Part->Has_Data_Media)
289 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000290 if (Part->Can_Encrypt_Backup)
291 printf("Can_Encrypt_Backup ");
292 if (Part->Use_Userdata_Encryption)
293 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800294 if (Part->Has_Android_Secure)
295 printf("Has_Android_Secure ");
296 if (Part->Is_Storage)
297 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500298 if (Part->Is_Settings_Storage)
299 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000300 if (Part->Ignore_Blkid)
301 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000302 if (Part->Retain_Layout_Version)
303 printf("Retain_Layout_Version ");
Ethan Yonker253368a2014-11-25 15:00:52 -0600304 if (Part->Mount_To_Decrypt)
305 printf("Mount_To_Decrypt ");
Ethan Yonker96af84a2015-01-05 14:58:36 -0600306 if (Part->Can_Flash_Img)
307 printf("Can_Flash_Img ");
Ethan Yonker66a19492015-12-10 10:19:45 -0600308 if (Part->Is_Adopted_Storage)
309 printf("Is_Adopted_Storage ");
Ethan Yonker1b190162016-12-05 15:25:19 -0600310 if (Part->SlotSelect)
311 printf("SlotSelect ");
Gary Peck004d48b2012-11-21 16:28:18 -0800312 printf("\n");
313 if (!Part->SubPartition_Of.empty())
314 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
315 if (!Part->Symlink_Path.empty())
316 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
317 if (!Part->Symlink_Mount_Point.empty())
318 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
319 if (!Part->Primary_Block_Device.empty())
320 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
321 if (!Part->Alternate_Block_Device.empty())
322 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
323 if (!Part->Decrypted_Block_Device.empty())
324 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
Ethan Yonker253368a2014-11-25 15:00:52 -0600325 if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer")
326 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800327 if (Part->Length != 0)
328 printf(" Length: %i\n", Part->Length);
329 if (!Part->Display_Name.empty())
330 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500331 if (!Part->Storage_Name.empty())
332 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800333 if (!Part->Backup_Path.empty())
334 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
335 if (!Part->Backup_Name.empty())
336 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500337 if (!Part->Backup_Display_Name.empty())
338 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800339 if (!Part->Backup_FileName.empty())
340 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
341 if (!Part->Storage_Path.empty())
342 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
343 if (!Part->Current_File_System.empty())
344 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
345 if (!Part->Fstab_File_System.empty())
346 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
347 if (Part->Format_Block_Size != 0)
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500348 printf(" Format_Block_Size: %lu\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400349 if (!Part->MTD_Name.empty())
350 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400351 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500352 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800353 if (Part->Mount_Flags || !Part->Mount_Options.empty())
Matt Mower4ab42b12016-04-21 13:52:18 -0500354 printf(" Mount_Options: %s\n", Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600355 if (Part->MTP_Storage_ID)
356 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker6277c792014-09-15 14:54:30 -0500357 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400358}
359
Dees_Troy51a0e822012-09-05 15:24:24 -0400360int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400361 std::vector<TWPartition*>::iterator iter;
362 int ret = false;
363 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400364 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400365
Dees_Troyd93bda52013-07-03 19:55:19 +0000366 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400367 return true;
368
Dees_Troy5bf43922012-09-07 16:07:55 -0400369 // Iterate through all partitions
370 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400371 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400372 ret = (*iter)->Mount(Display_Error);
373 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400374 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400375 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400376 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400377 }
378 if (found) {
379 return ret;
380 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500381 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Dees_Troy5bf43922012-09-07 16:07:55 -0400382 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000383 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400384 }
385 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400386}
387
Dees_Troy51a0e822012-09-05 15:24:24 -0400388int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400389 std::vector<TWPartition*>::iterator iter;
390 int ret = false;
391 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400392 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400393
394 // Iterate through all partitions
395 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400396 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400397 ret = (*iter)->UnMount(Display_Error);
398 found = true;
399 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
400 (*iter)->UnMount(Display_Error);
401 }
402 }
403 if (found) {
404 return ret;
405 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500406 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Dees_Troy51127312012-09-08 13:08:49 -0400407 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000408 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400409 }
410 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400411}
412
Dees_Troy51a0e822012-09-05 15:24:24 -0400413int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400414 TWPartition* Part = Find_Partition_By_Path(Path);
415
416 if (Part)
417 return Part->Is_Mounted();
418 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000419 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400420 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400421}
422
Dees_Troy5bf43922012-09-07 16:07:55 -0400423int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400424 string current_storage_path = DataManager::GetCurrentStoragePath();
425
426 if (Mount_By_Path(current_storage_path, Display_Error)) {
427 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
428 if (FreeStorage)
429 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
430 return true;
431 }
432 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400433}
434
Dees_Troy5bf43922012-09-07 16:07:55 -0400435int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
436 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
437}
438
439TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
440 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400441 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400442
443 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400444 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400445 return (*iter);
446 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 return NULL;
448}
449
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400450int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
451 // Check the backup name to ensure that it is the correct size and contains only valid characters
452 // and that a backup with that name doesn't already exist
453 char backup_name[MAX_BACKUP_NAME_LEN];
454 char backup_loc[255], tw_image_dir[255];
455 int copy_size;
456 int index, cur_char;
457 string Backup_Name, Backup_Loc;
458
459 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
460 copy_size = Backup_Name.size();
461 // Check size
462 if (copy_size > MAX_BACKUP_NAME_LEN) {
463 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500464 gui_err("backup_name_len=Backup name is too long.");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400465 return -2;
466 }
467
468 // Check each character
469 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500470 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400471 return 0; // A "0" (zero) means to use the current timestamp for the backup name
472 for (index=0; index<copy_size; index++) {
473 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500474 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_Troyc9ff7a32012-09-27 10:09:41 -0400475 // These are valid characters
476 // Numbers
477 // Upper case letters
478 // Lower case letters
479 // Space
480 // and -_.{}[]
481 } else {
482 if (Display_Error)
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600483 gui_msg(Msg(msg::kError, "backup_name_invalid=Backup name '{1}' contains invalid character: '{1}'")(Backup_Name)((char)cur_char));
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400484 return -3;
485 }
486 }
487
488 // Check to make sure that a backup with this name doesn't already exist
489 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
490 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500491 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500492 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400493 if (Display_Error)
Matt Mower3c366972015-12-25 19:28:31 -0600494 gui_err("backup_name_exists=A backup with that name already exists!");
bigbiffce8f83c2015-12-12 18:30:21 -0500495
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400496 return -4;
497 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400498 // No problems found, return 0
499 return 0;
500}
501
bigbiff bigbiffa481d362016-08-08 20:36:10 -0400502bool TWPartitionManager::Make_MD5(PartitionSettings *part_settings)
Dees_Troy43d8b002012-09-17 16:00:01 -0400503{
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500504 string command, result;
505
506 if (part_settings->Part == NULL)
507 return false;
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500508 string Full_File = part_settings->Backup_Folder + "/" + part_settings->Part->Backup_FileName;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500509 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400510
bigbiff bigbiffa481d362016-08-08 20:36:10 -0400511 if (!part_settings->generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400512 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400513
Ethan Yonker74db1572015-10-28 12:44:49 -0500514 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, gui_parse_text("{@generating_md51}"));
515 gui_msg("generating_md52= * Generating md5...");
Dees_Troy43d8b002012-09-17 16:00:01 -0400516 if (TWFunc::Path_Exists(Full_File)) {
bigbiffce8f83c2015-12-12 18:30:21 -0500517 md5sum.setfn(Full_File);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500518 if (md5sum.computeMD5() == 0)
519 if (md5sum.write_md5digest() == 0)
Ethan Yonker74db1572015-10-28 12:44:49 -0500520 gui_msg("md5_created= * MD5 Created.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500521 else
522 return -1;
523 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500524 gui_err("md5_error= * MD5 Error!");
Dees_Troy43d8b002012-09-17 16:00:01 -0400525 } else {
526 char filename[512];
527 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500528 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400529 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500530 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000531 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400532 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000533 if (TWFunc::Path_Exists(filename)) {
534 if (md5sum.computeMD5() == 0) {
535 if (md5sum.write_md5digest() != 0)
536 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500537 gui_err("md5_error= * MD5 Error!");
Dees_Troy83bd4832013-05-04 12:39:56 +0000538 return false;
539 }
540 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500541 gui_err("md5_compute_error= * Error computing MD5.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500542 return false;
543 }
544 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400545 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400546 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500547 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400548 }
549 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000550 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400551 return false;
552 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500553 gui_msg("md5_created= * MD5 Created.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400554 }
555 return true;
556}
557
bigbiffce8f83c2015-12-12 18:30:21 -0500558
559bool TWPartitionManager::Backup_Partition(PartitionSettings *part_settings) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400560 time_t start, stop;
bigbiffce8f83c2015-12-12 18:30:21 -0500561 int use_compression, adb_control_bu_fd;
562 string backup_log = part_settings->Backup_Folder + "/recovery.log";
Dees_Troy43d8b002012-09-17 16:00:01 -0400563
bigbiffce8f83c2015-12-12 18:30:21 -0500564 if (part_settings->Part == NULL)
Dees_Troy43d8b002012-09-17 16:00:01 -0400565 return true;
566
Dees_Troy093b7642012-09-21 15:59:38 -0400567 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400568
Tom Hite5a926722014-09-15 01:31:03 +0000569 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400570 time(&start);
571
bigbiffce8f83c2015-12-12 18:30:21 -0500572 if (part_settings->Part->Backup(part_settings, &tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000573 bool md5Success = false;
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400574 if (part_settings->adbbackup) {
575 md5Success = true;
576 }
577 else
578 md5Success = Make_MD5(part_settings);
579
580 TWFunc::SetPerformanceMode(false);
bigbiffce8f83c2015-12-12 18:30:21 -0500581 if (part_settings->Part->Has_SubPartition) {
Dees_Troy8170a922012-09-18 15:40:25 -0400582 std::vector<TWPartition*>::iterator subpart;
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400583 TWPartition *parentPart = part_settings->Part;
Dees_Troy8170a922012-09-18 15:40:25 -0400584
585 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400586 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500587 part_settings->Part = *subpart;
bigbiffce8f83c2015-12-12 18:30:21 -0500588 if (!(*subpart)->Backup(part_settings, &tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000589 TWFunc::SetPerformanceMode(false);
bigbiffce8f83c2015-12-12 18:30:21 -0500590 Clean_Backup_Folder(part_settings->Backup_Folder);
bigbiffbf1d6722015-02-14 16:25:59 -0500591 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
592 tw_set_default_metadata(backup_log.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400593 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000594 }
Dees_Troy2727b992013-08-14 20:09:30 +0000595 sync();
596 sync();
bigbiffce8f83c2015-12-12 18:30:21 -0500597 if (!part_settings->adbbackup) {
bigbiff bigbiffa481d362016-08-08 20:36:10 -0400598 if (!Make_MD5(part_settings)) {
bigbiffce8f83c2015-12-12 18:30:21 -0500599 TWFunc::SetPerformanceMode(false);
600 return false;
601 }
Tom Hite5a926722014-09-15 01:31:03 +0000602 }
Dees_Troy8170a922012-09-18 15:40:25 -0400603 }
604 }
605 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400606 time(&stop);
that203bcc92015-05-09 17:27:33 +0200607 int backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000608 LOGINFO("Partition Backup time: %d\n", backup_time);
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400609
bigbiffce8f83c2015-12-12 18:30:21 -0500610 if (part_settings->Part->Backup_Method == BM_FILES) {
611 part_settings->file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400612 } else {
bigbiffce8f83c2015-12-12 18:30:21 -0500613 part_settings->img_time += backup_time;
614
Dees_Troy43d8b002012-09-17 16:00:01 -0400615 }
Tom Hite5a926722014-09-15 01:31:03 +0000616
Tom Hite5a926722014-09-15 01:31:03 +0000617 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400618 } else {
bigbiffce8f83c2015-12-12 18:30:21 -0500619 Clean_Backup_Folder(part_settings->Backup_Folder);
bigbiffbf1d6722015-02-14 16:25:59 -0500620 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
621 tw_set_default_metadata(backup_log.c_str());
Tom Hite5a926722014-09-15 01:31:03 +0000622 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400623 return false;
624 }
bigbiff7abc5fe2015-01-17 16:53:12 -0500625 return 0;
626}
627
bigbiffbf1d6722015-02-14 16:25:59 -0500628void TWPartitionManager::Clean_Backup_Folder(string Backup_Folder) {
629 DIR *d = opendir(Backup_Folder.c_str());
630 struct dirent *p;
631 int r;
632
Ethan Yonker74db1572015-10-28 12:44:49 -0500633 gui_msg("backup_clean=Backup Failed. Cleaning Backup Folder.");
bigbiffbf1d6722015-02-14 16:25:59 -0500634
635 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500636 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Backup_Folder)(strerror(errno)));
bigbiffbf1d6722015-02-14 16:25:59 -0500637 return;
638 }
639
Matt Mower2b18a532015-02-20 16:58:05 -0600640 while ((p = readdir(d))) {
bigbiffbf1d6722015-02-14 16:25:59 -0500641 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
642 continue;
643
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500644 string path = Backup_Folder + "/" + p->d_name;
bigbiffbf1d6722015-02-14 16:25:59 -0500645
646 size_t dot = path.find_last_of(".") + 1;
647 if (path.substr(dot) == "win" || path.substr(dot) == "md5" || path.substr(dot) == "info") {
648 r = unlink(path.c_str());
649 if (r != 0) {
650 LOGINFO("Unable to unlink '%s: %s'\n", path.c_str(), strerror(errno));
651 }
652 }
653 }
654 closedir(d);
655}
656
Ethan Yonker472f5062016-02-25 13:47:30 -0600657int TWPartitionManager::Check_Backup_Cancel() {
658 return stop_backup.get_value();
659}
660
bigbiff7abc5fe2015-01-17 16:53:12 -0500661int TWPartitionManager::Cancel_Backup() {
662 string Backup_Folder, Backup_Name, Full_Backup_Path;
663
664 stop_backup.set_value(1);
665
666 if (tar_fork_pid != 0) {
667 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
668 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500669 Full_Backup_Path = Backup_Folder + "/" + Backup_Name;
bigbiff7abc5fe2015-01-17 16:53:12 -0500670 LOGINFO("Killing pid: %d\n", tar_fork_pid);
671 kill(tar_fork_pid, SIGUSR2);
672 while (kill(tar_fork_pid, 0) == 0) {
673 usleep(1000);
674 }
675 LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n");
676 LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str());
677 TWFunc::removeDir(Full_Backup_Path, false);
678 tar_fork_pid = 0;
679 }
680
681 return 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400682}
683
bigbiffce8f83c2015-12-12 18:30:21 -0500684int TWPartitionManager::Run_Backup(bool adbbackup) {
685 PartitionSettings part_settings;
686 int check, partition_count = 0, disable_free_space_check = 0, do_md5 = 0;
687 int gui_adb_backup;
688 string Backup_Name, Backup_List, backup_path;
689 unsigned long long total_bytes = 0, free_space = 0, subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400690 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400691 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400692 struct tm *t;
693 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500694 size_t start_pos = 0, end_pos = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500695 stop_backup.set_value(0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400696 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500697 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400698
bigbiffce8f83c2015-12-12 18:30:21 -0500699 part_settings.img_bytes_remaining = 0;
700 part_settings.file_bytes_remaining = 0;
701 part_settings.img_time = 0;
702 part_settings.file_time = 0;
703 part_settings.img_bytes = 0;
704 part_settings.file_bytes = 0;
705 part_settings.PM_Method = PM_BACKUP;
706
707 DataManager::GetValue("tw_enable_adb_backup", gui_adb_backup);
708 if (gui_adb_backup == true)
709 adbbackup = true;
710
711 part_settings.adbbackup = adbbackup;
Dees_Troy43d8b002012-09-17 16:00:01 -0400712 time(&total_start);
713
714 Update_System_Details();
715
716 if (!Mount_Current_Storage(true))
717 return false;
718
bigbiffce8f83c2015-12-12 18:30:21 -0500719
Dees_Troy43d8b002012-09-17 16:00:01 -0400720 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400721 if (do_md5 == 0)
bigbiffce8f83c2015-12-12 18:30:21 -0500722 part_settings.generate_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400723 else
bigbiffce8f83c2015-12-12 18:30:21 -0500724 part_settings.generate_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400725
bigbiffce8f83c2015-12-12 18:30:21 -0500726 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, part_settings.Backup_Folder);
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500727 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
728 if (Backup_Name == gui_lookup("curr_date", "(Current Date)")) {
729 Backup_Name = TWFunc::Get_Current_Date();
730 } else if (Backup_Name == gui_lookup("auto_generate", "(Auto Generate)") || Backup_Name == "0" || Backup_Name.empty()) {
Dees Troyb21cc642013-09-10 17:36:41 +0000731 TWFunc::Auto_Generate_Backup_Name();
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500732 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400733 }
bigbiffce8f83c2015-12-12 18:30:21 -0500734
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500735 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500736 part_settings.Backup_Folder = part_settings.Backup_Folder + "/" + Backup_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500737
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500738 LOGINFO("Backup_Folder is: '%s'\n", part_settings.Backup_Folder.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400739
Dees_Troy2673cec2013-04-02 20:22:16 +0000740 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500741 DataManager::GetValue("tw_backup_list", Backup_List);
742 if (!Backup_List.empty()) {
743 end_pos = Backup_List.find(";", start_pos);
744 while (end_pos != string::npos && start_pos < Backup_List.size()) {
745 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -0500746 part_settings.Part = Find_Partition_By_Path(backup_path);
747 if (part_settings.Part != NULL) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500748 partition_count++;
bigbiffce8f83c2015-12-12 18:30:21 -0500749 if (part_settings.Part->Backup_Method == BM_FILES)
750 part_settings.file_bytes += part_settings.Part->Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -0500751 else
bigbiffce8f83c2015-12-12 18:30:21 -0500752 part_settings.img_bytes += part_settings.Part->Backup_Size;
753 if (part_settings.Part->Has_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500754 std::vector<TWPartition*>::iterator subpart;
755
756 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiffce8f83c2015-12-12 18:30:21 -0500757 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == part_settings.Part->Mount_Point) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500758 partition_count++;
bigbiffce8f83c2015-12-12 18:30:21 -0500759 if ((*subpart)->Backup_Method == BM_FILES)
760 part_settings.file_bytes += (*subpart)->Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -0500761 else
bigbiffce8f83c2015-12-12 18:30:21 -0500762 part_settings.img_bytes += (*subpart)->Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -0500763 }
764 }
Dees_Troy8170a922012-09-18 15:40:25 -0400765 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500766 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500767 gui_msg(Msg(msg::kError, "unable_to_locate_partition=Unable to locate '{1}' partition for backup calculations.")(backup_path));
Dees_Troy8170a922012-09-18 15:40:25 -0400768 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500769 start_pos = end_pos + 1;
770 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400771 }
772 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400773
774 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500775 gui_msg("no_partition_selected=No partitions selected for backup.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400776 return false;
777 }
bigbiffce8f83c2015-12-12 18:30:21 -0500778 if (adbbackup) {
779 if (twadbbu::Write_ADB_Stream_Header(partition_count) == false) {
780 return false;
781 }
782 }
783 total_bytes = part_settings.file_bytes + part_settings.img_bytes;
Ethan Yonker472f5062016-02-25 13:47:30 -0600784 ProgressTracking progress(total_bytes);
bigbiffce8f83c2015-12-12 18:30:21 -0500785 part_settings.progress = &progress;
786
Ethan Yonker74db1572015-10-28 12:44:49 -0500787 gui_msg(Msg("total_partitions_backup= * Total number of partitions to back up: {1}")(partition_count));
788 gui_msg(Msg("total_backup_size= * Total size of all data: {1}MB")(total_bytes / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400789 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
790 if (storage != NULL) {
791 free_space = storage->Free;
Ethan Yonker74db1572015-10-28 12:44:49 -0500792 gui_msg(Msg("available_space= * Available space: {1}MB")(free_space / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400793 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500794 gui_err("unable_locate_storage=Unable to locate storage device.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400795 return false;
796 }
bigbiffbf1d6722015-02-14 16:25:59 -0500797
Matt Mowerbfccfb82016-04-25 23:22:31 -0500798 DataManager::GetValue(TW_DISABLE_FREE_SPACE_VAR, disable_free_space_check);
bigbiffce8f83c2015-12-12 18:30:21 -0500799
800 if (adbbackup)
801 disable_free_space_check = true;
802
bigbiffbf1d6722015-02-14 16:25:59 -0500803 if (!disable_free_space_check) {
804 if (free_space - (32 * 1024 * 1024) < total_bytes) {
805 // We require an extra 32MB just in case
Ethan Yonker74db1572015-10-28 12:44:49 -0500806 gui_err("no_space=Not enough free space on storage.");
bigbiffbf1d6722015-02-14 16:25:59 -0500807 return false;
808 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400809 }
bigbiffce8f83c2015-12-12 18:30:21 -0500810 part_settings.img_bytes_remaining = part_settings.img_bytes;
811 part_settings.file_bytes_remaining = part_settings.file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400812
Ethan Yonker74db1572015-10-28 12:44:49 -0500813 gui_msg("backup_started=[BACKUP STARTED]");
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500814 gui_msg(Msg("backup_folder= * Backup Folder: {1}")(part_settings.Backup_Folder));
815 if (!TWFunc::Recursive_Mkdir(part_settings.Backup_Folder)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500816 gui_err("fail_backup_folder=Failed to make backup folder.");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000817 return false;
818 }
819
Dees_Troy2673cec2013-04-02 20:22:16 +0000820 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400821
Dees_Troya13d74f2013-03-24 08:54:55 -0500822 start_pos = 0;
823 end_pos = Backup_List.find(";", start_pos);
824 while (end_pos != string::npos && start_pos < Backup_List.size()) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500825 if (stop_backup.get_value() != 0)
826 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500827 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -0500828 part_settings.Part = Find_Partition_By_Path(backup_path);
829 if (part_settings.Part != NULL) {
830 if (!Backup_Partition(&part_settings))
831
Dees_Troya13d74f2013-03-24 08:54:55 -0500832 return false;
833 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500834 gui_msg(Msg(msg::kError, "unable_to_locate_partition=Unable to locate '{1}' partition for backup calculations.")(backup_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500835 }
836 start_pos = end_pos + 1;
837 end_pos = Backup_List.find(";", start_pos);
838 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400839
840 // Average BPS
bigbiffce8f83c2015-12-12 18:30:21 -0500841 if (part_settings.img_time == 0)
842 part_settings.img_time = 1;
843 if (part_settings.file_time == 0)
844 part_settings.file_time = 1;
845 int img_bps = (int)part_settings.img_bytes / (int)part_settings.img_time;
846 unsigned long long file_bps = part_settings.file_bytes / (int)part_settings.file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400847
bigbiffce8f83c2015-12-12 18:30:21 -0500848 if (part_settings.file_bytes != 0)
Ethan Yonker472f5062016-02-25 13:47:30 -0600849 gui_msg(Msg("avg_backup_fs=Average backup rate for file systems: {1} MB/sec")(file_bps / (1024 * 1024)));
bigbiffce8f83c2015-12-12 18:30:21 -0500850 if (part_settings.img_bytes != 0)
Ethan Yonker472f5062016-02-25 13:47:30 -0600851 gui_msg(Msg("avg_backup_img=Average backup rate for imaged drives: {1} MB/sec")(img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400852
853 time(&total_stop);
854 int total_time = (int) difftime(total_stop, total_start);
bigbiffce8f83c2015-12-12 18:30:21 -0500855
856 uint64_t actual_backup_size;
Ethan Yonker3fdcda42016-11-30 12:29:37 -0600857 if (!adbbackup) {
858 TWExclude twe;
859 actual_backup_size = twe.Get_Folder_Size(part_settings.Backup_Folder);
860 } else
bigbiffce8f83c2015-12-12 18:30:21 -0500861 actual_backup_size = part_settings.file_bytes + part_settings.img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500862 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400863
bigbiffce8f83c2015-12-12 18:30:21 -0500864 int prev_img_bps = 0, use_compression = 0;
865 unsigned long long prev_file_bps = 0;
Dees_Troy093b7642012-09-21 15:59:38 -0400866 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500867 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400868
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500869 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400870 if (use_compression)
871 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500872 else
Dees_Troy093b7642012-09-21 15:59:38 -0400873 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
874 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500875 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400876
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500877 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400878 if (use_compression)
879 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
880 else
881 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
882
Ethan Yonker74db1572015-10-28 12:44:49 -0500883 gui_msg(Msg("total_backed_size=[{1} MB TOTAL BACKED UP]")(actual_backup_size));
Dees_Troy43d8b002012-09-17 16:00:01 -0400884 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400885 UnMount_Main_Partitions();
Ethan Yonker56db1c42015-12-20 22:49:00 -0600886 gui_msg(Msg(msg::kHighlight, "backup_completed=[BACKUP COMPLETED IN {1} SECONDS]")(total_time)); // the end
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500887 string backup_log = part_settings.Backup_Folder + "/recovery.log";
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000888 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600889 tw_set_default_metadata(backup_log.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -0500890
891 if (part_settings.adbbackup) {
892 if (twadbbu::Write_ADB_Stream_Trailer() == false) {
893 return false;
894 }
895 }
896 part_settings.adbbackup = false;
897 DataManager::SetValue("tw_enable_adb_backup", 0);
898
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000899 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400900}
901
bigbiffce8f83c2015-12-12 18:30:21 -0500902bool TWPartitionManager::Restore_Partition(PartitionSettings *part_settings) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400903 time_t Start, Stop;
bigbiffce8f83c2015-12-12 18:30:21 -0500904
Tom Hite5a926722014-09-15 01:31:03 +0000905 TWFunc::SetPerformanceMode(true);
bigbiffce8f83c2015-12-12 18:30:21 -0500906
Dees_Troy4a2a1262012-09-18 09:33:47 -0400907 time(&Start);
Ethan Yonker472f5062016-02-25 13:47:30 -0600908
bigbiffce8f83c2015-12-12 18:30:21 -0500909
910 if (!part_settings->Part->Restore(part_settings)) {
Tom Hite5a926722014-09-15 01:31:03 +0000911 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400912 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000913 }
bigbiffce8f83c2015-12-12 18:30:21 -0500914 if (part_settings->Part->Has_SubPartition) {
Dees_Troy8170a922012-09-18 15:40:25 -0400915 std::vector<TWPartition*>::iterator subpart;
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400916 TWPartition *parentPart = part_settings->Part;
Dees_Troy8170a922012-09-18 15:40:25 -0400917
918 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400919 part_settings->Part = *subpart;
920 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) {
921 part_settings->Part = (*subpart);
bigbiffce8f83c2015-12-12 18:30:21 -0500922 if (!(*subpart)->Restore(part_settings)) {
Tom Hite5a926722014-09-15 01:31:03 +0000923 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400924 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000925 }
Dees_Troy8170a922012-09-18 15:40:25 -0400926 }
927 }
928 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400929 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000930 TWFunc::SetPerformanceMode(false);
bigbiffce8f83c2015-12-12 18:30:21 -0500931 gui_msg(Msg("restore_part_done=[{1} done ({2} seconds)]")(part_settings->Part->Backup_Display_Name)((int)difftime(Stop, Start)));
932
Dees_Troy4a2a1262012-09-18 09:33:47 -0400933 return true;
934}
935
Ethan Yonker472f5062016-02-25 13:47:30 -0600936int TWPartitionManager::Run_Restore(const string& Restore_Name) {
bigbiffce8f83c2015-12-12 18:30:21 -0500937 PartitionSettings part_settings;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400938 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500939 TWPartition* restore_part = NULL;
bigbiffce8f83c2015-12-12 18:30:21 -0500940
Dees_Troy4a2a1262012-09-18 09:33:47 -0400941 time_t rStart, rStop;
942 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500943 string Restore_List, restore_path;
944 size_t start_pos = 0, end_pos;
bigbiffce8f83c2015-12-12 18:30:21 -0500945
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500946 part_settings.Backup_Folder = Restore_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500947 part_settings.Part = NULL;
948 part_settings.partition_count = 0;
949 part_settings.total_restore_size = 0;
950 part_settings.adbbackup = false;
951 part_settings.PM_Method = PM_RESTORE;
Dees_Troy43d8b002012-09-17 16:00:01 -0400952
Ethan Yonker74db1572015-10-28 12:44:49 -0500953 gui_msg("restore_started=[RESTORE STARTED]");
954 gui_msg(Msg("restore_folder=Restore folder: '{1}'")(Restore_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -0400955
Dees_Troy4a2a1262012-09-18 09:33:47 -0400956 if (!Mount_Current_Storage(true))
957 return false;
958
959 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500960 if (check_md5 > 0) {
961 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500962 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, gui_parse_text("{@verifying_md5}"));
963 gui_msg("verifying_md5=Verifying MD5");
Dees_Troya13d74f2013-03-24 08:54:55 -0500964 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500965 gui_msg("skip_md5=Skipping MD5 check based on user setting.");
Dees_Troya13d74f2013-03-24 08:54:55 -0500966 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500967 gui_msg("calc_restore=Calculating restore details...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500968 DataManager::GetValue("tw_restore_selected", Restore_List);
bigbiffce8f83c2015-12-12 18:30:21 -0500969
Dees_Troya13d74f2013-03-24 08:54:55 -0500970 if (!Restore_List.empty()) {
971 end_pos = Restore_List.find(";", start_pos);
972 while (end_pos != string::npos && start_pos < Restore_List.size()) {
973 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -0500974 part_settings.Part = Find_Partition_By_Path(restore_path);
975 if (part_settings.Part != NULL) {
bigbiffce8f83c2015-12-12 18:30:21 -0500976 if (part_settings.Part->Mount_Read_Only) {
977 gui_msg(Msg(msg::kError, "restore_read_only=Cannot restore {1} -- mounted read only.")(part_settings.Part->Backup_Display_Name));
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500978 return false;
979 }
bigbiffce8f83c2015-12-12 18:30:21 -0500980
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400981 if (check_md5 > 0 && !part_settings.Part->Check_MD5(&part_settings))
Dees_Troya13d74f2013-03-24 08:54:55 -0500982 return false;
bigbiffce8f83c2015-12-12 18:30:21 -0500983 part_settings.partition_count++;
984 part_settings.total_restore_size += part_settings.Part->Get_Restore_Size(&part_settings);
985 if (part_settings.Part->Has_SubPartition) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400986 TWPartition *parentPart = part_settings.Part;
Dees_Troya13d74f2013-03-24 08:54:55 -0500987 std::vector<TWPartition*>::iterator subpart;
988
989 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400990 part_settings.Part = *subpart;
991 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) {
992 if (check_md5 > 0 && !(*subpart)->Check_MD5(&part_settings))
Dees_Troya13d74f2013-03-24 08:54:55 -0500993 return false;
bigbiffce8f83c2015-12-12 18:30:21 -0500994 part_settings.total_restore_size += (*subpart)->Get_Restore_Size(&part_settings);
Dees_Troya13d74f2013-03-24 08:54:55 -0500995 }
996 }
997 }
998 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500999 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001000 }
1001 start_pos = end_pos + 1;
1002 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001003 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001004 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001005
bigbiffce8f83c2015-12-12 18:30:21 -05001006 if (part_settings.partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001007 gui_err("no_part_restore=No partitions selected for restore.");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001008 return false;
1009 }
1010
bigbiffce8f83c2015-12-12 18:30:21 -05001011 gui_msg(Msg("restore_part_count=Restoring {1} partitions...")(part_settings.partition_count));
1012 gui_msg(Msg("total_restore_size=Total restore size is {1}MB")(part_settings.total_restore_size / 1048576));
Dees_Troy2673cec2013-04-02 20:22:16 +00001013 DataManager::SetProgress(0.0);
bigbiffce8f83c2015-12-12 18:30:21 -05001014 ProgressTracking progress(part_settings.total_restore_size);
1015 part_settings.progress = &progress;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001016
Dees_Troya13d74f2013-03-24 08:54:55 -05001017 start_pos = 0;
1018 if (!Restore_List.empty()) {
1019 end_pos = Restore_List.find(";", start_pos);
1020 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1021 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -05001022
1023 part_settings.Part = Find_Partition_By_Path(restore_path);
1024 if (part_settings.Part != NULL) {
1025 part_settings.partition_count++;
bigbiffce8f83c2015-12-12 18:30:21 -05001026 if (!Restore_Partition(&part_settings))
Dees_Troya13d74f2013-03-24 08:54:55 -05001027 return false;
1028 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001029 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001030 }
1031 start_pos = end_pos + 1;
1032 end_pos = Restore_List.find(";", start_pos);
1033 }
1034 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001035 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, gui_parse_text("{@updating_system_details}"));
Dees_Troy43d8b002012-09-17 16:00:01 -04001036 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001037 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001038 time(&rStop);
Matt Mower3c366972015-12-25 19:28:31 -06001039 gui_msg(Msg(msg::kHighlight, "restore_completed=[RESTORE COMPLETED IN {1} SECONDS]")((int)difftime(rStop,rStart)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001040 DataManager::SetValue("tw_file_progress", "");
bigbiffce8f83c2015-12-12 18:30:21 -05001041
Dees_Troy63c8df72012-09-10 14:02:05 -04001042 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001043}
1044
1045void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001046 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -05001047 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +00001048 bool get_date = true, check_encryption = true;
1049
1050 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -04001051
1052 DIR* d;
1053 d = opendir(Restore_Name.c_str());
1054 if (d == NULL)
1055 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001056 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Name)(strerror(errno)));
Dees_Troy63c8df72012-09-10 14:02:05 -04001057 return;
1058 }
1059
1060 struct dirent* de;
1061 while ((de = readdir(d)) != NULL)
1062 {
1063 // Strip off three components
1064 char str[256];
1065 char* label;
1066 char* fstype = NULL;
1067 char* extn = NULL;
1068 char* ptr;
1069
1070 strcpy(str, de->d_name);
1071 if (strlen(str) <= 2)
1072 continue;
1073
1074 if (get_date) {
1075 char file_path[255];
1076 struct stat st;
1077
1078 strcpy(file_path, Restore_Name.c_str());
1079 strcat(file_path, "/");
1080 strcat(file_path, str);
1081 stat(file_path, &st);
1082 string backup_date = ctime((const time_t*)(&st.st_mtime));
1083 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1084 get_date = false;
1085 }
1086
1087 label = str;
1088 ptr = label;
1089 while (*ptr && *ptr != '.') ptr++;
1090 if (*ptr == '.')
1091 {
1092 *ptr = 0x00;
1093 ptr++;
1094 fstype = ptr;
1095 }
1096 while (*ptr && *ptr != '.') ptr++;
1097 if (*ptr == '.')
1098 {
1099 *ptr = 0x00;
1100 ptr++;
1101 extn = ptr;
1102 }
1103
Dees_Troy83bd4832013-05-04 12:39:56 +00001104 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001105 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001106 if (extnlength != 3 && extnlength != 6) continue;
1107 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1108 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1109
1110 if (check_encryption) {
1111 string filename = Restore_Name + "/";
1112 filename += de->d_name;
1113 if (TWFunc::Get_File_Type(filename) == 2) {
1114 LOGINFO("'%s' is encrypted\n", filename.c_str());
1115 DataManager::SetValue("tw_restore_encrypted", 1);
1116 }
1117 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001118 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001119
1120 TWPartition* Part = Find_Partition_By_Path(label);
1121 if (Part == NULL)
1122 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001123 gui_msg(Msg(msg::kError, "unable_locate_part_backup_name=Unable to locate partition by backup name: '{1}'")(label));
Dees_Troy63c8df72012-09-10 14:02:05 -04001124 continue;
1125 }
1126
1127 Part->Backup_FileName = de->d_name;
1128 if (strlen(extn) > 3) {
1129 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1130 }
1131
James Christopher Adduonofddbdfa2016-02-29 15:08:11 -05001132 if (!Part->Is_SubPartition)
1133 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001134 }
1135 closedir(d);
1136
Dees_Troya13d74f2013-03-24 08:54:55 -05001137 // Set the final value
1138 DataManager::SetValue("tw_restore_list", Restore_List);
1139 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001140 return;
1141}
1142
1143int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001144 std::vector<TWPartition*>::iterator iter;
1145 int ret = false;
1146 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001147 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001148
1149 // Iterate through all partitions
1150 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001151 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001152 if (Path == "/and-sec")
1153 ret = (*iter)->Wipe_AndSec();
1154 else
1155 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001156 found = true;
1157 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1158 (*iter)->Wipe();
1159 }
1160 }
1161 if (found) {
1162 return ret;
1163 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001164 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Dees_Troy63c8df72012-09-10 14:02:05 -04001165 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001166}
1167
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001168int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1169 std::vector<TWPartition*>::iterator iter;
1170 int ret = false;
1171 bool found = false;
1172 string Local_Path = TWFunc::Get_Root_Path(Path);
1173
1174 // Iterate through all partitions
1175 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1176 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1177 if (Path == "/and-sec")
1178 ret = (*iter)->Wipe_AndSec();
1179 else
1180 ret = (*iter)->Wipe(New_File_System);
1181 found = true;
1182 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1183 (*iter)->Wipe(New_File_System);
1184 }
1185 }
1186 if (found) {
1187 return ret;
1188 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001189 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001190 return false;
1191}
1192
Dees_Troy51a0e822012-09-05 15:24:24 -04001193int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001194 std::vector<TWPartition*>::iterator iter;
1195 int ret = true;
1196
1197 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001198 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Ethan Yonker89583ef2015-08-26 09:01:59 -05001199#ifdef TW_OEM_BUILD
1200 if ((*iter)->Mount_Point == "/data") {
1201 if (!(*iter)->Wipe_Encryption())
1202 ret = false;
1203 } else {
1204#endif
1205 if (!(*iter)->Wipe())
1206 ret = false;
1207#ifdef TW_OEM_BUILD
1208 }
1209#endif
Dees_Troy094207a2012-09-26 12:00:39 -04001210 } else if ((*iter)->Has_Android_Secure) {
1211 if (!(*iter)->Wipe_AndSec())
1212 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001213 }
1214 }
Ethan Yonker9fa76ba2016-06-30 14:05:43 -05001215 TWFunc::check_and_run_script("/sbin/factoryreset.sh", "Factory Reset Script");
Dees_Troy63c8df72012-09-10 14:02:05 -04001216 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001217}
1218
Dees_Troy38bd7602012-09-14 13:33:53 -04001219int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1220 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001221 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001222
1223 if (!Mount_By_Path("/data", true))
1224 return false;
1225
1226 if (!Mount_By_Path("/cache", true))
1227 return false;
1228
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001229 dir.push_back("/data/dalvik-cache");
1230 dir.push_back("/cache/dalvik-cache");
1231 dir.push_back("/cache/dc");
Ethan Yonker74db1572015-10-28 12:44:49 -05001232 gui_msg("wiping_dalvik=Wiping Dalvik Cache Directories...");
Dees_Troya13d74f2013-03-24 08:54:55 -05001233 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001234 if (stat(dir.at(i).c_str(), &st) == 0) {
1235 TWFunc::removeDir(dir.at(i), false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001236 gui_msg(Msg("cleaned=Cleaned: {1}...")(dir.at(i)));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001237 }
1238 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001239 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001240 if (sdext && sdext->Is_Present && sdext->Mount(false))
1241 {
1242 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1243 {
1244 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001245 gui_msg(Msg("cleaned=Cleaned: {1}...")("/sd-ext/dalvik-cache"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001246 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001247 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001248 gui_msg("dalvik_done=-- Dalvik Cache Directories Wipe Complete!");
Dees_Troy38bd7602012-09-14 13:33:53 -04001249 return true;
1250}
1251
1252int TWPartitionManager::Wipe_Rotate_Data(void) {
1253 if (!Mount_By_Path("/data", true))
1254 return false;
1255
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001256 unlink("/data/misc/akmd*");
1257 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001258 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001259 return true;
1260}
1261
1262int TWPartitionManager::Wipe_Battery_Stats(void) {
1263 struct stat st;
1264
1265 if (!Mount_By_Path("/data", true))
1266 return false;
1267
1268 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001269 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001270 } else {
1271 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001272 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001273 }
1274 return true;
1275}
1276
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001277int TWPartitionManager::Wipe_Android_Secure(void) {
1278 std::vector<TWPartition*>::iterator iter;
1279 int ret = false;
1280 bool found = false;
1281
1282 // Iterate through all partitions
1283 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1284 if ((*iter)->Has_Android_Secure) {
1285 ret = (*iter)->Wipe_AndSec();
1286 found = true;
1287 }
1288 }
1289 if (found) {
1290 return ret;
1291 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001292 gui_err("no_andsec=No android secure partitions found.");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001293 }
1294 return false;
1295}
1296
Dees_Troy38bd7602012-09-14 13:33:53 -04001297int TWPartitionManager::Format_Data(void) {
1298 TWPartition* dat = Find_Partition_By_Path("/data");
1299
1300 if (dat != NULL) {
1301 if (!dat->UnMount(true))
1302 return false;
1303
1304 return dat->Wipe_Encryption();
1305 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001306 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001307 return false;
1308 }
1309 return false;
1310}
1311
1312int TWPartitionManager::Wipe_Media_From_Data(void) {
1313 TWPartition* dat = Find_Partition_By_Path("/data");
1314
1315 if (dat != NULL) {
1316 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001317 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001318 return false;
1319 }
1320 if (!dat->Mount(true))
1321 return false;
1322
Ethan Yonker74db1572015-10-28 12:44:49 -05001323 gui_msg("wiping_datamedia=Wiping internal storage -- /data/media...");
Ethan Yonker726a0202014-12-16 20:01:38 -06001324 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001325 TWFunc::removeDir("/data/media", false);
xiaolu9416f4f2015-06-04 08:22:23 +08001326 dat->Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001327 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001328 return true;
1329 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001330 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001331 return false;
1332 }
1333 return false;
1334}
1335
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001336int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1337 std::vector<TWPartition*>::iterator iter;
1338 int ret = false;
1339 bool found = false;
1340 string Local_Path = TWFunc::Get_Root_Path(Path);
1341
1342 if (Local_Path == "/tmp" || Local_Path == "/")
1343 return true;
1344
1345 // Iterate through all partitions
1346 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1347 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1348 ret = (*iter)->Repair();
1349 found = true;
1350 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1351 (*iter)->Repair();
1352 }
1353 }
1354 if (found) {
1355 return ret;
1356 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001357 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001358 } else {
1359 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1360 }
1361 return false;
1362}
1363
Ethan Yonkera2719152015-05-28 09:44:41 -05001364int TWPartitionManager::Resize_By_Path(string Path, bool Display_Error) {
1365 std::vector<TWPartition*>::iterator iter;
1366 int ret = false;
1367 bool found = false;
1368 string Local_Path = TWFunc::Get_Root_Path(Path);
1369
1370 if (Local_Path == "/tmp" || Local_Path == "/")
1371 return true;
1372
1373 // Iterate through all partitions
1374 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1375 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1376 ret = (*iter)->Resize();
1377 found = true;
1378 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1379 (*iter)->Resize();
1380 }
1381 }
1382 if (found) {
1383 return ret;
1384 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001385 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Ethan Yonkera2719152015-05-28 09:44:41 -05001386 } else {
1387 LOGINFO("Resize: Unable to find partition for path '%s'\n", Local_Path.c_str());
1388 }
1389 return false;
1390}
1391
Dees_Troy51a0e822012-09-05 15:24:24 -04001392void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001393 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001394 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001395
Ethan Yonker74db1572015-10-28 12:44:49 -05001396 gui_msg("update_part_details=Updating partition details...");
Dees_Troy5bf43922012-09-07 16:07:55 -04001397 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Ethan Yonker1b190162016-12-05 15:25:19 -06001398 (*iter)->Update_Size(true);
Dees_Troy51127312012-09-08 13:08:49 -04001399 if ((*iter)->Can_Be_Mounted) {
Dees_Troy51127312012-09-08 13:08:49 -04001400 if ((*iter)->Mount_Point == "/system") {
1401 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1402 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1403 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1404 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1405 } else if ((*iter)->Mount_Point == "/cache") {
1406 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1407 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1408 } else if ((*iter)->Mount_Point == "/sd-ext") {
1409 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1410 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1411 if ((*iter)->Backup_Size == 0) {
1412 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1413 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1414 } else
1415 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001416 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001417 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001418 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001419 if ((*iter)->Backup_Size == 0) {
1420 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1421 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1422 } else
1423 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001424 } else if ((*iter)->Mount_Point == "/boot") {
1425 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1426 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1427 if ((*iter)->Backup_Size == 0) {
1428 DataManager::SetValue("tw_has_boot_partition", 0);
1429 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1430 } else
1431 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001432 }
Dees_Troyaa9cc402012-10-13 12:14:05 -04001433 } else {
1434 // Handle unmountable partitions in case we reset defaults
1435 if ((*iter)->Mount_Point == "/boot") {
1436 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1437 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1438 if ((*iter)->Backup_Size == 0) {
1439 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1440 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1441 } else
1442 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1443 } else if ((*iter)->Mount_Point == "/recovery") {
1444 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1445 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1446 if ((*iter)->Backup_Size == 0) {
1447 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1448 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1449 } else
1450 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001451 } else if ((*iter)->Mount_Point == "/data") {
1452 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001453 }
Dees_Troy51127312012-09-08 13:08:49 -04001454 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001455 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001456 gui_msg("update_part_details_done=...done");
Dees_Troy51127312012-09-08 13:08:49 -04001457 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1458 string current_storage_path = DataManager::GetCurrentStoragePath();
1459 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001460 if (FreeStorage != NULL) {
1461 // Attempt to mount storage
1462 if (!FreeStorage->Mount(false)) {
Matt Mower2d50cad2015-12-03 22:26:55 -06001463 gui_msg(Msg(msg::kError, "unable_to_mount_storage=Unable to mount storage"));
1464 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
Dees_Troy8170a922012-09-18 15:40:25 -04001465 } else {
1466 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1467 }
1468 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001469 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001470 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001471 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001472 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001473 return;
1474}
1475
1476int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001477#ifdef TW_INCLUDE_CRYPTO
1478 int ret_val, password_len;
Ethan Yonkera1de1492015-11-04 11:58:27 -06001479 char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX], cPassword[255];
Dees_Troy5bf43922012-09-07 16:07:55 -04001480 size_t result;
Ethan Yonker253368a2014-11-25 15:00:52 -06001481 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001482
Ethan Yonker253368a2014-11-25 15:00:52 -06001483 // Mount any partitions that need to be mounted for decrypt
1484 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1485 if ((*iter)->Mount_To_Decrypt) {
1486 (*iter)->Mount(true);
1487 }
a39552696ff55ce2013-01-08 16:14:56 +00001488 }
a39552696ff55ce2013-01-08 16:14:56 +00001489
Ethan Yonkera1de1492015-11-04 11:58:27 -06001490 property_get("ro.crypto.state", crypto_state, "error");
1491 if (strcmp(crypto_state, "error") == 0) {
1492 property_set("ro.crypto.state", "encrypted");
1493 // Sleep for a bit so that services can start if needed
1494 sleep(1);
1495 }
1496
Dees_Troy5bf43922012-09-07 16:07:55 -04001497 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001498 int pwret = cryptfs_check_passwd(cPassword);
1499
Ethan Yonker253368a2014-11-25 15:00:52 -06001500 // Unmount any partitions that were needed for decrypt
1501 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1502 if ((*iter)->Mount_To_Decrypt) {
1503 (*iter)->UnMount(false);
1504 }
1505 }
1506
a39552696ff55ce2013-01-08 16:14:56 +00001507 if (pwret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001508 gui_err("fail_decrypt=Failed to decrypt data.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001509 return -1;
1510 }
a39552696ff55ce2013-01-08 16:14:56 +00001511
Dees_Troy5bf43922012-09-07 16:07:55 -04001512 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1513 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001514 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001515 } else {
1516 TWPartition* dat = Find_Partition_By_Path("/data");
1517 if (dat != NULL) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001518 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1519 dat->Is_Decrypted = true;
1520 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001521 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001522 dat->Current_File_System = dat->Fstab_File_System; // Needed if we're ignoring blkid because encrypted devices start out as emmc
Matt Mower3c366972015-12-25 19:28:31 -06001523 gui_msg(Msg("decrypt_success_dev=Data successfully decrypted, new block device: '{1}'")(crypto_blkdev));
a39552696ff55ce2013-01-08 16:14:56 +00001524
Dees_Troy5bf43922012-09-07 16:07:55 -04001525 // Sleep for a bit so that the device will be ready
1526 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001527 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001528 dat->Storage_Path = "/data/media/0";
1529 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001530 DataManager::SetValue("tw_storage_path", "/data/media/0");
Ethan Yonkerec0fa4a2014-11-20 09:51:03 -06001531 DataManager::SetValue("tw_settings_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001532 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001533 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001534 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001535 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001536 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001537 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001538 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001539 }
1540 return 0;
1541#else
Ethan Yonker74db1572015-10-28 12:44:49 -05001542 gui_err("no_crypto_support=No crypto support was compiled into this build.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001543 return -1;
1544#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001545 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001546}
1547
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001548int TWPartitionManager::Fix_Contexts(void) {
thata3d31fb2014-12-21 22:27:40 +01001549#ifdef HAVE_SELINUX
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001550 std::vector<TWPartition*>::iterator iter;
1551 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1552 if ((*iter)->Has_Data_Media) {
1553 if ((*iter)->Mount(true)) {
1554 if (fixContexts::fixDataMediaContexts((*iter)->Mount_Point) != 0)
1555 return -1;
1556 }
1557 }
1558 }
Dees_Troy1a650e62012-10-19 20:54:32 -04001559 UnMount_Main_Partitions();
Ethan Yonker74db1572015-10-28 12:44:49 -05001560 gui_msg("done=Done.");
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001561 return 0;
1562#else
1563 LOGERR("Cannot fix contexts, no selinux support present.\n");
1564 return -1;
1565#endif
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001566}
1567
Ethan Yonker66a19492015-12-10 10:19:45 -06001568TWPartition* TWPartitionManager::Find_Next_Storage(string Path, bool Exclude_Data_Media) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001569 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1570
1571 if (!Path.empty()) {
1572 string Search_Path = TWFunc::Get_Root_Path(Path);
1573 for (; iter != Partitions.end(); iter++) {
Matt Mower9a561dd2016-02-22 21:25:51 -06001574 if ((*iter)->Mount_Point == Search_Path) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001575 iter++;
1576 break;
1577 }
1578 }
1579 }
1580
1581 for (; iter != Partitions.end(); iter++) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001582 if (Exclude_Data_Media && (*iter)->Has_Data_Media) {
1583 // do nothing, do not return this type of partition
1584 } else if ((*iter)->Is_Storage && (*iter)->Is_Present) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001585 return (*iter);
1586 }
1587 }
1588
1589 return NULL;
1590}
1591
Dees_Troyd21618c2012-10-14 18:48:49 -04001592int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001593 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1594
1595 if (Part == NULL