blob: 3e5d696a6190b5a681d725ae206757b4163784e0 [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;
Dees_Troy5bf43922012-09-07 16:07:55 -040060
61 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
62 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000063 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -040064 return false;
65 }
66
67 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
68 if (fstab_line[0] != '/')
69 continue;
70
Dees_Troy2a923582012-09-20 12:13:34 -040071 if (fstab_line[strlen(fstab_line) - 1] != '\n')
72 fstab_line[strlen(fstab_line)] = '\n';
73
Dees_Troy5bf43922012-09-07 16:07:55 -040074 TWPartition* partition = new TWPartition();
Dees_Troy2a923582012-09-20 12:13:34 -040075 string line = fstab_line;
Dees_Troyab10ee22012-09-21 14:27:30 -040076 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy2a923582012-09-20 12:13:34 -040077
Dees_Troy5bf43922012-09-07 16:07:55 -040078 if (partition->Process_Fstab_Line(line, Display_Error)) {
Dees Troy02a64532014-03-19 15:23:32 +000079 if (!settings_partition && partition->Is_Settings_Storage) {
80 settings_partition = partition;
Dees_Troya13d74f2013-03-24 08:54:55 -050081 } else {
82 partition->Is_Settings_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050083 }
Ethan Yonkerc05c5982014-03-13 09:19:56 -050084 Partitions.push_back(partition);
Dees_Troy5bf43922012-09-07 16:07:55 -040085 } else {
86 delete partition;
87 }
88 }
89 fclose(fstabFile);
Dees Troy02a64532014-03-19 15:23:32 +000090 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -050091 std::vector<TWPartition*>::iterator iter;
92 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
93 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +000094 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -050095 break;
96 }
97 }
Dees Troy02a64532014-03-19 15:23:32 +000098 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +000099 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500100 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400101 if (!Write_Fstab()) {
102 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000103 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400104 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000105 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400106 }
Dees_Troy51127312012-09-08 13:08:49 -0400107 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400108 UnMount_Main_Partitions();
Dees Troy02a64532014-03-19 15:23:32 +0000109 Setup_Settings_Storage_Partition(settings_partition);
Dees_Troy5bf43922012-09-07 16:07:55 -0400110 return true;
111}
112
113int TWPartitionManager::Write_Fstab(void) {
114 FILE *fp;
115 std::vector<TWPartition*>::iterator iter;
116 string Line;
117
118 fp = fopen("/etc/fstab", "w");
119 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000120 LOGINFO("Can not open /etc/fstab.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400121 return false;
122 }
Dees_Troy63c8df72012-09-10 14:02:05 -0400123 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -0400124 if ((*iter)->Can_Be_Mounted) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400125 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw\n";
Dees_Troy5bf43922012-09-07 16:07:55 -0400126 fputs(Line.c_str(), fp);
Dees_Troy91862e62013-04-04 23:48:21 +0000127 }
128 // Handle subpartition tracking
129 if ((*iter)->Is_SubPartition) {
130 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
131 if (ParentPartition)
132 ParentPartition->Has_SubPartition = true;
133 else
134 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 -0400135 }
136 }
137 fclose(fp);
138 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400139}
140
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500141void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
142#ifndef RECOVERY_SDCARD_ON_DATA
143 Part->Setup_AndSec();
144#endif
145 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
146 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
147 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
148}
149
Dees_Troy8170a922012-09-18 15:40:25 -0400150void TWPartitionManager::Output_Partition_Logging(void) {
151 std::vector<TWPartition*>::iterator iter;
152
153 printf("\n\nPartition Logs:\n");
154 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
155 Output_Partition((*iter));
156}
157
158void TWPartitionManager::Output_Partition(TWPartition* Part) {
159 unsigned long long mb = 1048576;
160
Gary Peck004d48b2012-11-21 16:28:18 -0800161 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 -0400162 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800163 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 -0400164 }
Gary Peck004d48b2012-11-21 16:28:18 -0800165 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500166 if (Part->Can_Be_Mounted)
167 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800168 if (Part->Can_Be_Wiped)
169 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700170 if (Part->Use_Rm_Rf)
171 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500172 if (Part->Can_Be_Backed_Up)
173 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800174 if (Part->Wipe_During_Factory_Reset)
175 printf("Wipe_During_Factory_Reset ");
176 if (Part->Wipe_Available_in_GUI)
177 printf("Wipe_Available_in_GUI ");
178 if (Part->Is_SubPartition)
179 printf("Is_SubPartition ");
180 if (Part->Has_SubPartition)
181 printf("Has_SubPartition ");
182 if (Part->Removable)
183 printf("Removable ");
184 if (Part->Is_Present)
185 printf("IsPresent ");
186 if (Part->Can_Be_Encrypted)
187 printf("Can_Be_Encrypted ");
188 if (Part->Is_Encrypted)
189 printf("Is_Encrypted ");
190 if (Part->Is_Decrypted)
191 printf("Is_Decrypted ");
192 if (Part->Has_Data_Media)
193 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000194 if (Part->Can_Encrypt_Backup)
195 printf("Can_Encrypt_Backup ");
196 if (Part->Use_Userdata_Encryption)
197 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800198 if (Part->Has_Android_Secure)
199 printf("Has_Android_Secure ");
200 if (Part->Is_Storage)
201 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500202 if (Part->Is_Settings_Storage)
203 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000204 if (Part->Ignore_Blkid)
205 printf("Ignore_Blkid ");
Dees_Troy16c2b312013-01-15 16:51:18 +0000206 if (Part->Retain_Layout_Version)
207 printf("Retain_Layout_Version ");
Gary Peck004d48b2012-11-21 16:28:18 -0800208 printf("\n");
209 if (!Part->SubPartition_Of.empty())
210 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
211 if (!Part->Symlink_Path.empty())
212 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
213 if (!Part->Symlink_Mount_Point.empty())
214 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
215 if (!Part->Primary_Block_Device.empty())
216 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
217 if (!Part->Alternate_Block_Device.empty())
218 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
219 if (!Part->Decrypted_Block_Device.empty())
220 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
221 if (Part->Length != 0)
222 printf(" Length: %i\n", Part->Length);
223 if (!Part->Display_Name.empty())
224 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500225 if (!Part->Storage_Name.empty())
226 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800227 if (!Part->Backup_Path.empty())
228 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
229 if (!Part->Backup_Name.empty())
230 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500231 if (!Part->Backup_Display_Name.empty())
232 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800233 if (!Part->Backup_FileName.empty())
234 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
235 if (!Part->Storage_Path.empty())
236 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
237 if (!Part->Current_File_System.empty())
238 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
239 if (!Part->Fstab_File_System.empty())
240 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
241 if (Part->Format_Block_Size != 0)
242 printf(" Format_Block_Size: %i\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400243 if (!Part->MTD_Name.empty())
244 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400245 string back_meth = Part->Backup_Method_By_Name();
246 printf(" Backup_Method: %s\n\n", back_meth.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800247 if (Part->Mount_Flags || !Part->Mount_Options.empty())
248 printf(" Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400249}
250
Dees_Troy51a0e822012-09-05 15:24:24 -0400251int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 std::vector<TWPartition*>::iterator iter;
253 int ret = false;
254 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400255 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400256
Dees_Troyd93bda52013-07-03 19:55:19 +0000257 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400258 return true;
259
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 // Iterate through all partitions
261 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400262 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400263 ret = (*iter)->Mount(Display_Error);
264 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400265 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400267 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400268 }
269 if (found) {
270 return ret;
271 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000272 LOGERR("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400273 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000274 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400275 }
276 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400277}
278
279int TWPartitionManager::Mount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400280 TWPartition* Part = Find_Partition_By_Block(Block);
Dees_Troy5bf43922012-09-07 16:07:55 -0400281
Dees_Troy51127312012-09-08 13:08:49 -0400282 if (Part) {
283 if (Part->Has_SubPartition) {
284 std::vector<TWPartition*>::iterator subpart;
285
286 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
287 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
288 (*subpart)->Mount(Display_Error);
289 }
290 return Part->Mount(Display_Error);
291 } else
292 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400293 }
294 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000295 LOGERR("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000297 LOGINFO("Mount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400298 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400299}
300
301int TWPartitionManager::Mount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400302 TWPartition* Part = Find_Partition_By_Name(Name);
Dees_Troy5bf43922012-09-07 16:07:55 -0400303
Dees_Troy51127312012-09-08 13:08:49 -0400304 if (Part) {
305 if (Part->Has_SubPartition) {
306 std::vector<TWPartition*>::iterator subpart;
307
308 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
309 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
310 (*subpart)->Mount(Display_Error);
311 }
312 return Part->Mount(Display_Error);
313 } else
314 return Part->Mount(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 }
316 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000317 LOGERR("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400318 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000319 LOGINFO("Mount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400320 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400321}
322
323int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400324 std::vector<TWPartition*>::iterator iter;
325 int ret = false;
326 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400327 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400328
329 // Iterate through all partitions
330 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400331 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400332 ret = (*iter)->UnMount(Display_Error);
333 found = true;
334 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
335 (*iter)->UnMount(Display_Error);
336 }
337 }
338 if (found) {
339 return ret;
340 } else if (Display_Error) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000341 LOGERR("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400342 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000343 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400344 }
345 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400346}
347
348int TWPartitionManager::UnMount_By_Block(string Block, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400349 TWPartition* Part = Find_Partition_By_Block(Block);
350
351 if (Part) {
352 if (Part->Has_SubPartition) {
353 std::vector<TWPartition*>::iterator subpart;
354
355 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
356 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
357 (*subpart)->UnMount(Display_Error);
358 }
359 return Part->UnMount(Display_Error);
360 } else
361 return Part->UnMount(Display_Error);
362 }
363 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000364 LOGERR("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400365 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000366 LOGINFO("UnMount: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400367 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400368}
369
370int TWPartitionManager::UnMount_By_Name(string Name, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400371 TWPartition* Part = Find_Partition_By_Name(Name);
372
373 if (Part) {
374 if (Part->Has_SubPartition) {
375 std::vector<TWPartition*>::iterator subpart;
376
377 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
378 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
379 (*subpart)->UnMount(Display_Error);
380 }
381 return Part->UnMount(Display_Error);
382 } else
383 return Part->UnMount(Display_Error);
384 }
385 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000386 LOGERR("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400387 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000388 LOGINFO("UnMount: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400389 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400390}
391
392int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400393 TWPartition* Part = Find_Partition_By_Path(Path);
394
395 if (Part)
396 return Part->Is_Mounted();
397 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000398 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400399 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400400}
401
402int TWPartitionManager::Is_Mounted_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400403 TWPartition* Part = Find_Partition_By_Block(Block);
404
405 if (Part)
406 return Part->Is_Mounted();
407 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000408 LOGINFO("Is_Mounted: Unable to find partition for block '%s'\n", Block.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_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400413 TWPartition* Part = Find_Partition_By_Name(Name);
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 name '%s'\n", Name.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400419 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400420}
421
Dees_Troy5bf43922012-09-07 16:07:55 -0400422int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400423 string current_storage_path = DataManager::GetCurrentStoragePath();
424
425 if (Mount_By_Path(current_storage_path, Display_Error)) {
426 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
427 if (FreeStorage)
428 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
429 return true;
430 }
431 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400432}
433
Dees_Troy5bf43922012-09-07 16:07:55 -0400434int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
435 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
436}
437
438TWPartition* TWPartitionManager::Find_Partition_By_Path(string Path) {
439 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400440 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400441
442 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400443 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400444 return (*iter);
445 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400446 return NULL;
447}
448
Dees_Troy5bf43922012-09-07 16:07:55 -0400449TWPartition* TWPartitionManager::Find_Partition_By_Block(string Block) {
Dees_Troy51127312012-09-08 13:08:49 -0400450 std::vector<TWPartition*>::iterator iter;
451
452 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400453 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 -0400454 return (*iter);
455 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400456 return NULL;
Dees_Troy5bf43922012-09-07 16:07:55 -0400457}
458
459TWPartition* TWPartitionManager::Find_Partition_By_Name(string Name) {
Dees_Troy51127312012-09-08 13:08:49 -0400460 std::vector<TWPartition*>::iterator iter;
461
462 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
463 if ((*iter)->Display_Name == Name)
464 return (*iter);
465 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400466 return NULL;
467}
Dees_Troy51a0e822012-09-05 15:24:24 -0400468
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400469int TWPartitionManager::Check_Backup_Name(bool Display_Error) {
470 // Check the backup name to ensure that it is the correct size and contains only valid characters
471 // and that a backup with that name doesn't already exist
472 char backup_name[MAX_BACKUP_NAME_LEN];
473 char backup_loc[255], tw_image_dir[255];
474 int copy_size;
475 int index, cur_char;
476 string Backup_Name, Backup_Loc;
477
478 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
479 copy_size = Backup_Name.size();
480 // Check size
481 if (copy_size > MAX_BACKUP_NAME_LEN) {
482 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000483 LOGERR("Backup name is too long.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400484 return -2;
485 }
486
487 // Check each character
488 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500489 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400490 return 0; // A "0" (zero) means to use the current timestamp for the backup name
491 for (index=0; index<copy_size; index++) {
492 cur_char = (int)backup_name[index];
493 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) {
494 // These are valid characters
495 // Numbers
496 // Upper case letters
497 // Lower case letters
498 // Space
499 // and -_.{}[]
500 } else {
501 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000502 LOGERR("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char);
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400503 return -3;
504 }
505 }
506
507 // Check to make sure that a backup with this name doesn't already exist
508 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
509 strcpy(backup_loc, Backup_Loc.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500510 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400511 if (TWFunc::Path_Exists(tw_image_dir)) {
512 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000513 LOGERR("A backup with this name already exists.\n");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400514 return -4;
515 }
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400516 // No problems found, return 0
517 return 0;
518}
519
Dees_Troy43d8b002012-09-17 16:00:01 -0400520bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename)
521{
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500522 string command;
Dees_Troy43d8b002012-09-17 16:00:01 -0400523 string Full_File = Backup_Folder + Backup_Filename;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500524 string result;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500525 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400526
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500527 if (!generate_md5)
Dees_Troy43d8b002012-09-17 16:00:01 -0400528 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400529
Dees_Troyb46a6842012-09-25 11:06:46 -0400530 TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000531 gui_print(" * Generating md5...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400532
533 if (TWFunc::Path_Exists(Full_File)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500534 md5sum.setfn(Backup_Folder + Backup_Filename);
535 if (md5sum.computeMD5() == 0)
536 if (md5sum.write_md5digest() == 0)
Dees_Troy2673cec2013-04-02 20:22:16 +0000537 gui_print(" * MD5 Created.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500538 else
539 return -1;
540 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000541 gui_print(" * MD5 Error!\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400542 } else {
543 char filename[512];
544 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500545 string strfn;
Dees_Troy43d8b002012-09-17 16:00:01 -0400546 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500547 strfn = filename;
Dees_Troy83bd4832013-05-04 12:39:56 +0000548 while (index < 1000) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400549 md5sum.setfn(filename);
Dees_Troy83bd4832013-05-04 12:39:56 +0000550 if (TWFunc::Path_Exists(filename)) {
551 if (md5sum.computeMD5() == 0) {
552 if (md5sum.write_md5digest() != 0)
553 {
554 gui_print(" * MD5 Error.\n");
555 return false;
556 }
557 } else {
558 gui_print(" * Error computing MD5.\n");
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500559 return false;
560 }
561 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400562 index++;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400563 sprintf(filename, "%s%03i", Full_File.c_str(), index);
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500564 strfn = filename;
Dees_Troy43d8b002012-09-17 16:00:01 -0400565 }
566 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000567 LOGERR("Backup file: '%s' not found!\n", filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400568 return false;
569 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000570 gui_print(" * MD5 Created.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400571 }
572 return true;
573}
574
Dees_Troy093b7642012-09-21 15:59:38 -0400575bool 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 -0400576 time_t start, stop;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500577 int img_bps;
578 unsigned long long file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400579 unsigned long total_time, remain_time, section_time;
580 int use_compression, backup_time;
581 float pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400582
583 if (Part == NULL)
584 return true;
585
Dees_Troy093b7642012-09-21 15:59:38 -0400586 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
587
588 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
589 if (use_compression)
590 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
591 else
592 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
593
594 // We know the speed for both, how far into the whole backup are we, based on time
595 total_time = (*img_bytes / (unsigned long)img_bps) + (*file_bytes / (unsigned long)file_bps);
596 remain_time = (*img_bytes_remaining / (unsigned long)img_bps) + (*file_bytes_remaining / (unsigned long)file_bps);
597
598 pos = (total_time - remain_time) / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000599 DataManager::SetProgress(pos);
Dees_Troy093b7642012-09-21 15:59:38 -0400600
Dees_Troy2673cec2013-04-02 20:22:16 +0000601 LOGINFO("Estimated Total time: %lu Estimated remaining time: %lu\n", total_time, remain_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400602
603 // And get the time
604 if (Part->Backup_Method == 1)
605 section_time = Part->Backup_Size / file_bps;
606 else
607 section_time = Part->Backup_Size / img_bps;
608
609 // Set the position
610 pos = section_time / (float) total_time;
Dees_Troy2673cec2013-04-02 20:22:16 +0000611 DataManager::ShowProgress(pos, section_time);
Dees_Troy093b7642012-09-21 15:59:38 -0400612
Dees_Troy43d8b002012-09-17 16:00:01 -0400613 time(&start);
614
615 if (Part->Backup(Backup_Folder)) {
Dees_Troy8170a922012-09-18 15:40:25 -0400616 if (Part->Has_SubPartition) {
617 std::vector<TWPartition*>::iterator subpart;
618
619 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500620 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
Dees_Troy8170a922012-09-18 15:40:25 -0400621 if (!(*subpart)->Backup(Backup_Folder))
622 return false;
Dees_Troy2727b992013-08-14 20:09:30 +0000623 sync();
624 sync();
Dees_Troy8170a922012-09-18 15:40:25 -0400625 if (!Make_MD5(generate_md5, Backup_Folder, (*subpart)->Backup_FileName))
626 return false;
Dees_Troy093b7642012-09-21 15:59:38 -0400627 if (Part->Backup_Method == 1) {
628 *file_bytes_remaining -= (*subpart)->Backup_Size;
629 } else {
630 *img_bytes_remaining -= (*subpart)->Backup_Size;
631 }
Dees_Troy8170a922012-09-18 15:40:25 -0400632 }
633 }
634 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400635 time(&stop);
Dees_Troy093b7642012-09-21 15:59:38 -0400636 backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000637 LOGINFO("Partition Backup time: %d\n", backup_time);
Dees_Troy43d8b002012-09-17 16:00:01 -0400638 if (Part->Backup_Method == 1) {
639 *file_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400640 *file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400641 } else {
642 *img_bytes_remaining -= Part->Backup_Size;
Dees_Troy093b7642012-09-21 15:59:38 -0400643 *img_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400644 }
645 return Make_MD5(generate_md5, Backup_Folder, Part->Backup_FileName);
646 } else {
647 return false;
648 }
649}
650
651int TWPartitionManager::Run_Backup(void) {
652 int check, do_md5, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500653 string Backup_Folder, Backup_Name, Full_Backup_Path, Backup_List, backup_path;
Dees_Troy8170a922012-09-18 15:40:25 -0400654 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 -0400655 unsigned long img_time = 0, file_time = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500656 TWPartition* backup_part = NULL;
Dees_Troy43d8b002012-09-17 16:00:01 -0400657 TWPartition* storage = NULL;
Dees_Troy8170a922012-09-18 15:40:25 -0400658 std::vector<TWPartition*>::iterator subpart;
Dees_Troy43d8b002012-09-17 16:00:01 -0400659 struct tm *t;
660 time_t start, stop, seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500661 size_t start_pos = 0, end_pos = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400662 seconds = time(0);
663 t = localtime(&seconds);
664
665 time(&total_start);
666
667 Update_System_Details();
668
669 if (!Mount_Current_Storage(true))
670 return false;
671
672 DataManager::GetValue(TW_SKIP_MD5_GENERATE_VAR, do_md5);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400673 if (do_md5 == 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400674 do_md5 = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400675 else
676 do_md5 = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400677
Dees_Troy43d8b002012-09-17 16:00:01 -0400678 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
679 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees Troyb21cc642013-09-10 17:36:41 +0000680 if (Backup_Name == "(Current Date)") {
681 Backup_Name = TWFunc::Get_Current_Date();
682 } else if (Backup_Name == "(Auto Generate)" || Backup_Name == "0" || Backup_Name.empty()) {
683 TWFunc::Auto_Generate_Backup_Name();
684 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400685 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000686 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400687 Full_Backup_Path = Backup_Folder + "/" + Backup_Name + "/";
Dees_Troy2673cec2013-04-02 20:22:16 +0000688 LOGINFO("Full_Backup_Path is: '%s'\n", Full_Backup_Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400689
Dees_Troy2673cec2013-04-02 20:22:16 +0000690 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500691 DataManager::GetValue("tw_backup_list", Backup_List);
692 if (!Backup_List.empty()) {
693 end_pos = Backup_List.find(";", start_pos);
694 while (end_pos != string::npos && start_pos < Backup_List.size()) {
695 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
696 backup_part = Find_Partition_By_Path(backup_path);
697 if (backup_part != NULL) {
698 partition_count++;
699 if (backup_part->Backup_Method == 1)
700 file_bytes += backup_part->Backup_Size;
701 else
702 img_bytes += backup_part->Backup_Size;
703 if (backup_part->Has_SubPartition) {
704 std::vector<TWPartition*>::iterator subpart;
705
706 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +0000707 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 -0500708 partition_count++;
709 if ((*subpart)->Backup_Method == 1)
710 file_bytes += (*subpart)->Backup_Size;
711 else
712 img_bytes += (*subpart)->Backup_Size;
713 }
714 }
Dees_Troy8170a922012-09-18 15:40:25 -0400715 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500716 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000717 LOGERR("Unable to locate '%s' partition for backup calculations.\n", backup_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -0400718 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500719 start_pos = end_pos + 1;
720 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400721 }
722 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400723
724 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000725 gui_print("No partitions selected for backup.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400726 return false;
727 }
728 total_bytes = file_bytes + img_bytes;
Dees_Troy2673cec2013-04-02 20:22:16 +0000729 gui_print(" * Total number of partitions to back up: %d\n", partition_count);
730 gui_print(" * Total size of all data: %lluMB\n", total_bytes / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400731 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
732 if (storage != NULL) {
733 free_space = storage->Free;
Dees_Troy2673cec2013-04-02 20:22:16 +0000734 gui_print(" * Available space: %lluMB\n", free_space / 1024 / 1024);
Dees_Troy43d8b002012-09-17 16:00:01 -0400735 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000736 LOGERR("Unable to locate storage device.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400737 return false;
738 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000739 if (free_space - (32 * 1024 * 1024) < total_bytes) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400740 // We require an extra 32MB just in case
Dees_Troy2673cec2013-04-02 20:22:16 +0000741 LOGERR("Not enough free space on storage.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400742 return false;
743 }
744 img_bytes_remaining = img_bytes;
745 file_bytes_remaining = file_bytes;
746
Dees_Troy2673cec2013-04-02 20:22:16 +0000747 gui_print("\n[BACKUP STARTED]\n");
748 gui_print(" * Backup Folder: %s\n", Full_Backup_Path.c_str());
Dees_Troyd4b22b02013-01-18 17:17:58 +0000749 if (!TWFunc::Recursive_Mkdir(Full_Backup_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000750 LOGERR("Failed to make backup folder.\n");
Dees_Troyd4b22b02013-01-18 17:17:58 +0000751 return false;
752 }
753
Dees_Troy2673cec2013-04-02 20:22:16 +0000754 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400755
Dees_Troya13d74f2013-03-24 08:54:55 -0500756 start_pos = 0;
757 end_pos = Backup_List.find(";", start_pos);
758 while (end_pos != string::npos && start_pos < Backup_List.size()) {
759 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
760 backup_part = Find_Partition_By_Path(backup_path);
761 if (backup_part != NULL) {
762 if (!Backup_Partition(backup_part, Full_Backup_Path, do_md5, &img_bytes_remaining, &file_bytes_remaining, &img_time, &file_time, &img_bytes, &file_bytes))
763 return false;
764 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000765 LOGERR("Unable to locate '%s' partition for backup process.\n", backup_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500766 }
767 start_pos = end_pos + 1;
768 end_pos = Backup_List.find(";", start_pos);
769 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400770
771 // Average BPS
772 if (img_time == 0)
773 img_time = 1;
774 if (file_time == 0)
775 file_time = 1;
Dees_Troy093b7642012-09-21 15:59:38 -0400776 int img_bps = (int)img_bytes / (int)img_time;
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500777 unsigned long long file_bps = file_bytes / (int)file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400778
Dees_Troy2673cec2013-04-02 20:22:16 +0000779 gui_print("Average backup rate for file systems: %llu MB/sec\n", (file_bps / (1024 * 1024)));
780 gui_print("Average backup rate for imaged drives: %lu MB/sec\n", (img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -0400781
782 time(&total_stop);
783 int total_time = (int) difftime(total_stop, total_start);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500784 uint64_t actual_backup_size = du.Get_Folder_Size(Full_Backup_Path);
Dees_Troy43d8b002012-09-17 16:00:01 -0400785 actual_backup_size /= (1024LLU * 1024LLU);
786
bigbiff bigbiff2c57d782013-02-19 10:09:21 -0500787 int prev_img_bps, use_compression;
788 unsigned long long prev_file_bps;
Dees_Troy093b7642012-09-21 15:59:38 -0400789 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
790 img_bps += (prev_img_bps * 4);
791 img_bps /= 5;
792
793 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
794 if (use_compression)
795 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
796 else
797 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
798 file_bps += (prev_file_bps * 4);
799 file_bps /= 5;
800
801 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
802 if (use_compression)
803 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
804 else
805 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
806
Dees_Troy2673cec2013-04-02 20:22:16 +0000807 gui_print("[%llu MB TOTAL BACKED UP]\n", actual_backup_size);
Dees_Troy43d8b002012-09-17 16:00:01 -0400808 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400809 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +0000810 gui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000811 string backup_log = Full_Backup_Path + "recovery.log";
812 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
813 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400814}
815
Dees_Troy093b7642012-09-21 15:59:38 -0400816bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400817 time_t Start, Stop;
818 time(&Start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000819 DataManager::ShowProgress(1.0 / (float)partition_count, 150);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400820 if (!Part->Restore(Restore_Name))
821 return false;
Dees_Troy8170a922012-09-18 15:40:25 -0400822 if (Part->Has_SubPartition) {
823 std::vector<TWPartition*>::iterator subpart;
824
825 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
826 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point) {
827 if (!(*subpart)->Restore(Restore_Name))
828 return false;
829 }
830 }
831 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400832 time(&Stop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000833 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 -0400834 return true;
835}
836
Dees_Troy51a0e822012-09-05 15:24:24 -0400837int TWPartitionManager::Run_Restore(string Restore_Name) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400838 int check_md5, check, partition_count = 0;
Dees_Troya13d74f2013-03-24 08:54:55 -0500839 TWPartition* restore_part = NULL;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400840 time_t rStart, rStop;
841 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -0500842 string Restore_List, restore_path;
843 size_t start_pos = 0, end_pos;
Dees_Troy43d8b002012-09-17 16:00:01 -0400844
Dees_Troy2673cec2013-04-02 20:22:16 +0000845 gui_print("\n[RESTORE STARTED]\n\n");
846 gui_print("Restore folder: '%s'\n", Restore_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400847
Dees_Troy4a2a1262012-09-18 09:33:47 -0400848 if (!Mount_Current_Storage(true))
849 return false;
850
851 DataManager::GetValue(TW_SKIP_MD5_CHECK_VAR, check_md5);
Dees_Troya13d74f2013-03-24 08:54:55 -0500852 if (check_md5 > 0) {
853 // Check MD5 files first before restoring to ensure that all of them match before starting a restore
854 TWFunc::GUI_Operation_Text(TW_VERIFY_MD5_TEXT, "Verifying MD5");
Dees_Troy2673cec2013-04-02 20:22:16 +0000855 gui_print("Verifying MD5...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500856 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000857 gui_print("Skipping MD5 check based on user setting.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500858 }
859 DataManager::GetValue("tw_restore_selected", Restore_List);
860 if (!Restore_List.empty()) {
861 end_pos = Restore_List.find(";", start_pos);
862 while (end_pos != string::npos && start_pos < Restore_List.size()) {
863 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
864 restore_part = Find_Partition_By_Path(restore_path);
865 if (restore_part != NULL) {
866 partition_count++;
867 if (check_md5 > 0 && !restore_part->Check_MD5(Restore_Name))
868 return false;
869 if (restore_part->Has_SubPartition) {
870 std::vector<TWPartition*>::iterator subpart;
871
872 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
873 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == restore_part->Mount_Point) {
bigbiff bigbiff07338812014-03-30 14:56:41 -0400874 if (check_md5 > 0 && !(*subpart)->Check_MD5(Restore_Name))
Dees_Troya13d74f2013-03-24 08:54:55 -0500875 return false;
876 }
877 }
878 }
879 } else {
Dees_Troy59df9262013-06-19 14:53:57 -0500880 LOGERR("Unable to locate '%s' partition for restoring (restore list).\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500881 }
882 start_pos = end_pos + 1;
883 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -0400884 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400885 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400886
887 if (partition_count == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000888 LOGERR("No partitions selected for restore.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -0400889 return false;
890 }
891
Dees_Troy2673cec2013-04-02 20:22:16 +0000892 gui_print("Restoring %i partitions...\n", partition_count);
893 DataManager::SetProgress(0.0);
Dees_Troya13d74f2013-03-24 08:54:55 -0500894 start_pos = 0;
895 if (!Restore_List.empty()) {
896 end_pos = Restore_List.find(";", start_pos);
897 while (end_pos != string::npos && start_pos < Restore_List.size()) {
898 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
899 restore_part = Find_Partition_By_Path(restore_path);
900 if (restore_part != NULL) {
901 partition_count++;
902 if (!Restore_Partition(restore_part, Restore_Name, partition_count))
903 return false;
904 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000905 LOGERR("Unable to locate '%s' partition for restoring.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500906 }
907 start_pos = end_pos + 1;
908 end_pos = Restore_List.find(";", start_pos);
909 }
910 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400911 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, "Updating System Details");
Dees_Troy43d8b002012-09-17 16:00:01 -0400912 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -0400913 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -0400914 time(&rStop);
Dees_Troy2673cec2013-04-02 20:22:16 +0000915 gui_print("[RESTORE COMPLETED IN %d SECONDS]\n\n",(int)difftime(rStop,rStart));
Dees_Troy63c8df72012-09-10 14:02:05 -0400916 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400917}
918
919void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -0400920 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -0500921 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +0000922 bool get_date = true, check_encryption = true;
923
924 DataManager::SetValue("tw_restore_encrypted", 0);
Dees_Troy63c8df72012-09-10 14:02:05 -0400925
926 DIR* d;
927 d = opendir(Restore_Name.c_str());
928 if (d == NULL)
929 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000930 LOGERR("Error opening %s\n", Restore_Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -0400931 return;
932 }
933
934 struct dirent* de;
935 while ((de = readdir(d)) != NULL)
936 {
937 // Strip off three components
938 char str[256];
939 char* label;
940 char* fstype = NULL;
941 char* extn = NULL;
942 char* ptr;
943
944 strcpy(str, de->d_name);
945 if (strlen(str) <= 2)
946 continue;
947
948 if (get_date) {
949 char file_path[255];
950 struct stat st;
951
952 strcpy(file_path, Restore_Name.c_str());
953 strcat(file_path, "/");
954 strcat(file_path, str);
955 stat(file_path, &st);
956 string backup_date = ctime((const time_t*)(&st.st_mtime));
957 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
958 get_date = false;
959 }
960
961 label = str;
962 ptr = label;
963 while (*ptr && *ptr != '.') ptr++;
964 if (*ptr == '.')
965 {
966 *ptr = 0x00;
967 ptr++;
968 fstype = ptr;
969 }
970 while (*ptr && *ptr != '.') ptr++;
971 if (*ptr == '.')
972 {
973 *ptr = 0x00;
974 ptr++;
975 extn = ptr;
976 }
977
Dees_Troy83bd4832013-05-04 12:39:56 +0000978 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
Dees_Troya13d74f2013-03-24 08:54:55 -0500979 int extnlength = strlen(extn);
Dees_Troy83bd4832013-05-04 12:39:56 +0000980 if (extnlength != 3 && extnlength != 6) continue;
981 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
982 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
983
984 if (check_encryption) {
985 string filename = Restore_Name + "/";
986 filename += de->d_name;
987 if (TWFunc::Get_File_Type(filename) == 2) {
988 LOGINFO("'%s' is encrypted\n", filename.c_str());
989 DataManager::SetValue("tw_restore_encrypted", 1);
990 }
991 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500992 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -0400993
994 TWPartition* Part = Find_Partition_By_Path(label);
995 if (Part == NULL)
996 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000997 LOGERR(" Unable to locate partition by backup name: '%s'\n", label);
Dees_Troy63c8df72012-09-10 14:02:05 -0400998 continue;
999 }
1000
1001 Part->Backup_FileName = de->d_name;
1002 if (strlen(extn) > 3) {
1003 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1004 }
1005
Dees_Troya13d74f2013-03-24 08:54:55 -05001006 Restore_List += Part->Backup_Path + ";";
Dees_Troy63c8df72012-09-10 14:02:05 -04001007 }
1008 closedir(d);
1009
Dees_Troya13d74f2013-03-24 08:54:55 -05001010 // Set the final value
1011 DataManager::SetValue("tw_restore_list", Restore_List);
1012 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001013 return;
1014}
1015
1016int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001017 std::vector<TWPartition*>::iterator iter;
1018 int ret = false;
1019 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001020 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001021
1022 // Iterate through all partitions
1023 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001024 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troye58d5262012-09-21 12:27:57 -04001025 if (Path == "/and-sec")
1026 ret = (*iter)->Wipe_AndSec();
1027 else
1028 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001029 found = true;
1030 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1031 (*iter)->Wipe();
1032 }
1033 }
1034 if (found) {
1035 return ret;
1036 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001037 LOGERR("Wipe: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001038 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001039}
1040
1041int TWPartitionManager::Wipe_By_Block(string Block) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001042 TWPartition* Part = Find_Partition_By_Block(Block);
1043
1044 if (Part) {
1045 if (Part->Has_SubPartition) {
1046 std::vector<TWPartition*>::iterator subpart;
1047
1048 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1049 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1050 (*subpart)->Wipe();
1051 }
1052 return Part->Wipe();
1053 } else
1054 return Part->Wipe();
1055 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001056 LOGERR("Wipe: Unable to find partition for block '%s'\n", Block.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001057 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001058}
1059
1060int TWPartitionManager::Wipe_By_Name(string Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001061 TWPartition* Part = Find_Partition_By_Name(Name);
1062
1063 if (Part) {
1064 if (Part->Has_SubPartition) {
1065 std::vector<TWPartition*>::iterator subpart;
1066
1067 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1068 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == Part->Mount_Point)
1069 (*subpart)->Wipe();
1070 }
1071 return Part->Wipe();
1072 } else
1073 return Part->Wipe();
1074 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001075 LOGERR("Wipe: Unable to find partition for name '%s'\n", Name.c_str());
Dees_Troy63c8df72012-09-10 14:02:05 -04001076 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001077}
1078
1079int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001080 std::vector<TWPartition*>::iterator iter;
1081 int ret = true;
1082
1083 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001084 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001085 if (!(*iter)->Wipe())
1086 ret = false;
Dees_Troy094207a2012-09-26 12:00:39 -04001087 } else if ((*iter)->Has_Android_Secure) {
1088 if (!(*iter)->Wipe_AndSec())
1089 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001090 }
1091 }
1092 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001093}
1094
Dees_Troy38bd7602012-09-14 13:33:53 -04001095int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1096 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001097 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001098
1099 if (!Mount_By_Path("/data", true))
1100 return false;
1101
1102 if (!Mount_By_Path("/cache", true))
1103 return false;
1104
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001105 dir.push_back("/data/dalvik-cache");
1106 dir.push_back("/cache/dalvik-cache");
1107 dir.push_back("/cache/dc");
Dees_Troy2673cec2013-04-02 20:22:16 +00001108 gui_print("\nWiping Dalvik Cache Directories...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -05001109 for (unsigned i = 0; i < dir.size(); ++i) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001110 if (stat(dir.at(i).c_str(), &st) == 0) {
1111 TWFunc::removeDir(dir.at(i), false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001112 gui_print("Cleaned: %s...\n", dir.at(i).c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001113 }
1114 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001115 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001116 if (sdext && sdext->Is_Present && sdext->Mount(false))
1117 {
1118 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1119 {
1120 TWFunc::removeDir("/sd-ext/dalvik-cache", false);
1121 gui_print("Cleaned: /sd-ext/dalvik-cache...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001122 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001123 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001124 gui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001125 return true;
1126}
1127
1128int TWPartitionManager::Wipe_Rotate_Data(void) {
1129 if (!Mount_By_Path("/data", true))
1130 return false;
1131
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001132 unlink("/data/misc/akmd*");
1133 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001134 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001135 return true;
1136}
1137
1138int TWPartitionManager::Wipe_Battery_Stats(void) {
1139 struct stat st;
1140
1141 if (!Mount_By_Path("/data", true))
1142 return false;
1143
1144 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001145 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001146 } else {
1147 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001148 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001149 }
1150 return true;
1151}
1152
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001153int TWPartitionManager::Wipe_Android_Secure(void) {
1154 std::vector<TWPartition*>::iterator iter;
1155 int ret = false;
1156 bool found = false;
1157
1158 // Iterate through all partitions
1159 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1160 if ((*iter)->Has_Android_Secure) {
1161 ret = (*iter)->Wipe_AndSec();
1162 found = true;
1163 }
1164 }
1165 if (found) {
1166 return ret;
1167 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001168 LOGERR("No android secure partitions found.\n");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001169 }
1170 return false;
1171}
1172
Dees_Troy38bd7602012-09-14 13:33:53 -04001173int TWPartitionManager::Format_Data(void) {
1174 TWPartition* dat = Find_Partition_By_Path("/data");
1175
1176 if (dat != NULL) {
1177 if (!dat->UnMount(true))
1178 return false;
1179
1180 return dat->Wipe_Encryption();
1181 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001182 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001183 return false;
1184 }
1185 return false;
1186}
1187
1188int TWPartitionManager::Wipe_Media_From_Data(void) {
1189 TWPartition* dat = Find_Partition_By_Path("/data");
1190
1191 if (dat != NULL) {
1192 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001193 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001194 return false;
1195 }
1196 if (!dat->Mount(true))
1197 return false;
1198
Dees_Troy2673cec2013-04-02 20:22:16 +00001199 gui_print("Wiping internal storage -- /data/media...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001200 TWFunc::removeDir("/data/media", false);
1201 if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
1202 return -1;
Dees_Troy38bd7602012-09-14 13:33:53 -04001203 if (dat->Has_Data_Media) {
1204 dat->Recreate_Media_Folder();
Dees_Troy74fb2e92013-04-15 14:35:47 +00001205 // Unmount and remount - slightly hackish way to ensure that the "/sdcard" folder is still mounted properly after wiping
1206 dat->UnMount(false);
1207 dat->Mount(false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001208 }
1209 return true;
1210 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001211 LOGERR("Unable to locate /data.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001212 return false;
1213 }
1214 return false;
1215}
1216
Dees_Troy51a0e822012-09-05 15:24:24 -04001217void TWPartitionManager::Refresh_Sizes(void) {
Dees_Troy51127312012-09-08 13:08:49 -04001218 Update_System_Details();
Dees_Troy51a0e822012-09-05 15:24:24 -04001219 return;
1220}
1221
1222void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001223 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001224 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001225
Dees_Troy2673cec2013-04-02 20:22:16 +00001226 gui_print("Updating partition details...\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001227 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy51127312012-09-08 13:08:49 -04001228 if ((*iter)->Can_Be_Mounted) {
1229 (*iter)->Update_Size(true);
1230 if ((*iter)->Mount_Point == "/system") {
1231 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1232 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
1233 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1234 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1235 } else if ((*iter)->Mount_Point == "/cache") {
1236 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1237 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1238 } else if ((*iter)->Mount_Point == "/sd-ext") {
1239 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1240 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1241 if ((*iter)->Backup_Size == 0) {
1242 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1243 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1244 } else
1245 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001246 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001247 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001248 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001249 if ((*iter)->Backup_Size == 0) {
1250 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1251 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1252 } else
1253 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001254 } else if ((*iter)->Mount_Point == "/boot") {
1255 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1256 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1257 if ((*iter)->Backup_Size == 0) {
1258 DataManager::SetValue("tw_has_boot_partition", 0);
1259 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1260 } else
1261 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001262 }
Dees_Troyab10ee22012-09-21 14:27:30 -04001263#ifdef SP1_NAME
1264 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1265 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1266 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1267 }
1268#endif
1269#ifdef SP2_NAME
1270 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1271 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1272 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1273 }
1274#endif
1275#ifdef SP3_NAME
1276 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1277 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1278 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1279 }
1280#endif
Dees_Troyaa9cc402012-10-13 12:14:05 -04001281 } else {
1282 // Handle unmountable partitions in case we reset defaults
1283 if ((*iter)->Mount_Point == "/boot") {
1284 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1285 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1286 if ((*iter)->Backup_Size == 0) {
1287 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1288 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1289 } else
1290 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1291 } else if ((*iter)->Mount_Point == "/recovery") {
1292 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1293 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1294 if ((*iter)->Backup_Size == 0) {
1295 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1296 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1297 } else
1298 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001299 } else if ((*iter)->Mount_Point == "/data") {
1300 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001301 }
1302#ifdef SP1_NAME
1303 if ((*iter)->Backup_Name == EXPAND(SP1_NAME)) {
1304 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1305 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
1306 }
1307#endif
1308#ifdef SP2_NAME
1309 if ((*iter)->Backup_Name == EXPAND(SP2_NAME)) {
1310 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1311 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
1312 }
1313#endif
1314#ifdef SP3_NAME
1315 if ((*iter)->Backup_Name == EXPAND(SP3_NAME)) {
1316 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1317 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
1318 }
1319#endif
Dees_Troy51127312012-09-08 13:08:49 -04001320 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001321 }
Dees_Troy51127312012-09-08 13:08:49 -04001322 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1323 string current_storage_path = DataManager::GetCurrentStoragePath();
1324 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001325 if (FreeStorage != NULL) {
1326 // Attempt to mount storage
1327 if (!FreeStorage->Mount(false)) {
1328 // We couldn't mount storage... check to see if we have dual storage
1329 int has_dual_storage;
1330 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual_storage);
1331 if (has_dual_storage == 1) {
1332 // We have dual storage, see if we're using the internal storage that should always be present
1333 if (current_storage_path == DataManager::GetSettingsStoragePath()) {
Dees_Troyab10ee22012-09-21 14:27:30 -04001334 if (!FreeStorage->Is_Encrypted) {
1335 // Not able to use internal, so error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001336 LOGERR("Unable to mount internal storage.\n");
Dees_Troyab10ee22012-09-21 14:27:30 -04001337 }
Dees_Troy8170a922012-09-18 15:40:25 -04001338 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1339 } else {
1340 // We were using external, flip to internal
1341 DataManager::SetValue(TW_USE_EXTERNAL_STORAGE, 0);
1342 current_storage_path = DataManager::GetCurrentStoragePath();
1343 FreeStorage = Find_Partition_By_Path(current_storage_path);
1344 if (FreeStorage != NULL) {
1345 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1346 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001347 LOGERR("Unable to locate internal storage partition.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001348 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1349 }
1350 }
1351 } else {
1352 // No dual storage and unable to mount storage, error!
Dees_Troy2673cec2013-04-02 20:22:16 +00001353 LOGERR("Unable to mount storage.\n");
Dees_Troy8170a922012-09-18 15:40:25 -04001354 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1355 }
1356 } else {
1357 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
1358 }
1359 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001360 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001361 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001362 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001363 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001364 return;
1365}
1366
1367int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001368#ifdef TW_INCLUDE_CRYPTO
1369 int ret_val, password_len;
1370 char crypto_blkdev[255], cPassword[255];
1371 size_t result;
1372
1373 property_set("ro.crypto.state", "encrypted");
1374#ifdef TW_INCLUDE_JB_CRYPTO
1375 // No extra flags needed
1376#else
1377 property_set("ro.crypto.fs_type", CRYPTO_FS_TYPE);
1378 property_set("ro.crypto.fs_real_blkdev", CRYPTO_REAL_BLKDEV);
1379 property_set("ro.crypto.fs_mnt_point", CRYPTO_MNT_POINT);
1380 property_set("ro.crypto.fs_options", CRYPTO_FS_OPTIONS);
1381 property_set("ro.crypto.fs_flags", CRYPTO_FS_FLAGS);
1382 property_set("ro.crypto.keyfile.userdata", CRYPTO_KEY_LOC);
a39552696ff55ce2013-01-08 16:14:56 +00001383
1384#ifdef CRYPTO_SD_FS_TYPE
Ethan Yonker71413f42014-02-26 13:36:08 -06001385 property_set("ro.crypto.sd_fs_type", CRYPTO_SD_FS_TYPE);
1386 property_set("ro.crypto.sd_fs_real_blkdev", CRYPTO_SD_REAL_BLKDEV);
1387 property_set("ro.crypto.sd_fs_mnt_point", EXPAND(TW_INTERNAL_STORAGE_PATH));
Dees_Troy5bf43922012-09-07 16:07:55 -04001388#endif
a39552696ff55ce2013-01-08 16:14:56 +00001389
1390 property_set("rw.km_fips_status", "ready");
1391
1392#endif
1393
1394 // some samsung devices store "footer" on efs partition
1395 TWPartition *efs = Find_Partition_By_Path("/efs");
1396 if(efs && !efs->Is_Mounted())
1397 efs->Mount(false);
1398 else
1399 efs = 0;
1400#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001401#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
a39552696ff55ce2013-01-08 16:14:56 +00001402 TWPartition* sdcard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
Dees_Troy85f44ed2013-01-09 18:42:36 +00001403 if (sdcard && sdcard->Mount(false)) {
a39552696ff55ce2013-01-08 16:14:56 +00001404 property_set("ro.crypto.external_encrypted", "1");
1405 property_set("ro.crypto.external_blkdev", sdcard->Actual_Block_Device.c_str());
1406 } else {
1407 property_set("ro.crypto.external_encrypted", "0");
1408 }
1409#endif
Dees_Troy20c02c02013-01-10 14:14:10 +00001410#endif
a39552696ff55ce2013-01-08 16:14:56 +00001411
Dees_Troy5bf43922012-09-07 16:07:55 -04001412 strcpy(cPassword, Password.c_str());
a39552696ff55ce2013-01-08 16:14:56 +00001413 int pwret = cryptfs_check_passwd(cPassword);
1414
1415 if (pwret != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001416 LOGERR("Failed to decrypt data.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001417 return -1;
1418 }
a39552696ff55ce2013-01-08 16:14:56 +00001419
1420 if(efs)
1421 efs->UnMount(false);
1422
Dees_Troy5bf43922012-09-07 16:07:55 -04001423 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1424 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001425 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001426 } else {
1427 TWPartition* dat = Find_Partition_By_Path("/data");
1428 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001429 DataManager::SetValue(TW_DATA_BLK_DEVICE, dat->Primary_Block_Device);
Dees_Troy5bf43922012-09-07 16:07:55 -04001430 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1431 dat->Is_Decrypted = true;
1432 dat->Decrypted_Block_Device = crypto_blkdev;
Gary Peck82599a82012-11-21 16:23:12 -08001433 dat->Setup_File_System(false);
Dees_Troy74fb2e92013-04-15 14:35:47 +00001434 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 +00001435 gui_print("Data successfully decrypted, new block device: '%s'\n", crypto_blkdev);
a39552696ff55ce2013-01-08 16:14:56 +00001436
1437#ifdef CRYPTO_SD_FS_TYPE
1438 char crypto_blkdev_sd[255];
1439 property_get("ro.crypto.sd_fs_crypto_blkdev", crypto_blkdev_sd, "error");
1440 if (strcmp(crypto_blkdev_sd, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001441 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troyc8bafa12013-01-10 15:43:00 +00001442 } else if(TWPartition* emmc = Find_Partition_By_Path(EXPAND(TW_INTERNAL_STORAGE_PATH))){
a39552696ff55ce2013-01-08 16:14:56 +00001443 emmc->Is_Decrypted = true;
1444 emmc->Decrypted_Block_Device = crypto_blkdev_sd;
1445 emmc->Setup_File_System(false);
Dees_Troy2673cec2013-04-02 20:22:16 +00001446 gui_print("Internal SD successfully decrypted, new block device: '%s'\n", crypto_blkdev_sd);
a39552696ff55ce2013-01-08 16:14:56 +00001447 }
a39552696ff55ce2013-01-08 16:14:56 +00001448#endif //ifdef CRYPTO_SD_FS_TYPE
Dees_Troy85f44ed2013-01-09 18:42:36 +00001449#ifdef TW_EXTERNAL_STORAGE_PATH
Dees_Troy20c02c02013-01-10 14:14:10 +00001450#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy85f44ed2013-01-09 18:42:36 +00001451 char is_external_decrypted[255];
1452 property_get("ro.crypto.external_use_ecryptfs", is_external_decrypted, "0");
1453 if (strcmp(is_external_decrypted, "1") == 0) {
1454 sdcard->Is_Decrypted = true;
1455 sdcard->EcryptFS_Password = Password;
1456 sdcard->Decrypted_Block_Device = sdcard->Actual_Block_Device;
Dees_Troy999b39d2013-01-14 15:36:13 +00001457 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
1458 MetaEcfsFile += "/.MetaEcfsFile";
1459 if (!TWFunc::Path_Exists(MetaEcfsFile)) {
1460 // External storage isn't actually encrypted so unmount and remount without ecryptfs
1461 sdcard->UnMount(false);
1462 sdcard->Mount(false);
1463 }
Dees_Troy85f44ed2013-01-09 18:42:36 +00001464 } else {
Dees_Troy066eb302013-08-23 17:20:32 +00001465 LOGINFO("External storage '%s' is not encrypted.\n", sdcard->Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +00001466 sdcard->Is_Decrypted = false;
1467 sdcard->Decrypted_Block_Device = "";
1468 }
Dees_Troy20c02c02013-01-10 14:14:10 +00001469#endif
Dees_Troy85f44ed2013-01-09 18:42:36 +00001470#endif //ifdef TW_EXTERNAL_STORAGE_PATH
a39552696ff55ce2013-01-08 16:14:56 +00001471
Dees_Troy5bf43922012-09-07 16:07:55 -04001472 // Sleep for a bit so that the device will be ready
1473 sleep(1);
Dees_Troy16b74352012-11-14 22:27:31 +00001474#ifdef RECOVERY_SDCARD_ON_DATA
1475 if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1476 dat->Storage_Path = "/data/media/0";
1477 dat->Symlink_Path = dat->Storage_Path;
Dees Troy98fb46c2013-12-04 16:56:45 +00001478 DataManager::SetValue("tw_storage_path", "/data/media/0");
Dees_Troy16b74352012-11-14 22:27:31 +00001479 dat->UnMount(false);
Dees_Troydc8bc1b2013-01-17 01:39:28 +00001480 Output_Partition(dat);
Dees_Troy16b74352012-11-14 22:27:31 +00001481 }
1482#endif
Dees_Troy5bf43922012-09-07 16:07:55 -04001483 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001484 UnMount_Main_Partitions();
Dees_Troy5bf43922012-09-07 16:07:55 -04001485 } else
Dees_Troy2673cec2013-04-02 20:22:16 +00001486 LOGERR("Unable to locate data partition.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001487 }
1488 return 0;
1489#else
Dees_Troy2673cec2013-04-02 20:22:16 +00001490 LOGERR("No crypto support was compiled into this build.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001491 return -1;
1492#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001493 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001494}
1495
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001496int TWPartitionManager::Fix_Permissions(void) {
1497 int result = 0;
1498 if (!Mount_By_Path("/data", true))
1499 return false;
1500
1501 if (!Mount_By_Path("/system", true))
1502 return false;
1503
1504 Mount_By_Path("/sd-ext", false);
1505
1506 fixPermissions perms;
1507 result = perms.fixPerms(true, false);
Dees_Troy1a650e62012-10-19 20:54:32 -04001508 UnMount_Main_Partitions();
Dees_Troy2673cec2013-04-02 20:22:16 +00001509 gui_print("Done.\n\n");
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001510 return result;
1511}
1512
Dees_Troyd21618c2012-10-14 18:48:49 -04001513int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001514 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1515
1516 if (Part == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001517 LOGERR("Unable to locate volume information for USB storage mode.");
Dees_Troyd21618c2012-10-14 18:48:49 -04001518 return false;
1519 }
1520 if (!Part->UnMount(true))
1521 return false;
1522
Dees_Troy2673cec2013-04-02 20:22:16 +00001523 if (TWFunc::write_file(Lun_File, Part->Actual_Block_Device)) {
1524 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001525 return false;
1526 }
Matt Mowerd9cb9062013-12-14 20:34:45 -06001527 property_set("sys.storage.ums_enabled", "1");
Dees_Troyd21618c2012-10-14 18:48:49 -04001528 return true;
1529}
1530
Dees_Troy8170a922012-09-18 15:40:25 -04001531int TWPartitionManager::usb_storage_enable(void) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001532 int has_dual, has_data_media;
Dees_Troy8170a922012-09-18 15:40:25 -04001533 char lun_file[255];
Dees_Troy8170a922012-09-18 15:40:25 -04001534 string ext_path;
Dees_Troyd21618c2012-10-14 18:48:49 -04001535 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001536
1537 DataManager::GetValue(TW_HAS_DUAL_STORAGE, has_dual);
1538 DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media);
1539 if (has_dual == 1 && has_data_media == 0) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001540 string Lun_File_str = CUSTOM_LUN_FILE;
1541 size_t found = Lun_File_str.find("%");
1542 if (found != string::npos) {
1543 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1544 if (TWFunc::Path_Exists(lun_file))
1545 has_multiple_lun = true;
1546 }
1547 if (!has_multiple_lun) {
Dees_Troyf4ca6122012-10-13 22:17:20 -04001548 // Device doesn't have multiple lun files, mount current storage
Dees_Troyf4ca6122012-10-13 22:17:20 -04001549 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001550 return Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001551 } else {
1552 // Device has multiple lun files
Dees_Troyf4ca6122012-10-13 22:17:20 -04001553 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001554 if (!Open_Lun_File(DataManager::GetSettingsStoragePath(), lun_file))
Dees_Troyf4ca6122012-10-13 22:17:20 -04001555 return false;
Dees_Troyf4ca6122012-10-13 22:17:20 -04001556 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troyf4ca6122012-10-13 22:17:20 -04001557 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Dees_Troyd21618c2012-10-14 18:48:49 -04001558 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001559 }
Dees_Troy8170a922012-09-18 15:40:25 -04001560 } else {
1561 if (has_data_media == 0)
1562 ext_path = DataManager::GetCurrentStoragePath();
1563 else
1564 DataManager::GetValue(TW_EXTERNAL_PATH, ext_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001565 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Dees_Troyd21618c2012-10-14 18:48:49 -04001566 return Open_Lun_File(ext_path, lun_file);
Dees_Troy8170a922012-09-18 15:40:25 -04001567 }
1568 return true;
1569}
1570
1571int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001572 int index, ret;
1573 char lun_file[255], ch[2] = {0, 0};
1574 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001575
1576 for (index=0; index<2; index++) {
1577 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001578 ret = TWFunc::write_file(lun_file, str);
1579 Mount_All_Storage();
1580 Update_System_Details();
1581 if (ret < 0) {
1582 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001583 }
Dees_Troy8170a922012-09-18 15:40:25 -04001584 }
Dees_Troye58d5262012-09-21 12:27:57 -04001585 Mount_All_Storage();
1586 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001587 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001588 property_set("sys.storage.ums_enabled", "0");
Dees_Troy2673cec2013-04-02 20:22:16 +00001589 if (ret < 0 && index == 0) {
1590 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1591 return false;
1592 } else {
1593 return true;
1594 }
Dees_Troy8170a922012-09-18 15:40:25 -04001595 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001596}
1597
1598void TWPartitionManager::Mount_All_Storage(void) {
1599 std::vector<TWPartition*>::iterator iter;
1600
1601 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1602 if ((*iter)->Is_Storage)
1603 (*iter)->Mount(false);
1604 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001605}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001606
Dees_Troyd0384ef2012-10-12 12:15:42 -04001607void TWPartitionManager::UnMount_Main_Partitions(void) {
1608 // Unmounts system and data if data is not data/media
1609 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001610 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001611
1612 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1613
1614 UnMount_By_Path("/system", true);
1615#ifndef RECOVERY_SDCARD_ON_DATA
1616 UnMount_By_Path("/data", true);
1617#endif
1618 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1619 Boot_Partition->UnMount(true);
1620}
1621
Dees_Troy9350b8d2012-09-27 12:38:38 -04001622int TWPartitionManager::Partition_SDCard(void) {
1623 char mkdir_path[255], temp[255], line[512];
1624 string Command, Device, fat_str, ext_str, swap_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
1625 int ext, swap, total_size = 0, fat_size;
1626 FILE* fp;
1627
Dees_Troy2673cec2013-04-02 20:22:16 +00001628 gui_print("Partitioning SD Card...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001629#ifdef TW_EXTERNAL_STORAGE_PATH
1630 TWPartition* SDCard = Find_Partition_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH));
1631#else
1632 TWPartition* SDCard = Find_Partition_By_Path("/sdcard");
1633#endif
1634 if (SDCard == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001635 LOGERR("Unable to locate device to partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001636 return false;
1637 }
1638 if (!SDCard->UnMount(true))
1639 return false;
1640 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
1641 if (SDext != NULL) {
1642 if (!SDext->UnMount(true))
1643 return false;
1644 }
Vojtech Bocek05534202013-09-11 08:11:56 +02001645
1646 TWFunc::Exec_Cmd("umount \"$SWAPPATH\"");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001647 Device = SDCard->Actual_Block_Device;
1648 // Just use the root block device
1649 Device.resize(strlen("/dev/block/mmcblkX"));
1650
1651 // Find the size of the block device:
1652 fp = fopen("/proc/partitions", "rt");
1653 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001654 LOGERR("Unable to open /proc/partitions\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001655 return false;
1656 }
1657
1658 while (fgets(line, sizeof(line), fp) != NULL)
1659 {
1660 unsigned long major, minor, blocks;
1661 char device[512];
1662 char tmpString[64];
1663
1664 if (strlen(line) < 7 || line[0] == 'm') continue;
1665 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
1666
1667 tmpdevice = "/dev/block/";
1668 tmpdevice += device;
1669 if (tmpdevice == Device) {
1670 // Adjust block size to byte size
1671 total_size = (int)(blocks * 1024ULL / 1000000LLU);
1672 break;
1673 }
1674 }
1675 fclose(fp);
1676
1677 DataManager::GetValue("tw_sdext_size", ext);
1678 DataManager::GetValue("tw_swap_size", swap);
1679 DataManager::GetValue("tw_sdpart_file_system", ext_format);
1680 fat_size = total_size - ext - swap;
Dees_Troy2673cec2013-04-02 20:22:16 +00001681 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 -04001682 memset(temp, 0, sizeof(temp));
1683 sprintf(temp, "%i", fat_size);
1684 fat_str = temp;
1685 memset(temp, 0, sizeof(temp));
1686 sprintf(temp, "%i", fat_size + ext);
1687 ext_str = temp;
1688 memset(temp, 0, sizeof(temp));
1689 sprintf(temp, "%i", fat_size + ext + swap);
1690 swap_str = temp;
1691 if (ext + swap > total_size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001692 LOGERR("EXT + Swap size is larger than sdcard size.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001693 return false;
1694 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001695 gui_print("Removing partition table...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001696 Command = "parted -s " + Device + " mklabel msdos";
Dees_Troy2673cec2013-04-02 20:22:16 +00001697 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001698 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 LOGERR("Unable to remove partition table.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001700 Update_System_Details();
1701 return false;
1702 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001703 gui_print("Creating FAT32 partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001704 Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001705 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001706 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001707 LOGERR("Unable to create FAT32 partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001708 return false;
1709 }
1710 if (ext > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001711 gui_print("Creating EXT partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001712 Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001713 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001714 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001715 LOGERR("Unable to create EXT partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001716 Update_System_Details();
1717 return false;
1718 }
1719 }
1720 if (swap > 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001721 gui_print("Creating swap partition...\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001722 Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB";
Dees_Troy2673cec2013-04-02 20:22:16 +00001723 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001724 if (TWFunc::Exec_Cmd(Command) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001725 LOGERR("Unable to create swap partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001726 Update_System_Details();
1727 return false;
1728 }
1729 }
1730 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
1731#ifdef TW_EXTERNAL_STORAGE_PATH
1732 Mount_By_Path(EXPAND(TW_EXTERNAL_STORAGE_PATH), 1);
1733 DataManager::GetValue(TW_EXTERNAL_PATH, sd_path);
1734 memset(mkdir_path, 0, sizeof(mkdir_path));
1735 sprintf(mkdir_path, "%s/TWRP", sd_path.c_str());
1736#else
1737 Mount_By_Path("/sdcard", 1);
1738 strcpy(mkdir_path, "/sdcard/TWRP");
1739#endif
1740 mkdir(mkdir_path, 0777);
1741 DataManager::Flush();
1742#ifdef TW_EXTERNAL_STORAGE_PATH
1743 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1744 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1745 DataManager::SetValue(TW_ZIP_LOCATION_VAR, EXPAND(TW_EXTERNAL_STORAGE_PATH));
1746#else
1747 DataManager::SetValue(TW_ZIP_EXTERNAL_VAR, "/sdcard");
1748 if (DataManager::GetIntValue(TW_USE_EXTERNAL_STORAGE) == 1)
1749 DataManager::SetValue(TW_ZIP_LOCATION_VAR, "/sdcard");
1750#endif
1751 if (ext > 0) {
1752 if (SDext == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001753 LOGERR("Unable to locate sd-ext partition.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001754 return false;
1755 }
1756 Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001757 gui_print("Formatting sd-ext as %s...\n", ext_format.c_str());
1758 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001759 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04001760 }
1761
1762 Update_System_Details();
Dees_Troy2673cec2013-04-02 20:22:16 +00001763 gui_print("Partitioning complete.\n");
Dees_Troy9350b8d2012-09-27 12:38:38 -04001764 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001765}
Dees_Troya13d74f2013-03-24 08:54:55 -05001766
1767void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
1768 std::vector<TWPartition*>::iterator iter;
1769 if (ListType == "mount") {
1770 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1771 if ((*iter)->Can_Be_Mounted && !(*iter)->Is_SubPartition) {
1772 struct PartitionList part;
1773 part.Display_Name = (*iter)->Display_Name;
1774 part.Mount_Point = (*iter)->Mount_Point;
1775 part.selected = (*iter)->Is_Mounted();
1776 Partition_List->push_back(part);
1777 }
1778 }
1779 } else if (ListType == "storage") {
1780 char free_space[255];
1781 string Current_Storage = DataManager::GetCurrentStoragePath();
1782 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1783 if ((*iter)->Is_Storage) {
1784 struct PartitionList part;
1785 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
1786 part.Display_Name = (*iter)->Storage_Name + " (";
1787 part.Display_Name += free_space;
1788 part.Display_Name += "MB)";
1789 part.Mount_Point = (*iter)->Storage_Path;
1790 if ((*iter)->Storage_Path == Current_Storage)
1791 part.selected = 1;
1792 else
1793 part.selected = 0;
1794 Partition_List->push_back(part);
1795 }
1796 }
1797 } else if (ListType == "backup") {
1798 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001799 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05001800 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001801 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001802 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00001803 Backup_Size = (*iter)->Backup_Size;
1804 if ((*iter)->Has_SubPartition) {
1805 std::vector<TWPartition*>::iterator subpart;
1806
1807 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
1808 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
1809 Backup_Size += (*subpart)->Backup_Size;
1810 }
1811 }
1812 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05001813 part.Display_Name = (*iter)->Backup_Display_Name + " (";
1814 part.Display_Name += backup_size;
1815 part.Display_Name += "MB)";
1816 part.Mount_Point = (*iter)->Backup_Path;
1817 part.selected = 0;
1818 Partition_List->push_back(part);
1819 }
1820 }
1821 } else if (ListType == "restore") {
1822 string Restore_List, restore_path;
1823 TWPartition* restore_part = NULL;
1824
1825 DataManager::GetValue("tw_restore_list", Restore_List);
1826 if (!Restore_List.empty()) {
1827 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
1828 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1829 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
Dees_Troy59df9262013-06-19 14:53:57 -05001830 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00001831 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05001832 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05001833 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05001834 } else {
1835 struct PartitionList part;
1836 part.Display_Name = restore_part->Backup_Display_Name;
1837 part.Mount_Point = restore_part->Backup_Path;
1838 part.selected = 1;
1839 Partition_List->push_back(part);
1840 }
1841 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001842 LOGERR("Unable to locate '%s' partition for restore.\n", restore_path.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001843 }
1844 start_pos = end_pos + 1;
1845 end_pos = Restore_List.find(";", start_pos);
1846 }
1847 }
1848 } else if (ListType == "wipe") {
1849 struct PartitionList dalvik;
1850 dalvik.Display_Name = "Dalvik Cache";
1851 dalvik.Mount_Point = "DALVIK";
1852 dalvik.selected = 0;
1853 Partition_List->push_back(dalvik);
1854 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1855 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
1856 struct PartitionList part;
1857 part.Display_Name = (*iter)->Display_Name;
1858 part.Mount_Point = (*iter)->Mount_Point;
1859 part.selected = 0;
1860 Partition_List->push_back(part);
1861 }
1862 if ((*iter)->Has_Android_Secure) {
1863 struct PartitionList part;
1864 part.Display_Name = (*iter)->Backup_Display_Name;
1865 part.Mount_Point = (*iter)->Backup_Path;
1866 part.selected = 0;
1867 Partition_List->push_back(part);
1868 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00001869 if ((*iter)->Has_Data_Media) {
1870 struct PartitionList datamedia;
1871 datamedia.Display_Name = (*iter)->Storage_Name;
1872 datamedia.Mount_Point = "INTERNAL";
1873 datamedia.selected = 0;
1874 Partition_List->push_back(datamedia);
1875 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001876 }
1877 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001878 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05001879 }
1880}
1881
1882int TWPartitionManager::Fstab_Processed(void) {
1883 return Partitions.size();
1884}
Dees_Troyd93bda52013-07-03 19:55:19 +00001885
1886void TWPartitionManager::Output_Storage_Fstab(void) {
1887 std::vector<TWPartition*>::iterator iter;
1888 char storage_partition[255];
1889 string Temp;
1890 FILE *fp = fopen("/cache/recovery/storage.fstab", "w");
1891
1892 if (fp == NULL) {
1893 LOGERR("Unable to open '/cache/recovery/storage.fstab'.\n");
1894 return;
1895 }
1896
1897 // Iterate through all partitions
1898 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1899 if ((*iter)->Is_Storage) {
1900 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
1901 strcpy(storage_partition, Temp.c_str());
1902 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
1903 }
1904 }
1905 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001906}