blob: 7215374965e51bec24d624b560bcc33cd854bb5d [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
Tom Hite5a926722014-09-15 01:31:03 +0000654 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400655 time(&start);
656
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500657 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
Tom Hite5a926722014-09-15 01:31:03 +0000658 bool md5Success = false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500659 current_size += Part->Backup_Size;
660 pos = (float)((float)(current_size) / (float)(total_size));
661 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400662 if (Part->Has_SubPartition) {
663 std::vector<TWPartition*>::iterator subpart;
664
665 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500666 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000667 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size)) {
668 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400669 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000670 }
Dees_Troy2727b992013-08-14 20:09:30 +0000671 sync();
672 sync();
Tom Hite5a926722014-09-15 01:31:03 +0000673 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName)) {
674 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400675 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000676 }
Dees_Troy093b7642012-09-21 15:59:38 -0400677 if (Part->Backup_Method == 1) {
678 *file_bytes_remaining -= (*subpart)->Backup_Size;
679 } else {
680 *img_bytes_remaining -= (*subpart)->Backup_Size;
681 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500682 current_size += Part->Backup_Size;
683 pos = (float)(current_size / total_size);
684 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400685 }
686 }
687 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400688 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400689 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000690 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400691 if (Part->Backup_Method == 1) {
692 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400693 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400694 } else {
695 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400696 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400697 }
Tom Hite5a926722014-09-15 01:31:03 +0000698
699 md5Success = Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
700 TWFunc::SetPerformanceMode(false);
701 return md5Success;
Dees_Troy43d8b002012-09-17 16:00:01 -0400702 } else {
Tom Hite5a926722014-09-15 01:31:03 +0000703 TWFunc::SetPerformanceMode(false);
Dees_Troy43d8b002012-09-17 16:00:01 -0400704 return false;
705 }
706}
707
708int TWPartitionManager::Run_Backup(void) {
709 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500710 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400711 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 -0400712 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500713 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400714 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400715 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400716 struct tm *t;
717 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500718 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400719 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500720 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400721
722 time(&total_start);
723
724 Update_System_Details();
725
726 if (!Mount_Current_Storage(true))
727 return false;
728
729 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400730 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400731 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400732 else
733 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400734
Dees_Troy43d8b002012-09-17 16:00:01 -0400735 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
736 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000737 if (Backup_Name == "(Current Date)") {
738 Backup_Name = TWFunc::Get_Current_Date();
739 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
740 TWFunc::Auto_Generate_Backup_Name();
741 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400742 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000743 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400744 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000745 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400746
Dees_Troy2673cec2013-04-02 20:22:16 +0000747 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500748 DataManager::GetValue("tw_backup_list", Backup_List);
749 if (!Backup_List.empty()) {
750 end_pos = Backup_List.find(";", start_pos);
751 while (end_pos != string::npos && start_pos < Backup_List.size()) {
752 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
753 backup_part = Find_Partition_By_Path(backup_path);
754 if (backup_part != NULL) {
755 partition_count++;
756 if (backup_part->Backup_Method == 1)
757 file_bytes += backup_part->Backup_Size;
758 else
759 img_bytes += backup_part->Backup_Size;
760 if (backup_part->Has_SubPartition) {
761 std::vector<TWPartition*>::iterator subpart;
762
763 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000764 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 -0500765 partition_count++;
766 if ((*subpart)->Backup_Method == 1)
767 file_bytes += (*subpart)->Backup_Size;
768 else
769 img_bytes += (*subpart)->Backup_Size;
770 }
771 }
Dees_Troy8170a922012-09-18 15:40:25 -0400772 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500773 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000774 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400775 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500776 start_pos = end_pos + 1;
777 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400778 }
779 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400780
781 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000782 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400783 return false;
784 }
785 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000786 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500787 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400788 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
789 if (storage != NULL) {
790 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000791 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400792 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000793 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400794 return false;
795 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000796 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400797 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000798 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400799 return false;
800 }
801 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500802 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400803
Dees_Troy2673cec2013-04-02 20:22:16 +0000804 gui_print("\n[BACKUP STARTED]\n");
805 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000806 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000807 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000808 return false;
809 }
810
Dees_Troy2673cec2013-04-02 20:22:16 +0000811 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400812
Dees_Troya13d74f2013-03-24 08:54:55 -0500813 start_pos = 0;
814 end_pos = Backup_List.find(";", start_pos);
815 while (end_pos != string::npos && start_pos < Backup_List.size()) {
816 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
817 backup_part = Find_Partition_By_Path(backup_path);
818 if (backup_part != NULL) {
819 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
820 return false;
821 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000822 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500823 }
824 start_pos = end_pos + 1;
825 end_pos = Backup_List.find(";", start_pos);
826 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400827
828 // Average BPS
829 if (img_time == 0)
830 img_time = 1;
831 if (file_time == 0)
832 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400833 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500834 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400835
Dees_Troy2673cec2013-04-02 20:22:16 +0000836 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
837 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400838
839 time(&total_stop);
840 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500841 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500842 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400843
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500844 int prev_img_bps, use_compression;
845 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400846 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
847 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500848 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400849
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500850 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400851 if (use_compression)
852 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500853 else
Dees_Troy093b7642012-09-21 15:59:38 -0400854 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
855 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500856 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400857
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500858 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400859 if (use_compression)
860 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
861 else
862 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
863
Dees_Troy2673cec2013-04-02 20:22:16 +0000864 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400865 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400866 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500867 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000868 string backup_log = Full_Backup_Path + "recovery.log";
869 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
870 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400871}
872
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500873bool 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 -0400874 time_t Start, Stop;
Tom Hite5a926722014-09-15 01:31:03 +0000875 TWFunc::SetPerformanceMode(true);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400876 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500877 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Tom Hite5a926722014-09-15 01:31:03 +0000878 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size)) {
879 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400880 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000881 }
Dees_Troy8170a922012-09-18 15:40:25 -0400882 if (Part->Has_SubPartition) {
883 std::vector<TWPartition*>::iterator subpart;
884
885 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
886 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Tom Hite5a926722014-09-15 01:31:03 +0000887 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size)) {
888 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -0400889 return false;
Tom Hite5a926722014-09-15 01:31:03 +0000890 }
Dees_Troy8170a922012-09-18 15:40:25 -0400891 }
892 }
893 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400894 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +0000895 TWFunc::SetPerformanceMode(false);
Dees_Troy2673cec2013-04-02 20:22:16 +0000896 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 -0400897 return true;
898}
899
Dees_Troy51a0e822012-09-05 15:24:24 -0400900int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400901 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500902 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400903 time_t rStart, rStop;
904 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500905 string Restore_List, restore_path;
906 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500907 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400908
Dees_Troy2673cec2013-04-02 20:22:16 +0000909 gui_print("\n[RESTORE STARTED]\n\n");
910 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400911
Dees_Troy4a2a1262012-09-18 09:33:47 -0400912 if (!Mount_Current_Storage(true))
913 return false;
914
915 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500916 if (check_md5 > 0) {
917 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
918 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000919 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500920 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000921 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500922 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500923 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500924 DataManager::GetValue("tw_restore_selected", Restore_List);
925 if (!Restore_List.empty()) {
926 end_pos = Restore_List.find(";", start_pos);
927 while (end_pos != string::npos && start_pos < Restore_List.size()) {
928 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
929 restore_part = Find_Partition_By_Path(restore_path);
930 if (restore_part != NULL) {
931 partition_count++;
932 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
933 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500934 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500935 if (restore_part->Has_SubPartition) {
936 std::vector<TWPartition*>::iterator subpart;
937
938 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
939 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400940 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500941 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500942 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500943 }
944 }
945 }
946 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500947 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500948 }
949 start_pos = end_pos + 1;
950 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400951 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400952 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400953
954 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000955 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400956 return false;
957 }
958
Dees_Troy2673cec2013-04-02 20:22:16 +0000959 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500960 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000961 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500962
Dees_Troya13d74f2013-03-24 08:54:55 -0500963 start_pos = 0;
964 if (!Restore_List.empty()) {
965 end_pos = Restore_List.find(";", start_pos);
966 while (end_pos != string::npos && start_pos < Restore_List.size()) {
967 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
968 restore_part = Find_Partition_By_Path(restore_path);
969 if (restore_part != NULL) {
970 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500971 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500972 return false;
973 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000974 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500975 }
976 start_pos = end_pos + 1;
977 end_pos = Restore_List.find(";", start_pos);
978 }
979 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400980 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400981 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400982 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400983 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500984 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500985 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400986 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400987}
988
989void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400990 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500991 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000992 bool get_date = true, check_encryption = true;
993
994 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400995
996 DIR* d;
997 d = opendir(Restore_Name.c_str());
998 if (d == NULL)
999 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001000 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001001 return;
1002 }
1003
1004 struct dirent* de;
1005 while ((de = readdir(d)) != NULL)
1006 {
1007 // Strip off three components
1008 char str[256];
1009 char* label;
1010 char* fstype = NULL;
1011 char* extn = NULL;
1012 char* ptr;
1013
1014 strcpy(str, de->d_name);
1015 if (strlen(str) <= 2)
1016 continue;
1017
1018 if (get_date) {
1019 char file_path[255];
1020 struct stat st;
1021
1022 strcpy(file_path, Restore_Name.c_str());
1023 strcat(file_path, "/");
1024 strcat(file_path, str);
1025 stat(file_path, &st);
1026 string backup_date = ctime((const time_t*)(&st.st_mtime));
1027 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1028 get_date = false;
1029 }
1030
1031 label = str;
1032 ptr = label;
1033 while (*ptr && *ptr != '.') ptr++;
1034 if (*ptr == '.')
1035 {
1036 *ptr = 0x00;
1037 ptr++;
1038 fstype = ptr;
1039 }
1040 while (*ptr && *ptr != '.') ptr++;
1041 if (*ptr == '.')
1042 {
1043 *ptr = 0x00;
1044 ptr++;
1045 extn = ptr;
1046 }
1047
Dees_Troy83bd4832013-05-04 12:39:56 +00001048 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001049 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001050 if (extnlength != 3 && extnlength != 6) continue;
1051 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1052 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1053
1054 if (check_encryption) {
1055 string filename = Restore_Name + "/";
1056 filename += de->d_name;
1057 if (TWFunc::Get_File_Type(filename) == 2) {
1058 LOGINFO("'%s' is encrypted\n", filename.c_str());
1059 DataManager::SetValue("tw_restore_encrypted", 1);
1060 }
1061 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001062 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001063
1064 TWPartition* Part = Find_Partition_By_Path(label);
1065 if (Part == NULL)
1066 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001067 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -04001068 continue;
1069 }
1070
1071 Part->Backup_FileName = de->d_name;
1072 if (strlen(extn) > 3) {
1073 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1074 }
1075
Dees_Troya13d74f2013-03-24 08:54:55 -05001076 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001077 }
1078 closedir(d);
1079
Dees_Troya13d74f2013-03-24 08:54:55 -05001080 // Set the final value
1081 DataManager::SetValue("tw_restore_list", Restore_List);
1082 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001083 return;
1084}
1085
1086int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001087 std::vector<TWPartition*>::iterator iter;
1088 int ret = false;
1089 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001090 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001091
1092 // Iterate through all partitions
1093 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001094 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001095 if (Path == "/and-sec")
1096 ret = (*iter)->Wipe_AndSec();
1097 else
1098 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001099 found = true;
1100 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1101 (*iter)->Wipe();
1102 }
1103 }
1104 if (found) {
1105 return ret;
1106 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001107 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001108 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001109}
1110
1111int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001112 TWPartition* Part = Find_Partition_By_Block(Block);
1113
1114 if (Part) {
1115 if (Part->Has_SubPartition) {
1116 std::vector<TWPartition*>::iterator subpart;
1117
1118 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1119 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1120 (*subpart)->Wipe();
1121 }
1122 return Part->Wipe();
1123 } else
1124 return Part->Wipe();
1125 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001126 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001127 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001128}
1129
1130int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001131 TWPartition* Part = Find_Partition_By_Name(Name);
1132
1133 if (Part) {
1134 if (Part->Has_SubPartition) {
1135 std::vector<TWPartition*>::iterator subpart;
1136
1137 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1138 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1139 (*subpart)->Wipe();
1140 }
1141 return Part->Wipe();
1142 } else
1143 return Part->Wipe();
1144 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001146 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001147}
1148
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001149int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1150 std::vector<TWPartition*>::iterator iter;
1151 int ret = false;
1152 bool found = false;
1153 string Local_Path = TWFunc::Get_Root_Path(Path);
1154
1155 // Iterate through all partitions
1156 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1157 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1158 if (Path == "/and-sec")
1159 ret = (*iter)->Wipe_AndSec();
1160 else
1161 ret = (*iter)->Wipe(New_File_System);
1162 found = true;
1163 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1164 (*iter)->Wipe(New_File_System);
1165 }
1166 }
1167 if (found) {
1168 return ret;
1169 } else
1170 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1171 return false;
1172}
1173
1174int TWPartitionManager::Wipe_By_Block(string Block, string New_File_System) {
1175 TWPartition* Part = Find_Partition_By_Block(Block);
1176
1177 if (Part) {
1178 if (Part->Has_SubPartition) {
1179 std::vector<TWPartition*>::iterator subpart;
1180
1181 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1182 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1183 (*subpart)->Wipe(New_File_System);
1184 }
1185 return Part->Wipe(New_File_System);
1186 } else
1187 return Part->Wipe(New_File_System);
1188 }
1189 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1190 return false;
1191}
1192
1193int TWPartitionManager::Wipe_By_Name(string Name, string New_File_System) {
1194 TWPartition* Part = Find_Partition_By_Name(Name);
1195
1196 if (Part) {
1197 if (Part->Has_SubPartition) {
1198 std::vector<TWPartition*>::iterator subpart;
1199
1200 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1201 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1202 (*subpart)->Wipe();
1203 }
1204 return Part->Wipe(New_File_System);
1205 } else
1206 return Part->Wipe(New_File_System);
1207 }
1208 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1209 return false;
1210}
1211
Dees_Troy51a0e822012-09-05 15:24:24 -04001212int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001213 std::vector<TWPartition*>::iterator iter;
1214 int ret = true;
1215
1216 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001217 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001218 if (!(*iter)->Wipe())
1219 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001220 } else if ((*iter)->Has_Android_Secure) {
1221 if (!(*iter)->Wipe_AndSec())
1222 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001223 }
1224 }
1225 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001226}
1227
Dees_Troy38bd7602012-09-14 13:33:53 -04001228int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1229 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001230 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001231
1232 if (!Mount_By_Path("/data", true))
1233 return false;
1234
1235 if (!Mount_By_Path("/cache", true))
1236 return false;
1237
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001238 dir.push_back("/data/dalvik-cache");
1239 dir.push_back("/cache/dalvik-cache");
1240 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001241 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001242 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001243 if (stat(dir.at(i).c_str(), &st) == 0) {
1244 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001245 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001246 }
1247 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001248 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001249 if (sdext && sdext->Is_Present && sdext->Mount(false))
1250 {
1251 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1252 {
1253 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001254 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001255 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001256 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001257 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001258 return true;
1259}
1260
1261int TWPartitionManager::Wipe_Rotate_Data(void) {
1262 if (!Mount_By_Path("/data", true))
1263 return false;
1264
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001265 unlink("/data/misc/akmd*");
1266 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001267 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001268 return true;
1269}
1270
1271int TWPartitionManager::Wipe_Battery_Stats(void) {
1272 struct stat st;
1273
1274 if (!Mount_By_Path("/data", true))
1275 return false;
1276
1277 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001278 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001279 } else {
1280 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001281 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001282 }
1283 return true;
1284}
1285
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001286int TWPartitionManager::Wipe_Android_Secure(void) {
1287 std::vector<TWPartition*>::iterator iter;
1288 int ret = false;
1289 bool found = false;
1290
1291 // Iterate through all partitions
1292 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1293 if ((*iter)->Has_Android_Secure) {
1294 ret = (*iter)->Wipe_AndSec();
1295 found = true;
1296 }
1297 }
1298 if (found) {
1299 return ret;
1300 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001301 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001302 }
1303 return false;
1304}
1305
Dees_Troy38bd7602012-09-14 13:33:53 -04001306int TWPartitionManager::Format_Data(void) {
1307 TWPartition* dat = Find_Partition_By_Path("/data");
1308
1309 if (dat != NULL) {
1310 if (!dat->UnMount(true))
1311 return false;
1312
1313 return dat->Wipe_Encryption();
1314 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001315 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001316 return false;
1317 }
1318 return false;
1319}
1320
1321int TWPartitionManager::Wipe_Media_From_Data(void) {
1322 TWPartition* dat = Find_Partition_By_Path("/data");
1323
1324 if (dat != NULL) {
1325 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001326 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001327 return false;
1328 }
1329 if (!dat->Mount(true))
1330 return false;
1331
Dees_Troy2673cec2013-04-02 20:22:16 +00001332 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001333 mtp_was_enabled = TWFunc::Toggle_MTP(false);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001334 TWFunc::removeDir("/data/media", false);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001335 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) {
1336 if (mtp_was_enabled) {
1337 if (!Enable_MTP())
1338 Disable_MTP();
1339 }
1340 return false;
1341 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001342 if (dat->Has_Data_Media) {
1343 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001344 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1345 dat->UnMount(false);
1346 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001347 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001348 if (mtp_was_enabled) {
1349 if (!Enable_MTP())
1350 Disable_MTP();
1351 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001352 return true;
1353 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001354 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001355 return false;
1356 }
1357 return false;
1358}
1359
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001360int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1361 std::vector<TWPartition*>::iterator iter;
1362 int ret = false;
1363 bool found = false;
1364 string Local_Path = TWFunc::Get_Root_Path(Path);
1365
1366 if (Local_Path == "/tmp" || Local_Path == "/")
1367 return true;
1368
1369 // Iterate through all partitions
1370 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1371 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1372 ret = (*iter)->Repair();
1373 found = true;
1374 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1375 (*iter)->Repair();
1376 }
1377 }
1378 if (found) {
1379 return ret;
1380 } else if (Display_Error) {
1381 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1382 } else {
1383 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1384 }
1385 return false;
1386}
1387
1388int TWPartitionManager::Repair_By_Block(string Block, bool Display_Error) {
1389 TWPartition* Part = Find_Partition_By_Block(Block);
1390
1391 if (Part) {
1392 if (Part->Has_SubPartition) {
1393 std::vector<TWPartition*>::iterator subpart;
1394
1395 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1396 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1397 (*subpart)->Repair();
1398 }
1399 return Part->Repair();
1400 } else
1401 return Part->Repair();
1402 }
1403 if (Display_Error)
1404 LOGERR("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1405 else
1406 LOGINFO("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1407 return false;
1408}
1409
1410int TWPartitionManager::Repair_By_Name(string Name, bool Display_Error) {
1411 TWPartition* Part = Find_Partition_By_Name(Name);
1412
1413 if (Part) {
1414 if (Part->Has_SubPartition) {
1415 std::vector<TWPartition*>::iterator subpart;
1416
1417 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1418 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1419 (*subpart)->Repair();
1420 }
1421 return Part->Repair();
1422 } else
1423 return Part->Repair();
1424 }
1425 if (Display_Error)
1426 LOGERR("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1427 else
1428 LOGINFO("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1429 return false;
1430}
1431
Dees_Troy51a0e822012-09-05 15:24:24 -04001432void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001433 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001434 return;
1435}
1436
1437void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001438 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001439 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001440
Dees_Troy2673cec2013-04-02 20:22:16 +00001441 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001442 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001443 if ((*iter)->Can_Be_Mounted) {
1444 (*iter)->Update_Size(true);
1445 if ((*iter)->Mount_Point == "/system") {
1446 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1447 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1448 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1449 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1450 } else if ((*iter)->Mount_Point == "/cache") {
1451 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1452 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1453 } else if ((*iter)->Mount_Point == "/sd-ext") {
1454 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1455 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1456 if ((*iter)->Backup_Size == 0) {
1457 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1458 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1459 } else
1460 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001461 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001462 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001463 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001464 if ((*iter)->Backup_Size == 0) {
1465 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1466 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1467 } else
1468 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001469 } else if ((*iter)->Mount_Point == "/boot") {
1470 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1471 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1472 if ((*iter)->Backup_Size == 0) {
1473 DataManager::SetValue("tw_has_boot_partition", 0);
1474 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1475 } else
1476 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001477 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001478#ifdef SP1_NAME
1479 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1480 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1481 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1482 }
1483#endif
1484#ifdef SP2_NAME
1485 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1486 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1487 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1488 }
1489#endif
1490#ifdef SP3_NAME
1491 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1492 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1493 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1494 }
1495#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001496 } else {
1497 // Handle unmountable partitions in case we reset defaults
1498 if ((*iter)->Mount_Point == "/boot") {
1499 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1500 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1501 if ((*iter)->Backup_Size == 0) {
1502 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1503 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1504 } else
1505 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1506 } else if ((*iter)->Mount_Point == "/recovery") {
1507 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1508 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1509 if ((*iter)->Backup_Size == 0) {
1510 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1511 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1512 } else
1513 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001514 } else if ((*iter)->Mount_Point == "/data") {
1515 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001516 }
1517#ifdef SP1_NAME
1518 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1519 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1520 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1521 }
1522#endif
1523#ifdef SP2_NAME
1524 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1525 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1526 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1527 }
1528#endif
1529#ifdef SP3_NAME
1530 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1531 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1532 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1533 }
1534#endif
Dees_Troy51127312012-09-08 13:08:49 -04001535 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001536 }
Dees_Troy51127312012-09-08 13:08:49 -04001537 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1538 string current_storage_path = DataManager::GetCurrentStoragePath();
1539 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001540 if (FreeStorage != NULL) {
1541 // Attempt to mount storage
1542 if (!FreeStorage->Mount(false)) {
1543 // We couldn't mount storage... check to see if we have dual storage
1544 int has_dual_storage;
1545 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1546 if (has_dual_storage == 1) {
1547 // We have dual storage, see if we're using the internal storage that should always be present
1548 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001549 if (!FreeStorage->Is_Encrypted) {
1550 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001551 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001552 }
Dees_Troy8170a922012-09-18 15:40:25 -04001553 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1554 } else {
1555 // We were using external, flip to internal
1556 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1557 current_storage_path = DataManager::GetCurrentStoragePath();
1558 FreeStorage = Find_Partition_By_Path(current_storage_path);
1559 if (FreeStorage != NULL) {
1560 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1561 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001562 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001563 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1564 }
1565 }
1566 } else {
1567 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001568 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001569 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1570 }
1571 } else {
1572 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1573 }
1574 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001575 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001576 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001577 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001578 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001579 return;
1580}
1581
1582int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001583#ifdef TW_INCLUDE_CRYPTO
1584 int ret_val, password_len;
1585 char crypto_blkdev[255], cPassword[255];
1586 size_t result;
1587
1588 property_set("ro.crypto.state", "encrypted");
1589#ifdef TW_INCLUDE_JB_CRYPTO
1590 // No extra flags needed
1591#else
1592 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1593 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1594 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1595 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1596 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1597 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001598
1599#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001600 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1601 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1602 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001603#endif
a39552696ff55ce2013-01-08 16:14:56 +00001604
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001605 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001606
1607#endif
1608
1609 // some samsung devices store "footer" on efs partition
1610 TWPartition *efs = Find_Partition_By_Path("/efs");
1611 if(efs && !efs->Is_Mounted())
1612 efs->Mount(false);
1613 else
1614 efs = 0;
1615#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001616#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001617 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001618 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001619 property_set("ro.crypto.external_encrypted", "1");
1620 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1621 } else {
1622 property_set("ro.crypto.external_encrypted", "0");
1623 }
1624#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001625#endif
a39552696ff55ce2013-01-08 16:14:56 +00001626
Dees_Troy5bf43922012-09-07 16:07:55 -04001627 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001628 int pwret = cryptfs_check_passwd(cPassword);
1629
1630 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001631 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001632 return -1;
1633 }
a39552696ff55ce2013-01-08 16:14:56 +00001634
1635 if(efs)
1636 efs->UnMount(false);
1637
Dees_Troy5bf43922012-09-07 16:07:55 -04001638 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1639 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001640 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001641 } else {
1642 TWPartition* dat = Find_Partition_By_Path("/data");
1643 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001644 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001645 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1646 dat->Is_Decrypted = true;
1647 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001648 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001649 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 +00001650 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001651
1652#ifdef CRYPTO_SD_FS_TYPE
1653 char crypto_blkdev_sd[255];
1654 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1655 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001656 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001657 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001658 emmc->Is_Decrypted = true;
1659 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1660 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001661 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001662 }
a39552696ff55ce2013-01-08 16:14:56 +00001663#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001664#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001665#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001666 char is_external_decrypted[255];
1667 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1668 if (strcmp(is_external_decrypted, "1") == 0) {
1669 sdcard->Is_Decrypted = true;
1670 sdcard->EcryptFS_Password = Password;
1671 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001672 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1673 MetaEcfsFile += "/.MetaEcfsFile";
1674 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1675 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1676 sdcard->UnMount(false);
1677 sdcard->Mount(false);
1678 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001679 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001680 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001681 sdcard->Is_Decrypted = false;
1682 sdcard->Decrypted_Block_Device = "";
1683 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001684#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001685#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001686
Dees_Troy5bf43922012-09-07 16:07:55 -04001687 // Sleep for a bit so that the device will be ready
1688 sleep(1);
Ethan Yonker6277c792014-09-15 14:54:30 -05001689 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
Dees_Troy16b74352012-11-14 22:27:31 +00001690 dat->Storage_Path = "/data/media/0";
1691 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001692 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001693 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001694 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001695 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001696 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001697 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001698 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001700 }
1701 return 0;
1702#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001703 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001704 return -1;
1705#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001706 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001707}
1708
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001709int TWPartitionManager::Fix_Permissions(void) {
1710 int result = 0;
1711 if (!Mount_By_Path("/data", true))
1712 return false;
1713
1714 if (!Mount_By_Path("/system", true))
1715 return false;
1716
1717 Mount_By_Path("/sd-ext", false);
1718
1719 fixPermissions perms;
1720 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001721 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001722 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001723 return result;
1724}
1725
Ethan Yonker47360be2014-04-01 10:34:34 -05001726TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1727 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1728
1729 if (!Path.empty()) {
1730 string Search_Path = TWFunc::Get_Root_Path(Path);
1731 for (; iter != Partitions.end(); iter++) {
1732 if ((*iter)->Mount_Point == Search_Path) {
1733 iter++;
1734 break;
1735 }
1736 }
1737 }
1738
1739 for (; iter != Partitions.end(); iter++) {
1740 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1741 return (*iter);
1742 }
1743 }
1744
1745 return NULL;
1746}
1747
Dees_Troyd21618c2012-10-14 18:48:49 -04001748int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001749 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1750
1751 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001752 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001753 return false;
1754 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001755 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1756 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001757 return false;
1758
Dees_Troy2673cec2013-04-02 20:22:16 +00001759 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1760 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001761 return false;
1762 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001763 return true;
1764}
1765
Dees_Troy8170a922012-09-18 15:40:25 -04001766int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001767 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001768 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001769 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001770
Dees_Troy8170a922012-09-18 15:40:25 -04001771 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001772 string Lun_File_str = CUSTOM_LUN_FILE;
1773 size_t found = Lun_File_str.find("%");
1774 if (found != string::npos) {
1775 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1776 if (TWFunc::Path_Exists(lun_file))
1777 has_multiple_lun = true;
1778 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001779 mtp_was_enabled = TWFunc::Toggle_MTP(false);
Ethan Yonker47360be2014-04-01 10:34:34 -05001780 if (!has_multiple_lun) {
1781 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1782 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1783 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1784 TWPartition* Mount = Find_Next_Storage("", "/data");
1785 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001786 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1787 goto error_handle;
1788 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001789 } else {
1790 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001791 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001792 }
1793 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001794 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001795 }
Dees_Troy8170a922012-09-18 15:40:25 -04001796 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001797 LOGINFO("Device has multiple lun files\n");
1798 TWPartition* Mount1;
1799 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001800 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001801 Mount1 = Find_Next_Storage("", "/data");
1802 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001803 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1804 goto error_handle;
1805 }
Matt Moweree717062014-04-26 01:46:46 -05001806 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001807 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1808 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001809 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001810 }
1811 } else {
1812 LOGERR("Unable to find storage partition to mount to USB\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001813 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001814 }
Dees_Troy8170a922012-09-18 15:40:25 -04001815 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001816 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001817 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001818error_handle:
1819 if (mtp_was_enabled)
1820 if (!Enable_MTP())
1821 Disable_MTP();
1822 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001823}
1824
1825int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001826 int index, ret;
1827 char lun_file[255], ch[2] = {0, 0};
1828 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001829
1830 for (index=0; index<2; index++) {
1831 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001832 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001833 if (ret < 0) {
1834 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001835 }
Dees_Troy8170a922012-09-18 15:40:25 -04001836 }
Dees_Troye58d5262012-09-21 12:27:57 -04001837 Mount_All_Storage();
1838 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001839 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001840 property_set("sys.storage.ums_enabled", "0");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001841 if (mtp_was_enabled)
1842 if (!Enable_MTP())
1843 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001844 if (ret < 0 && index == 0) {
1845 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1846 return false;
1847 } else {
1848 return true;
1849 }
Dees_Troy8170a922012-09-18 15:40:25 -04001850 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001851}
1852
1853void TWPartitionManager::Mount_All_Storage(void) {
1854 std::vector<TWPartition*>::iterator iter;
1855
1856 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1857 if ((*iter)->Is_Storage)
1858 (*iter)->Mount(false);
1859 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001860}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001861
Dees_Troyd0384ef2012-10-12 12:15:42 -04001862void TWPartitionManager::UnMount_Main_Partitions(void) {
1863 // Unmounts system and data if data is not data/media
1864 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001865 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001866
1867 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1868
1869 UnMount_By_Path("/system", true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001870 if (!datamedia)
1871 UnMount_By_Path("/data", true);
1872
Dees_Troyd0384ef2012-10-12 12:15:42 -04001873 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1874 Boot_Partition->UnMount(true);
1875}
1876
Dees_Troy9350b8d2012-09-27 12:38:38 -04001877int TWPartitionManager::Partition_SDCard(void) {
1878 char mkdir_path[255], temp[255], line[512];
1879 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1880 int ext, swap, total_size = 0, fat_size;
1881 FILE* fp;
1882
Dees_Troy2673cec2013-04-02 20:22:16 +00001883 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001884#ifdef TW_EXTERNAL_STORAGE_PATH
1885 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1886#else
1887 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1888#endif
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05001889 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001890 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001891 return false;
1892 }
1893 if (!SDCard->UnMount(true))
1894 return false;
1895 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1896 if (SDext != NULL) {
1897 if (!SDext->UnMount(true))
1898 return false;
1899 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001900
1901 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001902 Device = SDCard->Actual_Block_Device;
1903 // Just use the root block device
1904 Device.resize(strlen("/dev/block/mmcblkX"));
1905
1906 // Find the size of the block device:
1907 fp = fopen("/proc/partitions", "rt");
1908 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001909 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001910 return false;
1911 }
1912
1913 while (fgets(line, sizeof(line), fp) != NULL)
1914 {
1915 unsigned long major, minor, blocks;
1916 char device[512];
1917 char tmpString[64];
1918
1919 if (strlen(line) < 7 || line[0] == 'm') continue;
1920 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1921
1922 tmpdevice = "/dev/block/";
1923 tmpdevice += device;
1924 if (tmpdevice == Device) {
1925 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001926 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001927 break;
1928 }
1929 }
1930 fclose(fp);
1931
1932 DataManager::GetValue("tw_sdext_size", ext);
1933 DataManager::GetValue("tw_swap_size", swap);
1934 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1935 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001936 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 -04001937 memset(temp, 0, sizeof(temp));
1938 sprintf(temp, "%i", fat_size);
1939 fat_str = temp;
1940 memset(temp, 0, sizeof(temp));
1941 sprintf(temp, "%i", fat_size + ext);
1942 ext_str = temp;
1943 memset(temp, 0, sizeof(temp));
1944 sprintf(temp, "%i", fat_size + ext + swap);
1945 swap_str = temp;
1946 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001947 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001948 return false;
1949 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001950 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001951 Command = "parted -s " + Device + " mklabel msdos";
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 remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001955 Update_System_Details();
1956 return false;
1957 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001958 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001959 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001960 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001961 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001962 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001963 return false;
1964 }
1965 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001966 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001967 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001968 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001969 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001970 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001971 Update_System_Details();
1972 return false;
1973 }
1974 }
1975 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001976 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001977 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001978 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001979 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001980 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001981 Update_System_Details();
1982 return false;
1983 }
1984 }
1985 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1986#ifdef TW_EXTERNAL_STORAGE_PATH
1987 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1988 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1989 memset(mkdir_path, 0, sizeof(mkdir_path));
1990 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1991#else
1992 Mount_By_Path("/sdcard", 1);
1993 strcpy(mkdir_path, "/sdcard/TWRP");
1994#endif
1995 mkdir(mkdir_path, 0777);
1996 DataManager::Flush();
1997#ifdef TW_EXTERNAL_STORAGE_PATH
1998 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1999 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
2000 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
2001#else
2002 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
2003 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
2004 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
2005#endif
2006 if (ext > 0) {
2007 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00002008 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04002009 return false;
2010 }
2011 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002012 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
2013 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002014 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04002015 }
2016
2017 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00002018 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04002019 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04002020}
Dees_Troya13d74f2013-03-24 08:54:55 -05002021
2022void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
2023 std::vector<TWPartition*>::iterator iter;
2024 if (ListType == "mount") {
2025 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2026 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
2027 struct PartitionList part;
2028 part.Display_Name = (*iter)->Display_Name;
2029 part.Mount_Point = (*iter)->Mount_Point;
2030 part.selected = (*iter)->Is_Mounted();
2031 Partition_List->push_back(part);
2032 }
2033 }
2034 } else if (ListType == "storage") {
2035 char free_space[255];
2036 string Current_Storage = DataManager::GetCurrentStoragePath();
2037 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2038 if ((*iter)->Is_Storage) {
2039 struct PartitionList part;
2040 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
2041 part.Display_Name = (*iter)->Storage_Name + " (";
2042 part.Display_Name += free_space;
2043 part.Display_Name += "MB)";
2044 part.Mount_Point = (*iter)->Storage_Path;
2045 if ((*iter)->Storage_Path == Current_Storage)
2046 part.selected = 1;
2047 else
2048 part.selected = 0;
2049 Partition_List->push_back(part);
2050 }
2051 }
2052 } else if (ListType == "backup") {
2053 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002054 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05002055 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002056 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002057 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002058 Backup_Size = (*iter)->Backup_Size;
2059 if ((*iter)->Has_SubPartition) {
2060 std::vector<TWPartition*>::iterator subpart;
2061
2062 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
2063 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
2064 Backup_Size += (*subpart)->Backup_Size;
2065 }
2066 }
2067 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05002068 part.Display_Name = (*iter)->Backup_Display_Name + " (";
2069 part.Display_Name += backup_size;
2070 part.Display_Name += "MB)";
2071 part.Mount_Point = (*iter)->Backup_Path;
2072 part.selected = 0;
2073 Partition_List->push_back(part);
2074 }
2075 }
2076 } else if (ListType == "restore") {
2077 string Restore_List, restore_path;
2078 TWPartition* restore_part = NULL;
2079
2080 DataManager::GetValue("tw_restore_list", Restore_List);
2081 if (!Restore_List.empty()) {
2082 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
2083 while (end_pos != string::npos && start_pos < Restore_List.size()) {
2084 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05002085 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00002086 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002087 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05002088 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05002089 } else {
2090 struct PartitionList part;
2091 part.Display_Name = restore_part->Backup_Display_Name;
2092 part.Mount_Point = restore_part->Backup_Path;
2093 part.selected = 1;
2094 Partition_List->push_back(part);
2095 }
2096 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002097 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002098 }
2099 start_pos = end_pos + 1;
2100 end_pos = Restore_List.find(";", start_pos);
2101 }
2102 }
2103 } else if (ListType == "wipe") {
2104 struct PartitionList dalvik;
2105 dalvik.Display_Name = "Dalvik Cache";
2106 dalvik.Mount_Point = "DALVIK";
2107 dalvik.selected = 0;
2108 Partition_List->push_back(dalvik);
2109 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2110 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
2111 struct PartitionList part;
2112 part.Display_Name = (*iter)->Display_Name;
2113 part.Mount_Point = (*iter)->Mount_Point;
2114 part.selected = 0;
2115 Partition_List->push_back(part);
2116 }
2117 if ((*iter)->Has_Android_Secure) {
2118 struct PartitionList part;
2119 part.Display_Name = (*iter)->Backup_Display_Name;
2120 part.Mount_Point = (*iter)->Backup_Path;
2121 part.selected = 0;
2122 Partition_List->push_back(part);
2123 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00002124 if ((*iter)->Has_Data_Media) {
2125 struct PartitionList datamedia;
2126 datamedia.Display_Name = (*iter)->Storage_Name;
2127 datamedia.Mount_Point = "INTERNAL";
2128 datamedia.selected = 0;
2129 Partition_List->push_back(datamedia);
2130 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002131 }
2132 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002133 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002134 }
2135}
2136
2137int TWPartitionManager::Fstab_Processed(void) {
2138 return Partitions.size();
2139}
Dees_Troyd93bda52013-07-03 19:55:19 +00002140
2141void TWPartitionManager::Output_Storage_Fstab(void) {
2142 std::vector<TWPartition*>::iterator iter;
2143 char storage_partition[255];
2144 string Temp;
2145 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
2146
2147 if (fp == NULL) {
2148 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
2149 return;
2150 }
2151
2152 // Iterate through all partitions
2153 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2154 if ((*iter)->Is_Storage) {
2155 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
2156 strcpy(storage_partition, Temp.c_str());
2157 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
2158 }
2159 }
2160 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002161}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002162
2163TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
2164{
2165 TWPartition *res = NULL;
2166 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
2167 if(!(*iter)->Is_Storage)
2168 continue;
2169
2170 if((*iter)->Is_Settings_Storage)
2171 return *iter;
2172
2173 if(!res)
2174 res = *iter;
2175 }
2176 return res;
2177}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002178
2179bool TWPartitionManager::Enable_MTP(void) {
2180#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002181 if (mtppid) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002182 LOGERR("MTP already enabled\n");
2183 return true;
2184 }
2185 //Launch MTP Responder
2186 LOGINFO("Starting MTP\n");
2187 char vendor[PROPERTY_VALUE_MAX];
2188 char product[PROPERTY_VALUE_MAX];
2189 int count = 0;
2190 property_set("sys.usb.config", "none");
2191 property_get("usb.vendor", vendor, "18D1");
2192 property_get("usb.product.mtpadb", product, "4EE2");
2193 string vendorstr = vendor;
2194 string productstr = product;
2195 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2196 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
2197 property_set("sys.usb.config", "mtp,adb");
2198 std::vector<TWPartition*>::iterator iter;
Ethan Yonker6d154c42014-09-03 14:19:43 -05002199 /* To enable MTP debug, use the twrp command line feature to
2200 * twrp set tw_mtp_debug 1
2201 */
2202 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
that9e0593e2014-10-08 00:01:24 +02002203 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 -04002204 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2205 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false)) {
that9e0593e2014-10-08 00:01:24 +02002206 ++storageid;
2207 printf("twrp addStorage %s, mtpstorageid: %u\n", (*iter)->Storage_Path.c_str(), storageid);
2208 mtp->addStorage((*iter)->Storage_Name, (*iter)->Storage_Path, storageid);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002209 count++;
2210 }
2211 }
2212 if (count) {
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002213 mtppid = mtp->forkserver();
2214 if (mtppid) {
2215 DataManager::SetValue("tw_mtp_enabled", 1);
2216 return true;
2217 } else {
2218 LOGERR("Failed to enable MTP\n");
2219 return false;
2220 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002221 }
2222 LOGERR("No valid storage partitions found for MTP.\n");
2223#else
2224 LOGERR("MTP support not included\n");
2225#endif
2226 DataManager::SetValue("tw_mtp_enabled", 0);
2227 return false;
2228}
2229
2230bool TWPartitionManager::Disable_MTP(void) {
2231#ifdef TW_HAS_MTP
2232 char vendor[PROPERTY_VALUE_MAX];
2233 char product[PROPERTY_VALUE_MAX];
2234 property_set("sys.usb.config", "none");
2235 property_get("usb.vendor", vendor, "18D1");
2236 property_get("usb.product.adb", product, "D002");
2237 string vendorstr = vendor;
2238 string productstr = product;
2239 TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2240 TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002241 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002242 LOGINFO("Disabling MTP\n");
2243 int status;
2244 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002245 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002246 // We don't care about the exit value, but this prevents a zombie process
2247 waitpid(mtppid, &status, 0);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002248 }
2249 property_set("sys.usb.config", "adb");
2250 DataManager::SetValue("tw_mtp_enabled", 0);
2251 return true;
2252#else
2253 LOGERR("MTP support not included\n");
2254 DataManager::SetValue("tw_mtp_enabled", 0);
2255 return false;
2256#endif
2257}