blob: 47c2ebe4944fd01cd48fed42695b021a03b9bbf8 [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;
Dees_Troy43d8b002012-09-17 16:00:01 -0400602
603 if (Part == NULL)
604 return true;
605
Dees_Troy093b7642012-09-21 15:59:38 -0400606 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
607
608 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
609 if (use_compression)
610 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
611 else
612 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
613
614 // We know the speed for both, how far into the whole backup are we, based on time
615 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
616 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
617
618 pos = (total_time - remain_time) / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000619 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400620
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500621 LOGINFO("Estimated total time: %lu\nEstimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400622
623 // And get the time
624 if (Part->Backup_Method == 1)
625 section_time = Part->Backup_Size / file_bps;
626 else
627 section_time = Part->Backup_Size / img_bps;
628
629 // Set the position
630 pos = section_time / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000631 DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400632
Dees_Troy43d8b002012-09-17 16:00:01 -0400633 time(&start);
634
635 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400636 if (Part->Has_SubPartition) {
637 std::vector<TWPartition*>::iterator subpart;
638
639 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500640 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Dees_Troy8170a922012-09-18 15:40:25 -0400641 if (!(*subpart)->Backup(Backup_Folder))
642 return false;
Dees_Troy2727b992013-08-14 20:09:30 +0000643 sync();
644 sync();
Dees_Troy8170a922012-09-18 15:40:25 -0400645 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
646 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400647 if (Part->Backup_Method == 1) {
648 *file_bytes_remaining -= (*subpart)->Backup_Size;
649 } else {
650 *img_bytes_remaining -= (*subpart)->Backup_Size;
651 }
Dees_Troy8170a922012-09-18 15:40:25 -0400652 }
653 }
654 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400655 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400656 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000657 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400658 if (Part->Backup_Method == 1) {
659 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400660 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400661 } else {
662 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400663 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400664 }
665 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
666 } else {
667 return false;
668 }
669}
670
671int TWPartitionManager::Run_Backup(void) {
672 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500673 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400674 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 -0400675 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500676 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400677 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400678 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400679 struct tm *t;
680 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500681 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400682 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500683 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400684
685 time(&total_start);
686
687 Update_System_Details();
688
689 if (!Mount_Current_Storage(true))
690 return false;
691
692 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400693 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400694 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400695 else
696 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400697
Dees_Troy43d8b002012-09-17 16:00:01 -0400698 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
699 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000700 if (Backup_Name == "(Current Date)") {
701 Backup_Name = TWFunc::Get_Current_Date();
702 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
703 TWFunc::Auto_Generate_Backup_Name();
704 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400705 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000706 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400707 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000708 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400709
Dees_Troy2673cec2013-04-02 20:22:16 +0000710 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500711 DataManager::GetValue("tw_backup_list", Backup_List);
712 if (!Backup_List.empty()) {
713 end_pos = Backup_List.find(";", start_pos);
714 while (end_pos != string::npos && start_pos < Backup_List.size()) {
715 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
716 backup_part = Find_Partition_By_Path(backup_path);
717 if (backup_part != NULL) {
718 partition_count++;
719 if (backup_part->Backup_Method == 1)
720 file_bytes += backup_part->Backup_Size;
721 else
722 img_bytes += backup_part->Backup_Size;
723 if (backup_part->Has_SubPartition) {
724 std::vector<TWPartition*>::iterator subpart;
725
726 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000727 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 -0500728 partition_count++;
729 if ((*subpart)->Backup_Method == 1)
730 file_bytes += (*subpart)->Backup_Size;
731 else
732 img_bytes += (*subpart)->Backup_Size;
733 }
734 }
Dees_Troy8170a922012-09-18 15:40:25 -0400735 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500736 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000737 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400738 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500739 start_pos = end_pos + 1;
740 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400741 }
742 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400743
744 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000745 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400746 return false;
747 }
748 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000749 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500750 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400751 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
752 if (storage != NULL) {
753 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000754 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400755 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000756 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400757 return false;
758 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000759 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400760 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000761 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400762 return false;
763 }
764 img_bytes_remaining = img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500765 file_bytes_remaining = file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400766
Dees_Troy2673cec2013-04-02 20:22:16 +0000767 gui_print("\n[BACKUP STARTED]\n");
768 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000769 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000770 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000771 return false;
772 }
773
Dees_Troy2673cec2013-04-02 20:22:16 +0000774 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400775
Dees_Troya13d74f2013-03-24 08:54:55 -0500776 start_pos = 0;
777 end_pos = Backup_List.find(";", start_pos);
778 while (end_pos != string::npos && start_pos < Backup_List.size()) {
779 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
780 backup_part = Find_Partition_By_Path(backup_path);
781 if (backup_part != NULL) {
782 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
783 return false;
784 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000785 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500786 }
787 start_pos = end_pos + 1;
788 end_pos = Backup_List.find(";", start_pos);
789 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400790
791 // Average BPS
792 if (img_time == 0)
793 img_time = 1;
794 if (file_time == 0)
795 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400796 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500797 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400798
Dees_Troy2673cec2013-04-02 20:22:16 +0000799 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
800 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400801
802 time(&total_stop);
803 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500804 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500805 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -0400806
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500807 int prev_img_bps, use_compression;
808 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400809 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
810 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500811 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400812
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500813 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400814 if (use_compression)
815 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500816 else
Dees_Troy093b7642012-09-21 15:59:38 -0400817 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
818 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500819 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -0400820
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500821 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -0400822 if (use_compression)
823 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
824 else
825 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
826
Dees_Troy2673cec2013-04-02 20:22:16 +0000827 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400828 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400829 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +0000830 gui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000831 string backup_log = Full_Backup_Path + "recovery.log";
832 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
833 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400834}
835
Dees_Troy093b7642012-09-21 15:59:38 -0400836bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400837 time_t Start, Stop;
838 time(&Start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000839 DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400840 if (!Part->Restore(Restore_Name))
841 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400842 if (Part->Has_SubPartition) {
843 std::vector<TWPartition*>::iterator subpart;
844
845 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
846 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
847 if (!(*subpart)->Restore(Restore_Name))
848 return false;
849 }
850 }
851 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400852 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000853 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 -0400854 return true;
855}
856
Dees_Troy51a0e822012-09-05 15:24:24 -0400857int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400858 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500859 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400860 time_t rStart, rStop;
861 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500862 string Restore_List, restore_path;
863 size_t start_pos = 0, end_pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400864
Dees_Troy2673cec2013-04-02 20:22:16 +0000865 gui_print("\n[RESTORE STARTED]\n\n");
866 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400867
Dees_Troy4a2a1262012-09-18 09:33:47 -0400868 if (!Mount_Current_Storage(true))
869 return false;
870
871 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500872 if (check_md5 > 0) {
873 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
874 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000875 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500876 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000877 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500878 }
879 DataManager::GetValue("tw_restore_selected", Restore_List);
880 if (!Restore_List.empty()) {
881 end_pos = Restore_List.find(";", start_pos);
882 while (end_pos != string::npos && start_pos < Restore_List.size()) {
883 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
884 restore_part = Find_Partition_By_Path(restore_path);
885 if (restore_part != NULL) {
886 partition_count++;
887 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
888 return false;
889 if (restore_part->Has_SubPartition) {
890 std::vector<TWPartition*>::iterator subpart;
891
892 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
893 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400894 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500895 return false;
896 }
897 }
898 }
899 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500900 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500901 }
902 start_pos = end_pos + 1;
903 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400904 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400905 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400906
907 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000908 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400909 return false;
910 }
911
Dees_Troy2673cec2013-04-02 20:22:16 +0000912 gui_print("Restoring %i partitions...\n", partition_count);
913 DataManager::SetProgress(0.0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500914 start_pos = 0;
915 if (!Restore_List.empty()) {
916 end_pos = Restore_List.find(";", start_pos);
917 while (end_pos != string::npos && start_pos < Restore_List.size()) {
918 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
919 restore_part = Find_Partition_By_Path(restore_path);
920 if (restore_part != NULL) {
921 partition_count++;
922 if (!Restore_Partition(restore_part, Restore_Name, partition_count))
923 return false;
924 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000925 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500926 }
927 start_pos = end_pos + 1;
928 end_pos = Restore_List.find(";", start_pos);
929 }
930 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400931 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400932 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400933 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400934 time(&rStop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000935 gui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -0400936 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400937}
938
939void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400940 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500941 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000942 bool get_date = true, check_encryption = true;
943
944 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400945
946 DIR* d;
947 d = opendir(Restore_Name.c_str());
948 if (d == NULL)
949 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000950 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400951 return;
952 }
953
954 struct dirent* de;
955 while ((de = readdir(d)) != NULL)
956 {
957 // Strip off three components
958 char str[256];
959 char* label;
960 char* fstype = NULL;
961 char* extn = NULL;
962 char* ptr;
963
964 strcpy(str, de->d_name);
965 if (strlen(str) <= 2)
966 continue;
967
968 if (get_date) {
969 char file_path[255];
970 struct stat st;
971
972 strcpy(file_path, Restore_Name.c_str());
973 strcat(file_path, "/");
974 strcat(file_path, str);
975 stat(file_path, &st);
976 string backup_date = ctime((const time_t*)(&st.st_mtime));
977 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
978 get_date = false;
979 }
980
981 label = str;
982 ptr = label;
983 while (*ptr && *ptr != '.') ptr++;
984 if (*ptr == '.')
985 {
986 *ptr = 0x00;
987 ptr++;
988 fstype = ptr;
989 }
990 while (*ptr && *ptr != '.') ptr++;
991 if (*ptr == '.')
992 {
993 *ptr = 0x00;
994 ptr++;
995 extn = ptr;
996 }
997
Dees_Troy83bd4832013-05-04 12:39:56 +0000998 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500999 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +00001000 if (extnlength != 3 && extnlength != 6) continue;
1001 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1002 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1003
1004 if (check_encryption) {
1005 string filename = Restore_Name + "/";
1006 filename += de->d_name;
1007 if (TWFunc::Get_File_Type(filename) == 2) {
1008 LOGINFO("'%s' is encrypted\n", filename.c_str());
1009 DataManager::SetValue("tw_restore_encrypted", 1);
1010 }
1011 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001012 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001013
1014 TWPartition* Part = Find_Partition_By_Path(label);
1015 if (Part == NULL)
1016 {
Dees_Troy2673cec2013-04-02 20:22:16 +00001017 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -04001018 continue;
1019 }
1020
1021 Part->Backup_FileName = de->d_name;
1022 if (strlen(extn) > 3) {
1023 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1024 }
1025
Dees_Troya13d74f2013-03-24 08:54:55 -05001026 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001027 }
1028 closedir(d);
1029
Dees_Troya13d74f2013-03-24 08:54:55 -05001030 // Set the final value
1031 DataManager::SetValue("tw_restore_list", Restore_List);
1032 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001033 return;
1034}
1035
1036int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001037 std::vector<TWPartition*>::iterator iter;
1038 int ret = false;
1039 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001040 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001041
1042 // Iterate through all partitions
1043 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001044 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001045 if (Path == "/and-sec")
1046 ret = (*iter)->Wipe_AndSec();
1047 else
1048 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001049 found = true;
1050 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1051 (*iter)->Wipe();
1052 }
1053 }
1054 if (found) {
1055 return ret;
1056 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001057 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001058 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001059}
1060
1061int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001062 TWPartition* Part = Find_Partition_By_Block(Block);
1063
1064 if (Part) {
1065 if (Part->Has_SubPartition) {
1066 std::vector<TWPartition*>::iterator subpart;
1067
1068 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1069 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1070 (*subpart)->Wipe();
1071 }
1072 return Part->Wipe();
1073 } else
1074 return Part->Wipe();
1075 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001076 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001077 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001078}
1079
1080int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001081 TWPartition* Part = Find_Partition_By_Name(Name);
1082
1083 if (Part) {
1084 if (Part->Has_SubPartition) {
1085 std::vector<TWPartition*>::iterator subpart;
1086
1087 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1088 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1089 (*subpart)->Wipe();
1090 }
1091 return Part->Wipe();
1092 } else
1093 return Part->Wipe();
1094 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001095 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001096 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001097}
1098
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001099int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1100 std::vector<TWPartition*>::iterator iter;
1101 int ret = false;
1102 bool found = false;
1103 string Local_Path = TWFunc::Get_Root_Path(Path);
1104
1105 // Iterate through all partitions
1106 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1107 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1108 if (Path == "/and-sec")
1109 ret = (*iter)->Wipe_AndSec();
1110 else
1111 ret = (*iter)->Wipe(New_File_System);
1112 found = true;
1113 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1114 (*iter)->Wipe(New_File_System);
1115 }
1116 }
1117 if (found) {
1118 return ret;
1119 } else
1120 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1121 return false;
1122}
1123
1124int TWPartitionManager::Wipe_By_Block(string Block, string New_File_System) {
1125 TWPartition* Part = Find_Partition_By_Block(Block);
1126
1127 if (Part) {
1128 if (Part->Has_SubPartition) {
1129 std::vector<TWPartition*>::iterator subpart;
1130
1131 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1132 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1133 (*subpart)->Wipe(New_File_System);
1134 }
1135 return Part->Wipe(New_File_System);
1136 } else
1137 return Part->Wipe(New_File_System);
1138 }
1139 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1140 return false;
1141}
1142
1143int TWPartitionManager::Wipe_By_Name(string Name, string New_File_System) {
1144 TWPartition* Part = Find_Partition_By_Name(Name);
1145
1146 if (Part) {
1147 if (Part->Has_SubPartition) {
1148 std::vector<TWPartition*>::iterator subpart;
1149
1150 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1151 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1152 (*subpart)->Wipe();
1153 }
1154 return Part->Wipe(New_File_System);
1155 } else
1156 return Part->Wipe(New_File_System);
1157 }
1158 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1159 return false;
1160}
1161
Dees_Troy51a0e822012-09-05 15:24:24 -04001162int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001163 std::vector<TWPartition*>::iterator iter;
1164 int ret = true;
1165
1166 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001167 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001168 if (!(*iter)->Wipe())
1169 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001170 } else if ((*iter)->Has_Android_Secure) {
1171 if (!(*iter)->Wipe_AndSec())
1172 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001173 }
1174 }
1175 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001176}
1177
Dees_Troy38bd7602012-09-14 13:33:53 -04001178int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1179 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001180 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001181
1182 if (!Mount_By_Path("/data", true))
1183 return false;
1184
1185 if (!Mount_By_Path("/cache", true))
1186 return false;
1187
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001188 dir.push_back("/data/dalvik-cache");
1189 dir.push_back("/cache/dalvik-cache");
1190 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001191 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001192 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001193 if (stat(dir.at(i).c_str(), &st) == 0) {
1194 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001195 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001196 }
1197 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001198 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001199 if (sdext && sdext->Is_Present && sdext->Mount(false))
1200 {
1201 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1202 {
1203 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001204 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001205 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001206 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001207 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001208 return true;
1209}
1210
1211int TWPartitionManager::Wipe_Rotate_Data(void) {
1212 if (!Mount_By_Path("/data", true))
1213 return false;
1214
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001215 unlink("/data/misc/akmd*");
1216 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001217 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001218 return true;
1219}
1220
1221int TWPartitionManager::Wipe_Battery_Stats(void) {
1222 struct stat st;
1223
1224 if (!Mount_By_Path("/data", true))
1225 return false;
1226
1227 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001228 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001229 } else {
1230 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001231 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001232 }
1233 return true;
1234}
1235
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001236int TWPartitionManager::Wipe_Android_Secure(void) {
1237 std::vector<TWPartition*>::iterator iter;
1238 int ret = false;
1239 bool found = false;
1240
1241 // Iterate through all partitions
1242 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1243 if ((*iter)->Has_Android_Secure) {
1244 ret = (*iter)->Wipe_AndSec();
1245 found = true;
1246 }
1247 }
1248 if (found) {
1249 return ret;
1250 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001251 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001252 }
1253 return false;
1254}
1255
Dees_Troy38bd7602012-09-14 13:33:53 -04001256int TWPartitionManager::Format_Data(void) {
1257 TWPartition* dat = Find_Partition_By_Path("/data");
1258
1259 if (dat != NULL) {
1260 if (!dat->UnMount(true))
1261 return false;
1262
1263 return dat->Wipe_Encryption();
1264 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001265 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001266 return false;
1267 }
1268 return false;
1269}
1270
1271int TWPartitionManager::Wipe_Media_From_Data(void) {
1272 TWPartition* dat = Find_Partition_By_Path("/data");
1273
1274 if (dat != NULL) {
1275 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001276 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001277 return false;
1278 }
1279 if (!dat->Mount(true))
1280 return false;
1281
Dees_Troy2673cec2013-04-02 20:22:16 +00001282 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001283 TWFunc::removeDir("/data/media", false);
1284 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1285 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001286 if (dat->Has_Data_Media) {
1287 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001288 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1289 dat->UnMount(false);
1290 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001291 }
1292 return true;
1293 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001294 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001295 return false;
1296 }
1297 return false;
1298}
1299
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001300int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1301 std::vector<TWPartition*>::iterator iter;
1302 int ret = false;
1303 bool found = false;
1304 string Local_Path = TWFunc::Get_Root_Path(Path);
1305
1306 if (Local_Path == "/tmp" || Local_Path == "/")
1307 return true;
1308
1309 // Iterate through all partitions
1310 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1311 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1312 ret = (*iter)->Repair();
1313 found = true;
1314 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1315 (*iter)->Repair();
1316 }
1317 }
1318 if (found) {
1319 return ret;
1320 } else if (Display_Error) {
1321 LOGERR("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1322 } else {
1323 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1324 }
1325 return false;
1326}
1327
1328int TWPartitionManager::Repair_By_Block(string Block, bool Display_Error) {
1329 TWPartition* Part = Find_Partition_By_Block(Block);
1330
1331 if (Part) {
1332 if (Part->Has_SubPartition) {
1333 std::vector<TWPartition*>::iterator subpart;
1334
1335 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1336 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1337 (*subpart)->Repair();
1338 }
1339 return Part->Repair();
1340 } else
1341 return Part->Repair();
1342 }
1343 if (Display_Error)
1344 LOGERR("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1345 else
1346 LOGINFO("Repair: Unable to find partition for block '%s'\n", Block.c_str());
1347 return false;
1348}
1349
1350int TWPartitionManager::Repair_By_Name(string Name, bool Display_Error) {
1351 TWPartition* Part = Find_Partition_By_Name(Name);
1352
1353 if (Part) {
1354 if (Part->Has_SubPartition) {
1355 std::vector<TWPartition*>::iterator subpart;
1356
1357 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1358 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1359 (*subpart)->Repair();
1360 }
1361 return Part->Repair();
1362 } else
1363 return Part->Repair();
1364 }
1365 if (Display_Error)
1366 LOGERR("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1367 else
1368 LOGINFO("Repair: Unable to find partition for name '%s'\n", Name.c_str());
1369 return false;
1370}
1371
Dees_Troy51a0e822012-09-05 15:24:24 -04001372void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001373 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001374 return;
1375}
1376
1377void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001378 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001379 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001380
Dees_Troy2673cec2013-04-02 20:22:16 +00001381 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001382 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001383 if ((*iter)->Can_Be_Mounted) {
1384 (*iter)->Update_Size(true);
1385 if ((*iter)->Mount_Point == "/system") {
1386 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1387 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1388 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1389 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1390 } else if ((*iter)->Mount_Point == "/cache") {
1391 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1392 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1393 } else if ((*iter)->Mount_Point == "/sd-ext") {
1394 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1395 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1396 if ((*iter)->Backup_Size == 0) {
1397 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1398 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1399 } else
1400 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001401 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001402 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001403 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001404 if ((*iter)->Backup_Size == 0) {
1405 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1406 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1407 } else
1408 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001409 } else if ((*iter)->Mount_Point == "/boot") {
1410 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1411 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1412 if ((*iter)->Backup_Size == 0) {
1413 DataManager::SetValue("tw_has_boot_partition", 0);
1414 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1415 } else
1416 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001417 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001418#ifdef SP1_NAME
1419 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1420 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1421 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1422 }
1423#endif
1424#ifdef SP2_NAME
1425 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1426 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1427 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1428 }
1429#endif
1430#ifdef SP3_NAME
1431 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1432 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1433 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1434 }
1435#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001436 } else {
1437 // Handle unmountable partitions in case we reset defaults
1438 if ((*iter)->Mount_Point == "/boot") {
1439 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1440 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1441 if ((*iter)->Backup_Size == 0) {
1442 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1443 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1444 } else
1445 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1446 } else if ((*iter)->Mount_Point == "/recovery") {
1447 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1448 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1449 if ((*iter)->Backup_Size == 0) {
1450 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1451 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1452 } else
1453 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001454 } else if ((*iter)->Mount_Point == "/data") {
1455 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001456 }
1457#ifdef SP1_NAME
1458 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1459 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1460 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1461 }
1462#endif
1463#ifdef SP2_NAME
1464 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1465 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1466 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1467 }
1468#endif
1469#ifdef SP3_NAME
1470 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1471 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1472 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1473 }
1474#endif
Dees_Troy51127312012-09-08 13:08:49 -04001475 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001476 }
Dees_Troy51127312012-09-08 13:08:49 -04001477 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1478 string current_storage_path = DataManager::GetCurrentStoragePath();
1479 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001480 if (FreeStorage != NULL) {
1481 // Attempt to mount storage
1482 if (!FreeStorage->Mount(false)) {
1483 // We couldn't mount storage... check to see if we have dual storage
1484 int has_dual_storage;
1485 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1486 if (has_dual_storage == 1) {
1487 // We have dual storage, see if we're using the internal storage that should always be present
1488 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001489 if (!FreeStorage->Is_Encrypted) {
1490 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001491 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001492 }
Dees_Troy8170a922012-09-18 15:40:25 -04001493 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1494 } else {
1495 // We were using external, flip to internal
1496 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1497 current_storage_path = DataManager::GetCurrentStoragePath();
1498 FreeStorage = Find_Partition_By_Path(current_storage_path);
1499 if (FreeStorage != NULL) {
1500 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1501 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001502 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001503 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1504 }
1505 }
1506 } else {
1507 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001508 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001509 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1510 }
1511 } else {
1512 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1513 }
1514 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001515 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001516 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001517 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001518 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001519 return;
1520}
1521
1522int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001523#ifdef TW_INCLUDE_CRYPTO
1524 int ret_val, password_len;
1525 char crypto_blkdev[255], cPassword[255];
1526 size_t result;
1527
1528 property_set("ro.crypto.state", "encrypted");
1529#ifdef TW_INCLUDE_JB_CRYPTO
1530 // No extra flags needed
1531#else
1532 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1533 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1534 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1535 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1536 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1537 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001538
1539#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001540 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1541 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1542 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001543#endif
a39552696ff55ce2013-01-08 16:14:56 +00001544
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001545 property_set("rw.km_fips_status", "ready");
a39552696ff55ce2013-01-08 16:14:56 +00001546
1547#endif
1548
1549 // some samsung devices store "footer" on efs partition
1550 TWPartition *efs = Find_Partition_By_Path("/efs");
1551 if(efs && !efs->Is_Mounted())
1552 efs->Mount(false);
1553 else
1554 efs = 0;
1555#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001556#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001557 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001558 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001559 property_set("ro.crypto.external_encrypted", "1");
1560 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1561 } else {
1562 property_set("ro.crypto.external_encrypted", "0");
1563 }
1564#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001565#endif
a39552696ff55ce2013-01-08 16:14:56 +00001566
Dees_Troy5bf43922012-09-07 16:07:55 -04001567 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001568 int pwret = cryptfs_check_passwd(cPassword);
1569
1570 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001571 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001572 return -1;
1573 }
a39552696ff55ce2013-01-08 16:14:56 +00001574
1575 if(efs)
1576 efs->UnMount(false);
1577
Dees_Troy5bf43922012-09-07 16:07:55 -04001578 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1579 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001580 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001581 } else {
1582 TWPartition* dat = Find_Partition_By_Path("/data");
1583 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001584 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001585 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1586 dat->Is_Decrypted = true;
1587 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001588 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001589 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 +00001590 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001591
1592#ifdef CRYPTO_SD_FS_TYPE
1593 char crypto_blkdev_sd[255];
1594 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1595 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001596 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001597 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001598 emmc->Is_Decrypted = true;
1599 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1600 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001601 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001602 }
a39552696ff55ce2013-01-08 16:14:56 +00001603#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001604#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001605#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001606 char is_external_decrypted[255];
1607 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1608 if (strcmp(is_external_decrypted, "1") == 0) {
1609 sdcard->Is_Decrypted = true;
1610 sdcard->EcryptFS_Password = Password;
1611 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001612 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1613 MetaEcfsFile += "/.MetaEcfsFile";
1614 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1615 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1616 sdcard->UnMount(false);
1617 sdcard->Mount(false);
1618 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001619 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001620 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001621 sdcard->Is_Decrypted = false;
1622 sdcard->Decrypted_Block_Device = "";
1623 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001624#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001625#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001626
Dees_Troy5bf43922012-09-07 16:07:55 -04001627 // Sleep for a bit so that the device will be ready
1628 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001629#ifdef RECOVERY_SDCARD_ON_DATA
1630 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1631 dat->Storage_Path = "/data/media/0";
1632 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001633 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001634 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001635 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001636 }
1637#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001638 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001639 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001640 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001641 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001642 }
1643 return 0;
1644#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001645 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001646 return -1;
1647#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001648 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001649}
1650
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001651int TWPartitionManager::Fix_Permissions(void) {
1652 int result = 0;
1653 if (!Mount_By_Path("/data", true))
1654 return false;
1655
1656 if (!Mount_By_Path("/system", true))
1657 return false;
1658
1659 Mount_By_Path("/sd-ext", false);
1660
1661 fixPermissions perms;
1662 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001663 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001664 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001665 return result;
1666}
1667
Ethan Yonker47360be2014-04-01 10:34:34 -05001668TWPartition* TWPartitionManager::Find_Next_Storage(string Path, string Exclude) {
1669 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1670
1671 if (!Path.empty()) {
1672 string Search_Path = TWFunc::Get_Root_Path(Path);
1673 for (; iter != Partitions.end(); iter++) {
1674 if ((*iter)->Mount_Point == Search_Path) {
1675 iter++;
1676 break;
1677 }
1678 }
1679 }
1680
1681 for (; iter != Partitions.end(); iter++) {
1682 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount_Point != Exclude) {
1683 return (*iter);
1684 }
1685 }
1686
1687 return NULL;
1688}
1689
Dees_Troyd21618c2012-10-14 18:48:49 -04001690int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001691 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1692
1693 if (Part == NULL) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001694 LOGERR("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
Dees_Troyd21618c2012-10-14 18:48:49 -04001695 return false;
1696 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001697 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1698 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001699 return false;
1700
Dees_Troy2673cec2013-04-02 20:22:16 +00001701 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1702 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001703 return false;
1704 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001705 return true;
1706}
1707
Dees_Troy8170a922012-09-18 15:40:25 -04001708int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001709 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001710 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001711 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001712
Dees_Troy8170a922012-09-18 15:40:25 -04001713 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
Ethan Yonker47360be2014-04-01 10:34:34 -05001714 string Lun_File_str = CUSTOM_LUN_FILE;
1715 size_t found = Lun_File_str.find("%");
1716 if (found != string::npos) {
1717 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1718 if (TWFunc::Path_Exists(lun_file))
1719 has_multiple_lun = true;
1720 }
1721 if (!has_multiple_lun) {
1722 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1723 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1724 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
1725 TWPartition* Mount = Find_Next_Storage("", "/data");
1726 if (Mount) {
1727 if (!Open_Lun_File(Mount->Mount_Point, lun_file))
1728 return false;
1729 } else {
1730 LOGERR("Unable to find storage partition to mount to USB\n");
Dees_Troyf4ca6122012-10-13 22:17:20 -04001731 return false;
Ethan Yonker47360be2014-04-01 10:34:34 -05001732 }
1733 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
1734 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001735 }
Dees_Troy8170a922012-09-18 15:40:25 -04001736 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001737 LOGINFO("Device has multiple lun files\n");
1738 TWPartition* Mount1;
1739 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001740 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker47360be2014-04-01 10:34:34 -05001741 Mount1 = Find_Next_Storage("", "/data");
1742 if (Mount1) {
1743 if (!Open_Lun_File(Mount1->Mount_Point, lun_file))
1744 return false;
Matt Moweree717062014-04-26 01:46:46 -05001745 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker47360be2014-04-01 10:34:34 -05001746 Mount2 = Find_Next_Storage(Mount1->Mount_Point, "/data");
1747 if (Mount2) {
Matt Moweree717062014-04-26 01:46:46 -05001748 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001749 }
1750 } else {
1751 LOGERR("Unable to find storage partition to mount to USB\n");
1752 return false;
1753 }
Dees_Troy8170a922012-09-18 15:40:25 -04001754 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001755 property_set("sys.storage.ums_enabled", "1");
Dees_Troy8170a922012-09-18 15:40:25 -04001756 return true;
1757}
1758
1759int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001760 int index, ret;
1761 char lun_file[255], ch[2] = {0, 0};
1762 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001763
1764 for (index=0; index<2; index++) {
1765 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001766 ret = TWFunc::write_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001767 if (ret < 0) {
1768 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001769 }
Dees_Troy8170a922012-09-18 15:40:25 -04001770 }
Dees_Troye58d5262012-09-21 12:27:57 -04001771 Mount_All_Storage();
1772 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001773 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001774 property_set("sys.storage.ums_enabled", "0");
Dees_Troy2673cec2013-04-02 20:22:16 +00001775 if (ret < 0 && index == 0) {
1776 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1777 return false;
1778 } else {
1779 return true;
1780 }
Dees_Troy8170a922012-09-18 15:40:25 -04001781 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001782}
1783
1784void TWPartitionManager::Mount_All_Storage(void) {
1785 std::vector<TWPartition*>::iterator iter;
1786
1787 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1788 if ((*iter)->Is_Storage)
1789 (*iter)->Mount(false);
1790 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001791}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001792
Dees_Troyd0384ef2012-10-12 12:15:42 -04001793void TWPartitionManager::UnMount_Main_Partitions(void) {
1794 // Unmounts system and data if data is not data/media
1795 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001796 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001797
1798 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1799
1800 UnMount_By_Path("/system", true);
1801#ifndef RECOVERY_SDCARD_ON_DATA
1802 UnMount_By_Path("/data", true);
1803#endif
1804 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1805 Boot_Partition->UnMount(true);
1806}
1807
Dees_Troy9350b8d2012-09-27 12:38:38 -04001808int TWPartitionManager::Partition_SDCard(void) {
1809 char mkdir_path[255], temp[255], line[512];
1810 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1811 int ext, swap, total_size = 0, fat_size;
1812 FILE* fp;
1813
Dees_Troy2673cec2013-04-02 20:22:16 +00001814 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001815#ifdef TW_EXTERNAL_STORAGE_PATH
1816 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1817#else
1818 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1819#endif
1820 if (SDCard == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001821 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001822 return false;
1823 }
1824 if (!SDCard->UnMount(true))
1825 return false;
1826 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1827 if (SDext != NULL) {
1828 if (!SDext->UnMount(true))
1829 return false;
1830 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001831
1832 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001833 Device = SDCard->Actual_Block_Device;
1834 // Just use the root block device
1835 Device.resize(strlen("/dev/block/mmcblkX"));
1836
1837 // Find the size of the block device:
1838 fp = fopen("/proc/partitions", "rt");
1839 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001840 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001841 return false;
1842 }
1843
1844 while (fgets(line, sizeof(line), fp) != NULL)
1845 {
1846 unsigned long major, minor, blocks;
1847 char device[512];
1848 char tmpString[64];
1849
1850 if (strlen(line) < 7 || line[0] == 'm') continue;
1851 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1852
1853 tmpdevice = "/dev/block/";
1854 tmpdevice += device;
1855 if (tmpdevice == Device) {
1856 // Adjust block size to byte size
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001857 total_size = (int)(blocks * 1024ULL / 1000000LLU);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001858 break;
1859 }
1860 }
1861 fclose(fp);
1862
1863 DataManager::GetValue("tw_sdext_size", ext);
1864 DataManager::GetValue("tw_swap_size", swap);
1865 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1866 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001867 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 -04001868 memset(temp, 0, sizeof(temp));
1869 sprintf(temp, "%i", fat_size);
1870 fat_str = temp;
1871 memset(temp, 0, sizeof(temp));
1872 sprintf(temp, "%i", fat_size + ext);
1873 ext_str = temp;
1874 memset(temp, 0, sizeof(temp));
1875 sprintf(temp, "%i", fat_size + ext + swap);
1876 swap_str = temp;
1877 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001878 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001879 return false;
1880 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001881 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001882 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001883 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001884 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001885 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001886 Update_System_Details();
1887 return false;
1888 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001889 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001890 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001891 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001892 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001893 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001894 return false;
1895 }
1896 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001897 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001898 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001899 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001900 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001901 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001902 Update_System_Details();
1903 return false;
1904 }
1905 }
1906 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001907 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001908 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001909 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001910 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001911 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001912 Update_System_Details();
1913 return false;
1914 }
1915 }
1916 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1917#ifdef TW_EXTERNAL_STORAGE_PATH
1918 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1919 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1920 memset(mkdir_path, 0, sizeof(mkdir_path));
1921 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1922#else
1923 Mount_By_Path("/sdcard", 1);
1924 strcpy(mkdir_path, "/sdcard/TWRP");
1925#endif
1926 mkdir(mkdir_path, 0777);
1927 DataManager::Flush();
1928#ifdef TW_EXTERNAL_STORAGE_PATH
1929 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1930 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1931 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1932#else
1933 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1934 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1935 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1936#endif
1937 if (ext > 0) {
1938 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001939 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001940 return false;
1941 }
1942 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001943 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1944 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001945 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001946 }
1947
1948 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001949 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001950 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001951}
Dees_Troya13d74f2013-03-24 08:54:55 -05001952
1953void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1954 std::vector<TWPartition*>::iterator iter;
1955 if (ListType == "mount") {
1956 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1957 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1958 struct PartitionList part;
1959 part.Display_Name = (*iter)->Display_Name;
1960 part.Mount_Point = (*iter)->Mount_Point;
1961 part.selected = (*iter)->Is_Mounted();
1962 Partition_List->push_back(part);
1963 }
1964 }
1965 } else if (ListType == "storage") {
1966 char free_space[255];
1967 string Current_Storage = DataManager::GetCurrentStoragePath();
1968 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1969 if ((*iter)->Is_Storage) {
1970 struct PartitionList part;
1971 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1972 part.Display_Name = (*iter)->Storage_Name + " (";
1973 part.Display_Name += free_space;
1974 part.Display_Name += "MB)";
1975 part.Mount_Point = (*iter)->Storage_Path;
1976 if ((*iter)->Storage_Path == Current_Storage)
1977 part.selected = 1;
1978 else
1979 part.selected = 0;
1980 Partition_List->push_back(part);
1981 }
1982 }
1983 } else if (ListType == "backup") {
1984 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001985 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001986 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001987 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001988 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001989 Backup_Size = (*iter)->Backup_Size;
1990 if ((*iter)->Has_SubPartition) {
1991 std::vector<TWPartition*>::iterator subpart;
1992
1993 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1994 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1995 Backup_Size += (*subpart)->Backup_Size;
1996 }
1997 }
1998 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001999 part.Display_Name = (*iter)->Backup_Display_Name + " (";
2000 part.Display_Name += backup_size;
2001 part.Display_Name += "MB)";
2002 part.Mount_Point = (*iter)->Backup_Path;
2003 part.selected = 0;
2004 Partition_List->push_back(part);
2005 }
2006 }
2007 } else if (ListType == "restore") {
2008 string Restore_List, restore_path;
2009 TWPartition* restore_part = NULL;
2010
2011 DataManager::GetValue("tw_restore_list", Restore_List);
2012 if (!Restore_List.empty()) {
2013 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
2014 while (end_pos != string::npos && start_pos < Restore_List.size()) {
2015 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05002016 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00002017 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002018 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05002019 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05002020 } else {
2021 struct PartitionList part;
2022 part.Display_Name = restore_part->Backup_Display_Name;
2023 part.Mount_Point = restore_part->Backup_Path;
2024 part.selected = 1;
2025 Partition_List->push_back(part);
2026 }
2027 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002028 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002029 }
2030 start_pos = end_pos + 1;
2031 end_pos = Restore_List.find(";", start_pos);
2032 }
2033 }
2034 } else if (ListType == "wipe") {
2035 struct PartitionList dalvik;
2036 dalvik.Display_Name = "Dalvik Cache";
2037 dalvik.Mount_Point = "DALVIK";
2038 dalvik.selected = 0;
2039 Partition_List->push_back(dalvik);
2040 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2041 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
2042 struct PartitionList part;
2043 part.Display_Name = (*iter)->Display_Name;
2044 part.Mount_Point = (*iter)->Mount_Point;
2045 part.selected = 0;
2046 Partition_List->push_back(part);
2047 }
2048 if ((*iter)->Has_Android_Secure) {
2049 struct PartitionList part;
2050 part.Display_Name = (*iter)->Backup_Display_Name;
2051 part.Mount_Point = (*iter)->Backup_Path;
2052 part.selected = 0;
2053 Partition_List->push_back(part);
2054 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00002055 if ((*iter)->Has_Data_Media) {
2056 struct PartitionList datamedia;
2057 datamedia.Display_Name = (*iter)->Storage_Name;
2058 datamedia.Mount_Point = "INTERNAL";
2059 datamedia.selected = 0;
2060 Partition_List->push_back(datamedia);
2061 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002062 }
2063 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002064 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002065 }
2066}
2067
2068int TWPartitionManager::Fstab_Processed(void) {
2069 return Partitions.size();
2070}
Dees_Troyd93bda52013-07-03 19:55:19 +00002071
2072void TWPartitionManager::Output_Storage_Fstab(void) {
2073 std::vector<TWPartition*>::iterator iter;
2074 char storage_partition[255];
2075 string Temp;
2076 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
2077
2078 if (fp == NULL) {
2079 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
2080 return;
2081 }
2082
2083 // Iterate through all partitions
2084 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2085 if ((*iter)->Is_Storage) {
2086 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
2087 strcpy(storage_partition, Temp.c_str());
2088 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
2089 }
2090 }
2091 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002092}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002093
2094TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
2095{
2096 TWPartition *res = NULL;
2097 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
2098 if(!(*iter)->Is_Storage)
2099 continue;
2100
2101 if((*iter)->Is_Settings_Storage)
2102 return *iter;
2103
2104 if(!res)
2105 res = *iter;
2106 }
2107 return res;
2108}