blob: 31b1bfdb33bd6b8d88b024536d161e192cad8856 [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 },
77 { "unbindable", MS_UNBINDABLE },
78 { "private", MS_PRIVATE },
79 { "slave", MS_SLAVE },
80 { "shared", MS_SHARED },
81 { "sync", MS_SYNCHRONOUS },
82 { "defaults", 0 },
83 { 0, 0 },
84};
85
Dees_Troy51a0e822012-09-05 15:24:24 -040086TWPartition::TWPartition(void) {
87 Can_Be_Mounted = false;
88 Can_Be_Wiped = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050089 Can_Be_Backed_Up = false;
Vojtech Bocek1dc30982013-08-30 21:49:30 +020090 Use_Rm_Rf = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040091 Wipe_During_Factory_Reset = false;
92 Wipe_Available_in_GUI = false;
93 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040094 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040095 SubPartition_Of = "";
96 Symlink_Path = "";
97 Symlink_Mount_Point = "";
98 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040099 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400100 Actual_Block_Device = "";
101 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400102 Alternate_Block_Device = "";
103 Removable = false;
104 Is_Present = false;
105 Length = 0;
106 Size = 0;
107 Used = 0;
108 Free = 0;
109 Backup_Size = 0;
110 Can_Be_Encrypted = false;
111 Is_Encrypted = false;
112 Is_Decrypted = false;
113 Decrypted_Block_Device = "";
114 Display_Name = "";
Dees_Troya13d74f2013-03-24 08:54:55 -0500115 Backup_Display_Name = "";
116 Storage_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400117 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -0400118 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -0400119 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400120 Backup_Method = NONE;
Dees_Troy83bd4832013-05-04 12:39:56 +0000121 Can_Encrypt_Backup = false;
122 Use_Userdata_Encryption = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400123 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -0400124 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400125 Is_Storage = false;
Dees_Troya13d74f2013-03-24 08:54:55 -0500126 Is_Settings_Storage = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400127 Storage_Path = "";
128 Current_File_System = "";
129 Fstab_File_System = "";
Hashcode62bd9e02013-11-19 21:59:42 -0800130 Mount_Flags = 0;
131 Mount_Options = "";
Dees_Troy51a0e822012-09-05 15:24:24 -0400132 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +0000133 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +0000134 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000135#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
136 EcryptFS_Password = "";
137#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400138}
139
140TWPartition::~TWPartition(void) {
141 // Do nothing
142}
143
Dees_Troy5bf43922012-09-07 16:07:55 -0400144bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
145 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
146 int line_len = Line.size(), index = 0, item_index = 0;
147 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400148 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400149 strncpy(full_line, Line.c_str(), line_len);
Dees_Troya13d74f2013-03-24 08:54:55 -0500150 bool skip = false;
Dees_Troy5bf43922012-09-07 16:07:55 -0400151
Dees_Troy51127312012-09-08 13:08:49 -0400152 for (index = 0; index < line_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500153 if (full_line[index] == 34)
154 skip = !skip;
155 if (!skip && full_line[index] <= 32)
Dees_Troy5bf43922012-09-07 16:07:55 -0400156 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400157 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400158 Mount_Point = full_line;
Dees_Troy2673cec2013-04-02 20:22:16 +0000159 LOGINFO("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400160 Backup_Path = Mount_Point;
Dees_Troya13d74f2013-03-24 08:54:55 -0500161 Storage_Path = Mount_Point;
Dees_Troy70737fa2013-04-08 13:19:20 +0000162 Display_Name = full_line + 1;
163 Backup_Display_Name = Display_Name;
164 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400165 index = Mount_Point.size();
166 while (index < line_len) {
167 while (index < line_len && full_line[index] == '\0')
168 index++;
169 if (index >= line_len)
170 continue;
171 ptr = full_line + index;
172 if (item_index == 0) {
173 // File System
174 Fstab_File_System = ptr;
175 Current_File_System = ptr;
176 item_index++;
177 } else if (item_index == 1) {
178 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400179 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400180 MTD_Name = ptr;
181 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400182 } else if (Fstab_File_System == "bml") {
183 if (Mount_Point == "/boot")
184 MTD_Name = "boot";
185 else if (Mount_Point == "/recovery")
186 MTD_Name = "recovery";
187 Primary_Block_Device = ptr;
188 if (*ptr != '/')
Dees_Troy2673cec2013-04-02 20:22:16 +0000189 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 -0400190 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400191 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000192 LOGERR("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400193 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000194 LOGINFO("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
Dees_Troy5bf43922012-09-07 16:07:55 -0400195 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400196 } else {
197 Primary_Block_Device = ptr;
198 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400199 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400200 item_index++;
201 } else if (item_index > 1) {
202 if (*ptr == '/') {
203 // Alternate Block Device
204 Alternate_Block_Device = ptr;
205 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
206 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
207 // Partition length
208 ptr += 7;
209 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400210 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
211 // Custom flags, save for later so that new values aren't overwritten by defaults
212 ptr += 6;
213 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000214 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400215 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
216 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 } else {
218 // Unhandled data
Dees_Troy2673cec2013-04-02 20:22:16 +0000219 LOGINFO("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 }
221 }
222 while (index < line_len && full_line[index] != '\0')
223 index++;
224 }
225
226 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
227 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000228 LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400229 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000230 LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400231 return 0;
232 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400233 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400234 Setup_File_System(Display_Error);
235 if (Mount_Point == "/system") {
236 Display_Name = "System";
Dees_Troya13d74f2013-03-24 08:54:55 -0500237 Backup_Display_Name = Display_Name;
238 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400239 Wipe_Available_in_GUI = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500240 Can_Be_Backed_Up = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400241 } else if (Mount_Point == "/data") {
242 Display_Name = "Data";
Dees_Troya13d74f2013-03-24 08:54:55 -0500243 Backup_Display_Name = Display_Name;
244 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400245 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400246 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500247 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000248 Can_Encrypt_Backup = true;
249 Use_Userdata_Encryption = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400250#ifdef RECOVERY_SDCARD_ON_DATA
Dees_Troya13d74f2013-03-24 08:54:55 -0500251 Storage_Name = "Internal Storage";
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400253 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500254 Is_Settings_Storage = true;
Dees_Troy51127312012-09-08 13:08:49 -0400255 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000256 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400257 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
258 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400259 Symlink_Mount_Point = "/emmc";
260 } else {
261 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400262 Symlink_Mount_Point = "/sdcard";
263 }
Dees_Troy16b74352012-11-14 22:27:31 +0000264 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
265 Storage_Path = "/data/media/0";
266 Symlink_Path = Storage_Path;
267 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
268 UnMount(true);
269 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400270#endif
271#ifdef TW_INCLUDE_CRYPTO
272 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400273 char crypto_blkdev[255];
274 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
275 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400276 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400277 DataManager::SetValue(TW_IS_DECRYPTED, 1);
278 Is_Encrypted = true;
279 Is_Decrypted = true;
280 Decrypted_Block_Device = crypto_blkdev;
Dees_Troy2673cec2013-04-02 20:22:16 +0000281 LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
Dees_Troy657c3092012-09-10 20:32:10 -0400282 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400283 Is_Encrypted = true;
284 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800285 Can_Be_Mounted = false;
286 Current_File_System = "emmc";
287 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400288 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
289 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
290 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800291 } else {
292 // Filesystem is not encrypted and the mount
293 // succeeded, so get it back to the original
294 // unmounted state
295 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400296 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400297 #ifdef RECOVERY_SDCARD_ON_DATA
298 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
299 Recreate_Media_Folder();
300 #endif
301#else
302 #ifdef RECOVERY_SDCARD_ON_DATA
303 Recreate_Media_Folder();
304 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400305#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400306 } else if (Mount_Point == "/cache") {
307 Display_Name = "Cache";
Dees_Troya13d74f2013-03-24 08:54:55 -0500308 Backup_Display_Name = Display_Name;
309 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400311 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500312 Can_Be_Backed_Up = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400313 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000314 LOGINFO("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500315 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
316 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400317 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400318 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400319 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400320 Display_Name = "DataData";
Dees_Troya13d74f2013-03-24 08:54:55 -0500321 Backup_Display_Name = Display_Name;
322 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400323 Is_SubPartition = true;
324 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400325 DataManager::SetValue(TW_HAS_DATADATA, 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500326 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000327 Can_Encrypt_Backup = true;
328 Use_Userdata_Encryption = false; // This whole partition should be encrypted
Dees_Troy5bf43922012-09-07 16:07:55 -0400329 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400330 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400331 Display_Name = "SD-Ext";
Dees_Troya13d74f2013-03-24 08:54:55 -0500332 Backup_Display_Name = Display_Name;
333 Storage_Name = Display_Name;
Dees_Troy5bf43922012-09-07 16:07:55 -0400334 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400335 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500336 Can_Be_Backed_Up = true;
Dees_Troy83bd4832013-05-04 12:39:56 +0000337 Can_Encrypt_Backup = true;
338 Use_Userdata_Encryption = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400339 } else if (Mount_Point == "/boot") {
340 Display_Name = "Boot";
Dees_Troya13d74f2013-03-24 08:54:55 -0500341 Backup_Display_Name = Display_Name;
Dees_Troy2c50e182012-09-26 20:05:28 -0400342 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troya13d74f2013-03-24 08:54:55 -0500343 Can_Be_Backed_Up = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400344 }
345#ifdef TW_EXTERNAL_STORAGE_PATH
346 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
347 Is_Storage = true;
348 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400349 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500350 Wipe_Available_in_GUI = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400351#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000352 if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400353 Is_Storage = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400354 Removable = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500355 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400356#ifndef RECOVERY_SDCARD_ON_DATA
357 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400358 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400359#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400360#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000361 }
Dees_Troy8170a922012-09-18 15:40:25 -0400362#ifdef TW_INTERNAL_STORAGE_PATH
363 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
364 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500365 Is_Settings_Storage = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400366 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troya13d74f2013-03-24 08:54:55 -0500367 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400368#ifndef RECOVERY_SDCARD_ON_DATA
369 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400370 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400371#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400372 }
373#else
Dees_Troy70737fa2013-04-08 13:19:20 +0000374 if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") {
Dees_Troy8170a922012-09-18 15:40:25 -0400375 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500376 Is_Settings_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500377 Wipe_Available_in_GUI = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400378#ifndef RECOVERY_SDCARD_ON_DATA
379 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400380 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400381#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400382 }
383#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400384 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400385 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400386 Setup_Image(Display_Error);
Dees_Troya13d74f2013-03-24 08:54:55 -0500387 if (Mount_Point == "/boot") {
388 Display_Name = "Boot";
389 Backup_Display_Name = Display_Name;
390 Can_Be_Backed_Up = true;
391 } else if (Mount_Point == "/recovery") {
392 Display_Name = "Recovery";
393 Backup_Display_Name = Display_Name;
394 Can_Be_Backed_Up = true;
395 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400396 }
397
Dees_Troy51127312012-09-08 13:08:49 -0400398 // Process any custom flags
399 if (Flags.size() > 0)
400 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400401 return true;
402}
403
Hashcode62bd9e02013-11-19 21:59:42 -0800404bool TWPartition::Process_FS_Flags(string& Options, int Flags) {
405 int i;
406 char *p;
407 char *savep;
408 char fs_options[250];
409
410 strlcpy(fs_options, Options.c_str(), sizeof(fs_options));
411 Options = "";
412
413 p = strtok_r(fs_options, ",", &savep);
414 while (p) {
415 /* Look for the flag "p" in the flag list "fl"
416 * If not found, the loop exits with fl[i].name being null.
417 */
418 for (i = 0; mount_flags[i].name; i++) {
419 if (strncmp(p, mount_flags[i].name, strlen(mount_flags[i].name)) == 0) {
420 Flags |= mount_flags[i].flag;
421 break;
422 }
423 }
424
425 if (!mount_flags[i].name) {
426 if (Options.size() > 0)
427 Options += ",";
428 Options += p;
429 }
430 p = strtok_r(NULL, ",", &savep);
431 }
432
433 return true;
434}
435
Dees_Troy51127312012-09-08 13:08:49 -0400436bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
437 char flags[MAX_FSTAB_LINE_LENGTH];
Dees_Troya13d74f2013-03-24 08:54:55 -0500438 int flags_len, index = 0, ptr_len;
Dees_Troy51127312012-09-08 13:08:49 -0400439 char* ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500440 bool skip = false, has_display_name = false, has_storage_name = false, has_backup_name = false;
Dees_Troy51127312012-09-08 13:08:49 -0400441
442 strcpy(flags, Flags.c_str());
443 flags_len = Flags.size();
444 for (index = 0; index < flags_len; index++) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500445 if (flags[index] == 34)
446 skip = !skip;
447 if (!skip && flags[index] == ';')
Dees_Troy51127312012-09-08 13:08:49 -0400448 flags[index] = '\0';
449 }
450
451 index = 0;
452 while (index < flags_len) {
453 while (index < flags_len && flags[index] == '\0')
454 index++;
455 if (index >= flags_len)
456 continue;
457 ptr = flags + index;
Dees_Troya13d74f2013-03-24 08:54:55 -0500458 ptr_len = strlen(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400459 if (strcmp(ptr, "removable") == 0) {
460 Removable = true;
461 } else if (strcmp(ptr, "storage") == 0) {
462 Is_Storage = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500463 } else if (strcmp(ptr, "settingsstorage") == 0) {
464 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400465 } else if (strcmp(ptr, "canbewiped") == 0) {
466 Can_Be_Wiped = true;
Hashcodedabfd492013-08-29 22:45:30 -0700467 } else if (strcmp(ptr, "usermrf") == 0) {
468 Use_Rm_Rf = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500469 } else if (ptr_len > 7 && strncmp(ptr, "backup=", 7) == 0) {
470 ptr += 7;
471 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
472 Can_Be_Backed_Up = true;
473 else
474 Can_Be_Backed_Up = false;
Dees_Troy63c8df72012-09-10 14:02:05 -0400475 } else if (strcmp(ptr, "wipeingui") == 0) {
476 Can_Be_Wiped = true;
477 Wipe_Available_in_GUI = true;
478 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
479 Can_Be_Wiped = true;
480 Wipe_Available_in_GUI = true;
481 Wipe_During_Factory_Reset = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500482 } else if (ptr_len > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000483 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400484 Is_SubPartition = true;
485 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000486 } else if (strcmp(ptr, "ignoreblkid") == 0) {
487 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000488 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
489 Retain_Layout_Version = true;
Dees_Troya13d74f2013-03-24 08:54:55 -0500490 } else if (ptr_len > 8 && strncmp(ptr, "symlink=", 8) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400491 ptr += 8;
492 Symlink_Path = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500493 } else if (ptr_len > 8 && strncmp(ptr, "display=", 8) == 0) {
494 has_display_name = true;
Dees_Troy51127312012-09-08 13:08:49 -0400495 ptr += 8;
Dees_Troya13d74f2013-03-24 08:54:55 -0500496 if (*ptr == '\"') ptr++;
Dees_Troy51127312012-09-08 13:08:49 -0400497 Display_Name = ptr;
Dees_Troya13d74f2013-03-24 08:54:55 -0500498 if (Display_Name.substr(Display_Name.size() - 1, 1) == "\"") {
499 Display_Name.resize(Display_Name.size() - 1);
500 }
501 } else if (ptr_len > 11 && strncmp(ptr, "storagename=", 11) == 0) {
502 has_storage_name = true;
503 ptr += 11;
504 if (*ptr == '\"') ptr++;
505 Storage_Name = ptr;
506 if (Storage_Name.substr(Storage_Name.size() - 1, 1) == "\"") {
507 Storage_Name.resize(Storage_Name.size() - 1);
508 }
509 } else if (ptr_len > 11 && strncmp(ptr, "backupname=", 10) == 0) {
510 has_backup_name = true;
511 ptr += 10;
512 if (*ptr == '\"') ptr++;
513 Backup_Display_Name = ptr;
514 if (Backup_Display_Name.substr(Backup_Display_Name.size() - 1, 1) == "\"") {
515 Backup_Display_Name.resize(Backup_Display_Name.size() - 1);
516 }
517 } else if (ptr_len > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400518 ptr += 10;
519 Format_Block_Size = atoi(ptr);
Dees_Troya13d74f2013-03-24 08:54:55 -0500520 } else if (ptr_len > 7 && strncmp(ptr, "length=", 7) == 0) {
Dees_Troy51127312012-09-08 13:08:49 -0400521 ptr += 7;
522 Length = atoi(ptr);
Dees_Troy83bd4832013-05-04 12:39:56 +0000523 } else if (ptr_len > 17 && strncmp(ptr, "canencryptbackup=", 17) == 0) {
524 ptr += 17;
525 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y')
526 Can_Encrypt_Backup = true;
527 else
528 Can_Encrypt_Backup = false;
529 } else if (ptr_len > 21 && strncmp(ptr, "userdataencryptbackup=", 21) == 0) {
530 ptr += 21;
531 if (*ptr == '1' || *ptr == 'y' || *ptr == 'Y') {
532 Can_Encrypt_Backup = true;
533 Use_Userdata_Encryption = true;
534 } else {
535 Use_Userdata_Encryption = false;
536 }
Hashcode62bd9e02013-11-19 21:59:42 -0800537 } else if (ptr_len > 8 && strncmp(ptr, "fsflags=", 8) == 0) {
538 ptr += 8;
539 if (*ptr == '\"') ptr++;
540
541 Mount_Options = ptr;
542 if (Mount_Options.substr(Mount_Options.size() - 1, 1) == "\"") {
543 Mount_Options.resize(Mount_Options.size() - 1);
544 }
545 Process_FS_Flags(Mount_Options, Mount_Flags);
Dees_Troy51127312012-09-08 13:08:49 -0400546 } else {
547 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000548 LOGERR("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400549 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000550 LOGINFO("Unhandled flag: '%s'\n", ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400551 }
552 while (index < flags_len && flags[index] != '\0')
553 index++;
554 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500555 if (has_display_name && !has_storage_name)
556 Storage_Name = Display_Name;
557 if (!has_display_name && has_storage_name)
558 Display_Name = Storage_Name;
Dees_Troy74fb2e92013-04-15 14:35:47 +0000559 if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure")
Dees_Troya13d74f2013-03-24 08:54:55 -0500560 Backup_Display_Name = Display_Name;
561 if (!has_display_name && has_backup_name)
562 Display_Name = Backup_Display_Name;
Dees_Troy51127312012-09-08 13:08:49 -0400563 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400564}
565
Dees_Troy5bf43922012-09-07 16:07:55 -0400566bool TWPartition::Is_File_System(string File_System) {
567 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400568 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400569 File_System == "ext4" ||
570 File_System == "vfat" ||
571 File_System == "ntfs" ||
572 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500573 File_System == "exfat" ||
Dees_Troye5017042013-08-29 16:38:55 +0000574 File_System == "f2fs" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400575 File_System == "auto")
576 return true;
577 else
578 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400579}
580
Dees_Troy5bf43922012-09-07 16:07:55 -0400581bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400582 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400583 return true;
584 else
585 return false;
586}
587
Dees_Troy51127312012-09-08 13:08:49 -0400588bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400589 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400590 if (mkdir(Path.c_str(), 0777) == -1) {
591 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000592 LOGERR("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400593 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000594 LOGINFO("Can not create '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400595 return false;
596 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000597 LOGINFO("Created '%s' folder.\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400598 return true;
599 }
600 }
601 return true;
602}
603
Dees_Troy5bf43922012-09-07 16:07:55 -0400604void TWPartition::Setup_File_System(bool Display_Error) {
605 struct statfs st;
606
607 Can_Be_Mounted = true;
608 Can_Be_Wiped = true;
609
Dees_Troy5bf43922012-09-07 16:07:55 -0400610 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400611 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400612 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
613 Backup_Name = Display_Name;
614 Backup_Method = FILES;
615}
616
617void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400618 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
619 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800620 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400621 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800622 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400623 Backup_Method = FLASH_UTILS;
624 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000625 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 -0400626 if (Find_Partition_Size()) {
627 Used = Size;
628 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400629 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400630 if (Display_Error)
Dees Troyd932ce12013-10-18 17:12:59 +0000631 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400632 else
Dees Troyd932ce12013-10-18 17:12:59 +0000633 LOGINFO("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400634 }
635}
636
Dees_Troye58d5262012-09-21 12:27:57 -0400637void TWPartition::Setup_AndSec(void) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500638 Backup_Display_Name = "Android Secure";
Dees_Troye58d5262012-09-21 12:27:57 -0400639 Backup_Name = "and-sec";
Dees_Troya13d74f2013-03-24 08:54:55 -0500640 Can_Be_Backed_Up = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400641 Has_Android_Secure = true;
642 Symlink_Path = Mount_Point + "/.android_secure";
643 Symlink_Mount_Point = "/and-sec";
644 Backup_Path = Symlink_Mount_Point;
645 Make_Dir("/and-sec", true);
646 Recreate_AndSec_Folder();
647}
648
Dees_Troy5bf43922012-09-07 16:07:55 -0400649void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
650 char device[512], realDevice[512];
651
652 strcpy(device, Block.c_str());
653 memset(realDevice, 0, sizeof(realDevice));
654 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
655 {
656 strcpy(device, realDevice);
657 memset(realDevice, 0, sizeof(realDevice));
658 }
659
660 if (device[0] != '/') {
661 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000662 LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400663 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000664 LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400665 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400666 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400667 Block = device;
668 return;
669 }
670}
671
Dees_Troy8e337f32012-10-13 22:07:49 -0400672void TWPartition::Mount_Storage_Retry(void) {
673 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500674 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400675 int retry_count = 5;
676 while (retry_count > 0 && !Mount(false)) {
677 usleep(500000);
678 retry_count--;
679 }
680 Mount(true);
681 }
682}
683
Dees_Troy38bd7602012-09-14 13:33:53 -0400684bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
685 FILE *fp = NULL;
686 char line[255];
687
688 fp = fopen("/proc/mtd", "rt");
689 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000690 LOGERR("Device does not support /proc/mtd\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400691 return false;
692 }
693
694 while (fgets(line, sizeof(line), fp) != NULL)
695 {
696 char device[32], label[32];
697 unsigned long size = 0;
698 char* fstype = NULL;
699 int deviceId;
700
701 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
702
703 // Skip header and blank lines
704 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
705 continue;
706
707 // Strip off the trailing " from the label
708 label[strlen(label)-1] = '\0';
709
710 if (strcmp(label, MTD_Name.c_str()) == 0) {
711 // We found our device
712 // Strip off the trailing : from the device
713 device[strlen(device)-1] = '\0';
714 if (sscanf(device,"mtd%d", &deviceId) == 1) {
715 sprintf(device, "/dev/block/mtdblock%d", deviceId);
716 Primary_Block_Device = device;
Dees_Troy76543db2013-06-19 16:24:30 +0000717 fclose(fp);
718 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400719 }
720 }
721 }
722 fclose(fp);
723
724 return false;
725}
726
Dees_Troy51127312012-09-08 13:08:49 -0400727bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
728 struct statfs st;
729 string Local_Path = Mount_Point + "/.";
730
731 if (!Mount(Display_Error))
732 return false;
733
734 if (statfs(Local_Path.c_str(), &st) != 0) {
735 if (!Removable) {
736 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000737 LOGERR("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400738 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000739 LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400740 }
741 return false;
742 }
743 Size = (st.f_blocks * st.f_bsize);
744 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
745 Free = (st.f_bfree * st.f_bsize);
746 Backup_Size = Used;
747 return true;
748}
749
750bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400751 FILE* fp;
752 char command[255], line[512];
753 int include_block = 1;
754 unsigned int min_len;
755
756 if (!Mount(Display_Error))
757 return false;
758
Dees_Troy38bd7602012-09-14 13:33:53 -0400759 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400760 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +0200761 TWFunc::Exec_Cmd(command);
Dees_Troy51127312012-09-08 13:08:49 -0400762 fp = fopen("/tmp/dfoutput.txt", "rt");
763 if (fp == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000764 LOGINFO("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400765 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400766 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400767
768 while (fgets(line, sizeof(line), fp) != NULL)
769 {
770 unsigned long blocks, used, available;
771 char device[64];
772 char tmpString[64];
773
774 if (strncmp(line, "Filesystem", 10) == 0)
775 continue;
776 if (strlen(line) < min_len) {
777 include_block = 0;
778 continue;
779 }
780 if (include_block) {
781 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
782 } else {
783 // The device block string is so long that the df information is on the next line
784 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400785 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400786 while (tmpString[space_count] == 32)
787 space_count++;
788 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
789 }
790
791 // Adjust block size to byte size
792 Size = blocks * 1024ULL;
793 Used = used * 1024ULL;
794 Free = available * 1024ULL;
795 Backup_Size = Used;
796 }
797 fclose(fp);
798 return true;
799}
800
Dees_Troy5bf43922012-09-07 16:07:55 -0400801bool TWPartition::Find_Partition_Size(void) {
802 FILE* fp;
803 char line[512];
804 string tmpdevice;
805
igoriok87e3d932013-01-31 21:03:53 +0200806 fp = fopen("/proc/dumchar_info", "rt");
807 if (fp != NULL) {
808 while (fgets(line, sizeof(line), fp) != NULL)
809 {
810 char label[32], device[32];
811 unsigned long size = 0;
812
813 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
814
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500815 // Skip header, annotation and blank lines
igoriok87e3d932013-01-31 21:03:53 +0200816 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
817 continue;
818
819 tmpdevice = "/dev/";
820 tmpdevice += label;
821 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
822 Size = size;
823 fclose(fp);
824 return true;
825 }
826 }
827 }
828
Dees_Troy5bf43922012-09-07 16:07:55 -0400829 // In this case, we'll first get the partitions we care about (with labels)
830 fp = fopen("/proc/partitions", "rt");
831 if (fp == NULL)
832 return false;
833
834 while (fgets(line, sizeof(line), fp) != NULL)
835 {
836 unsigned long major, minor, blocks;
837 char device[512];
838 char tmpString[64];
839
Dees_Troy63c8df72012-09-10 14:02:05 -0400840 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400841 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
842
843 tmpdevice = "/dev/block/";
844 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400845 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400846 // Adjust block size to byte size
847 Size = blocks * 1024ULL;
848 fclose(fp);
849 return true;
850 }
851 }
852 fclose(fp);
853 return false;
854}
855
Dees_Troy5bf43922012-09-07 16:07:55 -0400856bool TWPartition::Is_Mounted(void) {
857 if (!Can_Be_Mounted)
858 return false;
859
860 struct stat st1, st2;
861 string test_path;
862
863 // Check to see if the mount point directory exists
864 test_path = Mount_Point + "/.";
865 if (stat(test_path.c_str(), &st1) != 0) return false;
866
867 // Check to see if the directory above the mount point exists
868 test_path = Mount_Point + "/../.";
869 if (stat(test_path.c_str(), &st2) != 0) return false;
870
871 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
872 int ret = (st1.st_dev != st2.st_dev) ? true : false;
873
874 return ret;
875}
876
877bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000878 int exfat_mounted = 0;
879
Dees_Troy5bf43922012-09-07 16:07:55 -0400880 if (Is_Mounted()) {
881 return true;
882 } else if (!Can_Be_Mounted) {
883 return false;
884 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400885
886 Find_Actual_Block_Device();
887
888 // Check the current file system before mounting
889 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000890 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000891 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 +0000892 LOGINFO("cmd: %s\n", cmd.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000893 string result;
894 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000895 LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000896 Current_File_System = "vfat";
897 } else {
898#ifdef TW_NO_EXFAT_FUSE
899 UnMount(false);
900 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
901 // Some kernels let us mount vfat as exfat which doesn't work out too well
902#else
903 exfat_mounted = 1;
904#endif
905 }
906 }
Dees_Troy22042032012-12-18 21:23:08 +0000907 if (Fstab_File_System == "yaffs2") {
908 // mount an MTD partition as a YAFFS2 filesystem.
Dees_Troy76543db2013-06-19 16:24:30 +0000909 const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
910 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
911 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
912 if (Display_Error)
913 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
914 else
915 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
916 return false;
917 } else {
918 LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
919 return true;
920 }
921 } else {
922 struct stat st;
923 string test_path = Mount_Point;
924 if (stat(test_path.c_str(), &st) < 0) {
925 if (Display_Error)
926 LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
927 else
928 LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
929 return false;
930 }
931 mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
932 if (new_mode != st.st_mode) {
933 LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
934 if (chmod(Mount_Point.c_str(), new_mode) < 0) {
935 if (Display_Error)
936 LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
937 else
938 LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
939 return false;
940 }
941 }
Dees_Troy22042032012-12-18 21:23:08 +0000942 return true;
Dees_Troy76543db2013-06-19 16:24:30 +0000943 }
Hashcode62bd9e02013-11-19 21:59:42 -0800944 } 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 +0000945#ifdef TW_NO_EXFAT_FUSE
946 if (Current_File_System == "exfat") {
Dees_Troy2673cec2013-04-02 20:22:16 +0000947 LOGINFO("Mounting exfat failed, trying vfat...\n");
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000948 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000949 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000950 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000951 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000952 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800953 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 +0000954 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000955 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000956 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000957#endif
958 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000959 LOGERR("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000960 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000961 LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str());
962 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 +0000963 return false;
964#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000965 }
966#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000967 }
968#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
969 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
970 MetaEcfsFile += "/.MetaEcfsFile";
971 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
972 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
973 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000974 LOGERR("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000975 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000976 LOGINFO("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000977 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000978 LOGINFO("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000979 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400980 }
Dees_Troy066eb302013-08-23 17:20:32 +0000981 } else if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
982 if (Is_Decrypted)
983 LOGINFO("Mounting external storage, '%s' is not encrypted\n", Mount_Point.c_str());
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000984 Is_Decrypted = false;
985 }
986#endif
987 if (Removable)
988 Update_Size(Display_Error);
989
990 if (!Symlink_Mount_Point.empty()) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200991 string Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
992 TWFunc::Exec_Cmd(Command);
Dees_Troy5bf43922012-09-07 16:07:55 -0400993 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400994 return true;
995}
996
997bool TWPartition::UnMount(bool Display_Error) {
998 if (Is_Mounted()) {
999 int never_unmount_system;
1000
1001 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
1002 if (never_unmount_system == 1 && Mount_Point == "/system")
1003 return true; // Never unmount system if you're not supposed to unmount it
1004
Dees_Troyc8bafa12013-01-10 15:43:00 +00001005#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1006 if (EcryptFS_Password.size() > 0) {
1007 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
1008 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001009 LOGERR("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001010 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001011 LOGINFO("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001012 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001013 LOGINFO("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
Dees_Troyc8bafa12013-01-10 15:43:00 +00001014 }
1015 }
1016#endif
1017
Dees_Troy38bd7602012-09-14 13:33:53 -04001018 if (!Symlink_Mount_Point.empty())
1019 umount(Symlink_Mount_Point.c_str());
1020
Dees_Troyb05ddee2013-01-28 20:24:50 +00001021 umount(Mount_Point.c_str());
1022 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001023 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +00001024 LOGERR("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001025 else
Dees_Troy2673cec2013-04-02 20:22:16 +00001026 LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -04001027 return false;
1028 } else
1029 return true;
1030 } else {
1031 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001032 }
1033}
1034
Gary Peck43acadf2012-11-21 21:19:01 -08001035bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +00001036 bool wiped = false, update_crypt = false;
1037 int check;
1038 string Layout_Filename = Mount_Point + "/.layout_version";
1039
Dees_Troy38bd7602012-09-14 13:33:53 -04001040 if (!Can_Be_Wiped) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001041 LOGERR("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001042 return false;
1043 }
1044
Dees_Troyc51f1f92012-09-20 15:32:13 -04001045 if (Mount_Point == "/cache")
Dees_Troy2673cec2013-04-02 20:22:16 +00001046 Log_Offset = 0;
Dees_Troyc51f1f92012-09-20 15:32:13 -04001047
Dees_Troyce675462013-01-09 19:48:21 +00001048#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1049 if (Mount_Point == "/data" && Mount(false)) {
1050 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
1051 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
1052 }
1053#endif
1054
Dees_Troy16c2b312013-01-15 16:51:18 +00001055 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
1056 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
1057 else
1058 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -04001059
Dees_Troy16c2b312013-01-15 16:51:18 +00001060 if (Has_Data_Media) {
1061 wiped = Wipe_Data_Without_Wiping_Media();
1062 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001063
Dees_Troy16c2b312013-01-15 16:51:18 +00001064 DataManager::GetValue(TW_RM_RF_VAR, check);
1065
Hashcodedabfd492013-08-29 22:45:30 -07001066 if (check || Use_Rm_Rf)
Dees_Troy16c2b312013-01-15 16:51:18 +00001067 wiped = Wipe_RMRF();
1068 else if (New_File_System == "ext4")
1069 wiped = Wipe_EXT4();
1070 else if (New_File_System == "ext2" || New_File_System == "ext3")
1071 wiped = Wipe_EXT23(New_File_System);
1072 else if (New_File_System == "vfat")
1073 wiped = Wipe_FAT();
1074 else if (New_File_System == "exfat")
1075 wiped = Wipe_EXFAT();
1076 else if (New_File_System == "yaffs2")
1077 wiped = Wipe_MTD();
Dees_Troye5017042013-08-29 16:38:55 +00001078 else if (New_File_System == "f2fs")
1079 wiped = Wipe_F2FS();
Dees_Troy16c2b312013-01-15 16:51:18 +00001080 else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001081 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 +00001082 unlink("/.layout_version");
1083 return false;
1084 }
1085 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001086 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001087
Gary Pecke8bc5d72012-12-21 06:45:25 -08001088 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +00001089#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1090 if (Mount_Point == "/data" && Mount(false)) {
1091 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1092 Make_Dir("/data/system", true);
1093 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1094 }
1095 }
1096#endif
Dees_Troy16c2b312013-01-15 16:51:18 +00001097
Dees_Troy1c1ac442013-01-17 21:42:14 +00001098 if (Mount_Point == "/cache")
1099 DataManager::Output_Version();
1100
Dees_Troy16c2b312013-01-15 16:51:18 +00001101 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
1102 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
1103
1104 if (update_crypt) {
1105 Setup_File_System(false);
1106 if (Is_Encrypted && !Is_Decrypted) {
1107 // just wiped an encrypted partition back to its unencrypted state
1108 Is_Encrypted = false;
1109 Is_Decrypted = false;
1110 Decrypted_Block_Device = "";
1111 if (Mount_Point == "/data") {
1112 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
1113 DataManager::SetValue(TW_IS_DECRYPTED, 0);
1114 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001115 }
1116 }
1117 }
1118 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -04001119}
1120
Gary Peck43acadf2012-11-21 21:19:01 -08001121bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -08001122 if (Is_File_System(Current_File_System))
1123 return Wipe(Current_File_System);
1124 else
1125 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -08001126}
1127
Dees_Troye58d5262012-09-21 12:27:57 -04001128bool TWPartition::Wipe_AndSec(void) {
1129 if (!Has_Android_Secure)
1130 return false;
1131
Dees_Troye58d5262012-09-21 12:27:57 -04001132 if (!Mount(true))
1133 return false;
1134
Dees_Troy2673cec2013-04-02 20:22:16 +00001135 gui_print("Wiping %s\n", Backup_Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001136 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001137 return true;
Dees_Troye58d5262012-09-21 12:27:57 -04001138}
1139
Dees_Troy51a0e822012-09-05 15:24:24 -04001140bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001141 if (Backup_Method == FILES)
1142 return Backup_Tar(backup_folder);
1143 else if (Backup_Method == DD)
1144 return Backup_DD(backup_folder);
1145 else if (Backup_Method == FLASH_UTILS)
1146 return Backup_Dump_Image(backup_folder);
Dees_Troy2673cec2013-04-02 20:22:16 +00001147 LOGERR("Unknown backup method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001148 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001149}
1150
Dees_Troy43d8b002012-09-17 16:00:01 -04001151bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001152 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -04001153 char split_filename[512];
1154 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001155 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -04001156
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001157 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -04001158 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001159 if (!TWFunc::Path_Exists(Full_Filename)) {
1160 // This is a split archive, we presume
1161 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy2673cec2013-04-02 20:22:16 +00001162 LOGINFO("split_filename: %s\n", split_filename);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001163 md5file = split_filename;
1164 md5file += ".md5";
1165 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001166 LOGERR("No md5 file found for '%s'.\n", split_filename);
1167 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001168 return false;
1169 }
1170 md5sum.setfn(split_filename);
Dees_Troy83bd4832013-05-04 12:39:56 +00001171 while (index < 1000) {
1172 if (TWFunc::Path_Exists(split_filename) && md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001173 LOGERR("MD5 failed to match on '%s'.\n", split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001174 return false;
1175 }
1176 index++;
1177 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001178 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -04001179 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001180 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001181 } else {
1182 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001183 md5file = Full_Filename + ".md5";
1184 if (!TWFunc::Path_Exists(md5file)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001185 LOGERR("No md5 file found for '%s'.\n", Full_Filename.c_str());
1186 LOGERR("Please unselect Enable MD5 verification to restore.\n");
bigbiff bigbiff65a4c732013-03-15 15:17:50 -04001187 return false;
1188 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001189 md5sum.setfn(Full_Filename);
1190 if (md5sum.verify_md5digest() != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001191 LOGERR("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001192 return false;
1193 } else
1194 return true;
1195 }
1196 return false;
1197}
1198
Dees_Troy51a0e822012-09-05 15:24:24 -04001199bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -08001200 size_t first_period, second_period;
1201 string Restore_File_System;
1202
1203 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001204 LOGINFO("Restore filename is: %s\n", Backup_FileName.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001205
1206 // Parse backup filename to extract the file system before wiping
1207 first_period = Backup_FileName.find(".");
1208 if (first_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001209 LOGERR("Unable to find file system (first period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001210 return false;
1211 }
1212 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1213 second_period = Restore_File_System.find(".");
1214 if (second_period == string::npos) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001215 LOGERR("Unable to find file system (second period).\n");
Gary Peck43acadf2012-11-21 21:19:01 -08001216 return false;
1217 }
1218 Restore_File_System.resize(second_period);
Dees_Troy2673cec2013-04-02 20:22:16 +00001219 LOGINFO("Restore file system is: '%s'.\n", Restore_File_System.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001220
1221 if (Is_File_System(Restore_File_System))
1222 return Restore_Tar(restore_folder, Restore_File_System);
1223 else if (Is_Image(Restore_File_System)) {
1224 if (Restore_File_System == "emmc")
1225 return Restore_DD(restore_folder);
1226 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1227 return Restore_Flash_Image(restore_folder);
1228 }
1229
Dees_Troy2673cec2013-04-02 20:22:16 +00001230 LOGERR("Unknown restore method for '%s'\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001231 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001232}
1233
1234string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001235 if (Backup_Method == NONE)
1236 return "none";
1237 else if (Backup_Method == FILES)
1238 return "files";
1239 else if (Backup_Method == DD)
1240 return "dd";
1241 else if (Backup_Method == FLASH_UTILS)
1242 return "flash_utils";
1243 else
1244 return "undefined";
1245 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001246}
1247
1248bool TWPartition::Decrypt(string Password) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001249 LOGINFO("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001250 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001251 return 1;
1252}
1253
1254bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001255 bool Save_Data_Media = Has_Data_Media;
1256
1257 if (!UnMount(true))
1258 return false;
1259
Dees_Troy38bd7602012-09-14 13:33:53 -04001260 Has_Data_Media = false;
Dees_Troy74fb2e92013-04-15 14:35:47 +00001261 Decrypted_Block_Device = "";
1262 Is_Decrypted = false;
1263 Is_Encrypted = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001264 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001265 Has_Data_Media = Save_Data_Media;
1266 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1267 Recreate_Media_Folder();
1268 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001269 gui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001270 return true;
1271 } else {
1272 Has_Data_Media = Save_Data_Media;
Dees_Troy2673cec2013-04-02 20:22:16 +00001273 LOGERR("Unable to format to remove encryption.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001274 return false;
1275 }
1276 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001277}
1278
1279void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001280 const char* type;
1281 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001282
Dees_Troy68cab492012-12-12 19:29:35 +00001283 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1284 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001285
Dees_Troy38bd7602012-09-14 13:33:53 -04001286 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001287 if (!Is_Present)
1288 return;
Dees_Troy51127312012-09-08 13:08:49 -04001289
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001290 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1291 if (blkid_do_fullprobe(pr)) {
1292 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001293 LOGINFO("Can't probe device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001294 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001295 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001296
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001297 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1298 blkid_free_probe(pr);
Dees_Troy2673cec2013-04-02 20:22:16 +00001299 LOGINFO("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001300 return;
1301 }
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001302
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001303 Current_File_System = type;
Vojtech Bocek4d4b3362013-06-24 22:46:13 +02001304 blkid_free_probe(pr);
Dees_Troy51a0e822012-09-05 15:24:24 -04001305}
1306
Gary Peck43acadf2012-11-21 21:19:01 -08001307bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001308 if (!UnMount(true))
1309 return false;
1310
Dees_Troy43d8b002012-09-17 16:00:01 -04001311 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001312 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001313
Dees_Troy2673cec2013-04-02 20:22:16 +00001314 gui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001315 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001316 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001317 LOGINFO("mke2fs command: %s\n", command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001318 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001319 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001320 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001321 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001322 return true;
1323 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001324 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001325 return false;
1326 }
1327 } else
1328 return Wipe_RMRF();
1329
1330 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001331}
1332
1333bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001334 if (!UnMount(true))
1335 return false;
1336
Dees_Troyb3265ab2013-08-30 02:59:14 +00001337#if defined(HAVE_SELINUX) && defined(USE_EXT4)
Dees_Troya95f55c2013-08-17 13:14:43 +00001338 gui_print("Formatting %s using make_ext4fs function.\n", Display_Name.c_str());
Dees_Troya95f55c2013-08-17 13:14:43 +00001339 if (make_ext4fs(Actual_Block_Device.c_str(), Length, Mount_Point.c_str(), selinux_handle) != 0) {
Dees_Troya95f55c2013-08-17 13:14:43 +00001340 LOGERR("Unable to wipe '%s' using function call.\n", Mount_Point.c_str());
1341 return false;
1342 } else {
bigbiff bigbiffc49d7062013-10-11 20:28:00 -04001343 #ifdef HAVE_SELINUX
1344 string sedir = Mount_Point + "/lost+found";
1345 PartitionManager.Mount_By_Path(sedir.c_str(), true);
1346 rmdir(sedir.c_str());
1347 mkdir(sedir.c_str(), S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP);
1348 #endif
Dees_Troya95f55c2013-08-17 13:14:43 +00001349 return true;
1350 }
1351#else
Dees_Troy43d8b002012-09-17 16:00:01 -04001352 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001353 string Command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001354
Dees_Troy2673cec2013-04-02 20:22:16 +00001355 gui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001356 Find_Actual_Block_Device();
1357 Command = "make_ext4fs";
1358 if (!Is_Decrypted && Length != 0) {
1359 // Only use length if we're not decrypted
1360 char len[32];
1361 sprintf(len, "%i", Length);
1362 Command += " -l ";
1363 Command += len;
1364 }
Dees_Troy5295d582013-09-06 15:51:08 +00001365 if (TWFunc::Path_Exists("/file_contexts")) {
1366 Command += " -S /file_contexts";
1367 }
1368 Command += " -a " + Mount_Point + " " + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001369 LOGINFO("make_ext4fs command: %s\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001370 if (TWFunc::Exec_Cmd(Command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001371 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001372 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001373 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001374 return true;
1375 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001376 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001377 return false;
1378 }
1379 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001380 return Wipe_EXT23("ext4");
Dees_Troya95f55c2013-08-17 13:14:43 +00001381#endif
Dees_Troy38bd7602012-09-14 13:33:53 -04001382 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001383}
1384
1385bool TWPartition::Wipe_FAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001386 string command;
Dees_Troy38bd7602012-09-14 13:33:53 -04001387
Dees_Troy43d8b002012-09-17 16:00:01 -04001388 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001389 if (!UnMount(true))
1390 return false;
1391
Dees_Troy2673cec2013-04-02 20:22:16 +00001392 gui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001393 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001394 command = "mkdosfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001395 if (TWFunc::Exec_Cmd(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001396 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001397 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001398 gui_print("Done.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001399 return true;
1400 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001401 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001402 return false;
1403 }
1404 return true;
1405 }
1406 else
1407 return Wipe_RMRF();
1408
1409 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001410}
1411
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001412bool TWPartition::Wipe_EXFAT() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001413 string command;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001414
1415 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1416 if (!UnMount(true))
1417 return false;
1418
Dees_Troy2673cec2013-04-02 20:22:16 +00001419 gui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001420 Find_Actual_Block_Device();
1421 command = "mkexfatfs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001422 if (TWFunc::Exec_Cmd(command) == 0) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001423 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001424 gui_print("Done.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001425 return true;
1426 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001427 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001428 return false;
1429 }
1430 return true;
1431 }
1432 return false;
1433}
1434
Dees_Troy38bd7602012-09-14 13:33:53 -04001435bool TWPartition::Wipe_MTD() {
1436 if (!UnMount(true))
1437 return false;
1438
Dees_Troy2673cec2013-04-02 20:22:16 +00001439 gui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001440
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001441 mtd_scan_partitions();
1442 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1443 if (mtd == NULL) {
1444 LOGERR("No mtd partition named '%s'", MTD_Name.c_str());
1445 return false;
1446 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001447
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001448 MtdWriteContext* ctx = mtd_write_partition(mtd);
1449 if (ctx == NULL) {
1450 LOGERR("Can't write '%s', failed to format.", MTD_Name.c_str());
1451 return false;
1452 }
1453 if (mtd_erase_blocks(ctx, -1) == -1) {
1454 mtd_write_close(ctx);
1455 LOGERR("Failed to format '%s'", MTD_Name.c_str());
1456 return false;
1457 }
1458 if (mtd_write_close(ctx) != 0) {
1459 LOGERR("Failed to close '%s'", MTD_Name.c_str());
1460 return false;
1461 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001462 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001463 Recreate_AndSec_Folder();
Dees_Troy2673cec2013-04-02 20:22:16 +00001464 gui_print("Done.\n");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001465 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001466}
1467
1468bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001469 if (!Mount(true))
1470 return false;
1471
Dees_Troy2673cec2013-04-02 20:22:16 +00001472 gui_print("Removing all files under '%s'\n", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001473 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001474 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001475 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001476}
1477
Dees_Troye5017042013-08-29 16:38:55 +00001478bool TWPartition::Wipe_F2FS() {
Vojtech Bocek05534202013-09-11 08:11:56 +02001479 string command;
Dees_Troye5017042013-08-29 16:38:55 +00001480
1481 if (TWFunc::Path_Exists("/sbin/mkfs.f2fs")) {
1482 if (!UnMount(true))
1483 return false;
1484
1485 gui_print("Formatting %s using mkfs.f2fs...\n", Display_Name.c_str());
1486 Find_Actual_Block_Device();
1487 command = "mkfs.f2fs " + Actual_Block_Device;
Vojtech Bocek05534202013-09-11 08:11:56 +02001488 if (TWFunc::Exec_Cmd(command) == 0) {
Dees_Troye5017042013-08-29 16:38:55 +00001489 Recreate_AndSec_Folder();
1490 gui_print("Done.\n");
1491 return true;
1492 } else {
1493 LOGERR("Unable to wipe '%s'.\n", Mount_Point.c_str());
1494 return false;
1495 }
1496 return true;
1497 } else {
1498 gui_print("mkfs.f2fs binary not found, using rm -rf to wipe.\n");
1499 return Wipe_RMRF();
1500 }
1501 return false;
1502}
1503
Dees_Troy51a0e822012-09-05 15:24:24 -04001504bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001505 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001506
1507 // This handles wiping data on devices with "sdcard" in /data/media
1508 if (!Mount(true))
1509 return false;
1510
Dees_Troy2673cec2013-04-02 20:22:16 +00001511 gui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001512
1513 DIR* d;
1514 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001515 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001516 struct dirent* de;
1517 while ((de = readdir(d)) != NULL) {
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001518 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
Dees_Troy16b74352012-11-14 22:27:31 +00001519 // The media folder is the "internal sdcard"
1520 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001521 // the media folder for multi-user.
Dees_Troy16b74352012-11-14 22:27:31 +00001522 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001523
1524 dir = "/data/";
1525 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001526 if (de->d_type == DT_DIR) {
1527 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001528 } 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 +00001529 if (!unlink(dir.c_str()))
Dees_Troy2673cec2013-04-02 20:22:16 +00001530 LOGINFO("Unable to unlink '%s'\n", dir.c_str());
Dees_Troyce675462013-01-09 19:48:21 +00001531 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001532 }
1533 closedir(d);
Dees_Troy2673cec2013-04-02 20:22:16 +00001534 gui_print("Done.\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001535 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001536 }
Dees_Troy2673cec2013-04-02 20:22:16 +00001537 gui_print("Dirent failed to open /data, error!\n");
Dees_Troy16b74352012-11-14 22:27:31 +00001538 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001539}
1540
1541bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001542 char back_name[255], split_index[5];
1543 string Full_FileName, Split_FileName, Tar_Args, Command;
Dees_Troy83bd4832013-05-04 12:39:56 +00001544 int use_compression, use_encryption = 0, index, backup_count;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001545 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001546 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001547 twrpTar tar;
1548 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001549
Dees_Troy43d8b002012-09-17 16:00:01 -04001550 if (!Mount(true))
1551 return false;
1552
Dees_Troya13d74f2013-03-24 08:54:55 -05001553 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Backup_Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001554 gui_print("Backing up %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001555
1556 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy83bd4832013-05-04 12:39:56 +00001557 tar.use_compression = use_compression;
bigbiff bigbiff86e77bc2013-08-26 21:36:23 -04001558 //exclude Google Music Cache
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001559 vector<string> excludedirs = du.get_absolute_dirs();
1560 for (int i = 0; i < excludedirs.size(); ++i) {
1561 tar.setexcl(excludedirs.at(i));
1562 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001563#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
1564 DataManager::GetValue("tw_encrypt_backup", use_encryption);
1565 if (use_encryption && Can_Encrypt_Backup) {
1566 tar.use_encryption = use_encryption;
1567 if (Use_Userdata_Encryption)
1568 tar.userdata_encryption = use_encryption;
1569 } else {
1570 use_encryption = false;
1571 }
1572#endif
Dees_Troy43d8b002012-09-17 16:00:01 -04001573
1574 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1575 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001576 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001577 tar.has_data_media = Has_Data_Media;
1578 if (!use_encryption && Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001579 // This backup needs to be split into multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001580 gui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001581 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001582 tar.setdir(back_name);
1583 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001584 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001585 if (backup_count == -1) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001586 LOGERR("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001587 return false;
1588 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001589 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001590 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001591 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001592 tar.setdir(Backup_Path);
1593 tar.setfn(Full_FileName);
1594 if (tar.createTarFork() != 0)
1595 return false;
1596 if (use_compression && !use_encryption) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001597 string gzname = Full_FileName + ".gz";
1598 rename(gzname.c_str(), Full_FileName.c_str());
1599 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001600 if (use_encryption)
1601 Full_FileName += "000";
Dees_Troy7c2dec82012-09-26 09:49:14 -04001602 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001603 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001604 return false;
1605 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001606 }
1607 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001608}
1609
1610bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001611 char back_name[255], backup_size[32];
Vojtech Bocek05534202013-09-11 08:11:56 +02001612 string Full_FileName, Command, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001613 int use_compression;
1614
igoriok87e3d932013-01-31 21:03:53 +02001615 sprintf(backup_size, "%llu", Backup_Size);
1616 DD_BS = backup_size;
1617
Dees_Troyb46a6842012-09-25 11:06:46 -04001618 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001619 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001620
1621 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1622 Backup_FileName = back_name;
1623
1624 Full_FileName = backup_folder + "/" + Backup_FileName;
1625
igoriok87e3d932013-01-31 21:03:53 +02001626 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy2673cec2013-04-02 20:22:16 +00001627 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001628 TWFunc::Exec_Cmd(Command);
Dees_Troyc154ac22012-10-12 15:36:47 -04001629 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001630 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001631 return false;
1632 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001633 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001634}
1635
1636bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001637 char back_name[255];
Vojtech Bocek05534202013-09-11 08:11:56 +02001638 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001639 int use_compression;
1640
Dees_Troyb46a6842012-09-25 11:06:46 -04001641 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy2673cec2013-04-02 20:22:16 +00001642 gui_print("Backing up %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001643
1644 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1645 Backup_FileName = back_name;
1646
1647 Full_FileName = backup_folder + "/" + Backup_FileName;
1648
1649 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001650 LOGINFO("Backup command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001651 TWFunc::Exec_Cmd(Command);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001652 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1653 // 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 +00001654 LOGERR("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001655 return false;
1656 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001657 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001658}
1659
Gary Peck43acadf2012-11-21 21:19:01 -08001660bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1661 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001662 int index = 0;
1663 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001664
Dees_Troye58d5262012-09-21 12:27:57 -04001665 if (Has_Android_Secure) {
Dees_Troye58d5262012-09-21 12:27:57 -04001666 if (!Wipe_AndSec())
1667 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001668 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001669 gui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001670 if (!Wipe(Restore_File_System))
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001671 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001672 }
Dees_Troya13d74f2013-03-24 08:54:55 -05001673 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Backup_Display_Name, "Restoring");
Dees_Troy2673cec2013-04-02 20:22:16 +00001674 gui_print("Restoring %s...\n", Backup_Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001675
1676 if (!Mount(true))
1677 return false;
1678
Dees_Troy4a2a1262012-09-18 09:33:47 -04001679 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy83bd4832013-05-04 12:39:56 +00001680 /*if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001681 if (!TWFunc::Path_Exists(Full_FileName)) {
1682 // Backup is multiple archives
Dees_Troy2673cec2013-04-02 20:22:16 +00001683 LOGINFO("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001684 sprintf(split_index, "%03i", index);
1685 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001686 while (TWFunc::Path_Exists(Full_FileName)) {
1687 index++;
Dees_Troy2673cec2013-04-02 20:22:16 +00001688 gui_print("Restoring archive %i...\n", index);
1689 LOGINFO("Restoring '%s'...\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001690 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001691 tar.setdir("/");
1692 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001693 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001694 return false;
1695 sprintf(split_index, "%03i", index);
1696 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1697 }
1698 if (index == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001699 LOGERR("Error locating restore file: '%s'\n", Full_FileName.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001700 return false;
1701 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001702 }
Dees_Troy83bd4832013-05-04 12:39:56 +00001703 } else {*/
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001704 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001705 tar.setdir(Backup_Path);
1706 tar.setfn(Full_FileName);
Dees_Troy83bd4832013-05-04 12:39:56 +00001707 tar.backup_name = Backup_Name;
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001708 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001709 return false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001710 //}
Dees_Troy43d8b002012-09-17 16:00:01 -04001711 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001712}
1713
1714bool TWPartition::Restore_DD(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001715 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001716
Dees_Troyda8b55a2012-12-12 19:18:30 +00001717 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001718 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001719
1720 if (!Find_Partition_Size()) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001721 LOGERR("Unable to find partition size for '%s'\n", Mount_Point.c_str());
Gary Peck15e623d2012-11-21 21:07:58 -08001722 return false;
1723 }
1724 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1725 if (backup_size > Size) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001726 LOGERR("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
Gary Peck15e623d2012-11-21 21:07:58 -08001727 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1728 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1729 return false;
1730 }
1731
Dees_Troy2673cec2013-04-02 20:22:16 +00001732 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001733 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00001734 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001735 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001736 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001737}
1738
1739bool TWPartition::Restore_Flash_Image(string restore_folder) {
Vojtech Bocek05534202013-09-11 08:11:56 +02001740 string Full_FileName, Command;
Dees_Troy43d8b002012-09-17 16:00:01 -04001741
Dees_Troy2673cec2013-04-02 20:22:16 +00001742 gui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001743 Full_FileName = restore_folder + "/" + Backup_FileName;
1744 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1745 Command = "erase_image " + MTD_Name;
Dees_Troy2673cec2013-04-02 20:22:16 +00001746 LOGINFO("Erase command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001747 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001748 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
Dees_Troy2673cec2013-04-02 20:22:16 +00001749 LOGINFO("Restore command: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02001750 TWFunc::Exec_Cmd(Command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001751 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001752}
Dees_Troy5bf43922012-09-07 16:07:55 -04001753
1754bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001755 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001756
Dees_Troyab10ee22012-09-21 14:27:30 -04001757 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001758 return false;
1759
Dees_Troy0550cfb2012-10-13 11:56:13 -04001760 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001761 if (Removable || Is_Encrypted) {
1762 if (!Mount(false))
1763 return true;
1764 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001765 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001766
1767 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001768 if (!ret || Size == 0) {
1769 if (!Get_Size_Via_df(Display_Error)) {
1770 if (!Was_Already_Mounted)
1771 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001772 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001773 }
1774 }
Dees_Troy51127312012-09-08 13:08:49 -04001775
Dees_Troy5bf43922012-09-07 16:07:55 -04001776 if (Has_Data_Media) {
1777 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001778 unsigned long long data_media_used, actual_data;
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001779 du.add_relative_dir("media");
1780 Used = du.Get_Folder_Size("/data");
1781 Backup_Size = Used;
1782 int bak = (int)(Used / 1048576LLU);
Dees_Troy51127312012-09-08 13:08:49 -04001783 int fre = (int)(Free / 1048576LLU);
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001784 LOGINFO("Data backup size is %iMB, free: %iMB.\n", bak, fre);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001785 } else {
1786 if (!Was_Already_Mounted)
1787 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001788 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001789 }
Dees_Troye58d5262012-09-21 12:27:57 -04001790 } else if (Has_Android_Secure) {
1791 if (Mount(Display_Error))
bigbiff bigbiff34684ff2013-12-01 21:03:45 -05001792 Backup_Size = du.Get_Folder_Size(Backup_Path);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001793 else {
1794 if (!Was_Already_Mounted)
1795 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001796 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001797 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001798 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001799 if (!Was_Already_Mounted)
1800 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001801 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001802}
Dees_Troy38bd7602012-09-14 13:33:53 -04001803
1804void TWPartition::Find_Actual_Block_Device(void) {
1805 if (Is_Decrypted) {
1806 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001807 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001808 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001809 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001810 Is_Present = true;
1811 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001812 return;
1813 }
1814 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001815 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001816 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001817 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001818 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001819 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001820 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001821}
1822
1823void TWPartition::Recreate_Media_Folder(void) {
1824 string Command;
1825
1826 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001827 LOGERR("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001828 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001829 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy2673cec2013-04-02 20:22:16 +00001830 LOGINFO("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001831 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1832 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001833 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001834}
Dees_Troye58d5262012-09-21 12:27:57 -04001835
1836void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001837 if (!Has_Android_Secure)
1838 return;
Dees_Troy2673cec2013-04-02 20:22:16 +00001839 LOGINFO("Creating %s: %s\n", Backup_Display_Name.c_str(), Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001840 if (!Mount(true)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001841 LOGERR("Unable to recreate %s folder.\n", Backup_Name.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001842 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001843 LOGINFO("Recreating %s folder.\n", Backup_Name.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001844 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1845 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1846 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001847 }
1848}