blob: 320944c3f2d9e8f54867c21603da59850fc1157c [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>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030#include <iostream>
31#include <iomanip>
Ethan Yonker8613dc02014-09-11 09:28:20 -050032#include <sys/wait.h>
Ethan Yonker483e9f42016-01-11 22:21:18 -060033#include <linux/fs.h>
34#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040039#include "twrp-functions.hpp"
Ethan Yonkerb5fab762016-01-28 14:03:33 -060040#include "fixContexts.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050041#include "twrpDigest.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050042#include "twrpDU.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060043#include "set_metadata.h"
bigbiff7abc5fe2015-01-17 16:53:12 -050044#include "tw_atomic.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050045#include "gui/gui.hpp"
Ethan Yonker472f5062016-02-25 13:47:30 -060046#include "progresstracking.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040047
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040048#ifdef TW_HAS_MTP
49#include "mtp/mtp_MtpServer.hpp"
50#include "mtp/twrpMtp.hpp"
Ethan Yonker726a0202014-12-16 20:01:38 -060051#include "mtp/MtpMessage.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040052#endif
53
Dees Troy6f6441d2014-01-23 02:07:03 +000054extern "C" {
55 #include "cutils/properties.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050056 #include "gui/gui.h"
Dees Troy6f6441d2014-01-23 02:07:03 +000057}
58
Dees_Troy5bf43922012-09-07 16:07:55 -040059#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060060 #include "crypto/lollipop/cryptfs.h"
Ethan Yonker66a19492015-12-10 10:19:45 -060061 #include "gui/rapidxml.hpp"
62 #include "gui/pages.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040063#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040064
Ethan Yonker6277c792014-09-15 14:54:30 -050065extern bool datamedia;
66
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050067TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040068 mtp_was_enabled = false;
Ethan Yonker726a0202014-12-16 20:01:38 -060069 mtp_write_fd = -1;
bigbiff7abc5fe2015-01-17 16:53:12 -050070 stop_backup.set_value(0);
71 tar_fork_pid = 0;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050072}
73
Dees_Troy51a0e822012-09-05 15:24:24 -040074int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040075 FILE *fstabFile;
76 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000077 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050078 TWPartition* andsec_partition = NULL;
Ethan Yonker726a0202014-12-16 20:01:38 -060079 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 -040080
81 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
82 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000083 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040084 return false;
85 }
86
87 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
88 if (fstab_line[0] != '/')
89 continue;
90
Dees_Troy2a923582012-09-20 12:13:34 -040091 if (fstab_line[strlen(fstab_line) - 1] != '\n')
92 fstab_line[strlen(fstab_line)] = '\n';
Dees_Troy2a923582012-09-20 12:13:34 -040093
Matt Mower2b2dd152016-04-26 11:24:08 -050094 TWPartition* partition = new TWPartition();
Matt Mower72c87ce2016-04-26 14:34:56 -050095 if (partition->Process_Fstab_Line(fstab_line, Display_Error))
Ethan Yonkerc05c5982014-03-13 09:19:56 -050096 Partitions.push_back(partition);
Matt Mower72c87ce2016-04-26 14:34:56 -050097 else
Dees_Troy5bf43922012-09-07 16:07:55 -040098 delete partition;
Matt Mower2b2dd152016-04-26 11:24:08 -050099
100 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy5bf43922012-09-07 16:07:55 -0400101 }
102 fclose(fstabFile);
Matt Mower72c87ce2016-04-26 14:34:56 -0500103
104 std::vector<TWPartition*>::iterator iter;
105 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
106 (*iter)->Partition_Post_Processing(Display_Error);
107
108 if ((*iter)->Is_Storage) {
109 ++storageid;
110 (*iter)->MTP_Storage_ID = storageid;
111 }
112
113 if (!settings_partition && (*iter)->Is_Settings_Storage && (*iter)->Is_Present)
114 settings_partition = (*iter);
115 else
116 (*iter)->Is_Settings_Storage = false;
117
118 if (!andsec_partition && (*iter)->Has_Android_Secure && (*iter)->Is_Present)
119 andsec_partition = (*iter);
120 else
121 (*iter)->Has_Android_Secure = false;
122 }
123
Ethan Yonker6277c792014-09-15 14:54:30 -0500124 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) {
125 // Attempt to automatically identify /data/media emulated storage devices
126 TWPartition* Dat = Find_Partition_By_Path("/data");
127 if (Dat) {
128 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
129 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200130 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500131 settings_partition = Dat;
Ethan Yonker726a0202014-12-16 20:01:38 -0600132 // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID
133 ++storageid;
134 Dat->MTP_Storage_ID = storageid;
Ethan Yonker6277c792014-09-15 14:54:30 -0500135 }
136 }
Dees Troy02a64532014-03-19 15:23:32 +0000137 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500138 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
139 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000140 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500141 break;
142 }
143 }
Dees Troy02a64532014-03-19 15:23:32 +0000144 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000145 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500146 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400147 if (!Write_Fstab()) {
148 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000149 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400150 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000151 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400152 }
Matt Mowered71fa32014-04-16 13:21:47 -0500153
Matt Mowerbf4efa32014-04-14 23:25:26 -0500154 if (andsec_partition) {
155 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500156 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500157 Setup_Android_Secure_Location(settings_partition);
158 }
Matt Mowered71fa32014-04-16 13:21:47 -0500159 if (settings_partition) {
160 Setup_Settings_Storage_Partition(settings_partition);
161 }
Ethan Yonker253368a2014-11-25 15:00:52 -0600162#ifdef TW_INCLUDE_CRYPTO
Ethan Yonkercceebb82014-11-18 10:17:59 -0600163 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
164 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
165 int password_type = cryptfs_get_password_type();
166 if (password_type == CRYPT_TYPE_DEFAULT) {
167 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
168 if (Decrypt_Device("default_password") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500169 gui_msg("decrypt_success=Successfully decrypted with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600170 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
171 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500172 gui_err("unable_to_decrypt=Unable to decrypt with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600173 }
174 } else {
175 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
176 }
177 }
Ethan Yonker66a19492015-12-10 10:19:45 -0600178 if (Decrypt_Data && (!Decrypt_Data->Is_Encrypted || Decrypt_Data->Is_Decrypted) && Decrypt_Data->Mount(false)) {
179 Decrypt_Adopted();
180 }
Ethan Yonkercceebb82014-11-18 10:17:59 -0600181#endif
Dees_Troy51127312012-09-08 13:08:49 -0400182 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400183 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400184 return true;
185}
186
187int TWPartitionManager::Write_Fstab(void) {
188 FILE *fp;
189 std::vector<TWPartition*>::iterator iter;
190 string Line;
191
192 fp = fopen("/etc/fstab", "w");
193 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000194 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400195 return false;
196 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400197 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400198 if ((*iter)->Can_Be_Mounted) {
dianlujitao7d304c72016-01-02 12:15:50 +0800199 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw 0 0\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400200 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000201 }
202 // Handle subpartition tracking
203 if ((*iter)->Is_SubPartition) {
204 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
205 if (ParentPartition)
206 ParentPartition->Has_SubPartition = true;
207 else
208 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 -0400209 }
210 }
211 fclose(fp);
212 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400213}
214
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500215void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500216 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
217 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
218 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
219}
220
Matt Mowerbf4efa32014-04-14 23:25:26 -0500221void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
222 if (Part->Has_Android_Secure)
223 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500224 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500225 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500226}
227
Dees_Troy8170a922012-09-18 15:40:25 -0400228void TWPartitionManager::Output_Partition_Logging(void) {
229 std::vector<TWPartition*>::iterator iter;
230
231 printf("\n\nPartition Logs:\n");
232 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
233 Output_Partition((*iter));
234}
235
236void TWPartitionManager::Output_Partition(TWPartition* Part) {
237 unsigned long long mb = 1048576;
238
Gary Peck004d48b2012-11-21 16:28:18 -0800239 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 -0400240 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800241 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 -0400242 }
Gary Peck004d48b2012-11-21 16:28:18 -0800243 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500244 if (Part->Can_Be_Mounted)
245 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800246 if (Part->Can_Be_Wiped)
247 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700248 if (Part->Use_Rm_Rf)
249 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500250 if (Part->Can_Be_Backed_Up)
251 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800252 if (Part->Wipe_During_Factory_Reset)
253 printf("Wipe_During_Factory_Reset ");
254 if (Part->Wipe_Available_in_GUI)
255 printf("Wipe_Available_in_GUI ");
256 if (Part->Is_SubPartition)
257 printf("Is_SubPartition ");
258 if (Part->Has_SubPartition)
259 printf("Has_SubPartition ");
260 if (Part->Removable)
261 printf("Removable ");
262 if (Part->Is_Present)
263 printf("IsPresent ");
264 if (Part->Can_Be_Encrypted)
265 printf("Can_Be_Encrypted ");
266 if (Part->Is_Encrypted)
267 printf("Is_Encrypted ");
268 if (Part->Is_Decrypted)
269 printf("Is_Decrypted ");
270 if (Part->Has_Data_Media)
271 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000272 if (Part->Can_Encrypt_Backup)
273 printf("Can_Encrypt_Backup ");
274 if (Part->Use_Userdata_Encryption)
275 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800276 if (Part->Has_Android_Secure)
277 printf("Has_Android_Secure ");
278 if (Part->Is_Storage)
279 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500280 if (Part->Is_Settings_Storage)
281 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000282 if (Part->Ignore_Blkid)
283 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000284 if (Part->Retain_Layout_Version)
285 printf("Retain_Layout_Version ");
Ethan Yonker253368a2014-11-25 15:00:52 -0600286 if (Part->Mount_To_Decrypt)
287 printf("Mount_To_Decrypt ");
Ethan Yonker96af84a2015-01-05 14:58:36 -0600288 if (Part->Can_Flash_Img)
289 printf("Can_Flash_Img ");
Ethan Yonker66a19492015-12-10 10:19:45 -0600290 if (Part->Is_Adopted_Storage)
291 printf("Is_Adopted_Storage ");
Gary Peck004d48b2012-11-21 16:28:18 -0800292 printf("\n");
293 if (!Part->SubPartition_Of.empty())
294 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
295 if (!Part->Symlink_Path.empty())
296 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
297 if (!Part->Symlink_Mount_Point.empty())
298 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
299 if (!Part->Primary_Block_Device.empty())
300 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
301 if (!Part->Alternate_Block_Device.empty())
302 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
303 if (!Part->Decrypted_Block_Device.empty())
304 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
Ethan Yonker253368a2014-11-25 15:00:52 -0600305 if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer")
306 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800307 if (Part->Length != 0)
308 printf(" Length: %i\n", Part->Length);
309 if (!Part->Display_Name.empty())
310 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500311 if (!Part->Storage_Name.empty())
312 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800313 if (!Part->Backup_Path.empty())
314 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
315 if (!Part->Backup_Name.empty())
316 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500317 if (!Part->Backup_Display_Name.empty())
318 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800319 if (!Part->Backup_FileName.empty())
320 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
321 if (!Part->Storage_Path.empty())
322 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
323 if (!Part->Current_File_System.empty())
324 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
325 if (!Part->Fstab_File_System.empty())
326 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
327 if (Part->Format_Block_Size != 0)
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500328 printf(" Format_Block_Size: %lu\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400329 if (!Part->MTD_Name.empty())
330 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400331 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500332 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800333 if (Part->Mount_Flags || !Part->Mount_Options.empty())
Matt Mower4ab42b12016-04-21 13:52:18 -0500334 printf(" Mount_Options: %s\n", Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600335 if (Part->MTP_Storage_ID)
336 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker6277c792014-09-15 14:54:30 -0500337 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400338}
339
Dees_Troy51a0e822012-09-05 15:24:24 -0400340int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 std::vector<TWPartition*>::iterator iter;
342 int ret = false;
343 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400344 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400345
Dees_Troyd93bda52013-07-03 19:55:19 +0000346 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400347 return true;
348
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 // Iterate through all partitions
350 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400351 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 ret = (*iter)->Mount(Display_Error);
353 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400354 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400356 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400357 }
358 if (found) {
359 return ret;
360 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500361 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 -0400362 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000363 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400364 }
365 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400366}
367
Dees_Troy51a0e822012-09-05 15:24:24 -0400368int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400369 std::vector<TWPartition*>::iterator iter;
370 int ret = false;
371 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400372 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400373
374 // Iterate through all partitions
375 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400376 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400377 ret = (*iter)->UnMount(Display_Error);
378 found = true;
379 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
380 (*iter)->UnMount(Display_Error);
381 }
382 }
383 if (found) {
384 return ret;
385 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500386 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 -0400387 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000388 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400389 }
390 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400391}
392
Dees_Troy51a0e822012-09-05 15:24:24 -0400393int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400394 TWPartition* Part = Find_Partition_By_Path(Path);
395
396 if (Part)
397 return Part->Is_Mounted();
398 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000399 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400400 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400401}
402
Dees_Troy5bf43922012-09-07 16:07:55 -0400403int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400404 string current_storage_path = DataManager::GetCurrentStoragePath();
405
406 if (Mount_By_Path(current_storage_path, Display_Error)) {
407 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
408 if (FreeStorage)
409 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
410 return true;
411 }
412 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400413}
414
Dees_Troy5bf43922012-09-07 16:07:55 -0400415int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
416 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
417}
418
419TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
420 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400421 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400422
423 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400424 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400425 return (*iter);
426 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400427 return NULL;
428}
429
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400430int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
431 // Check the backup name to ensure that it is the correct size and contains only valid characters
432 // and that a backup with that name doesn't already exist
433 char backup_name[MAX_BACKUP_NAME_LEN];
434 char backup_loc[255], tw_image_dir[255];
435 int copy_size;
436 int index, cur_char;
437 string Backup_Name, Backup_Loc;
438
439 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
440 copy_size = Backup_Name.size();
441 // Check size
442 if (copy_size > MAX_BACKUP_NAME_LEN) {
443 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500444 gui_err("backup_name_len=Backup name is too long.");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400445 return -2;
446 }
447
448 // Check each character
449 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500450 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400451 return 0; // A "0" (zero) means to use the current timestamp for the backup name
452 for (index=0; index<copy_size; index++) {
453 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500454 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 -0400455 // These are valid characters
456 // Numbers
457 // Upper case letters
458 // Lower case letters
459 // Space
460 // and -_.{}[]
461 } else {
462 if (Display_Error)
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600463 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 -0400464 return -3;
465 }
466 }
467
468 // Check to make sure that a backup with this name doesn't already exist
469 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
470 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500471 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500472 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400473 if (Display_Error)
Matt Mower3c366972015-12-25 19:28:31 -0600474 gui_err("backup_name_exists=A backup with that name already exists!");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400475 return -4;
476 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400477 // No problems found, return 0
478 return 0;
479}
480
Dees_Troy43d8b002012-09-17 16:00:01 -0400481bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
482{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500483 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400484 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500485 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500486 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400487
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500488 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400489 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400490
Ethan Yonker74db1572015-10-28 12:44:49 -0500491 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, gui_parse_text("{@generating_md51}"));
492 gui_msg("generating_md52= * Generating md5...");
Dees_Troy43d8b002012-09-17 16:00:01 -0400493
494 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500495 md5sum.setfn(Backup_Folder + Backup_Filename);
496 if (md5sum.computeMD5() == 0)
497 if (md5sum.write_md5digest() == 0)
Ethan Yonker74db1572015-10-28 12:44:49 -0500498 gui_msg("md5_created= * MD5 Created.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500499 else
500 return -1;
501 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500502 gui_err("md5_error= * MD5 Error!");
Dees_Troy43d8b002012-09-17 16:00:01 -0400503 } else {
504 char filename[512];
505 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500506 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400507 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500508 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000509 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400510 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000511 if (TWFunc::Path_Exists(filename)) {
512 if (md5sum.computeMD5() == 0) {
513 if (md5sum.write_md5digest() != 0)
514 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500515 gui_err("md5_error= * MD5 Error!");
Dees_Troy83bd4832013-05-04 12:39:56 +0000516 return false;
517 }
518 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500519 gui_err("md5_compute_error= * Error computing MD5.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500520 return false;
521 }
522 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400523 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400524 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500525 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400526 }
527 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000528 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400529 return false;
530 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500531 gui_msg("md5_created= * MD5 Created.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400532 }
533 return true;
534}
535
Ethan Yonker472f5062016-02-25 13:47:30 -0600536bool TWPartitionManager::Backup_Partition(TWPartition* Part, const string& Backup_Folder, bool generate_md5, unsigned long *img_time, unsigned long *file_time, ProgressTracking *progress) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400537 time_t start, stop;
that203bcc92015-05-09 17:27:33 +0200538 int use_compression;
Dees_Troy43d8b002012-09-17 16:00:01 -0400539
bigbiffbf1d6722015-02-14 16:25:59 -0500540 string backup_log = Backup_Folder + "recovery.log";
541
Dees_Troy43d8b002012-09-17 16:00:01 -0400542 if (Part == NULL)
543 return true;
544
Dees_Troy093b7642012-09-21 15:59:38 -0400545 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400546
Tom Hite5a926722014-09-15 01:31:03 +0000547 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400548 time(&start);
549
Ethan Yonker472f5062016-02-25 13:47:30 -0600550 if (Part->Backup(Backup_Folder, tar_fork_pid, progress)) {
Tom Hite5a926722014-09-15 01:31:03 +0000551 bool md5Success = false;
Dees_Troy8170a922012-09-18 15:40:25 -0400552 if (Part->Has_SubPartition) {
553 std::vector<TWPartition*>::iterator subpart;
554
555 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500556 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker472f5062016-02-25 13:47:30 -0600557 if (!(*subpart)->Backup(Backup_Folder, tar_fork_pid, progress)) {
Tom Hite5a926722014-09-15 01:31:03 +0000558 TWFunc::SetPerformanceMode(false);
bigbiffbf1d6722015-02-14 16:25:59 -0500559 Clean_Backup_Folder(Backup_Folder);
560 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
561 tw_set_default_metadata(backup_log.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400562 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000563 }
Dees_Troy2727b992013-08-14 20:09:30 +0000564 sync();
565 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000566 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
567 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400568 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000569 }
Dees_Troy8170a922012-09-18 15:40:25 -0400570 }
571 }
572 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400573 time(&stop);
that203bcc92015-05-09 17:27:33 +0200574 int backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000575 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400576 if (Part->Backup_Method == 1) {
Dees_Troy093b7642012-09-21 15:59:38 -0400577 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400578 } else {
Dees_Troy093b7642012-09-21 15:59:38 -0400579 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400580 }
Tom Hite5a926722014-09-15 01:31:03 +0000581
582 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
583 TWFunc::SetPerformanceMode(false);
584 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400585 } else {
bigbiffbf1d6722015-02-14 16:25:59 -0500586 Clean_Backup_Folder(Backup_Folder);
587 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
588 tw_set_default_metadata(backup_log.c_str());
Tom Hite5a926722014-09-15 01:31:03 +0000589 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400590 return false;
591 }
bigbiff7abc5fe2015-01-17 16:53:12 -0500592 return 0;
593}
594
bigbiffbf1d6722015-02-14 16:25:59 -0500595void TWPartitionManager::Clean_Backup_Folder(string Backup_Folder) {
596 DIR *d = opendir(Backup_Folder.c_str());
597 struct dirent *p;
598 int r;
599
Ethan Yonker74db1572015-10-28 12:44:49 -0500600 gui_msg("backup_clean=Backup Failed. Cleaning Backup Folder.");
bigbiffbf1d6722015-02-14 16:25:59 -0500601
602 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500603 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Backup_Folder)(strerror(errno)));
bigbiffbf1d6722015-02-14 16:25:59 -0500604 return;
605 }
606
Matt Mower2b18a532015-02-20 16:58:05 -0600607 while ((p = readdir(d))) {
bigbiffbf1d6722015-02-14 16:25:59 -0500608 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
609 continue;
610
611 string path = Backup_Folder + p->d_name;
612
613 size_t dot = path.find_last_of(".") + 1;
614 if (path.substr(dot) == "win" || path.substr(dot) == "md5" || path.substr(dot) == "info") {
615 r = unlink(path.c_str());
616 if (r != 0) {
617 LOGINFO("Unable to unlink '%s: %s'\n", path.c_str(), strerror(errno));
618 }
619 }
620 }
621 closedir(d);
622}
623
Ethan Yonker472f5062016-02-25 13:47:30 -0600624int TWPartitionManager::Check_Backup_Cancel() {
625 return stop_backup.get_value();
626}
627
bigbiff7abc5fe2015-01-17 16:53:12 -0500628int TWPartitionManager::Cancel_Backup() {
629 string Backup_Folder, Backup_Name, Full_Backup_Path;
630
631 stop_backup.set_value(1);
632
633 if (tar_fork_pid != 0) {
634 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
635 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
636 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
637 LOGINFO("Killing pid: %d\n", tar_fork_pid);
638 kill(tar_fork_pid, SIGUSR2);
639 while (kill(tar_fork_pid, 0) == 0) {
640 usleep(1000);
641 }
642 LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n");
643 LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str());
644 TWFunc::removeDir(Full_Backup_Path, false);
645 tar_fork_pid = 0;
646 }
647
648 return 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400649}
650
651int TWPartitionManager::Run_Backup(void) {
Matt Mower2b18a532015-02-20 16:58:05 -0600652 int check, do_md5, partition_count = 0, disable_free_space_check = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500653 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400654 unsigned long long total_bytes = 0, file_bytes = 0, img_bytes = 0, free_space = 0, img_bytes_remaining, file_bytes_remaining, subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400655 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500656 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400657 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400658 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400659 struct tm *t;
660 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500661 size_t start_pos = 0, end_pos = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500662 stop_backup.set_value(0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400663 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500664 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400665
666 time(&total_start);
667
668 Update_System_Details();
669
670 if (!Mount_Current_Storage(true))
671 return false;
672
673 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400674 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400675 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400676 else
677 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400678
Dees_Troy43d8b002012-09-17 16:00:01 -0400679 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
680 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600681 if (Backup_Name == gui_lookup("curr_date", "(Current Date)")) {
Dees Troyb21cc642013-09-10 17:36:41 +0000682 Backup_Name = TWFunc::Get_Current_Date();
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600683 } else if (Backup_Name == gui_lookup("auto_generate", "(Auto Generate)") || Backup_Name == "0" || Backup_Name.empty()) {
Dees Troyb21cc642013-09-10 17:36:41 +0000684 TWFunc::Auto_Generate_Backup_Name();
685 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400686 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000687 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400688 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000689 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400690
Dees_Troy2673cec2013-04-02 20:22:16 +0000691 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500692 DataManager::GetValue("tw_backup_list", Backup_List);
693 if (!Backup_List.empty()) {
694 end_pos = Backup_List.find(";", start_pos);
695 while (end_pos != string::npos && start_pos < Backup_List.size()) {
696 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
697 backup_part = Find_Partition_By_Path(backup_path);
698 if (backup_part != NULL) {
699 partition_count++;
700 if (backup_part->Backup_Method == 1)
701 file_bytes += backup_part->Backup_Size;
702 else
703 img_bytes += backup_part->Backup_Size;
704 if (backup_part->Has_SubPartition) {
705 std::vector<TWPartition*>::iterator subpart;
706
707 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000708 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_part->Mount_Point) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500709 partition_count++;
710 if ((*subpart)->Backup_Method == 1)
711 file_bytes += (*subpart)->Backup_Size;
712 else
713 img_bytes += (*subpart)->Backup_Size;
714 }
715 }
Dees_Troy8170a922012-09-18 15:40:25 -0400716 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500717 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500718 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 -0400719 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500720 start_pos = end_pos + 1;
721 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400722 }
723 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400724
725 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500726 gui_msg("no_partition_selected=No partitions selected for backup.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400727 return false;
728 }
729 total_bytes = file_bytes + img_bytes;
Ethan Yonker472f5062016-02-25 13:47:30 -0600730 ProgressTracking progress(total_bytes);
Ethan Yonker74db1572015-10-28 12:44:49 -0500731 gui_msg(Msg("total_partitions_backup= * Total number of partitions to back up: {1}")(partition_count));
732 gui_msg(Msg("total_backup_size= * Total size of all data: {1}MB")(total_bytes / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400733 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
734 if (storage != NULL) {
735 free_space = storage->Free;
Ethan Yonker74db1572015-10-28 12:44:49 -0500736 gui_msg(Msg("available_space= * Available space: {1}MB")(free_space / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400737 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500738 gui_err("unable_locate_storage=Unable to locate storage device.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400739 return false;
740 }
bigbiffbf1d6722015-02-14 16:25:59 -0500741
742 DataManager::GetValue("tw_disable_free_space", disable_free_space_check);
743 if (!disable_free_space_check) {
744 if (free_space - (32 * 1024 * 1024) < total_bytes) {
745 // We require an extra 32MB just in case
Ethan Yonker74db1572015-10-28 12:44:49 -0500746 gui_err("no_space=Not enough free space on storage.");
bigbiffbf1d6722015-02-14 16:25:59 -0500747 return false;
748 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400749 }
750 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500751 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400752
Ethan Yonker74db1572015-10-28 12:44:49 -0500753 gui_msg("backup_started=[BACKUP STARTED]");
754 gui_msg(Msg("backup_folder= * Backup Folder: {1}")(Full_Backup_Path));
Dees_Troyd4b22b02013-01-18 17:17:58 +0000755 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500756 gui_err("fail_backup_folder=Failed to make backup folder.");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000757 return false;
758 }
759
Dees_Troy2673cec2013-04-02 20:22:16 +0000760 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400761
Dees_Troya13d74f2013-03-24 08:54:55 -0500762 start_pos = 0;
763 end_pos = Backup_List.find(";", start_pos);
764 while (end_pos != string::npos && start_pos < Backup_List.size()) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500765 if (stop_backup.get_value() != 0)
766 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500767 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
768 backup_part = Find_Partition_By_Path(backup_path);
769 if (backup_part != NULL) {
Ethan Yonker472f5062016-02-25 13:47:30 -0600770 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_time, &file_time, &progress))
Dees_Troya13d74f2013-03-24 08:54:55 -0500771 return false;
772 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500773 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 -0500774 }
775 start_pos = end_pos + 1;
776 end_pos = Backup_List.find(";", start_pos);
777 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400778
779 // Average BPS
780 if (img_time == 0)
781 img_time = 1;
782 if (file_time == 0)
783 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400784 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500785 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400786
Ethan Yonker472f5062016-02-25 13:47:30 -0600787 if (file_bytes != 0)
788 gui_msg(Msg("avg_backup_fs=Average backup rate for file systems: {1} MB/sec")(file_bps / (1024 * 1024)));
789 if (img_bytes != 0)
790 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 -0400791
792 time(&total_stop);
793 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500794 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500795 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400796
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500797 int prev_img_bps, use_compression;
798 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400799 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
800 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500801 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400802
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500803 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400804 if (use_compression)
805 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500806 else
Dees_Troy093b7642012-09-21 15:59:38 -0400807 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
808 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500809 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400810
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500811 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400812 if (use_compression)
813 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
814 else
815 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
816
Ethan Yonker74db1572015-10-28 12:44:49 -0500817 gui_msg(Msg("total_backed_size=[{1} MB TOTAL BACKED UP]")(actual_backup_size));
Dees_Troy43d8b002012-09-17 16:00:01 -0400818 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400819 UnMount_Main_Partitions();
Ethan Yonker56db1c42015-12-20 22:49:00 -0600820 gui_msg(Msg(msg::kHighlight, "backup_completed=[BACKUP COMPLETED IN {1} SECONDS]")(total_time)); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000821 string backup_log = Full_Backup_Path + "recovery.log";
822 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600823 tw_set_default_metadata(backup_log.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000824 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400825}
826
Ethan Yonker472f5062016-02-25 13:47:30 -0600827bool TWPartitionManager::Restore_Partition(TWPartition* Part, const string& Restore_Name, ProgressTracking *progress) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400828 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000829 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400830 time(&Start);
Ethan Yonker472f5062016-02-25 13:47:30 -0600831
832 if (!Part->Restore(Restore_Name, progress)) {
Tom Hite5a926722014-09-15 01:31:03 +0000833 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400834 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000835 }
Dees_Troy8170a922012-09-18 15:40:25 -0400836 if (Part->Has_SubPartition) {
837 std::vector<TWPartition*>::iterator subpart;
838
839 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
840 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker472f5062016-02-25 13:47:30 -0600841 if (!(*subpart)->Restore(Restore_Name, progress)) {
Tom Hite5a926722014-09-15 01:31:03 +0000842 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400843 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000844 }
Dees_Troy8170a922012-09-18 15:40:25 -0400845 }
846 }
847 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400848 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000849 TWFunc::SetPerformanceMode(false);
z31s1g2053fe02016-02-04 06:45:18 +0100850 gui_msg(Msg("restore_part_done=[{1} done ({2} seconds)]")(Part->Backup_Display_Name)((int)difftime(Stop, Start)));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400851 return true;
852}
853
Ethan Yonker472f5062016-02-25 13:47:30 -0600854int TWPartitionManager::Run_Restore(const string& Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400855 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500856 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400857 time_t rStart, rStop;
858 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500859 string Restore_List, restore_path;
860 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500861 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400862
Ethan Yonker74db1572015-10-28 12:44:49 -0500863 gui_msg("restore_started=[RESTORE STARTED]");
864 gui_msg(Msg("restore_folder=Restore folder: '{1}'")(Restore_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -0400865
Dees_Troy4a2a1262012-09-18 09:33:47 -0400866 if (!Mount_Current_Storage(true))
867 return false;
868
869 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500870 if (check_md5 > 0) {
871 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500872 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, gui_parse_text("{@verifying_md5}"));
873 gui_msg("verifying_md5=Verifying MD5");
Dees_Troya13d74f2013-03-24 08:54:55 -0500874 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500875 gui_msg("skip_md5=Skipping MD5 check based on user setting.");
Dees_Troya13d74f2013-03-24 08:54:55 -0500876 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500877 gui_msg("calc_restore=Calculating restore details...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500878 DataManager::GetValue("tw_restore_selected", Restore_List);
879 if (!Restore_List.empty()) {
880 end_pos = Restore_List.find(";", start_pos);
881 while (end_pos != string::npos && start_pos < Restore_List.size()) {
882 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
883 restore_part = Find_Partition_By_Path(restore_path);
884 if (restore_part != NULL) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500885 if (restore_part->Mount_Read_Only) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500886 gui_msg(Msg(msg::kError, "restore_read_only=Cannot restore {1} -- mounted read only.")(restore_part->Backup_Display_Name));
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500887 return false;
888 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500889 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
890 return false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500891 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500892 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500893 if (restore_part->Has_SubPartition) {
894 std::vector<TWPartition*>::iterator subpart;
895
896 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
897 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400898 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500899 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500900 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500901 }
902 }
903 }
904 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500905 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500906 }
907 start_pos = end_pos + 1;
908 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400909 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400910 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400911
912 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500913 gui_err("no_part_restore=No partitions selected for restore.");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400914 return false;
915 }
916
Ethan Yonker74db1572015-10-28 12:44:49 -0500917 gui_msg(Msg("restore_part_count=Restoring {1} partitions...")(partition_count));
918 gui_msg(Msg("total_restore_size=Total restore size is {1}MB")(total_restore_size / 1048576));
Dees_Troy2673cec2013-04-02 20:22:16 +0000919 DataManager::SetProgress(0.0);
Ethan Yonker472f5062016-02-25 13:47:30 -0600920 ProgressTracking progress(total_restore_size);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500921
Dees_Troya13d74f2013-03-24 08:54:55 -0500922 start_pos = 0;
923 if (!Restore_List.empty()) {
924 end_pos = Restore_List.find(";", start_pos);
925 while (end_pos != string::npos && start_pos < Restore_List.size()) {
926 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
927 restore_part = Find_Partition_By_Path(restore_path);
928 if (restore_part != NULL) {
929 partition_count++;
Ethan Yonker472f5062016-02-25 13:47:30 -0600930 if (!Restore_Partition(restore_part, Restore_Name, &progress))
Dees_Troya13d74f2013-03-24 08:54:55 -0500931 return false;
932 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500933 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500934 }
935 start_pos = end_pos + 1;
936 end_pos = Restore_List.find(";", start_pos);
937 }
938 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500939 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, gui_parse_text("{@updating_system_details}"));
Dees_Troy43d8b002012-09-17 16:00:01 -0400940 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400941 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400942 time(&rStop);
Matt Mower3c366972015-12-25 19:28:31 -0600943 gui_msg(Msg(msg::kHighlight, "restore_completed=[RESTORE COMPLETED IN {1} SECONDS]")((int)difftime(rStop,rStart)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500944 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400945 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400946}
947
948void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400949 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500950 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000951 bool get_date = true, check_encryption = true;
952
953 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400954
955 DIR* d;
956 d = opendir(Restore_Name.c_str());
957 if (d == NULL)
958 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500959 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Name)(strerror(errno)));
Dees_Troy63c8df72012-09-10 14:02:05 -0400960 return;
961 }
962
963 struct dirent* de;
964 while ((de = readdir(d)) != NULL)
965 {
966 // Strip off three components
967 char str[256];
968 char* label;
969 char* fstype = NULL;
970 char* extn = NULL;
971 char* ptr;
972
973 strcpy(str, de->d_name);
974 if (strlen(str) <= 2)
975 continue;
976
977 if (get_date) {
978 char file_path[255];
979 struct stat st;
980
981 strcpy(file_path, Restore_Name.c_str());
982 strcat(file_path, "/");
983 strcat(file_path, str);
984 stat(file_path, &st);
985 string backup_date = ctime((const time_t*)(&st.st_mtime));
986 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
987 get_date = false;
988 }
989
990 label = str;
991 ptr = label;
992 while (*ptr && *ptr != '.') ptr++;
993 if (*ptr == '.')
994 {
995 *ptr = 0x00;
996 ptr++;
997 fstype = ptr;
998 }
999 while (*ptr && *ptr != '.') ptr++;
1000 if (*ptr == '.')
1001 {
1002 *ptr = 0x00;
1003 ptr++;
1004 extn = ptr;
1005 }
1006
Dees_Troy83bd4832013-05-04 12:39:56 +00001007 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001008 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001009 if (extnlength != 3 && extnlength != 6) continue;
1010 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1011 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1012
1013 if (check_encryption) {
1014 string filename = Restore_Name + "/";
1015 filename += de->d_name;
1016 if (TWFunc::Get_File_Type(filename) == 2) {
1017 LOGINFO("'%s' is encrypted\n", filename.c_str());
1018 DataManager::SetValue("tw_restore_encrypted", 1);
1019 }
1020 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001021 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001022
1023 TWPartition* Part = Find_Partition_By_Path(label);
1024 if (Part == NULL)
1025 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001026 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 -04001027 continue;
1028 }
1029
1030 Part->Backup_FileName = de->d_name;
1031 if (strlen(extn) > 3) {
1032 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1033 }
1034
James Christopher Adduonofddbdfa2016-02-29 15:08:11 -05001035 if (!Part->Is_SubPartition)
1036 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001037 }
1038 closedir(d);
1039
Dees_Troya13d74f2013-03-24 08:54:55 -05001040 // Set the final value
1041 DataManager::SetValue("tw_restore_list", Restore_List);
1042 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001043 return;
1044}
1045
1046int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001047 std::vector<TWPartition*>::iterator iter;
1048 int ret = false;
1049 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001050 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001051
1052 // Iterate through all partitions
1053 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001054 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001055 if (Path == "/and-sec")
1056 ret = (*iter)->Wipe_AndSec();
1057 else
1058 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001059 found = true;
1060 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1061 (*iter)->Wipe();
1062 }
1063 }
1064 if (found) {
1065 return ret;
1066 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001067 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 -04001068 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001069}
1070
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001071int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1072 std::vector<TWPartition*>::iterator iter;
1073 int ret = false;
1074 bool found = false;
1075 string Local_Path = TWFunc::Get_Root_Path(Path);
1076
1077 // Iterate through all partitions
1078 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1079 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1080 if (Path == "/and-sec")
1081 ret = (*iter)->Wipe_AndSec();
1082 else
1083 ret = (*iter)->Wipe(New_File_System);
1084 found = true;
1085 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1086 (*iter)->Wipe(New_File_System);
1087 }
1088 }
1089 if (found) {
1090 return ret;
1091 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001092 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 -05001093 return false;
1094}
1095
Dees_Troy51a0e822012-09-05 15:24:24 -04001096int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001097 std::vector<TWPartition*>::iterator iter;
1098 int ret = true;
1099
1100 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001101 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Ethan Yonker89583ef2015-08-26 09:01:59 -05001102#ifdef TW_OEM_BUILD
1103 if ((*iter)->Mount_Point == "/data") {
1104 if (!(*iter)->Wipe_Encryption())
1105 ret = false;
1106 } else {
1107#endif
1108 if (!(*iter)->Wipe())
1109 ret = false;
1110#ifdef TW_OEM_BUILD
1111 }
1112#endif
Dees_Troy094207a2012-09-26 12:00:39 -04001113 } else if ((*iter)->Has_Android_Secure) {
1114 if (!(*iter)->Wipe_AndSec())
1115 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001116 }
1117 }
1118 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001119}
1120
Dees_Troy38bd7602012-09-14 13:33:53 -04001121int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1122 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001123 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001124
1125 if (!Mount_By_Path("/data", true))
1126 return false;
1127
1128 if (!Mount_By_Path("/cache", true))
1129 return false;
1130
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001131 dir.push_back("/data/dalvik-cache");
1132 dir.push_back("/cache/dalvik-cache");
1133 dir.push_back("/cache/dc");
Ethan Yonker74db1572015-10-28 12:44:49 -05001134 gui_msg("wiping_dalvik=Wiping Dalvik Cache Directories...");
Dees_Troya13d74f2013-03-24 08:54:55 -05001135 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001136 if (stat(dir.at(i).c_str(), &st) == 0) {
1137 TWFunc::removeDir(dir.at(i), false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001138 gui_msg(Msg("cleaned=Cleaned: {1}...")(dir.at(i)));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001139 }
1140 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001141 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001142 if (sdext && sdext->Is_Present && sdext->Mount(false))
1143 {
1144 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1145 {
1146 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001147 gui_msg(Msg("cleaned=Cleaned: {1}...")("/sd-ext/dalvik-cache"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001148 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001149 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001150 gui_msg("dalvik_done=-- Dalvik Cache Directories Wipe Complete!");
Dees_Troy38bd7602012-09-14 13:33:53 -04001151 return true;
1152}
1153
1154int TWPartitionManager::Wipe_Rotate_Data(void) {
1155 if (!Mount_By_Path("/data", true))
1156 return false;
1157
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001158 unlink("/data/misc/akmd*");
1159 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001160 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001161 return true;
1162}
1163
1164int TWPartitionManager::Wipe_Battery_Stats(void) {
1165 struct stat st;
1166
1167 if (!Mount_By_Path("/data", true))
1168 return false;
1169
1170 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001171 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001172 } else {
1173 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001174 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001175 }
1176 return true;
1177}
1178
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001179int TWPartitionManager::Wipe_Android_Secure(void) {
1180 std::vector<TWPartition*>::iterator iter;
1181 int ret = false;
1182 bool found = false;
1183
1184 // Iterate through all partitions
1185 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1186 if ((*iter)->Has_Android_Secure) {
1187 ret = (*iter)->Wipe_AndSec();
1188 found = true;
1189 }
1190 }
1191 if (found) {
1192 return ret;
1193 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001194 gui_err("no_andsec=No android secure partitions found.");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001195 }
1196 return false;
1197}
1198
Dees_Troy38bd7602012-09-14 13:33:53 -04001199int TWPartitionManager::Format_Data(void) {
1200 TWPartition* dat = Find_Partition_By_Path("/data");
1201
1202 if (dat != NULL) {
1203 if (!dat->UnMount(true))
1204 return false;
1205
1206 return dat->Wipe_Encryption();
1207 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001208 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001209 return false;
1210 }
1211 return false;
1212}
1213
1214int TWPartitionManager::Wipe_Media_From_Data(void) {
1215 TWPartition* dat = Find_Partition_By_Path("/data");
1216
1217 if (dat != NULL) {
1218 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001219 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001220 return false;
1221 }
1222 if (!dat->Mount(true))
1223 return false;
1224
Ethan Yonker74db1572015-10-28 12:44:49 -05001225 gui_msg("wiping_datamedia=Wiping internal storage -- /data/media...");
Ethan Yonker726a0202014-12-16 20:01:38 -06001226 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001227 TWFunc::removeDir("/data/media", false);
xiaolu9416f4f2015-06-04 08:22:23 +08001228 dat->Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001229 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001230 return true;
1231 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001232 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001233 return false;
1234 }
1235 return false;
1236}
1237
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001238int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1239 std::vector<TWPartition*>::iterator iter;
1240 int ret = false;
1241 bool found = false;
1242 string Local_Path = TWFunc::Get_Root_Path(Path);
1243
1244 if (Local_Path == "/tmp" || Local_Path == "/")
1245 return true;
1246
1247 // Iterate through all partitions
1248 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1249 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1250 ret = (*iter)->Repair();
1251 found = true;
1252 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1253 (*iter)->Repair();
1254 }
1255 }
1256 if (found) {
1257 return ret;
1258 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001259 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 -05001260 } else {
1261 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1262 }
1263 return false;
1264}
1265
Ethan Yonkera2719152015-05-28 09:44:41 -05001266int TWPartitionManager::Resize_By_Path(string Path, bool Display_Error) {
1267 std::vector<TWPartition*>::iterator iter;
1268 int ret = false;
1269 bool found = false;
1270 string Local_Path = TWFunc::Get_Root_Path(Path);
1271
1272 if (Local_Path == "/tmp" || Local_Path == "/")
1273 return true;
1274
1275 // Iterate through all partitions
1276 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1277 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1278 ret = (*iter)->Resize();
1279 found = true;
1280 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1281 (*iter)->Resize();
1282 }
1283 }
1284 if (found) {
1285 return ret;
1286 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001287 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 -05001288 } else {
1289 LOGINFO("Resize: Unable to find partition for path '%s'\n", Local_Path.c_str());
1290 }
1291 return false;
1292}
1293
Dees_Troy51a0e822012-09-05 15:24:24 -04001294void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001295 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001296 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001297
Ethan Yonker74db1572015-10-28 12:44:49 -05001298 gui_msg("update_part_details=Updating partition details...");
Dees_Troy5bf43922012-09-07 16:07:55 -04001299 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001300 if ((*iter)->Can_Be_Mounted) {
1301 (*iter)->Update_Size(true);
1302 if ((*iter)->Mount_Point == "/system") {
1303 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1304 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1305 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1306 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1307 } else if ((*iter)->Mount_Point == "/cache") {
1308 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1309 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1310 } else if ((*iter)->Mount_Point == "/sd-ext") {
1311 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1312 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1313 if ((*iter)->Backup_Size == 0) {
1314 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1315 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1316 } else
1317 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001318 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001319 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001320 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001321 if ((*iter)->Backup_Size == 0) {
1322 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1323 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1324 } else
1325 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001326 } else if ((*iter)->Mount_Point == "/boot") {
1327 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1328 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1329 if ((*iter)->Backup_Size == 0) {
1330 DataManager::SetValue("tw_has_boot_partition", 0);
1331 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1332 } else
1333 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001334 }
Dees_Troyaa9cc402012-10-13 12:14:05 -04001335 } else {
1336 // Handle unmountable partitions in case we reset defaults
1337 if ((*iter)->Mount_Point == "/boot") {
1338 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1339 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1340 if ((*iter)->Backup_Size == 0) {
1341 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1342 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1343 } else
1344 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1345 } else if ((*iter)->Mount_Point == "/recovery") {
1346 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1347 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1348 if ((*iter)->Backup_Size == 0) {
1349 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1350 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1351 } else
1352 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001353 } else if ((*iter)->Mount_Point == "/data") {
1354 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001355 }
Dees_Troy51127312012-09-08 13:08:49 -04001356 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001357 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001358 gui_msg("update_part_details_done=...done");
Dees_Troy51127312012-09-08 13:08:49 -04001359 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1360 string current_storage_path = DataManager::GetCurrentStoragePath();
1361 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001362 if (FreeStorage != NULL) {
1363 // Attempt to mount storage
1364 if (!FreeStorage->Mount(false)) {
Matt Mower2d50cad2015-12-03 22:26:55 -06001365 gui_msg(Msg(msg::kError, "unable_to_mount_storage=Unable to mount storage"));
1366 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
Dees_Troy8170a922012-09-18 15:40:25 -04001367 } else {
1368 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1369 }
1370 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001371 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001372 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001373 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001374 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001375 return;
1376}
1377
1378int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001379#ifdef TW_INCLUDE_CRYPTO
1380 int ret_val, password_len;
Ethan Yonkera1de1492015-11-04 11:58:27 -06001381 char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX], cPassword[255];
Dees_Troy5bf43922012-09-07 16:07:55 -04001382 size_t result;
Ethan Yonker253368a2014-11-25 15:00:52 -06001383 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001384
Ethan Yonker253368a2014-11-25 15:00:52 -06001385 // Mount any partitions that need to be mounted for decrypt
1386 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1387 if ((*iter)->Mount_To_Decrypt) {
1388 (*iter)->Mount(true);
1389 }
a39552696ff55ce2013-01-08 16:14:56 +00001390 }
a39552696ff55ce2013-01-08 16:14:56 +00001391
Ethan Yonkera1de1492015-11-04 11:58:27 -06001392 property_get("ro.crypto.state", crypto_state, "error");
1393 if (strcmp(crypto_state, "error") == 0) {
1394 property_set("ro.crypto.state", "encrypted");
1395 // Sleep for a bit so that services can start if needed
1396 sleep(1);
1397 }
1398
Dees_Troy5bf43922012-09-07 16:07:55 -04001399 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001400 int pwret = cryptfs_check_passwd(cPassword);
1401
Ethan Yonker253368a2014-11-25 15:00:52 -06001402 // Unmount any partitions that were needed for decrypt
1403 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1404 if ((*iter)->Mount_To_Decrypt) {
1405 (*iter)->UnMount(false);
1406 }
1407 }
1408
a39552696ff55ce2013-01-08 16:14:56 +00001409 if (pwret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001410 gui_err("fail_decrypt=Failed to decrypt data.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001411 return -1;
1412 }
a39552696ff55ce2013-01-08 16:14:56 +00001413
Dees_Troy5bf43922012-09-07 16:07:55 -04001414 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1415 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001416 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001417 } else {
1418 TWPartition* dat = Find_Partition_By_Path("/data");
1419 if (dat != NULL) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001420 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1421 dat->Is_Decrypted = true;
1422 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001423 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001424 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 -06001425 gui_msg(Msg("decrypt_success_dev=Data successfully decrypted, new block device: '{1}'")(crypto_blkdev));
a39552696ff55ce2013-01-08 16:14:56 +00001426
Dees_Troy5bf43922012-09-07 16:07:55 -04001427 // Sleep for a bit so that the device will be ready
1428 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001429 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001430 dat->Storage_Path = "/data/media/0";
1431 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001432 DataManager::SetValue("tw_storage_path", "/data/media/0");
Ethan Yonkerec0fa4a2014-11-20 09:51:03 -06001433 DataManager::SetValue("tw_settings_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001434 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001435 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001436 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001437 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001438 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001439 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001440 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001441 }
1442 return 0;
1443#else
Ethan Yonker74db1572015-10-28 12:44:49 -05001444 gui_err("no_crypto_support=No crypto support was compiled into this build.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001445 return -1;
1446#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001447 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001448}
1449
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001450int TWPartitionManager::Fix_Contexts(void) {
thata3d31fb2014-12-21 22:27:40 +01001451#ifdef HAVE_SELINUX
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001452 std::vector<TWPartition*>::iterator iter;
1453 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1454 if ((*iter)->Has_Data_Media) {
1455 if ((*iter)->Mount(true)) {
1456 if (fixContexts::fixDataMediaContexts((*iter)->Mount_Point) != 0)
1457 return -1;
1458 }
1459 }
1460 }
Dees_Troy1a650e62012-10-19 20:54:32 -04001461 UnMount_Main_Partitions();
Ethan Yonker74db1572015-10-28 12:44:49 -05001462 gui_msg("done=Done.");
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001463 return 0;
1464#else
1465 LOGERR("Cannot fix contexts, no selinux support present.\n");
1466 return -1;
1467#endif
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001468}
1469
Ethan Yonker66a19492015-12-10 10:19:45 -06001470TWPartition* TWPartitionManager::Find_Next_Storage(string Path, bool Exclude_Data_Media) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001471 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1472
1473 if (!Path.empty()) {
1474 string Search_Path = TWFunc::Get_Root_Path(Path);
1475 for (; iter != Partitions.end(); iter++) {
Matt Mower9a561dd2016-02-22 21:25:51 -06001476 if ((*iter)->Mount_Point == Search_Path) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001477 iter++;
1478 break;
1479 }
1480 }
1481 }
1482
1483 for (; iter != Partitions.end(); iter++) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001484 if (Exclude_Data_Media && (*iter)->Has_Data_Media) {
1485 // do nothing, do not return this type of partition
1486 } else if ((*iter)->Is_Storage && (*iter)->Is_Present) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001487 return (*iter);
1488 }
1489 }
1490
1491 return NULL;
1492}
1493
Dees_Troyd21618c2012-10-14 18:48:49 -04001494int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001495 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1496
1497 if (Part == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001498 LOGINFO("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
1499 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Partition_Path));
Dees_Troyd21618c2012-10-14 18:48:49 -04001500 return false;
1501 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001502 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1503 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001504 return false;
1505
Dees_Troy2673cec2013-04-02 20:22:16 +00001506 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1507 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001508 return false;
1509 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001510 return true;
1511}
1512
Dees_Troy8170a922012-09-18 15:40:25 -04001513int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001514 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001515 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001516 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001517
Dees_Troy8170a922012-09-18 15:40:25 -04001518 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001519 string Lun_File_str = CUSTOM_LUN_FILE;
1520 size_t found = Lun_File_str.find("%");
1521 if (found != string::npos) {
1522 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1523 if (TWFunc::Path_Exists(lun_file))
1524 has_multiple_lun = true;
1525 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001526 mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage
Ethan Yonker47360be2014-04-01 10:34:34 -05001527 if (!has_multiple_lun) {
1528 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1529 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1530 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
Ethan Yonker66a19492015-12-10 10:19:45 -06001531 TWPartition* Mount = Find_Next_Storage("", true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001532 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001533 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1534 goto error_handle;
1535 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001536 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001537 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001538 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001539 }
1540 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001541 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001542 }
Dees_Troy8170a922012-09-18 15:40:25 -04001543 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001544 LOGINFO("Device has multiple lun files\n");
1545 TWPartition* Mount1;
1546 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001547 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker66a19492015-12-10 10:19:45 -06001548 Mount1 = Find_Next_Storage("", true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001549 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001550 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1551 goto error_handle;
1552 }
Matt Moweree717062014-04-26 01:46:46 -05001553 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker66a19492015-12-10 10:19:45 -06001554 Mount2 = Find_Next_Storage(Mount1->Mount_Point, true);
Matt Mower1fdcdb72016-01-25 20:53:47 -06001555 if (Mount2 && Mount2->Mount_Point != Mount1->Mount_Point) {
Matt Moweree717062014-04-26 01:46:46 -05001556 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001557 }
1558 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001559 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001560 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001561 }
Dees_Troy8170a922012-09-18 15:40:25 -04001562 }
Matt Mowerb6ef4782014-11-06 02:24:36 -06001563 property_set("sys.storage.ums_enabled", "1");
HandyMenny37d42992014-10-26 22:15:59 +01001564 property_set("sys.usb.config", "mass_storage,adb");
Dees_Troy8170a922012-09-18 15:40:25 -04001565 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001566error_handle:
1567 if (mtp_was_enabled)
1568 if (!Enable_MTP())
1569 Disable_MTP();
1570 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001571}
1572
1573int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001574 int index, ret;
1575 char lun_file[255], ch[2] = {0, 0};
1576 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001577
1578 for (index=0; index<2; index++) {
1579 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001580 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001581 if (ret < 0) {
1582 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001583 }
Dees_Troy8170a922012-09-18 15:40:25 -04001584 }
Dees_Troye58d5262012-09-21 12:27:57 -04001585 Mount_All_Storage();
1586 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001587 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001588 property_set("sys.storage.ums_enabled", "0");
HandyMenny37d42992014-10-26 22:15:59 +01001589 property_set("sys.usb.config", "adb");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001590 if (mtp_was_enabled)
1591 if (!Enable_MTP())
1592 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +0000