blob: 25f9dc7aeabb0b4e9880d7c54b5eac027bc222d0 [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"
Dees_Troy38bd7602012-09-14 13:33:53 -040043
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040044#ifdef TW_HAS_MTP
45#include "mtp/mtp_MtpServer.hpp"
46#include "mtp/twrpMtp.hpp"
Ethan Yonker726a0202014-12-16 20:01:38 -060047#include "mtp/MtpMessage.hpp"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040048#endif
49
Dees Troy6f6441d2014-01-23 02:07:03 +000050extern "C" {
51 #include "cutils/properties.h"
52}
53
Dees_Troy5bf43922012-09-07 16:07:55 -040054#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker253368a2014-11-25 15:00:52 -060055 #include "crypto/lollipop/cryptfs.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040056#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040057
Ethan Yonker6277c792014-09-15 14:54:30 -050058extern bool datamedia;
59
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050060TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040061 mtp_was_enabled = false;
Ethan Yonker726a0202014-12-16 20:01:38 -060062 mtp_write_fd = -1;
bigbiff7abc5fe2015-01-17 16:53:12 -050063 stop_backup.set_value(0);
64 tar_fork_pid = 0;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050065}
66
Dees_Troy51a0e822012-09-05 15:24:24 -040067int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040068 FILE *fstabFile;
69 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000070 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050071 TWPartition* andsec_partition = NULL;
Ethan Yonker726a0202014-12-16 20:01:38 -060072 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 -040073
74 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
75 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000076 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040077 return false;
78 }
79
80 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
81 if (fstab_line[0] != '/')
82 continue;
83
Dees_Troy2a923582012-09-20 12:13:34 -040084 if (fstab_line[strlen(fstab_line) - 1] != '\n')
85 fstab_line[strlen(fstab_line)] = '\n';
that9e0593e2014-10-08 00:01:24 +020086 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040087 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040088 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040089
Dees_Troy5bf43922012-09-07 16:07:55 -040090 if (partition->Process_Fstab_Line(line, Display_Error)) {
Ethan Yonker726a0202014-12-16 20:01:38 -060091 if (partition->Is_Storage) {
92 ++storageid;
93 partition->MTP_Storage_ID = storageid;
94 }
Matt Mowered71fa32014-04-16 13:21:47 -050095 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000096 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050097 } else {
98 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050099 }
Matt Mowered71fa32014-04-16 13:21:47 -0500100 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500101 andsec_partition = partition;
102 } else {
103 partition->Has_Android_Secure = false;
104 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500105 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -0400106 } else {
107 delete partition;
108 }
109 }
110 fclose(fstabFile);
Ethan Yonker6277c792014-09-15 14:54:30 -0500111 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) {
112 // Attempt to automatically identify /data/media emulated storage devices
113 TWPartition* Dat = Find_Partition_By_Path("/data");
114 if (Dat) {
115 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
116 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200117 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500118 settings_partition = Dat;
Ethan Yonker726a0202014-12-16 20:01:38 -0600119 // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID
120 ++storageid;
121 Dat->MTP_Storage_ID = storageid;
Ethan Yonker6277c792014-09-15 14:54:30 -0500122 }
123 }
Dees Troy02a64532014-03-19 15:23:32 +0000124 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500125 std::vector<TWPartition*>::iterator iter;
126 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
127 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000128 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500129 break;
130 }
131 }
Dees Troy02a64532014-03-19 15:23:32 +0000132 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000133 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500134 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400135 if (!Write_Fstab()) {
136 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000137 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400138 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000139 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400140 }
Matt Mowered71fa32014-04-16 13:21:47 -0500141
Matt Mowerbf4efa32014-04-14 23:25:26 -0500142 if (andsec_partition) {
143 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500144 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500145 Setup_Android_Secure_Location(settings_partition);
146 }
Matt Mowered71fa32014-04-16 13:21:47 -0500147 if (settings_partition) {
148 Setup_Settings_Storage_Partition(settings_partition);
149 }
Ethan Yonker253368a2014-11-25 15:00:52 -0600150#ifdef TW_INCLUDE_CRYPTO
Ethan Yonkercceebb82014-11-18 10:17:59 -0600151 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
152 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
153 int password_type = cryptfs_get_password_type();
154 if (password_type == CRYPT_TYPE_DEFAULT) {
155 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
156 if (Decrypt_Device("default_password") == 0) {
157 gui_print("Successfully decrypted with default password.\n");
158 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
159 } else {
160 LOGERR("Unable to decrypt with default password.");
161 LOGERR("You may need to perform a Format Data.\n");
162 }
163 } else {
164 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
165 }
166 }
167#endif
Dees_Troy51127312012-09-08 13:08:49 -0400168 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400169 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400170 return true;
171}
172
173int TWPartitionManager::Write_Fstab(void) {
174 FILE *fp;
175 std::vector<TWPartition*>::iterator iter;
176 string Line;
177
178 fp = fopen("/etc/fstab", "w");
179 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000180 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400181 return false;
182 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400183 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400184 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400185 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400186 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000187 }
188 // Handle subpartition tracking
189 if ((*iter)->Is_SubPartition) {
190 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
191 if (ParentPartition)
192 ParentPartition->Has_SubPartition = true;
193 else
194 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 -0400195 }
196 }
197 fclose(fp);
198 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400199}
200
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500201void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500202 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
203 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
204 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
205}
206
Matt Mowerbf4efa32014-04-14 23:25:26 -0500207void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
208 if (Part->Has_Android_Secure)
209 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500210 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500211 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500212}
213
Dees_Troy8170a922012-09-18 15:40:25 -0400214void TWPartitionManager::Output_Partition_Logging(void) {
215 std::vector<TWPartition*>::iterator iter;
216
217 printf("\n\nPartition Logs:\n");
218 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
219 Output_Partition((*iter));
220}
221
222void TWPartitionManager::Output_Partition(TWPartition* Part) {
223 unsigned long long mb = 1048576;
224
Gary Peck004d48b2012-11-21 16:28:18 -0800225 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 -0400226 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800227 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 -0400228 }
Gary Peck004d48b2012-11-21 16:28:18 -0800229 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500230 if (Part->Can_Be_Mounted)
231 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800232 if (Part->Can_Be_Wiped)
233 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700234 if (Part->Use_Rm_Rf)
235 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500236 if (Part->Can_Be_Backed_Up)
237 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800238 if (Part->Wipe_During_Factory_Reset)
239 printf("Wipe_During_Factory_Reset ");
240 if (Part->Wipe_Available_in_GUI)
241 printf("Wipe_Available_in_GUI ");
242 if (Part->Is_SubPartition)
243 printf("Is_SubPartition ");
244 if (Part->Has_SubPartition)
245 printf("Has_SubPartition ");
246 if (Part->Removable)
247 printf("Removable ");
248 if (Part->Is_Present)
249 printf("IsPresent ");
250 if (Part->Can_Be_Encrypted)
251 printf("Can_Be_Encrypted ");
252 if (Part->Is_Encrypted)
253 printf("Is_Encrypted ");
254 if (Part->Is_Decrypted)
255 printf("Is_Decrypted ");
256 if (Part->Has_Data_Media)
257 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000258 if (Part->Can_Encrypt_Backup)
259 printf("Can_Encrypt_Backup ");
260 if (Part->Use_Userdata_Encryption)
261 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800262 if (Part->Has_Android_Secure)
263 printf("Has_Android_Secure ");
264 if (Part->Is_Storage)
265 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500266 if (Part->Is_Settings_Storage)
267 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000268 if (Part->Ignore_Blkid)
269 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000270 if (Part->Retain_Layout_Version)
271 printf("Retain_Layout_Version ");
Ethan Yonker253368a2014-11-25 15:00:52 -0600272 if (Part->Mount_To_Decrypt)
273 printf("Mount_To_Decrypt ");
Ethan Yonker96af84a2015-01-05 14:58:36 -0600274 if (Part->Can_Flash_Img)
275 printf("Can_Flash_Img ");
Gary Peck004d48b2012-11-21 16:28:18 -0800276 printf("\n");
277 if (!Part->SubPartition_Of.empty())
278 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
279 if (!Part->Symlink_Path.empty())
280 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
281 if (!Part->Symlink_Mount_Point.empty())
282 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
283 if (!Part->Primary_Block_Device.empty())
284 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
285 if (!Part->Alternate_Block_Device.empty())
286 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
287 if (!Part->Decrypted_Block_Device.empty())
288 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
Ethan Yonker253368a2014-11-25 15:00:52 -0600289 if (!Part->Crypto_Key_Location.empty() && Part->Crypto_Key_Location != "footer")
290 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800291 if (Part->Length != 0)
292 printf(" Length: %i\n", Part->Length);
293 if (!Part->Display_Name.empty())
294 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500295 if (!Part->Storage_Name.empty())
296 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800297 if (!Part->Backup_Path.empty())
298 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
299 if (!Part->Backup_Name.empty())
300 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500301 if (!Part->Backup_Display_Name.empty())
302 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800303 if (!Part->Backup_FileName.empty())
304 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
305 if (!Part->Storage_Path.empty())
306 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
307 if (!Part->Current_File_System.empty())
308 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
309 if (!Part->Fstab_File_System.empty())
310 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
311 if (Part->Format_Block_Size != 0)
312 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400313 if (!Part->MTD_Name.empty())
314 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400315 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500316 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800317 if (Part->Mount_Flags || !Part->Mount_Options.empty())
318 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600319 if (Part->MTP_Storage_ID)
320 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker6277c792014-09-15 14:54:30 -0500321 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400322}
323
Dees_Troy51a0e822012-09-05 15:24:24 -0400324int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 std::vector<TWPartition*>::iterator iter;
326 int ret = false;
327 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400328 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400329
Dees_Troyd93bda52013-07-03 19:55:19 +0000330 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400331 return true;
332
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 // Iterate through all partitions
334 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400335 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400336 ret = (*iter)->Mount(Display_Error);
337 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400338 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400340 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 }
342 if (found) {
343 return ret;
344 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000345 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400346 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000347 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400348 }
349 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400350}
351
Dees_Troy51a0e822012-09-05 15:24:24 -0400352int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400353 std::vector<TWPartition*>::iterator iter;
354 int ret = false;
355 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400356 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400357
358 // Iterate through all partitions
359 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400360 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400361 ret = (*iter)->UnMount(Display_Error);
362 found = true;
363 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
364 (*iter)->UnMount(Display_Error);
365 }
366 }
367 if (found) {
368 return ret;
369 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000370 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400371 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000372 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400373 }
374 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400375}
376
Dees_Troy51a0e822012-09-05 15:24:24 -0400377int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400378 TWPartition* Part = Find_Partition_By_Path(Path);
379
380 if (Part)
381 return Part->Is_Mounted();
382 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000383 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400384 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400385}
386
Dees_Troy5bf43922012-09-07 16:07:55 -0400387int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400388 string current_storage_path = DataManager::GetCurrentStoragePath();
389
390 if (Mount_By_Path(current_storage_path, Display_Error)) {
391 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
392 if (FreeStorage)
393 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
394 return true;
395 }
396 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400397}
398
Dees_Troy5bf43922012-09-07 16:07:55 -0400399int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
400 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
401}
402
403TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
404 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400405 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400406
407 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400408 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400409 return (*iter);
410 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400411 return NULL;
412}
413
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400414int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
415 // Check the backup name to ensure that it is the correct size and contains only valid characters
416 // and that a backup with that name doesn't already exist
417 char backup_name[MAX_BACKUP_NAME_LEN];
418 char backup_loc[255], tw_image_dir[255];
419 int copy_size;
420 int index, cur_char;
421 string Backup_Name, Backup_Loc;
422
423 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
424 copy_size = Backup_Name.size();
425 // Check size
426 if (copy_size > MAX_BACKUP_NAME_LEN) {
427 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000428 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400429 return -2;
430 }
431
432 // Check each character
433 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500434 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400435 return 0; // A "0" (zero) means to use the current timestamp for the backup name
436 for (index=0; index<copy_size; index++) {
437 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500438 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 -0400439 // These are valid characters
440 // Numbers
441 // Upper case letters
442 // Lower case letters
443 // Space
444 // and -_.{}[]
445 } else {
446 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000447 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400448 return -3;
449 }
450 }
451
452 // Check to make sure that a backup with this name doesn't already exist
453 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
454 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500455 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500456 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400457 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000458 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400459 return -4;
460 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400461 // No problems found, return 0
462 return 0;
463}
464
Dees_Troy43d8b002012-09-17 16:00:01 -0400465bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
466{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500467 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400468 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500469 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500470 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400471
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500472 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400473 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400474
Dees_Troyb46a6842012-09-25 11:06:46 -0400475 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000476 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400477
478 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500479 md5sum.setfn(Backup_Folder + Backup_Filename);
480 if (md5sum.computeMD5() == 0)
481 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000482 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500483 else
484 return -1;
485 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000486 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400487 } else {
488 char filename[512];
489 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500490 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400491 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500492 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000493 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400494 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000495 if (TWFunc::Path_Exists(filename)) {
496 if (md5sum.computeMD5() == 0) {
497 if (md5sum.write_md5digest() != 0)
498 {
499 gui_print(" * MD5 Error.\n");
500 return false;
501 }
502 } else {
503 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500504 return false;
505 }
506 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400507 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400508 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500509 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400510 }
511 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000512 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400513 return false;
514 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000515 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400516 }
517 return true;
518}
519
Dees_Troy093b7642012-09-21 15:59:38 -0400520bool 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 -0400521 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500522 int img_bps;
523 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400524 unsigned long total_time, remain_time, section_time;
525 int use_compression, backup_time;
526 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500527 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400528
529 if (Part == NULL)
530 return true;
531
Dees_Troy093b7642012-09-21 15:59:38 -0400532 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
533
534 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
535 if (use_compression)
536 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
537 else
538 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
539
540 // We know the speed for both, how far into the whole backup are we, based on time
541 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
542 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
543
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500544 //pos = (total_time - remain_time) / (float) total_time;
545 total_size = *file_bytes + *img_bytes;
546 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
547 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000548 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400549
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500550 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400551
552 // And get the time
553 if (Part->Backup_Method == 1)
554 section_time = Part->Backup_Size / file_bps;
555 else
556 section_time = Part->Backup_Size / img_bps;
557
558 // Set the position
559 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500560 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400561
Tom Hite5a926722014-09-15 01:31:03 +0000562 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400563 time(&start);
564
bigbiff7abc5fe2015-01-17 16:53:12 -0500565 if (Part->Backup(Backup_Folder, &total_size, &current_size, tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000566 bool md5Success = false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500567 current_size += Part->Backup_Size;
568 pos = (float)((float)(current_size) / (float)(total_size));
569 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400570 if (Part->Has_SubPartition) {
571 std::vector<TWPartition*>::iterator subpart;
572
573 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500574 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500575 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size, tar_fork_pid)) {
Tom Hite5a926722014-09-15 01:31:03 +0000576 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400577 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000578 }
Dees_Troy2727b992013-08-14 20:09:30 +0000579 sync();
580 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000581 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
582 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400583 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000584 }
Dees_Troy093b7642012-09-21 15:59:38 -0400585 if (Part->Backup_Method == 1) {
586 *file_bytes_remaining -= (*subpart)->Backup_Size;
587 } else {
588 *img_bytes_remaining -= (*subpart)->Backup_Size;
589 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500590 current_size += Part->Backup_Size;
591 pos = (float)(current_size / total_size);
592 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400593 }
594 }
595 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400596 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400597 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000598 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400599 if (Part->Backup_Method == 1) {
600 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400601 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400602 } else {
603 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400604 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400605 }
Tom Hite5a926722014-09-15 01:31:03 +0000606
607 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
608 TWFunc::SetPerformanceMode(false);
609 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400610 } else {
Tom Hite5a926722014-09-15 01:31:03 +0000611 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400612 return false;
613 }
bigbiff7abc5fe2015-01-17 16:53:12 -0500614 return 0;
615}
616
617int TWPartitionManager::Cancel_Backup() {
618 string Backup_Folder, Backup_Name, Full_Backup_Path;
619
620 stop_backup.set_value(1);
621
622 if (tar_fork_pid != 0) {
623 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
624 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
625 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
626 LOGINFO("Killing pid: %d\n", tar_fork_pid);
627 kill(tar_fork_pid, SIGUSR2);
628 while (kill(tar_fork_pid, 0) == 0) {
629 usleep(1000);
630 }
631 LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n");
632 LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str());
633 TWFunc::removeDir(Full_Backup_Path, false);
634 tar_fork_pid = 0;
635 }
636
637 return 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400638}
639
640int TWPartitionManager::Run_Backup(void) {
641 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500642 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400643 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 -0400644 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500645 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400646 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400647 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400648 struct tm *t;
649 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500650 size_t start_pos = 0, end_pos = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500651 stop_backup.set_value(0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400652 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500653 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400654
655 time(&total_start);
656
657 Update_System_Details();
658
659 if (!Mount_Current_Storage(true))
660 return false;
661
662 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400663 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400664 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400665 else
666 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400667
Dees_Troy43d8b002012-09-17 16:00:01 -0400668 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
669 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000670 if (Backup_Name == "(Current Date)") {
671 Backup_Name = TWFunc::Get_Current_Date();
672 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
673 TWFunc::Auto_Generate_Backup_Name();
674 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400675 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000676 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400677 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000678 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400679
Dees_Troy2673cec2013-04-02 20:22:16 +0000680 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500681 DataManager::GetValue("tw_backup_list", Backup_List);
682 if (!Backup_List.empty()) {
683 end_pos = Backup_List.find(";", start_pos);
684 while (end_pos != string::npos && start_pos < Backup_List.size()) {
685 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
686 backup_part = Find_Partition_By_Path(backup_path);
687 if (backup_part != NULL) {
688 partition_count++;
689 if (backup_part->Backup_Method == 1)
690 file_bytes += backup_part->Backup_Size;
691 else
692 img_bytes += backup_part->Backup_Size;
693 if (backup_part->Has_SubPartition) {
694 std::vector<TWPartition*>::iterator subpart;
695
696 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000697 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 -0500698 partition_count++;
699 if ((*subpart)->Backup_Method == 1)
700 file_bytes += (*subpart)->Backup_Size;
701 else
702 img_bytes += (*subpart)->Backup_Size;
703 }
704 }
Dees_Troy8170a922012-09-18 15:40:25 -0400705 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500706 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000707 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400708 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500709 start_pos = end_pos + 1;
710 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400711 }
712 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400713
714 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000715 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400716 return false;
717 }
718 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000719 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500720 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400721 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
722 if (storage != NULL) {
723 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000724 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400725 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000726 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400727 return false;
728 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000729 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400730 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000731 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400732 return false;
733 }
734 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500735 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400736
Dees_Troy2673cec2013-04-02 20:22:16 +0000737 gui_print("\n[BACKUP STARTED]\n");
738 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000739 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000740 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000741 return false;
742 }
743
Dees_Troy2673cec2013-04-02 20:22:16 +0000744 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400745
Dees_Troya13d74f2013-03-24 08:54:55 -0500746 start_pos = 0;
747 end_pos = Backup_List.find(";", start_pos);
748 while (end_pos != string::npos && start_pos < Backup_List.size()) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500749 if (stop_backup.get_value() != 0)
750 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500751 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
752 backup_part = Find_Partition_By_Path(backup_path);
753 if (backup_part != NULL) {
754 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
755 return false;
756 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000757 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500758 }
759 start_pos = end_pos + 1;
760 end_pos = Backup_List.find(";", start_pos);
761 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400762
763 // Average BPS
764 if (img_time == 0)
765 img_time = 1;
766 if (file_time == 0)
767 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400768 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500769 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400770
Dees_Troy2673cec2013-04-02 20:22:16 +0000771 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
772 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400773
774 time(&total_stop);
775 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500776 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500777 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400778
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500779 int prev_img_bps, use_compression;
780 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400781 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
782 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500783 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400784
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500785 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400786 if (use_compression)
787 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500788 else
Dees_Troy093b7642012-09-21 15:59:38 -0400789 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
790 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500791 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400792
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500793 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400794 if (use_compression)
795 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
796 else
797 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
798
Dees_Troy2673cec2013-04-02 20:22:16 +0000799 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400800 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400801 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500802 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000803 string backup_log = Full_Backup_Path + "recovery.log";
804 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -0600805 tw_set_default_metadata(backup_log.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000806 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400807}
808
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500809bool 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 -0400810 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000811 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400812 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500813 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Tom Hite5a926722014-09-15 01:31:03 +0000814 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) {
815 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400816 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000817 }
Dees_Troy8170a922012-09-18 15:40:25 -0400818 if (Part->Has_SubPartition) {
819 std::vector<TWPartition*>::iterator subpart;
820
821 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
822 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000823 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) {
824 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400825 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000826 }
Dees_Troy8170a922012-09-18 15:40:25 -0400827 }
828 }
829 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400830 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000831 TWFunc::SetPerformanceMode(false);
Dees_Troy2673cec2013-04-02 20:22:16 +0000832 gui_print("[%s done (%d seconds)]\n\n", Part->Backup_Display_Name.c_str(), (int)difftime(Stop, Start));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400833 return true;
834}
835
Dees_Troy51a0e822012-09-05 15:24:24 -0400836int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400837 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500838 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400839 time_t rStart, rStop;
840 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500841 string Restore_List, restore_path;
842 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500843 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400844
Dees_Troy2673cec2013-04-02 20:22:16 +0000845 gui_print("\n[RESTORE STARTED]\n\n");
846 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400847
Dees_Troy4a2a1262012-09-18 09:33:47 -0400848 if (!Mount_Current_Storage(true))
849 return false;
850
851 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500852 if (check_md5 > 0) {
853 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
854 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000855 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500856 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000857 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500858 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500859 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500860 DataManager::GetValue("tw_restore_selected", Restore_List);
861 if (!Restore_List.empty()) {
862 end_pos = Restore_List.find(";", start_pos);
863 while (end_pos != string::npos && start_pos < Restore_List.size()) {
864 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
865 restore_part = Find_Partition_By_Path(restore_path);
866 if (restore_part != NULL) {
867 partition_count++;
868 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
869 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500870 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500871 if (restore_part->Has_SubPartition) {
872 std::vector<TWPartition*>::iterator subpart;
873
874 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
875 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400876 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500877 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500878 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500879 }
880 }
881 }
882 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500883 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500884 }
885 start_pos = end_pos + 1;
886 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400887 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400888 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400889
890 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000891 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400892 return false;
893 }
894
Dees_Troy2673cec2013-04-02 20:22:16 +0000895 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500896 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000897 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500898
Dees_Troya13d74f2013-03-24 08:54:55 -0500899 start_pos = 0;
900 if (!Restore_List.empty()) {
901 end_pos = Restore_List.find(";", start_pos);
902 while (end_pos != string::npos && start_pos < Restore_List.size()) {
903 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
904 restore_part = Find_Partition_By_Path(restore_path);
905 if (restore_part != NULL) {
906 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500907 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500908 return false;
909 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000910 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500911 }
912 start_pos = end_pos + 1;
913 end_pos = Restore_List.find(";", start_pos);
914 }
915 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400916 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400917 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400918 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400919 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500920 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500921 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400922 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400923}
924
925void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400926 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500927 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000928 bool get_date = true, check_encryption = true;
929
930 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400931
932 DIR* d;
933 d = opendir(Restore_Name.c_str());
934 if (d == NULL)
935 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000936 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400937 return;
938 }
939
940 struct dirent* de;
941 while ((de = readdir(d)) != NULL)
942 {
943 // Strip off three components
944 char str[256];
945 char* label;
946 char* fstype = NULL;
947 char* extn = NULL;
948 char* ptr;
949
950 strcpy(str, de->d_name);
951 if (strlen(str) <= 2)
952 continue;
953
954 if (get_date) {
955 char file_path[255];
956 struct stat st;
957
958 strcpy(file_path, Restore_Name.c_str());
959 strcat(file_path, "/");
960 strcat(file_path, str);
961 stat(file_path, &st);
962 string backup_date = ctime((const time_t*)(&st.st_mtime));
963 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
964 get_date = false;
965 }
966
967 label = str;
968 ptr = label;
969 while (*ptr && *ptr != '.') ptr++;
970 if (*ptr == '.')
971 {
972 *ptr = 0x00;
973 ptr++;
974 fstype = ptr;
975 }
976 while (*ptr && *ptr != '.') ptr++;
977 if (*ptr == '.')
978 {
979 *ptr = 0x00;
980 ptr++;
981 extn = ptr;
982 }
983
Dees_Troy83bd4832013-05-04 12:39:56 +0000984 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500985 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000986 if (extnlength != 3 && extnlength != 6) continue;
987 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
988 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
989
990 if (check_encryption) {
991 string filename = Restore_Name + "/";
992 filename += de->d_name;
993 if (TWFunc::Get_File_Type(filename) == 2) {
994 LOGINFO("'%s' is encrypted\n", filename.c_str());
995 DataManager::SetValue("tw_restore_encrypted", 1);
996 }
997 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500998 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400999
1000 TWPartition* Part = Find_Partition_By_Path(label);
1001 if (Part == NULL)
1002 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001003 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -04001004 continue;
1005 }
1006
1007 Part->Backup_FileName = de->d_name;
1008 if (strlen(extn) > 3) {
1009 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1010 }
1011
Dees_Troya13d74f2013-03-24 08:54:55 -05001012 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001013 }
1014 closedir(d);
1015
Dees_Troya13d74f2013-03-24 08:54:55 -05001016 // Set the final value
1017 DataManager::SetValue("tw_restore_list", Restore_List);
1018 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001019 return;
1020}
1021
1022int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001023 std::vector<TWPartition*>::iterator iter;
1024 int ret = false;
1025 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001026 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001027
1028 // Iterate through all partitions
1029 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001030 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001031 if (Path == "/and-sec")
1032 ret = (*iter)->Wipe_AndSec();
1033 else
1034 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001035 found = true;
1036 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1037 (*iter)->Wipe();
1038 }
1039 }
1040 if (found) {
1041 return ret;
1042 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001043 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001044 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001045}
1046
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001047int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1048 std::vector<TWPartition*>::iterator iter;
1049 int ret = false;
1050 bool found = false;
1051 string Local_Path = TWFunc::Get_Root_Path(Path);
1052
1053 // Iterate through all partitions
1054 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1055 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1056 if (Path == "/and-sec")
1057 ret = (*iter)->Wipe_AndSec();
1058 else
1059 ret = (*iter)->Wipe(New_File_System);
1060 found = true;
1061 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1062 (*iter)->Wipe(New_File_System);
1063 }
1064 }
1065 if (found) {
1066 return ret;
1067 } else
1068 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1069 return false;
1070}
1071
Dees_Troy51a0e822012-09-05 15:24:24 -04001072int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001073 std::vector<TWPartition*>::iterator iter;
1074 int ret = true;
1075
1076 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001077 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001078 if (!(*iter)->Wipe())
1079 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001080 } else if ((*iter)->Has_Android_Secure) {
1081 if (!(*iter)->Wipe_AndSec())
1082 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001083 }
1084 }
1085 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001086}
1087
Dees_Troy38bd7602012-09-14 13:33:53 -04001088int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1089 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001090 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001091
1092 if (!Mount_By_Path("/data", true))
1093 return false;
1094
1095 if (!Mount_By_Path("/cache", true))
1096 return false;
1097
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001098 dir.push_back("/data/dalvik-cache");
1099 dir.push_back("/cache/dalvik-cache");
1100 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001101 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001102 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001103 if (stat(dir.at(i).c_str(), &st) == 0) {
1104 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001105 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001106 }
1107 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001108 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001109 if (sdext && sdext->Is_Present && sdext->Mount(false))
1110 {
1111 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1112 {
1113 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001114 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001115 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001116 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001117 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001118 return true;
1119}
1120
1121int TWPartitionManager::Wipe_Rotate_Data(void) {
1122 if (!Mount_By_Path("/data", true))
1123 return false;
1124
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001125 unlink("/data/misc/akmd*");
1126 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001127 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001128 return true;
1129}
1130
1131int TWPartitionManager::Wipe_Battery_Stats(void) {
1132 struct stat st;
1133
1134 if (!Mount_By_Path("/data", true))
1135 return false;
1136
1137 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001138 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001139 } else {
1140 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001141 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001142 }
1143 return true;
1144}
1145
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001146int TWPartitionManager::Wipe_Android_Secure(void) {
1147 std::vector<TWPartition*>::iterator iter;
1148 int ret = false;
1149 bool found = false;
1150
1151 // Iterate through all partitions
1152 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1153 if ((*iter)->Has_Android_Secure) {
1154 ret = (*iter)->Wipe_AndSec();
1155 found = true;
1156 }
1157 }
1158 if (found) {
1159 return ret;
1160 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001161 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001162 }
1163 return false;
1164}
1165
Dees_Troy38bd7602012-09-14 13:33:53 -04001166int TWPartitionManager::Format_Data(void) {
1167 TWPartition* dat = Find_Partition_By_Path("/data");
1168
1169 if (dat != NULL) {
1170 if (!dat->UnMount(true))
1171 return false;
1172
1173 return dat->Wipe_Encryption();
1174 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001175 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001176 return false;
1177 }
1178 return false;
1179}
1180
1181int TWPartitionManager::Wipe_Media_From_Data(void) {
1182 TWPartition* dat = Find_Partition_By_Path("/data");
1183
1184 if (dat != NULL) {
1185 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001186 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001187 return false;
1188 }
1189 if (!dat->Mount(true))
1190 return false;
1191
Dees_Troy2673cec2013-04-02 20:22:16 +00001192 gui_print("Wiping internal storage -- /data/media...\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001193 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001194 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001195 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
Ethan Yonker726a0202014-12-16 20:01:38 -06001196 Add_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001197 return false;
1198 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001199 if (dat->Has_Data_Media) {
1200 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001201 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1202 dat->UnMount(false);
1203 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001204 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001205 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001206 return true;
1207 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001208 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001209 return false;
1210 }
1211 return false;
1212}
1213
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001214int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1215 std::vector<TWPartition*>::iterator iter;
1216 int ret = false;
1217 bool found = false;
1218 string Local_Path = TWFunc::Get_Root_Path(Path);
1219
1220 if (Local_Path == "/tmp" || Local_Path == "/")
1221 return true;
1222
1223 // Iterate through all partitions
1224 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1225 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1226 ret = (*iter)->Repair();
1227 found = true;
1228 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1229 (*iter)->Repair();
1230 }
1231 }
1232 if (found) {
1233 return ret;
1234 } else if (Display_Error) {
1235 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1236 } else {
1237 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1238 }
1239 return false;
1240}
1241
Dees_Troy51a0e822012-09-05 15:24:24 -04001242void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001243 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001244 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001245
Dees_Troy2673cec2013-04-02 20:22:16 +00001246 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001247 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001248 if ((*iter)->Can_Be_Mounted) {
1249 (*iter)->Update_Size(true);
1250 if ((*iter)->Mount_Point == "/system") {
1251 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1252 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1253 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1254 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1255 } else if ((*iter)->Mount_Point == "/cache") {
1256 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1257 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1258 } else if ((*iter)->Mount_Point == "/sd-ext") {
1259 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1260 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1261 if ((*iter)->Backup_Size == 0) {
1262 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1263 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1264 } else
1265 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001266 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001267 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001268 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001269 if ((*iter)->Backup_Size == 0) {
1270 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1271 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1272 } else
1273 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001274 } else if ((*iter)->Mount_Point == "/boot") {
1275 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1276 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1277 if ((*iter)->Backup_Size == 0) {
1278 DataManager::SetValue("tw_has_boot_partition", 0);
1279 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1280 } else
1281 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001282 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001283#ifdef SP1_NAME
1284 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1285 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1286 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1287 }
1288#endif
1289#ifdef SP2_NAME
1290 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1291 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1292 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1293 }
1294#endif
1295#ifdef SP3_NAME
1296 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1297 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1298 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1299 }
1300#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001301 } else {
1302 // Handle unmountable partitions in case we reset defaults
1303 if ((*iter)->Mount_Point == "/boot") {
1304 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1305 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1306 if ((*iter)->Backup_Size == 0) {
1307 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1308 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1309 } else
1310 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1311 } else if ((*iter)->Mount_Point == "/recovery") {
1312 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1313 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1314 if ((*iter)->Backup_Size == 0) {
1315 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1316 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1317 } else
1318 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001319 } else if ((*iter)->Mount_Point == "/data") {
1320 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001321 }
1322#ifdef SP1_NAME
1323 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1324 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1325 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1326 }
1327#endif
1328#ifdef SP2_NAME
1329 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1330 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1331 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1332 }
1333#endif
1334#ifdef SP3_NAME
1335 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1336 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1337 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1338 }
1339#endif
Dees_Troy51127312012-09-08 13:08:49 -04001340 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001341 }
that39b7c162013-11-22 22:07:33 +01001342 gui_print("...done\n");
Dees_Troy51127312012-09-08 13:08:49 -04001343 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1344 string current_storage_path = DataManager::GetCurrentStoragePath();
1345 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001346 if (FreeStorage != NULL) {
1347 // Attempt to mount storage
1348 if (!FreeStorage->Mount(false)) {
1349 // We couldn't mount storage... check to see if we have dual storage
1350 int has_dual_storage;
1351 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1352 if (has_dual_storage == 1) {
1353 // We have dual storage, see if we're using the internal storage that should always be present
1354 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001355 if (!FreeStorage->Is_Encrypted) {
1356 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001357 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001358 }
Dees_Troy8170a922012-09-18 15:40:25 -04001359 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1360 } else {
1361 // We were using external, flip to internal
1362 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1363 current_storage_path = DataManager::GetCurrentStoragePath();
1364 FreeStorage = Find_Partition_By_Path(current_storage_path);
1365 if (FreeStorage != NULL) {
1366 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1367 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001368 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001369 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1370 }
1371 }
1372 } else {
1373 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001374 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001375 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1376 }
1377 } else {
1378 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1379 }
1380 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001381 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001382 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001383 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001384 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001385 return;
1386}
1387
1388int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001389#ifdef TW_INCLUDE_CRYPTO
1390 int ret_val, password_len;
1391 char crypto_blkdev[255], cPassword[255];
1392 size_t result;
Ethan Yonker253368a2014-11-25 15:00:52 -06001393 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001394
1395 property_set("ro.crypto.state", "encrypted");
a39552696ff55ce2013-01-08 16:14:56 +00001396
Ethan Yonker253368a2014-11-25 15:00:52 -06001397 // Mount any partitions that need to be mounted for decrypt
1398 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1399 if ((*iter)->Mount_To_Decrypt) {
1400 (*iter)->Mount(true);
1401 }
a39552696ff55ce2013-01-08 16:14:56 +00001402 }
a39552696ff55ce2013-01-08 16:14:56 +00001403
Dees_Troy5bf43922012-09-07 16:07:55 -04001404 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001405 int pwret = cryptfs_check_passwd(cPassword);
1406
Ethan Yonker253368a2014-11-25 15:00:52 -06001407 // Unmount any partitions that were needed for decrypt
1408 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1409 if ((*iter)->Mount_To_Decrypt) {
1410 (*iter)->UnMount(false);
1411 }
1412 }
1413
a39552696ff55ce2013-01-08 16:14:56 +00001414 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001415 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001416 return -1;
1417 }
a39552696ff55ce2013-01-08 16:14:56 +00001418
Dees_Troy5bf43922012-09-07 16:07:55 -04001419 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1420 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001421 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001422 } else {
1423 TWPartition* dat = Find_Partition_By_Path("/data");
1424 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001425 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001426 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1427 dat->Is_Decrypted = true;
1428 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001429 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001430 dat->Current_File_System = dat->Fstab_File_System; // Needed if we're ignoring blkid because encrypted devices start out as emmc
Dees_Troy2673cec2013-04-02 20:22:16 +00001431 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001432
Dees_Troy5bf43922012-09-07 16:07:55 -04001433 // Sleep for a bit so that the device will be ready
1434 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001435 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001436 dat->Storage_Path = "/data/media/0";
1437 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001438 DataManager::SetValue("tw_storage_path", "/data/media/0");
Ethan Yonkerec0fa4a2014-11-20 09:51:03 -06001439 DataManager::SetValue("tw_settings_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001440 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001441 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001442 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001443 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001444 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001445 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001446 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001447 }
1448 return 0;
1449#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001450 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001451 return -1;
1452#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001453 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001454}
1455
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001456int TWPartitionManager::Fix_Permissions(void) {
1457 int result = 0;
1458 if (!Mount_By_Path("/data", true))
1459 return false;
1460
1461 if (!Mount_By_Path("/system", true))
1462 return false;
1463
1464 Mount_By_Path("/sd-ext", false);
1465
1466 fixPermissions perms;
1467 result = perms.fixPerms(true, false);
thata3d31fb2014-12-21 22:27:40 +01001468#ifdef HAVE_SELINUX
1469 if (result == 0 && DataManager::GetIntValue("tw_fixperms_restorecon") == 1)
1470 result = perms.fixContexts();
1471#endif
Dees_Troy1a650e62012-10-19 20:54:32 -04001472 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001473 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001474 return result;
1475}
1476
Ethan Yonker47360be2014-04-01 10:34:34 -05001477TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1478 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1479
1480 if (!Path.empty()) {
1481 string Search_Path = TWFunc::Get_Root_Path(Path);
1482 for (; iter != Partitions.end(); iter++) {
1483 if ((*iter)->Mount_Point == Search_Path) {
1484 iter++;
1485 break;
1486 }
1487 }
1488 }
1489
1490 for (; iter != Partitions.end(); iter++) {
1491 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1492 return (*iter);
1493 }
1494 }
1495
1496 return NULL;
1497}
1498
Dees_Troyd21618c2012-10-14 18:48:49 -04001499int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001500 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1501
1502 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001503 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001504 return false;
1505 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001506 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1507 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001508 return false;
1509
Dees_Troy2673cec2013-04-02 20:22:16 +00001510 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1511 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001512 return false;
1513 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001514 return true;
1515}
1516
Dees_Troy8170a922012-09-18 15:40:25 -04001517int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001518 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001519 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001520 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001521
Dees_Troy8170a922012-09-18 15:40:25 -04001522 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001523 string Lun_File_str = CUSTOM_LUN_FILE;
1524 size_t found = Lun_File_str.find("%");
1525 if (found != string::npos) {
1526 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1527 if (TWFunc::Path_Exists(lun_file))
1528 has_multiple_lun = true;
1529 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001530 mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage
Ethan Yonker47360be2014-04-01 10:34:34 -05001531 if (!has_multiple_lun) {
1532 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1533 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1534 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1535 TWPartition* Mount = Find_Next_Storage("", "/data");
1536 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001537 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1538 goto error_handle;
1539 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001540 } else {
1541 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001542 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001543 }
1544 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001545 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001546 }
Dees_Troy8170a922012-09-18 15:40:25 -04001547 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001548 LOGINFO("Device has multiple lun files\n");
1549 TWPartition* Mount1;
1550 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001551 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001552 Mount1 = Find_Next_Storage("", "/data");
1553 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001554 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1555 goto error_handle;
1556 }
Matt Moweree717062014-04-26 01:46:46 -05001557 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001558 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1559 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001560 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001561 }
1562 } else {
1563 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001564 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001565 }
Dees_Troy8170a922012-09-18 15:40:25 -04001566 }
Matt Mowerb6ef4782014-11-06 02:24:36 -06001567 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001568 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001569error_handle:
1570 if (mtp_was_enabled)
1571 if (!Enable_MTP())
1572 Disable_MTP();
1573 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001574}
1575
1576int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001577 int index, ret;
1578 char lun_file[255], ch[2] = {0, 0};
1579 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001580
1581 for (index=0; index<2; index++) {
1582 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001583 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001584 if (ret < 0) {
1585 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001586 }
Dees_Troy8170a922012-09-18 15:40:25 -04001587 }
Dees_Troye58d5262012-09-21 12:27:57 -04001588 Mount_All_Storage();
1589 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001590 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001591 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001592 if (mtp_was_enabled)
1593 if (!Enable_MTP())
1594 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001595 if (ret < 0 && index == 0) {
1596 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1597 return false;
1598 } else {
1599 return true;
1600 }
Dees_Troy8170a922012-09-18 15:40:25 -04001601 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001602}
1603
1604void TWPartitionManager::Mount_All_Storage(void) {
1605 std::vector<TWPartition*>::iterator iter;
1606
1607 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1608 if ((*iter)->Is_Storage)
1609 (*iter)->Mount(false);
1610 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001611}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001612
Dees_Troyd0384ef2012-10-12 12:15:42 -04001613void TWPartitionManager::UnMount_Main_Partitions(void) {
1614 // Unmounts system and data if data is not data/media
1615 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001616 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001617
1618 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1619
1620 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001621 if (!datamedia)
1622 UnMount_By_Path("/data", true);
1623
Dees_Troyd0384ef2012-10-12 12:15:42 -04001624 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1625 Boot_Partition->UnMount(true);
1626}
1627
Dees_Troy9350b8d2012-09-27 12:38:38 -04001628int TWPartitionManager::Partition_SDCard(void) {
1629 char mkdir_path[255], temp[255], line[512];
1630 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1631 int ext, swap, total_size = 0, fat_size;
1632 FILE* fp;
1633
Dees_Troy2673cec2013-04-02 20:22:16 +00001634 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001635#ifdef TW_EXTERNAL_STORAGE_PATH
1636 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1637#else
1638 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1639#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001640 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001641 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001642 return false;
1643 }
1644 if (!SDCard->UnMount(true))
1645 return false;
1646 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1647 if (SDext != NULL) {
1648 if (!SDext->UnMount(true))
1649 return false;
1650 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001651
1652 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001653 Device = SDCard->Actual_Block_Device;
1654 // Just use the root block device
1655 Device.resize(strlen("/dev/block/mmcblkX"));
1656
1657 // Find the size of the block device:
1658 fp = fopen("/proc/partitions", "rt");
1659 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001660 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001661 return false;
1662 }
1663
1664 while (fgets(line, sizeof(line), fp) != NULL)
1665 {
1666 unsigned long major, minor, blocks;
1667 char device[512];
1668 char tmpString[64];
1669
1670 if (strlen(line) < 7 || line[0] == 'm') continue;
1671 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1672
1673 tmpdevice = "/dev/block/";
1674 tmpdevice += device;
1675 if (tmpdevice == Device) {
1676 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001677 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001678 break;
1679 }
1680 }
1681 fclose(fp);
1682
1683 DataManager::GetValue("tw_sdext_size", ext);
1684 DataManager::GetValue("tw_swap_size", swap);
1685 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1686 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001687 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 -04001688 memset(temp, 0, sizeof(temp));
1689 sprintf(temp, "%i", fat_size);
1690 fat_str = temp;
1691 memset(temp, 0, sizeof(temp));
1692 sprintf(temp, "%i", fat_size + ext);
1693 ext_str = temp;
1694 memset(temp, 0, sizeof(temp));
1695 sprintf(temp, "%i", fat_size + ext + swap);
1696 swap_str = temp;
1697 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001698 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001699 return false;
1700 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001701 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001702 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001703 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001704 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001705 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001706 Update_System_Details();
1707 return false;
1708 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001709 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001710 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001711 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001712 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001713 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001714 return false;
1715 }
1716 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001717 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001718 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001719 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001720 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001721 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001722 Update_System_Details();
1723 return false;
1724 }
1725 }
1726 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001727 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001728 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001729 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001730 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001731 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001732 Update_System_Details();
1733 return false;
1734 }
1735 }
1736 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1737#ifdef TW_EXTERNAL_STORAGE_PATH
1738 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1739 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1740 memset(mkdir_path, 0, sizeof(mkdir_path));
1741 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1742#else
1743 Mount_By_Path("/sdcard", 1);
1744 strcpy(mkdir_path, "/sdcard/TWRP");
1745#endif
1746 mkdir(mkdir_path, 0777);
1747 DataManager::Flush();
1748#ifdef TW_EXTERNAL_STORAGE_PATH
1749 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1750 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1751 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1752#else
1753 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1754 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1755 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1756#endif
1757 if (ext > 0) {
1758 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001759 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001760 return false;
1761 }
1762 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001763 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1764 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001765 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001766 }
1767
1768 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001769 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001770 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001771}
Dees_Troya13d74f2013-03-24 08:54:55 -05001772
1773void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1774 std::vector<TWPartition*>::iterator iter;
1775 if (ListType == "mount") {
1776 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1777 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1778 struct PartitionList part;
1779 part.Display_Name = (*iter)->Display_Name;
1780 part.Mount_Point = (*iter)->Mount_Point;
1781 part.selected = (*iter)->Is_Mounted();
1782 Partition_List->push_back(part);
1783 }
1784 }
1785 } else if (ListType == "storage") {
1786 char free_space[255];
1787 string Current_Storage = DataManager::GetCurrentStoragePath();
1788 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1789 if ((*iter)->Is_Storage) {
1790 struct PartitionList part;
1791 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1792 part.Display_Name = (*iter)->Storage_Name + " (";
1793 part.Display_Name += free_space;
1794 part.Display_Name += "MB)";
1795 part.Mount_Point = (*iter)->Storage_Path;
1796 if ((*iter)->Storage_Path == Current_Storage)
1797 part.selected = 1;
1798 else
1799 part.selected = 0;
1800 Partition_List->push_back(part);
1801 }
1802 }
1803 } else if (ListType == "backup") {
1804 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001805 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001806 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001807 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001808 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001809 Backup_Size = (*iter)->Backup_Size;
1810 if ((*iter)->Has_SubPartition) {
1811 std::vector<TWPartition*>::iterator subpart;
1812
1813 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1814 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1815 Backup_Size += (*subpart)->Backup_Size;
1816 }
1817 }
1818 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001819 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1820 part.Display_Name += backup_size;
1821 part.Display_Name += "MB)";
1822 part.Mount_Point = (*iter)->Backup_Path;
1823 part.selected = 0;
1824 Partition_List->push_back(part);
1825 }
1826 }
1827 } else if (ListType == "restore") {
1828 string Restore_List, restore_path;
1829 TWPartition* restore_part = NULL;
1830
1831 DataManager::GetValue("tw_restore_list", Restore_List);
1832 if (!Restore_List.empty()) {
1833 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1834 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1835 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001836 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001837 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001838 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001839 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001840 } else {
1841 struct PartitionList part;
1842 part.Display_Name = restore_part->Backup_Display_Name;
1843 part.Mount_Point = restore_part->Backup_Path;
1844 part.selected = 1;
1845 Partition_List->push_back(part);
1846 }
1847 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001848 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001849 }
1850 start_pos = end_pos + 1;
1851 end_pos = Restore_List.find(";", start_pos);
1852 }
1853 }
1854 } else if (ListType == "wipe") {
1855 struct PartitionList dalvik;
1856 dalvik.Display_Name = "Dalvik Cache";
1857 dalvik.Mount_Point = "DALVIK";
1858 dalvik.selected = 0;
1859 Partition_List->push_back(dalvik);
1860 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1861 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1862 struct PartitionList part;
1863 part.Display_Name = (*iter)->Display_Name;
1864 part.Mount_Point = (*iter)->Mount_Point;
1865 part.selected = 0;
1866 Partition_List->push_back(part);
1867 }
1868 if ((*iter)->Has_Android_Secure) {
1869 struct PartitionList part;
1870 part.Display_Name = (*iter)->Backup_Display_Name;
1871 part.Mount_Point = (*iter)->Backup_Path;
1872 part.selected = 0;
1873 Partition_List->push_back(part);
1874 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001875 if ((*iter)->Has_Data_Media) {
1876 struct PartitionList datamedia;
1877 datamedia.Display_Name = (*iter)->Storage_Name;
1878 datamedia.Mount_Point = "INTERNAL";
1879 datamedia.selected = 0;
1880 Partition_List->push_back(datamedia);
1881 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001882 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06001883 } else if (ListType == "flashimg") {
1884 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1885 if ((*iter)->Can_Flash_Img && (*iter)->Is_Present) {
1886 struct PartitionList part;
1887 part.Display_Name = (*iter)->Backup_Display_Name;
1888 part.Mount_Point = (*iter)->Backup_Path;
1889 part.selected = 0;
1890 Partition_List->push_back(part);
1891 }
1892 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001893 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001894 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001895 }
1896}
1897
1898int TWPartitionManager::Fstab_Processed(void) {
1899 return Partitions.size();
1900}
Dees_Troyd93bda52013-07-03 19:55:19 +00001901
1902void TWPartitionManager::Output_Storage_Fstab(void) {
1903 std::vector<TWPartition*>::iterator iter;
1904 char storage_partition[255];
1905 string Temp;
1906 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1907
1908 if (fp == NULL) {
1909 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1910 return;
1911 }
1912
1913 // Iterate through all partitions
1914 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1915 if ((*iter)->Is_Storage) {
1916 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1917 strcpy(storage_partition, Temp.c_str());
1918 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1919 }
1920 }
1921 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001922}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02001923
1924TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
1925{
1926 TWPartition *res = NULL;
1927 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
1928 if(!(*iter)->Is_Storage)
1929 continue;
1930
1931 if((*iter)->Is_Settings_Storage)
1932 return *iter;
1933
1934 if(!res)
1935 res = *iter;
1936 }
1937 return res;
1938}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001939
1940bool TWPartitionManager::Enable_MTP(void) {
1941#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05001942 if (mtppid) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001943 LOGERR("MTP already enabled\n");
1944 return true;
1945 }
1946 //Launch MTP Responder
1947 LOGINFO("Starting MTP\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06001948
1949 int mtppipe[2];
1950
1951 if (pipe(mtppipe) < 0) {
1952 LOGERR("Error creating MTP pipe\n");
1953 return false;
1954 }
1955
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06001956 char old_value[PROPERTY_VALUE_MAX];
1957 property_get("sys.usb.config", old_value, "error");
1958 if (strcmp(old_value, "error") != 0 && strcmp(old_value, "mtp,adb") != 0) {
1959 char vendor[PROPERTY_VALUE_MAX];
1960 char product[PROPERTY_VALUE_MAX];
1961 property_set("sys.usb.config", "none");
1962 property_get("usb.vendor", vendor, "18D1");
1963 property_get("usb.product.mtpadb", product, "4EE2");
1964 string vendorstr = vendor;
1965 string productstr = product;
1966 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
1967 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
1968 property_set("sys.usb.config", "mtp,adb");
1969 }
Ethan Yonker6d154c42014-09-03 14:19:43 -05001970 /* To enable MTP debug, use the twrp command line feature to
1971 * twrp set tw_mtp_debug 1
1972 */
1973 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
Ethan Yonker1b039202015-01-30 10:08:48 -06001974 mtppid = mtp->forkserver(mtppipe);
1975 if (mtppid) {
1976 close(mtppipe[0]); // Host closes read side
1977 mtp_write_fd = mtppipe[1];
1978 DataManager::SetValue("tw_mtp_enabled", 1);
1979 Add_All_MTP_Storage();
1980 return true;
Ethan Yonker726a0202014-12-16 20:01:38 -06001981 } else {
1982 close(mtppipe[0]);
1983 close(mtppipe[1]);
Ethan Yonker1b039202015-01-30 10:08:48 -06001984 LOGERR("Failed to enable MTP\n");
1985 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001986 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001987#else
1988 LOGERR("MTP support not included\n");
1989#endif
1990 DataManager::SetValue("tw_mtp_enabled", 0);
1991 return false;
1992}
1993
Ethan Yonker1b039202015-01-30 10:08:48 -06001994void TWPartitionManager::Add_All_MTP_Storage(void) {
1995#ifdef TW_HAS_MTP
1996 std::vector<TWPartition*>::iterator iter;
1997
1998 if (!mtppid)
1999 return; // MTP is not enabled
2000
2001 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2002 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false))
2003 Add_Remove_MTP_Storage((*iter), MTP_MESSAGE_ADD_STORAGE);
2004 }
2005#else
2006 return;
2007#endif
2008}
2009
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002010bool TWPartitionManager::Disable_MTP(void) {
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002011 char old_value[PROPERTY_VALUE_MAX];
2012 property_get("sys.usb.config", old_value, "error");
2013 if (strcmp(old_value, "adb") != 0) {
2014 char vendor[PROPERTY_VALUE_MAX];
2015 char product[PROPERTY_VALUE_MAX];
2016 property_set("sys.usb.config", "none");
2017 property_get("usb.vendor", vendor, "18D1");
2018 property_get("usb.product.adb", product, "D002");
2019 string vendorstr = vendor;
2020 string productstr = product;
2021 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2022 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2023 usleep(2000);
2024 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002025#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002026 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002027 LOGINFO("Disabling MTP\n");
2028 int status;
2029 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002030 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002031 // We don't care about the exit value, but this prevents a zombie process
2032 waitpid(mtppid, &status, 0);
Ethan Yonker726a0202014-12-16 20:01:38 -06002033 close(mtp_write_fd);
2034 mtp_write_fd = -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002035 }
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002036#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002037 property_set("sys.usb.config", "adb");
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002038#ifdef TW_HAS_MTP
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002039 DataManager::SetValue("tw_mtp_enabled", 0);
2040 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002041#endif
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002042 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002043}
Ethan Yonker726a0202014-12-16 20:01:38 -06002044
2045TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) {
2046 std::vector<TWPartition*>::iterator iter;
2047
2048 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2049 if ((*iter)->MTP_Storage_ID == Storage_ID)
2050 return (*iter);
2051 }
2052 return NULL;
2053}
2054
2055bool TWPartitionManager::Add_Remove_MTP_Storage(TWPartition* Part, int message_type) {
2056#ifdef TW_HAS_MTP
2057 struct mtpmsg mtp_message;
2058
2059 if (!mtppid)
2060 return false; // MTP is disabled
2061
2062 if (mtp_write_fd < 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002063 LOGINFO("MTP: mtp_write_fd is not set\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002064 return false;
2065 }
2066
2067 if (Part) {
Ethan Yonker4bfabab2014-12-29 09:15:37 -06002068 if (Part->MTP_Storage_ID == 0)
2069 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002070 if (message_type == MTP_MESSAGE_REMOVE_STORAGE) {
2071 mtp_message.message_type = MTP_MESSAGE_REMOVE_STORAGE; // Remove
2072 LOGINFO("sending message to remove %i\n", Part->MTP_Storage_ID);
2073 mtp_message.storage_id = Part->MTP_Storage_ID;
2074 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002075 LOGINFO("error sending message to remove storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002076 return false;
2077 } else {
2078 LOGINFO("Message sent, remove storage ID: %i\n", Part->MTP_Storage_ID);
2079 return true;
2080 }
2081 } else if (message_type == MTP_MESSAGE_ADD_STORAGE && Part->Is_Mounted()) {
2082 mtp_message.message_type = MTP_MESSAGE_ADD_STORAGE; // Add
2083 mtp_message.storage_id = Part->MTP_Storage_ID;
2084 mtp_message.path = Part->Storage_Path.c_str();
2085 mtp_message.display = Part->Storage_Name.c_str();
2086 mtp_message.maxFileSize = Part->Get_Max_FileSize();
Ethan Yonker1b039202015-01-30 10:08:48 -06002087 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 -06002088 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002089 LOGINFO("error sending message to add storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002090 return false;
2091 } else {
2092 LOGINFO("Message sent, add storage ID: %i\n", Part->MTP_Storage_ID);
2093 return true;
2094 }
2095 } else {
2096 LOGERR("Unknown MTP message type: %i\n", message_type);
2097 }
2098 } else {
2099 // This hopefully never happens as the error handling should
2100 // occur in the calling function.
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002101 LOGINFO("TWPartitionManager::Add_Remove_MTP_Storage NULL partition given\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002102 }
2103 return true;
2104#else
2105 LOGERR("MTP support not included\n");
2106 DataManager::SetValue("tw_mtp_enabled", 0);
2107 return false;
2108#endif
2109}
2110
2111bool TWPartitionManager::Add_MTP_Storage(string Mount_Point) {
2112#ifdef TW_HAS_MTP
2113 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2114 if (Part) {
2115 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2116 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002117 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002118 }
2119#endif
2120 return false;
2121}
2122
2123bool TWPartitionManager::Add_MTP_Storage(unsigned int Storage_ID) {
2124#ifdef TW_HAS_MTP
2125 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2126 if (Part) {
2127 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2128 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002129 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002130 }
2131#endif
2132 return false;
2133}
2134
2135bool TWPartitionManager::Remove_MTP_Storage(string Mount_Point) {
2136#ifdef TW_HAS_MTP
2137 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2138 if (Part) {
2139 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2140 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002141 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002142 }
2143#endif
2144 return false;
2145}
2146
2147bool TWPartitionManager::Remove_MTP_Storage(unsigned int Storage_ID) {
2148#ifdef TW_HAS_MTP
2149 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2150 if (Part) {
2151 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2152 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002153 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002154 }
2155#endif
2156 return false;
2157}
Ethan Yonker96af84a2015-01-05 14:58:36 -06002158
2159bool TWPartitionManager::Flash_Image(string Filename) {
2160 int check, partition_count = 0;
2161 TWPartition* flash_part = NULL;
2162 string Flash_List, flash_path;
2163 size_t start_pos = 0, end_pos = 0;
2164
2165 gui_print("\n[IMAGE FLASH STARTED]\n\n");
2166 gui_print("Image to flash: '%s'\n", Filename.c_str());
2167
2168 if (!Mount_Current_Storage(true))
2169 return false;
2170
2171 gui_print("Calculating restore details...\n");
2172 DataManager::GetValue("tw_flash_partition", Flash_List);
2173 if (!Flash_List.empty()) {
2174 end_pos = Flash_List.find(";", start_pos);
2175 while (end_pos != string::npos && start_pos < Flash_List.size()) {
2176 flash_path = Flash_List.substr(start_pos, end_pos - start_pos);
2177 flash_part = Find_Partition_By_Path(flash_path);
2178 if (flash_part != NULL) {
2179 partition_count++;
2180 if (partition_count > 1) {
2181 LOGERR("Too many partitions selected for flashing.\n");
2182 return false;
2183 }
2184 } else {
2185 LOGERR("Unable to locate '%s' partition for flashing (flash list).\n", flash_path.c_str());
2186 return false;
2187 }
2188 start_pos = end_pos + 1;
2189 end_pos = Flash_List.find(";", start_pos);
2190 }
2191 }
2192
2193 if (partition_count == 0) {
2194 LOGERR("No partitions selected for flashing.\n");
2195 return false;
2196 }
2197
2198 DataManager::SetProgress(0.0);
2199 if (flash_part) {
2200 if (!flash_part->Flash_Image(Filename))
2201 return false;
2202 } else {
2203 LOGERR("Invalid flash partition specified.\n");
2204 return false;
2205 }
2206 gui_print_color("highlight", "[IMAGE FLASH COMPLETED]\n\n");
2207 return true;
2208}