blob: de727f4a269a30e1929dd0412d510d9845fb5822 [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>
Dees_Troy51a0e822012-09-05 15:24:24 -040032#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000033#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040034#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040035#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040036#include "twrp-functions.hpp"
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -040037#include "fixPermissions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050038#include "twrpDigest.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050039#include "twrpDU.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040
Dees Troy6f6441d2014-01-23 02:07:03 +000041extern "C" {
42 #include "cutils/properties.h"
43}
44
Dees_Troy5bf43922012-09-07 16:07:55 -040045#ifdef TW_INCLUDE_CRYPTO
46 #ifdef TW_INCLUDE_JB_CRYPTO
47 #include "crypto/jb/cryptfs.h"
48 #else
49 #include "crypto/ics/cryptfs.h"
50 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -040051#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040052
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050053TWPartitionManager::TWPartitionManager(void) {
54}
55
Dees_Troy51a0e822012-09-05 15:24:24 -040056int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040057 FILE *fstabFile;
58 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +000059 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -050060 TWPartition* andsec_partition = NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -040061
62 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
63 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000064 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040065 return false;
66 }
67
68 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
69 if (fstab_line[0] != '/')
70 continue;
71
Dees_Troy2a923582012-09-20 12:13:34 -040072 if (fstab_line[strlen(fstab_line) - 1] != '\n')
73 fstab_line[strlen(fstab_line)] = '\n';
74
Dees_Troy5bf43922012-09-07 16:07:55 -040075 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040076 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040077 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040078
Dees_Troy5bf43922012-09-07 16:07:55 -040079 if (partition->Process_Fstab_Line(line, Display_Error)) {
Matt Mowered71fa32014-04-16 13:21:47 -050080 if (!settings_partition && partition->Is_Settings_Storage && partition->Is_Present) {
Dees Troy02a64532014-03-19 15:23:32 +000081 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050082 } else {
83 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050084 }
Matt Mowered71fa32014-04-16 13:21:47 -050085 if (!andsec_partition && partition->Has_Android_Secure && partition->Is_Present) {
Matt Mowerbf4efa32014-04-14 23:25:26 -050086 andsec_partition = partition;
87 } else {
88 partition->Has_Android_Secure = false;
89 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -050090 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -040091 } else {
92 delete partition;
93 }
94 }
95 fclose(fstabFile);
Dees Troy02a64532014-03-19 15:23:32 +000096 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -050097 std::vector<TWPartition*>::iterator iter;
98 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
99 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000100 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500101 break;
102 }
103 }
Dees Troy02a64532014-03-19 15:23:32 +0000104 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000105 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500106 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400107 if (!Write_Fstab()) {
108 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000109 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400110 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000111 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400112 }
Matt Mowered71fa32014-04-16 13:21:47 -0500113
Matt Mowerbf4efa32014-04-14 23:25:26 -0500114 if (andsec_partition) {
115 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500116 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500117 Setup_Android_Secure_Location(settings_partition);
118 }
Matt Mowered71fa32014-04-16 13:21:47 -0500119 if (settings_partition) {
120 Setup_Settings_Storage_Partition(settings_partition);
121 }
Dees_Troy51127312012-09-08 13:08:49 -0400122 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400123 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -0400124 return true;
125}
126
127int TWPartitionManager::Write_Fstab(void) {
128 FILE *fp;
129 std::vector<TWPartition*>::iterator iter;
130 string Line;
131
132 fp = fopen("/etc/fstab", "w");
133 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000134 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400135 return false;
136 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400137 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400138 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400139 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400140 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000141 }
142 // Handle subpartition tracking
143 if ((*iter)->Is_SubPartition) {
144 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
145 if (ParentPartition)
146 ParentPartition->Has_SubPartition = true;
147 else
148 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 -0400149 }
150 }
151 fclose(fp);
152 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153}
154
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500155void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500156 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
157 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
158 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
159}
160
Matt Mowerbf4efa32014-04-14 23:25:26 -0500161void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
162 if (Part->Has_Android_Secure)
163 Part->Setup_AndSec();
164#ifndef RECOVERY_SDCARD_ON_DATA
165 else
166 Part->Setup_AndSec();
167#endif
168}
169
Dees_Troy8170a922012-09-18 15:40:25 -0400170void TWPartitionManager::Output_Partition_Logging(void) {
171 std::vector<TWPartition*>::iterator iter;
172
173 printf("\n\nPartition Logs:\n");
174 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
175 Output_Partition((*iter));
176}
177
178void TWPartitionManager::Output_Partition(TWPartition* Part) {
179 unsigned long long mb = 1048576;
180
Gary Peck004d48b2012-11-21 16:28:18 -0800181 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 -0400182 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800183 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 -0400184 }
Gary Peck004d48b2012-11-21 16:28:18 -0800185 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500186 if (Part->Can_Be_Mounted)
187 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800188 if (Part->Can_Be_Wiped)
189 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700190 if (Part->Use_Rm_Rf)
191 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500192 if (Part->Can_Be_Backed_Up)
193 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800194 if (Part->Wipe_During_Factory_Reset)
195 printf("Wipe_During_Factory_Reset ");
196 if (Part->Wipe_Available_in_GUI)
197 printf("Wipe_Available_in_GUI ");
198 if (Part->Is_SubPartition)
199 printf("Is_SubPartition ");
200 if (Part->Has_SubPartition)
201 printf("Has_SubPartition ");
202 if (Part->Removable)
203 printf("Removable ");
204 if (Part->Is_Present)
205 printf("IsPresent ");
206 if (Part->Can_Be_Encrypted)
207 printf("Can_Be_Encrypted ");
208 if (Part->Is_Encrypted)
209 printf("Is_Encrypted ");
210 if (Part->Is_Decrypted)
211 printf("Is_Decrypted ");
212 if (Part->Has_Data_Media)
213 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000214 if (Part->Can_Encrypt_Backup)
215 printf("Can_Encrypt_Backup ");
216 if (Part->Use_Userdata_Encryption)
217 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800218 if (Part->Has_Android_Secure)
219 printf("Has_Android_Secure ");
220 if (Part->Is_Storage)
221 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500222 if (Part->Is_Settings_Storage)
223 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000224 if (Part->Ignore_Blkid)
225 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000226 if (Part->Retain_Layout_Version)
227 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800228 printf("\n");
229 if (!Part->SubPartition_Of.empty())
230 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
231 if (!Part->Symlink_Path.empty())
232 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
233 if (!Part->Symlink_Mount_Point.empty())
234 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
235 if (!Part->Primary_Block_Device.empty())
236 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
237 if (!Part->Alternate_Block_Device.empty())
238 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
239 if (!Part->Decrypted_Block_Device.empty())
240 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
241 if (Part->Length != 0)
242 printf(" Length: %i\n", Part->Length);
243 if (!Part->Display_Name.empty())
244 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500245 if (!Part->Storage_Name.empty())
246 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800247 if (!Part->Backup_Path.empty())
248 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
249 if (!Part->Backup_Name.empty())
250 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500251 if (!Part->Backup_Display_Name.empty())
252 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800253 if (!Part->Backup_FileName.empty())
254 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
255 if (!Part->Storage_Path.empty())
256 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
257 if (!Part->Current_File_System.empty())
258 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
259 if (!Part->Fstab_File_System.empty())
260 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
261 if (Part->Format_Block_Size != 0)
262 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400263 if (!Part->MTD_Name.empty())
264 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400265 string back_meth = Part->Backup_Method_By_Name();
266 printf(" Backup_Method: %s\n\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800267 if (Part->Mount_Flags || !Part->Mount_Options.empty())
268 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400269}
270
Dees_Troy51a0e822012-09-05 15:24:24 -0400271int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400272 std::vector<TWPartition*>::iterator iter;
273 int ret = false;
274 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400275 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400276
Dees_Troyd93bda52013-07-03 19:55:19 +0000277 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400278 return true;
279
Dees_Troy5bf43922012-09-07 16:07:55 -0400280 // Iterate through all partitions
281 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400282 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400283 ret = (*iter)->Mount(Display_Error);
284 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400285 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400286 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400287 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400288 }
289 if (found) {
290 return ret;
291 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000292 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000294 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400295 }
296 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400297}
298
299int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400300 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400301
Dees_Troy51127312012-09-08 13:08:49 -0400302 if (Part) {
303 if (Part->Has_SubPartition) {
304 std::vector<TWPartition*>::iterator subpart;
305
306 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
307 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
308 (*subpart)->Mount(Display_Error);
309 }
310 return Part->Mount(Display_Error);
311 } else
312 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400313 }
314 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000315 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400316 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000317 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400318 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400319}
320
321int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400322 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400323
Dees_Troy51127312012-09-08 13:08:49 -0400324 if (Part) {
325 if (Part->Has_SubPartition) {
326 std::vector<TWPartition*>::iterator subpart;
327
328 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
329 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
330 (*subpart)->Mount(Display_Error);
331 }
332 return Part->Mount(Display_Error);
333 } else
334 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400335 }
336 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000337 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400338 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000339 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400340 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400341}
342
343int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400344 std::vector<TWPartition*>::iterator iter;
345 int ret = false;
346 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400347 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400348
349 // Iterate through all partitions
350 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400351 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400352 ret = (*iter)->UnMount(Display_Error);
353 found = true;
354 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
355 (*iter)->UnMount(Display_Error);
356 }
357 }
358 if (found) {
359 return ret;
360 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000361 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400362 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000363 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400364 }
365 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400366}
367
368int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400369 TWPartition* Part = Find_Partition_By_Block(Block);
370
371 if (Part) {
372 if (Part->Has_SubPartition) {
373 std::vector<TWPartition*>::iterator subpart;
374
375 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
376 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
377 (*subpart)->UnMount(Display_Error);
378 }
379 return Part->UnMount(Display_Error);
380 } else
381 return Part->UnMount(Display_Error);
382 }
383 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000384 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400385 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000386 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400387 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400388}
389
390int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400391 TWPartition* Part = Find_Partition_By_Name(Name);
392
393 if (Part) {
394 if (Part->Has_SubPartition) {
395 std::vector<TWPartition*>::iterator subpart;
396
397 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
398 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
399 (*subpart)->UnMount(Display_Error);
400 }
401 return Part->UnMount(Display_Error);
402 } else
403 return Part->UnMount(Display_Error);
404 }
405 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000406 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400407 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000408 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400409 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400410}
411
412int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400413 TWPartition* Part = Find_Partition_By_Path(Path);
414
415 if (Part)
416 return Part->Is_Mounted();
417 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000418 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400419 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400420}
421
422int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400423 TWPartition* Part = Find_Partition_By_Block(Block);
424
425 if (Part)
426 return Part->Is_Mounted();
427 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000428 LOGINFO("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400429 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400430}
431
432int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400433 TWPartition* Part = Find_Partition_By_Name(Name);
434
435 if (Part)
436 return Part->Is_Mounted();
437 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000438 LOGINFO("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400439 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400440}
441
Dees_Troy5bf43922012-09-07 16:07:55 -0400442int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400443 string current_storage_path = DataManager::GetCurrentStoragePath();
444
445 if (Mount_By_Path(current_storage_path, Display_Error)) {
446 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
447 if (FreeStorage)
448 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
449 return true;
450 }
451 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400452}
453
Dees_Troy5bf43922012-09-07 16:07:55 -0400454int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
455 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
456}
457
458TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
459 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400460 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400461
462 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400463 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400464 return (*iter);
465 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400466 return NULL;
467}
468
Dees_Troy5bf43922012-09-07 16:07:55 -0400469TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400470 std::vector<TWPartition*>::iterator iter;
471
472 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400473 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 -0400474 return (*iter);
475 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400477}
478
479TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400480 std::vector<TWPartition*>::iterator iter;
481
482 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
483 if ((*iter)->Display_Name == Name)
484 return (*iter);
485 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400486 return NULL;
487}
Dees_Troy51a0e822012-09-05 15:24:24 -0400488
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400489int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
490 // Check the backup name to ensure that it is the correct size and contains only valid characters
491 // and that a backup with that name doesn't already exist
492 char backup_name[MAX_BACKUP_NAME_LEN];
493 char backup_loc[255], tw_image_dir[255];
494 int copy_size;
495 int index, cur_char;
496 string Backup_Name, Backup_Loc;
497
498 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
499 copy_size = Backup_Name.size();
500 // Check size
501 if (copy_size > MAX_BACKUP_NAME_LEN) {
502 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000503 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400504 return -2;
505 }
506
507 // Check each character
508 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500509 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400510 return 0; // A "0" (zero) means to use the current timestamp for the backup name
511 for (index=0; index<copy_size; index++) {
512 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500513 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 -0400514 // These are valid characters
515 // Numbers
516 // Upper case letters
517 // Lower case letters
518 // Space
519 // and -_.{}[]
520 } else {
521 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000522 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400523 return -3;
524 }
525 }
526
527 // Check to make sure that a backup with this name doesn't already exist
528 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
529 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500530 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500531 if (TWFunc::Path_Exists(tw_image_dir)) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400532 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000533 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400534 return -4;
535 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400536 // No problems found, return 0
537 return 0;
538}
539
Dees_Troy43d8b002012-09-17 16:00:01 -0400540bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
541{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500542 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400543 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500544 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500545 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400546
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500547 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400548 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400549
Dees_Troyb46a6842012-09-25 11:06:46 -0400550 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000551 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400552
553 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500554 md5sum.setfn(Backup_Folder + Backup_Filename);
555 if (md5sum.computeMD5() == 0)
556 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000557 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500558 else
559 return -1;
560 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000561 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400562 } else {
563 char filename[512];
564 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500565 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400566 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500567 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000568 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400569 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000570 if (TWFunc::Path_Exists(filename)) {
571 if (md5sum.computeMD5() == 0) {
572 if (md5sum.write_md5digest() != 0)
573 {
574 gui_print(" * MD5 Error.\n");
575 return false;
576 }
577 } else {
578 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500579 return false;
580 }
581 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400582 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400583 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500584 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400585 }
586 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000587 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400588 return false;
589 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000590 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400591 }
592 return true;
593}
594
Dees_Troy093b7642012-09-21 15:59:38 -0400595bool 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 -0400596 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500597 int img_bps;
598 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400599 unsigned long total_time, remain_time, section_time;
600 int use_compression, backup_time;
601 float pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500602 unsigned long long total_size, current_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400603
604 if (Part == NULL)
605 return true;
606
Dees_Troy093b7642012-09-21 15:59:38 -0400607 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
608
609 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
610 if (use_compression)
611 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
612 else
613 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
614
615 // We know the speed for both, how far into the whole backup are we, based on time
616 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
617 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
618
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500619 //pos = (total_time - remain_time) / (float) total_time;
620 total_size = *file_bytes + *img_bytes;
621 current_size = *file_bytes + *img_bytes - *file_bytes_remaining - *img_bytes_remaining;
622 pos = ((float)(current_size) / (float)(total_size));
Dees_Troy2673cec2013-04-02 20:22:16 +0000623 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400624
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500625 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400626
627 // And get the time
628 if (Part->Backup_Method == 1)
629 section_time = Part->Backup_Size / file_bps;
630 else
631 section_time = Part->Backup_Size / img_bps;
632
633 // Set the position
634 pos = section_time / (float) total_time;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500635 //DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400636
Dees_Troy43d8b002012-09-17 16:00:01 -0400637 time(&start);
638
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500639 if (Part->Backup(Backup_Folder, &total_size, &current_size)) {
640 current_size += Part->Backup_Size;
641 pos = (float)((float)(current_size) / (float)(total_size));
642 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400643 if (Part->Has_SubPartition) {
644 std::vector<TWPartition*>::iterator subpart;
645
646 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500647 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500648 if (!(*subpart)->Backup(Backup_Folder, &total_size, &current_size))
Dees_Troy8170a922012-09-18 15:40:25 -0400649 return false;
Dees_Troy2727b992013-08-14 20:09:30 +0000650 sync();
651 sync();
Dees_Troy8170a922012-09-18 15:40:25 -0400652 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
653 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400654 if (Part->Backup_Method == 1) {
655 *file_bytes_remaining -= (*subpart)->Backup_Size;
656 } else {
657 *img_bytes_remaining -= (*subpart)->Backup_Size;
658 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500659 current_size += Part->Backup_Size;
660 pos = (float)(current_size / total_size);
661 DataManager::SetProgress(pos);
Dees_Troy8170a922012-09-18 15:40:25 -0400662 }
663 }
664 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400665 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400666 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000667 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400668 if (Part->Backup_Method == 1) {
669 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400670 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400671 } else {
672 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400673 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400674 }
675 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
676 } else {
677 return false;
678 }
679}
680
681int TWPartitionManager::Run_Backup(void) {
682 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500683 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400684 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 -0400685 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500686 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400687 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400688 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400689 struct tm *t;
690 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500691 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400692 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500693 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400694
695 time(&total_start);
696
697 Update_System_Details();
698
699 if (!Mount_Current_Storage(true))
700 return false;
701
702 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400703 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400704 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400705 else
706 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400707
Dees_Troy43d8b002012-09-17 16:00:01 -0400708 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
709 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000710 if (Backup_Name == "(Current Date)") {
711 Backup_Name = TWFunc::Get_Current_Date();
712 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
713 TWFunc::Auto_Generate_Backup_Name();
714 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400715 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000716 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400717 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000718 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400719
Dees_Troy2673cec2013-04-02 20:22:16 +0000720 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500721 DataManager::GetValue("tw_backup_list", Backup_List);
722 if (!Backup_List.empty()) {
723 end_pos = Backup_List.find(";", start_pos);
724 while (end_pos != string::npos && start_pos < Backup_List.size()) {
725 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
726 backup_part = Find_Partition_By_Path(backup_path);
727 if (backup_part != NULL) {
728 partition_count++;
729 if (backup_part->Backup_Method == 1)
730 file_bytes += backup_part->Backup_Size;
731 else
732 img_bytes += backup_part->Backup_Size;
733 if (backup_part->Has_SubPartition) {
734 std::vector<TWPartition*>::iterator subpart;
735
736 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000737 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 -0500738 partition_count++;
739 if ((*subpart)->Backup_Method == 1)
740 file_bytes += (*subpart)->Backup_Size;
741 else
742 img_bytes += (*subpart)->Backup_Size;
743 }
744 }
Dees_Troy8170a922012-09-18 15:40:25 -0400745 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500746 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000747 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400748 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500749 start_pos = end_pos + 1;
750 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400751 }
752 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400753
754 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000755 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400756 return false;
757 }
758 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000759 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500760 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400761 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
762 if (storage != NULL) {
763 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000764 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400765 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000766 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400767 return false;
768 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000769 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400770 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000771 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400772 return false;
773 }
774 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500775 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400776
Dees_Troy2673cec2013-04-02 20:22:16 +0000777 gui_print("\n[BACKUP STARTED]\n");
778 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000779 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000780 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000781 return false;
782 }
783
Dees_Troy2673cec2013-04-02 20:22:16 +0000784 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400785
Dees_Troya13d74f2013-03-24 08:54:55 -0500786 start_pos = 0;
787 end_pos = Backup_List.find(";", start_pos);
788 while (end_pos != string::npos && start_pos < Backup_List.size()) {
789 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
790 backup_part = Find_Partition_By_Path(backup_path);
791 if (backup_part != NULL) {
792 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
793 return false;
794 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000795 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500796 }
797 start_pos = end_pos + 1;
798 end_pos = Backup_List.find(";", start_pos);
799 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400800
801 // Average BPS
802 if (img_time == 0)
803 img_time = 1;
804 if (file_time == 0)
805 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400806 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500807 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400808
Dees_Troy2673cec2013-04-02 20:22:16 +0000809 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
810 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400811
812 time(&total_stop);
813 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500814 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500815 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400816
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500817 int prev_img_bps, use_compression;
818 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400819 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
820 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500821 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400822
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500823 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400824 if (use_compression)
825 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500826 else
Dees_Troy093b7642012-09-21 15:59:38 -0400827 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
828 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500829 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400830
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500831 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400832 if (use_compression)
833 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
834 else
835 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
836
Dees_Troy2673cec2013-04-02 20:22:16 +0000837 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400838 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400839 UnMount_Main_Partitions();
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500840 gui_print_color("highlight", "[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000841 string backup_log = Full_Backup_Path + "recovery.log";
842 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
843 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400844}
845
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500846bool 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 -0400847 time_t Start, Stop;
848 time(&Start);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500849 //DataManager::ShowProgress(1.0 / (float)partition_count, 150);
850 if (!Part->Restore(Restore_Name, total_restore_size, already_restored_size))
Dees_Troy4a2a1262012-09-18 09:33:47 -0400851 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400852 if (Part->Has_SubPartition) {
853 std::vector<TWPartition*>::iterator subpart;
854
855 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
856 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500857 if (!(*subpart)->Restore(Restore_Name, total_restore_size, already_restored_size))
Dees_Troy8170a922012-09-18 15:40:25 -0400858 return false;
859 }
860 }
861 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400862 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000863 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 -0400864 return true;
865}
866
Dees_Troy51a0e822012-09-05 15:24:24 -0400867int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400868 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500869 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400870 time_t rStart, rStop;
871 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500872 string Restore_List, restore_path;
873 size_t start_pos = 0, end_pos;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500874 unsigned long long total_restore_size = 0, already_restored_size = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400875
Dees_Troy2673cec2013-04-02 20:22:16 +0000876 gui_print("\n[RESTORE STARTED]\n\n");
877 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400878
Dees_Troy4a2a1262012-09-18 09:33:47 -0400879 if (!Mount_Current_Storage(true))
880 return false;
881
882 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500883 if (check_md5 > 0) {
884 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
885 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000886 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500887 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000888 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500889 }
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500890 gui_print("Calculating restore details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500891 DataManager::GetValue("tw_restore_selected", Restore_List);
892 if (!Restore_List.empty()) {
893 end_pos = Restore_List.find(";", start_pos);
894 while (end_pos != string::npos && start_pos < Restore_List.size()) {
895 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
896 restore_part = Find_Partition_By_Path(restore_path);
897 if (restore_part != NULL) {
898 partition_count++;
899 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
900 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500901 total_restore_size += restore_part->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500902 if (restore_part->Has_SubPartition) {
903 std::vector<TWPartition*>::iterator subpart;
904
905 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
906 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400907 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500908 return false;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500909 total_restore_size += (*subpart)->Get_Restore_Size(Restore_Name);
Dees_Troya13d74f2013-03-24 08:54:55 -0500910 }
911 }
912 }
913 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500914 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500915 }
916 start_pos = end_pos + 1;
917 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400918 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400919 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400920
921 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000922 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400923 return false;
924 }
925
Dees_Troy2673cec2013-04-02 20:22:16 +0000926 gui_print("Restoring %i partitions...\n", partition_count);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500927 gui_print("Total restore size is %lluMB\n", total_restore_size / 1048576);
Dees_Troy2673cec2013-04-02 20:22:16 +0000928 DataManager::SetProgress(0.0);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500929
Dees_Troya13d74f2013-03-24 08:54:55 -0500930 start_pos = 0;
931 if (!Restore_List.empty()) {
932 end_pos = Restore_List.find(";", start_pos);
933 while (end_pos != string::npos && start_pos < Restore_List.size()) {
934 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
935 restore_part = Find_Partition_By_Path(restore_path);
936 if (restore_part != NULL) {
937 partition_count++;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500938 if (!Restore_Partition(restore_part, Restore_Name, partition_count, &total_restore_size, &already_restored_size))
Dees_Troya13d74f2013-03-24 08:54:55 -0500939 return false;
940 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000941 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500942 }
943 start_pos = end_pos + 1;
944 end_pos = Restore_List.find(";", start_pos);
945 }
946 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400947 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400948 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400949 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400950 time(&rStop);
Ethan Yonkerbf2cb1c2014-07-02 10:15:54 -0500951 gui_print_color("highlight", "[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500952 DataManager::SetValue("tw_file_progress", "");
Dees_Troy63c8df72012-09-10 14:02:05 -0400953 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400954}
955
956void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400957 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500958 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000959 bool get_date = true, check_encryption = true;
960
961 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400962
963 DIR* d;
964 d = opendir(Restore_Name.c_str());
965 if (d == NULL)
966 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000967 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400968 return;
969 }
970
971 struct dirent* de;
972 while ((de = readdir(d)) != NULL)
973 {
974 // Strip off three components
975 char str[256];
976 char* label;
977 char* fstype = NULL;
978 char* extn = NULL;
979 char* ptr;
980
981 strcpy(str, de->d_name);
982 if (strlen(str) <= 2)
983 continue;
984
985 if (get_date) {
986 char file_path[255];
987 struct stat st;
988
989 strcpy(file_path, Restore_Name.c_str());
990 strcat(file_path, "/");
991 strcat(file_path, str);
992 stat(file_path, &st);
993 string backup_date = ctime((const time_t*)(&st.st_mtime));
994 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
995 get_date = false;
996 }
997
998 label = str;
999 ptr = label;
1000 while (*ptr && *ptr != '.') ptr++;
1001 if (*ptr == '.')
1002 {
1003 *ptr = 0x00;
1004 ptr++;
1005 fstype = ptr;
1006 }
1007 while (*ptr && *ptr != '.') ptr++;
1008 if (*ptr == '.')
1009 {
1010 *ptr = 0x00;
1011 ptr++;
1012 extn = ptr;
1013 }
1014
Dees_Troy83bd4832013-05-04 12:39:56 +00001015 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -05001016 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001017 if (extnlength != 3 && extnlength != 6) continue;
1018 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1019 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1020
1021 if (check_encryption) {
1022 string filename = Restore_Name + "/";
1023 filename += de->d_name;
1024 if (TWFunc::Get_File_Type(filename) == 2) {
1025 LOGINFO("'%s' is encrypted\n", filename.c_str());
1026 DataManager::SetValue("tw_restore_encrypted", 1);
1027 }
1028 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001029 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001030
1031 TWPartition* Part = Find_Partition_By_Path(label);
1032 if (Part == NULL)
1033 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001034 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -04001035 continue;
1036 }
1037
1038 Part->Backup_FileName = de->d_name;
1039 if (strlen(extn) > 3) {
1040 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1041 }
1042
Dees_Troya13d74f2013-03-24 08:54:55 -05001043 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001044 }
1045 closedir(d);
1046
Dees_Troya13d74f2013-03-24 08:54:55 -05001047 // Set the final value
1048 DataManager::SetValue("tw_restore_list", Restore_List);
1049 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001050 return;
1051}
1052
1053int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001054 std::vector<TWPartition*>::iterator iter;
1055 int ret = false;
1056 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001057 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001058
1059 // Iterate through all partitions
1060 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001061 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001062 if (Path == "/and-sec")
1063 ret = (*iter)->Wipe_AndSec();
1064 else
1065 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001066 found = true;
1067 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1068 (*iter)->Wipe();
1069 }
1070 }
1071 if (found) {
1072 return ret;
1073 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001074 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001075 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001076}
1077
1078int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001079 TWPartition* Part = Find_Partition_By_Block(Block);
1080
1081 if (Part) {
1082 if (Part->Has_SubPartition) {
1083 std::vector<TWPartition*>::iterator subpart;
1084
1085 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1086 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1087 (*subpart)->Wipe();
1088 }
1089 return Part->Wipe();
1090 } else
1091 return Part->Wipe();
1092 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001093 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001094 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001095}
1096
1097int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001098 TWPartition* Part = Find_Partition_By_Name(Name);
1099
1100 if (Part) {
1101 if (Part->Has_SubPartition) {
1102 std::vector<TWPartition*>::iterator subpart;
1103
1104 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1105 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1106 (*subpart)->Wipe();
1107 }
1108 return Part->Wipe();
1109 } else
1110 return Part->Wipe();
1111 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001112 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001113 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001114}
1115
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001116int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1117 std::vector<TWPartition*>::iterator iter;
1118 int ret = false;
1119 bool found = false;
1120 string Local_Path = TWFunc::Get_Root_Path(Path);
1121
1122 // Iterate through all partitions
1123 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1124 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1125 if (Path == "/and-sec")
1126 ret = (*iter)->Wipe_AndSec();
1127 else
1128 ret = (*iter)->Wipe(New_File_System);
1129 found = true;
1130 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1131 (*iter)->Wipe(New_File_System);
1132 }
1133 }
1134 if (found) {
1135 return ret;
1136 } else
1137 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1138 return false;
1139}
1140
1141int TWPartitionManager::Wipe_By_Block(string Block, string New_File_System) {
1142 TWPartition* Part = Find_Partition_By_Block(Block);
1143
1144 if (Part) {
1145 if (Part->Has_SubPartition) {
1146 std::vector<TWPartition*>::iterator subpart;
1147
1148 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1149 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1150 (*subpart)->Wipe(New_File_System);
1151 }
1152 return Part->Wipe(New_File_System);
1153 } else
1154 return Part->Wipe(New_File_System);
1155 }
1156 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1157 return false;
1158}
1159
1160int TWPartitionManager::Wipe_By_Name(string Name, string New_File_System) {
1161 TWPartition* Part = Find_Partition_By_Name(Name);
1162
1163 if (Part) {
1164 if (Part->Has_SubPartition) {
1165 std::vector<TWPartition*>::iterator subpart;
1166
1167 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1168 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1169 (*subpart)->Wipe();
1170 }
1171 return Part->Wipe(New_File_System);
1172 } else
1173 return Part->Wipe(New_File_System);
1174 }
1175 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1176 return false;
1177}
1178
Dees_Troy51a0e822012-09-05 15:24:24 -04001179int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001180 std::vector<TWPartition*>::iterator iter;
1181 int ret = true;
1182
1183 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001184 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001185 if (!(*iter)->Wipe())
1186 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001187 } else if ((*iter)->Has_Android_Secure) {
1188 if (!(*iter)->Wipe_AndSec())
1189 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001190 }
1191 }
1192 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001193}
1194
Dees_Troy38bd7602012-09-14 13:33:53 -04001195int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1196 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001197 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001198
1199 if (!Mount_By_Path("/data", true))
1200 return false;
1201
1202 if (!Mount_By_Path("/cache", true))
1203 return false;
1204
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001205 dir.push_back("/data/dalvik-cache");
1206 dir.push_back("/cache/dalvik-cache");
1207 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001208 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001209 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001210 if (stat(dir.at(i).c_str(), &st) == 0) {
1211 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001212 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001213 }
1214 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001215 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001216 if (sdext && sdext->Is_Present && sdext->Mount(false))
1217 {
1218 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1219 {
1220 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001221 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001222 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001223 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001224 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001225 return true;
1226}
1227
1228int TWPartitionManager::Wipe_Rotate_Data(void) {
1229 if (!Mount_By_Path("/data", true))
1230 return false;
1231
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001232 unlink("/data/misc/akmd*");
1233 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001234 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001235 return true;
1236}
1237
1238int TWPartitionManager::Wipe_Battery_Stats(void) {
1239 struct stat st;
1240
1241 if (!Mount_By_Path("/data", true))
1242 return false;
1243
1244 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001245 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001246 } else {
1247 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001248 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001249 }
1250 return true;
1251}
1252
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001253int TWPartitionManager::Wipe_Android_Secure(void) {
1254 std::vector<TWPartition*>::iterator iter;
1255 int ret = false;
1256 bool found = false;
1257
1258 // Iterate through all partitions
1259 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1260 if ((*iter)->Has_Android_Secure) {
1261 ret = (*iter)->Wipe_AndSec();
1262 found = true;
1263 }
1264 }
1265 if (found) {
1266 return ret;
1267 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001268 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001269 }
1270 return false;
1271}
1272
Dees_Troy38bd7602012-09-14 13:33:53 -04001273int TWPartitionManager::Format_Data(void) {
1274 TWPartition* dat = Find_Partition_By_Path("/data");
1275
1276 if (dat != NULL) {
1277 if (!dat->UnMount(true))
1278 return false;
1279
1280 return dat->Wipe_Encryption();
1281 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001282 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001283 return false;
1284 }
1285 return false;
1286}
1287
1288int TWPartitionManager::Wipe_Media_From_Data(void) {
1289 TWPartition* dat = Find_Partition_By_Path("/data");
1290
1291 if (dat != NULL) {
1292 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001293 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001294 return false;
1295 }
1296 if (!dat->Mount(true))
1297 return false;
1298
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001300 TWFunc::removeDir("/data/media", false);
1301 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1302 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001303 if (dat->Has_Data_Media) {
1304 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001305 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1306 dat->UnMount(false);
1307 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001308 }
1309 return true;
1310 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001311 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001312 return false;
1313 }
1314 return false;
1315}
1316
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001317int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1318 std::vector<TWPartition*>::iterator iter;
1319 int ret = false;
1320 bool found = false;
1321 string Local_Path = TWFunc::Get_Root_Path(Path);
1322
1323 if (Local_Path == "/tmp" || Local_Path == "/")
1324 return true;
1325
1326 // Iterate through all partitions
1327 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1328 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1329 ret = (*iter)->Repair();
1330 found = true;
1331 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1332 (*iter)->Repair();
1333 }
1334 }
1335 if (found) {
1336 return ret;
1337 } else if (Display_Error) {
1338 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1339 } else {
1340 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1341 }
1342 return false;
1343}
1344
1345int TWPartitionManager::Repair_By_Block(string Block, bool Display_Error) {
1346 TWPartition* Part = Find_Partition_By_Block(Block);
1347
1348 if (Part) {
1349 if (Part->Has_SubPartition) {
1350 std::vector<TWPartition*>::iterator subpart;
1351
1352 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1353 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1354 (*subpart)->Repair();
1355 }
1356 return Part->Repair();
1357 } else
1358 return Part->Repair();
1359 }
1360 if (Display_Error)
1361 LOGERR("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1362 else
1363 LOGINFO("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1364 return false;
1365}
1366
1367int TWPartitionManager::Repair_By_Name(string Name, bool Display_Error) {
1368 TWPartition* Part = Find_Partition_By_Name(Name);
1369
1370 if (Part) {
1371 if (Part->Has_SubPartition) {
1372 std::vector<TWPartition*>::iterator subpart;
1373
1374 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1375 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1376 (*subpart)->Repair();
1377 }
1378 return Part->Repair();
1379 } else
1380 return Part->Repair();
1381 }
1382 if (Display_Error)
1383 LOGERR("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1384 else
1385 LOGINFO("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1386 return false;
1387}
1388
Dees_Troy51a0e822012-09-05 15:24:24 -04001389void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001390 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001391 return;
1392}
1393
1394void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001395 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001396 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001397
Dees_Troy2673cec2013-04-02 20:22:16 +00001398 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001399 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001400 if ((*iter)->Can_Be_Mounted) {
1401 (*iter)->Update_Size(true);
1402 if ((*iter)->Mount_Point == "/system") {
1403 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1404 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1405 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1406 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1407 } else if ((*iter)->Mount_Point == "/cache") {
1408 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1409 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1410 } else if ((*iter)->Mount_Point == "/sd-ext") {
1411 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1412 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1413 if ((*iter)->Backup_Size == 0) {
1414 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1415 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1416 } else
1417 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001418 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001419 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001420 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001421 if ((*iter)->Backup_Size == 0) {
1422 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1423 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1424 } else
1425 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001426 } else if ((*iter)->Mount_Point == "/boot") {
1427 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1428 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1429 if ((*iter)->Backup_Size == 0) {
1430 DataManager::SetValue("tw_has_boot_partition", 0);
1431 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1432 } else
1433 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001434 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001435#ifdef SP1_NAME
1436 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1437 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1438 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1439 }
1440#endif
1441#ifdef SP2_NAME
1442 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1443 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1444 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1445 }
1446#endif
1447#ifdef SP3_NAME
1448 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1449 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1450 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1451 }
1452#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001453 } else {
1454 // Handle unmountable partitions in case we reset defaults
1455 if ((*iter)->Mount_Point == "/boot") {
1456 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1457 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1458 if ((*iter)->Backup_Size == 0) {
1459 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1460 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1461 } else
1462 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1463 } else if ((*iter)->Mount_Point == "/recovery") {
1464 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1465 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1466 if ((*iter)->Backup_Size == 0) {
1467 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1468 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1469 } else
1470 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001471 } else if ((*iter)->Mount_Point == "/data") {
1472 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001473 }
1474#ifdef SP1_NAME
1475 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1476 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1477 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1478 }
1479#endif
1480#ifdef SP2_NAME
1481 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1482 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1483 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1484 }
1485#endif
1486#ifdef SP3_NAME
1487 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1488 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1489 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1490 }
1491#endif
Dees_Troy51127312012-09-08 13:08:49 -04001492 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001493 }
Dees_Troy51127312012-09-08 13:08:49 -04001494 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1495 string current_storage_path = DataManager::GetCurrentStoragePath();
1496 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001497 if (FreeStorage != NULL) {
1498 // Attempt to mount storage
1499 if (!FreeStorage->Mount(false)) {
1500 // We couldn't mount storage... check to see if we have dual storage
1501 int has_dual_storage;
1502 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1503 if (has_dual_storage == 1) {
1504 // We have dual storage, see if we're using the internal storage that should always be present
1505 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001506 if (!FreeStorage->Is_Encrypted) {
1507 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001508 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001509 }
Dees_Troy8170a922012-09-18 15:40:25 -04001510 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1511 } else {
1512 // We were using external, flip to internal
1513 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1514 current_storage_path = DataManager::GetCurrentStoragePath();
1515 FreeStorage = Find_Partition_By_Path(current_storage_path);
1516 if (FreeStorage != NULL) {
1517 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1518 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001519 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001520 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1521 }
1522 }
1523 } else {
1524 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001525 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001526 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1527 }
1528 } else {
1529 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1530 }
1531 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001532 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001533 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001534 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001535 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001536 return;
1537}
1538
1539int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001540#ifdef TW_INCLUDE_CRYPTO
1541 int ret_val, password_len;
1542 char crypto_blkdev[255], cPassword[255];
1543 size_t result;
1544
1545 property_set("ro.crypto.state", "encrypted");
1546#ifdef TW_INCLUDE_JB_CRYPTO
1547 // No extra flags needed
1548#else
1549 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1550 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1551 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1552 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1553 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1554 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001555
1556#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001557 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1558 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1559 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001560#endif
a39552696ff55ce2013-01-08 16:14:56 +00001561
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001562 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001563
1564#endif
1565
1566 // some samsung devices store "footer" on efs partition
1567 TWPartition *efs = Find_Partition_By_Path("/efs");
1568 if(efs && !efs->Is_Mounted())
1569 efs->Mount(false);
1570 else
1571 efs = 0;
1572#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001573#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001574 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001575 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001576 property_set("ro.crypto.external_encrypted", "1");
1577 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1578 } else {
1579 property_set("ro.crypto.external_encrypted", "0");
1580 }
1581#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001582#endif
a39552696ff55ce2013-01-08 16:14:56 +00001583
Dees_Troy5bf43922012-09-07 16:07:55 -04001584 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001585 int pwret = cryptfs_check_passwd(cPassword);
1586
1587 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001588 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001589 return -1;
1590 }
a39552696ff55ce2013-01-08 16:14:56 +00001591
1592 if(efs)
1593 efs->UnMount(false);
1594
Dees_Troy5bf43922012-09-07 16:07:55 -04001595 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1596 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001597 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001598 } else {
1599 TWPartition* dat = Find_Partition_By_Path("/data");
1600 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001601 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001602 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1603 dat->Is_Decrypted = true;
1604 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001605 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001606 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 +00001607 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001608
1609#ifdef CRYPTO_SD_FS_TYPE
1610 char crypto_blkdev_sd[255];
1611 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1612 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001613 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001614 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001615 emmc->Is_Decrypted = true;
1616 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1617 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001618 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001619 }
a39552696ff55ce2013-01-08 16:14:56 +00001620#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001621#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001622#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001623 char is_external_decrypted[255];
1624 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1625 if (strcmp(is_external_decrypted, "1") == 0) {
1626 sdcard->Is_Decrypted = true;
1627 sdcard->EcryptFS_Password = Password;
1628 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001629 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1630 MetaEcfsFile += "/.MetaEcfsFile";
1631 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1632 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1633 sdcard->UnMount(false);
1634 sdcard->Mount(false);
1635 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001636 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001637 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001638 sdcard->Is_Decrypted = false;
1639 sdcard->Decrypted_Block_Device = "";
1640 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001641#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001642#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001643
Dees_Troy5bf43922012-09-07 16:07:55 -04001644 // Sleep for a bit so that the device will be ready
1645 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001646#ifdef RECOVERY_SDCARD_ON_DATA
1647 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1648 dat->Storage_Path = "/data/media/0";
1649 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001650 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001651 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001652 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001653 }
1654#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001655 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001656 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001657 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001658 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001659 }
1660 return 0;
1661#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001662 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001663 return -1;
1664#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001665 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001666}
1667
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001668int TWPartitionManager::Fix_Permissions(void) {
1669 int result = 0;
1670 if (!Mount_By_Path("/data", true))
1671 return false;
1672
1673 if (!Mount_By_Path("/system", true))
1674 return false;
1675
1676 Mount_By_Path("/sd-ext", false);
1677
1678 fixPermissions perms;
1679 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001680 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001681 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001682 return result;
1683}
1684
Ethan Yonker47360be2014-04-01 10:34:34 -05001685TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1686 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1687
1688 if (!Path.empty()) {
1689 string Search_Path = TWFunc::Get_Root_Path(Path);
1690 for (; iter != Partitions.end(); iter++) {
1691 if ((*iter)->Mount_Point == Search_Path) {
1692 iter++;
1693 break;
1694 }
1695 }
1696 }
1697
1698 for (; iter != Partitions.end(); iter++) {
1699 if ((*iter)->Is_Storage && (*iter)->Is