Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
bigbiff bigbiff | 56cf564 | 2016-08-19 17:43:45 -0400 | [diff] [blame] | 2 | Copyright 2013 to 2017 TeamWin |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/vfs.h> |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 24 | #include <sys/mount.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | #include <unistd.h> |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 26 | #include <dirent.h> |
that | f54e539 | 2016-01-29 22:04:43 +0100 | [diff] [blame] | 27 | #include <libgen.h> |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 28 | #include <zlib.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 29 | #include <iostream> |
| 30 | #include <sstream> |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 31 | #include <sys/param.h> |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 32 | #include <fcntl.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 33 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 34 | #ifdef TW_INCLUDE_CRYPTO |
| 35 | #include "cutils/properties.h" |
| 36 | #endif |
| 37 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 38 | #include "libblkid/include/blkid.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 39 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 40 | #include "twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 42 | #include "data.hpp" |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 43 | #include "twrp-functions.hpp" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 44 | #include "twrpTar.hpp" |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 45 | #include "exclude.hpp" |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 46 | #include "infomanager.hpp" |
Ethan Yonker | 4b94cfd | 2014-12-11 10:00:45 -0600 | [diff] [blame] | 47 | #include "set_metadata.h" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 48 | #include "gui/gui.hpp" |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 49 | #include "adbbu/libtwadbbu.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 50 | extern "C" { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 51 | #include "mtdutils/mtdutils.h" |
| 52 | #include "mtdutils/mounts.h" |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 53 | #ifdef USE_EXT4 |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 54 | // #include "make_ext4fs.h" TODO need ifdef for android8 |
| 55 | #include <ext4_utils/make_ext4fs.h> |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 56 | #endif |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 57 | |
| 58 | #ifdef TW_INCLUDE_CRYPTO |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 59 | #include "crypto/lollipop/cryptfs.h" |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 60 | #include "gpt/gpt.h" |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 61 | #ifdef TW_INCLUDE_FBE |
| 62 | #include "crypto/ext4crypt/Decrypt.h" |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 63 | #endif |
Ethan Yonker | bc85b63 | 2015-08-09 12:48:14 -0500 | [diff] [blame] | 64 | #else |
| 65 | #define CRYPT_FOOTER_OFFSET 0x4000 |
Ethan Yonker | 71413f4 | 2014-02-26 13:36:08 -0600 | [diff] [blame] | 66 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 67 | } |
Matt Mower | 8741364 | 2017-01-17 21:14:46 -0600 | [diff] [blame] | 68 | #include <selinux/selinux.h> |
Ethan Yonker | f27497f | 2014-02-09 11:48:33 -0600 | [diff] [blame] | 69 | #include <selinux/label.h> |
Dees Troy | 4159aed | 2014-02-28 17:24:43 +0000 | [diff] [blame] | 70 | #ifdef HAVE_CAPABILITIES |
| 71 | #include <sys/capability.h> |
| 72 | #include <sys/xattr.h> |
| 73 | #include <linux/xattr.h> |
| 74 | #endif |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 75 | #include <sparse_format.h> |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 76 | #include "progresstracking.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 77 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 78 | using namespace std; |
| 79 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 80 | static int auto_index = 0; // v2 fstab allows you to specify a mount point of "auto" with no /. These items are given a mount point of /auto* where * == auto_index |
| 81 | |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 82 | extern struct selabel_handle *selinux_handle; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 83 | extern bool datamedia; |
Dees_Troy | a95f55c | 2013-08-17 13:14:43 +0000 | [diff] [blame] | 84 | |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 85 | struct flag_list { |
| 86 | const char *name; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 87 | unsigned long flag; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 90 | const struct flag_list mount_flags[] = { |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 91 | { "noatime", MS_NOATIME }, |
| 92 | { "noexec", MS_NOEXEC }, |
| 93 | { "nosuid", MS_NOSUID }, |
| 94 | { "nodev", MS_NODEV }, |
| 95 | { "nodiratime", MS_NODIRATIME }, |
| 96 | { "ro", MS_RDONLY }, |
| 97 | { "rw", 0 }, |
| 98 | { "remount", MS_REMOUNT }, |
| 99 | { "bind", MS_BIND }, |
| 100 | { "rec", MS_REC }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 101 | #ifdef MS_UNBINDABLE |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 102 | { "unbindable", MS_UNBINDABLE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 103 | #endif |
| 104 | #ifdef MS_PRIVATE |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 105 | { "private", MS_PRIVATE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 106 | #endif |
| 107 | #ifdef MS_SLAVE |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 108 | { "slave", MS_SLAVE }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 109 | #endif |
| 110 | #ifdef MS_SHARED |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 111 | { "shared", MS_SHARED }, |
Dees Troy | c4bc30e | 2014-02-03 15:04:19 +0000 | [diff] [blame] | 112 | #endif |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 113 | { "sync", MS_SYNCHRONOUS }, |
| 114 | { 0, 0 }, |
| 115 | }; |
| 116 | |
| 117 | const char *ignored_mount_items[] = { |
| 118 | "defaults=", |
| 119 | "errors=", |
| 120 | NULL |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 123 | enum TW_FSTAB_FLAGS { |
| 124 | TWFLAG_DEFAULTS, // Retain position |
| 125 | TWFLAG_ANDSEC, |
| 126 | TWFLAG_BACKUP, |
| 127 | TWFLAG_BACKUPNAME, |
| 128 | TWFLAG_BLOCKSIZE, |
| 129 | TWFLAG_CANBEWIPED, |
| 130 | TWFLAG_CANENCRYPTBACKUP, |
| 131 | TWFLAG_DISPLAY, |
| 132 | TWFLAG_ENCRYPTABLE, |
| 133 | TWFLAG_FLASHIMG, |
| 134 | TWFLAG_FORCEENCRYPT, |
| 135 | TWFLAG_FSFLAGS, |
| 136 | TWFLAG_IGNOREBLKID, |
| 137 | TWFLAG_LENGTH, |
| 138 | TWFLAG_MOUNTTODECRYPT, |
| 139 | TWFLAG_REMOVABLE, |
| 140 | TWFLAG_RETAINLAYOUTVERSION, |
| 141 | TWFLAG_SETTINGSSTORAGE, |
| 142 | TWFLAG_STORAGE, |
| 143 | TWFLAG_STORAGENAME, |
| 144 | TWFLAG_SUBPARTITIONOF, |
| 145 | TWFLAG_SYMLINK, |
| 146 | TWFLAG_USERDATAENCRYPTBACKUP, |
| 147 | TWFLAG_USERMRF, |
| 148 | TWFLAG_WIPEDURINGFACTORYRESET, |
| 149 | TWFLAG_WIPEINGUI, |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 150 | TWFLAG_SLOTSELECT, |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 151 | TWFLAG_WAIT, |
| 152 | TWFLAG_VERIFY, |
| 153 | TWFLAG_CHECK, |
| 154 | TWFLAG_ALTDEVICE, |
| 155 | TWFLAG_NOTRIM, |
| 156 | TWFLAG_VOLDMANAGED, |
| 157 | TWFLAG_FORMATTABLE, |
| 158 | TWFLAG_RESIZE, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /* Flags without a trailing '=' are considered dual format flags and can be |
| 162 | * written as either 'flagname' or 'flagname=', where the character following |
| 163 | * the '=' is Y,y,1 for true and false otherwise. |
| 164 | */ |
| 165 | const struct flag_list tw_flags[] = { |
| 166 | { "andsec", TWFLAG_ANDSEC }, |
| 167 | { "backup", TWFLAG_BACKUP }, |
| 168 | { "backupname=", TWFLAG_BACKUPNAME }, |
| 169 | { "blocksize=", TWFLAG_BLOCKSIZE }, |
| 170 | { "canbewiped", TWFLAG_CANBEWIPED }, |
| 171 | { "canencryptbackup", TWFLAG_CANENCRYPTBACKUP }, |
| 172 | { "defaults", TWFLAG_DEFAULTS }, |
| 173 | { "display=", TWFLAG_DISPLAY }, |
| 174 | { "encryptable=", TWFLAG_ENCRYPTABLE }, |
| 175 | { "flashimg", TWFLAG_FLASHIMG }, |
| 176 | { "forceencrypt=", TWFLAG_FORCEENCRYPT }, |
| 177 | { "fsflags=", TWFLAG_FSFLAGS }, |
| 178 | { "ignoreblkid", TWFLAG_IGNOREBLKID }, |
| 179 | { "length=", TWFLAG_LENGTH }, |
| 180 | { "mounttodecrypt", TWFLAG_MOUNTTODECRYPT }, |
| 181 | { "removable", TWFLAG_REMOVABLE }, |
| 182 | { "retainlayoutversion", TWFLAG_RETAINLAYOUTVERSION }, |
| 183 | { "settingsstorage", TWFLAG_SETTINGSSTORAGE }, |
| 184 | { "storage", TWFLAG_STORAGE }, |
| 185 | { "storagename=", TWFLAG_STORAGENAME }, |
| 186 | { "subpartitionof=", TWFLAG_SUBPARTITIONOF }, |
| 187 | { "symlink=", TWFLAG_SYMLINK }, |
| 188 | { "userdataencryptbackup", TWFLAG_USERDATAENCRYPTBACKUP }, |
| 189 | { "usermrf", TWFLAG_USERMRF }, |
| 190 | { "wipeduringfactoryreset", TWFLAG_WIPEDURINGFACTORYRESET }, |
| 191 | { "wipeingui", TWFLAG_WIPEINGUI }, |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 192 | { "slotselect", TWFLAG_SLOTSELECT }, |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 193 | { "wait", TWFLAG_WAIT }, |
| 194 | { "verify", TWFLAG_VERIFY }, |
| 195 | { "check", TWFLAG_CHECK }, |
| 196 | { "altdevice", TWFLAG_ALTDEVICE }, |
| 197 | { "notrim", TWFLAG_NOTRIM }, |
| 198 | { "voldmanaged=", TWFLAG_VOLDMANAGED }, |
| 199 | { "formattable", TWFLAG_FORMATTABLE }, |
| 200 | { "resize", TWFLAG_RESIZE }, |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 201 | { 0, 0 }, |
| 202 | }; |
| 203 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 204 | TWPartition::TWPartition() { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 205 | Can_Be_Mounted = false; |
| 206 | Can_Be_Wiped = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 207 | Can_Be_Backed_Up = false; |
Vojtech Bocek | 1dc3098 | 2013-08-30 21:49:30 +0200 | [diff] [blame] | 208 | Use_Rm_Rf = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 209 | Wipe_During_Factory_Reset = false; |
| 210 | Wipe_Available_in_GUI = false; |
| 211 | Is_SubPartition = false; |
Dees_Troy | 2691f9d | 2012-09-24 11:15:49 -0400 | [diff] [blame] | 212 | Has_SubPartition = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 213 | SubPartition_Of = ""; |
| 214 | Symlink_Path = ""; |
| 215 | Symlink_Mount_Point = ""; |
| 216 | Mount_Point = ""; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 217 | Backup_Path = ""; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 218 | Wildcard_Block_Device = false; |
| 219 | Sysfs_Entry = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 220 | Actual_Block_Device = ""; |
| 221 | Primary_Block_Device = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 222 | Alternate_Block_Device = ""; |
| 223 | Removable = false; |
| 224 | Is_Present = false; |
| 225 | Length = 0; |
| 226 | Size = 0; |
| 227 | Used = 0; |
| 228 | Free = 0; |
| 229 | Backup_Size = 0; |
| 230 | Can_Be_Encrypted = false; |
| 231 | Is_Encrypted = false; |
| 232 | Is_Decrypted = false; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 233 | Is_FBE = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 234 | Mount_To_Decrypt = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 235 | Decrypted_Block_Device = ""; |
| 236 | Display_Name = ""; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 237 | Backup_Display_Name = ""; |
| 238 | Storage_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 239 | Backup_Name = ""; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 240 | Backup_FileName = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 241 | MTD_Name = ""; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 242 | Backup_Method = BM_NONE; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 243 | Can_Encrypt_Backup = false; |
| 244 | Use_Userdata_Encryption = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 245 | Has_Data_Media = false; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 246 | Has_Android_Secure = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 247 | Is_Storage = false; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 248 | Is_Settings_Storage = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 249 | Storage_Path = ""; |
| 250 | Current_File_System = ""; |
| 251 | Fstab_File_System = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 252 | Mount_Flags = 0; |
| 253 | Mount_Options = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 254 | Format_Block_Size = 0; |
Dees_Troy | 68cab49 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 255 | Ignore_Blkid = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 256 | Retain_Layout_Version = false; |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 257 | Crypto_Key_Location = "footer"; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 258 | MTP_Storage_ID = 0; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 259 | Can_Flash_Img = false; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 260 | Mount_Read_Only = false; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 261 | Is_Adopted_Storage = false; |
| 262 | Adopted_GUID = ""; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 263 | SlotSelect = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | TWPartition::~TWPartition(void) { |
| 267 | // Do nothing |
| 268 | } |
| 269 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 270 | bool TWPartition::Process_Fstab_Line(const char *fstab_line, bool Display_Error, std::map<string, Flags_Map> *twrp_flags) { |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 271 | char full_line[MAX_FSTAB_LINE_LENGTH]; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 272 | char twflags[MAX_FSTAB_LINE_LENGTH] = ""; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 273 | char* ptr; |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 274 | int line_len = strlen(fstab_line), index = 0, item_index = 0; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 275 | bool skip = false; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 276 | int fstab_version = 1, mount_point_index = 0, fs_index = 1, block_device_index = 2; |
| 277 | TWPartition *additional_entry = NULL; |
| 278 | std::map<string, Flags_Map>::iterator it; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 279 | |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 280 | strlcpy(full_line, fstab_line, sizeof(full_line)); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 281 | for (index = 0; index < line_len; index++) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 282 | if (full_line[index] == 34) |
| 283 | skip = !skip; |
| 284 | if (!skip && full_line[index] <= 32) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 285 | full_line[index] = '\0'; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 286 | } |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 287 | if (line_len < 10) |
| 288 | return false; // There can't possibly be a valid fstab line that is less than 10 chars |
| 289 | if (strncmp(fstab_line, "/dev/", strlen("/dev/")) == 0 || strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) { |
| 290 | fstab_version = 2; |
| 291 | block_device_index = 0; |
| 292 | mount_point_index = 1; |
| 293 | fs_index = 2; |
| 294 | } |
| 295 | |
| 296 | index = 0; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 297 | while (index < line_len) { |
| 298 | while (index < line_len && full_line[index] == '\0') |
| 299 | index++; |
| 300 | if (index >= line_len) |
| 301 | continue; |
| 302 | ptr = full_line + index; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 303 | if (item_index == mount_point_index) { |
| 304 | Mount_Point = ptr; |
| 305 | if (fstab_version == 2) { |
| 306 | additional_entry = PartitionManager.Find_Partition_By_Path(Mount_Point); |
| 307 | if (additional_entry) { |
| 308 | LOGINFO("Found an additional entry for '%s'\n", Mount_Point.c_str()); |
| 309 | } |
| 310 | } |
| 311 | LOGINFO("Processing '%s'\n", Mount_Point.c_str()); |
| 312 | Backup_Path = Mount_Point; |
| 313 | Storage_Path = Mount_Point; |
| 314 | Display_Name = ptr + 1; |
| 315 | Backup_Display_Name = Display_Name; |
| 316 | Storage_Name = Display_Name; |
| 317 | item_index++; |
| 318 | } else if (item_index == fs_index) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 319 | // File System |
| 320 | Fstab_File_System = ptr; |
| 321 | Current_File_System = ptr; |
| 322 | item_index++; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 323 | } else if (item_index == block_device_index) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 324 | // Primary Block Device |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 325 | if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") { |
Dees_Troy | 094207a | 2012-09-26 12:00:39 -0400 | [diff] [blame] | 326 | MTD_Name = ptr; |
| 327 | Find_MTD_Block_Device(MTD_Name); |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 328 | } else if (Fstab_File_System == "bml") { |
| 329 | if (Mount_Point == "/boot") |
| 330 | MTD_Name = "boot"; |
| 331 | else if (Mount_Point == "/recovery") |
| 332 | MTD_Name = "recovery"; |
| 333 | Primary_Block_Device = ptr; |
| 334 | if (*ptr != '/') |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 335 | 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_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 336 | } else if (*ptr != '/') { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 337 | if (Display_Error) |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 338 | LOGERR("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 339 | else |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 340 | LOGINFO("Invalid block device '%s' in fstab line '%s'", ptr, fstab_line); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 341 | return false; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 342 | } else { |
| 343 | Primary_Block_Device = ptr; |
| 344 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 345 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 346 | item_index++; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 347 | } else if (item_index > 2) { |
| 348 | if (fstab_version == 2) { |
| 349 | if (item_index == 3) { |
| 350 | Process_FS_Flags(ptr); |
| 351 | if (additional_entry) { |
| 352 | additional_entry->Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options); |
| 353 | return false; // We save the extra fs flags in the other partition entry and by returning false, this entry will be deleted |
| 354 | } |
| 355 | } else { |
| 356 | strlcpy(twflags, ptr, sizeof(twflags)); |
| 357 | } |
| 358 | item_index++; |
| 359 | } else if (*ptr == '/') { // v2 fstab does not allow alternate block devices |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 360 | // Alternate Block Device |
| 361 | Alternate_Block_Device = ptr; |
| 362 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 363 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 364 | // Partition length |
| 365 | ptr += 7; |
| 366 | Length = atoi(ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 367 | } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) { |
| 368 | // Custom flags, save for later so that new values aren't overwritten by defaults |
| 369 | ptr += 6; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 370 | strlcpy(twflags, ptr, sizeof(twflags)); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 371 | } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) { |
| 372 | // Do nothing |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 373 | } else { |
| 374 | // Unhandled data |
Matt Mower | 2b2dd15 | 2016-04-26 11:24:08 -0500 | [diff] [blame] | 375 | LOGINFO("Unhandled fstab information '%s' in fstab line '%s'\n", ptr, fstab_line); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | while (index < line_len && full_line[index] != '\0') |
| 379 | index++; |
| 380 | } |
| 381 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 382 | // override block devices from the v2 fstab with the ones we read from the twrp.flags file in case they are different |
| 383 | if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) { |
| 384 | it = twrp_flags->find(Mount_Point); |
| 385 | if (it != twrp_flags->end()) { |
| 386 | if (!it->second.Primary_Block_Device.empty()) { |
| 387 | Primary_Block_Device = it->second.Primary_Block_Device; |
| 388 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
| 389 | } |
| 390 | if (!it->second.Alternate_Block_Device.empty()) { |
| 391 | Alternate_Block_Device = it->second.Alternate_Block_Device; |
| 392 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (strncmp(fstab_line, "/devices/", strlen("/devices/")) == 0) { |
| 398 | Sysfs_Entry = Primary_Block_Device; |
| 399 | Primary_Block_Device = ""; |
| 400 | Is_Storage = true; |
| 401 | Removable = true; |
| 402 | Wipe_Available_in_GUI = true; |
| 403 | Wildcard_Block_Device = true; |
| 404 | } |
| 405 | if (Primary_Block_Device.find("*") != string::npos) |
| 406 | Wildcard_Block_Device = true; |
| 407 | |
| 408 | if (Mount_Point == "auto") { |
| 409 | Mount_Point = "/auto"; |
| 410 | char autoi[5]; |
| 411 | sprintf(autoi, "%i", auto_index); |
| 412 | Mount_Point += autoi; |
| 413 | Backup_Path = Mount_Point; |
| 414 | Storage_Path = Mount_Point; |
| 415 | auto_index++; |
| 416 | Setup_File_System(Display_Error); |
| 417 | Display_Name = "Storage"; |
| 418 | Backup_Display_Name = Display_Name; |
| 419 | Storage_Name = Display_Name; |
| 420 | Can_Be_Backed_Up = false; |
| 421 | Wipe_Available_in_GUI = true; |
| 422 | Is_Storage = true; |
| 423 | Removable = true; |
| 424 | Wipe_Available_in_GUI = true; |
| 425 | } else if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 426 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 427 | LOGERR("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 428 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 429 | LOGINFO("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 430 | return false; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 431 | } else if (Is_File_System(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 432 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 433 | Setup_File_System(Display_Error); |
| 434 | if (Mount_Point == "/system") { |
| 435 | Display_Name = "System"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 436 | Backup_Display_Name = Display_Name; |
| 437 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 438 | Wipe_Available_in_GUI = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 439 | Can_Be_Backed_Up = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 440 | Mount_Read_Only = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 441 | } else if (Mount_Point == "/data") { |
| 442 | Display_Name = "Data"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 443 | Backup_Display_Name = Display_Name; |
| 444 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 445 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 446 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 447 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 448 | Can_Encrypt_Backup = true; |
| 449 | Use_Userdata_Encryption = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 450 | } else if (Mount_Point == "/cache") { |
| 451 | Display_Name = "Cache"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 452 | Backup_Display_Name = Display_Name; |
| 453 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 454 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 455 | Wipe_During_Factory_Reset = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 456 | Can_Be_Backed_Up = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 457 | } else if (Mount_Point == "/datadata") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 458 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 459 | Display_Name = "DataData"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 460 | Backup_Display_Name = Display_Name; |
| 461 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 462 | Is_SubPartition = true; |
| 463 | SubPartition_Of = "/data"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 464 | DataManager::SetValue(TW_HAS_DATADATA, 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 465 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 466 | Can_Encrypt_Backup = true; |
| 467 | Use_Userdata_Encryption = false; // This whole partition should be encrypted |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 468 | } else if (Mount_Point == "/sd-ext") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 469 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 470 | Display_Name = "SD-Ext"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 471 | Backup_Display_Name = Display_Name; |
| 472 | Storage_Name = Display_Name; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 473 | Wipe_Available_in_GUI = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 474 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 475 | Can_Be_Backed_Up = true; |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 476 | Can_Encrypt_Backup = true; |
| 477 | Use_Userdata_Encryption = true; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 478 | } else if (Mount_Point == "/boot") { |
| 479 | Display_Name = "Boot"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 480 | Backup_Display_Name = Display_Name; |
Dees_Troy | 2c50e18 | 2012-09-26 20:05:28 -0400 | [diff] [blame] | 481 | DataManager::SetValue("tw_boot_is_mountable", 1); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 482 | Can_Be_Backed_Up = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 483 | } else if (Mount_Point == "/vendor") { |
| 484 | Display_Name = "Vendor"; |
| 485 | Backup_Display_Name = Display_Name; |
| 486 | Storage_Name = Display_Name; |
| 487 | Mount_Read_Only = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 488 | } |
| 489 | #ifdef TW_EXTERNAL_STORAGE_PATH |
| 490 | if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) { |
| 491 | Is_Storage = true; |
| 492 | Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH); |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 493 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 494 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 495 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 496 | if (Mount_Point == "/sdcard" || Mount_Point == "/external_sd" || Mount_Point == "/external_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 497 | Is_Storage = true; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 498 | Removable = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 499 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 500 | #endif |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 501 | } |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 502 | #ifdef TW_INTERNAL_STORAGE_PATH |
| 503 | if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) { |
| 504 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 505 | Is_Settings_Storage = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 506 | Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 507 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 508 | } |
| 509 | #else |
Dees_Troy | 70737fa | 2013-04-08 13:19:20 +0000 | [diff] [blame] | 510 | if (Mount_Point == "/emmc" || Mount_Point == "/internal_sd" || Mount_Point == "/internal_sdcard") { |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 511 | Is_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 512 | Is_Settings_Storage = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 513 | Wipe_Available_in_GUI = true; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 514 | } |
| 515 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 516 | } else if (Is_Image(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 517 | Find_Actual_Block_Device(); |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 518 | Setup_Image(); |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 519 | if (Mount_Point == "/boot") { |
| 520 | Display_Name = "Boot"; |
| 521 | Backup_Display_Name = Display_Name; |
| 522 | Can_Be_Backed_Up = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 523 | Can_Flash_Img = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 524 | } else if (Mount_Point == "/recovery") { |
| 525 | Display_Name = "Recovery"; |
| 526 | Backup_Display_Name = Display_Name; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 527 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 528 | } else if (Mount_Point == "/system_image") { |
| 529 | Display_Name = "System Image"; |
| 530 | Backup_Display_Name = Display_Name; |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 531 | Can_Flash_Img = true; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 532 | Can_Be_Backed_Up = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 533 | } else if (Mount_Point == "/vendor_image") { |
| 534 | Display_Name = "Vendor Image"; |
| 535 | Backup_Display_Name = Display_Name; |
HashBang | ed974bb | 2016-01-30 14:20:09 -0500 | [diff] [blame] | 536 | Can_Flash_Img = true; |
Ethan Yonker | 1673e3d | 2015-10-26 21:51:58 -0500 | [diff] [blame] | 537 | Can_Be_Backed_Up = true; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 538 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 539 | } |
| 540 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 541 | // Process TWRP fstab flags |
| 542 | if (strlen(twflags) > 0) { |
| 543 | string Prev_Display_Name = Display_Name; |
| 544 | string Prev_Storage_Name = Storage_Name; |
| 545 | string Prev_Backup_Display_Name = Backup_Display_Name; |
| 546 | Display_Name = ""; |
| 547 | Storage_Name = ""; |
| 548 | Backup_Display_Name = ""; |
| 549 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 550 | Process_TW_Flags(twflags, (fstab_version == 1), fstab_version); |
| 551 | Save_FS_Flags(Fstab_File_System, Mount_Flags, Mount_Options); |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 552 | |
| 553 | bool has_display_name = !Display_Name.empty(); |
| 554 | bool has_storage_name = !Storage_Name.empty(); |
| 555 | bool has_backup_name = !Backup_Display_Name.empty(); |
| 556 | if (!has_display_name) Display_Name = Prev_Display_Name; |
| 557 | if (!has_storage_name) Storage_Name = Prev_Storage_Name; |
| 558 | if (!has_backup_name) Backup_Display_Name = Prev_Backup_Display_Name; |
| 559 | |
| 560 | if (has_display_name && !has_storage_name) |
| 561 | Storage_Name = Display_Name; |
| 562 | if (!has_display_name && has_storage_name) |
| 563 | Display_Name = Storage_Name; |
| 564 | if (has_display_name && !has_backup_name && Backup_Display_Name != "Android Secure") |
| 565 | Backup_Display_Name = Display_Name; |
| 566 | if (!has_display_name && has_backup_name) |
| 567 | Display_Name = Backup_Display_Name; |
| 568 | } |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 569 | |
| 570 | if (fstab_version == 2 && twrp_flags && twrp_flags->size() > 0) { |
| 571 | it = twrp_flags->find(Mount_Point); |
| 572 | if (it != twrp_flags->end()) { |
| 573 | char twrpflags[MAX_FSTAB_LINE_LENGTH] = ""; |
| 574 | int skip = 0; |
| 575 | string Flags = it->second.Flags; |
| 576 | strcpy(twrpflags, Flags.c_str()); |
| 577 | if (strlen(twrpflags) > strlen("flags=") && strncmp(twrpflags, "flags=", strlen("flags=")) == 0) |
| 578 | skip += strlen("flags="); |
| 579 | char* flagptr = twrpflags; |
| 580 | flagptr += skip; |
| 581 | Process_TW_Flags(flagptr, Display_Error, 1); // Forcing the fstab to ver 1 because this data is coming from the /etc/twrp.flags which should be using the TWRP v1 flags format |
| 582 | } |
| 583 | } |
nkk71 | 98fc399 | 2017-12-16 16:26:42 +0200 | [diff] [blame] | 584 | |
| 585 | if (Mount_Point == "/persist" && Can_Be_Mounted) { |
| 586 | bool mounted = Is_Mounted(); |
| 587 | if (mounted || Mount(false)) { |
| 588 | // Read the backup settings file |
| 589 | DataManager::LoadPersistValues(); |
| 590 | TWFunc::Fixup_Time_On_Boot("/persist/time/"); |
| 591 | if (!mounted) |
| 592 | UnMount(false); |
| 593 | } |
| 594 | } |
| 595 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 596 | return true; |
| 597 | } |
| 598 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 599 | void TWPartition::Partition_Post_Processing(bool Display_Error) { |
| 600 | if (Mount_Point == "/data") |
| 601 | Setup_Data_Partition(Display_Error); |
| 602 | else if (Mount_Point == "/cache") |
| 603 | Setup_Cache_Partition(Display_Error); |
| 604 | } |
| 605 | |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 606 | void TWPartition::ExcludeAll(const string& path) { |
| 607 | backup_exclusions.add_absolute_dir(path); |
| 608 | wipe_exclusions.add_absolute_dir(path); |
| 609 | } |
| 610 | |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 611 | void TWPartition::Setup_Data_Partition(bool Display_Error) { |
| 612 | if (Mount_Point != "/data") |
| 613 | return; |
| 614 | |
| 615 | // Ensure /data is not mounted as tmpfs for qcom hardware decrypt |
| 616 | UnMount(false); |
| 617 | |
| 618 | #ifdef TW_INCLUDE_CRYPTO |
| 619 | if (datamedia) |
| 620 | Setup_Data_Media(); |
| 621 | Can_Be_Encrypted = true; |
| 622 | char crypto_blkdev[255]; |
| 623 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 624 | if (strcmp(crypto_blkdev, "error") != 0) { |
| 625 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 626 | Is_Encrypted = true; |
| 627 | Is_Decrypted = true; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 628 | Is_FBE = false; |
| 629 | DataManager::SetValue(TW_IS_FBE, 0); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 630 | Decrypted_Block_Device = crypto_blkdev; |
| 631 | LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev); |
| 632 | } else if (!Mount(false)) { |
| 633 | if (Is_Present) { |
| 634 | set_partition_data(Actual_Block_Device.c_str(), Crypto_Key_Location.c_str(), Fstab_File_System.c_str()); |
| 635 | if (cryptfs_check_footer() == 0) { |
| 636 | Is_Encrypted = true; |
| 637 | Is_Decrypted = false; |
| 638 | Can_Be_Mounted = false; |
| 639 | Current_File_System = "emmc"; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 640 | Setup_Image(); |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 641 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 642 | DataManager::SetValue(TW_CRYPTO_PWTYPE, cryptfs_get_password_type()); |
| 643 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 644 | DataManager::SetValue("tw_crypto_display", ""); |
| 645 | } else { |
| 646 | gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer."); |
| 647 | } |
| 648 | } else { |
| 649 | LOGERR("Primary block device '%s' for mount point '%s' is not present!\n", Primary_Block_Device.c_str(), Mount_Point.c_str()); |
| 650 | } |
| 651 | } else { |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 652 | if (TWFunc::Path_Exists("/data/unencrypted/key/version")) { |
| 653 | LOGINFO("File Based Encryption is present\n"); |
| 654 | #ifdef TW_INCLUDE_FBE |
| 655 | ExcludeAll(Mount_Point + "/convert_fbe"); |
| 656 | ExcludeAll(Mount_Point + "/unencrypted"); |
| 657 | //ExcludeAll(Mount_Point + "/system/users/0"); // we WILL need to retain some of this if multiple users are present or we just need to delete more folders for the extra users somewhere else |
| 658 | ExcludeAll(Mount_Point + "/misc/vold/user_keys"); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 659 | //ExcludeAll(Mount_Point + "/system_ce"); |
| 660 | //ExcludeAll(Mount_Point + "/system_de"); |
| 661 | //ExcludeAll(Mount_Point + "/misc_ce"); |
| 662 | //ExcludeAll(Mount_Point + "/misc_de"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 663 | ExcludeAll(Mount_Point + "/system/gatekeeper.password.key"); |
| 664 | ExcludeAll(Mount_Point + "/system/gatekeeper.pattern.key"); |
| 665 | ExcludeAll(Mount_Point + "/system/locksettings.db"); |
| 666 | //ExcludeAll(Mount_Point + "/system/locksettings.db-shm"); // don't seem to need this one, but the other 2 are needed |
| 667 | ExcludeAll(Mount_Point + "/system/locksettings.db-wal"); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 668 | //ExcludeAll(Mount_Point + "/user_de"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 669 | //ExcludeAll(Mount_Point + "/misc/profiles/cur/0"); // might be important later |
| 670 | ExcludeAll(Mount_Point + "/misc/gatekeeper"); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 671 | ExcludeAll(Mount_Point + "/misc/keystore"); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 672 | ExcludeAll(Mount_Point + "/drm/kek.dat"); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 673 | ExcludeAll(Mount_Point + "/system_de/0/spblob"); // contains data needed to decrypt pixel 2 |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 674 | int retry_count = 3; |
| 675 | while (!Decrypt_DE() && --retry_count) |
| 676 | usleep(2000); |
| 677 | if (retry_count > 0) { |
| 678 | property_set("ro.crypto.state", "encrypted"); |
| 679 | Is_Encrypted = true; |
| 680 | Is_Decrypted = false; |
| 681 | Is_FBE = true; |
| 682 | DataManager::SetValue(TW_IS_FBE, 1); |
| 683 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 684 | string filename; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 685 | int pwd_type = Get_Password_Type(0, filename); |
| 686 | if (pwd_type < 0) { |
| 687 | LOGERR("This TWRP does not have synthetic password decrypt support\n"); |
| 688 | pwd_type = 0; // default password |
| 689 | } |
| 690 | DataManager::SetValue(TW_CRYPTO_PWTYPE, pwd_type); |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 691 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 692 | DataManager::SetValue("tw_crypto_display", ""); |
| 693 | } |
| 694 | #else |
| 695 | LOGERR("FBE found but FBE support not present in TWRP\n"); |
| 696 | #endif |
| 697 | } else { |
| 698 | // Filesystem is not encrypted and the mount succeeded, so return to |
| 699 | // the original unmounted state |
| 700 | UnMount(false); |
| 701 | } |
Matt Mower | 72c87ce | 2016-04-26 14:34:56 -0500 | [diff] [blame] | 702 | } |
| 703 | if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) { |
| 704 | Setup_Data_Media(); |
| 705 | Recreate_Media_Folder(); |
| 706 | } |
| 707 | #else |
| 708 | if (datamedia) { |
| 709 | Setup_Data_Media(); |
| 710 | Recreate_Media_Folder(); |
| 711 | } |
| 712 | #endif |
| 713 | } |
| 714 | |
| 715 | void TWPartition::Setup_Cache_Partition(bool Display_Error __unused) { |
| 716 | if (Mount_Point != "/cache") |
| 717 | return; |
| 718 | |
| 719 | if (!Mount(true)) |
| 720 | return; |
| 721 | |
| 722 | if (!TWFunc::Path_Exists("/cache/recovery/.")) { |
| 723 | LOGINFO("Recreating /cache/recovery folder\n"); |
| 724 | if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) |
| 725 | LOGERR("Could not create /cache/recovery\n"); |
| 726 | } |
| 727 | } |
| 728 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 729 | void TWPartition::Process_FS_Flags(const char *str) { |
| 730 | char *options = strdup(str); |
| 731 | char *ptr, *savep; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 732 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 733 | Mount_Options = ""; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 734 | |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 735 | // Avoid issues with potentially nested strtok by using strtok_r |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 736 | for (ptr = strtok_r(options, ",", &savep); ptr; ptr = strtok_r(NULL, ",", &savep)) { |
| 737 | char *equals = strstr(ptr, "="); |
| 738 | size_t name_len; |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 739 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 740 | if (!equals) |
| 741 | name_len = strlen(ptr); |
| 742 | else |
| 743 | name_len = equals - ptr; |
| 744 | |
| 745 | // There are some flags that we want to ignore in TWRP |
| 746 | bool found_match = false; |
| 747 | for (const char** ignored_mount_item = ignored_mount_items; *ignored_mount_item; ignored_mount_item++) { |
| 748 | if (strncmp(ptr, *ignored_mount_item, name_len) == 0) { |
| 749 | found_match = true; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 750 | break; |
| 751 | } |
| 752 | } |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 753 | if (found_match) |
| 754 | continue; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 755 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 756 | // mount_flags are never postfixed by '=' |
| 757 | if (!equals) { |
| 758 | const struct flag_list* mount_flag = mount_flags; |
| 759 | for (; mount_flag->name; mount_flag++) { |
| 760 | if (strcmp(ptr, mount_flag->name) == 0) { |
| 761 | if (mount_flag->flag == MS_RDONLY) |
| 762 | Mount_Read_Only = true; |
| 763 | else |
| 764 | Mount_Flags |= (unsigned)mount_flag->flag; |
| 765 | found_match = true; |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | if (found_match) |
| 770 | continue; |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 771 | } |
| 772 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 773 | // If we aren't ignoring this flag and it's not a mount flag, then it must be a mount option |
| 774 | if (!Mount_Options.empty()) |
| 775 | Mount_Options += ","; |
| 776 | Mount_Options += ptr; |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 777 | } |
| 778 | free(options); |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 781 | void TWPartition::Save_FS_Flags(const string& local_File_System, int local_Mount_Flags, const string& local_Mount_Options) { |
| 782 | partition_fs_flags_struct flags; |
| 783 | flags.File_System = local_File_System; |
| 784 | flags.Mount_Flags = local_Mount_Flags; |
| 785 | flags.Mount_Options = local_Mount_Options; |
| 786 | fs_flags.push_back(flags); |
| 787 | } |
| 788 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 789 | void TWPartition::Apply_TW_Flag(const unsigned flag, const char* str, const bool val) { |
| 790 | switch (flag) { |
| 791 | case TWFLAG_ANDSEC: |
| 792 | Has_Android_Secure = val; |
| 793 | break; |
| 794 | case TWFLAG_BACKUP: |
| 795 | Can_Be_Backed_Up = val; |
| 796 | break; |
| 797 | case TWFLAG_BACKUPNAME: |
| 798 | Backup_Display_Name = str; |
| 799 | break; |
| 800 | case TWFLAG_BLOCKSIZE: |
| 801 | Format_Block_Size = (unsigned long)(atol(str)); |
| 802 | break; |
| 803 | case TWFLAG_CANBEWIPED: |
| 804 | Can_Be_Wiped = val; |
| 805 | break; |
| 806 | case TWFLAG_CANENCRYPTBACKUP: |
| 807 | Can_Encrypt_Backup = val; |
| 808 | break; |
| 809 | case TWFLAG_DEFAULTS: |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 810 | case TWFLAG_WAIT: |
| 811 | case TWFLAG_VERIFY: |
| 812 | case TWFLAG_CHECK: |
| 813 | case TWFLAG_NOTRIM: |
| 814 | case TWFLAG_VOLDMANAGED: |
| 815 | case TWFLAG_RESIZE: |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 816 | // Do nothing |
| 817 | break; |
| 818 | case TWFLAG_DISPLAY: |
| 819 | Display_Name = str; |
| 820 | break; |
| 821 | case TWFLAG_ENCRYPTABLE: |
| 822 | case TWFLAG_FORCEENCRYPT: |
| 823 | Crypto_Key_Location = str; |
| 824 | break; |
| 825 | case TWFLAG_FLASHIMG: |
| 826 | Can_Flash_Img = val; |
| 827 | break; |
| 828 | case TWFLAG_FSFLAGS: |
Matt Mower | 4ab42b1 | 2016-04-21 13:52:18 -0500 | [diff] [blame] | 829 | Process_FS_Flags(str); |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 830 | break; |
| 831 | case TWFLAG_IGNOREBLKID: |
| 832 | Ignore_Blkid = val; |
| 833 | break; |
| 834 | case TWFLAG_LENGTH: |
| 835 | Length = atoi(str); |
| 836 | break; |
| 837 | case TWFLAG_MOUNTTODECRYPT: |
| 838 | Mount_To_Decrypt = val; |
| 839 | break; |
| 840 | case TWFLAG_REMOVABLE: |
| 841 | Removable = val; |
| 842 | break; |
| 843 | case TWFLAG_RETAINLAYOUTVERSION: |
| 844 | Retain_Layout_Version = val; |
| 845 | break; |
| 846 | case TWFLAG_SETTINGSSTORAGE: |
| 847 | Is_Settings_Storage = val; |
| 848 | if (Is_Settings_Storage) |
| 849 | Is_Storage = true; |
| 850 | break; |
| 851 | case TWFLAG_STORAGE: |
| 852 | Is_Storage = val; |
| 853 | break; |
| 854 | case TWFLAG_STORAGENAME: |
| 855 | Storage_Name = str; |
| 856 | break; |
| 857 | case TWFLAG_SUBPARTITIONOF: |
| 858 | Is_SubPartition = true; |
| 859 | SubPartition_Of = str; |
| 860 | break; |
| 861 | case TWFLAG_SYMLINK: |
| 862 | Symlink_Path = str; |
| 863 | break; |
| 864 | case TWFLAG_USERDATAENCRYPTBACKUP: |
| 865 | Use_Userdata_Encryption = val; |
| 866 | if (Use_Userdata_Encryption) |
| 867 | Can_Encrypt_Backup = true; |
| 868 | break; |
| 869 | case TWFLAG_USERMRF: |
| 870 | Use_Rm_Rf = val; |
| 871 | break; |
| 872 | case TWFLAG_WIPEDURINGFACTORYRESET: |
| 873 | Wipe_During_Factory_Reset = val; |
| 874 | if (Wipe_During_Factory_Reset) { |
| 875 | Can_Be_Wiped = true; |
| 876 | Wipe_Available_in_GUI = true; |
| 877 | } |
| 878 | break; |
| 879 | case TWFLAG_WIPEINGUI: |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 880 | case TWFLAG_FORMATTABLE: |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 881 | Wipe_Available_in_GUI = val; |
| 882 | if (Wipe_Available_in_GUI) |
| 883 | Can_Be_Wiped = true; |
| 884 | break; |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 885 | case TWFLAG_SLOTSELECT: |
| 886 | SlotSelect = true; |
| 887 | break; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 888 | case TWFLAG_ALTDEVICE: |
| 889 | Alternate_Block_Device = str; |
| 890 | break; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 891 | default: |
| 892 | // Should not get here |
| 893 | LOGINFO("Flag identified for processing, but later unmatched: %i\n", flag); |
| 894 | break; |
| 895 | } |
| 896 | } |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 897 | |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 898 | void TWPartition::Process_TW_Flags(char *flags, bool Display_Error, int fstab_ver) { |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 899 | char separator[2] = {'\n', 0}; |
| 900 | char *ptr, *savep; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 901 | char source_separator = ';'; |
| 902 | |
| 903 | if (fstab_ver == 2) |
| 904 | source_separator = ','; |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 905 | |
| 906 | // Semicolons within double-quotes are not forbidden, so replace |
| 907 | // only the semicolons intended as separators with '\n' for strtok |
| 908 | for (unsigned i = 0, skip = 0; i < strlen(flags); i++) { |
| 909 | if (flags[i] == '\"') |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 910 | skip = !skip; |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 911 | if (!skip && flags[i] == source_separator) |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 912 | flags[i] = separator[0]; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 913 | } |
| 914 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 915 | // Avoid issues with potentially nested strtok by using strtok_r |
| 916 | ptr = strtok_r(flags, separator, &savep); |
| 917 | while (ptr) { |
| 918 | int ptr_len = strlen(ptr); |
| 919 | const struct flag_list* tw_flag = tw_flags; |
Hashcode | 62bd9e0 | 2013-11-19 21:59:42 -0800 | [diff] [blame] | 920 | |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 921 | for (; tw_flag->name; tw_flag++) { |
| 922 | int flag_len = strlen(tw_flag->name); |
| 923 | |
| 924 | if (strncmp(ptr, tw_flag->name, flag_len) == 0) { |
| 925 | bool flag_val = false; |
| 926 | |
| 927 | if (ptr_len > flag_len && (tw_flag->name)[flag_len-1] != '=' |
| 928 | && ptr[flag_len] != '=') { |
| 929 | // Handle flags with same starting string |
| 930 | // (e.g. backup and backupname) |
| 931 | continue; |
| 932 | } else if (ptr_len > flag_len && ptr[flag_len] == '=') { |
| 933 | // Handle flags with dual format: Part 1 |
| 934 | // (e.g. backup and backup=y. backup=y handled here) |
| 935 | ptr += flag_len + 1; |
| 936 | TWFunc::Strip_Quotes(ptr); |
| 937 | // Skip flags with empty argument |
| 938 | // (e.g. backup=) |
| 939 | if (strlen(ptr) == 0) { |
| 940 | LOGINFO("Flag missing argument or should not include '=': %s=\n", tw_flag->name); |
| 941 | break; |
| 942 | } |
| 943 | flag_val = strchr("yY1", *ptr) != NULL; |
| 944 | } else if (ptr_len == flag_len |
| 945 | && (tw_flag->name)[flag_len-1] == '=') { |
| 946 | // Skip flags missing argument after = |
| 947 | // (e.g. backupname=) |
| 948 | LOGINFO("Flag missing argument: %s\n", tw_flag->name); |
| 949 | break; |
| 950 | } else if (ptr_len > flag_len |
| 951 | && (tw_flag->name)[flag_len-1] == '=') { |
| 952 | // Handle arguments to flags |
| 953 | // (e.g. backupname="My Stuff") |
| 954 | ptr += flag_len; |
| 955 | TWFunc::Strip_Quotes(ptr); |
| 956 | // Skip flags with empty argument |
| 957 | // (e.g. backupname="") |
| 958 | if (strlen(ptr) == 0) { |
| 959 | LOGINFO("Flag missing argument: %s\n", tw_flag->name); |
| 960 | break; |
| 961 | } |
| 962 | } else if (ptr_len == flag_len) { |
| 963 | // Handle flags with dual format: Part 2 |
| 964 | // (e.g. backup and backup=y. backup handled here) |
| 965 | flag_val = true; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 966 | } else { |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 967 | LOGINFO("Flag matched, but could not be processed: %s\n", ptr); |
| 968 | break; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 969 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 970 | |
| 971 | Apply_TW_Flag(tw_flag->flag, ptr, flag_val); |
| 972 | break; |
Ethan Yonker | 96af84a | 2015-01-05 14:58:36 -0600 | [diff] [blame] | 973 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 974 | } |
| 975 | if (tw_flag->name == 0) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 976 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 977 | LOGERR("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 978 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 979 | LOGINFO("Unhandled flag: '%s'\n", ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 980 | } |
Matt Mower | 2416a50 | 2016-04-12 19:54:46 -0500 | [diff] [blame] | 981 | ptr = strtok_r(NULL, separator, &savep); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 982 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 983 | } |
| 984 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 985 | bool TWPartition::Is_File_System(string File_System) { |
| 986 | if (File_System == "ext2" || |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 987 | File_System == "ext3" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 988 | File_System == "ext4" || |
| 989 | File_System == "vfat" || |
| 990 | File_System == "ntfs" || |
| 991 | File_System == "yaffs2" || |
bigbiff bigbiff | 3e14652 | 2012-11-14 14:32:59 -0500 | [diff] [blame] | 992 | File_System == "exfat" || |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 993 | File_System == "f2fs" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 994 | File_System == "auto") |
| 995 | return true; |
| 996 | else |
| 997 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 998 | } |
| 999 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1000 | bool TWPartition::Is_Image(string File_System) { |
Dees_Troy | 5fcd8f9 | 2012-10-16 12:22:05 -0400 | [diff] [blame] | 1001 | if (File_System == "emmc" || File_System == "mtd" || File_System == "bml") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1002 | return true; |
| 1003 | else |
| 1004 | return false; |
| 1005 | } |
| 1006 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1007 | bool TWPartition::Make_Dir(string Path, bool Display_Error) { |
Ethan Yonker | 93ac7a0 | 2016-11-07 22:05:58 -0600 | [diff] [blame] | 1008 | if (TWFunc::Get_D_Type_From_Stat(Path) != S_IFDIR) |
| 1009 | unlink(Path.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 1010 | if (!TWFunc::Path_Exists(Path)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1011 | if (mkdir(Path.c_str(), 0777) == -1) { |
| 1012 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1013 | gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2}).")(Path)(strerror(errno))); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1014 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1015 | LOGINFO("Can not create '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1016 | return false; |
| 1017 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1018 | LOGINFO("Created '%s' folder.\n", Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1019 | return true; |
| 1020 | } |
| 1021 | } |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1025 | void TWPartition::Setup_File_System(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1026 | Can_Be_Mounted = true; |
| 1027 | Can_Be_Wiped = true; |
| 1028 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1029 | // Make the mount point folder if it doesn't exist |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1030 | Make_Dir(Mount_Point, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1031 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 1032 | Backup_Name = Display_Name; |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1033 | Backup_Method = BM_FILES; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1034 | } |
| 1035 | |
Ethan Yonker | 1b19016 | 2016-12-05 15:25:19 -0600 | [diff] [blame] | 1036 | void TWPartition::Setup_Image() { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1037 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 1038 | Backup_Name = Display_Name; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1039 | if (Current_File_System == "emmc") |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1040 | Backup_Method = BM_DD; |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1041 | else if (Current_File_System == "mtd" || Current_File_System == "bml") |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1042 | Backup_Method = BM_FLASH_UTILS; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1043 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1044 | LOGINFO("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1045 | } |
| 1046 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1047 | void TWPartition::Setup_AndSec(void) { |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1048 | Backup_Display_Name = "Android Secure"; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1049 | Backup_Name = "and-sec"; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 1050 | Can_Be_Backed_Up = true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1051 | Has_Android_Secure = true; |
| 1052 | Symlink_Path = Mount_Point + "/.android_secure"; |
| 1053 | Symlink_Mount_Point = "/and-sec"; |
| 1054 | Backup_Path = Symlink_Mount_Point; |
| 1055 | Make_Dir("/and-sec", true); |
| 1056 | Recreate_AndSec_Folder(); |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1057 | Mount_Storage_Retry(true); |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1058 | } |
| 1059 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 1060 | void TWPartition::Setup_Data_Media() { |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1061 | LOGINFO("Setting up '%s' as data/media emulated storage.\n", Mount_Point.c_str()); |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1062 | if (Storage_Name.empty() || Storage_Name == "Data") |
| 1063 | Storage_Name = "Internal Storage"; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1064 | Has_Data_Media = true; |
| 1065 | Is_Storage = true; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1066 | Storage_Path = Mount_Point + "/media"; |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1067 | Symlink_Path = Storage_Path; |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1068 | if (Mount_Point == "/data") { |
| 1069 | Is_Settings_Storage = true; |
| 1070 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 1071 | Make_Dir("/emmc", false); |
| 1072 | Symlink_Mount_Point = "/emmc"; |
| 1073 | } else { |
| 1074 | Make_Dir("/sdcard", false); |
| 1075 | Symlink_Mount_Point = "/sdcard"; |
| 1076 | } |
| 1077 | if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) { |
| 1078 | Storage_Path = Mount_Point + "/media/0"; |
| 1079 | Symlink_Path = Storage_Path; |
| 1080 | DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0"); |
| 1081 | UnMount(true); |
| 1082 | } |
| 1083 | DataManager::SetValue("tw_has_internal", 1); |
| 1084 | DataManager::SetValue("tw_has_data_media", 1); |
Ethan Yonker | 3fdcda4 | 2016-11-30 12:29:37 -0600 | [diff] [blame] | 1085 | backup_exclusions.add_absolute_dir("/data/data/com.google.android.music/files"); |
Ethan Yonker | 6355b56 | 2017-05-01 09:42:17 -0500 | [diff] [blame] | 1086 | wipe_exclusions.add_absolute_dir(Mount_Point + "/misc/vold"); // adopted storage keys |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 1087 | ExcludeAll(Mount_Point + "/.layout_version"); |
| 1088 | ExcludeAll(Mount_Point + "/system/storage.xml"); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1089 | } else { |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1090 | if (Mount(true) && TWFunc::Path_Exists(Mount_Point + "/media/0")) { |
| 1091 | Storage_Path = Mount_Point + "/media/0"; |
| 1092 | Symlink_Path = Storage_Path; |
| 1093 | UnMount(true); |
| 1094 | } |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1095 | } |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 1096 | ExcludeAll(Mount_Point + "/media"); |
Ethan Yonker | 6277c79 | 2014-09-15 14:54:30 -0500 | [diff] [blame] | 1097 | } |
| 1098 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1099 | void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { |
Ethan Yonker | 6e8c27a | 2016-12-22 17:55:57 -0600 | [diff] [blame] | 1100 | char device[PATH_MAX], realDevice[PATH_MAX]; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1101 | |
| 1102 | strcpy(device, Block.c_str()); |
| 1103 | memset(realDevice, 0, sizeof(realDevice)); |
| 1104 | while (readlink(device, realDevice, sizeof(realDevice)) > 0) |
| 1105 | { |
| 1106 | strcpy(device, realDevice); |
| 1107 | memset(realDevice, 0, sizeof(realDevice)); |
| 1108 | } |
| 1109 | |
| 1110 | if (device[0] != '/') { |
| 1111 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1112 | LOGERR("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1113 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1114 | LOGINFO("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1115 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1116 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1117 | Block = device; |
| 1118 | return; |
| 1119 | } |
| 1120 | } |
| 1121 | |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1122 | bool TWPartition::Mount_Storage_Retry(bool Display_Error) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1123 | // On some devices, storage doesn't want to mount right away, retry and sleep |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1124 | if (!Mount(Display_Error)) { |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1125 | int retry_count = 5; |
| 1126 | while (retry_count > 0 && !Mount(false)) { |
| 1127 | usleep(500000); |
| 1128 | retry_count--; |
| 1129 | } |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1130 | return Mount(Display_Error); |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1131 | } |
Kjell Braden | 3126a11 | 2016-06-19 16:58:15 +0000 | [diff] [blame] | 1132 | return true; |
Dees_Troy | 8e337f3 | 2012-10-13 22:07:49 -0400 | [diff] [blame] | 1133 | } |
| 1134 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1135 | bool TWPartition::Find_MTD_Block_Device(string MTD_Name) { |
| 1136 | FILE *fp = NULL; |
| 1137 | char line[255]; |
| 1138 | |
| 1139 | fp = fopen("/proc/mtd", "rt"); |
| 1140 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1141 | LOGERR("Device does not support /proc/mtd\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1142 | return false; |
| 1143 | } |
| 1144 | |
| 1145 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1146 | { |
| 1147 | char device[32], label[32]; |
| 1148 | unsigned long size = 0; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1149 | int deviceId; |
| 1150 | |
| 1151 | sscanf(line, "%s %lx %*s %*c%s", device, &size, label); |
| 1152 | |
| 1153 | // Skip header and blank lines |
| 1154 | if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8)) |
| 1155 | continue; |
| 1156 | |
| 1157 | // Strip off the trailing " from the label |
| 1158 | label[strlen(label)-1] = '\0'; |
| 1159 | |
| 1160 | if (strcmp(label, MTD_Name.c_str()) == 0) { |
| 1161 | // We found our device |
| 1162 | // Strip off the trailing : from the device |
| 1163 | device[strlen(device)-1] = '\0'; |
| 1164 | if (sscanf(device,"mtd%d", &deviceId) == 1) { |
| 1165 | sprintf(device, "/dev/block/mtdblock%d", deviceId); |
| 1166 | Primary_Block_Device = device; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1167 | fclose(fp); |
| 1168 | return true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | fclose(fp); |
| 1173 | |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1177 | bool TWPartition::Get_Size_Via_statfs(bool Display_Error) { |
| 1178 | struct statfs st; |
| 1179 | string Local_Path = Mount_Point + "/."; |
| 1180 | |
| 1181 | if (!Mount(Display_Error)) |
| 1182 | return false; |
| 1183 | |
| 1184 | if (statfs(Local_Path.c_str(), &st) != 0) { |
| 1185 | if (!Removable) { |
| 1186 | if (Display_Error) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1187 | LOGERR("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1188 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1189 | LOGINFO("Unable to statfs '%s'\n", Local_Path.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1190 | } |
| 1191 | return false; |
| 1192 | } |
| 1193 | Size = (st.f_blocks * st.f_bsize); |
| 1194 | Used = ((st.f_blocks - st.f_bfree) * st.f_bsize); |
| 1195 | Free = (st.f_bfree * st.f_bsize); |
| 1196 | Backup_Size = Used; |
| 1197 | return true; |
| 1198 | } |
| 1199 | |
| 1200 | bool TWPartition::Get_Size_Via_df(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1201 | FILE* fp; |
| 1202 | char command[255], line[512]; |
| 1203 | int include_block = 1; |
| 1204 | unsigned int min_len; |
| 1205 | |
| 1206 | if (!Mount(Display_Error)) |
| 1207 | return false; |
| 1208 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1209 | min_len = Actual_Block_Device.size() + 2; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1210 | sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str()); |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1211 | TWFunc::Exec_Cmd(command); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1212 | fp = fopen("/tmp/dfoutput.txt", "rt"); |
| 1213 | if (fp == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1214 | LOGINFO("Unable to open /tmp/dfoutput.txt.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1215 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1216 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1217 | |
| 1218 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1219 | { |
| 1220 | unsigned long blocks, used, available; |
| 1221 | char device[64]; |
| 1222 | char tmpString[64]; |
| 1223 | |
| 1224 | if (strncmp(line, "Filesystem", 10) == 0) |
| 1225 | continue; |
| 1226 | if (strlen(line) < min_len) { |
| 1227 | include_block = 0; |
| 1228 | continue; |
| 1229 | } |
| 1230 | if (include_block) { |
| 1231 | sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available); |
| 1232 | } else { |
| 1233 | // The device block string is so long that the df information is on the next line |
| 1234 | int space_count = 0; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1235 | sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1236 | while (tmpString[space_count] == 32) |
| 1237 | space_count++; |
| 1238 | sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available); |
| 1239 | } |
| 1240 | |
| 1241 | // Adjust block size to byte size |
| 1242 | Size = blocks * 1024ULL; |
| 1243 | Used = used * 1024ULL; |
| 1244 | Free = available * 1024ULL; |
| 1245 | Backup_Size = Used; |
| 1246 | } |
| 1247 | fclose(fp); |
| 1248 | return true; |
| 1249 | } |
| 1250 | |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1251 | unsigned long long TWPartition::IOCTL_Get_Block_Size() { |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1252 | Find_Actual_Block_Device(); |
Ethan Yonker | 483e9f4 | 2016-01-11 22:21:18 -0600 | [diff] [blame] | 1253 | |
| 1254 | return TWFunc::IOCTL_Get_Block_Size(Actual_Block_Device.c_str()); |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1255 | } |
| 1256 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1257 | bool TWPartition::Find_Partition_Size(void) { |
| 1258 | FILE* fp; |
| 1259 | char line[512]; |
| 1260 | string tmpdevice; |
| 1261 | |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1262 | fp = fopen("/proc/dumchar_info", "rt"); |
| 1263 | if (fp != NULL) { |
| 1264 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1265 | { |
| 1266 | char label[32], device[32]; |
| 1267 | unsigned long size = 0; |
| 1268 | |
that | d43bf2d | 2014-09-21 23:13:02 +0200 | [diff] [blame] | 1269 | sscanf(line, "%s %lx %*x %*u %s", label, &size, device); |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1270 | |
bigbiff bigbiff | 34684ff | 2013-12-01 21:03:45 -0500 | [diff] [blame] | 1271 | // Skip header, annotation and blank lines |
igoriok | 87e3d93 | 2013-01-31 21:03:53 +0200 | [diff] [blame] | 1272 | if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8)) |
| 1273 | continue; |
| 1274 | |
| 1275 | tmpdevice = "/dev/"; |
| 1276 | tmpdevice += label; |
| 1277 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
| 1278 | Size = size; |
| 1279 | fclose(fp); |
| 1280 | return true; |
| 1281 | } |
| 1282 | } |
| 1283 | } |
| 1284 | |
Ethan Yonker | d18a821 | 2015-12-14 10:17:00 -0600 | [diff] [blame] | 1285 | unsigned long long ioctl_size = IOCTL_Get_Block_Size(); |
| 1286 | if (ioctl_size) { |
| 1287 | Size = ioctl_size; |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1291 | // In this case, we'll first get the partitions we care about (with labels) |
| 1292 | fp = fopen("/proc/partitions", "rt"); |
| 1293 | if (fp == NULL) |
| 1294 | return false; |
| 1295 | |
| 1296 | while (fgets(line, sizeof(line), fp) != NULL) |
| 1297 | { |
| 1298 | unsigned long major, minor, blocks; |
| 1299 | char device[512]; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1300 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 1301 | if (strlen(line) < 7 || line[0] == 'm') continue; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1302 | sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device); |
| 1303 | |
| 1304 | tmpdevice = "/dev/block/"; |
| 1305 | tmpdevice += device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1306 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1307 | // Adjust block size to byte size |
| 1308 | Size = blocks * 1024ULL; |
| 1309 | fclose(fp); |
| 1310 | return true; |
| 1311 | } |
| 1312 | } |
| 1313 | fclose(fp); |
| 1314 | return false; |
| 1315 | } |
| 1316 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1317 | bool TWPartition::Is_Mounted(void) { |
| 1318 | if (!Can_Be_Mounted) |
| 1319 | return false; |
| 1320 | |
| 1321 | struct stat st1, st2; |
| 1322 | string test_path; |
| 1323 | |
| 1324 | // Check to see if the mount point directory exists |
| 1325 | test_path = Mount_Point + "/."; |
| 1326 | if (stat(test_path.c_str(), &st1) != 0) return false; |
| 1327 | |
| 1328 | // Check to see if the directory above the mount point exists |
| 1329 | test_path = Mount_Point + "/../."; |
| 1330 | if (stat(test_path.c_str(), &st2) != 0) return false; |
| 1331 | |
| 1332 | // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device |
| 1333 | int ret = (st1.st_dev != st2.st_dev) ? true : false; |
| 1334 | |
| 1335 | return ret; |
| 1336 | } |
| 1337 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 1338 | bool TWPartition::Is_File_System_Writable(void) { |
| 1339 | if (!Is_File_System(Current_File_System) || !Is_Mounted()) |
| 1340 | return false; |
| 1341 | |
| 1342 | string test_path = Mount_Point + "/."; |
| 1343 | return (access(test_path.c_str(), W_OK) == 0); |
| 1344 | } |
| 1345 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1346 | bool TWPartition::Mount(bool Display_Error) { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1347 | int exfat_mounted = 0; |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1348 | unsigned long flags = Mount_Flags; |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1349 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1350 | if (Is_Mounted()) { |
| 1351 | return true; |
| 1352 | } else if (!Can_Be_Mounted) { |
| 1353 | return false; |
| 1354 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1355 | |
| 1356 | Find_Actual_Block_Device(); |
| 1357 | |
| 1358 | // Check the current file system before mounting |
| 1359 | Check_FS_Type(); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1360 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) { |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 1361 | string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1362 | LOGINFO("cmd: %s\n", cmd.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1363 | string result; |
| 1364 | if (TWFunc::Exec_Cmd(cmd, result) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1365 | LOGINFO("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1366 | Current_File_System = "vfat"; |
| 1367 | } else { |
| 1368 | #ifdef TW_NO_EXFAT_FUSE |
| 1369 | UnMount(false); |
| 1370 | // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat |
| 1371 | // Some kernels let us mount vfat as exfat which doesn't work out too well |
| 1372 | #else |
| 1373 | exfat_mounted = 1; |
| 1374 | #endif |
| 1375 | } |
| 1376 | } |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1377 | |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1378 | if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfs-3g") || TWFunc::Path_Exists("/sbin/mount.ntfs"))) { |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1379 | string cmd; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1380 | string Ntfsmount_Binary = ""; |
| 1381 | |
| 1382 | if (TWFunc::Path_Exists("/sbin/ntfs-3g")) |
| 1383 | Ntfsmount_Binary = "ntfs-3g"; |
| 1384 | else if (TWFunc::Path_Exists("/sbin/mount.ntfs")) |
| 1385 | Ntfsmount_Binary = "mount.ntfs"; |
| 1386 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1387 | if (Mount_Read_Only) |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1388 | cmd = "/sbin/" + Ntfsmount_Binary + " -o ro " + Actual_Block_Device + " " + Mount_Point; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1389 | else |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1390 | cmd = "/sbin/" + Ntfsmount_Binary + " " + Actual_Block_Device + " " + Mount_Point; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1391 | LOGINFO("cmd: '%s'\n", cmd.c_str()); |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1392 | |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1393 | if (TWFunc::Exec_Cmd(cmd) == 0) { |
| 1394 | return true; |
| 1395 | } else { |
| 1396 | LOGINFO("ntfs-3g failed to mount, trying regular mount method.\n"); |
| 1397 | } |
| 1398 | } |
| 1399 | |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1400 | if (Mount_Read_Only) |
| 1401 | flags |= MS_RDONLY; |
| 1402 | |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1403 | if (Fstab_File_System == "yaffs2") { |
| 1404 | // mount an MTD partition as a YAFFS2 filesystem. |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1405 | flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME; |
| 1406 | if (Mount_Read_Only) |
| 1407 | flags |= MS_RDONLY; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1408 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) { |
| 1409 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) { |
| 1410 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1411 | gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1412 | else |
| 1413 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1414 | return false; |
| 1415 | } else { |
| 1416 | LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str()); |
| 1417 | return true; |
| 1418 | } |
| 1419 | } else { |
| 1420 | struct stat st; |
| 1421 | string test_path = Mount_Point; |
| 1422 | if (stat(test_path.c_str(), &st) < 0) { |
| 1423 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1424 | gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1425 | else |
| 1426 | LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str()); |
| 1427 | return false; |
| 1428 | } |
| 1429 | mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH; |
| 1430 | if (new_mode != st.st_mode) { |
| 1431 | LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str()); |
| 1432 | if (chmod(Mount_Point.c_str(), new_mode) < 0) { |
| 1433 | if (Display_Error) |
| 1434 | LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1435 | else |
| 1436 | LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno)); |
| 1437 | return false; |
| 1438 | } |
| 1439 | } |
Dees_Troy | 2204203 | 2012-12-18 21:23:08 +0000 | [diff] [blame] | 1440 | return true; |
Dees_Troy | 76543db | 2013-06-19 16:24:30 +0000 | [diff] [blame] | 1441 | } |
that | c7572eb | 2015-03-31 18:55:30 +0200 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | string mount_fs = Current_File_System; |
| 1445 | if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sys/module/texfat")) |
| 1446 | mount_fs = "texfat"; |
| 1447 | |
| 1448 | if (!exfat_mounted && |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1449 | mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, Mount_Options.c_str()) != 0 && |
| 1450 | mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), mount_fs.c_str(), flags, NULL) != 0) { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1451 | #ifdef TW_NO_EXFAT_FUSE |
| 1452 | if (Current_File_System == "exfat") { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1453 | LOGINFO("Mounting exfat failed, trying vfat...\n"); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1454 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) { |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1455 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1456 | gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1457 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1458 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
Ethan Yonker | eb32b1f | 2015-05-18 10:23:03 -0500 | [diff] [blame] | 1459 | 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(), flags, Mount_Options.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1460 | return false; |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1461 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1462 | } else { |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1463 | #endif |
bigbiff bigbiff | 26774a0 | 2014-03-29 18:22:00 -0400 | [diff] [blame] | 1464 | if (!Removable && Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1465 | gui_msg(Msg(msg::kError, "fail_mount=Failed to mount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1466 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1467 | LOGINFO("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 1468 | LOGINFO("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str()); |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1469 | return false; |
| 1470 | #ifdef TW_NO_EXFAT_FUSE |
Dees_Troy | 85f44ed | 2013-01-09 18:42:36 +0000 | [diff] [blame] | 1471 | } |
| 1472 | #endif |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1473 | } |
Ethan Yonker | 253368a | 2014-11-25 15:00:52 -0600 | [diff] [blame] | 1474 | |
Dees_Troy | 3f5c4e8 | 2013-02-01 15:16:59 +0000 | [diff] [blame] | 1475 | if (Removable) |
| 1476 | Update_Size(Display_Error); |
| 1477 | |
xiaolu | 9416f4f | 2015-06-04 08:22:23 +0800 | [diff] [blame] | 1478 | if (!Symlink_Mount_Point.empty() && TWFunc::Path_Exists(Symlink_Path)) { |
codelover | 2a3d4ce | 2015-03-14 20:26:49 +0800 | [diff] [blame] | 1479 | string Command = "mount -o bind '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'"; |
Vojtech Bocek | 0553420 | 2013-09-11 08:11:56 +0200 | [diff] [blame] | 1480 | TWFunc::Exec_Cmd(Command); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1481 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1482 | return true; |
| 1483 | } |
| 1484 | |
| 1485 | bool TWPartition::UnMount(bool Display_Error) { |
| 1486 | if (Is_Mounted()) { |
| 1487 | int never_unmount_system; |
| 1488 | |
| 1489 | DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system); |
| 1490 | if (never_unmount_system == 1 && Mount_Point == "/system") |
| 1491 | return true; // Never unmount system if you're not supposed to unmount it |
| 1492 | |
Matt Mower | a8e6d83 | 2017-02-14 20:20:59 -0600 | [diff] [blame] | 1493 | if (Is_Storage && MTP_Storage_ID > 0) |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1494 | PartitionManager.Remove_MTP_Storage(MTP_Storage_ID); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1495 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1496 | if (!Symlink_Mount_Point.empty()) |
| 1497 | umount(Symlink_Mount_Point.c_str()); |
| 1498 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 1499 | umount(Mount_Point.c_str()); |
| 1500 | if (Is_Mounted()) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1501 | if (Display_Error) |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1502 | gui_msg(Msg(msg::kError, "fail_unmount=Failed to unmount '{1}' ({2})")(Mount_Point)(strerror(errno))); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1503 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1504 | LOGINFO("Unable to unmount '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1505 | return false; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1506 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1507 | return true; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1508 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1509 | } else { |
| 1510 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1511 | } |
| 1512 | } |
| 1513 | |
James Christopher Adduono | d6f94ac | 2016-02-29 04:26:04 -0500 | [diff] [blame] | 1514 | bool TWPartition::ReMount(bool Display_Error) { |
| 1515 | if (UnMount(Display_Error)) |
| 1516 | return Mount(Display_Error); |
| 1517 | return false; |
| 1518 | } |
| 1519 | |
| 1520 | bool TWPartition::ReMount_RW(bool Display_Error) { |
| 1521 | // No need to remount if already mounted rw |
| 1522 | if (Is_File_System_Writable()) |
| 1523 | return true; |
| 1524 | |
| 1525 | bool ro = Mount_Read_Only; |
| 1526 | int flags = Mount_Flags; |
| 1527 | |
| 1528 | Mount_Read_Only = false; |
| 1529 | Mount_Flags &= ~MS_RDONLY; |
| 1530 | |
| 1531 | bool ret = ReMount(Display_Error); |
| 1532 | |
| 1533 | Mount_Read_Only = ro; |
| 1534 | Mount_Flags = flags; |
| 1535 | |
| 1536 | return ret; |
| 1537 | } |
| 1538 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1539 | bool TWPartition::Wipe(string New_File_System) { |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 1540 | bool wiped = false, update_crypt = false, recreate_media = true; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1541 | int check; |
| 1542 | string Layout_Filename = Mount_Point + "/.layout_version"; |
| 1543 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1544 | if (!Can_Be_Wiped) { |
Matt Mower | 3c36697 | 2015-12-25 19:28:31 -0600 | [diff] [blame] | 1545 | gui_msg(Msg(msg::kError, "cannot_wipe=Partition {1} cannot be wiped.")(Display_Name)); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1546 | return false; |
| 1547 | } |
| 1548 | |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1549 | if (Mount_Point == "/cache") |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1550 | Log_Offset = 0; |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 1551 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1552 | if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename)) |
| 1553 | TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600); |
| 1554 | else |
| 1555 | unlink("/.layout_version"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1556 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1557 | if (Has_Data_Media && Current_File_System == New_File_System) { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1558 | wiped = Wipe_Data_Without_Wiping_Media(); |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1559 | recreate_media = false; |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1560 | } else { |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1561 | DataManager::GetValue(TW_RM_RF_VAR, check); |
| 1562 | |
Hashcode | dabfd49 | 2013-08-29 22:45:30 -0700 | [diff] [blame] | 1563 | if (check || Use_Rm_Rf) |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1564 | wiped = Wipe_RMRF(); |
| 1565 | else if (New_File_System == "ext4") |
| 1566 | wiped = Wipe_EXT4(); |
| 1567 | else if (New_File_System == "ext2" || New_File_System == "ext3") |
| 1568 | wiped = Wipe_EXT23(New_File_System); |
| 1569 | else if (New_File_System == "vfat") |
| 1570 | wiped = Wipe_FAT(); |
| 1571 | else if (New_File_System == "exfat") |
| 1572 | wiped = Wipe_EXFAT(); |
| 1573 | else if (New_File_System == "yaffs2") |
| 1574 | wiped = Wipe_MTD(); |
Dees_Troy | e501704 | 2013-08-29 16:38:55 +0000 | [diff] [blame] | 1575 | else if (New_File_System == "f2fs") |
| 1576 | wiped = Wipe_F2FS(); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1577 | else if (New_File_System == "ntfs") |
| 1578 | wiped = Wipe_NTFS(); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1579 | else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 1580 | LOGERR("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str()); |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1581 | unlink("/.layout_version"); |
| 1582 | return false; |
| 1583 | } |
| 1584 | update_crypt = wiped; |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1585 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1586 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1587 | if (wiped) { |
Dees_Troy | 1c1ac44 | 2013-01-17 21:42:14 +0000 | [diff] [blame] | 1588 | if (Mount_Point == "/cache") |
| 1589 | DataManager::Output_Version(); |
| 1590 | |
Dees_Troy | 16c2b31 | 2013-01-15 16:51:18 +0000 | [diff] [blame] | 1591 | if (TWFunc::Path_Exists("/.layout_version") && Mount(false)) |
| 1592 | TWFunc::copy_file("/.layout_version", Layout_Filename, 0600); |
| 1593 | |
| 1594 | if (update_crypt) { |
| 1595 | Setup_File_System(false); |
| 1596 | if (Is_Encrypted && !Is_Decrypted) { |
| 1597 | // just wiped an encrypted partition back to its unencrypted state |
| 1598 | Is_Encrypted = false; |
| 1599 | Is_Decrypted = false; |
| 1600 | Decrypted_Block_Device = ""; |
| 1601 | if (Mount_Point == "/data") { |
| 1602 | DataManager::SetValue(TW_IS_ENCRYPTED, 0); |
| 1603 | DataManager::SetValue(TW_IS_DECRYPTED, 0); |
| 1604 | } |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1605 | } |
| 1606 | } |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1607 | |
Ethan Yonker | 66a1949 | 2015-12-10 10:19:45 -0600 | [diff] [blame] | 1608 | if (Has_Data_Media && recreate_media) { |
Ethan Yonker | 5eac222 | 2014-06-11 12:22:55 -0500 | [diff] [blame] | 1609 | Recreate_Media_Folder(); |
| 1610 | } |
Matt Mower | 209c963 | 2016-01-20 12:08:35 -0600 | [diff] [blame] | 1611 | if (Is_Storage && Mount(false)) |
| 1612 | PartitionManager.Add_MTP_Storage(MTP_Storage_ID); |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1613 | } |
Matt Mower | 209c963 | 2016-01-20 12:08:35 -0600 | [diff] [blame] | 1614 | |
Gary Peck | e8bc5d7 | 2012-12-21 06:45:25 -0800 | [diff] [blame] | 1615 | return wiped; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1616 | } |
| 1617 | |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1618 | bool TWPartition::Wipe() { |
Gary Peck | 82599a8 | 2012-11-21 16:23:12 -0800 | [diff] [blame] | 1619 | if (Is_File_System(Current_File_System)) |
| 1620 | return Wipe(Current_File_System); |
| 1621 | else |
| 1622 | return Wipe(Fstab_File_System); |
Gary Peck | 43acadf | 2012-11-21 21:19:01 -0800 | [diff] [blame] | 1623 | } |
| 1624 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1625 | bool TWPartition::Wipe_AndSec(void) { |
| 1626 | if (!Has_Android_Secure) |
| 1627 | return false; |
| 1628 | |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1629 | if (!Mount(true)) |
| 1630 | return false; |
| 1631 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1632 | gui_msg(Msg("wiping=Wiping {1}")(Backup_Display_Name)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1633 | TWFunc::removeDir(Mount_Point + "/.android_secure/", true); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 1634 | return true; |
Dees_Troy | e58d526 | 2012-09-21 12:27:57 -0400 | [diff] [blame] | 1635 | } |
| 1636 | |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1637 | bool TWPartition::Can_Repair() { |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1638 | if (Mount_Read_Only) |
| 1639 | return false; |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1640 | if (Current_File_System == "vfat" && TWFunc::Path_Exists("/sbin/fsck.fat")) |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1641 | return true; |
| 1642 | else if ((Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") && TWFunc::Path_Exists("/sbin/e2fsck")) |
| 1643 | return true; |
| 1644 | else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/fsck.exfat")) |
| 1645 | return true; |
| 1646 | else if (Current_File_System == "f2fs" && TWFunc::Path_Exists("/sbin/fsck.f2fs")) |
| 1647 | return true; |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1648 | else if (Current_File_System == "ntfs" && (TWFunc::Path_Exists("/sbin/ntfsfix") || TWFunc::Path_Exists("/sbin/fsck.ntfs"))) |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1649 | return true; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1650 | return false; |
| 1651 | } |
| 1652 | |
| 1653 | bool TWPartition::Repair() { |
| 1654 | string command; |
| 1655 | |
| 1656 | if (Current_File_System == "vfat") { |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1657 | if (!TWFunc::Path_Exists("/sbin/fsck.fat")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1658 | gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.fat")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1659 | return false; |
| 1660 | } |
| 1661 | if (!UnMount(true)) |
| 1662 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1663 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.fat")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1664 | Find_Actual_Block_Device(); |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 1665 | command = "/sbin/fsck.fat -y " + Actual_Block_Device; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1666 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1667 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1668 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1669 | return true; |
| 1670 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1671 | gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name)); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1672 | return false; |
| 1673 | } |
| 1674 | } |
| 1675 | if (Current_File_System == "ext2" || Current_File_System == "ext3" || Current_File_System == "ext4") { |
| 1676 | if (!TWFunc::Path_Exists("/sbin/e2fsck")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1677 | gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("e2fsck")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1678 | return false; |
| 1679 | } |
| 1680 | if (!UnMount(true)) |
| 1681 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1682 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("e2fsck")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1683 | Find_Actual_Block_Device(); |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame] | 1684 | command = "/sbin/e2fsck -fp " + Actual_Block_Device; |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1685 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1686 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1687 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1688 | return true; |
| 1689 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1690 | gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name)); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1691 | return false; |
| 1692 | } |
| 1693 | } |
| 1694 | if (Current_File_System == "exfat") { |
| 1695 | if (!TWFunc::Path_Exists("/sbin/fsck.exfat")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1696 | gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.exfat")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1697 | return false; |
| 1698 | } |
| 1699 | if (!UnMount(true)) |
| 1700 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1701 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.exfat")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1702 | Find_Actual_Block_Device(); |
| 1703 | command = "/sbin/fsck.exfat " + Actual_Block_Device; |
| 1704 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1705 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1706 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1707 | return true; |
| 1708 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1709 | gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name)); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1710 | return false; |
| 1711 | } |
| 1712 | } |
| 1713 | if (Current_File_System == "f2fs") { |
| 1714 | if (!TWFunc::Path_Exists("/sbin/fsck.f2fs")) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1715 | gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("fsck.f2fs")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1716 | return false; |
| 1717 | } |
| 1718 | if (!UnMount(true)) |
| 1719 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1720 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)("fsck.f2fs")); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1721 | Find_Actual_Block_Device(); |
| 1722 | command = "/sbin/fsck.f2fs " + Actual_Block_Device; |
| 1723 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1724 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1725 | gui_msg("done=Done."); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1726 | return true; |
| 1727 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1728 | gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name)); |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1729 | return false; |
| 1730 | } |
| 1731 | } |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1732 | if (Current_File_System == "ntfs") { |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1733 | string Ntfsfix_Binary; |
| 1734 | if (TWFunc::Path_Exists("/sbin/ntfsfix")) |
| 1735 | Ntfsfix_Binary = "ntfsfix"; |
| 1736 | else if (TWFunc::Path_Exists("/sbin/fsck.ntfs")) |
| 1737 | Ntfsfix_Binary = "fsck.ntfs"; |
| 1738 | else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1739 | gui_msg(Msg(msg::kError, "repair_not_exist={1} does not exist! Cannot repair!")("ntfsfix")); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1740 | return false; |
| 1741 | } |
| 1742 | if (!UnMount(true)) |
| 1743 | return false; |
Greg Wallace | 2d65340 | 2015-12-29 14:19:21 -0500 | [diff] [blame] | 1744 | gui_msg(Msg("repairing_using=Repairing {1} using {2}...")(Display_Name)(Ntfsfix_Binary)); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1745 | Find_Actual_Block_Device(); |
Greg Wallace | b39e6c6 | 2015-12-29 00:55:26 -0500 | [diff] [blame] | 1746 | command = "/sbin/" + Ntfsfix_Binary + " " + Actual_Block_Device; |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1747 | LOGINFO("Repair command: %s\n", command.c_str()); |
| 1748 | if (TWFunc::Exec_Cmd(command) == 0) { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1749 | gui_msg("done=Done."); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1750 | return true; |
| 1751 | } else { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1752 | gui_msg(Msg(msg::kError, "unable_repair=Unable to repair {1}.")(Display_Name)); |
Ethan Yonker | b81d905 | 2015-07-09 13:20:53 -0500 | [diff] [blame] | 1753 | return false; |
| 1754 | } |
| 1755 | } |
Ethan Yonker | 87c7bac | 2014-05-25 21:41:08 -0500 | [diff] [blame] | 1756 | return false; |
| 1757 | } |
| 1758 | |
Ethan Yonker | a271915 | 2015-05-28 09:44:41 -0500 | [diff] [blame
|