blob: cdd9847f3cc11629ba5f8761477319d31f6b3c42 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05002 Copyright 2013 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 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>
Dees_Troy5bf43922012-09-07 16:07:55 -040024#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040025#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040026#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#include <iostream>
28#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040029
Dees_Troy657c3092012-09-10 20:32:10 -040030#ifdef TW_INCLUDE_CRYPTO
31 #include "cutils/properties.h"
32#endif
33
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050034#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000036#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040037#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040038#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040039#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050040#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041#include "twrpTar.hpp"
bigbiff bigbiff34684ff2013-12-01 21:03:45 -050042#include "twrpDU.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040043extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040044 #include "mtdutils/mtdutils.h"
45 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000046#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
47 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
48#endif
Dees_Troya95f55c2013-08-17 13:14:43 +000049#ifdef USE_EXT4
50 #include "make_ext4fs.h"
51#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040052}
bigbiff bigbiffc49d7062013-10-11 20:28:00 -040053#ifdef HAVE_SELINUX
54#include "selinux/selinux.h"
55#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040056
bigbiff bigbiff9c754052013-01-09 09:09:08 -050057using namespace std;
58
Dees_Troya95f55c2013-08-17 13:14:43 +000059extern struct selabel_handle *selinux_handle;
60
Hashcode62bd9e02013-11-19 21:59:42 -080061struct flag_list {
62 const char *name;
63 unsigned flag;
64};
65
66static struct flag_list mount_flags[] = {
67 { "noatime", MS_NOATIME },
68 { "noexec", MS_NOEXEC },
69 { "nosuid", MS_NOSUID },
70 { "nodev", MS_NODEV },
71 { "nodiratime", MS_NODIRATIME },
72 { "ro", MS_RDONLY },
73 { "rw", 0 },
74 { "remount", MS_REMOUNT },
75 { "bind", MS_BIND },
76 { "rec", MS_REC },
Dees Troyc4bc30e2014-02-03 15:04:19 +000077#ifdef MS_UNBINDABLE
Hashcode62bd9e02013-11-19 21:59:42 -080078 { "unbindable", MS_UNBINDABLE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000079#endif
80#ifdef MS_PRIVATE
Hashcode62bd9e02013-11-19 21:59:42 -080081 { "private", MS_PRIVATE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000082#endif
83#ifdef MS_SLAVE
Hashcode62bd9e02013-11-19 21:59:42 -080084 { "slave", MS_SLAVE },
Dees Troyc4bc30e2014-02-03 15:04:19 +000085#endif
86#ifdef MS_SHARED
Hashcode62bd9e02013-11-19 21:59:42 -080087 { "shared", MS_SHARED },
Dees Troyc4bc30e2014-02-03 15:04:19 +000088#endif
Hashcode62bd9e02013-11-19 21:59:42 -080089 { "sync", MS_SYNCHRONOUS },
90 { "defaults", 0 },
91 { 0, 0 },
92};
93
Dees_Troy51a0e822012-09-05 15:24:24 -040094TWPartition::TWPartition(void) {
95 Can_Be_Mounted = false;
96 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050097 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +020098 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040099 Wipe_During_Factory_Reset = false;
100 Wipe_Available_in_GUI = false;
101 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -0400102 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400103 SubPartition_Of = "";
104 Symlink_Path = "";
105 Symlink_Mount_Point = "";
106 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -0400107 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400108 Actual_Block_Device = "";
109 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400110 Alternate_Block_Device = "";
111 Removable = false;
112 Is_Present = false;
113 Length = 0;
114 Size = 0;
115 Used = 0;
116 Free = 0;
117 Backup_Size = 0;
118 Can_Be_Encrypted = false;
119 Is_Encrypted = false;
120 Is_Decrypted = false;
121 Decrypted_Block_Device = "";
122 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500123 Backup_Display_Name = "";
124 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400125 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400126 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400127 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400128 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000129 Can_Encrypt_Backup = false;
130 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400131 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400132 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500134 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400135 Storage_Path = "";
136 Current_File_System = "";
137 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800138 Mount_Flags = 0;
139 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400140 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000141 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000142 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000143#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
144 EcryptFS_Password = "";
145#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400146}
147
148TWPartition::~TWPartition(void) {
149 // Do nothing
150}
151
Dees_Troy5bf43922012-09-07 16:07:55 -0400152bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
153 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
154 int line_len = Line.size(), index = 0, item_index = 0;
155 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400156 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400157 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500158 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400159
Dees_Troy51127312012-09-08 13:08:49 -0400160 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500161 if (full_line[index] == 34)
162 skip = !skip;
163 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400164 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400165 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400166 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000167 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400168 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500169 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000170 Display_Name = full_line + 1;
171 Backup_Display_Name = Display_Name;
172 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400173 index = Mount_Point.size();
174 while (index < line_len) {
175 while (index < line_len && full_line[index] == '\0')
176 index++;
177 if (index >= line_len)
178 continue;
179 ptr = full_line + index;
180 if (item_index == 0) {
181 // File System
182 Fstab_File_System = ptr;
183 Current_File_System = ptr;
184 item_index++;
185 } else if (item_index == 1) {
186 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400187 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400188 MTD_Name = ptr;
189 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400190 } else if (Fstab_File_System == "bml") {
191 if (Mount_Point == "/boot")
192 MTD_Name = "boot";
193 else if (Mount_Point == "/recovery")
194 MTD_Name = "recovery";
195 Primary_Block_Device = ptr;
196 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000197 LOGERR("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400198 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400199 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000200 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400201 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000202 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400203 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400204 } else {
205 Primary_Block_Device = ptr;
206 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400207 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400208 item_index++;
209 } else if (item_index > 1) {
210 if (*ptr == '/') {
211 // Alternate Block Device
212 Alternate_Block_Device = ptr;
213 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
214 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
215 // Partition length
216 ptr += 7;
217 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400218 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
219 // Custom flags, save for later so that new values aren't overwritten by defaults
220 ptr += 6;
221 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000222 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400223 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
224 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400225 } else {
226 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000227 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400228 }
229 }
230 while (index < line_len && full_line[index] != '\0')
231 index++;
232 }
233
234 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
235 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000236 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400237 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000238 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400239 return 0;
240 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400241 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400242 Setup_File_System(Display_Error);
243 if (Mount_Point == "/system") {
244 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500245 Backup_Display_Name = Display_Name;
246 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500248 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400249 } else if (Mount_Point == "/data") {
250 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500251 Backup_Display_Name = Display_Name;
252 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400253 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400254 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500255 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000256 Can_Encrypt_Backup = true;
257 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400258#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500259 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400261 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500262 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400263 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000264 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400265 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
266 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400267 Symlink_Mount_Point = "/emmc";
268 } else {
269 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400270 Symlink_Mount_Point = "/sdcard";
271 }
Dees_Troy16b74352012-11-14 22:27:31 +0000272 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
273 Storage_Path = "/data/media/0";
274 Symlink_Path = Storage_Path;
275 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
276 UnMount(true);
277 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400278#endif
279#ifdef TW_INCLUDE_CRYPTO
280 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400281 char crypto_blkdev[255];
282 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
283 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400284 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400285 DataManager::SetValue(TW_IS_DECRYPTED, 1);
286 Is_Encrypted = true;
287 Is_Decrypted = true;
288 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000289 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400290 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400291 Is_Encrypted = true;
292 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800293 Can_Be_Mounted = false;
294 Current_File_System = "emmc";
295 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400296 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
297 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
298 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800299 } else {
300 // Filesystem is not encrypted and the mount
301 // succeeded, so get it back to the original
302 // unmounted state
303 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400304 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400305 #ifdef RECOVERY_SDCARD_ON_DATA
306 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
307 Recreate_Media_Folder();
308 #endif
309#else
310 #ifdef RECOVERY_SDCARD_ON_DATA
311 Recreate_Media_Folder();
312 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400313#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400314 } else if (Mount_Point == "/cache") {
315 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500316 Backup_Display_Name = Display_Name;
317 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400318 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400319 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500320 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400321 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000322 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500323 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
324 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400325 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400326 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400327 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400328 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500329 Backup_Display_Name = Display_Name;
330 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400331 Is_SubPartition = true;
332 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400333 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500334 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000335 Can_Encrypt_Backup = true;
336 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400337 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400338 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400339 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500340 Backup_Display_Name = Display_Name;
341 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400342 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400343 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500344 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000345 Can_Encrypt_Backup = true;
346 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400347 } else if (Mount_Point == "/boot") {
348 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500349 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400350 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500351 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400352 }
353#ifdef TW_EXTERNAL_STORAGE_PATH
354 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
355 Is_Storage = true;
356 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400357 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500358 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400359#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000360 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400361 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400362 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500363 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400364#ifndef RECOVERY_SDCARD_ON_DATA
365 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400366 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400367#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400368#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000369 }
Dees_Troy8170a922012-09-18 15:40:25 -0400370#ifdef TW_INTERNAL_STORAGE_PATH
371 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
372 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500373 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400374 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500375 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400376#ifndef RECOVERY_SDCARD_ON_DATA
377 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400378 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400379#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400380 }
381#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000382 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400383 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500384 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500385 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400386#ifndef RECOVERY_SDCARD_ON_DATA
387 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400388 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400389#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400390 }
391#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400392 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400393 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400394 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500395 if (Mount_Point == "/boot") {
396 Display_Name = "Boot";
397 Backup_Display_Name = Display_Name;
398 Can_Be_Backed_Up = true;
399 } else if (Mount_Point == "/recovery") {
400 Display_Name = "Recovery";
401 Backup_Display_Name = Display_Name;
402 Can_Be_Backed_Up = true;
403 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400404 }
405
Dees_Troy51127312012-09-08 13:08:49 -0400406 // Process any custom flags
407 if (Flags.size() > 0)
408 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400409 return true;
410}
411
Hashcode62bd9e02013-11-19 21:59:42 -0800412bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
413 int i;
414 char *p;
415 char *savep;
416 char fs_options[250];
417
418 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
419 Options = "";
420
421 p = strtok_r(fs_options, ",", &savep);
422 while (p) {
423 /* Look for the flag "p" in the flag list "fl"
424 * If not found, the loop exits with fl[i].name being null.
425 */
426 for (i = 0; mount_flags[i].name; i++) {
427 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
428 Flags |= mount_flags[i].flag;
429 break;
430 }
431 }
432
433 if (!mount_flags[i].name) {
434 if (Options.size() > 0)
435 Options += ",";
436 Options += p;
437 }
438 p = strtok_r(NULL, ",", &savep);
439 }
440
441 return true;
442}
443
Dees_Troy51127312012-09-08 13:08:49 -0400444bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
445 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500446 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400447 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500448 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400449
450 strcpy(flags, Flags.c_str());
451 flags_len = Flags.size();
452 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500453 if (flags[index] == 34)
454 skip = !skip;
455 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400456 flags[index] = '\0';
457 }
458
459 index = 0;
460 while (index < flags_len) {
461 while (index < flags_len && flags[index] == '\0')
462 index++;
463 if (index >= flags_len)
464 continue;
465 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500466 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400467 if (strcmp(ptr, "removable") == 0) {
468 Removable = true;
469 } else if (strcmp(ptr, "storage") == 0) {
470 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500471 } else if (strcmp(ptr, "settingsstorage") == 0) {
472 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400473 } else if (strcmp(ptr, "canbewiped") == 0) {
474 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700475 } else if (strcmp(ptr, "usermrf") == 0) {
476 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500477 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
478 ptr += 7;
479 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
480 Can_Be_Backed_Up = true;
481 else
482 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400483 } else if (strcmp(ptr, "wipeingui") == 0) {
484 Can_Be_Wiped = true;
485 Wipe_Available_in_GUI = true;
486 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
487 Can_Be_Wiped = true;
488 Wipe_Available_in_GUI = true;
489 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500490 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000491 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400492 Is_SubPartition = true;
493 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000494 } else if (strcmp(ptr, "ignoreblkid") == 0) {
495 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000496 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
497 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400499 ptr += 8;
500 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500501 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
502 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400503 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500504 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400505 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500506 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
507 Display_Name.resize(Display_Name.size() - 1);
508 }
509 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
510 has_storage_name = true;
511 ptr += 11;
512 if (*ptr == '\"') ptr++;
513 Storage_Name = ptr;
514 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
515 Storage_Name.resize(Storage_Name.size() - 1);
516 }
517 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
518 has_backup_name = true;
519 ptr += 10;
520 if (*ptr == '\"') ptr++;
521 Backup_Display_Name = ptr;
522 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
523 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
524 }
525 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400526 ptr += 10;
527 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500528 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400529 ptr += 7;
530 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000531 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
532 ptr += 17;
533 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
534 Can_Encrypt_Backup = true;
535 else
536 Can_Encrypt_Backup = false;
537 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
538 ptr += 21;
539 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
540 Can_Encrypt_Backup = true;
541 Use_Userdata_Encryption = true;
542 } else {
543 Use_Userdata_Encryption = false;
544 }
Hashcode62bd9e02013-11-19 21:59:42 -0800545 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
546 ptr += 8;
547 if (*ptr == '\"') ptr++;
548
549 Mount_Options = ptr;
550 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
551 Mount_Options.resize(Mount_Options.size() - 1);
552 }
553 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400554 } else {
555 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000556 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400557 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000558 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400559 }
560 while (index < flags_len && flags[index] != '\0')
561 index++;
562 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500563 if (has_display_name && !has_storage_name)
564 Storage_Name = Display_Name;
565 if (!has_display_name && has_storage_name)
566 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000567 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500568 Backup_Display_Name = Display_Name;
569 if (!has_display_name && has_backup_name)
570 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400571 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400572}
573
Dees_Troy5bf43922012-09-07 16:07:55 -0400574bool TWPartition::Is_File_System(string File_System) {
575 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400576 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400577 File_System == "ext4" ||
578 File_System == "vfat" ||
579 File_System == "ntfs" ||
580 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500581 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000582 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400583 File_System == "auto")
584 return true;
585 else
586 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400587}
588
Dees_Troy5bf43922012-09-07 16:07:55 -0400589bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400590 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400591 return true;
592 else
593 return false;
594}
595
Dees_Troy51127312012-09-08 13:08:49 -0400596bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400597 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400598 if (mkdir(Path.c_str(), 0777) == -1) {
599 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000600 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400601 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000602 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400603 return false;
604 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000605 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400606 return true;
607 }
608 }
609 return true;
610}
611
Dees_Troy5bf43922012-09-07 16:07:55 -0400612void TWPartition::Setup_File_System(bool Display_Error) {
613 struct statfs st;
614
615 Can_Be_Mounted = true;
616 Can_Be_Wiped = true;
617
Dees_Troy5bf43922012-09-07 16:07:55 -0400618 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400619 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400620 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
621 Backup_Name = Display_Name;
622 Backup_Method = FILES;
623}
624
625void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400626 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
627 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800628 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400629 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800630 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400631 Backup_Method = FLASH_UTILS;
632 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000633 LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400634 if (Find_Partition_Size()) {
635 Used = Size;
636 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400637 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400638 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000639 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400640 else
Dees Troyd932ce12013-10-18 17:12:59 +0000641 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400642 }
643}
644
Dees_Troye58d5262012-09-21 12:27:57 -0400645void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500646 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400647 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500648 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400649 Has_Android_Secure = true;
650 Symlink_Path = Mount_Point + "/.android_secure";
651 Symlink_Mount_Point = "/and-sec";
652 Backup_Path = Symlink_Mount_Point;
653 Make_Dir("/and-sec", true);
654 Recreate_AndSec_Folder();
655}
656
Dees_Troy5bf43922012-09-07 16:07:55 -0400657void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
658 char device[512], realDevice[512];
659
660 strcpy(device, Block.c_str());
661 memset(realDevice, 0, sizeof(realDevice));
662 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
663 {
664 strcpy(device, realDevice);
665 memset(realDevice, 0, sizeof(realDevice));
666 }
667
668 if (device[0] != '/') {
669 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000670 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400671 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000672 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400673 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400674 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400675 Block = device;
676 return;
677 }
678}
679
Dees_Troy8e337f32012-10-13 22:07:49 -0400680void TWPartition::Mount_Storage_Retry(void) {
681 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500682 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400683 int retry_count = 5;
684 while (retry_count > 0 && !Mount(false)) {
685 usleep(500000);
686 retry_count--;
687 }
688 Mount(true);
689 }
690}
691
Dees_Troy38bd7602012-09-14 13:33:53 -0400692bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
693 FILE *fp = NULL;
694 char line[255];
695
696 fp = fopen("/proc/mtd", "rt");
697 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000698 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400699 return false;
700 }
701
702 while (fgets(line, sizeof(line), fp) != NULL)
703 {
704 char device[32], label[32];
705 unsigned long size = 0;
706 char* fstype = NULL;
707 int deviceId;
708
709 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
710
711 // Skip header and blank lines
712 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
713 continue;
714
715 // Strip off the trailing " from the label
716 label[strlen(label)-1] = '\0';
717
718 if (strcmp(label, MTD_Name.c_str()) == 0) {
719 // We found our device
720 // Strip off the trailing : from the device
721 device[strlen(device)-1] = '\0';
722 if (sscanf(device,"mtd%d", &deviceId) == 1) {
723 sprintf(device, "/dev/block/mtdblock%d", deviceId);
724 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000725 fclose(fp);
726 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400727 }
728 }
729 }
730 fclose(fp);
731
732 return false;
733}
734
Dees_Troy51127312012-09-08 13:08:49 -0400735bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
736 struct statfs st;
737 string Local_Path = Mount_Point + "/.";
738
739 if (!Mount(Display_Error))
740 return false;
741
742 if (statfs(Local_Path.c_str(), &st) != 0) {
743 if (!Removable) {
744 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000745 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400746 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000747 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400748 }
749 return false;
750 }
751 Size = (st.f_blocks * st.f_bsize);
752 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
753 Free = (st.f_bfree * st.f_bsize);
754 Backup_Size = Used;
755 return true;
756}
757
758bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400759 FILE* fp;
760 char command[255], line[512];
761 int include_block = 1;
762 unsigned int min_len;
763
764 if (!Mount(Display_Error))
765 return false;
766
Dees_Troy38bd7602012-09-14 13:33:53 -0400767 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400768 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200769 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400770 fp = fopen("/tmp/dfoutput.txt", "rt");
771 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000772 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400773 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400774 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400775
776 while (fgets(line, sizeof(line), fp) != NULL)
777 {
778 unsigned long blocks, used, available;
779 char device[64];
780 char tmpString[64];
781
782 if (strncmp(line, "Filesystem", 10) == 0)
783 continue;
784 if (strlen(line) < min_len) {
785 include_block = 0;
786 continue;
787 }
788 if (include_block) {
789 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
790 } else {
791 // The device block string is so long that the df information is on the next line
792 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400793 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400794 while (tmpString[space_count] == 32)
795 space_count++;
796 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
797 }
798
799 // Adjust block size to byte size
800 Size = blocks * 1024ULL;
801 Used = used * 1024ULL;
802 Free = available * 1024ULL;
803 Backup_Size = Used;
804 }
805 fclose(fp);
806 return true;
807}
808
Dees_Troy5bf43922012-09-07 16:07:55 -0400809bool TWPartition::Find_Partition_Size(void) {
810 FILE* fp;
811 char line[512];
812 string tmpdevice;
813
igoriok87e3d932013-01-31 21:03:53 +0200814 fp = fopen("/proc/dumchar_info", "rt");
815 if (fp != NULL) {
816 while (fgets(line, sizeof(line), fp) != NULL)
817 {
818 char label[32], device[32];
819 unsigned long size = 0;
820
821 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
822
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500823 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200824 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
825 continue;
826
827 tmpdevice = "/dev/";
828 tmpdevice += label;
829 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
830 Size = size;
831 fclose(fp);
832 return true;
833 }
834 }
835 }
836
Dees_Troy5bf43922012-09-07 16:07:55 -0400837 // In this case, we'll first get the partitions we care about (with labels)
838 fp = fopen("/proc/partitions", "rt");
839 if (fp == NULL)
840 return false;
841
842 while (fgets(line, sizeof(line), fp) != NULL)
843 {
844 unsigned long major, minor, blocks;
845 char device[512];
846 char tmpString[64];
847
Dees_Troy63c8df72012-09-10 14:02:05 -0400848 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400849 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
850
851 tmpdevice = "/dev/block/";
852 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400853 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400854 // Adjust block size to byte size
855 Size = blocks * 1024ULL;
856 fclose(fp);
857 return true;
858 }
859 }
860 fclose(fp);
861 return false;
862}
863
Dees_Troy5bf43922012-09-07 16:07:55 -0400864bool TWPartition::Is_Mounted(void) {
865 if (!Can_Be_Mounted)
866 return false;
867
868 struct stat st1, st2;
869 string test_path;
870
871 // Check to see if the mount point directory exists
872 test_path = Mount_Point + "/.";
873 if (stat(test_path.c_str(), &st1) != 0) return false;
874
875 // Check to see if the directory above the mount point exists
876 test_path = Mount_Point + "/../.";
877 if (stat(test_path.c_str(), &st2) != 0) return false;
878
879 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
880 int ret = (st1.st_dev != st2.st_dev) ? true : false;
881
882 return ret;
883}
884
885bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000886 int exfat_mounted = 0;
887
Dees_Troy5bf43922012-09-07 16:07:55 -0400888 if (Is_Mounted()) {
889 return true;
890 } else if (!Can_Be_Mounted) {
891 return false;
892 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400893
894 Find_Actual_Block_Device();
895
896 // Check the current file system before mounting
897 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000898 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000899 string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point;
Dees_Troy2673cec2013-04-02 20:22:16 +0000900 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000901 string result;
902 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000903 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000904 Current_File_System = "vfat";
905 } else {
906#ifdef TW_NO_EXFAT_FUSE
907 UnMount(false);
908 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
909 // Some kernels let us mount vfat as exfat which doesn't work out too well
910#else
911 exfat_mounted = 1;
912#endif
913 }
914 }
Dees_Troy22042032012-12-18 21:23:08 +0000915 if (Fstab_File_System == "yaffs2") {
916 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000917 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
918 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
919 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
920 if (Display_Error)
921 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
922 else
923 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
924 return false;
925 } else {
926 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
927 return true;
928 }
929 } else {
930 struct stat st;
931 string test_path = Mount_Point;
932 if (stat(test_path.c_str(), &st) < 0) {
933 if (Display_Error)
934 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
935 else
936 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
937 return false;
938 }
939 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
940 if (new_mode != st.st_mode) {
941 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
942 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
943 if (Display_Error)
944 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
945 else
946 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
947 return false;
948 }
949 }
Dees_Troy22042032012-12-18 21:23:08 +0000950 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000951 }
Hashcode62bd9e02013-11-19 21:59:42 -0800952 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), Mount_Flags, Mount_Options.c_str()) != 0) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000953#ifdef TW_NO_EXFAT_FUSE
954 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000955 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000956 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000957 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000958 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000959 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000960 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800961 LOGINFO("Actual block device: '%s', current file system: '%s', flags: 0x%8x, options: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str(), Mount_Flags, Mount_Options.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000962 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000963 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000964 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000965#endif
966 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000967 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000968 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000969 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
970 LOGINFO("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000971 return false;
972#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000973 }
974#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000975 }
976#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
977 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
978 MetaEcfsFile += "/.MetaEcfsFile";
979 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
980 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
981 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000982 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000983 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000984 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000985 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000986 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000987 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400988 }
Dees_Troy066eb302013-08-23 17:20:32 +0000989 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
990 if (Is_Decrypted)
991 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000992 Is_Decrypted = false;
993 }
994#endif
995 if (Removable)
996 Update_Size(Display_Error);
997
998 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200999 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
1000 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -04001001 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001002 return true;
1003}
1004
1005bool TWPartition::UnMount(bool Display_Error) {
1006 if (Is_Mounted()) {
1007 int never_unmount_system;
1008
1009 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1010 if (never_unmount_system == 1 && Mount_Point == "/system")
1011 return true; // Never unmount system if you're not supposed to unmount it
1012
Dees_Troyc8bafa12013-01-10 15:43:00 +00001013#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1014 if (EcryptFS_Password.size() > 0) {
1015 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1016 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001017 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001018 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001019 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001020 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001021 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001022 }
1023 }
1024#endif
1025
Dees_Troy38bd7602012-09-14 13:33:53 -04001026 if (!Symlink_Mount_Point.empty())
1027 umount(Symlink_Mount_Point.c_str());
1028
Dees_Troyb05ddee2013-01-28 20:24:50 +00001029 umount(Mount_Point.c_str());
1030 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001031 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001032 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001033 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001034 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001035 return false;
1036 } else
1037 return true;
1038 } else {
1039 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001040 }
1041}
1042
Gary Peck43acadf2012-11-21 21:19:01 -08001043bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001044 bool wiped = false, update_crypt = false;
1045 int check;
1046 string Layout_Filename = Mount_Point + "/.layout_version";
1047
Dees_Troy38bd7602012-09-14 13:33:53 -04001048 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001049 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001050 return false;
1051 }
1052
Dees_Troyc51f1f92012-09-20 15:32:13 -04001053 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001054 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001055
Dees_Troyce675462013-01-09 19:48:21 +00001056#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1057 if (Mount_Point == "/data" && Mount(false)) {
1058 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1059 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1060 }
1061#endif
1062
Dees_Troy16c2b312013-01-15 16:51:18 +00001063 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1064 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1065 else
1066 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001067
Dees_Troy16c2b312013-01-15 16:51:18 +00001068 if (Has_Data_Media) {
1069 wiped = Wipe_Data_Without_Wiping_Media();
1070 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001071
Dees_Troy16c2b312013-01-15 16:51:18 +00001072 DataManager::GetValue(TW_RM_RF_VAR, check);
1073
Hashcodedabfd492013-08-29 22:45:30 -07001074 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001075 wiped = Wipe_RMRF();
1076 else if (New_File_System == "ext4")
1077 wiped = Wipe_EXT4();
1078 else if (New_File_System == "ext2" || New_File_System == "ext3")
1079 wiped = Wipe_EXT23(New_File_System);
1080 else if (New_File_System == "vfat")
1081 wiped = Wipe_FAT();
1082 else if (New_File_System == "exfat")
1083 wiped = Wipe_EXFAT();
1084 else if (New_File_System == "yaffs2")
1085 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001086 else if (New_File_System == "f2fs")
1087 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001088 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001089 LOGERR("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
Dees_Troy16c2b312013-01-15 16:51:18 +00001090 unlink("/.layout_version");
1091 return false;
1092 }
1093 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001094 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001095
Gary Pecke8bc5d72012-12-21 06:45:25 -08001096 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001097#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1098 if (Mount_Point == "/data" && Mount(false)) {
1099 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1100 Make_Dir("/data/system", true);
1101 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1102 }
1103 }
1104#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001105
Dees_Troy1c1ac442013-01-17 21:42:14 +00001106 if (Mount_Point == "/cache")
1107 DataManager::Output_Version();
1108
Dees_Troy16c2b312013-01-15 16:51:18 +00001109 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1110 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1111
1112 if (update_crypt) {
1113 Setup_File_System(false);
1114 if (Is_Encrypted && !Is_Decrypted) {
1115 // just wiped an encrypted partition back to its unencrypted state
1116 Is_Encrypted = false;
1117 Is_Decrypted = false;
1118 Decrypted_Block_Device = "";
1119 if (Mount_Point == "/data") {
1120 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1121 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1122 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001123 }
1124 }
1125 }
1126 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001127}
1128
Gary Peck43acadf2012-11-21 21:19:01 -08001129bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001130 if (Is_File_System(Current_File_System))
1131 return Wipe(Current_File_System);
1132 else
1133 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001134}
1135
Dees_Troye58d5262012-09-21 12:27:57 -04001136bool TWPartition::Wipe_AndSec(void) {
1137 if (!Has_Android_Secure)
1138 return false;
1139
Dees_Troye58d5262012-09-21 12:27:57 -04001140 if (!Mount(true))
1141 return false;
1142
Dees_Troy2673cec2013-04-02 20:22:16 +00001143 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001144 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001145 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001146}
1147
Dees_Troy51a0e822012-09-05 15:24:24 -04001148bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001149 if (Backup_Method == FILES)
1150 return Backup_Tar(backup_folder);
1151 else if (Backup_Method == DD)
1152 return Backup_DD(backup_folder);
1153 else if (Backup_Method == FLASH_UTILS)
1154 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001155 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001156 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001157}
1158
Dees_Troy43d8b002012-09-17 16:00:01 -04001159bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001160 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001161 char split_filename[512];
1162 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001163 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001164
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001165 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001166 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001167 if (!TWFunc::Path_Exists(Full_Filename)) {
1168 // This is a split archive, we presume
1169 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001170 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001171 md5file = split_filename;
1172 md5file += ".md5";
1173 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001174 LOGERR("No md5 file found for '%s'.\n", split_filename);
1175 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001176 return false;
1177 }
1178 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001179 while (index < 1000) {
1180 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001181 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001182 return false;
1183 }
1184 index++;
1185 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001186 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001187 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001188 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001189 } else {
1190 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001191 md5file = Full_Filename + ".md5";
1192 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001193 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1194 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001195 return false;
1196 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001197 md5sum.setfn(Full_Filename);
1198 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001199 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001200 return false;
1201 } else
1202 return true;
1203 }
1204 return false;
1205}
1206
Dees_Troy51a0e822012-09-05 15:24:24 -04001207bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001208 size_t first_period, second_period;
1209 string Restore_File_System;
1210
1211 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001212 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001213
1214 // Parse backup filename to extract the file system before wiping
1215 first_period = Backup_FileName.find(".");
1216 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001217 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001218 return false;
1219 }
1220 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1221 second_period = Restore_File_System.find(".");
1222 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001223 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001224 return false;
1225 }
1226 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001227 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001228
1229 if (Is_File_System(Restore_File_System))
1230 return Restore_Tar(restore_folder, Restore_File_System);
1231 else if (Is_Image(Restore_File_System)) {
1232 if (Restore_File_System == "emmc")
1233 return Restore_DD(restore_folder);
1234 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1235 return Restore_Flash_Image(restore_folder);
1236 }
1237
Dees_Troy2673cec2013-04-02 20:22:16 +00001238 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001239 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001240}
1241
1242string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001243 if (Backup_Method == NONE)
1244 return "none";
1245 else if (Backup_Method == FILES)
1246 return "files";
1247 else if (Backup_Method == DD)
1248 return "dd";
1249 else if (Backup_Method == FLASH_UTILS)
1250 return "flash_utils";
1251 else
1252 return "undefined";
1253 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001254}
1255
1256bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001257 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001258 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001259 return 1;
1260}
1261
1262bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001263 bool Save_Data_Media = Has_Data_Media;
1264
1265 if (!UnMount(true))
1266 return false;
1267
Dees_Troy38bd7602012-09-14 13:33:53 -04001268 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001269 Decrypted_Block_Device = "";
1270 Is_Decrypted = false;
1271 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001272 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001273 Has_Data_Media = Save_Data_Media;
1274 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1275 Recreate_Media_Folder();
1276 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001277 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001278 return true;
1279 } else {
1280 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001281 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001282 return false;
1283 }
1284 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001285}
1286
1287void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001288 const char* type;
1289 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001290
Dees_Troy68cab492012-12-12 19:29:35 +00001291 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1292 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001293
Dees_Troy38bd7602012-09-14 13:33:53 -04001294 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001295 if (!Is_Present)
1296 return;
Dees_Troy51127312012-09-08 13:08:49 -04001297
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001298 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1299 if (blkid_do_fullprobe(pr)) {
1300 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001301 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001302 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001303 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001304
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001305 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1306 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001307 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001308 return;
1309 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001310
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001311 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001312 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001313}
1314
Gary Peck43acadf2012-11-21 21:19:01 -08001315bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001316 if (!UnMount(true))
1317 return false;
1318
Dees_Troy43d8b002012-09-17 16:00:01 -04001319 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001320 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001321
Dees_Troy2673cec2013-04-02 20:22:16 +00001322 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001323 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001324 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001325 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001326 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001327 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001328 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001329 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001330 return true;
1331 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001332 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001333 return false;
1334 }
1335 } else
1336 return Wipe_RMRF();
1337
1338 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001339}
1340
1341bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001342 if (!UnMount(true))
1343 return false;
1344
Dees_Troyb3265ab2013-08-30 02:59:14 +00001345#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Dees_Troya95f55c2013-08-17 13:14:43 +00001346 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Dees_Troya95f55c2013-08-17 13:14:43 +00001347 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001348 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1349 return false;
1350 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001351 #ifdef HAVE_SELINUX
1352 string sedir = Mount_Point + "/lost+found";
1353 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1354 rmdir(sedir.c_str());
1355 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
1356 #endif
Dees_Troya95f55c2013-08-17 13:14:43 +00001357 return true;
1358 }
1359#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001360 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001361 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001362
Dees_Troy2673cec2013-04-02 20:22:16 +00001363 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001364 Find_Actual_Block_Device();
1365 Command = "make_ext4fs";
1366 if (!Is_Decrypted && Length != 0) {
1367 // Only use length if we're not decrypted
1368 char len[32];
1369 sprintf(len, "%i", Length);
1370 Command += " -l ";
1371 Command += len;
1372 }
Dees_Troy5295d582013-09-06 15:51:08 +00001373 if (TWFunc::Path_Exists("/file_contexts")) {
1374 Command += " -S /file_contexts";
1375 }
1376 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001377 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001378 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001379 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001380 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001381 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001382 return true;
1383 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001384 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001385 return false;
1386 }
1387 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001388 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001389#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001390 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001391}
1392
1393bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001394 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001395
Dees_Troy43d8b002012-09-17 16:00:01 -04001396 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001397 if (!UnMount(true))
1398 return false;
1399
Dees_Troy2673cec2013-04-02 20:22:16 +00001400 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001401 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001402 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001403 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001404 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001405 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001406 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001407 return true;
1408 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001409 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001410 return false;
1411 }
1412 return true;
1413 }
1414 else
1415 return Wipe_RMRF();
1416
1417 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001418}
1419
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001420bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001421 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001422
1423 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1424 if (!UnMount(true))
1425 return false;
1426
Dees_Troy2673cec2013-04-02 20:22:16 +00001427 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001428 Find_Actual_Block_Device();
1429 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001430 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001431 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001432 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001433 return true;
1434 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001435 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001436 return false;
1437 }
1438 return true;
1439 }
1440 return false;
1441}
1442
Dees_Troy38bd7602012-09-14 13:33:53 -04001443bool TWPartition::Wipe_MTD() {
1444 if (!UnMount(true))
1445 return false;
1446
Dees_Troy2673cec2013-04-02 20:22:16 +00001447 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001448
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001449 mtd_scan_partitions();
1450 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1451 if (mtd == NULL) {
1452 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1453 return false;
1454 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001455
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001456 MtdWriteContext* ctx = mtd_write_partition(mtd);
1457 if (ctx == NULL) {
1458 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1459 return false;
1460 }
1461 if (mtd_erase_blocks(ctx, -1) == -1) {
1462 mtd_write_close(ctx);
1463 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1464 return false;
1465 }
1466 if (mtd_write_close(ctx) != 0) {
1467 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1468 return false;
1469 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001470 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001471 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001472 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001473 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001474}
1475
1476bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001477 if (!Mount(true))
1478 return false;
1479
Dees_Troy2673cec2013-04-02 20:22:16 +00001480 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001481 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001482 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001483 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001484}
1485
Dees_Troye5017042013-08-29 16:38:55 +00001486bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001487 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001488
1489 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1490 if (!UnMount(true))
1491 return false;
1492
1493 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1494 Find_Actual_Block_Device();
1495 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001496 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001497 Recreate_AndSec_Folder();
1498 gui_print("Done.\n");
1499 return true;
1500 } else {
1501 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1502 return false;
1503 }
1504 return true;
1505 } else {
1506 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1507 return Wipe_RMRF();
1508 }
1509 return false;
1510}
1511
Dees_Troy51a0e822012-09-05 15:24:24 -04001512bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001513 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001514
1515 // This handles wiping data on devices with "sdcard" in /data/media
1516 if (!Mount(true))
1517 return false;
1518
Dees_Troy2673cec2013-04-02 20:22:16 +00001519 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001520
1521 DIR* d;
1522 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001523 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001524 struct dirent* de;
1525 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001526 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001527 // The media folder is the "internal sdcard"
1528 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001529 // the media folder for multi-user.
Dees_Troy16b74352012-11-14 22:27:31 +00001530 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001531
1532 dir = "/data/";
1533 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001534 if (de->d_type == DT_DIR) {
1535 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001536 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +00001537 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001538 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001539 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001540 }
1541 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001542 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001543 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001544 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001545 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001546 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001547}
1548
1549bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001550 char back_name[255], split_index[5];
1551 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001552 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001553 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001554 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001555 twrpTar tar;
1556 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001557
Dees_Troy43d8b002012-09-17 16:00:01 -04001558 if (!Mount(true))
1559 return false;
1560
Dees_Troya13d74f2013-03-24 08:54:55 -05001561 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001562 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001563
1564 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001565 tar.use_compression = use_compression;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001566 //exclude Google Music Cache
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001567 vector<string> excludedirs = du.get_absolute_dirs();
1568 for (int i = 0; i < excludedirs.size(); ++i) {
1569 tar.setexcl(excludedirs.at(i));
1570 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001571#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1572 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1573 if (use_encryption && Can_Encrypt_Backup) {
1574 tar.use_encryption = use_encryption;
1575 if (Use_Userdata_Encryption)
1576 tar.userdata_encryption = use_encryption;
1577 } else {
1578 use_encryption = false;
1579 }
1580#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001581
1582 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1583 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001584 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001585 tar.has_data_media = Has_Data_Media;
1586 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001587 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001588 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001589 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001590 tar.setdir(back_name);
1591 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001592 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001593 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001594 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001595 return false;
1596 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001597 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001598 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001599 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001600 tar.setdir(Backup_Path);
1601 tar.setfn(Full_FileName);
1602 if (tar.createTarFork() != 0)
1603 return false;
1604 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001605 string gzname = Full_FileName + ".gz";
1606 rename(gzname.c_str(), Full_FileName.c_str());
1607 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001608 if (use_encryption)
1609 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001610 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001611 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001612 return false;
1613 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001614 }
1615 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001616}
1617
1618bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001619 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001620 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001621 int use_compression;
1622
igoriok87e3d932013-01-31 21:03:53 +02001623 sprintf(backup_size, "%llu", Backup_Size);
1624 DD_BS = backup_size;
1625
Dees_Troyb46a6842012-09-25 11:06:46 -04001626 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001627 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001628
1629 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1630 Backup_FileName = back_name;
1631
1632 Full_FileName = backup_folder + "/" + Backup_FileName;
1633
igoriok87e3d932013-01-31 21:03:53 +02001634 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001635 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001636 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001637 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001638 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001639 return false;
1640 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001641 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001642}
1643
1644bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001645 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001646 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001647 int use_compression;
1648
Dees_Troyb46a6842012-09-25 11:06:46 -04001649 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001650 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001651
1652 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1653 Backup_FileName = back_name;
1654
1655 Full_FileName = backup_folder + "/" + Backup_FileName;
1656
1657 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001658 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001659 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001660 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1661 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
Dees_Troy2673cec2013-04-02 20:22:16 +00001662 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001663 return false;
1664 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001665 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001666}
1667
Gary Peck43acadf2012-11-21 21:19:01 -08001668bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1669 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001670 int index = 0;
1671 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001672
Dees_Troye58d5262012-09-21 12:27:57 -04001673 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001674 if (!Wipe_AndSec())
1675 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001676 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001677 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001678 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001679 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001680 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001681 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001682 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001683
1684 if (!Mount(true))
1685 return false;
1686
Dees_Troy4a2a1262012-09-18 09:33:47 -04001687 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001688 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001689 if (!TWFunc::Path_Exists(Full_FileName)) {
1690 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001691 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001692 sprintf(split_index, "%03i", index);
1693 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001694 while (TWFunc::Path_Exists(Full_FileName)) {
1695 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001696 gui_print("Restoring archive %i...\n", index);
1697 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001698 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001699 tar.setdir("/");
1700 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001701 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001702 return false;
1703 sprintf(split_index, "%03i", index);
1704 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1705 }
1706 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001707 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001708 return false;
1709 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001710 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001711 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001712 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001713 tar.setdir(Backup_Path);
1714 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001715 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001716 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001717 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001718 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001719 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001720}
1721
1722bool TWPartition::Restore_DD(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001723 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001724
Dees_Troyda8b55a2012-12-12 19:18:30 +00001725 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001726 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001727
1728 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001729 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001730 return false;
1731 }
1732 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1733 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001734 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001735 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1736 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1737 return false;
1738 }
1739
Dees_Troy2673cec2013-04-02 20:22:16 +00001740 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001741 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001742 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001743 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001744 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001745}
1746
1747bool TWPartition::Restore_Flash_Image(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001748 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001749
Dees_Troy2673cec2013-04-02 20:22:16 +00001750 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001751 Full_FileName = restore_folder + "/" + Backup_FileName;
1752 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1753 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001754 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001755 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001756 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001757 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001758 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001759 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001760}
Dees_Troy5bf43922012-09-07 16:07:55 -04001761
1762bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001763 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001764
Dees_Troyab10ee22012-09-21 14:27:30 -04001765 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001766 return false;
1767
Dees_Troy0550cfb2012-10-13 11:56:13 -04001768 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001769 if (Removable || Is_Encrypted) {
1770 if (!Mount(false))
1771 return true;
1772 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001773 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001774
1775 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001776 if (!ret || Size == 0) {
1777 if (!Get_Size_Via_df(Display_Error)) {
1778 if (!Was_Already_Mounted)
1779 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001780 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001781 }
1782 }
Dees_Troy51127312012-09-08 13:08:49 -04001783
Dees_Troy5bf43922012-09-07 16:07:55 -04001784 if (Has_Data_Media) {
1785 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001786 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001787 du.add_relative_dir("media");
1788 Used = du.Get_Folder_Size("/data");
1789 Backup_Size = Used;
1790 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001791 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001792 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001793 } else {
1794 if (!Was_Already_Mounted)
1795 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001796 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001797 }
Dees_Troye58d5262012-09-21 12:27:57 -04001798 } else if (Has_Android_Secure) {
1799 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001800 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001801 else {
1802 if (!Was_Already_Mounted)
1803 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001804 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001805 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001806 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001807 if (!Was_Already_Mounted)
1808 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001809 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001810}
Dees_Troy38bd7602012-09-14 13:33:53 -04001811
1812void TWPartition::Find_Actual_Block_Device(void) {
1813 if (Is_Decrypted) {
1814 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001815 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001816 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001817 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001818 Is_Present = true;
1819 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001820 return;
1821 }
1822 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001823 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001824 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001825 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001826 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001827 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001828 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001829}
1830
1831void TWPartition::Recreate_Media_Folder(void) {
1832 string Command;
1833
1834 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001835 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001836 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001837 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001838 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001839 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1840 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001841 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001842}
Dees_Troye58d5262012-09-21 12:27:57 -04001843
1844void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001845 if (!Has_Android_Secure)
1846 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001847 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001848 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001849 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001850 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001851 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001852 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1853 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1854 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001855 }
1856}