blob: 98ba462c2bdae45f8678204cb2a01aaa08cc6a1f [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* Partition Management classes for TWRP
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/vfs.h>
28#include <unistd.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040029#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040030#include <dirent.h>
31#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040032#include <errno.h>
33#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040034
35#include "variables.h"
36#include "common.h"
Dees_Troy093b7642012-09-21 15:59:38 -040037#include "ui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040040#include "twrp-functions.hpp"
41
Dees_Troy5bf43922012-09-07 16:07:55 -040042#ifdef TW_INCLUDE_CRYPTO
43 #ifdef TW_INCLUDE_JB_CRYPTO
44 #include "crypto/jb/cryptfs.h"
45 #else
46 #include "crypto/ics/cryptfs.h"
47 #endif
48 #include "cutils/properties.h"
49#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040050
Dees_Troy093b7642012-09-21 15:59:38 -040051extern RecoveryUI* ui;
52
Dees_Troy51a0e822012-09-05 15:24:24 -040053int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -040054 FILE *fstabFile;
55 char fstab_line[MAX_FSTAB_LINE_LENGTH];
56
57 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
58 if (fstabFile == NULL) {
59 LOGE("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
60 return false;
61 }
62
63 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
64 if (fstab_line[0] != '/')
65 continue;
66
Dees_Troy2a923582012-09-20 12:13:34 -040067 if (fstab_line[strlen(fstab_line) - 1] != '\n')
68 fstab_line[strlen(fstab_line)] = '\n';
69
Dees_Troy5bf43922012-09-07 16:07:55 -040070 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040071 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040072 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040073
Dees_Troy5bf43922012-09-07 16:07:55 -040074 if (partition->Process_Fstab_Line(line, Display_Error)) {
75 Partitions.push_back(partition);
76 } else {
77 delete partition;
78 }
79 }
80 fclose(fstabFile);
81 if (!Write_Fstab()) {
82 if (Display_Error)
83 LOGE("Error creating fstab\n");
84 else
85 LOGI("Error creating fstab\n");
86 }
Dees_Troy51127312012-09-08 13:08:49 -040087 Update_System_Details();
Dees_Troy5bf43922012-09-07 16:07:55 -040088 return true;
89}
90
91int TWPartitionManager::Write_Fstab(void) {
92 FILE *fp;
93 std::vector<TWPartition*>::iterator iter;
94 string Line;
95
96 fp = fopen("/etc/fstab", "w");
97 if (fp == NULL) {
Dees_Troy63c8df72012-09-10 14:02:05 -040098 LOGI("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -040099 return false;
100 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400101 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400102 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400103 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400104 fputs(Line.c_str(), fp);
Dees_Troy51127312012-09-08 13:08:49 -0400105 // Handle subpartition tracking
106 if ((*iter)->Is_SubPartition) {
107 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
108 if (ParentPartition)
109 ParentPartition->Has_SubPartition = true;
110 else
111 LOGE("Unable to locate parent partition '%s' of '%s'\n", (*iter)->SubPartition_Of.c_str(), (*iter)->Mount_Point.c_str());
112 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400113 }
114 }
115 fclose(fp);
116 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400117}
118
Dees_Troy8170a922012-09-18 15:40:25 -0400119void TWPartitionManager::Output_Partition_Logging(void) {
120 std::vector<TWPartition*>::iterator iter;
121
122 printf("\n\nPartition Logs:\n");
123 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
124 Output_Partition((*iter));
125}
126
127void TWPartitionManager::Output_Partition(TWPartition* Part) {
128 unsigned long long mb = 1048576;
129
130 if (Part->Can_Be_Mounted) {
131 printf("%s | %s | Size: %iMB Used: %iMB Free: %iMB Backup Size: %iMB\n Flags: ", Part->Mount_Point.c_str(), Part->Actual_Block_Device.c_str(), (int)(Part->Size / mb), (int)(Part->Used / mb), (int)(Part->Free / mb), (int)(Part->Backup_Size / mb));
132 if (Part->Can_Be_Wiped)
133 printf("Can_Be_Wiped ");
134 if (Part->Wipe_During_Factory_Reset)
135 printf("Wipe_During_Factory_Reset ");
136 if (Part->Wipe_Available_in_GUI)
137 printf("Wipe_Available_in_GUI ");
138 if (Part->Is_SubPartition)
139 printf("Is_SubPartition ");
140 if (Part->Has_SubPartition)
141 printf("Has_SubPartition ");
142 if (Part->Removable)
143 printf("Removable ");
144 if (Part->Is_Present)
145 printf("IsPresent ");
146 if (Part->Can_Be_Encrypted)
147 printf("Can_Be_Encrypted ");
148 if (Part->Is_Encrypted)
149 printf("Is_Encrypted ");
150 if (Part->Is_Decrypted)
151 printf("Is_Decrypted ");
152 if (Part->Has_Data_Media)
153 printf("Has_Data_Media ");
Dees_Troye58d5262012-09-21 12:27:57 -0400154 if (Part->Has_Android_Secure)
155 printf("Has_Android_Secure ");
Dees_Troy8170a922012-09-18 15:40:25 -0400156 if (Part->Is_Storage)
157 printf("Is_Storage ");
158 printf("\n");
159 if (!Part->SubPartition_Of.empty())
160 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
161 if (!Part->Symlink_Path.empty())
162 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
163 if (!Part->Symlink_Mount_Point.empty())
164 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
165 if (!Part->Primary_Block_Device.empty())
166 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
167 if (!Part->Alternate_Block_Device.empty())
168 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
169 if (!Part->Decrypted_Block_Device.empty())
170 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
171 if (Part->Length != 0)
172 printf(" Length: %i\n", Part->Length);
173 if (!Part->Display_Name.empty())
174 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400175 if (!Part->Backup_Path.empty())
176 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400177 if (!Part->Backup_Name.empty())
178 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
179 if (!Part->Backup_FileName.empty())
180 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400181 if (!Part->Storage_Path.empty())
182 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
183 if (!Part->Current_File_System.empty())
184 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
185 if (!Part->Fstab_File_System.empty())
186 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
187 if (Part->Format_Block_Size != 0)
188 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
189 } else {
190 printf("%s | %s | Size: %iMB\n", Part->Mount_Point.c_str(), Part->Actual_Block_Device.c_str(), (int)(Part->Size / mb));
191 }
Dees_Troy094207a2012-09-26 12:00:39 -0400192 if (!Part->MTD_Name.empty())
193 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400194 string back_meth = Part->Backup_Method_By_Name();
195 printf(" Backup_Method: %s\n\n", back_meth.c_str());
196}
197
Dees_Troy51a0e822012-09-05 15:24:24 -0400198int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400199 std::vector<TWPartition*>::iterator iter;
200 int ret = false;
201 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400202 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400203
Dees_Troy43d8b002012-09-17 16:00:01 -0400204 if (Local_Path == "/tmp")
205 return true;
206
Dees_Troy5bf43922012-09-07 16:07:55 -0400207 // Iterate through all partitions
208 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400209 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400210 ret = (*iter)->Mount(Display_Error);
211 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400212 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400213 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400214 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 }
216 if (found) {
217 return ret;
218 } else if (Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400219 LOGE("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 } else {
Dees_Troy51127312012-09-08 13:08:49 -0400221 LOGI("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 }
223 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400224}
225
226int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400227 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400228
Dees_Troy51127312012-09-08 13:08:49 -0400229 if (Part) {
230 if (Part->Has_SubPartition) {
231 std::vector<TWPartition*>::iterator subpart;
232
233 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
234 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
235 (*subpart)->Mount(Display_Error);
236 }
237 return Part->Mount(Display_Error);
238 } else
239 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400240 }
241 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400242 LOGE("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400243 else
Dees_Troy51127312012-09-08 13:08:49 -0400244 LOGI("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400245 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400246}
247
248int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400249 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400250
Dees_Troy51127312012-09-08 13:08:49 -0400251 if (Part) {
252 if (Part->Has_SubPartition) {
253 std::vector<TWPartition*>::iterator subpart;
254
255 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
256 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
257 (*subpart)->Mount(Display_Error);
258 }
259 return Part->Mount(Display_Error);
260 } else
261 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 }
263 if (Display_Error)
Dees_Troy51127312012-09-08 13:08:49 -0400264 LOGE("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400265 else
Dees_Troy51127312012-09-08 13:08:49 -0400266 LOGI("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400267 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400268}
269
270int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400271 std::vector<TWPartition*>::iterator iter;
272 int ret = false;
273 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400274 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400275
276 // Iterate through all partitions
277 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400278 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400279 ret = (*iter)->UnMount(Display_Error);
280 found = true;
281 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
282 (*iter)->UnMount(Display_Error);
283 }
284 }
285 if (found) {
286 return ret;
287 } else if (Display_Error) {
288 LOGE("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
289 } else {
290 LOGI("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
291 }
292 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400293}
294
295int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400296 TWPartition* Part = Find_Partition_By_Block(Block);
297
298 if (Part) {
299 if (Part->Has_SubPartition) {
300 std::vector<TWPartition*>::iterator subpart;
301
302 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
303 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
304 (*subpart)->UnMount(Display_Error);
305 }
306 return Part->UnMount(Display_Error);
307 } else
308 return Part->UnMount(Display_Error);
309 }
310 if (Display_Error)
311 LOGE("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
312 else
313 LOGI("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
314 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400315}
316
317int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400318 TWPartition* Part = Find_Partition_By_Name(Name);
319
320 if (Part) {
321 if (Part->Has_SubPartition) {
322 std::vector<TWPartition*>::iterator subpart;
323
324 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
325 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
326 (*subpart)->UnMount(Display_Error);
327 }
328 return Part->UnMount(Display_Error);
329 } else
330 return Part->UnMount(Display_Error);
331 }
332 if (Display_Error)
333 LOGE("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
334 else
335 LOGI("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
336 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400337}
338
339int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400340 TWPartition* Part = Find_Partition_By_Path(Path);
341
342 if (Part)
343 return Part->Is_Mounted();
344 else
345 LOGI("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
346 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400347}
348
349int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400350 TWPartition* Part = Find_Partition_By_Block(Block);
351
352 if (Part)
353 return Part->Is_Mounted();
354 else
355 LOGI("Is_Mounted: Unable to find partition for block '%s'\n", Block.c_str());
356 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400357}
358
359int TWPartitionManager::Is_Mounted_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400360 TWPartition* Part = Find_Partition_By_Name(Name);
361
362 if (Part)
363 return Part->Is_Mounted();
364 else
365 LOGI("Is_Mounted: Unable to find partition for name '%s'\n", Name.c_str());
366 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400367}
368
Dees_Troy5bf43922012-09-07 16:07:55 -0400369int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400370 string current_storage_path = DataManager::GetCurrentStoragePath();
371
372 if (Mount_By_Path(current_storage_path, Display_Error)) {
373 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
374 if (FreeStorage)
375 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
376 return true;
377 }
378 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400379}
380
Dees_Troy5bf43922012-09-07 16:07:55 -0400381int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
382 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
383}
384
385TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
386 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400387 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400388
389 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400390 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400391 return (*iter);
392 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400393 return NULL;
394}
395
Dees_Troy5bf43922012-09-07 16:07:55 -0400396TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400397 std::vector<TWPartition*>::iterator iter;
398
399 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400400 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 -0400401 return (*iter);
402 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400403 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400404}
405
406TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400407 std::vector<TWPartition*>::iterator iter;
408
409 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
410 if ((*iter)->Display_Name == Name)
411 return (*iter);
412 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400413 return NULL;
414}
Dees_Troy51a0e822012-09-05 15:24:24 -0400415
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400416int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
417 // Check the backup name to ensure that it is the correct size and contains only valid characters
418 // and that a backup with that name doesn't already exist
419 char backup_name[MAX_BACKUP_NAME_LEN];
420 char backup_loc[255], tw_image_dir[255];
421 int copy_size;
422 int index, cur_char;
423 string Backup_Name, Backup_Loc;
424
425 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
426 copy_size = Backup_Name.size();
427 // Check size
428 if (copy_size > MAX_BACKUP_NAME_LEN) {
429 if (Display_Error)
430 LOGE("Backup name is too long.\n");
431 return -2;
432 }
433
434 // Check each character
435 strncpy(backup_name, Backup_Name.c_str(), copy_size);
436 if (strcmp(backup_name, "0") == 0)
437 return 0; // A "0" (zero) means to use the current timestamp for the backup name
438 for (index=0; index<copy_size; index++) {
439 cur_char = (int)backup_name[index];
440 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) {
441 // These are valid characters
442 // Numbers
443 // Upper case letters
444 // Lower case letters
445 // Space
446 // and -_.{}[]
447 } else {
448 if (Display_Error)
449 LOGE("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
450 return -3;
451 }
452 }
453
454 // Check to make sure that a backup with this name doesn't already exist
455 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
456 strcpy(backup_loc, Backup_Loc.c_str());
457 sprintf(tw_image_dir,"%s/%s/.", backup_loc, backup_name);
458 if (TWFunc::Path_Exists(tw_image_dir)) {
459 if (Display_Error)
460 LOGE("A backup with this name already exists.\n");
461 return -4;
462 }
463
464 // No problems found, return 0
465 return 0;
466}
467
Dees_Troy43d8b002012-09-17 16:00:01 -0400468bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
469{
470 char command[512];
471 string Full_File = Backup_Folder + Backup_Filename;
472
Dees_Troy4a2a1262012-09-18 09:33:47 -0400473 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400474 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400475
Dees_Troyb46a6842012-09-25 11:06:46 -0400476 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troyc51f1f92012-09-20 15:32:13 -0400477 ui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400478
479 if (TWFunc::Path_Exists(Full_File)) {
480 sprintf(command, "cd '%s' && md5sum %s > %s.md5",Backup_Folder.c_str(), Backup_Filename.c_str(), Backup_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400481 if (system(command) == 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400482 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400483 return true;
484 } else {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400485 ui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400486 return false;
487 }
488 } else {
489 char filename[512];
490 int index = 0;
491
492 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400493 while (TWFunc::Path_Exists(filename) == true) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400494 sprintf(command, "cd '%s' && md5sum %s%03i > %s%03i.md5",Backup_Folder.c_str(), Backup_Filename.c_str(), index, Backup_Filename.c_str(), index);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400495 if (system(command) != 0) {
Dees_Troyc5865ab2012-09-24 15:08:04 -0400496 ui_print(" * MD5 Error.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400497 return false;
498 }
499 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400500 sprintf(filename, "%s%03i", Full_File.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400501 }
502 if (index == 0) {
503 LOGE("Backup file: '%s' not found!\n", filename);
504 return false;
505 }
Dees_Troyc5865ab2012-09-24 15:08:04 -0400506 ui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400507 }
508 return true;
509}
510
Dees_Troy093b7642012-09-21 15:59:38 -0400511bool 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 -0400512 time_t start, stop;
Dees_Troy093b7642012-09-21 15:59:38 -0400513 int img_bps, file_bps;
514 unsigned long total_time, remain_time, section_time;
515 int use_compression, backup_time;
516 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400517
518 if (Part == NULL)
519 return true;
520
Dees_Troy093b7642012-09-21 15:59:38 -0400521 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
522
523 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
524 if (use_compression)
525 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
526 else
527 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
528
529 // We know the speed for both, how far into the whole backup are we, based on time
530 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
531 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
532
533 pos = (total_time - remain_time) / (float) total_time;
534 ui->SetProgress(pos);
535
536 LOGI("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
537
538 // And get the time
539 if (Part->Backup_Method == 1)
540 section_time = Part->Backup_Size / file_bps;
541 else
542 section_time = Part->Backup_Size / img_bps;
543
544 // Set the position
545 pos = section_time / (float) total_time;
546 ui->ShowProgress(pos, section_time);
547
Dees_Troy43d8b002012-09-17 16:00:01 -0400548 time(&start);
549
550 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400551 if (Part->Has_SubPartition) {
552 std::vector<TWPartition*>::iterator subpart;
553
554 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
555 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
556 if (!(*subpart)->Backup(Backup_Folder))
557 return false;
558 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
559 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400560 if (Part->Backup_Method == 1) {
561 *file_bytes_remaining -= (*subpart)->Backup_Size;
562 } else {
563 *img_bytes_remaining -= (*subpart)->Backup_Size;
564 }
Dees_Troy8170a922012-09-18 15:40:25 -0400565 }
566 }
567 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400568 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400569 backup_time = (int) difftime(stop, start);
570 LOGI("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400571 if (Part->Backup_Method == 1) {
572 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400573 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400574 } else {
575 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400576 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400577 }
578 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
579 } else {
580 return false;
581 }
582}
583
584int TWPartitionManager::Run_Backup(void) {
585 int check, do_md5, partition_count = 0;
586 string Backup_Folder, Backup_Name, Full_Backup_Path;
Dees_Troy8170a922012-09-18 15:40:25 -0400587 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 -0400588 unsigned long img_time = 0, file_time = 0;
589 TWPartition* backup_sys = NULL;
590 TWPartition* backup_data = NULL;
591 TWPartition* backup_cache = NULL;
592 TWPartition* backup_recovery = NULL;
593 TWPartition* backup_boot = NULL;
594 TWPartition* backup_andsec = NULL;
595 TWPartition* backup_sdext = NULL;
596 TWPartition* backup_sp1 = NULL;
597 TWPartition* backup_sp2 = NULL;
598 TWPartition* backup_sp3 = NULL;
599 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400600 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400601 struct tm *t;
602 time_t start, stop, seconds, total_start, total_stop;
603 seconds = time(0);
604 t = localtime(&seconds);
605
606 time(&total_start);
607
608 Update_System_Details();
609
610 if (!Mount_Current_Storage(true))
611 return false;
612
613 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400614 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400615 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400616 else
617 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400618
Dees_Troy43d8b002012-09-17 16:00:01 -0400619 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
620 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400621 if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name.empty()) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400622 char timestamp[255];
623 sprintf(timestamp,"%04d-%02d-%02d--%02d-%02d-%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
624 Backup_Name = timestamp;
625 }
626 LOGI("Backup Name is: '%s'\n", Backup_Name.c_str());
627 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
628 LOGI("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
629
630 ui_print("\n[BACKUP STARTED]\n");
631 ui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
632 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
633 LOGE("Failed to make backup folder.\n");
634 return false;
635 }
636
637 LOGI("Calculating backup details...\n");
638 DataManager::GetValue(TW_BACKUP_SYSTEM_VAR, check);
639 if (check) {
640 backup_sys = Find_Partition_By_Path("/system");
641 if (backup_sys != NULL) {
642 partition_count++;
643 if (backup_sys->Backup_Method == 1)
644 file_bytes += backup_sys->Backup_Size;
645 else
646 img_bytes += backup_sys->Backup_Size;
647 } else {
648 LOGE("Unable to locate system partition.\n");
649 return false;
650 }
651 }
652 DataManager::GetValue(TW_BACKUP_DATA_VAR, check);
653 if (check) {
654 backup_data = Find_Partition_By_Path("/data");
655 if (backup_data != NULL) {
656 partition_count++;
Dees_Troy8170a922012-09-18 15:40:25 -0400657 subpart_size = 0;
658 if (backup_data->Has_SubPartition) {
659 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
660 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == backup_data->Mount_Point)
661 subpart_size += (*subpart)->Backup_Size;
662 }
663 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400664 if (backup_data->Backup_Method == 1)
Dees_Troy8170a922012-09-18 15:40:25 -0400665 file_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400666 else
Dees_Troy8170a922012-09-18 15:40:25 -0400667 img_bytes += backup_data->Backup_Size + subpart_size;
Dees_Troy43d8b002012-09-17 16:00:01 -0400668 } else {
669 LOGE("Unable to locate data partition.\n");
670 return false;
671 }
672 }
673 DataManager::GetValue(TW_BACKUP_CACHE_VAR, check);
674 if (check) {
675 backup_cache = Find_Partition_By_Path("/cache");
676 if (backup_cache != NULL) {
677 partition_count++;
678 if (backup_cache->Backup_Method == 1)
679 file_bytes += backup_cache->Backup_Size;
680 else
681 img_bytes += backup_cache->Backup_Size;
682 } else {
683 LOGE("Unable to locate cache partition.\n");
684 return false;
685 }
686 }
687 DataManager::GetValue(TW_BACKUP_RECOVERY_VAR, check);
688 if (check) {
689 backup_recovery = Find_Partition_By_Path("/recovery");
690 if (backup_recovery != NULL) {
691 partition_count++;
692 if (backup_recovery->Backup_Method == 1)
693 file_bytes += backup_recovery->Backup_Size;
694 else
695 img_bytes += backup_recovery->Backup_Size;
696 } else {
697 LOGE("Unable to locate recovery partition.\n");
698 return false;
699 }
700 }
701 DataManager::GetValue(TW_BACKUP_BOOT_VAR, check);
702 if (check) {
703 backup_boot = Find_Partition_By_Path("/boot");
704 if (backup_boot != NULL) {
705 partition_count++;
706 if (backup_boot->Backup_Method == 1)
707 file_bytes += backup_boot->Backup_Size;
708 else
709 img_bytes += backup_boot->Backup_Size;
710 } else {
711 LOGE("Unable to locate boot partition.\n");
712 return false;
713 }
714 }
715 DataManager::GetValue(TW_BACKUP_ANDSEC_VAR, check);
716 if (check) {
717 backup_andsec = Find_Partition_By_Path("/and-sec");
718 if (backup_andsec != NULL) {
719 partition_count++;
720 if (backup_andsec->Backup_Method == 1)
721 file_bytes += backup_andsec->Backup_Size;
722 else
723 img_bytes += backup_andsec->Backup_Size;
724 } else {
725 LOGE("Unable to locate android secure partition.\n");
726 return false;
727 }
728 }
729 DataManager::GetValue(TW_BACKUP_SDEXT_VAR, check);
730 if (check) {
731 backup_sdext = Find_Partition_By_Path("/sd-ext");
732 if (backup_sdext != NULL) {
733 partition_count++;
734 if (backup_sdext->Backup_Method == 1)
735 file_bytes += backup_sdext->Backup_Size;
736 else
737 img_bytes += backup_sdext->Backup_Size;
738 } else {
739 LOGE("Unable to locate sd-ext partition.\n");
740 return false;
741 }
742 }
743#ifdef SP1_NAME
744 DataManager::GetValue(TW_BACKUP_SP1_VAR, check);
745 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400746 backup_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400747 if (backup_sp1 != NULL) {
748 partition_count++;
749 if (backup_sp1->Backup_Method == 1)
750 file_bytes += backup_sp1->Backup_Size;
751 else
752 img_bytes += backup_sp1->Backup_Size;
753 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400754 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400755 return false;
756 }
757 }
758#endif
759#ifdef SP2_NAME
760 DataManager::GetValue(TW_BACKUP_SP2_VAR, check);
761 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400762 backup_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400763 if (backup_sp2 != NULL) {
764 partition_count++;
765 if (backup_sp2->Backup_Method == 1)
766 file_bytes += backup_sp2->Backup_Size;
767 else
768 img_bytes += backup_sp2->Backup_Size;
769 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400770 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400771 return false;
772 }
773 }
774#endif
775#ifdef SP3_NAME
776 DataManager::GetValue(TW_BACKUP_SP3_VAR, check);
777 if (check) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400778 backup_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400779 if (backup_sp3 != NULL) {
780 partition_count++;
781 if (backup_sp3->Backup_Method == 1)
782 file_bytes += backup_sp3->Backup_Size;
783 else
784 img_bytes += backup_sp3->Backup_Size;
785 } else {
Dees_Troyab10ee22012-09-21 14:27:30 -0400786 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troy43d8b002012-09-17 16:00:01 -0400787 return false;
788 }
789 }
790#endif
791
792 if (partition_count == 0) {
793 ui_print("No partitions selected for backup.\n");
794 return false;
795 }
796 total_bytes = file_bytes + img_bytes;
797 ui_print(" * Total number of partitions to back up: %d\n", partition_count);
798 ui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
799 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
800 if (storage != NULL) {
801 free_space = storage->Free;
802 ui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
803 } else {
804 LOGE("Unable to locate storage device.\n");
805 return false;
806 }
807 if (free_space + (32 * 1024 * 1024) < total_bytes) {
808 // We require an extra 32MB just in case
809 LOGE("Not enough free space on storage.\n");
810 return false;
811 }
812 img_bytes_remaining = img_bytes;
813 file_bytes_remaining = file_bytes;
814
Dees_Troy093b7642012-09-21 15:59:38 -0400815 ui->SetProgress(0.0);
816
817 if (!Backup_Partition(backup_sys, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400818 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400819 if (!Backup_Partition(backup_data, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400820 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400821 if (!Backup_Partition(backup_cache, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400822 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400823 if (!Backup_Partition(backup_recovery, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400824 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400825 if (!Backup_Partition(backup_boot, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400826 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400827 if (!Backup_Partition(backup_andsec, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400828 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400829 if (!Backup_Partition(backup_sdext, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400830 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400831 if (!Backup_Partition(backup_sp1, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400832 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400833 if (!Backup_Partition(backup_sp2, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400834 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400835 if (!Backup_Partition(backup_sp3, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
Dees_Troy43d8b002012-09-17 16:00:01 -0400836 return false;
837
838 // Average BPS
839 if (img_time == 0)
840 img_time = 1;
841 if (file_time == 0)
842 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400843 int img_bps = (int)img_bytes / (int)img_time;
844 int file_bps = (int)file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400845
846 ui_print("Average backup rate for file systems: %lu MB/sec\n", (file_bps / (1024 * 1024)));
847 ui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
848
849 time(&total_stop);
850 int total_time = (int) difftime(total_stop, total_start);
851 unsigned long long actual_backup_size = TWFunc::Get_Folder_Size(Full_Backup_Path, true);
852 actual_backup_size /= (1024LLU * 1024LLU);
853
Dees_Troy093b7642012-09-21 15:59:38 -0400854 int prev_img_bps, prev_file_bps, use_compression;
855 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
856 img_bps += (prev_img_bps * 4);
857 img_bps /= 5;
858
859 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
860 if (use_compression)
861 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
862 else
863 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
864 file_bps += (prev_file_bps * 4);
865 file_bps /= 5;
866
867 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
868 if (use_compression)
869 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
870 else
871 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
872
Dees_Troy43d8b002012-09-17 16:00:01 -0400873 ui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
874 Update_System_Details();
875 ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
876 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400877}
878
Dees_Troy093b7642012-09-21 15:59:38 -0400879bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400880 time_t Start, Stop;
881 time(&Start);
Dees_Troy093b7642012-09-21 15:59:38 -0400882 ui->ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400883 if (!Part->Restore(Restore_Name))
884 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400885 if (Part->Has_SubPartition) {
886 std::vector<TWPartition*>::iterator subpart;
887
888 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
889 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
890 if (!(*subpart)->Restore(Restore_Name))
891 return false;
892 }
893 }
894 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400895 time(&Stop);
896 ui_print("[%s done (%d seconds)]\n\n", Part->Display_Name.c_str(), (int)difftime(Stop, Start));
897 return true;
898}
899
Dees_Troy51a0e822012-09-05 15:24:24 -0400900int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400901 int check_md5, check, partition_count = 0;
902 TWPartition* restore_sys = NULL;
903 TWPartition* restore_data = NULL;
904 TWPartition* restore_cache = NULL;
905 TWPartition* restore_boot = NULL;
906 TWPartition* restore_andsec = NULL;
907 TWPartition* restore_sdext = NULL;
908 TWPartition* restore_sp1 = NULL;
909 TWPartition* restore_sp2 = NULL;
910 TWPartition* restore_sp3 = NULL;
911 time_t rStart, rStop;
912 time(&rStart);
Dees_Troy43d8b002012-09-17 16:00:01 -0400913
Dees_Troy4a2a1262012-09-18 09:33:47 -0400914 ui_print("\n[RESTORE STARTED]\n\n");
915 ui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400916
Dees_Troy4a2a1262012-09-18 09:33:47 -0400917 if (!Mount_Current_Storage(true))
918 return false;
919
920 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
921 DataManager::GetValue(TW_RESTORE_SYSTEM_VAR, check);
Dees_Troy63c8df72012-09-10 14:02:05 -0400922 if (check > 0) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400923 restore_sys = Find_Partition_By_Path("/system");
924 if (restore_sys == NULL) {
925 LOGE("Unable to locate system partition.\n");
926 return false;
927 }
928 partition_count++;
929 }
930 DataManager::GetValue(TW_RESTORE_DATA_VAR, check);
931 if (check > 0) {
932 restore_data = Find_Partition_By_Path("/data");
933 if (restore_data == NULL) {
934 LOGE("Unable to locate data partition.\n");
935 return false;
936 }
937 partition_count++;
938 }
939 DataManager::GetValue(TW_RESTORE_CACHE_VAR, check);
940 if (check > 0) {
941 restore_cache = Find_Partition_By_Path("/cache");
942 if (restore_cache == NULL) {
943 LOGE("Unable to locate cache partition.\n");
944 return false;
945 }
946 partition_count++;
947 }
948 DataManager::GetValue(TW_RESTORE_BOOT_VAR, check);
949 if (check > 0) {
950 restore_boot = Find_Partition_By_Path("/boot");
951 if (restore_boot == NULL) {
952 LOGE("Unable to locate boot partition.\n");
953 return false;
954 }
955 partition_count++;
956 }
957 DataManager::GetValue(TW_RESTORE_ANDSEC_VAR, check);
958 if (check > 0) {
959 restore_andsec = Find_Partition_By_Path("/and-sec");
960 if (restore_andsec == NULL) {
961 LOGE("Unable to locate android secure partition.\n");
962 return false;
963 }
964 partition_count++;
965 }
966 DataManager::GetValue(TW_RESTORE_SDEXT_VAR, check);
967 if (check > 0) {
968 restore_sdext = Find_Partition_By_Path("/sd-ext");
969 if (restore_sdext == NULL) {
970 LOGE("Unable to locate sd-ext partition.\n");
971 return false;
972 }
973 partition_count++;
974 }
975#ifdef SP1_NAME
976 DataManager::GetValue(TW_RESTORE_SP1_VAR, check);
977 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400978 restore_sp1 = Find_Partition_By_Path(EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400979 if (restore_sp1 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400980 LOGE("Unable to locate %s partition.\n", EXPAND(SP1_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400981 return false;
982 }
983 partition_count++;
984 }
985#endif
986#ifdef SP2_NAME
987 DataManager::GetValue(TW_RESTORE_SP2_VAR, check);
988 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400989 restore_sp2 = Find_Partition_By_Path(EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400990 if (restore_sp2 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -0400991 LOGE("Unable to locate %s partition.\n", EXPAND(SP2_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -0400992 return false;
993 }
994 partition_count++;
995 }
996#endif
997#ifdef SP3_NAME
998 DataManager::GetValue(TW_RESTORE_SP3_VAR, check);
999 if (check > 0) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001000 restore_sp3 = Find_Partition_By_Path(EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001001 if (restore_sp3 == NULL) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001002 LOGE("Unable to locate %s partition.\n", EXPAND(SP3_NAME));
Dees_Troy4a2a1262012-09-18 09:33:47 -04001003 return false;
1004 }
1005 partition_count++;
1006 }
1007#endif
1008
1009 if (partition_count == 0) {
1010 LOGE("No partitions selected for restore.\n");
1011 return false;
1012 }
1013
1014 if (check_md5 > 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001015 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
Dees_Troyb46a6842012-09-25 11:06:46 -04001016 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001017 ui_print("Verifying MD5...\n");
1018 if (restore_sys != NULL && !restore_sys->Check_MD5(Restore_Name))
1019 return false;
1020 if (restore_data != NULL && !restore_data->Check_MD5(Restore_Name))
1021 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001022 if (restore_data != NULL && restore_data->Has_SubPartition) {
1023 std::vector<TWPartition*>::iterator subpart;
1024
1025 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1026 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_data->Mount_Point) {
1027 if (!(*subpart)->Check_MD5(Restore_Name))
1028 return false;
1029 }
1030 }
1031 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001032 if (restore_cache != NULL && !restore_cache->Check_MD5(Restore_Name))
1033 return false;
1034 if (restore_boot != NULL && !restore_boot->Check_MD5(Restore_Name))
1035 return false;
1036 if (restore_andsec != NULL && !restore_andsec->Check_MD5(Restore_Name))
1037 return false;
1038 if (restore_sdext != NULL && !restore_sdext->Check_MD5(Restore_Name))
1039 return false;
1040 if (restore_sp1 != NULL && !restore_sp1->Check_MD5(Restore_Name))
1041 return false;
1042 if (restore_sp2 != NULL && !restore_sp2->Check_MD5(Restore_Name))
1043 return false;
1044 if (restore_sp3 != NULL && !restore_sp3->Check_MD5(Restore_Name))
1045 return false;
1046 ui_print("Done verifying MD5.\n");
1047 } else
1048 ui_print("Skipping MD5 check based on user setting.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001049
Dees_Troy4a2a1262012-09-18 09:33:47 -04001050 ui_print("Restoring %i partitions...\n", partition_count);
Dees_Troy093b7642012-09-21 15:59:38 -04001051 ui->SetProgress(0.0);
1052 if (restore_sys != NULL && !Restore_Partition(restore_sys, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001053 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001054 if (restore_data != NULL && !Restore_Partition(restore_data, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001055 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001056 if (restore_cache != NULL && !Restore_Partition(restore_cache, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001057 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001058 if (restore_boot != NULL && !Restore_Partition(restore_boot, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001059 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001060 if (restore_andsec != NULL && !Restore_Partition(restore_andsec, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001061 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001062 if (restore_sdext != NULL && !Restore_Partition(restore_sdext, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001063 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001064 if (restore_sp1 != NULL && !Restore_Partition(restore_sp1, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001065 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001066 if (restore_sp2 != NULL && !Restore_Partition(restore_sp2, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001067 return false;
Dees_Troy093b7642012-09-21 15:59:38 -04001068 if (restore_sp3 != NULL && !Restore_Partition(restore_sp3, Restore_Name, partition_count))
Dees_Troy4a2a1262012-09-18 09:33:47 -04001069 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001070
Dees_Troyb46a6842012-09-25 11:06:46 -04001071 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -04001072 Update_System_Details();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001073 time(&rStop);
1074 ui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -04001075 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001076}
1077
1078void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001079 // Start with the default values
1080 int tw_restore_system = -1;
1081 int tw_restore_data = -1;
1082 int tw_restore_cache = -1;
1083 int tw_restore_recovery = -1;
1084 int tw_restore_boot = -1;
1085 int tw_restore_andsec = -1;
1086 int tw_restore_sdext = -1;
1087 int tw_restore_sp1 = -1;
1088 int tw_restore_sp2 = -1;
1089 int tw_restore_sp3 = -1;
1090 bool get_date = true;
1091
1092 DIR* d;
1093 d = opendir(Restore_Name.c_str());
1094 if (d == NULL)
1095 {
1096 LOGE("Error opening %s\n", Restore_Name.c_str());
1097 return;
1098 }
1099
1100 struct dirent* de;
1101 while ((de = readdir(d)) != NULL)
1102 {
1103 // Strip off three components
1104 char str[256];
1105 char* label;
1106 char* fstype = NULL;
1107 char* extn = NULL;
1108 char* ptr;
1109
1110 strcpy(str, de->d_name);
1111 if (strlen(str) <= 2)
1112 continue;
1113
1114 if (get_date) {
1115 char file_path[255];
1116 struct stat st;
1117
1118 strcpy(file_path, Restore_Name.c_str());
1119 strcat(file_path, "/");
1120 strcat(file_path, str);
1121 stat(file_path, &st);
1122 string backup_date = ctime((const time_t*)(&st.st_mtime));
1123 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1124 get_date = false;
1125 }
1126
1127 label = str;
1128 ptr = label;
1129 while (*ptr && *ptr != '.') ptr++;
1130 if (*ptr == '.')
1131 {
1132 *ptr = 0x00;
1133 ptr++;
1134 fstype = ptr;
1135 }
1136 while (*ptr && *ptr != '.') ptr++;
1137 if (*ptr == '.')
1138 {
1139 *ptr = 0x00;
1140 ptr++;
1141 extn = ptr;
1142 }
1143
1144 if (extn == NULL || (strlen(extn) >= 3 && strncmp(extn, "win", 3) != 0)) continue;
1145
1146 TWPartition* Part = Find_Partition_By_Path(label);
1147 if (Part == NULL)
1148 {
1149 LOGE(" Unable to locate partition by backup name: '%s'\n", label);
1150 continue;
1151 }
1152
1153 Part->Backup_FileName = de->d_name;
1154 if (strlen(extn) > 3) {
1155 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1156 }
1157
1158 // Now, we just need to find the correct label
Dees_Troye58d5262012-09-21 12:27:57 -04001159 if (Part->Backup_Path == "/system")
Dees_Troy63c8df72012-09-10 14:02:05 -04001160 tw_restore_system = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001161 if (Part->Backup_Path == "/data")
Dees_Troy63c8df72012-09-10 14:02:05 -04001162 tw_restore_data = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001163 if (Part->Backup_Path == "/cache")
Dees_Troy63c8df72012-09-10 14:02:05 -04001164 tw_restore_cache = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001165 if (Part->Backup_Path == "/recovery")
Dees_Troy63c8df72012-09-10 14:02:05 -04001166 tw_restore_recovery = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001167 if (Part->Backup_Path == "/boot")
Dees_Troy63c8df72012-09-10 14:02:05 -04001168 tw_restore_boot = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001169 if (Part->Backup_Path == "/and-sec")
Dees_Troy63c8df72012-09-10 14:02:05 -04001170 tw_restore_andsec = 1;
Dees_Troye58d5262012-09-21 12:27:57 -04001171 if (Part->Backup_Path == "/sd-ext")
Dees_Troy63c8df72012-09-10 14:02:05 -04001172 tw_restore_sdext = 1;
1173#ifdef SP1_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001174 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP1_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001175 tw_restore_sp1 = 1;
1176#endif
1177#ifdef SP2_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001178 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP2_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001179 tw_restore_sp2 = 1;
1180#endif
1181#ifdef SP3_NAME
Dees_Troyab10ee22012-09-21 14:27:30 -04001182 if (Part->Backup_Path == TWFunc::Get_Root_Path(EXPAND(SP3_NAME)))
Dees_Troy63c8df72012-09-10 14:02:05 -04001183 tw_restore_sp3 = 1;
1184#endif
1185 }
1186 closedir(d);
1187
1188 // Set the final values
1189 DataManager::SetValue(TW_RESTORE_SYSTEM_VAR, tw_restore_system);
1190 DataManager::SetValue(TW_RESTORE_DATA_VAR, tw_restore_data);
1191 DataManager::SetValue(TW_RESTORE_CACHE_VAR, tw_restore_cache);
1192 DataManager::SetValue(TW_RESTORE_RECOVERY_VAR, tw_restore_recovery);
1193 DataManager::SetValue(TW_RESTORE_BOOT_VAR, tw_restore_boot);
1194 DataManager::SetValue(TW_RESTORE_ANDSEC_VAR, tw_restore_andsec);
1195 DataManager::SetValue(TW_RESTORE_SDEXT_VAR, tw_restore_sdext);
1196 DataManager::SetValue(TW_RESTORE_SP1_VAR, tw_restore_sp1);
1197 DataManager::SetValue(TW_RESTORE_SP2_VAR, tw_restore_sp2);
1198 DataManager::SetValue(TW_RESTORE_SP3_VAR, tw_restore_sp3);
1199
Dees_Troy51a0e822012-09-05 15:24:24 -04001200 return;
1201}
1202
1203int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001204 std::vector<TWPartition*>::iterator iter;
1205 int ret = false;
1206 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001207 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001208
1209 // Iterate through all partitions
1210 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001211 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001212 if (Path == "/and-sec")
1213 ret = (*iter)->Wipe_AndSec();
1214 else
1215 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001216 found = true;
1217 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1218 (*iter)->Wipe();
1219 }
1220 }
1221 if (found) {
1222 return ret;
1223 } else
1224 LOGE("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
1225 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001226}
1227
1228int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001229 TWPartition* Part = Find_Partition_By_Block(Block);
1230
1231 if (Part) {
1232 if (Part->Has_SubPartition) {
1233 std::vector<TWPartition*>::iterator subpart;
1234
1235 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1236 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1237 (*subpart)->Wipe();
1238 }
1239 return Part->Wipe();
1240 } else
1241 return Part->Wipe();
1242 }
1243 LOGE("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
1244 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001245}
1246
1247int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001248 TWPartition* Part = Find_Partition_By_Name(Name);
1249
1250 if (Part) {
1251 if (Part->Has_SubPartition) {
1252 std::vector<TWPartition*>::iterator subpart;
1253
1254 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1255 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1256 (*subpart)->Wipe();
1257 }
1258 return Part->Wipe();
1259 } else
1260 return Part->Wipe();
1261 }
1262 LOGE("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
1263 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001264}
1265
1266int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001267 std::vector<TWPartition*>::iterator iter;
1268 int ret = true;
1269
1270 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001271 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001272 if (!(*iter)->Wipe())
1273 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001274 } else if ((*iter)->Has_Android_Secure) {
1275 if (!(*iter)->Wipe_AndSec())
1276 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001277 }
1278 }
1279 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001280}
1281
Dees_Troy38bd7602012-09-14 13:33:53 -04001282int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1283 struct stat st;
1284
1285 if (!Mount_By_Path("/data", true))
1286 return false;
1287
1288 if (!Mount_By_Path("/cache", true))
1289 return false;
1290
1291 ui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001292 system("rm -rf /data/dalvik-cache");
Dees_Troy38bd7602012-09-14 13:33:53 -04001293 ui_print("Cleaned: /data/dalvik-cache...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001294 system("rm -rf /cache/dalvik-cache");
Dees_Troy38bd7602012-09-14 13:33:53 -04001295 ui_print("Cleaned: /cache/dalvik-cache...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001296 system("rm -rf /cache/dc");
Dees_Troy38bd7602012-09-14 13:33:53 -04001297 ui_print("Cleaned: /cache/dc\n");
1298
1299 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
1300 if (sdext != NULL) {
1301 if (sdext->Is_Present && sdext->Mount(false)) {
1302 if (stat("/sd-ext/dalvik-cache", &st) == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001303 system("rm -rf /sd-ext/dalvik-cache");
Dees_Troy38bd7602012-09-14 13:33:53 -04001304 ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
1305 }
1306 }
1307 }
1308 ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
1309 return true;
1310}
1311
1312int TWPartitionManager::Wipe_Rotate_Data(void) {
1313 if (!Mount_By_Path("/data", true))
1314 return false;
1315
Dees_Troy8170a922012-09-18 15:40:25 -04001316 system("rm -r /data/misc/akmd*");
1317 system("rm -r /data/misc/rild*");
1318 system("rm -r /data/misc/rild*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001319 ui_print("Rotation data wiped.\n");
1320 return true;
1321}
1322
1323int TWPartitionManager::Wipe_Battery_Stats(void) {
1324 struct stat st;
1325
1326 if (!Mount_By_Path("/data", true))
1327 return false;
1328
1329 if (0 != stat("/data/system/batterystats.bin", &st)) {
1330 ui_print("No Battery Stats Found. No Need To Wipe.\n");
1331 } else {
1332 remove("/data/system/batterystats.bin");
1333 ui_print("Cleared battery stats.\n");
1334 }
1335 return true;
1336}
1337
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001338int TWPartitionManager::Wipe_Android_Secure(void) {
1339 std::vector<TWPartition*>::iterator iter;
1340 int ret = false;
1341 bool found = false;
1342
1343 // Iterate through all partitions
1344 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1345 if ((*iter)->Has_Android_Secure) {
1346 ret = (*iter)->Wipe_AndSec();
1347 found = true;
1348 }
1349 }
1350 if (found) {
1351 return ret;
1352 } else {
1353 LOGE("No android secure partitions found.\n");
1354 }
1355 return false;
1356}
1357
Dees_Troy38bd7602012-09-14 13:33:53 -04001358int TWPartitionManager::Format_Data(void) {
1359 TWPartition* dat = Find_Partition_By_Path("/data");
1360
1361 if (dat != NULL) {
1362 if (!dat->UnMount(true))
1363 return false;
1364
1365 return dat->Wipe_Encryption();
1366 } else {
1367 LOGE("Unable to locate /data.\n");
1368 return false;
1369 }
1370 return false;
1371}
1372
1373int TWPartitionManager::Wipe_Media_From_Data(void) {
1374 TWPartition* dat = Find_Partition_By_Path("/data");
1375
1376 if (dat != NULL) {
1377 if (!dat->Has_Data_Media) {
1378 LOGE("This device does not have /data/media\n");
1379 return false;
1380 }
1381 if (!dat->Mount(true))
1382 return false;
1383
1384 ui_print("Wiping internal storage -- /data/media...\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001385 system("rm -rf /data/media");
1386 system("cd /data && mkdir media && chmod 775 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001387 if (dat->Has_Data_Media) {
1388 dat->Recreate_Media_Folder();
1389 }
1390 return true;
1391 } else {
1392 LOGE("Unable to locate /data.\n");
1393 return false;
1394 }
1395 return false;
1396}
1397
Dees_Troy51a0e822012-09-05 15:24:24 -04001398void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001399 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001400 return;
1401}
1402
1403void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001404 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001405 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001406
Dees_Troy32c8eb82012-09-11 15:28:06 -04001407 ui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001408 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001409 if ((*iter)->Can_Be_Mounted) {
1410 (*iter)->Update_Size(true);
1411 if ((*iter)->Mount_Point == "/system") {
1412 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1413 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1414 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1415 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1416 } else if ((*iter)->Mount_Point == "/cache") {
1417 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1418 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1419 } else if ((*iter)->Mount_Point == "/sd-ext") {
1420 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1421 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1422 if ((*iter)->Backup_Size == 0) {
1423 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1424 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1425 } else
1426 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001427 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001428 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001429 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001430 if ((*iter)->Backup_Size == 0) {
1431 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1432 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1433 } else
1434 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001435 } else if ((*iter)->Mount_Point == "/boot") {
1436 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1437 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1438 if ((*iter)->Backup_Size == 0) {
1439 DataManager::SetValue("tw_has_boot_partition", 0);
1440 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1441 } else
1442 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001443 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001444#ifdef SP1_NAME
1445 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1446 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1447 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1448 }
1449#endif
1450#ifdef SP2_NAME
1451 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1452 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1453 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1454 }
1455#endif
1456#ifdef SP3_NAME
1457 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1458 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1459 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1460 }
1461#endif
Dees_Troy51127312012-09-08 13:08:49 -04001462 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001463 }
Dees_Troy51127312012-09-08 13:08:49 -04001464 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1465 string current_storage_path = DataManager::GetCurrentStoragePath();
1466 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001467 if (FreeStorage != NULL) {
1468 // Attempt to mount storage
1469 if (!FreeStorage->Mount(false)) {
1470 // We couldn't mount storage... check to see if we have dual storage
1471 int has_dual_storage;
1472 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1473 if (has_dual_storage == 1) {
1474 // We have dual storage, see if we're using the internal storage that should always be present
1475 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001476 if (!FreeStorage->Is_Encrypted) {
1477 // Not able to use internal, so error!
1478 LOGE("Unable to mount internal storage.\n");
1479 }
Dees_Troy8170a922012-09-18 15:40:25 -04001480 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1481 } else {
1482 // We were using external, flip to internal
1483 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1484 current_storage_path = DataManager::GetCurrentStoragePath();
1485 FreeStorage = Find_Partition_By_Path(current_storage_path);
1486 if (FreeStorage != NULL) {
1487 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1488 } else {
1489 LOGE("Unable to locate internal storage partition.\n");
1490 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1491 }
1492 }
1493 } else {
1494 // No dual storage and unable to mount storage, error!
1495 LOGE("Unable to mount storage.\n");
1496 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1497 }
1498 } else {
1499 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1500 }
1501 } else {
Dees_Troy51127312012-09-08 13:08:49 -04001502 LOGI("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001503 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001504 if (!Write_Fstab())
1505 LOGE("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001506 return;
1507}
1508
1509int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001510#ifdef TW_INCLUDE_CRYPTO
1511 int ret_val, password_len;
1512 char crypto_blkdev[255], cPassword[255];
1513 size_t result;
1514
1515 property_set("ro.crypto.state", "encrypted");
1516#ifdef TW_INCLUDE_JB_CRYPTO
1517 // No extra flags needed
1518#else
1519 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1520 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1521 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1522 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1523 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1524 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
1525#endif
1526 strcpy(cPassword, Password.c_str());
1527 if (cryptfs_check_passwd(cPassword) != 0) {
1528 LOGE("Failed to decrypt data.\n");
1529 return -1;
1530 }
1531 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1532 if (strcmp(crypto_blkdev, "error") == 0) {
1533 LOGE("Error retrieving decrypted data block device.\n");
1534 } else {
1535 TWPartition* dat = Find_Partition_By_Path("/data");
1536 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001537 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001538 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1539 dat->Is_Decrypted = true;
1540 dat->Decrypted_Block_Device = crypto_blkdev;
Dees_Troy32c8eb82012-09-11 15:28:06 -04001541 ui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy5bf43922012-09-07 16:07:55 -04001542 // Sleep for a bit so that the device will be ready
1543 sleep(1);
1544 Update_System_Details();
1545 } else
1546 LOGE("Unable to locate data partition.\n");
1547 }
1548 return 0;
1549#else
1550 LOGE("No crypto support was compiled into this build.\n");
1551 return -1;
1552#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001553 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001554}
1555
Dees_Troy8170a922012-09-18 15:40:25 -04001556//partial kangbang from system/vold
1557#ifndef CUSTOM_LUN_FILE
1558#define CUSTOM_LUN_FILE "/sys/devices/platform/usb_mass_storage/lun%d/file"
1559#endif
1560
1561int TWPartitionManager::usb_storage_enable(void) {
1562 int fd, has_dual, has_data_media;
1563 char lun_file[255];
1564 TWPartition* Part;
1565 string ext_path;
1566
1567 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1568 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1569 if (has_dual == 1 && has_data_media == 0) {
1570 Part = Find_Partition_By_Path(DataManager::GetSettingsStoragePath());
1571 if (Part == NULL) {
1572 LOGE("Unable to locate volume information.");
1573 return false;
1574 }
1575 if (!Part->UnMount(true))
1576 return false;
1577
1578 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1579 if ((fd = open(lun_file, O_WRONLY)) < 0) {
1580 LOGE("Unable to open ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
1581 return false;
1582 }
1583
1584 if (write(fd, Part->Actual_Block_Device.c_str(), Part->Actual_Block_Device.size()) < 0) {
1585 LOGE("Unable to write to ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
1586 close(fd);
1587 return false;
1588 }
1589 close(fd);
1590
1591 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
1592 Part = Find_Partition_By_Path(ext_path);
1593 if (Part == NULL) {
1594 LOGE("Unable to locate volume information.\n");
1595 return false;
1596 }
1597 if (!Part->UnMount(true))
1598 return false;
1599
1600 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1601 if ((fd = open(lun_file, O_WRONLY)) < 0) {
1602 LOGE("Unable to open ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
1603 return false;
1604 }
1605
1606 if (write(fd, Part->Actual_Block_Device.c_str(), Part->Actual_Block_Device.size()) < 0) {
1607 LOGE("Unable to write to ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
1608 close(fd);
1609 return false;
1610 }
1611 close(fd);
1612 } else {
1613 if (has_data_media == 0)
1614 ext_path = DataManager::GetCurrentStoragePath();
1615 else
1616 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
1617
1618 Part = Find_Partition_By_Path(ext_path);
1619 if (Part == NULL) {
1620 LOGE("Unable to locate volume information.\n");
1621 return false;
1622 }
1623 if (!Part->UnMount(true))
1624 return false;
1625
1626 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1627
1628 if ((fd = open(lun_file, O_WRONLY)) < 0) {
1629 LOGE("Unable to open ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
1630 return false;
1631 }
1632
1633 if (write(fd, Part->Actual_Block_Device.c_str(), Part->Actual_Block_Device.size()) < 0) {
1634 LOGE("Unable to write to ums lunfile '%s': (%s)\n", lun_file, strerror(errno));
1635 close(fd);
1636 return false;
1637 }
1638 close(fd);
1639 }
1640 return true;
1641}
1642
1643int TWPartitionManager::usb_storage_disable(void) {
1644 int fd, index;
1645 char lun_file[255];
1646
1647 for (index=0; index<2; index++) {
1648 sprintf(lun_file, CUSTOM_LUN_FILE, index);
1649
1650 if ((fd = open(lun_file, O_WRONLY)) < 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001651 Mount_All_Storage();
1652 Update_System_Details();
1653 if (index == 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001654 LOGE("Unable to open ums lunfile '%s': (%s)", lun_file, strerror(errno));
Dees_Troye58d5262012-09-21 12:27:57 -04001655 return false;
1656 } else
1657 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001658 }
1659
1660 char ch = 0;
1661 if (write(fd, &ch, 1) < 0) {
Dees_Troy8170a922012-09-18 15:40:25 -04001662 close(fd);
Dees_Troye58d5262012-09-21 12:27:57 -04001663 Mount_All_Storage();
1664 Update_System_Details();
1665 if (index == 0) {
1666 LOGE("Unable to write to ums lunfile '%s': (%s)", lun_file, strerror(errno));
1667 return false;
1668 } else
1669 return true;
Dees_Troy8170a922012-09-18 15:40:25 -04001670 }
1671
1672 close(fd);
1673 }
Dees_Troye58d5262012-09-21 12:27:57 -04001674 Mount_All_Storage();
1675 Update_System_Details();
Dees_Troy8170a922012-09-18 15:40:25 -04001676 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001677}
1678
1679void TWPartitionManager::Mount_All_Storage(void) {
1680 std::vector<TWPartition*>::iterator iter;
1681
1682 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1683 if ((*iter)->Is_Storage)
1684 (*iter)->Mount(false);
1685 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001686}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001687
1688int TWPartitionManager::Partition_SDCard(void) {
1689 char mkdir_path[255], temp[255], line[512];
1690 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1691 int ext, swap, total_size = 0, fat_size;
1692 FILE* fp;
1693
1694 ui_print("Partitioning SD Card...\n");
1695#ifdef TW_EXTERNAL_STORAGE_PATH
1696 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1697#else
1698 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1699#endif
1700 if (SDCard == NULL) {
1701 LOGE("Unable to locate device to partition.\n");
1702 return false;
1703 }
1704 if (!SDCard->UnMount(true))
1705 return false;
1706 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1707 if (SDext != NULL) {
1708 if (!SDext->UnMount(true))
1709 return false;
1710 }
1711 system("umount \"$SWAPPATH\"");
1712 Device = SDCard->Actual_Block_Device;
1713 // Just use the root block device
1714 Device.resize(strlen("/dev/block/mmcblkX"));
1715
1716 // Find the size of the block device:
1717 fp = fopen("/proc/partitions", "rt");
1718 if (fp == NULL) {
1719 LOGE("Unable to open /proc/partitions\n");
1720 return false;
1721 }
1722
1723 while (fgets(line, sizeof(line), fp) != NULL)
1724 {
1725 unsigned long major, minor, blocks;
1726 char device[512];
1727 char tmpString[64];
1728
1729 if (strlen(line) < 7 || line[0] == 'm') continue;
1730 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1731
1732 tmpdevice = "/dev/block/";
1733 tmpdevice += device;
1734 if (tmpdevice == Device) {
1735 // Adjust block size to byte size
1736 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1737 break;
1738 }
1739 }
1740 fclose(fp);
1741
1742 DataManager::GetValue("tw_sdext_size", ext);
1743 DataManager::GetValue("tw_swap_size", swap);
1744 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1745 fat_size = total_size - ext - swap;
1746 LOGI("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);
1747 memset(temp, 0, sizeof(temp));
1748 sprintf(temp, "%i", fat_size);
1749 fat_str = temp;
1750 memset(temp, 0, sizeof(temp));
1751 sprintf(temp, "%i", fat_size + ext);
1752 ext_str = temp;
1753 memset(temp, 0, sizeof(temp));
1754 sprintf(temp, "%i", fat_size + ext + swap);
1755 swap_str = temp;
1756 if (ext + swap > total_size) {
1757 LOGE("EXT + Swap size is larger than sdcard size.\n");
1758 return false;
1759 }
1760 ui_print("Removing partition table...\n");
1761 Command = "parted -s " + Device + " mklabel msdos";
1762 LOGI("Command is: '%s'\n", Command.c_str());
1763 if (system(Command.c_str()) != 0) {
1764 LOGE("Unable to remove partition table.\n");
1765 Update_System_Details();
1766 return false;
1767 }
1768 ui_print("Creating FAT32 partition...\n");
1769 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
1770 LOGI("Command is: '%s'\n", Command.c_str());
1771 if (system(Command.c_str()) != 0) {
1772 LOGE("Unable to create FAT32 partition.\n");
1773 return false;
1774 }
1775 if (ext > 0) {
1776 ui_print("Creating EXT partition...\n");
1777 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
1778 LOGI("Command is: '%s'\n", Command.c_str());
1779 if (system(Command.c_str()) != 0) {
1780 LOGE("Unable to create EXT partition.\n");
1781 Update_System_Details();
1782 return false;
1783 }
1784 }
1785 if (swap > 0) {
1786 ui_print("Creating swap partition...\n");
1787 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
1788 LOGI("Command is: '%s'\n", Command.c_str());
1789 if (system(Command.c_str()) != 0) {
1790 LOGE("Unable to create swap partition.\n");
1791 Update_System_Details();
1792 return false;
1793 }
1794 }
1795 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1796#ifdef TW_EXTERNAL_STORAGE_PATH
1797 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1798 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1799 memset(mkdir_path, 0, sizeof(mkdir_path));
1800 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1801#else
1802 Mount_By_Path("/sdcard", 1);
1803 strcpy(mkdir_path, "/sdcard/TWRP");
1804#endif
1805 mkdir(mkdir_path, 0777);
1806 DataManager::Flush();
1807#ifdef TW_EXTERNAL_STORAGE_PATH
1808 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1809 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1810 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1811#else
1812 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1813 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1814 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1815#endif
1816 if (ext > 0) {
1817 if (SDext == NULL) {
1818 LOGE("Unable to locate sd-ext partition.\n");
1819 return false;
1820 }
1821 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
1822 ui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1823 LOGI("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
1824 system(Command.c_str());
1825 }
1826
1827 Update_System_Details();
1828 ui_print("Partitioning complete.\n");
1829 return true;
1830}