blob: 5194be8040bb5809fc9374a1ebc666ff42d686ed [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff7abc5fe2015-01-17 16:53:12 -05002 Copyright 2014 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>
Dees_Troy51a0e822012-09-05 15:24:24 -040033#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000034#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040036#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040037#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040038#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050039#include "twrpDigest.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050040#include "twrpDU.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060041#include "set_metadata.h"
bigbiff7abc5fe2015-01-17 16:53:12 -050042#include "tw_atomic.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050043#include "gui/gui.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040044
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040045#ifdef TW_HAS_MTP
46#include "mtp/mtp_MtpServer.hpp"
47#include "mtp/twrpMtp.hpp"
Ethan Yonker726a0202014-12-16 20:01:38 -060048#include "mtp/MtpMessage.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040049#endif
50
Dees Troy6f6441d2014-01-23 02:07:03 +000051extern "C" {
52 #include "cutils/properties.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050053 #include "gui/gui.h"
Dees Troy6f6441d2014-01-23 02:07:03 +000054}
55
Dees_Troy5bf43922012-09-07 16:07:55 -040056#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060057 #include "crypto/lollipop/cryptfs.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040058#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040059
Ethan Yonker6277c792014-09-15 14:54:30 -050060extern bool datamedia;
61
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050062TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040063 mtp_was_enabled = false;
Ethan Yonker726a0202014-12-16 20:01:38 -060064 mtp_write_fd = -1;
bigbiff7abc5fe2015-01-17 16:53:12 -050065 stop_backup.set_value(0);
66 tar_fork_pid = 0;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050067}
68
Dees_Troy51a0e822012-09-05 15:24:24 -040069int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040070 FILE *fstabFile;
71 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000072 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050073 TWPartition* andsec_partition = NULL;
Ethan Yonker726a0202014-12-16 20:01:38 -060074 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 -040075
76 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
77 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000078 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040079 return false;
80 }
81
82 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
83 if (fstab_line[0] != '/')
84 continue;
85
Dees_Troy2a923582012-09-20 12:13:34 -040086 if (fstab_line[strlen(fstab_line) - 1] != '\n')
87 fstab_line[strlen(fstab_line)] = '\n';
that9e0593e2014-10-08 00:01:24 +020088 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040089 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040090 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040091
Dees_Troy5bf43922012-09-07 16:07:55 -040092 if (partition->Process_Fstab_Line(line, Display_Error)) {
Ethan Yonker726a0202014-12-16 20:01:38 -060093 if (partition->Is_Storage) {
94 ++storageid;
95 partition->MTP_Storage_ID = storageid;
96 }
Matt Mowered71fa32014-04-16 13:21:47 -050097 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000098 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050099 } else {
100 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500101 }
Matt Mowered71fa32014-04-16 13:21:47 -0500102 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500103 andsec_partition = partition;
104 } else {
105 partition->Has_Android_Secure = false;
106 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500107 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -0400108 } else {
109 delete partition;
110 }
111 }
112 fclose(fstabFile);
Ethan Yonker6277c792014-09-15 14:54:30 -0500113 if (!datamedia && !settings_partition && Find_Partition_By_Path("/sdcard") == NULL && Find_Partition_By_Path("/internal_sd") == NULL && Find_Partition_By_Path("/internal_sdcard") == NULL && Find_Partition_By_Path("/emmc") == NULL) {
114 // Attempt to automatically identify /data/media emulated storage devices
115 TWPartition* Dat = Find_Partition_By_Path("/data");
116 if (Dat) {
117 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
118 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200119 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500120 settings_partition = Dat;
Ethan Yonker726a0202014-12-16 20:01:38 -0600121 // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID
122 ++storageid;
123 Dat->MTP_Storage_ID = storageid;
Ethan Yonker6277c792014-09-15 14:54:30 -0500124 }
125 }
Dees Troy02a64532014-03-19 15:23:32 +0000126 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500127 std::vector<TWPartition*>::iterator iter;
128 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
129 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000130 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500131 break;
132 }
133 }
Dees Troy02a64532014-03-19 15:23:32 +0000134 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000135 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500136 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400137 if (!Write_Fstab()) {
138 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000139 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400140 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000141 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400142 }
Matt Mowered71fa32014-04-16 13:21:47 -0500143
Matt Mowerbf4efa32014-04-14 23:25:26 -0500144 if (andsec_partition) {
145 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500146 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500147 Setup_Android_Secure_Location(settings_partition);
148 }
Matt Mowered71fa32014-04-16 13:21:47 -0500149 if (settings_partition) {
150 Setup_Settings_Storage_Partition(settings_partition);
151 }
Ethan Yonker253368a2014-11-25 15:00:52 -0600152#ifdef TW_INCLUDE_CRYPTO
Ethan Yonkercceebb82014-11-18 10:17:59 -0600153 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
154 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
155 int password_type = cryptfs_get_password_type();
156 if (password_type == CRYPT_TYPE_DEFAULT) {
157 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
158 if (Decrypt_Device("default_password") == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500159 gui_msg("decrypt_success=Successfully decrypted with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600160 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
161 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500162 gui_err("unable_to_decrypt=Unable to decrypt with default password.");
Ethan Yonkercceebb82014-11-18 10:17:59 -0600163 }
164 } else {
165 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
166 }
167 }
168#endif
Dees_Troy51127312012-09-08 13:08:49 -0400169 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400170 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400171 return true;
172}
173
174int TWPartitionManager::Write_Fstab(void) {
175 FILE *fp;
176 std::vector<TWPartition*>::iterator iter;
177 string Line;
178
179 fp = fopen("/etc/fstab", "w");
180 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000181 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400182 return false;
183 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400184 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400185 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400186 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400187 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000188 }
189 // Handle subpartition tracking
190 if ((*iter)->Is_SubPartition) {
191 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
192 if (ParentPartition)
193 ParentPartition->Has_SubPartition = true;
194 else
195 LOGERR("Unable to locate parent partition '%s' of '%s'\n", (*iter)->SubPartition_Of.c_str(), (*iter)->Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400196 }
197 }
198 fclose(fp);
199 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400200}
201
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500202void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500203 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
204 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
205 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
206}
207
Matt Mowerbf4efa32014-04-14 23:25:26 -0500208void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
209 if (Part->Has_Android_Secure)
210 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500211 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500212 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500213}
214
Dees_Troy8170a922012-09-18 15:40:25 -0400215void TWPartitionManager::Output_Partition_Logging(void) {
216 std::vector<TWPartition*>::iterator iter;
217
218 printf("\n\nPartition Logs:\n");
219 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
220 Output_Partition((*iter));
221}
222
223void TWPartitionManager::Output_Partition(TWPartition* Part) {
224 unsigned long long mb = 1048576;
225
Gary Peck004d48b2012-11-21 16:28:18 -0800226 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 -0400227 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800228 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 -0400229 }
Gary Peck004d48b2012-11-21 16:28:18 -0800230 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500231 if (Part->Can_Be_Mounted)
232 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800233 if (Part->Can_Be_Wiped)
234 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700235 if (Part->Use_Rm_Rf)
236 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500237 if (Part->Can_Be_Backed_Up)
238 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800239 if (Part->Wipe_During_Factory_Reset)
240 printf("Wipe_During_Factory_Reset ");
241 if (Part->Wipe_Available_in_GUI)
242 printf("Wipe_Available_in_GUI ");
243 if (Part->Is_SubPartition)
244 printf("Is_SubPartition ");
245 if (Part->Has_SubPartition)
246 printf("Has_SubPartition ");
247 if (Part->Removable)
248 printf("Removable ");
249 if (Part->Is_Present)
250 printf("IsPresent ");
251 if (Part->Can_Be_Encrypted)
252 printf("Can_Be_Encrypted ");
253 if (Part->Is_Encrypted)
254 printf("Is_Encrypted ");
255 if (Part->Is_Decrypted)
256 printf("Is_Decrypted ");
257 if (Part->Has_Data_Media)
258 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000259 if (Part->Can_Encrypt_Backup)
260 printf("Can_Encrypt_Backup ");
261 if (Part->Use_Userdata_Encryption)
262 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800263 if (Part->Has_Android_Secure)
264 printf("Has_Android_Secure ");
265 if (Part->Is_Storage)
266 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 if (Part->Is_Settings_Storage)
268 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000269 if (Part->Ignore_Blkid)
270 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000271 if (Part->Retain_Layout_Version)
272 printf("Retain_Layout_Version ");
Ethan Yonker253368a2014-11-25 15:00:52 -0600273 if (Part->Mount_To_Decrypt)
274 printf("Mount_To_Decrypt ");
Ethan Yonker96af84a2015-01-05 14:58:36 -0600275 if (Part->Can_Flash_Img)
276 printf("Can_Flash_Img ");
Gary Peck004d48b2012-11-21 16:28:18 -0800277 printf("\n");
278 if (!Part->SubPartition_Of.empty())
279 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
280 if (!Part->Symlink_Path.empty())
281 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
282 if (!Part->Symlink_Mount_Point.empty())
283 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
284 if (!Part->Primary_Block_Device.empty())
285 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
286 if (!Part->Alternate_Block_Device.empty())
287 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
288 if (!Part->Decrypted_Block_Device.empty())
289 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
Ethan Yonker253368a2014-11-25 15:00:52 -0600290 if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer")
291 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800292 if (Part->Length != 0)
293 printf(" Length: %i\n", Part->Length);
294 if (!Part->Display_Name.empty())
295 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500296 if (!Part->Storage_Name.empty())
297 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800298 if (!Part->Backup_Path.empty())
299 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
300 if (!Part->Backup_Name.empty())
301 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500302 if (!Part->Backup_Display_Name.empty())
303 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800304 if (!Part->Backup_FileName.empty())
305 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
306 if (!Part->Storage_Path.empty())
307 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
308 if (!Part->Current_File_System.empty())
309 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
310 if (!Part->Fstab_File_System.empty())
311 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
312 if (Part->Format_Block_Size != 0)
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500313 printf(" Format_Block_Size: %lu\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400314 if (!Part->MTD_Name.empty())
315 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400316 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500317 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800318 if (Part->Mount_Flags || !Part->Mount_Options.empty())
319 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600320 if (Part->MTP_Storage_ID)
321 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker6277c792014-09-15 14:54:30 -0500322 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400323}
324
Dees_Troy51a0e822012-09-05 15:24:24 -0400325int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400326 std::vector<TWPartition*>::iterator iter;
327 int ret = false;
328 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400329 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400330
Dees_Troyd93bda52013-07-03 19:55:19 +0000331 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400332 return true;
333
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 // Iterate through all partitions
335 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400336 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 ret = (*iter)->Mount(Display_Error);
338 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400339 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400340 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400341 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 }
343 if (found) {
344 return ret;
345 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500346 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 -0400347 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000348 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400349 }
350 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400351}
352
Dees_Troy51a0e822012-09-05 15:24:24 -0400353int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400354 std::vector<TWPartition*>::iterator iter;
355 int ret = false;
356 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400357 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400358
359 // Iterate through all partitions
360 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400361 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400362 ret = (*iter)->UnMount(Display_Error);
363 found = true;
364 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
365 (*iter)->UnMount(Display_Error);
366 }
367 }
368 if (found) {
369 return ret;
370 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500371 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 -0400372 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000373 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400374 }
375 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400376}
377
Dees_Troy51a0e822012-09-05 15:24:24 -0400378int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400379 TWPartition* Part = Find_Partition_By_Path(Path);
380
381 if (Part)
382 return Part->Is_Mounted();
383 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000384 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400385 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400386}
387
Dees_Troy5bf43922012-09-07 16:07:55 -0400388int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400389 string current_storage_path = DataManager::GetCurrentStoragePath();
390
391 if (Mount_By_Path(current_storage_path, Display_Error)) {
392 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
393 if (FreeStorage)
394 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
395 return true;
396 }
397 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400398}
399
Dees_Troy5bf43922012-09-07 16:07:55 -0400400int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
401 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
402}
403
404TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
405 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400406 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400407
408 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400409 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400410 return (*iter);
411 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400412 return NULL;
413}
414
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400415int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
416 // Check the backup name to ensure that it is the correct size and contains only valid characters
417 // and that a backup with that name doesn't already exist
418 char backup_name[MAX_BACKUP_NAME_LEN];
419 char backup_loc[255], tw_image_dir[255];
420 int copy_size;
421 int index, cur_char;
422 string Backup_Name, Backup_Loc;
423
424 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
425 copy_size = Backup_Name.size();
426 // Check size
427 if (copy_size > MAX_BACKUP_NAME_LEN) {
428 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500429 gui_err("backup_name_len=Backup name is too long.");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400430 return -2;
431 }
432
433 // Check each character
434 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500435 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400436 return 0; // A "0" (zero) means to use the current timestamp for the backup name
437 for (index=0; index<copy_size; index++) {
438 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500439 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 -0400440 // These are valid characters
441 // Numbers
442 // Upper case letters
443 // Lower case letters
444 // Space
445 // and -_.{}[]
446 } else {
447 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500448 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 -0400449 return -3;
450 }
451 }
452
453 // Check to make sure that a backup with this name doesn't already exist
454 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
455 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500456 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500457 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400458 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500459 gui_err("backup_name_exists=A backup with this name already exists.");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400460 return -4;
461 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400462 // No problems found, return 0
463 return 0;
464}
465
Dees_Troy43d8b002012-09-17 16:00:01 -0400466bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
467{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500468 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400469 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500470 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500471 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400472
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500473 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400474 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400475
Ethan Yonker74db1572015-10-28 12:44:49 -0500476 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, gui_parse_text("{@generating_md51}"));
477 gui_msg("generating_md52= * Generating md5...");
Dees_Troy43d8b002012-09-17 16:00:01 -0400478
479 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500480 md5sum.setfn(Backup_Folder + Backup_Filename);
481 if (md5sum.computeMD5() == 0)
482 if (md5sum.write_md5digest() == 0)
Ethan Yonker74db1572015-10-28 12:44:49 -0500483 gui_msg("md5_created= * MD5 Created.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500484 else
485 return -1;
486 else
Ethan Yonker74db1572015-10-28 12:44:49 -0500487 gui_err("md5_error= * MD5 Error!");
Dees_Troy43d8b002012-09-17 16:00:01 -0400488 } else {
489 char filename[512];
490 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500491 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400492 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500493 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000494 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400495 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000496 if (TWFunc::Path_Exists(filename)) {
497 if (md5sum.computeMD5() == 0) {
498 if (md5sum.write_md5digest() != 0)
499 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500500 gui_err("md5_error= * MD5 Error!");
Dees_Troy83bd4832013-05-04 12:39:56 +0000501 return false;
502 }
503 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500504 gui_err("md5_compute_error= * Error computing MD5.");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500505 return false;
506 }
507 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400508 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400509 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500510 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400511 }
512 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000513 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400514 return false;
515 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500516 gui_msg("md5_created= * MD5 Created.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400517 }
518 return true;
519}
520
Dees_Troy093b7642012-09-21 15:59:38 -0400521bool TWPartitionManager::Backup_Partition(TWPartition* Part, string Backup_Folder, bool generate_md5, unsigned long long* img_bytes_remaining, unsigned long long* file_bytes_remaining, unsigned long *img_time, unsigned long *file_time, unsigned long long *img_bytes, unsigned long long *file_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400522 time_t start, stop;
that203bcc92015-05-09 17:27:33 +0200523 int use_compression;
Dees_Troy093b7642012-09-21 15:59:38 -0400524 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500525 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400526
bigbiffbf1d6722015-02-14 16:25:59 -0500527 string backup_log = Backup_Folder + "recovery.log";
528
Dees_Troy43d8b002012-09-17 16:00:01 -0400529 if (Part == NULL)
530 return true;
531
Dees_Troy093b7642012-09-21 15:59:38 -0400532 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400533
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500534 total_size = *file_bytes + *img_bytes;
535 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
that203bcc92015-05-09 17:27:33 +0200536 // Set the position
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500537 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000538 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400539
Tom Hite5a926722014-09-15 01:31:03 +0000540 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400541 time(&start);
542
bigbiff7abc5fe2015-01-17 16:53:12 -0500543 if (Part->Backup(Backup_Folder, &total_size, &current_size, tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000544 bool md5Success = false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500545 current_size += Part->Backup_Size;
546 pos = (float)((float)(current_size) / (float)(total_size));
547 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400548 if (Part->Has_SubPartition) {
549 std::vector<TWPartition*>::iterator subpart;
550
551 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500552 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500553 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size, tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000554 TWFunc::SetPerformanceMode(false);
bigbiffbf1d6722015-02-14 16:25:59 -0500555 Clean_Backup_Folder(Backup_Folder);
556 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
557 tw_set_default_metadata(backup_log.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400558 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000559 }
Dees_Troy2727b992013-08-14 20:09:30 +0000560 sync();
561 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000562 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
563 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400564 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000565 }
Dees_Troy093b7642012-09-21 15:59:38 -0400566 if (Part->Backup_Method == 1) {
567 *file_bytes_remaining -= (*subpart)->Backup_Size;
568 } else {
569 *img_bytes_remaining -= (*subpart)->Backup_Size;
570 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500571 current_size += Part->Backup_Size;
572 pos = (float)(current_size / total_size);
573 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400574 }
575 }
576 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400577 time(&stop);
that203bcc92015-05-09 17:27:33 +0200578 int backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000579 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400580 if (Part->Backup_Method == 1) {
581 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400582 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400583 } else {
584 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400585 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400586 }
Tom Hite5a926722014-09-15 01:31:03 +0000587
588 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
589 TWFunc::SetPerformanceMode(false);
590 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400591 } else {
bigbiffbf1d6722015-02-14 16:25:59 -0500592 Clean_Backup_Folder(Backup_Folder);
593 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
594 tw_set_default_metadata(backup_log.c_str());
Tom Hite5a926722014-09-15 01:31:03 +0000595 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400596 return false;
597 }
bigbiff7abc5fe2015-01-17 16:53:12 -0500598 return 0;
599}
600
bigbiffbf1d6722015-02-14 16:25:59 -0500601void TWPartitionManager::Clean_Backup_Folder(string Backup_Folder) {
602 DIR *d = opendir(Backup_Folder.c_str());
603 struct dirent *p;
604 int r;
605
Ethan Yonker74db1572015-10-28 12:44:49 -0500606 gui_msg("backup_clean=Backup Failed. Cleaning Backup Folder.");
bigbiffbf1d6722015-02-14 16:25:59 -0500607
608 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500609 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Backup_Folder)(strerror(errno)));
bigbiffbf1d6722015-02-14 16:25:59 -0500610 return;
611 }
612
Matt Mower2b18a532015-02-20 16:58:05 -0600613 while ((p = readdir(d))) {
bigbiffbf1d6722015-02-14 16:25:59 -0500614 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
615 continue;
616
617 string path = Backup_Folder + p->d_name;
618
619 size_t dot = path.find_last_of(".") + 1;
620 if (path.substr(dot) == "win" || path.substr(dot) == "md5" || path.substr(dot) == "info") {
621 r = unlink(path.c_str());
622 if (r != 0) {
623 LOGINFO("Unable to unlink '%s: %s'\n", path.c_str(), strerror(errno));
624 }
625 }
626 }
627 closedir(d);
628}
629
bigbiff7abc5fe2015-01-17 16:53:12 -0500630int TWPartitionManager::Cancel_Backup() {
631 string Backup_Folder, Backup_Name, Full_Backup_Path;
632
633 stop_backup.set_value(1);
634
635 if (tar_fork_pid != 0) {
636 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
637 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
638 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
639 LOGINFO("Killing pid: %d\n", tar_fork_pid);
640 kill(tar_fork_pid, SIGUSR2);
641 while (kill(tar_fork_pid, 0) == 0) {
642 usleep(1000);
643 }
644 LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n");
645 LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str());
646 TWFunc::removeDir(Full_Backup_Path, false);
647 tar_fork_pid = 0;
648 }
649
650 return 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400651}
652
653int TWPartitionManager::Run_Backup(void) {
Matt Mower2b18a532015-02-20 16:58:05 -0600654 int check, do_md5, partition_count = 0, disable_free_space_check = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500655 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400656 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 -0400657 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500658 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400659 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400660 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400661 struct tm *t;
662 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500663 size_t start_pos = 0, end_pos = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500664 stop_backup.set_value(0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400665 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500666 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400667
668 time(&total_start);
669
670 Update_System_Details();
671
672 if (!Mount_Current_Storage(true))
673 return false;
674
675 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400676 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400677 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400678 else
679 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400680
Dees_Troy43d8b002012-09-17 16:00:01 -0400681 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
682 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Ethan Yonker74db1572015-10-28 12:44:49 -0500683 if (Backup_Name == gui_parse_text("{@current_date}")) {
Dees Troyb21cc642013-09-10 17:36:41 +0000684 Backup_Name = TWFunc::Get_Current_Date();
Ethan Yonker74db1572015-10-28 12:44:49 -0500685 } else if (Backup_Name == gui_parse_text("{@auto_generate}") || Backup_Name == "0" || Backup_Name.empty()) {
Dees Troyb21cc642013-09-10 17:36:41 +0000686 TWFunc::Auto_Generate_Backup_Name();
687 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400688 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000689 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400690 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000691 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400692
Dees_Troy2673cec2013-04-02 20:22:16 +0000693 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500694 DataManager::GetValue("tw_backup_list", Backup_List);
695 if (!Backup_List.empty()) {
696 end_pos = Backup_List.find(";", start_pos);
697 while (end_pos != string::npos && start_pos < Backup_List.size()) {
698 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
699 backup_part = Find_Partition_By_Path(backup_path);
700 if (backup_part != NULL) {
701 partition_count++;
702 if (backup_part->Backup_Method == 1)
703 file_bytes += backup_part->Backup_Size;
704 else
705 img_bytes += backup_part->Backup_Size;
706 if (backup_part->Has_SubPartition) {
707 std::vector<TWPartition*>::iterator subpart;
708
709 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000710 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 -0500711 partition_count++;
712 if ((*subpart)->Backup_Method == 1)
713 file_bytes += (*subpart)->Backup_Size;
714 else
715 img_bytes += (*subpart)->Backup_Size;
716 }
717 }
Dees_Troy8170a922012-09-18 15:40:25 -0400718 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500719 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500720 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 -0400721 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500722 start_pos = end_pos + 1;
723 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400724 }
725 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400726
727 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500728 gui_msg("no_partition_selected=No partitions selected for backup.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400729 return false;
730 }
731 total_bytes = file_bytes + img_bytes;
Ethan Yonker74db1572015-10-28 12:44:49 -0500732 gui_msg(Msg("total_partitions_backup= * Total number of partitions to back up: {1}")(partition_count));
733 gui_msg(Msg("total_backup_size= * Total size of all data: {1}MB")(total_bytes / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400734 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
735 if (storage != NULL) {
736 free_space = storage->Free;
Ethan Yonker74db1572015-10-28 12:44:49 -0500737 gui_msg(Msg("available_space= * Available space: {1}MB")(free_space / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400738 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500739 gui_err("unable_locate_storage=Unable to locate storage device.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400740 return false;
741 }
bigbiffbf1d6722015-02-14 16:25:59 -0500742
743 DataManager::GetValue("tw_disable_free_space", disable_free_space_check);
744 if (!disable_free_space_check) {
745 if (free_space - (32 * 1024 * 1024) < total_bytes) {
746 // We require an extra 32MB just in case
Ethan Yonker74db1572015-10-28 12:44:49 -0500747 gui_err("no_space=Not enough free space on storage.");
bigbiffbf1d6722015-02-14 16:25:59 -0500748 return false;
749 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400750 }
751 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500752 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400753
Ethan Yonker74db1572015-10-28 12:44:49 -0500754 gui_msg("backup_started=[BACKUP STARTED]");
755 gui_msg(Msg("backup_folder= * Backup Folder: {1}")(Full_Backup_Path));
Dees_Troyd4b22b02013-01-18 17:17:58 +0000756 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500757 gui_err("fail_backup_folder=Failed to make backup folder.");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000758 return false;
759 }
760
Dees_Troy2673cec2013-04-02 20:22:16 +0000761 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400762
Dees_Troya13d74f2013-03-24 08:54:55 -0500763 start_pos = 0;
764 end_pos = Backup_List.find(";", start_pos);
765 while (end_pos != string::npos && start_pos < Backup_List.size()) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500766 if (stop_backup.get_value() != 0)
767 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500768 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
769 backup_part = Find_Partition_By_Path(backup_path);
770 if (backup_part != NULL) {
771 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
772 return false;
773 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500774 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 -0500775 }
776 start_pos = end_pos + 1;
777 end_pos = Backup_List.find(";", start_pos);
778 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400779
780 // Average BPS
781 if (img_time == 0)
782 img_time = 1;
783 if (file_time == 0)
784 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400785 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500786 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400787
Ethan Yonker74db1572015-10-28 12:44:49 -0500788 gui_msg(Msg("avg_backup_fs=Average backup rate for file systems: {1} MB/sec")(file_bps / (1024 * 1024)));
789 gui_msg(Msg("avg_backup_img=Average backup rate for imaged drives: {1} MB/sec")(img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400790
791 time(&total_stop);
792 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500793 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500794 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400795
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500796 int prev_img_bps, use_compression;
797 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400798 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
799 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500800 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400801
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500802 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400803 if (use_compression)
804 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500805 else
Dees_Troy093b7642012-09-21 15:59:38 -0400806 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
807 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500808 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400809
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500810 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400811 if (use_compression)
812 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
813 else
814 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
815
Ethan Yonker74db1572015-10-28 12:44:49 -0500816 gui_msg(Msg("total_backed_size=[{1} MB TOTAL BACKED UP]")(actual_backup_size));
Dees_Troy43d8b002012-09-17 16:00:01 -0400817 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400818 UnMount_Main_Partitions();
Ethan Yonker56db1c42015-12-20 22:49:00 -0600819 gui_msg(Msg(msg::kHighlight, "backup_completed=[BACKUP COMPLETED IN {1} SECONDS]")(total_time)); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000820 string backup_log = Full_Backup_Path + "recovery.log";
821 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600822 tw_set_default_metadata(backup_log.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000823 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400824}
825
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500826bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count, const unsigned long long *total_restore_size, unsigned long long *already_restored_size) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400827 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000828 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400829 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500830 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Tom Hite5a926722014-09-15 01:31:03 +0000831 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) {
832 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400833 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000834 }
Dees_Troy8170a922012-09-18 15:40:25 -0400835 if (Part->Has_SubPartition) {
836 std::vector<TWPartition*>::iterator subpart;
837
838 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
839 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000840 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) {
841 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400842 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000843 }
Dees_Troy8170a922012-09-18 15:40:25 -0400844 }
845 }
846 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400847 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000848 TWFunc::SetPerformanceMode(false);
Ethan Yonker74db1572015-10-28 12:44:49 -0500849 gui_msg(Msg("restort_part_done=[{1} done ({2} seconds)]")(Part->Backup_Display_Name)((int)difftime(Stop, Start)));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400850 return true;
851}
852
Dees_Troy51a0e822012-09-05 15:24:24 -0400853int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400854 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500855 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400856 time_t rStart, rStop;
857 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500858 string Restore_List, restore_path;
859 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500860 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400861
Ethan Yonker74db1572015-10-28 12:44:49 -0500862 gui_msg("restore_started=[RESTORE STARTED]");
863 gui_msg(Msg("restore_folder=Restore folder: '{1}'")(Restore_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -0400864
Dees_Troy4a2a1262012-09-18 09:33:47 -0400865 if (!Mount_Current_Storage(true))
866 return false;
867
868 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500869 if (check_md5 > 0) {
870 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Ethan Yonker74db1572015-10-28 12:44:49 -0500871 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, gui_parse_text("{@verifying_md5}"));
872 gui_msg("verifying_md5=Verifying MD5");
Dees_Troya13d74f2013-03-24 08:54:55 -0500873 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500874 gui_msg("skip_md5=Skipping MD5 check based on user setting.");
Dees_Troya13d74f2013-03-24 08:54:55 -0500875 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500876 gui_msg("calc_restore=Calculating restore details...");
Dees_Troya13d74f2013-03-24 08:54:55 -0500877 DataManager::GetValue("tw_restore_selected", Restore_List);
878 if (!Restore_List.empty()) {
879 end_pos = Restore_List.find(";", start_pos);
880 while (end_pos != string::npos && start_pos < Restore_List.size()) {
881 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
882 restore_part = Find_Partition_By_Path(restore_path);
883 if (restore_part != NULL) {
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500884 if (restore_part->Mount_Read_Only) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500885 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 -0500886 return false;
887 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500888 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
889 return false;
Ethan Yonkereb32b1f2015-05-18 10:23:03 -0500890 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500891 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500892 if (restore_part->Has_SubPartition) {
893 std::vector<TWPartition*>::iterator subpart;
894
895 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
896 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400897 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500898 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500899 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500900 }
901 }
902 }
903 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500904 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500905 }
906 start_pos = end_pos + 1;
907 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400908 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400909 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400910
911 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500912 gui_err("no_part_restore=No partitions selected for restore.");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400913 return false;
914 }
915
Ethan Yonker74db1572015-10-28 12:44:49 -0500916 gui_msg(Msg("restore_part_count=Restoring {1} partitions...")(partition_count));
917 gui_msg(Msg("total_restore_size=Total restore size is {1}MB")(total_restore_size / 1048576));
Dees_Troy2673cec2013-04-02 20:22:16 +0000918 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500919
Dees_Troya13d74f2013-03-24 08:54:55 -0500920 start_pos = 0;
921 if (!Restore_List.empty()) {
922 end_pos = Restore_List.find(";", start_pos);
923 while (end_pos != string::npos && start_pos < Restore_List.size()) {
924 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
925 restore_part = Find_Partition_By_Path(restore_path);
926 if (restore_part != NULL) {
927 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500928 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500929 return false;
930 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500931 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500932 }
933 start_pos = end_pos + 1;
934 end_pos = Restore_List.find(";", start_pos);
935 }
936 }
Ethan Yonker74db1572015-10-28 12:44:49 -0500937 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, gui_parse_text("{@updating_system_details}"));
Dees_Troy43d8b002012-09-17 16:00:01 -0400938 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400939 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400940 time(&rStop);
Ethan Yonker74db1572015-10-28 12:44:49 -0500941 gui_msg(Msg(msg::kHighlight, "restore_complete=[RESTORE COMPLETED IN {1} SECONDS]")((int)difftime(rStop,rStart)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500942 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400943 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400944}
945
946void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400947 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500948 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000949 bool get_date = true, check_encryption = true;
950
951 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400952
953 DIR* d;
954 d = opendir(Restore_Name.c_str());
955 if (d == NULL)
956 {
Ethan Yonker74db1572015-10-28 12:44:49 -0500957 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Name)(strerror(errno)));
Dees_Troy63c8df72012-09-10 14:02:05 -0400958 return;
959 }
960
961 struct dirent* de;
962 while ((de = readdir(d)) != NULL)
963 {
964 // Strip off three components
965 char str[256];
966 char* label;
967 char* fstype = NULL;
968 char* extn = NULL;
969 char* ptr;
970
971 strcpy(str, de->d_name);
972 if (strlen(str) <= 2)
973 continue;
974
975 if (get_date) {
976 char file_path[255];
977 struct stat st;
978
979 strcpy(file_path, Restore_Name.c_str());
980 strcat(file_path, "/");
981 strcat(file_path, str);
982 stat(file_path, &st);
983 string backup_date = ctime((const time_t*)(&st.st_mtime));
984 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
985 get_date = false;
986 }
987
988 label = str;
989 ptr = label;
990 while (*ptr && *ptr != '.') ptr++;
991 if (*ptr == '.')
992 {
993 *ptr = 0x00;
994 ptr++;
995 fstype = ptr;
996 }
997 while (*ptr && *ptr != '.') ptr++;
998 if (*ptr == '.')
999 {
1000 *ptr = 0x00;
1001 ptr++;
1002 extn = ptr;
1003 }
1004
Dees_Troy83bd4832013-05-04 12:39:56 +00001005 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001006 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001007 if (extnlength != 3 && extnlength != 6) continue;
1008 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1009 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1010
1011 if (check_encryption) {
1012 string filename = Restore_Name + "/";
1013 filename += de->d_name;
1014 if (TWFunc::Get_File_Type(filename) == 2) {
1015 LOGINFO("'%s' is encrypted\n", filename.c_str());
1016 DataManager::SetValue("tw_restore_encrypted", 1);
1017 }
1018 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001019 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001020
1021 TWPartition* Part = Find_Partition_By_Path(label);
1022 if (Part == NULL)
1023 {
Ethan Yonker74db1572015-10-28 12:44:49 -05001024 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 -04001025 continue;
1026 }
1027
1028 Part->Backup_FileName = de->d_name;
1029 if (strlen(extn) > 3) {
1030 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1031 }
1032
Dees_Troya13d74f2013-03-24 08:54:55 -05001033 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001034 }
1035 closedir(d);
1036
Dees_Troya13d74f2013-03-24 08:54:55 -05001037 // Set the final value
1038 DataManager::SetValue("tw_restore_list", Restore_List);
1039 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001040 return;
1041}
1042
1043int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001044 std::vector<TWPartition*>::iterator iter;
1045 int ret = false;
1046 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001047 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001048
1049 // Iterate through all partitions
1050 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001051 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001052 if (Path == "/and-sec")
1053 ret = (*iter)->Wipe_AndSec();
1054 else
1055 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001056 found = true;
1057 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1058 (*iter)->Wipe();
1059 }
1060 }
1061 if (found) {
1062 return ret;
1063 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001064 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 -04001065 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001066}
1067
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001068int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1069 std::vector<TWPartition*>::iterator iter;
1070 int ret = false;
1071 bool found = false;
1072 string Local_Path = TWFunc::Get_Root_Path(Path);
1073
1074 // Iterate through all partitions
1075 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1076 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1077 if (Path == "/and-sec")
1078 ret = (*iter)->Wipe_AndSec();
1079 else
1080 ret = (*iter)->Wipe(New_File_System);
1081 found = true;
1082 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1083 (*iter)->Wipe(New_File_System);
1084 }
1085 }
1086 if (found) {
1087 return ret;
1088 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001089 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 -05001090 return false;
1091}
1092
Dees_Troy51a0e822012-09-05 15:24:24 -04001093int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001094 std::vector<TWPartition*>::iterator iter;
1095 int ret = true;
1096
1097 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001098 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Ethan Yonker89583ef2015-08-26 09:01:59 -05001099#ifdef TW_OEM_BUILD
1100 if ((*iter)->Mount_Point == "/data") {
1101 if (!(*iter)->Wipe_Encryption())
1102 ret = false;
1103 } else {
1104#endif
1105 if (!(*iter)->Wipe())
1106 ret = false;
1107#ifdef TW_OEM_BUILD
1108 }
1109#endif
Dees_Troy094207a2012-09-26 12:00:39 -04001110 } else if ((*iter)->Has_Android_Secure) {
1111 if (!(*iter)->Wipe_AndSec())
1112 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001113 }
1114 }
1115 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001116}
1117
Dees_Troy38bd7602012-09-14 13:33:53 -04001118int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1119 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001120 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001121
1122 if (!Mount_By_Path("/data", true))
1123 return false;
1124
1125 if (!Mount_By_Path("/cache", true))
1126 return false;
1127
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001128 dir.push_back("/data/dalvik-cache");
1129 dir.push_back("/cache/dalvik-cache");
1130 dir.push_back("/cache/dc");
Ethan Yonker74db1572015-10-28 12:44:49 -05001131 gui_msg("wiping_dalvik=Wiping Dalvik Cache Directories...");
Dees_Troya13d74f2013-03-24 08:54:55 -05001132 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001133 if (stat(dir.at(i).c_str(), &st) == 0) {
1134 TWFunc::removeDir(dir.at(i), false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001135 gui_msg(Msg("cleaned=Cleaned: {1}...")(dir.at(i)));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001136 }
1137 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001138 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001139 if (sdext && sdext->Is_Present && sdext->Mount(false))
1140 {
1141 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1142 {
1143 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Ethan Yonker74db1572015-10-28 12:44:49 -05001144 gui_msg(Msg("cleaned=Cleaned: {1}...")("/sd-ext/dalvik-cache"));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001145 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001146 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001147 gui_msg("dalvik_done=-- Dalvik Cache Directories Wipe Complete!");
Dees_Troy38bd7602012-09-14 13:33:53 -04001148 return true;
1149}
1150
1151int TWPartitionManager::Wipe_Rotate_Data(void) {
1152 if (!Mount_By_Path("/data", true))
1153 return false;
1154
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001155 unlink("/data/misc/akmd*");
1156 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001157 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001158 return true;
1159}
1160
1161int TWPartitionManager::Wipe_Battery_Stats(void) {
1162 struct stat st;
1163
1164 if (!Mount_By_Path("/data", true))
1165 return false;
1166
1167 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001168 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001169 } else {
1170 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001171 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001172 }
1173 return true;
1174}
1175
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001176int TWPartitionManager::Wipe_Android_Secure(void) {
1177 std::vector<TWPartition*>::iterator iter;
1178 int ret = false;
1179 bool found = false;
1180
1181 // Iterate through all partitions
1182 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1183 if ((*iter)->Has_Android_Secure) {
1184 ret = (*iter)->Wipe_AndSec();
1185 found = true;
1186 }
1187 }
1188 if (found) {
1189 return ret;
1190 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001191 gui_err("no_andsec=No android secure partitions found.");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001192 }
1193 return false;
1194}
1195
Dees_Troy38bd7602012-09-14 13:33:53 -04001196int TWPartitionManager::Format_Data(void) {
1197 TWPartition* dat = Find_Partition_By_Path("/data");
1198
1199 if (dat != NULL) {
1200 if (!dat->UnMount(true))
1201 return false;
1202
1203 return dat->Wipe_Encryption();
1204 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001205 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1].")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001206 return false;
1207 }
1208 return false;
1209}
1210
1211int TWPartitionManager::Wipe_Media_From_Data(void) {
1212 TWPartition* dat = Find_Partition_By_Path("/data");
1213
1214 if (dat != NULL) {
1215 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001216 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001217 return false;
1218 }
1219 if (!dat->Mount(true))
1220 return false;
1221
Ethan Yonker74db1572015-10-28 12:44:49 -05001222 gui_msg("wiping_datamedia=Wiping internal storage -- /data/media...");
Ethan Yonker726a0202014-12-16 20:01:38 -06001223 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001224 TWFunc::removeDir("/data/media", false);
xiaolu9416f4f2015-06-04 08:22:23 +08001225 dat->Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001226 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001227 return true;
1228 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001229 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1].")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001230 return false;
1231 }
1232 return false;
1233}
1234
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001235int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1236 std::vector<TWPartition*>::iterator iter;
1237 int ret = false;
1238 bool found = false;
1239 string Local_Path = TWFunc::Get_Root_Path(Path);
1240
1241 if (Local_Path == "/tmp" || Local_Path == "/")
1242 return true;
1243
1244 // Iterate through all partitions
1245 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1246 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1247 ret = (*iter)->Repair();
1248 found = true;
1249 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1250 (*iter)->Repair();
1251 }
1252 }
1253 if (found) {
1254 return ret;
1255 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001256 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 -05001257 } else {
1258 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1259 }
1260 return false;
1261}
1262
Ethan Yonkera2719152015-05-28 09:44:41 -05001263int TWPartitionManager::Resize_By_Path(string Path, bool Display_Error) {
1264 std::vector<TWPartition*>::iterator iter;
1265 int ret = false;
1266 bool found = false;
1267 string Local_Path = TWFunc::Get_Root_Path(Path);
1268
1269 if (Local_Path == "/tmp" || Local_Path == "/")
1270 return true;
1271
1272 // Iterate through all partitions
1273 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1274 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1275 ret = (*iter)->Resize();
1276 found = true;
1277 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1278 (*iter)->Resize();
1279 }
1280 }
1281 if (found) {
1282 return ret;
1283 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001284 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 -05001285 } else {
1286 LOGINFO("Resize: Unable to find partition for path '%s'\n", Local_Path.c_str());
1287 }
1288 return false;
1289}
1290
Dees_Troy51a0e822012-09-05 15:24:24 -04001291void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001292 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001293 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001294
Ethan Yonker74db1572015-10-28 12:44:49 -05001295 gui_msg("update_part_details=Updating partition details...");
Dees_Troy5bf43922012-09-07 16:07:55 -04001296 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001297 if ((*iter)->Can_Be_Mounted) {
1298 (*iter)->Update_Size(true);
1299 if ((*iter)->Mount_Point == "/system") {
1300 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1301 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1302 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1303 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1304 } else if ((*iter)->Mount_Point == "/cache") {
1305 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1306 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1307 } else if ((*iter)->Mount_Point == "/sd-ext") {
1308 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1309 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1310 if ((*iter)->Backup_Size == 0) {
1311 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1312 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1313 } else
1314 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001315 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001316 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001317 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001318 if ((*iter)->Backup_Size == 0) {
1319 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1320 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1321 } else
1322 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001323 } else if ((*iter)->Mount_Point == "/boot") {
1324 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1325 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1326 if ((*iter)->Backup_Size == 0) {
1327 DataManager::SetValue("tw_has_boot_partition", 0);
1328 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1329 } else
1330 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001331 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001332#ifdef SP1_NAME
1333 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1334 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1335 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1336 }
1337#endif
1338#ifdef SP2_NAME
1339 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1340 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1341 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1342 }
1343#endif
1344#ifdef SP3_NAME
1345 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1346 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1347 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1348 }
1349#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001350 } else {
1351 // Handle unmountable partitions in case we reset defaults
1352 if ((*iter)->Mount_Point == "/boot") {
1353 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1354 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1355 if ((*iter)->Backup_Size == 0) {
1356 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1357 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1358 } else
1359 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1360 } else if ((*iter)->Mount_Point == "/recovery") {
1361 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1362 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1363 if ((*iter)->Backup_Size == 0) {
1364 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1365 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1366 } else
1367 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001368 } else if ((*iter)->Mount_Point == "/data") {
1369 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001370 }
1371#ifdef SP1_NAME
1372 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1373 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1374 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1375 }
1376#endif
1377#ifdef SP2_NAME
1378 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1379 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1380 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1381 }
1382#endif
1383#ifdef SP3_NAME
1384 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1385 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1386 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1387 }
1388#endif
Dees_Troy51127312012-09-08 13:08:49 -04001389 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001390 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001391 gui_msg("update_part_details_done=...done");
Dees_Troy51127312012-09-08 13:08:49 -04001392 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1393 string current_storage_path = DataManager::GetCurrentStoragePath();
1394 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001395 if (FreeStorage != NULL) {
1396 // Attempt to mount storage
1397 if (!FreeStorage->Mount(false)) {
Matt Mower2d50cad2015-12-03 22:26:55 -06001398 gui_msg(Msg(msg::kError, "unable_to_mount_storage=Unable to mount storage"));
1399 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
Dees_Troy8170a922012-09-18 15:40:25 -04001400 } else {
1401 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1402 }
1403 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001404 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001405 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001406 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001407 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001408 return;
1409}
1410
1411int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001412#ifdef TW_INCLUDE_CRYPTO
1413 int ret_val, password_len;
Ethan Yonkera1de1492015-11-04 11:58:27 -06001414 char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX], cPassword[255];
Dees_Troy5bf43922012-09-07 16:07:55 -04001415 size_t result;
Ethan Yonker253368a2014-11-25 15:00:52 -06001416 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001417
Ethan Yonker253368a2014-11-25 15:00:52 -06001418 // Mount any partitions that need to be mounted for decrypt
1419 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1420 if ((*iter)->Mount_To_Decrypt) {
1421 (*iter)->Mount(true);
1422 }
a39552696ff55ce2013-01-08 16:14:56 +00001423 }
a39552696ff55ce2013-01-08 16:14:56 +00001424
Ethan Yonkera1de1492015-11-04 11:58:27 -06001425 property_get("ro.crypto.state", crypto_state, "error");
1426 if (strcmp(crypto_state, "error") == 0) {
1427 property_set("ro.crypto.state", "encrypted");
1428 // Sleep for a bit so that services can start if needed
1429 sleep(1);
1430 }
1431
Dees_Troy5bf43922012-09-07 16:07:55 -04001432 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001433 int pwret = cryptfs_check_passwd(cPassword);
1434
Ethan Yonker253368a2014-11-25 15:00:52 -06001435 // Unmount any partitions that were needed for decrypt
1436 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1437 if ((*iter)->Mount_To_Decrypt) {
1438 (*iter)->UnMount(false);
1439 }
1440 }
1441
a39552696ff55ce2013-01-08 16:14:56 +00001442 if (pwret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001443 gui_err("fail_decrypt=Failed to decrypt data.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001444 return -1;
1445 }
a39552696ff55ce2013-01-08 16:14:56 +00001446
Dees_Troy5bf43922012-09-07 16:07:55 -04001447 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1448 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001449 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001450 } else {
1451 TWPartition* dat = Find_Partition_By_Path("/data");
1452 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001453 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001454 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1455 dat->Is_Decrypted = true;
1456 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001457 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001458 dat->Current_File_System = dat->Fstab_File_System; // Needed if we're ignoring blkid because encrypted devices start out as emmc
Ethan Yonker74db1572015-10-28 12:44:49 -05001459 gui_msg(Msg("decrypt_success=Data successfully decrypted, new block device: '{1}'")(crypto_blkdev));
a39552696ff55ce2013-01-08 16:14:56 +00001460
Dees_Troy5bf43922012-09-07 16:07:55 -04001461 // Sleep for a bit so that the device will be ready
1462 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001463 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001464 dat->Storage_Path = "/data/media/0";
1465 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001466 DataManager::SetValue("tw_storage_path", "/data/media/0");
Ethan Yonkerec0fa4a2014-11-20 09:51:03 -06001467 DataManager::SetValue("tw_settings_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001468 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001469 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001470 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001471 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001472 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001473 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001474 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001475 }
1476 return 0;
1477#else
Ethan Yonker74db1572015-10-28 12:44:49 -05001478 gui_err("no_crypto_support=No crypto support was compiled into this build.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001479 return -1;
1480#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001481 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001482}
1483
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001484int TWPartitionManager::Fix_Permissions(void) {
1485 int result = 0;
1486 if (!Mount_By_Path("/data", true))
1487 return false;
1488
1489 if (!Mount_By_Path("/system", true))
1490 return false;
1491
1492 Mount_By_Path("/sd-ext", false);
1493
1494 fixPermissions perms;
1495 result = perms.fixPerms(true, false);
thata3d31fb2014-12-21 22:27:40 +01001496#ifdef HAVE_SELINUX
1497 if (result == 0 && DataManager::GetIntValue("tw_fixperms_restorecon") == 1)
1498 result = perms.fixContexts();
1499#endif
Dees_Troy1a650e62012-10-19 20:54:32 -04001500 UnMount_Main_Partitions();
Ethan Yonker74db1572015-10-28 12:44:49 -05001501 gui_msg("done=Done.");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001502 return result;
1503}
1504
Ethan Yonker47360be2014-04-01 10:34:34 -05001505TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1506 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1507
1508 if (!Path.empty()) {
1509 string Search_Path = TWFunc::Get_Root_Path(Path);
1510 for (; iter != Partitions.end(); iter++) {
1511 if ((*iter)->Mount_Point == Search_Path) {
1512 iter++;
1513 break;
1514 }
1515 }
1516 }
1517
1518 for (; iter != Partitions.end(); iter++) {
1519 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1520 return (*iter);
1521 }
1522 }
1523
1524 return NULL;
1525}
1526
Dees_Troyd21618c2012-10-14 18:48:49 -04001527int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001528 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1529
1530 if (Part == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001531 LOGINFO("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
1532 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Partition_Path));
Dees_Troyd21618c2012-10-14 18:48:49 -04001533 return false;
1534 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001535 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1536 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001537 return false;
1538
Dees_Troy2673cec2013-04-02 20:22:16 +00001539 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1540 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001541 return false;
1542 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001543 return true;
1544}
1545
Dees_Troy8170a922012-09-18 15:40:25 -04001546int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001547 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001548 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001549 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001550
Dees_Troy8170a922012-09-18 15:40:25 -04001551 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001552 string Lun_File_str = CUSTOM_LUN_FILE;
1553 size_t found = Lun_File_str.find("%");
1554 if (found != string::npos) {
1555 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1556 if (TWFunc::Path_Exists(lun_file))
1557 has_multiple_lun = true;
1558 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001559 mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage
Ethan Yonker47360be2014-04-01 10:34:34 -05001560 if (!has_multiple_lun) {
1561 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1562 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1563 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1564 TWPartition* Mount = Find_Next_Storage("", "/data");
1565 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001566 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1567 goto error_handle;
1568 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001569 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001570 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001571 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001572 }
1573 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001574 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001575 }
Dees_Troy8170a922012-09-18 15:40:25 -04001576 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001577 LOGINFO("Device has multiple lun files\n");
1578 TWPartition* Mount1;
1579 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001580 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001581 Mount1 = Find_Next_Storage("", "/data");
1582 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001583 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1584 goto error_handle;
1585 }
Matt Moweree717062014-04-26 01:46:46 -05001586 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001587 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1588 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001589 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001590 }
1591 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001592 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001593 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001594 }
Dees_Troy8170a922012-09-18 15:40:25 -04001595 }
Matt Mowerb6ef4782014-11-06 02:24:36 -06001596 property_set("sys.storage.ums_enabled", "1");
HandyMenny37d42992014-10-26 22:15:59 +01001597 property_set("sys.usb.config", "mass_storage,adb");
Dees_Troy8170a922012-09-18 15:40:25 -04001598 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001599error_handle:
1600 if (mtp_was_enabled)
1601 if (!Enable_MTP())
1602 Disable_MTP();
1603 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001604}
1605
1606int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001607 int index, ret;
1608 char lun_file[255], ch[2] = {0, 0};
1609 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001610
1611 for (index=0; index<2; index++) {
1612 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001613 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001614 if (ret < 0) {
1615 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001616 }
Dees_Troy8170a922012-09-18 15:40:25 -04001617 }
Dees_Troye58d5262012-09-21 12:27:57 -04001618 Mount_All_Storage();
1619 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001620 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001621 property_set("sys.storage.ums_enabled", "0");
HandyMenny37d42992014-10-26 22:15:59 +01001622 property_set("sys.usb.config", "adb");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001623 if (mtp_was_enabled)
1624 if (!Enable_MTP())
1625 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001626 if (ret < 0 && index == 0) {
1627 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1628 return false;
1629 } else {
1630 return true;
1631 }
Dees_Troy8170a922012-09-18 15:40:25 -04001632 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001633}
1634
1635void TWPartitionManager::Mount_All_Storage(void) {
1636 std::vector<TWPartition*>::iterator iter;
1637
1638 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1639 if ((*iter)->Is_Storage)
1640 (*iter)->Mount(false);
1641 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001642}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001643
Dees_Troyd0384ef2012-10-12 12:15:42 -04001644void TWPartitionManager::UnMount_Main_Partitions(void) {
1645 // Unmounts system and data if data is not data/media
1646 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001647 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001648
1649 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1650
1651 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001652 if (!datamedia)
1653 UnMount_By_Path("/data", true);
1654
Dees_Troyd0384ef2012-10-12 12:15:42 -04001655 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1656 Boot_Partition->UnMount(true);
1657}
1658
Dees_Troy9350b8d2012-09-27 12:38:38 -04001659int TWPartitionManager::Partition_SDCard(void) {
1660 char mkdir_path[255], temp[255], line[512];
1661 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1662 int ext, swap, total_size = 0, fat_size;
1663 FILE* fp;
1664
Ethan Yonker74db1572015-10-28 12:44:49 -05001665 gui_msg("start_partition_sd=Partitioning SD Card...");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001666#ifdef TW_EXTERNAL_STORAGE_PATH
1667 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1668#else
1669 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1670#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001671 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001672 gui_err("partition_sd_locate=Unable to locate device to partition.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001673 return false;
1674 }
1675 if (!SDCard->UnMount(true))
1676 return false;
1677 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1678 if (SDext != NULL) {
1679 if (!SDext->UnMount(true))
1680 return false;
1681 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001682
1683 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001684 Device = SDCard->Actual_Block_Device;
1685 // Just use the root block device
1686 Device.resize(strlen("/dev/block/mmcblkX"));
1687
1688 // Find the size of the block device:
1689 fp = fopen("/proc/partitions", "rt");
1690 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001691 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")("/proc/partitions")(strerror(errno)));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001692 return false;
1693 }
1694
1695 while (fgets(line, sizeof(line), fp) != NULL)
1696 {
1697 unsigned long major, minor, blocks;
1698 char device[512];
1699 char tmpString[64];
1700
1701 if (strlen(line) < 7 || line[0] == 'm') continue;
1702 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1703
1704 tmpdevice = "/dev/block/";
1705 tmpdevice += device;
1706 if (tmpdevice == Device) {
1707 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001708 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001709 break;
1710 }
1711 }
1712 fclose(fp);
1713
1714 DataManager::GetValue("tw_sdext_size", ext);
1715 DataManager::GetValue("tw_swap_size", swap);
1716 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1717 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001718 LOGINFO("sd card block device is '%s', sdcard size is: %iMB, fat size: %iMB, ext size: %iMB, ext system: '%s', swap size: %iMB\n", Device.c_str(), total_size, fat_size, ext, ext_format.c_str(), swap);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001719 memset(temp, 0, sizeof(temp));
1720 sprintf(temp, "%i", fat_size);
1721 fat_str = temp;
1722 memset(temp, 0, sizeof(temp));
1723 sprintf(temp, "%i", fat_size + ext);
1724 ext_str = temp;
1725 memset(temp, 0, sizeof(temp));
1726 sprintf(temp, "%i", fat_size + ext + swap);
1727 swap_str = temp;
1728 if (ext + swap > total_size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001729 gui_err("ext_swap_size=EXT + Swap size is larger than sdcard size.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001730 return false;
1731 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001732 gui_msg("remove_part_table=Removing partition table...");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001733 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001734 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001735 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001736 gui_err("unable_rm_part=Unable to remove partition table.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001737 Update_System_Details();
1738 return false;
1739 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001740 gui_msg(Msg("create_part=Creating {1} partition...")("FAT32"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001741 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001742 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001743 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001744 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("FAT32"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001745 return false;
1746 }
1747 if (ext > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001748 gui_msg(Msg("create_part=Creating {1} partition...")("EXT"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001749 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001750 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001751 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001752 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("EXT"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001753 Update_System_Details();
1754 return false;
1755 }
1756 }
1757 if (swap > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001758 gui_msg(Msg("create_part=Creating {1} partition...")("swap"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001759 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001760 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001761 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001762 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("swap"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04001763 Update_System_Details();
1764 return false;
1765 }
1766 }
1767 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1768#ifdef TW_EXTERNAL_STORAGE_PATH
1769 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1770 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1771 memset(mkdir_path, 0, sizeof(mkdir_path));
1772 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1773#else
1774 Mount_By_Path("/sdcard", 1);
1775 strcpy(mkdir_path, "/sdcard/TWRP");
1776#endif
1777 mkdir(mkdir_path, 0777);
1778 DataManager::Flush();
1779#ifdef TW_EXTERNAL_STORAGE_PATH
1780 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1781 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1782 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1783#else
1784 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1785 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1786 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1787#endif
1788 if (ext > 0) {
1789 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001790 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001791 return false;
1792 }
1793 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Ethan Yonker74db1572015-10-28 12:44:49 -05001794 gui_msg(Msg("format_sdext_as=Formatting sd-ext as {1}...")(ext_format));
Dees_Troy2673cec2013-04-02 20:22:16 +00001795 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001796 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001797 }
1798
1799 Update_System_Details();
Ethan Yonker74db1572015-10-28 12:44:49 -05001800 gui_msg("part_complete=Partitioning complete.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001801 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001802}
Dees_Troya13d74f2013-03-24 08:54:55 -05001803
1804void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1805 std::vector<TWPartition*>::iterator iter;
1806 if (ListType == "mount") {
1807 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1808 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1809 struct PartitionList part;
1810 part.Display_Name = (*iter)->Display_Name;
1811 part.Mount_Point = (*iter)->Mount_Point;
1812 part.selected = (*iter)->Is_Mounted();
1813 Partition_List->push_back(part);
1814 }
1815 }
1816 } else if (ListType == "storage") {
1817 char free_space[255];
1818 string Current_Storage = DataManager::GetCurrentStoragePath();
1819 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1820 if ((*iter)->Is_Storage) {
1821 struct PartitionList part;
1822 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1823 part.Display_Name = (*iter)->Storage_Name + " (";
1824 part.Display_Name += free_space;
1825 part.Display_Name += "MB)";
1826 part.Mount_Point = (*iter)->Storage_Path;
1827 if ((*iter)->Storage_Path == Current_Storage)
1828 part.selected = 1;
1829 else
1830 part.selected = 0;
1831 Partition_List->push_back(part);
1832 }
1833 }
1834 } else if (ListType == "backup") {
1835 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001836 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001837 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001838 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001839 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001840 Backup_Size = (*iter)->Backup_Size;
1841 if ((*iter)->Has_SubPartition) {
1842 std::vector<TWPartition*>::iterator subpart;
1843
1844 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1845 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1846 Backup_Size += (*subpart)->Backup_Size;
1847 }
1848 }
1849 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001850 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1851 part.Display_Name += backup_size;
1852 part.Display_Name += "MB)";
1853 part.Mount_Point = (*iter)->Backup_Path;
1854 part.selected = 0;
1855 Partition_List->push_back(part);
1856 }
1857 }
1858 } else if (ListType == "restore") {
1859 string Restore_List, restore_path;
1860 TWPartition* restore_part = NULL;
1861
1862 DataManager::GetValue("tw_restore_list", Restore_List);
1863 if (!Restore_List.empty()) {
1864 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1865 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1866 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001867 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001868 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001869 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001870 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001871 } else {
1872 struct PartitionList part;
1873 part.Display_Name = restore_part->Backup_Display_Name;
1874 part.Mount_Point = restore_part->Backup_Path;
1875 part.selected = 1;
1876 Partition_List->push_back(part);
1877 }
1878 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001879 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001880 }
1881 start_pos = end_pos + 1;
1882 end_pos = Restore_List.find(";", start_pos);
1883 }
1884 }
1885 } else if (ListType == "wipe") {
1886 struct PartitionList dalvik;
Ethan Yonker74db1572015-10-28 12:44:49 -05001887 dalvik.Display_Name = gui_parse_text("{@dalvik}");
Dees_Troya13d74f2013-03-24 08:54:55 -05001888 dalvik.Mount_Point = "DALVIK";
1889 dalvik.selected = 0;
1890 Partition_List->push_back(dalvik);
1891 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1892 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1893 struct PartitionList part;
1894 part.Display_Name = (*iter)->Display_Name;
1895 part.Mount_Point = (*iter)->Mount_Point;
1896 part.selected = 0;
1897 Partition_List->push_back(part);
1898 }
1899 if ((*iter)->Has_Android_Secure) {
1900 struct PartitionList part;
1901 part.Display_Name = (*iter)->Backup_Display_Name;
1902 part.Mount_Point = (*iter)->Backup_Path;
1903 part.selected = 0;
1904 Partition_List->push_back(part);
1905 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001906 if ((*iter)->Has_Data_Media) {
1907 struct PartitionList datamedia;
1908 datamedia.Display_Name = (*iter)->Storage_Name;
1909 datamedia.Mount_Point = "INTERNAL";
1910 datamedia.selected = 0;
1911 Partition_List->push_back(datamedia);
1912 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001913 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06001914 } else if (ListType == "flashimg") {
1915 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1916 if ((*iter)->Can_Flash_Img && (*iter)->Is_Present) {
1917 struct PartitionList part;
1918 part.Display_Name = (*iter)->Backup_Display_Name;
1919 part.Mount_Point = (*iter)->Backup_Path;
1920 part.selected = 0;
1921 Partition_List->push_back(part);
1922 }
1923 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001924 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001925 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001926 }
1927}
1928
1929int TWPartitionManager::Fstab_Processed(void) {
1930 return Partitions.size();
1931}
Dees_Troyd93bda52013-07-03 19:55:19 +00001932
1933void TWPartitionManager::Output_Storage_Fstab(void) {
1934 std::vector<TWPartition*>::iterator iter;
1935 char storage_partition[255];
1936 string Temp;
1937 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1938
1939 if (fp == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001940 gui_msg(Msg(msg::kError, "unable_to_open=Unable to open '{1}'.")("/cache/recovery/storage.fstab"));
Dees_Troyd93bda52013-07-03 19:55:19 +00001941 return;
1942 }
1943
1944 // Iterate through all partitions
1945 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1946 if ((*iter)->Is_Storage) {
1947 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1948 strcpy(storage_partition, Temp.c_str());
1949 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1950 }
1951 }
1952 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001953}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02001954
1955TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
1956{
1957 TWPartition *res = NULL;
1958 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
1959 if(!(*iter)->Is_Storage)
1960 continue;
1961
1962 if((*iter)->Is_Settings_Storage)
1963 return *iter;
1964
1965 if(!res)
1966 res = *iter;
1967 }
1968 return res;
1969}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001970
1971bool TWPartitionManager::Enable_MTP(void) {
1972#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001973 if (mtppid) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001974 gui_err("mtp_already_enabled=MTP already enabled");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001975 return true;
1976 }
1977 //Launch MTP Responder
1978 LOGINFO("Starting MTP\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001979
1980 int mtppipe[2];
1981
1982 if (pipe(mtppipe) < 0) {
1983 LOGERR("Error creating MTP pipe\n");
1984 return false;
1985 }
1986
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06001987 char old_value[PROPERTY_VALUE_MAX];
1988 property_get("sys.usb.config", old_value, "error");
1989 if (strcmp(old_value, "error") != 0 && strcmp(old_value, "mtp,adb") != 0) {
1990 char vendor[PROPERTY_VALUE_MAX];
1991 char product[PROPERTY_VALUE_MAX];
1992 property_set("sys.usb.config", "none");
1993 property_get("usb.vendor", vendor, "18D1");
1994 property_get("usb.product.mtpadb", product, "4EE2");
1995 string vendorstr = vendor;
1996 string productstr = product;
1997 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1998 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
1999 property_set("sys.usb.config", "mtp,adb");
2000 }
Ethan Yonker6d154c42014-09-03 14:19:43 -05002001 /* To enable MTP debug, use the twrp command line feature to
2002 * twrp set tw_mtp_debug 1
2003 */
2004 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
Ethan Yonker1b039202015-01-30 10:08:48 -06002005 mtppid = mtp->forkserver(mtppipe);
2006 if (mtppid) {
2007 close(mtppipe[0]); // Host closes read side
2008 mtp_write_fd = mtppipe[1];
2009 DataManager::SetValue("tw_mtp_enabled", 1);
2010 Add_All_MTP_Storage();
2011 return true;
Ethan Yonker726a0202014-12-16 20:01:38 -06002012 } else {
2013 close(mtppipe[0]);
2014 close(mtppipe[1]);
Ethan Yonker74db1572015-10-28 12:44:49 -05002015 gui_err("mtp_fail=Failed to enable MTP");
Ethan Yonker1b039202015-01-30 10:08:48 -06002016 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002017 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002018#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002019 gui_err("no_mtp=MTP support not included");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002020#endif
2021 DataManager::SetValue("tw_mtp_enabled", 0);
2022 return false;
2023}
2024
Ethan Yonker1b039202015-01-30 10:08:48 -06002025void TWPartitionManager::Add_All_MTP_Storage(void) {
2026#ifdef TW_HAS_MTP
2027 std::vector<TWPartition*>::iterator iter;
2028
2029 if (!mtppid)
2030 return; // MTP is not enabled
2031
2032 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2033 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false))
2034 Add_Remove_MTP_Storage((*iter), MTP_MESSAGE_ADD_STORAGE);
2035 }
2036#else
2037 return;
2038#endif
2039}
2040
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002041bool TWPartitionManager::Disable_MTP(void) {
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002042 char old_value[PROPERTY_VALUE_MAX];
2043 property_get("sys.usb.config", old_value, "error");
2044 if (strcmp(old_value, "adb") != 0) {
2045 char vendor[PROPERTY_VALUE_MAX];
2046 char product[PROPERTY_VALUE_MAX];
2047 property_set("sys.usb.config", "none");
2048 property_get("usb.vendor", vendor, "18D1");
2049 property_get("usb.product.adb", product, "D002");
2050 string vendorstr = vendor;
2051 string productstr = product;
2052 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2053 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2054 usleep(2000);
2055 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002056#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002057 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002058 LOGINFO("Disabling MTP\n");
2059 int status;
2060 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002061 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002062 // We don't care about the exit value, but this prevents a zombie process
2063 waitpid(mtppid, &status, 0);
Ethan Yonker726a0202014-12-16 20:01:38 -06002064 close(mtp_write_fd);
2065 mtp_write_fd = -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002066 }
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002067#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002068 property_set("sys.usb.config", "adb");
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002069#ifdef TW_HAS_MTP
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002070 DataManager::SetValue("tw_mtp_enabled", 0);
2071 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002072#endif
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002073 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002074}
Ethan Yonker726a0202014-12-16 20:01:38 -06002075
2076TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) {
2077 std::vector<TWPartition*>::iterator iter;
2078
2079 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2080 if ((*iter)->MTP_Storage_ID == Storage_ID)
2081 return (*iter);
2082 }
2083 return NULL;
2084}
2085
2086bool TWPartitionManager::Add_Remove_MTP_Storage(TWPartition* Part, int message_type) {
2087#ifdef TW_HAS_MTP
2088 struct mtpmsg mtp_message;
2089
2090 if (!mtppid)
2091 return false; // MTP is disabled
2092
2093 if (mtp_write_fd < 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002094 LOGINFO("MTP: mtp_write_fd is not set\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002095 return false;
2096 }
2097
2098 if (Part) {
Ethan Yonker4bfabab2014-12-29 09:15:37 -06002099 if (Part->MTP_Storage_ID == 0)
2100 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002101 if (message_type == MTP_MESSAGE_REMOVE_STORAGE) {
2102 mtp_message.message_type = MTP_MESSAGE_REMOVE_STORAGE; // Remove
2103 LOGINFO("sending message to remove %i\n", Part->MTP_Storage_ID);
2104 mtp_message.storage_id = Part->MTP_Storage_ID;
2105 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002106 LOGINFO("error sending message to remove storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002107 return false;
2108 } else {
2109 LOGINFO("Message sent, remove storage ID: %i\n", Part->MTP_Storage_ID);
2110 return true;
2111 }
2112 } else if (message_type == MTP_MESSAGE_ADD_STORAGE && Part->Is_Mounted()) {
2113 mtp_message.message_type = MTP_MESSAGE_ADD_STORAGE; // Add
2114 mtp_message.storage_id = Part->MTP_Storage_ID;
2115 mtp_message.path = Part->Storage_Path.c_str();
2116 mtp_message.display = Part->Storage_Name.c_str();
2117 mtp_message.maxFileSize = Part->Get_Max_FileSize();
Ethan Yonker1b039202015-01-30 10:08:48 -06002118 LOGINFO("sending message to add %i '%s' '%s'\n", mtp_message.storage_id, mtp_message.path, mtp_message.display);
Ethan Yonker726a0202014-12-16 20:01:38 -06002119 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002120 LOGINFO("error sending message to add storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002121 return false;
2122 } else {
2123 LOGINFO("Message sent, add storage ID: %i\n", Part->MTP_Storage_ID);
2124 return true;
2125 }
2126 } else {
2127 LOGERR("Unknown MTP message type: %i\n", message_type);
2128 }
2129 } else {
2130 // This hopefully never happens as the error handling should
2131 // occur in the calling function.
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002132 LOGINFO("TWPartitionManager::Add_Remove_MTP_Storage NULL partition given\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002133 }
2134 return true;
2135#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002136 gui_err("no_mtp=MTP support not included");
Ethan Yonker726a0202014-12-16 20:01:38 -06002137 DataManager::SetValue("tw_mtp_enabled", 0);
2138 return false;
2139#endif
2140}
2141
2142bool TWPartitionManager::Add_MTP_Storage(string Mount_Point) {
2143#ifdef TW_HAS_MTP
2144 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2145 if (Part) {
2146 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2147 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002148 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002149 }
2150#endif
2151 return false;
2152}
2153
2154bool TWPartitionManager::Add_MTP_Storage(unsigned int Storage_ID) {
2155#ifdef TW_HAS_MTP
2156 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2157 if (Part) {
2158 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2159 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002160 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002161 }
2162#endif
2163 return false;
2164}
2165
2166bool TWPartitionManager::Remove_MTP_Storage(string Mount_Point) {
2167#ifdef TW_HAS_MTP
2168 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2169 if (Part) {
2170 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2171 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002172 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002173 }
2174#endif
2175 return false;
2176}
2177
2178bool TWPartitionManager::Remove_MTP_Storage(unsigned int Storage_ID) {
2179#ifdef TW_HAS_MTP
2180 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2181 if (Part) {
2182 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2183 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002184 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002185 }
2186#endif
2187 return false;
2188}
Ethan Yonker96af84a2015-01-05 14:58:36 -06002189
2190bool TWPartitionManager::Flash_Image(string Filename) {
2191 int check, partition_count = 0;
2192 TWPartition* flash_part = NULL;
2193 string Flash_List, flash_path;
2194 size_t start_pos = 0, end_pos = 0;
2195
Ethan Yonker74db1572015-10-28 12:44:49 -05002196 gui_msg("image_flash_start=[IMAGE FLASH STARTED]");
2197 gui_msg(Msg("img_to_flash=Image to flash: '{1}'")(Filename));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002198
2199 if (!Mount_Current_Storage(true))
2200 return false;
2201
Ethan Yonker74db1572015-10-28 12:44:49 -05002202 gui_msg("calc_restore=Calculating restore details...");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002203 DataManager::GetValue("tw_flash_partition", Flash_List);
2204 if (!Flash_List.empty()) {
2205 end_pos = Flash_List.find(";", start_pos);
2206 while (end_pos != string::npos && start_pos < Flash_List.size()) {
2207 flash_path = Flash_List.substr(start_pos, end_pos - start_pos);
2208 flash_part = Find_Partition_By_Path(flash_path);
2209 if (flash_part != NULL) {
2210 partition_count++;
2211 if (partition_count > 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002212 gui_err("too_many_flash=Too many partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002213 return false;
2214 }
2215 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002216 gui_msg(Msg(msg::kError, "flash_unable_locate=Unable to locate '{1}' partition for flashing.")(flash_path));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002217 return false;
2218 }
2219 start_pos = end_pos + 1;
2220 end_pos = Flash_List.find(";", start_pos);
2221 }
2222 }
2223
2224 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002225 gui_err("no_part_flash=No partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002226 return false;
2227 }
2228
2229 DataManager::SetProgress(0.0);
2230 if (flash_part) {
2231 if (!flash_part->Flash_Image(Filename))
2232 return false;
2233 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002234 gui_err("invalid_flash=Invalid flash partition specified.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002235 return false;
2236 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002237 gui_highlight("flash_done=IMAGE FLASH COMPLETED]");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002238 return true;
2239}
Ethan Yonker74db1572015-10-28 12:44:49 -05002240
2241void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value) {
2242 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2243 if (part) {
2244 part->Display_Name = gui_lookup(resource_name, default_value);
2245 part->Backup_Display_Name = part->Display_Name;
2246 }
2247}
2248
2249void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value, const char* storage_resource_name, const char* storage_default_value) {
2250 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2251 if (part) {
2252 part->Display_Name = gui_lookup(resource_name, default_value);
2253 part->Backup_Display_Name = part->Display_Name;
2254 if (part->Is_Storage)
2255 part->Storage_Name = gui_lookup(storage_resource_name, storage_default_value);
2256 }
2257}
2258
2259void TWPartitionManager::Translate_Partition_Display_Names() {
2260 Translate_Partition("/system", "system", "System");
2261 Translate_Partition("/system_image", "system_image", "System Image");
2262 Translate_Partition("/vendor", "vendor", "Vendor");
2263 Translate_Partition("/vendor_image", "vendor_image", "Vendor Image");
2264 Translate_Partition("/cache", "cache", "Cache");
2265 Translate_Partition("/data", "data", "Data", "internal", "Internal Storage");
2266 Translate_Partition("/boot", "boot", "Boot");
2267 Translate_Partition("/recovery", "recovery", "Recovery");
2268 if (!datamedia) {
2269 Translate_Partition("/sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2270 Translate_Partition("/internal_sd", "sdcard", "SDCard", "sdcard", "SDCard");
2271 Translate_Partition("/internal_sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2272 Translate_Partition("/emmc", "sdcard", "SDCard", "sdcard", "SDCard");
2273 }
2274 Translate_Partition("/external_sd", "microsd", "Micro SDCard", "microsd", "Micro SDCard");
2275 Translate_Partition("/external_sdcard", "microsd", "Micro SDCard", "microsd", "Micro SDCard");
2276 Translate_Partition("/usb-otg", "usbotg", "USB OTG", "usbotg", "USB OTG");
2277 Translate_Partition("/sd-ext", "sdext", "SD-EXT");
2278
2279 // Android secure is a special case
2280 TWPartition* part = PartitionManager.Find_Partition_By_Path("/and-sec");
2281 if (part)
2282 part->Backup_Display_Name = gui_lookup("android_secure", "Android Secure");
2283
2284 // This updates the text on all of the storage selection buttons in the GUI
2285 DataManager::SetBackupFolder();
2286}