blob: b2d7b4e025a504b6e921ed4fd4855b31f22b94d2 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_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"
Dees_Troy38bd7602012-09-14 13:33:53 -040041
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040042#ifdef TW_HAS_MTP
43#include "mtp/mtp_MtpServer.hpp"
44#include "mtp/twrpMtp.hpp"
45#endif
46
Dees Troy6f6441d2014-01-23 02:07:03 +000047extern "C" {
48 #include "cutils/properties.h"
49}
50
Dees_Troy5bf43922012-09-07 16:07:55 -040051#ifdef TW_INCLUDE_CRYPTO
52 #ifdef TW_INCLUDE_JB_CRYPTO
53 #include "crypto/jb/cryptfs.h"
54 #else
55 #include "crypto/ics/cryptfs.h"
56 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -040057#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Ethan Yonker6277c792014-09-15 14:54:30 -050059extern bool datamedia;
60
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050061TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040062 mtp_was_enabled = false;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050063}
64
Dees_Troy51a0e822012-09-05 15:24:24 -040065int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040066 FILE *fstabFile;
67 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000068 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050069 TWPartition* andsec_partition = NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -040070
71 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
72 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000073 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040074 return false;
75 }
76
77 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
78 if (fstab_line[0] != '/')
79 continue;
80
Dees_Troy2a923582012-09-20 12:13:34 -040081 if (fstab_line[strlen(fstab_line) - 1] != '\n')
82 fstab_line[strlen(fstab_line)] = '\n';
that9e0593e2014-10-08 00:01:24 +020083 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040084 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040085 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040086
Dees_Troy5bf43922012-09-07 16:07:55 -040087 if (partition->Process_Fstab_Line(line, Display_Error)) {
Matt Mowered71fa32014-04-16 13:21:47 -050088 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000089 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050090 } else {
91 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050092 }
Matt Mowered71fa32014-04-16 13:21:47 -050093 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -050094 andsec_partition = partition;
95 } else {
96 partition->Has_Android_Secure = false;
97 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -050098 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -040099 } else {
100 delete partition;
101 }
102 }
103 fclose(fstabFile);
Ethan Yonker6277c792014-09-15 14:54:30 -0500104 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) {
105 // Attempt to automatically identify /data/media emulated storage devices
106 TWPartition* Dat = Find_Partition_By_Path("/data");
107 if (Dat) {
108 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
109 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200110 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500111 settings_partition = Dat;
112 }
113 }
Dees Troy02a64532014-03-19 15:23:32 +0000114 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500115 std::vector<TWPartition*>::iterator iter;
116 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
117 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000118 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500119 break;
120 }
121 }
Dees Troy02a64532014-03-19 15:23:32 +0000122 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000123 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500124 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400125 if (!Write_Fstab()) {
126 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000127 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400128 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000129 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 }
Matt Mowered71fa32014-04-16 13:21:47 -0500131
Matt Mowerbf4efa32014-04-14 23:25:26 -0500132 if (andsec_partition) {
133 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500134 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500135 Setup_Android_Secure_Location(settings_partition);
136 }
Matt Mowered71fa32014-04-16 13:21:47 -0500137 if (settings_partition) {
138 Setup_Settings_Storage_Partition(settings_partition);
139 }
Dees_Troy51127312012-09-08 13:08:49 -0400140 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400141 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400142 return true;
143}
144
145int TWPartitionManager::Write_Fstab(void) {
146 FILE *fp;
147 std::vector<TWPartition*>::iterator iter;
148 string Line;
149
150 fp = fopen("/etc/fstab", "w");
151 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000152 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400153 return false;
154 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400155 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400156 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400157 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400158 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000159 }
160 // Handle subpartition tracking
161 if ((*iter)->Is_SubPartition) {
162 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
163 if (ParentPartition)
164 ParentPartition->Has_SubPartition = true;
165 else
166 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 -0400167 }
168 }
169 fclose(fp);
170 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400171}
172
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500173void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500174 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
175 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
176 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
177}
178
Matt Mowerbf4efa32014-04-14 23:25:26 -0500179void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
180 if (Part->Has_Android_Secure)
181 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500182 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500183 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500184}
185
Dees_Troy8170a922012-09-18 15:40:25 -0400186void TWPartitionManager::Output_Partition_Logging(void) {
187 std::vector<TWPartition*>::iterator iter;
188
189 printf("\n\nPartition Logs:\n");
190 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
191 Output_Partition((*iter));
192}
193
194void TWPartitionManager::Output_Partition(TWPartition* Part) {
195 unsigned long long mb = 1048576;
196
Gary Peck004d48b2012-11-21 16:28:18 -0800197 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 -0400198 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800199 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 -0400200 }
Gary Peck004d48b2012-11-21 16:28:18 -0800201 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500202 if (Part->Can_Be_Mounted)
203 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800204 if (Part->Can_Be_Wiped)
205 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700206 if (Part->Use_Rm_Rf)
207 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500208 if (Part->Can_Be_Backed_Up)
209 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800210 if (Part->Wipe_During_Factory_Reset)
211 printf("Wipe_During_Factory_Reset ");
212 if (Part->Wipe_Available_in_GUI)
213 printf("Wipe_Available_in_GUI ");
214 if (Part->Is_SubPartition)
215 printf("Is_SubPartition ");
216 if (Part->Has_SubPartition)
217 printf("Has_SubPartition ");
218 if (Part->Removable)
219 printf("Removable ");
220 if (Part->Is_Present)
221 printf("IsPresent ");
222 if (Part->Can_Be_Encrypted)
223 printf("Can_Be_Encrypted ");
224 if (Part->Is_Encrypted)
225 printf("Is_Encrypted ");
226 if (Part->Is_Decrypted)
227 printf("Is_Decrypted ");
228 if (Part->Has_Data_Media)
229 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000230 if (Part->Can_Encrypt_Backup)
231 printf("Can_Encrypt_Backup ");
232 if (Part->Use_Userdata_Encryption)
233 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800234 if (Part->Has_Android_Secure)
235 printf("Has_Android_Secure ");
236 if (Part->Is_Storage)
237 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500238 if (Part->Is_Settings_Storage)
239 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000240 if (Part->Ignore_Blkid)
241 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000242 if (Part->Retain_Layout_Version)
243 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800244 printf("\n");
245 if (!Part->SubPartition_Of.empty())
246 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
247 if (!Part->Symlink_Path.empty())
248 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
249 if (!Part->Symlink_Mount_Point.empty())
250 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
251 if (!Part->Primary_Block_Device.empty())
252 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
253 if (!Part->Alternate_Block_Device.empty())
254 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
255 if (!Part->Decrypted_Block_Device.empty())
256 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
257 if (Part->Length != 0)
258 printf(" Length: %i\n", Part->Length);
259 if (!Part->Display_Name.empty())
260 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500261 if (!Part->Storage_Name.empty())
262 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800263 if (!Part->Backup_Path.empty())
264 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
265 if (!Part->Backup_Name.empty())
266 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500267 if (!Part->Backup_Display_Name.empty())
268 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800269 if (!Part->Backup_FileName.empty())
270 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
271 if (!Part->Storage_Path.empty())
272 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
273 if (!Part->Current_File_System.empty())
274 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
275 if (!Part->Fstab_File_System.empty())
276 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
277 if (Part->Format_Block_Size != 0)
278 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400279 if (!Part->MTD_Name.empty())
280 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400281 string back_meth = Part->Backup_Method_By_Name();
Ethan Yonker6277c792014-09-15 14:54:30 -0500282 printf(" Backup_Method: %s\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800283 if (Part->Mount_Flags || !Part->Mount_Options.empty())
284 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker6277c792014-09-15 14:54:30 -0500285 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400286}
287
Dees_Troy51a0e822012-09-05 15:24:24 -0400288int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400289 std::vector<TWPartition*>::iterator iter;
290 int ret = false;
291 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400292 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400293
Dees_Troyd93bda52013-07-03 19:55:19 +0000294 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400295 return true;
296
Dees_Troy5bf43922012-09-07 16:07:55 -0400297 // Iterate through all partitions
298 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400299 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400300 ret = (*iter)->Mount(Display_Error);
301 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400302 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400303 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400304 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400305 }
306 if (found) {
307 return ret;
308 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000309 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000311 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400312 }
313 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400314}
315
316int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400317 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400318
Dees_Troy51127312012-09-08 13:08:49 -0400319 if (Part) {
320 if (Part->Has_SubPartition) {
321 std::vector<TWPartition*>::iterator subpart;
322
323 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
324 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
325 (*subpart)->Mount(Display_Error);
326 }
327 return Part->Mount(Display_Error);
328 } else
329 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 }
331 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000332 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000334 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400336}
337
338int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400339 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400340
Dees_Troy51127312012-09-08 13:08:49 -0400341 if (Part) {
342 if (Part->Has_SubPartition) {
343 std::vector<TWPartition*>::iterator subpart;
344
345 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
346 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
347 (*subpart)->Mount(Display_Error);
348 }
349 return Part->Mount(Display_Error);
350 } else
351 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400352 }
353 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000354 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400355 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000356 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400357 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400358}
359
360int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400361 std::vector<TWPartition*>::iterator iter;
362 int ret = false;
363 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400364 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400365
366 // Iterate through all partitions
367 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400368 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400369 ret = (*iter)->UnMount(Display_Error);
370 found = true;
371 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
372 (*iter)->UnMount(Display_Error);
373 }
374 }
375 if (found) {
376 return ret;
377 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000378 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400379 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000380 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400381 }
382 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400383}
384
385int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400386 TWPartition* Part = Find_Partition_By_Block(Block);
387
388 if (Part) {
389 if (Part->Has_SubPartition) {
390 std::vector<TWPartition*>::iterator subpart;
391
392 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
393 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
394 (*subpart)->UnMount(Display_Error);
395 }
396 return Part->UnMount(Display_Error);
397 } else
398 return Part->UnMount(Display_Error);
399 }
400 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000401 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400402 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000403 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400404 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400405}
406
407int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400408 TWPartition* Part = Find_Partition_By_Name(Name);
409
410 if (Part) {
411 if (Part->Has_SubPartition) {
412 std::vector<TWPartition*>::iterator subpart;
413
414 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
415 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
416 (*subpart)->UnMount(Display_Error);
417 }
418 return Part->UnMount(Display_Error);
419 } else
420 return Part->UnMount(Display_Error);
421 }
422 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000423 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400424 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000425 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400426 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400427}
428
429int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400430 TWPartition* Part = Find_Partition_By_Path(Path);
431
432 if (Part)
433 return Part->Is_Mounted();
434 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000435 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400436 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400437}
438
439int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400440 TWPartition* Part = Find_Partition_By_Block(Block);
441
442 if (Part)
443 return Part->Is_Mounted();
444 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000445 LOGINFO("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400446 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400447}
448
449int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400450 TWPartition* Part = Find_Partition_By_Name(Name);
451
452 if (Part)
453 return Part->Is_Mounted();
454 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000455 LOGINFO("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400456 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400457}
458
Dees_Troy5bf43922012-09-07 16:07:55 -0400459int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400460 string current_storage_path = DataManager::GetCurrentStoragePath();
461
462 if (Mount_By_Path(current_storage_path, Display_Error)) {
463 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
464 if (FreeStorage)
465 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
466 return true;
467 }
468 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400469}
470
Dees_Troy5bf43922012-09-07 16:07:55 -0400471int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
472 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
473}
474
475TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
476 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400477 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400478
479 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400480 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400481 return (*iter);
482 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400483 return NULL;
484}
485
Dees_Troy5bf43922012-09-07 16:07:55 -0400486TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400487 std::vector<TWPartition*>::iterator iter;
488
489 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400490 if ((*iter)->Primary_Block_Device == Block || (*iter)->Alternate_Block_Device == Block || ((*iter)->Is_Decrypted && (*iter)->Decrypted_Block_Device == Block))
Dees_Troy51127312012-09-08 13:08:49 -0400491 return (*iter);
492 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400493 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400494}
495
496TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400497 std::vector<TWPartition*>::iterator iter;
498
499 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
500 if ((*iter)->Display_Name == Name)
501 return (*iter);
502 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400503 return NULL;
504}
Dees_Troy51a0e822012-09-05 15:24:24 -0400505
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400506int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
507 // Check the backup name to ensure that it is the correct size and contains only valid characters
508 // and that a backup with that name doesn't already exist
509 char backup_name[MAX_BACKUP_NAME_LEN];
510 char backup_loc[255], tw_image_dir[255];
511 int copy_size;
512 int index, cur_char;
513 string Backup_Name, Backup_Loc;
514
515 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
516 copy_size = Backup_Name.size();
517 // Check size
518 if (copy_size > MAX_BACKUP_NAME_LEN) {
519 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000520 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400521 return -2;
522 }
523
524 // Check each character
525 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500526 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400527 return 0; // A "0" (zero) means to use the current timestamp for the backup name
528 for (index=0; index<copy_size; index++) {
529 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500530 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 -0400531 // These are valid characters
532 // Numbers
533 // Upper case letters
534 // Lower case letters
535 // Space
536 // and -_.{}[]
537 } else {
538 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000539 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400540 return -3;
541 }
542 }
543
544 // Check to make sure that a backup with this name doesn't already exist
545 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
546 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500547 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500548 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400549 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000550 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400551 return -4;
552 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400553 // No problems found, return 0
554 return 0;
555}
556
Dees_Troy43d8b002012-09-17 16:00:01 -0400557bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
558{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500559 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400560 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500561 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500562 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400563
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500564 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400565 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400566
Dees_Troyb46a6842012-09-25 11:06:46 -0400567 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000568 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400569
570 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500571 md5sum.setfn(Backup_Folder + Backup_Filename);
572 if (md5sum.computeMD5() == 0)
573 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000574 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500575 else
576 return -1;
577 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000578 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400579 } else {
580 char filename[512];
581 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500582 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400583 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500584 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000585 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400586 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000587 if (TWFunc::Path_Exists(filename)) {
588 if (md5sum.computeMD5() == 0) {
589 if (md5sum.write_md5digest() != 0)
590 {
591 gui_print(" * MD5 Error.\n");
592 return false;
593 }
594 } else {
595 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500596 return false;
597 }
598 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400599 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400600 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500601 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400602 }
603 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000604 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400605 return false;
606 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000607 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400608 }
609 return true;
610}
611
Dees_Troy093b7642012-09-21 15:59:38 -0400612bool 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 -0400613 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500614 int img_bps;
615 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400616 unsigned long total_time, remain_time, section_time;
617 int use_compression, backup_time;
618 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500619 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400620
621 if (Part == NULL)
622 return true;
623
Dees_Troy093b7642012-09-21 15:59:38 -0400624 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
625
626 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
627 if (use_compression)
628 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
629 else
630 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
631
632 // We know the speed for both, how far into the whole backup are we, based on time
633 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
634 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
635
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500636 //pos = (total_time - remain_time) / (float) total_time;
637 total_size = *file_bytes + *img_bytes;
638 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
639 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000640 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400641
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500642 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400643
644 // And get the time
645 if (Part->Backup_Method == 1)
646 section_time = Part->Backup_Size / file_bps;
647 else
648 section_time = Part->Backup_Size / img_bps;
649
650 // Set the position
651 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500652 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400653
Dees_Troy43d8b002012-09-17 16:00:01 -0400654 time(&start);
655
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500656 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
657 current_size += Part->Backup_Size;
658 pos = (float)((float)(current_size) / (float)(total_size));
659 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400660 if (Part->Has_SubPartition) {
661 std::vector<TWPartition*>::iterator subpart;
662
663 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500664 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500665 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size))
Dees_Troy8170a922012-09-18 15:40:25 -0400666 return false;
Dees_Troy2727b992013-08-14 20:09:30 +0000667 sync();
668 sync();
Dees_Troy8170a922012-09-18 15:40:25 -0400669 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
670 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400671 if (Part->Backup_Method == 1) {
672 *file_bytes_remaining -= (*subpart)->Backup_Size;
673 } else {
674 *img_bytes_remaining -= (*subpart)->Backup_Size;
675 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500676 current_size += Part->Backup_Size;
677 pos = (float)(current_size / total_size);
678 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400679 }
680 }
681 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400682 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400683 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000684 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400685 if (Part->Backup_Method == 1) {
686 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400687 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400688 } else {
689 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400690 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400691 }
692 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
693 } else {
694 return false;
695 }
696}
697
698int TWPartitionManager::Run_Backup(void) {
699 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500700 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400701 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 -0400702 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500703 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400704 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400705 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400706 struct tm *t;
707 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500708 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400709 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500710 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400711
712 time(&total_start);
713
714 Update_System_Details();
715
716 if (!Mount_Current_Storage(true))
717 return false;
718
719 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400720 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400721 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400722 else
723 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400724
Dees_Troy43d8b002012-09-17 16:00:01 -0400725 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
726 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000727 if (Backup_Name == "(Current Date)") {
728 Backup_Name = TWFunc::Get_Current_Date();
729 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
730 TWFunc::Auto_Generate_Backup_Name();
731 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400732 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000733 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400734 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000735 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400736
Dees_Troy2673cec2013-04-02 20:22:16 +0000737 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500738 DataManager::GetValue("tw_backup_list", Backup_List);
739 if (!Backup_List.empty()) {
740 end_pos = Backup_List.find(";", start_pos);
741 while (end_pos != string::npos && start_pos < Backup_List.size()) {
742 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
743 backup_part = Find_Partition_By_Path(backup_path);
744 if (backup_part != NULL) {
745 partition_count++;
746 if (backup_part->Backup_Method == 1)
747 file_bytes += backup_part->Backup_Size;
748 else
749 img_bytes += backup_part->Backup_Size;
750 if (backup_part->Has_SubPartition) {
751 std::vector<TWPartition*>::iterator subpart;
752
753 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000754 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 -0500755 partition_count++;
756 if ((*subpart)->Backup_Method == 1)
757 file_bytes += (*subpart)->Backup_Size;
758 else
759 img_bytes += (*subpart)->Backup_Size;
760 }
761 }
Dees_Troy8170a922012-09-18 15:40:25 -0400762 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500763 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000764 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400765 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500766 start_pos = end_pos + 1;
767 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400768 }
769 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400770
771 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000772 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400773 return false;
774 }
775 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000776 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500777 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400778 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
779 if (storage != NULL) {
780 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000781 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400782 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000783 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400784 return false;
785 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000786 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400787 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000788 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400789 return false;
790 }
791 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500792 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400793
Dees_Troy2673cec2013-04-02 20:22:16 +0000794 gui_print("\n[BACKUP STARTED]\n");
795 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000796 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000797 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000798 return false;
799 }
800
Dees_Troy2673cec2013-04-02 20:22:16 +0000801 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400802
Dees_Troya13d74f2013-03-24 08:54:55 -0500803 start_pos = 0;
804 end_pos = Backup_List.find(";", start_pos);
805 while (end_pos != string::npos && start_pos < Backup_List.size()) {
806 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
807 backup_part = Find_Partition_By_Path(backup_path);
808 if (backup_part != NULL) {
809 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
810 return false;
811 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000812 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500813 }
814 start_pos = end_pos + 1;
815 end_pos = Backup_List.find(";", start_pos);
816 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400817
818 // Average BPS
819 if (img_time == 0)
820 img_time = 1;
821 if (file_time == 0)
822 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400823 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500824 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400825
Dees_Troy2673cec2013-04-02 20:22:16 +0000826 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
827 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400828
829 time(&total_stop);
830 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500831 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500832 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400833
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500834 int prev_img_bps, use_compression;
835 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400836 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
837 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500838 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400839
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500840 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400841 if (use_compression)
842 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500843 else
Dees_Troy093b7642012-09-21 15:59:38 -0400844 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
845 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500846 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400847
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500848 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400849 if (use_compression)
850 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
851 else
852 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
853
Dees_Troy2673cec2013-04-02 20:22:16 +0000854 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400855 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400856 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500857 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000858 string backup_log = Full_Backup_Path + "recovery.log";
859 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
860 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400861}
862
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500863bool 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 -0400864 time_t Start, Stop;
865 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500866 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
867 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size))
Dees_Troy4a2a1262012-09-18 09:33:47 -0400868 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400869 if (Part->Has_SubPartition) {
870 std::vector<TWPartition*>::iterator subpart;
871
872 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
873 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500874 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size))
Dees_Troy8170a922012-09-18 15:40:25 -0400875 return false;
876 }
877 }
878 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400879 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000880 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 -0400881 return true;
882}
883
Dees_Troy51a0e822012-09-05 15:24:24 -0400884int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400885 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500886 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400887 time_t rStart, rStop;
888 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500889 string Restore_List, restore_path;
890 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500891 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400892
Dees_Troy2673cec2013-04-02 20:22:16 +0000893 gui_print("\n[RESTORE STARTED]\n\n");
894 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400895
Dees_Troy4a2a1262012-09-18 09:33:47 -0400896 if (!Mount_Current_Storage(true))
897 return false;
898
899 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500900 if (check_md5 > 0) {
901 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
902 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000903 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500904 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000905 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500906 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500907 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500908 DataManager::GetValue("tw_restore_selected", Restore_List);
909 if (!Restore_List.empty()) {
910 end_pos = Restore_List.find(";", start_pos);
911 while (end_pos != string::npos && start_pos < Restore_List.size()) {
912 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
913 restore_part = Find_Partition_By_Path(restore_path);
914 if (restore_part != NULL) {
915 partition_count++;
916 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
917 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500918 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500919 if (restore_part->Has_SubPartition) {
920 std::vector<TWPartition*>::iterator subpart;
921
922 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
923 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400924 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500925 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500926 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500927 }
928 }
929 }
930 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500931 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500932 }
933 start_pos = end_pos + 1;
934 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400935 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400936 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400937
938 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000939 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400940 return false;
941 }
942
Dees_Troy2673cec2013-04-02 20:22:16 +0000943 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500944 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000945 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500946
Dees_Troya13d74f2013-03-24 08:54:55 -0500947 start_pos = 0;
948 if (!Restore_List.empty()) {
949 end_pos = Restore_List.find(";", start_pos);
950 while (end_pos != string::npos && start_pos < Restore_List.size()) {
951 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
952 restore_part = Find_Partition_By_Path(restore_path);
953 if (restore_part != NULL) {
954 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500955 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500956 return false;
957 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000958 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500959 }
960 start_pos = end_pos + 1;
961 end_pos = Restore_List.find(";", start_pos);
962 }
963 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400964 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400965 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400966 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400967 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500968 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500969 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400970 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400971}
972
973void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400974 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500975 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000976 bool get_date = true, check_encryption = true;
977
978 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400979
980 DIR* d;
981 d = opendir(Restore_Name.c_str());
982 if (d == NULL)
983 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000984 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400985 return;
986 }
987
988 struct dirent* de;
989 while ((de = readdir(d)) != NULL)
990 {
991 // Strip off three components
992 char str[256];
993 char* label;
994 char* fstype = NULL;
995 char* extn = NULL;
996 char* ptr;
997
998 strcpy(str, de->d_name);
999 if (strlen(str) <= 2)
1000 continue;
1001
1002 if (get_date) {
1003 char file_path[255];
1004 struct stat st;
1005
1006 strcpy(file_path, Restore_Name.c_str());
1007 strcat(file_path, "/");
1008 strcat(file_path, str);
1009 stat(file_path, &st);
1010 string backup_date = ctime((const time_t*)(&st.st_mtime));
1011 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1012 get_date = false;
1013 }
1014
1015 label = str;
1016 ptr = label;
1017 while (*ptr && *ptr != '.') ptr++;
1018 if (*ptr == '.')
1019 {
1020 *ptr = 0x00;
1021 ptr++;
1022 fstype = ptr;
1023 }
1024 while (*ptr && *ptr != '.') ptr++;
1025 if (*ptr == '.')
1026 {
1027 *ptr = 0x00;
1028 ptr++;
1029 extn = ptr;
1030 }
1031
Dees_Troy83bd4832013-05-04 12:39:56 +00001032 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001033 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001034 if (extnlength != 3 && extnlength != 6) continue;
1035 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1036 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1037
1038 if (check_encryption) {
1039 string filename = Restore_Name + "/";
1040 filename += de->d_name;
1041 if (TWFunc::Get_File_Type(filename) == 2) {
1042 LOGINFO("'%s' is encrypted\n", filename.c_str());
1043 DataManager::SetValue("tw_restore_encrypted", 1);
1044 }
1045 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001046 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001047
1048 TWPartition* Part = Find_Partition_By_Path(label);
1049 if (Part == NULL)
1050 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001051 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -04001052 continue;
1053 }
1054
1055 Part->Backup_FileName = de->d_name;
1056 if (strlen(extn) > 3) {
1057 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1058 }
1059
Dees_Troya13d74f2013-03-24 08:54:55 -05001060 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001061 }
1062 closedir(d);
1063
Dees_Troya13d74f2013-03-24 08:54:55 -05001064 // Set the final value
1065 DataManager::SetValue("tw_restore_list", Restore_List);
1066 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001067 return;
1068}
1069
1070int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001071 std::vector<TWPartition*>::iterator iter;
1072 int ret = false;
1073 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001074 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001075
1076 // Iterate through all partitions
1077 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001078 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001079 if (Path == "/and-sec")
1080 ret = (*iter)->Wipe_AndSec();
1081 else
1082 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001083 found = true;
1084 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1085 (*iter)->Wipe();
1086 }
1087 }
1088 if (found) {
1089 return ret;
1090 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001091 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001092 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001093}
1094
1095int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001096 TWPartition* Part = Find_Partition_By_Block(Block);
1097
1098 if (Part) {
1099 if (Part->Has_SubPartition) {
1100 std::vector<TWPartition*>::iterator subpart;
1101
1102 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1103 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1104 (*subpart)->Wipe();
1105 }
1106 return Part->Wipe();
1107 } else
1108 return Part->Wipe();
1109 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001110 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001111 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001112}
1113
1114int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001115 TWPartition* Part = Find_Partition_By_Name(Name);
1116
1117 if (Part) {
1118 if (Part->Has_SubPartition) {
1119 std::vector<TWPartition*>::iterator subpart;
1120
1121 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1122 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1123 (*subpart)->Wipe();
1124 }
1125 return Part->Wipe();
1126 } else
1127 return Part->Wipe();
1128 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001129 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001130 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001131}
1132
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001133int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1134 std::vector<TWPartition*>::iterator iter;
1135 int ret = false;
1136 bool found = false;
1137 string Local_Path = TWFunc::Get_Root_Path(Path);
1138
1139 // Iterate through all partitions
1140 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1141 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1142 if (Path == "/and-sec")
1143 ret = (*iter)->Wipe_AndSec();
1144 else
1145 ret = (*iter)->Wipe(New_File_System);
1146 found = true;
1147 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1148 (*iter)->Wipe(New_File_System);
1149 }
1150 }
1151 if (found) {
1152 return ret;
1153 } else
1154 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1155 return false;
1156}
1157
1158int TWPartitionManager::Wipe_By_Block(string Block, string New_File_System) {
1159 TWPartition* Part = Find_Partition_By_Block(Block);
1160
1161 if (Part) {
1162 if (Part->Has_SubPartition) {
1163 std::vector<TWPartition*>::iterator subpart;
1164
1165 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1166 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1167 (*subpart)->Wipe(New_File_System);
1168 }
1169 return Part->Wipe(New_File_System);
1170 } else
1171 return Part->Wipe(New_File_System);
1172 }
1173 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1174 return false;
1175}
1176
1177int TWPartitionManager::Wipe_By_Name(string Name, string New_File_System) {
1178 TWPartition* Part = Find_Partition_By_Name(Name);
1179
1180 if (Part) {
1181 if (Part->Has_SubPartition) {
1182 std::vector<TWPartition*>::iterator subpart;
1183
1184 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1185 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1186 (*subpart)->Wipe();
1187 }
1188 return Part->Wipe(New_File_System);
1189 } else
1190 return Part->Wipe(New_File_System);
1191 }
1192 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1193 return false;
1194}
1195
Dees_Troy51a0e822012-09-05 15:24:24 -04001196int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001197 std::vector<TWPartition*>::iterator iter;
1198 int ret = true;
1199
1200 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001201 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001202 if (!(*iter)->Wipe())
1203 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001204 } else if ((*iter)->Has_Android_Secure) {
1205 if (!(*iter)->Wipe_AndSec())
1206 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001207 }
1208 }
1209 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001210}
1211
Dees_Troy38bd7602012-09-14 13:33:53 -04001212int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1213 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001214 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001215
1216 if (!Mount_By_Path("/data", true))
1217 return false;
1218
1219 if (!Mount_By_Path("/cache", true))
1220 return false;
1221
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001222 dir.push_back("/data/dalvik-cache");
1223 dir.push_back("/cache/dalvik-cache");
1224 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001225 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001226 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001227 if (stat(dir.at(i).c_str(), &st) == 0) {
1228 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001229 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001230 }
1231 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001232 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001233 if (sdext && sdext->Is_Present && sdext->Mount(false))
1234 {
1235 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1236 {
1237 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001238 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001239 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001240 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001241 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001242 return true;
1243}
1244
1245int TWPartitionManager::Wipe_Rotate_Data(void) {
1246 if (!Mount_By_Path("/data", true))
1247 return false;
1248
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001249 unlink("/data/misc/akmd*");
1250 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001251 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001252 return true;
1253}
1254
1255int TWPartitionManager::Wipe_Battery_Stats(void) {
1256 struct stat st;
1257
1258 if (!Mount_By_Path("/data", true))
1259 return false;
1260
1261 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001262 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001263 } else {
1264 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001265 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001266 }
1267 return true;
1268}
1269
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001270int TWPartitionManager::Wipe_Android_Secure(void) {
1271 std::vector<TWPartition*>::iterator iter;
1272 int ret = false;
1273 bool found = false;
1274
1275 // Iterate through all partitions
1276 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1277 if ((*iter)->Has_Android_Secure) {
1278 ret = (*iter)->Wipe_AndSec();
1279 found = true;
1280 }
1281 }
1282 if (found) {
1283 return ret;
1284 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001285 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001286 }
1287 return false;
1288}
1289
Dees_Troy38bd7602012-09-14 13:33:53 -04001290int TWPartitionManager::Format_Data(void) {
1291 TWPartition* dat = Find_Partition_By_Path("/data");
1292
1293 if (dat != NULL) {
1294 if (!dat->UnMount(true))
1295 return false;
1296
1297 return dat->Wipe_Encryption();
1298 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001300 return false;
1301 }
1302 return false;
1303}
1304
1305int TWPartitionManager::Wipe_Media_From_Data(void) {
1306 TWPartition* dat = Find_Partition_By_Path("/data");
1307
1308 if (dat != NULL) {
1309 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001310 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001311 return false;
1312 }
1313 if (!dat->Mount(true))
1314 return false;
1315
Dees_Troy2673cec2013-04-02 20:22:16 +00001316 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001317 mtp_was_enabled = TWFunc::Toggle_MTP(false);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001318 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001319 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
1320 if (mtp_was_enabled) {
1321 if (!Enable_MTP())
1322 Disable_MTP();
1323 }
1324 return false;
1325 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 if (dat->Has_Data_Media) {
1327 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001328 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1329 dat->UnMount(false);
1330 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001331 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001332 if (mtp_was_enabled) {
1333 if (!Enable_MTP())
1334 Disable_MTP();
1335 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001336 return true;
1337 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001338 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001339 return false;
1340 }
1341 return false;
1342}
1343
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001344int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1345 std::vector<TWPartition*>::iterator iter;
1346 int ret = false;
1347 bool found = false;
1348 string Local_Path = TWFunc::Get_Root_Path(Path);
1349
1350 if (Local_Path == "/tmp" || Local_Path == "/")
1351 return true;
1352
1353 // Iterate through all partitions
1354 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1355 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1356 ret = (*iter)->Repair();
1357 found = true;
1358 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1359 (*iter)->Repair();
1360 }
1361 }
1362 if (found) {
1363 return ret;
1364 } else if (Display_Error) {
1365 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1366 } else {
1367 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1368 }
1369 return false;
1370}
1371
1372int TWPartitionManager::Repair_By_Block(string Block, bool Display_Error) {
1373 TWPartition* Part = Find_Partition_By_Block(Block);
1374
1375 if (Part) {
1376 if (Part->Has_SubPartition) {
1377 std::vector<TWPartition*>::iterator subpart;
1378
1379 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1380 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1381 (*subpart)->Repair();
1382 }
1383 return Part->Repair();
1384 } else
1385 return Part->Repair();
1386 }
1387 if (Display_Error)
1388 LOGERR("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1389 else
1390 LOGINFO("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1391 return false;
1392}
1393
1394int TWPartitionManager::Repair_By_Name(string Name, bool Display_Error) {
1395 TWPartition* Part = Find_Partition_By_Name(Name);
1396
1397 if (Part) {
1398 if (Part->Has_SubPartition) {
1399 std::vector<TWPartition*>::iterator subpart;
1400
1401 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1402 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1403 (*subpart)->Repair();
1404 }
1405 return Part->Repair();
1406 } else
1407 return Part->Repair();
1408 }
1409 if (Display_Error)
1410 LOGERR("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1411 else
1412 LOGINFO("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1413 return false;
1414}
1415
Dees_Troy51a0e822012-09-05 15:24:24 -04001416void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001417 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001418 return;
1419}
1420
1421void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001422 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001423 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001424
Dees_Troy2673cec2013-04-02 20:22:16 +00001425 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001426 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001427 if ((*iter)->Can_Be_Mounted) {
1428 (*iter)->Update_Size(true);
1429 if ((*iter)->Mount_Point == "/system") {
1430 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1431 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1432 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1433 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1434 } else if ((*iter)->Mount_Point == "/cache") {
1435 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1436 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1437 } else if ((*iter)->Mount_Point == "/sd-ext") {
1438 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1439 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1440 if ((*iter)->Backup_Size == 0) {
1441 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1442 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1443 } else
1444 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001445 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001446 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001447 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001448 if ((*iter)->Backup_Size == 0) {
1449 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1450 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1451 } else
1452 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001453 } else if ((*iter)->Mount_Point == "/boot") {
1454 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1455 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1456 if ((*iter)->Backup_Size == 0) {
1457 DataManager::SetValue("tw_has_boot_partition", 0);
1458 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1459 } else
1460 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001461 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001462#ifdef SP1_NAME
1463 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1464 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1465 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1466 }
1467#endif
1468#ifdef SP2_NAME
1469 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1470 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1471 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1472 }
1473#endif
1474#ifdef SP3_NAME
1475 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1476 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1477 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1478 }
1479#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001480 } else {
1481 // Handle unmountable partitions in case we reset defaults
1482 if ((*iter)->Mount_Point == "/boot") {
1483 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1484 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1485 if ((*iter)->Backup_Size == 0) {
1486 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1487 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1488 } else
1489 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1490 } else if ((*iter)->Mount_Point == "/recovery") {
1491 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1492 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1493 if ((*iter)->Backup_Size == 0) {
1494 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1495 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1496 } else
1497 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001498 } else if ((*iter)->Mount_Point == "/data") {
1499 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001500 }
1501#ifdef SP1_NAME
1502 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1503 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1504 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1505 }
1506#endif
1507#ifdef SP2_NAME
1508 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1509 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1510 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1511 }
1512#endif
1513#ifdef SP3_NAME
1514 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1515 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1516 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1517 }
1518#endif
Dees_Troy51127312012-09-08 13:08:49 -04001519 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001520 }
Dees_Troy51127312012-09-08 13:08:49 -04001521 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1522 string current_storage_path = DataManager::GetCurrentStoragePath();
1523 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001524 if (FreeStorage != NULL) {
1525 // Attempt to mount storage
1526 if (!FreeStorage->Mount(false)) {
1527 // We couldn't mount storage... check to see if we have dual storage
1528 int has_dual_storage;
1529 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1530 if (has_dual_storage == 1) {
1531 // We have dual storage, see if we're using the internal storage that should always be present
1532 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001533 if (!FreeStorage->Is_Encrypted) {
1534 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001535 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001536 }
Dees_Troy8170a922012-09-18 15:40:25 -04001537 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1538 } else {
1539 // We were using external, flip to internal
1540 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1541 current_storage_path = DataManager::GetCurrentStoragePath();
1542 FreeStorage = Find_Partition_By_Path(current_storage_path);
1543 if (FreeStorage != NULL) {
1544 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1545 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001546 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001547 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1548 }
1549 }
1550 } else {
1551 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001552 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001553 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1554 }
1555 } else {
1556 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1557 }
1558 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001559 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001560 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001561 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001562 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001563 return;
1564}
1565
1566int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001567#ifdef TW_INCLUDE_CRYPTO
1568 int ret_val, password_len;
1569 char crypto_blkdev[255], cPassword[255];
1570 size_t result;
1571
1572 property_set("ro.crypto.state", "encrypted");
1573#ifdef TW_INCLUDE_JB_CRYPTO
1574 // No extra flags needed
1575#else
1576 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1577 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1578 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1579 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1580 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1581 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001582
1583#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001584 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1585 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1586 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001587#endif
a39552696ff55ce2013-01-08 16:14:56 +00001588
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001589 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001590
1591#endif
1592
1593 // some samsung devices store "footer" on efs partition
1594 TWPartition *efs = Find_Partition_By_Path("/efs");
1595 if(efs && !efs->Is_Mounted())
1596 efs->Mount(false);
1597 else
1598 efs = 0;
1599#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001600#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001601 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001602 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001603 property_set("ro.crypto.external_encrypted", "1");
1604 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1605 } else {
1606 property_set("ro.crypto.external_encrypted", "0");
1607 }
1608#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001609#endif
a39552696ff55ce2013-01-08 16:14:56 +00001610
Dees_Troy5bf43922012-09-07 16:07:55 -04001611 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001612 int pwret = cryptfs_check_passwd(cPassword);
1613
1614 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001615 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001616 return -1;
1617 }
a39552696ff55ce2013-01-08 16:14:56 +00001618
1619 if(efs)
1620 efs->UnMount(false);
1621
Dees_Troy5bf43922012-09-07 16:07:55 -04001622 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1623 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001624 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001625 } else {
1626 TWPartition* dat = Find_Partition_By_Path("/data");
1627 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001628 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001629 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1630 dat->Is_Decrypted = true;
1631 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001632 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001633 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 +00001634 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001635
1636#ifdef CRYPTO_SD_FS_TYPE
1637 char crypto_blkdev_sd[255];
1638 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1639 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001640 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001641 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001642 emmc->Is_Decrypted = true;
1643 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1644 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001645 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001646 }
a39552696ff55ce2013-01-08 16:14:56 +00001647#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001648#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001649#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001650 char is_external_decrypted[255];
1651 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1652 if (strcmp(is_external_decrypted, "1") == 0) {
1653 sdcard->Is_Decrypted = true;
1654 sdcard->EcryptFS_Password = Password;
1655 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001656 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1657 MetaEcfsFile += "/.MetaEcfsFile";
1658 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1659 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1660 sdcard->UnMount(false);
1661 sdcard->Mount(false);
1662 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001663 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001664 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001665 sdcard->Is_Decrypted = false;
1666 sdcard->Decrypted_Block_Device = "";
1667 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001668#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001669#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001670
Dees_Troy5bf43922012-09-07 16:07:55 -04001671 // Sleep for a bit so that the device will be ready
1672 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001673 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001674 dat->Storage_Path = "/data/media/0";
1675 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001676 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001677 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001678 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001679 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001680 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001681 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001682 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001683 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001684 }
1685 return 0;
1686#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001687 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001688 return -1;
1689#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001690 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001691}
1692
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001693int TWPartitionManager::Fix_Permissions(void) {
1694 int result = 0;
1695 if (!Mount_By_Path("/data", true))
1696 return false;
1697
1698 if (!Mount_By_Path("/system", true))
1699 return false;
1700
1701 Mount_By_Path("/sd-ext", false);
1702
1703 fixPermissions perms;
1704 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001705 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001706 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001707 return result;
1708}
1709
Ethan Yonker47360be2014-04-01 10:34:34 -05001710TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1711 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1712
1713 if (!Path.empty()) {
1714 string Search_Path = TWFunc::Get_Root_Path(Path);
1715 for (; iter != Partitions.end(); iter++) {
1716 if ((*iter)->Mount_Point == Search_Path) {
1717 iter++;
1718 break;
1719 }
1720 }
1721 }
1722
1723 for (; iter != Partitions.end(); iter++) {
1724 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1725 return (*iter);
1726 }
1727 }
1728
1729 return NULL;
1730}
1731
Dees_Troyd21618c2012-10-14 18:48:49 -04001732int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001733 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1734
1735 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001736 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001737 return false;
1738 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001739 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1740 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001741 return false;
1742
Dees_Troy2673cec2013-04-02 20:22:16 +00001743 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1744 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001745 return false;
1746 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001747 return true;
1748}
1749
Dees_Troy8170a922012-09-18 15:40:25 -04001750int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001751 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001752 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001753 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001754
Dees_Troy8170a922012-09-18 15:40:25 -04001755 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001756 string Lun_File_str = CUSTOM_LUN_FILE;
1757 size_t found = Lun_File_str.find("%");
1758 if (found != string::npos) {
1759 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1760 if (TWFunc::Path_Exists(lun_file))
1761 has_multiple_lun = true;
1762 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001763 mtp_was_enabled = TWFunc::Toggle_MTP(false);
Ethan Yonker47360be2014-04-01 10:34:34 -05001764 if (!has_multiple_lun) {
1765 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1766 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1767 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1768 TWPartition* Mount = Find_Next_Storage("", "/data");
1769 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001770 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1771 goto error_handle;
1772 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001773 } else {
1774 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001775 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001776 }
1777 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001778 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001779 }
Dees_Troy8170a922012-09-18 15:40:25 -04001780 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001781 LOGINFO("Device has multiple lun files\n");
1782 TWPartition* Mount1;
1783 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001784 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001785 Mount1 = Find_Next_Storage("", "/data");
1786 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001787 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1788 goto error_handle;
1789 }
Matt Moweree717062014-04-26 01:46:46 -05001790 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001791 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1792 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001793 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001794 }
1795 } else {
1796 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001797 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001798 }
Dees_Troy8170a922012-09-18 15:40:25 -04001799 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001800 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001801 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001802error_handle:
1803 if (mtp_was_enabled)
1804 if (!Enable_MTP())
1805 Disable_MTP();
1806 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001807}
1808
1809int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001810 int index, ret;
1811 char lun_file[255], ch[2] = {0, 0};
1812 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001813
1814 for (index=0; index<2; index++) {
1815 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001816 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001817 if (ret < 0) {
1818 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001819 }
Dees_Troy8170a922012-09-18 15:40:25 -04001820 }
Dees_Troye58d5262012-09-21 12:27:57 -04001821 Mount_All_Storage();
1822 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001823 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001824 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001825 if (mtp_was_enabled)
1826 if (!Enable_MTP())
1827 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001828 if (ret < 0 && index == 0) {
1829 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1830 return false;
1831 } else {
1832 return true;
1833 }
Dees_Troy8170a922012-09-18 15:40:25 -04001834 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001835}
1836
1837void TWPartitionManager::Mount_All_Storage(void) {
1838 std::vector<TWPartition*>::iterator iter;
1839
1840 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1841 if ((*iter)->Is_Storage)
1842 (*iter)->Mount(false);
1843 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001844}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001845
Dees_Troyd0384ef2012-10-12 12:15:42 -04001846void TWPartitionManager::UnMount_Main_Partitions(void) {
1847 // Unmounts system and data if data is not data/media
1848 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001849 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001850
1851 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1852
1853 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001854 if (!datamedia)
1855 UnMount_By_Path("/data", true);
1856
Dees_Troyd0384ef2012-10-12 12:15:42 -04001857 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1858 Boot_Partition->UnMount(true);
1859}
1860
Dees_Troy9350b8d2012-09-27 12:38:38 -04001861int TWPartitionManager::Partition_SDCard(void) {
1862 char mkdir_path[255], temp[255], line[512];
1863 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1864 int ext, swap, total_size = 0, fat_size;
1865 FILE* fp;
1866
Dees_Troy2673cec2013-04-02 20:22:16 +00001867 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001868#ifdef TW_EXTERNAL_STORAGE_PATH
1869 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1870#else
1871 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1872#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001873 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001874 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001875 return false;
1876 }
1877 if (!SDCard->UnMount(true))
1878 return false;
1879 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1880 if (SDext != NULL) {
1881 if (!SDext->UnMount(true))
1882 return false;
1883 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001884
1885 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001886 Device = SDCard->Actual_Block_Device;
1887 // Just use the root block device
1888 Device.resize(strlen("/dev/block/mmcblkX"));
1889
1890 // Find the size of the block device:
1891 fp = fopen("/proc/partitions", "rt");
1892 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001893 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001894 return false;
1895 }
1896
1897 while (fgets(line, sizeof(line), fp) != NULL)
1898 {
1899 unsigned long major, minor, blocks;
1900 char device[512];
1901 char tmpString[64];
1902
1903 if (strlen(line) < 7 || line[0] == 'm') continue;
1904 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1905
1906 tmpdevice = "/dev/block/";
1907 tmpdevice += device;
1908 if (tmpdevice == Device) {
1909 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001910 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001911 break;
1912 }
1913 }
1914 fclose(fp);
1915
1916 DataManager::GetValue("tw_sdext_size", ext);
1917 DataManager::GetValue("tw_swap_size", swap);
1918 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1919 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001920 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 -04001921 memset(temp, 0, sizeof(temp));
1922 sprintf(temp, "%i", fat_size);
1923 fat_str = temp;
1924 memset(temp, 0, sizeof(temp));
1925 sprintf(temp, "%i", fat_size + ext);
1926 ext_str = temp;
1927 memset(temp, 0, sizeof(temp));
1928 sprintf(temp, "%i", fat_size + ext + swap);
1929 swap_str = temp;
1930 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001931 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001932 return false;
1933 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001934 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001935 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001936 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001937 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001938 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001939 Update_System_Details();
1940 return false;
1941 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001942 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001943 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001944 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001945 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001946 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001947 return false;
1948 }
1949 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001950 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001951 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001952 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001953 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001954 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001955 Update_System_Details();
1956 return false;
1957 }
1958 }
1959 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001960 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001961 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001962 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001963 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001964 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001965 Update_System_Details();
1966 return false;
1967 }
1968 }
1969 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1970#ifdef TW_EXTERNAL_STORAGE_PATH
1971 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1972 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1973 memset(mkdir_path, 0, sizeof(mkdir_path));
1974 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1975#else
1976 Mount_By_Path("/sdcard", 1);
1977 strcpy(mkdir_path, "/sdcard/TWRP");
1978#endif
1979 mkdir(mkdir_path, 0777);
1980 DataManager::Flush();
1981#ifdef TW_EXTERNAL_STORAGE_PATH
1982 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1983 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1984 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1985#else
1986 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1987 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1988 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1989#endif
1990 if (ext > 0) {
1991 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001992 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001993 return false;
1994 }
1995 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001996 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1997 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001998 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001999 }
2000
2001 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00002002 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04002003 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04002004}
Dees_Troya13d74f2013-03-24 08:54:55 -05002005
2006void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
2007 std::vector<TWPartition*>::iterator iter;
2008 if (ListType == "mount") {
2009 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2010 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
2011 struct PartitionList part;
2012 part.Display_Name = (*iter)->Display_Name;
2013 part.Mount_Point = (*iter)->Mount_Point;
2014 part.selected = (*iter)->Is_Mounted();
2015 Partition_List->push_back(part);
2016 }
2017 }
2018 } else if (ListType == "storage") {
2019 char free_space[255];
2020 string Current_Storage = DataManager::GetCurrentStoragePath();
2021 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2022 if ((*iter)->Is_Storage) {
2023 struct PartitionList part;
2024 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
2025 part.Display_Name = (*iter)->Storage_Name + " (";
2026 part.Display_Name += free_space;
2027 part.Display_Name += "MB)";
2028 part.Mount_Point = (*iter)->Storage_Path;
2029 if ((*iter)->Storage_Path == Current_Storage)
2030 part.selected = 1;
2031 else
2032 part.selected = 0;
2033 Partition_List->push_back(part);
2034 }
2035 }
2036 } else if (ListType == "backup") {
2037 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002038 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05002039 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002040 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002041 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002042 Backup_Size = (*iter)->Backup_Size;
2043 if ((*iter)->Has_SubPartition) {
2044 std::vector<TWPartition*>::iterator subpart;
2045
2046 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
2047 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
2048 Backup_Size += (*subpart)->Backup_Size;
2049 }
2050 }
2051 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05002052 part.Display_Name = (*iter)->Backup_Display_Name + " (";
2053 part.Display_Name += backup_size;
2054 part.Display_Name += "MB)";
2055 part.Mount_Point = (*iter)->Backup_Path;
2056 part.selected = 0;
2057 Partition_List->push_back(part);
2058 }
2059 }
2060 } else if (ListType == "restore") {
2061 string Restore_List, restore_path;
2062 TWPartition* restore_part = NULL;
2063
2064 DataManager::GetValue("tw_restore_list", Restore_List);
2065 if (!Restore_List.empty()) {
2066 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
2067 while (end_pos != string::npos && start_pos < Restore_List.size()) {
2068 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05002069 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00002070 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002071 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05002072 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05002073 } else {
2074 struct PartitionList part;
2075 part.Display_Name = restore_part->Backup_Display_Name;
2076 part.Mount_Point = restore_part->Backup_Path;
2077 part.selected = 1;
2078 Partition_List->push_back(part);
2079 }
2080 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002081 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002082 }
2083 start_pos = end_pos + 1;
2084 end_pos = Restore_List.find(";", start_pos);
2085 }
2086 }
2087 } else if (ListType == "wipe") {
2088 struct PartitionList dalvik;
2089 dalvik.Display_Name = "Dalvik Cache";
2090 dalvik.Mount_Point = "DALVIK";
2091 dalvik.selected = 0;
2092 Partition_List->push_back(dalvik);
2093 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2094 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
2095 struct PartitionList part;
2096 part.Display_Name = (*iter)->Display_Name;
2097 part.Mount_Point = (*iter)->Mount_Point;
2098 part.selected = 0;
2099 Partition_List->push_back(part);
2100 }
2101 if ((*iter)->Has_Android_Secure) {
2102 struct PartitionList part;
2103 part.Display_Name = (*iter)->Backup_Display_Name;
2104 part.Mount_Point = (*iter)->Backup_Path;
2105 part.selected = 0;
2106 Partition_List->push_back(part);
2107 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00002108 if ((*iter)->Has_Data_Media) {
2109 struct PartitionList datamedia;
2110 datamedia.Display_Name = (*iter)->Storage_Name;
2111 datamedia.Mount_Point = "INTERNAL";
2112 datamedia.selected = 0;
2113 Partition_List->push_back(datamedia);
2114 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002115 }
2116 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002117 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002118 }
2119}
2120
2121int TWPartitionManager::Fstab_Processed(void) {
2122 return Partitions.size();
2123}
Dees_Troyd93bda52013-07-03 19:55:19 +00002124
2125void TWPartitionManager::Output_Storage_Fstab(void) {
2126 std::vector<TWPartition*>::iterator iter;
2127 char storage_partition[255];
2128 string Temp;
2129 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
2130
2131 if (fp == NULL) {
2132 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
2133 return;
2134 }
2135
2136 // Iterate through all partitions
2137 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2138 if ((*iter)->Is_Storage) {
2139 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
2140 strcpy(storage_partition, Temp.c_str());
2141 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
2142 }
2143 }
2144 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002145}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002146
2147TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
2148{
2149 TWPartition *res = NULL;
2150 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
2151 if(!(*iter)->Is_Storage)
2152 continue;
2153
2154 if((*iter)->Is_Settings_Storage)
2155 return *iter;
2156
2157 if(!res)
2158 res = *iter;
2159 }
2160 return res;
2161}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002162
2163bool TWPartitionManager::Enable_MTP(void) {
2164#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002165 if (mtppid) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002166 LOGERR("MTP already enabled\n");
2167 return true;
2168 }
2169 //Launch MTP Responder
2170 LOGINFO("Starting MTP\n");
2171 char vendor[PROPERTY_VALUE_MAX];
2172 char product[PROPERTY_VALUE_MAX];
2173 int count = 0;
2174 property_set("sys.usb.config", "none");
2175 property_get("usb.vendor", vendor, "18D1");
2176 property_get("usb.product.mtpadb", product, "4EE2");
2177 string vendorstr = vendor;
2178 string productstr = product;
2179 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2180 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2181 property_set("sys.usb.config", "mtp,adb");
2182 std::vector<TWPartition*>::iterator iter;
Ethan Yonker6d154c42014-09-03 14:19:43 -05002183 /* To enable MTP debug, use the twrp command line feature to
2184 * twrp set tw_mtp_debug 1
2185 */
2186 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
that9e0593e2014-10-08 00:01:24 +02002187 unsigned int storageid = 1 << 16; // upper 16 bits are for physical storage device, we pretend to have only one
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002188 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2189 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) {
that9e0593e2014-10-08 00:01:24 +02002190 ++storageid;
2191 printf("twrp addStorage %s, mtpstorageid: %u\n", (*iter)->Storage_Path.c_str(), storageid);
2192 mtp->addStorage((*iter)->Storage_Name, (*iter)->Storage_Path, storageid);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002193 count++;
2194 }
2195 }
2196 if (count) {
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002197 mtppid = mtp->forkserver();
2198 if (mtppid) {
2199 DataManager::SetValue("tw_mtp_enabled", 1);
2200 return true;
2201 } else {
2202 LOGERR("Failed to enable MTP\n");
2203 return false;
2204 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002205 }
2206 LOGERR("No valid storage partitions found for MTP.\n");
2207#else
2208 LOGERR("MTP support not included\n");
2209#endif
2210 DataManager::SetValue("tw_mtp_enabled", 0);
2211 return false;
2212}
2213
2214bool TWPartitionManager::Disable_MTP(void) {
2215#ifdef TW_HAS_MTP
2216 char vendor[PROPERTY_VALUE_MAX];
2217 char product[PROPERTY_VALUE_MAX];
2218 property_set("sys.usb.config", "none");
2219 property_get("usb.vendor", vendor, "18D1");
2220 property_get("usb.product.adb", product, "D002");
2221 string vendorstr = vendor;
2222 string productstr = product;
2223 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2224 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002225 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002226 LOGINFO("Disabling MTP\n");
2227 int status;
2228 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002229 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002230 // We don't care about the exit value, but this prevents a zombie process
2231 waitpid(mtppid, &status, 0);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002232 }
2233 property_set("sys.usb.config", "adb");
2234 DataManager::SetValue("tw_mtp_enabled", 0);
2235 return true;
2236#else
2237 LOGERR("MTP support not included\n");
2238 DataManager::SetValue("tw_mtp_enabled", 0);
2239 return false;
2240#endif
2241}