Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* Partition class for TWRP |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 15 | * 02110-1301, USA. |
| 16 | * |
| 17 | * The code was written from scratch by Dees_Troy dees_troy at |
| 18 | * yahoo |
| 19 | * |
| 20 | * Copyright (c) 2012 |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/vfs.h> |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame^] | 28 | #include <sys/mount.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | |
| 31 | #include "variables.h" |
| 32 | #include "common.h" |
| 33 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame^] | 34 | #include "data.hpp" |
| 35 | extern "C" { |
| 36 | #include "extra-functions.h" |
| 37 | int __system(const char *command); |
| 38 | FILE * __popen(const char *program, const char *type); |
| 39 | int __pclose(FILE *iop); |
| 40 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | |
| 42 | TWPartition::TWPartition(void) { |
| 43 | Can_Be_Mounted = false; |
| 44 | Can_Be_Wiped = false; |
| 45 | Wipe_During_Factory_Reset = false; |
| 46 | Wipe_Available_in_GUI = false; |
| 47 | Is_SubPartition = false; |
| 48 | SubPartition_Of = ""; |
| 49 | Symlink_Path = ""; |
| 50 | Symlink_Mount_Point = ""; |
| 51 | Mount_Point = ""; |
| 52 | Block_Device = ""; |
| 53 | Alternate_Block_Device = ""; |
| 54 | Removable = false; |
| 55 | Is_Present = false; |
| 56 | Length = 0; |
| 57 | Size = 0; |
| 58 | Used = 0; |
| 59 | Free = 0; |
| 60 | Backup_Size = 0; |
| 61 | Can_Be_Encrypted = false; |
| 62 | Is_Encrypted = false; |
| 63 | Is_Decrypted = false; |
| 64 | Decrypted_Block_Device = ""; |
| 65 | Display_Name = ""; |
| 66 | Backup_Name = ""; |
| 67 | Backup_Method = NONE; |
| 68 | Has_Data_Media = false; |
| 69 | Is_Storage = false; |
| 70 | Storage_Path = ""; |
| 71 | Current_File_System = ""; |
| 72 | Fstab_File_System = ""; |
| 73 | Format_Block_Size = 0; |
| 74 | } |
| 75 | |
| 76 | TWPartition::~TWPartition(void) { |
| 77 | // Do nothing |
| 78 | } |
| 79 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame^] | 80 | bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) { |
| 81 | char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH]; |
| 82 | int line_len = Line.size(), index = 0, item_index = 0; |
| 83 | char* ptr; |
| 84 | |
| 85 | strncpy(full_line, Line.c_str(), line_len); |
| 86 | |
| 87 | while (index < line_len) { |
| 88 | if (full_line[index] <= 32) |
| 89 | full_line[index] = '\0'; |
| 90 | index++; |
| 91 | } |
| 92 | string mount_pt(full_line); |
| 93 | Mount_Point = mount_pt; |
| 94 | index = Mount_Point.size(); |
| 95 | while (index < line_len) { |
| 96 | while (index < line_len && full_line[index] == '\0') |
| 97 | index++; |
| 98 | if (index >= line_len) |
| 99 | continue; |
| 100 | ptr = full_line + index; |
| 101 | if (item_index == 0) { |
| 102 | // File System |
| 103 | Fstab_File_System = ptr; |
| 104 | Current_File_System = ptr; |
| 105 | item_index++; |
| 106 | } else if (item_index == 1) { |
| 107 | // Primary Block Device |
| 108 | if (*ptr != '/') { |
| 109 | if (Display_Error) |
| 110 | LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index); |
| 111 | else |
| 112 | LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index); |
| 113 | return 0; |
| 114 | } |
| 115 | Block_Device = ptr; |
| 116 | Find_Real_Block_Device(Block_Device, Display_Error); |
| 117 | item_index++; |
| 118 | } else if (item_index > 1) { |
| 119 | if (*ptr == '/') { |
| 120 | // Alternate Block Device |
| 121 | Alternate_Block_Device = ptr; |
| 122 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 123 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 124 | // Partition length |
| 125 | ptr += 7; |
| 126 | Length = atoi(ptr); |
| 127 | } else { |
| 128 | // Unhandled data |
| 129 | LOGI("Unhandled fstab information: '%s', %i\n", ptr, index); |
| 130 | } |
| 131 | } |
| 132 | while (index < line_len && full_line[index] != '\0') |
| 133 | index++; |
| 134 | } |
| 135 | |
| 136 | if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) { |
| 137 | if (Display_Error) |
| 138 | LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
| 139 | else |
| 140 | LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
| 141 | return 0; |
| 142 | } else if (Is_File_System(Fstab_File_System)) { |
| 143 | Setup_File_System(Display_Error); |
| 144 | if (Mount_Point == "/system") { |
| 145 | Display_Name = "System"; |
| 146 | Wipe_Available_in_GUI = true; |
| 147 | Update_Size(Display_Error); |
| 148 | int backup_display_size = (int)(Backup_Size / 1048576LLU); |
| 149 | DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size); |
| 150 | } else if (Mount_Point == "/data") { |
| 151 | Display_Name = "Data"; |
| 152 | Wipe_Available_in_GUI = true; |
| 153 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 154 | Has_Data_Media = true; |
| 155 | #endif |
| 156 | #ifdef TW_INCLUDE_CRYPTO |
| 157 | Can_Be_Encrypted = true; |
| 158 | if (!Mount(false)) { |
| 159 | Is_Encrypted = true; |
| 160 | Is_Decrypted = false; |
| 161 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 162 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 163 | DataManager::SetValue("tw_crypto_display", ""); |
| 164 | } else |
| 165 | Update_Size(Display_Error); |
| 166 | #else |
| 167 | Update_Size(Display_Error); |
| 168 | #endif |
| 169 | int backup_display_size = (int)(Backup_Size / 1048576LLU); |
| 170 | DataManager::SetValue(TW_BACKUP_DATA_SIZE, backup_display_size); |
| 171 | } else if (Mount_Point == "/cache") { |
| 172 | Display_Name = "Cache"; |
| 173 | Wipe_Available_in_GUI = true; |
| 174 | Update_Size(Display_Error); |
| 175 | int backup_display_size = (int)(Backup_Size / 1048576LLU); |
| 176 | DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size); |
| 177 | } else if (Mount_Point == "/datadata") { |
| 178 | Display_Name = "DataData"; |
| 179 | Is_SubPartition = true; |
| 180 | SubPartition_Of = "/data"; |
| 181 | Update_Size(Display_Error); |
| 182 | DataManager::SetValue(TW_HAS_DATADATA, 1); |
| 183 | } else if (Mount_Point == "/sd-ext") { |
| 184 | Display_Name = "SD-Ext"; |
| 185 | Wipe_Available_in_GUI = true; |
| 186 | Update_Size(Display_Error); |
| 187 | int backup_display_size = (int)(Backup_Size / 1048576LLU); |
| 188 | DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size); |
| 189 | if (Backup_Size == 0) { |
| 190 | DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0); |
| 191 | DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0); |
| 192 | } else |
| 193 | DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1); |
| 194 | } else |
| 195 | Update_Size(Display_Error); |
| 196 | } else if (Is_Image(Fstab_File_System)) { |
| 197 | Setup_Image(Display_Error); |
| 198 | if (Mount_Point == "/boot") { |
| 199 | int backup_display_size = (int)(Backup_Size / 1048576LLU); |
| 200 | DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size); |
| 201 | if (Backup_Size == 0) { |
| 202 | DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0); |
| 203 | DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0); |
| 204 | } else |
| 205 | DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1); |
| 206 | } else if (Mount_Point == "/recovery") { |
| 207 | int backup_display_size = (int)(Backup_Size / 1048576LLU); |
| 208 | DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size); |
| 209 | if (Backup_Size == 0) { |
| 210 | DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0); |
| 211 | DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0); |
| 212 | } else |
| 213 | DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1); |
| 214 | } |
| 215 | } |
| 216 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 217 | return 1; |
| 218 | } |
| 219 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame^] | 220 | bool TWPartition::Is_File_System(string File_System) { |
| 221 | if (File_System == "ext2" || |
| 222 | File_System == "ext3" || |
| 223 | File_System == "ext4" || |
| 224 | File_System == "vfat" || |
| 225 | File_System == "ntfs" || |
| 226 | File_System == "yaffs2" || |
| 227 | File_System == "auto") |
| 228 | return true; |
| 229 | else |
| 230 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame^] | 233 | bool TWPartition::Is_Image(string File_System) { |
| 234 | if (File_System == "emmc" || |
| 235 | File_System == "mtd") |
| 236 | return true; |
| 237 | else |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | void TWPartition::Setup_File_System(bool Display_Error) { |
| 242 | struct statfs st; |
| 243 | |
| 244 | Can_Be_Mounted = true; |
| 245 | Can_Be_Wiped = true; |
| 246 | |
| 247 | // Check to see if the block device exists |
| 248 | if (Path_Exists(Block_Device)) { |
| 249 | Is_Present = true; |
| 250 | } else if (Alternate_Block_Device != "" && Path_Exists(Alternate_Block_Device)) { |
| 251 | Flip_Block_Device(); |
| 252 | Is_Present = true; |
| 253 | } |
| 254 | // Make the mount point folder if it doesn't exist |
| 255 | if (!Path_Exists(Mount_Point.c_str())) { |
| 256 | if (mkdir(Mount_Point.c_str(), 0777) == -1) { |
| 257 | if (Display_Error) |
| 258 | LOGE("Can not create '%s' folder.\n", Mount_Point.c_str()); |
| 259 | else |
| 260 | LOGI("Can not create '%s' folder.\n", Mount_Point.c_str()); |
| 261 | } else |
| 262 | LOGI("Created '%s' folder.\n", Mount_Point.c_str()); |
| 263 | } |
| 264 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 265 | Backup_Name = Display_Name; |
| 266 | Backup_Method = FILES; |
| 267 | } |
| 268 | |
| 269 | void TWPartition::Setup_Image(bool Display_Error) { |
| 270 | if (Path_Exists(Block_Device)) { |
| 271 | Is_Present = true; |
| 272 | } else if (Alternate_Block_Device != "" && Path_Exists(Alternate_Block_Device)) { |
| 273 | Flip_Block_Device(); |
| 274 | Is_Present = true; |
| 275 | } |
| 276 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 277 | Backup_Name = Display_Name; |
| 278 | if (Fstab_File_System == "emmc") |
| 279 | Backup_Method = DD; |
| 280 | else if (Fstab_File_System == "mtd") |
| 281 | Backup_Method = FLASH_UTILS; |
| 282 | else |
| 283 | LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str()); |
| 284 | if (Find_Partition_Size()) { |
| 285 | Used = Size; |
| 286 | Backup_Size = Size; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 287 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame^] | 288 | if (Display_Error) |
| 289 | LOGE("Unable to find parition size for '%s'\n", Block_Device.c_str()); |
| 290 | else |
| 291 | LOGI("Unable to find parition size for '%s'\n", Block_Device.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame^] | 295 | void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { |
| 296 | char device[512], realDevice[512]; |
| 297 | |
| 298 | strcpy(device, Block.c_str()); |
| 299 | memset(realDevice, 0, sizeof(realDevice)); |
| 300 | while (readlink(device, realDevice, sizeof(realDevice)) > 0) |
| 301 | { |
| 302 | strcpy(device, realDevice); |
| 303 | memset(realDevice, 0, sizeof(realDevice)); |
| 304 | } |
| 305 | |
| 306 | if (device[0] != '/') { |
| 307 | if (Display_Error) |
| 308 | LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str()); |
| 309 | else |
| 310 | LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str()); |
| 311 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 312 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame^] | 313 | Block = device; |
| 314 | return; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | bool TWPartition::Get_Size_Via_df(string Path, bool Display_Error) { |
| 319 | FILE* fp; |
| 320 | char command[255], line[512]; |
| 321 | int include_block = 1; |
| 322 | unsigned int min_len; |
| 323 | |
| 324 | if (!Mount(Display_Error)) |
| 325 | return false; |
| 326 | |
| 327 | min_len = Block_Device.size() + 2; |
| 328 | sprintf(command, "df %s", Path.c_str()); |
| 329 | fp = __popen(command, "r"); |
| 330 | if (fp == NULL) |
| 331 | return false; |
| 332 | |
| 333 | while (fgets(line, sizeof(line), fp) != NULL) |
| 334 | { |
| 335 | unsigned long blocks, used, available; |
| 336 | char device[64]; |
| 337 | char tmpString[64]; |
| 338 | |
| 339 | if (strncmp(line, "Filesystem", 10) == 0) |
| 340 | continue; |
| 341 | if (strlen(line) < min_len) { |
| 342 | include_block = 0; |
| 343 | continue; |
| 344 | } |
| 345 | if (include_block) { |
| 346 | sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available); |
| 347 | } else { |
| 348 | // The device block string is so long that the df information is on the next line |
| 349 | int space_count = 0; |
| 350 | while (tmpString[space_count] == 32) |
| 351 | space_count++; |
| 352 | sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available); |
| 353 | } |
| 354 | |
| 355 | // Adjust block size to byte size |
| 356 | Size = blocks * 1024ULL; |
| 357 | Used = used * 1024ULL; |
| 358 | Free = available * 1024ULL; |
| 359 | Backup_Size = Used; |
| 360 | } |
| 361 | fclose(fp); |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | unsigned long long TWPartition::Get_Size_Via_du(string Path, bool Display_Error) { |
| 366 | char cmd[512]; |
| 367 | sprintf(cmd, "du -sk %s | awk '{ print $1 }'", Path.c_str()); |
| 368 | |
| 369 | FILE *fp; |
| 370 | fp = __popen(cmd, "r"); |
| 371 | |
| 372 | char str[512]; |
| 373 | fgets(str, sizeof(str), fp); |
| 374 | __pclose(fp); |
| 375 | |
| 376 | unsigned long long dusize = atol(str); |
| 377 | dusize *= 1024ULL; |
| 378 | |
| 379 | return dusize; |
| 380 | } |
| 381 | |
| 382 | bool TWPartition::Find_Partition_Size(void) { |
| 383 | FILE* fp; |
| 384 | char line[512]; |
| 385 | string tmpdevice; |
| 386 | |
| 387 | // In this case, we'll first get the partitions we care about (with labels) |
| 388 | fp = fopen("/proc/partitions", "rt"); |
| 389 | if (fp == NULL) |
| 390 | return false; |
| 391 | |
| 392 | while (fgets(line, sizeof(line), fp) != NULL) |
| 393 | { |
| 394 | unsigned long major, minor, blocks; |
| 395 | char device[512]; |
| 396 | char tmpString[64]; |
| 397 | |
| 398 | if (strlen(line) < 7 || line[0] == 'm') continue; |
| 399 | sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device); |
| 400 | |
| 401 | tmpdevice = "/dev/block/"; |
| 402 | tmpdevice += device; |
| 403 | if (tmpdevice == Block_Device || tmpdevice == Alternate_Block_Device) { |
| 404 | // Adjust block size to byte size |
| 405 | Size = blocks * 1024ULL; |
| 406 | fclose(fp); |
| 407 | return true; |
| 408 | } |
| 409 | } |
| 410 | fclose(fp); |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | bool TWPartition::Path_Exists(string Path) { |
| 415 | // Check to see if the Path exists |
| 416 | struct statfs st; |
| 417 | |
| 418 | if (statfs(Path.c_str(), &st) != 0) |
| 419 | return false; |
| 420 | else |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | void TWPartition::Flip_Block_Device(void) { |
| 425 | string temp; |
| 426 | |
| 427 | temp = Alternate_Block_Device; |
| 428 | Block_Device = Alternate_Block_Device; |
| 429 | Alternate_Block_Device = temp; |
| 430 | } |
| 431 | |
| 432 | bool TWPartition::Is_Mounted(void) { |
| 433 | if (!Can_Be_Mounted) |
| 434 | return false; |
| 435 | |
| 436 | struct stat st1, st2; |
| 437 | string test_path; |
| 438 | |
| 439 | // Check to see if the mount point directory exists |
| 440 | test_path = Mount_Point + "/."; |
| 441 | if (stat(test_path.c_str(), &st1) != 0) return false; |
| 442 | |
| 443 | // Check to see if the directory above the mount point exists |
| 444 | test_path = Mount_Point + "/../."; |
| 445 | if (stat(test_path.c_str(), &st2) != 0) return false; |
| 446 | |
| 447 | // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device |
| 448 | int ret = (st1.st_dev != st2.st_dev) ? true : false; |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | bool TWPartition::Mount(bool Display_Error) { |
| 454 | if (Is_Mounted()) { |
| 455 | return true; |
| 456 | } else if (!Can_Be_Mounted) { |
| 457 | return false; |
| 458 | } |
| 459 | if (Is_Decrypted) { |
| 460 | if (mount(Decrypted_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) { |
| 461 | Check_FS_Type(); |
| 462 | if (mount(Decrypted_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) { |
| 463 | if (Display_Error) |
| 464 | LOGE("Unable to mount decrypted block device '%s' to '%s'\n", Decrypted_Block_Device.c_str(), Mount_Point.c_str()); |
| 465 | else |
| 466 | LOGI("Unable to mount decrypted block device '%s' to '%s'\n", Decrypted_Block_Device.c_str(), Mount_Point.c_str()); |
| 467 | return false; |
| 468 | } else |
| 469 | return true; |
| 470 | } else |
| 471 | return true; |
| 472 | } |
| 473 | if (mount(Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) { |
| 474 | Check_FS_Type(); |
| 475 | if (mount(Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) { |
| 476 | if (Alternate_Block_Device != "" && Path_Exists(Alternate_Block_Device)) { |
| 477 | Flip_Block_Device(); |
| 478 | Check_FS_Type(); |
| 479 | if (mount(Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) { |
| 480 | if (Display_Error) |
| 481 | LOGE("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 482 | else |
| 483 | LOGI("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 484 | return false; |
| 485 | } else |
| 486 | return true; |
| 487 | } else |
| 488 | return false; |
| 489 | } else |
| 490 | return true; |
| 491 | } |
| 492 | return true; |
| 493 | } |
| 494 | |
| 495 | bool TWPartition::UnMount(bool Display_Error) { |
| 496 | if (Is_Mounted()) { |
| 497 | int never_unmount_system; |
| 498 | |
| 499 | DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system); |
| 500 | if (never_unmount_system == 1 && Mount_Point == "/system") |
| 501 | return true; // Never unmount system if you're not supposed to unmount it |
| 502 | |
| 503 | if (umount(Mount_Point.c_str()) != 0) { |
| 504 | if (Display_Error) |
| 505 | LOGE("Unable to unmount '%s'\n", Mount_Point.c_str()); |
| 506 | else |
| 507 | LOGI("Unable to unmount '%s'\n", Mount_Point.c_str()); |
| 508 | return false; |
| 509 | } else |
| 510 | return true; |
| 511 | } else { |
| 512 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | bool TWPartition::Wipe() { |
| 517 | LOGI("STUB TWPartition::Wipe\n"); |
| 518 | return 1; |
| 519 | } |
| 520 | |
| 521 | bool TWPartition::Backup(string backup_folder) { |
| 522 | LOGI("STUB TWPartition::Backup, backup_folder: '%s'\n", backup_folder.c_str()); |
| 523 | return 1; |
| 524 | } |
| 525 | |
| 526 | bool TWPartition::Restore(string restore_folder) { |
| 527 | LOGI("STUB TWPartition::Restore, restore_folder: '%s'\n", restore_folder.c_str()); |
| 528 | return 1; |
| 529 | } |
| 530 | |
| 531 | string TWPartition::Backup_Method_By_Name() { |
| 532 | LOGI("STUB TWPartition::Backup_Method_By_Name\n"); |
| 533 | return "STUB"; |
| 534 | } |
| 535 | |
| 536 | bool TWPartition::Decrypt(string Password) { |
| 537 | LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str()); |
| 538 | return 1; |
| 539 | } |
| 540 | |
| 541 | bool TWPartition::Wipe_Encryption() { |
| 542 | LOGI("STUB TWPartition::Wipe_Encryption\n"); |
| 543 | return 1; |
| 544 | } |
| 545 | |
| 546 | void TWPartition::Check_FS_Type() { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame^] | 547 | FILE *fp; |
| 548 | string blkCommand; |
| 549 | char blkOutput[255]; |
| 550 | char* blk; |
| 551 | char* arg; |
| 552 | char* ptr; |
| 553 | |
| 554 | if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd") |
| 555 | return; // Running blkid on some mtd devices causes a massive crash |
| 556 | |
| 557 | if (Is_Decrypted) |
| 558 | blkCommand = "blkid " + Decrypted_Block_Device; |
| 559 | else |
| 560 | blkCommand = "blkid " + Block_Device; |
| 561 | fp = __popen(blkCommand.c_str(), "r"); |
| 562 | while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL) |
| 563 | { |
| 564 | blk = blkOutput; |
| 565 | ptr = blkOutput; |
| 566 | while (*ptr > 32 && *ptr != ':') ptr++; |
| 567 | if (*ptr == 0) continue; |
| 568 | *ptr = 0; |
| 569 | |
| 570 | // Increment by two, but verify that we don't hit a NULL |
| 571 | ptr++; |
| 572 | if (*ptr != 0) ptr++; |
| 573 | |
| 574 | // Now, find the TYPE field |
| 575 | while (1) |
| 576 | { |
| 577 | arg = ptr; |
| 578 | while (*ptr > 32) ptr++; |
| 579 | if (*ptr != 0) |
| 580 | { |
| 581 | *ptr = 0; |
| 582 | ptr++; |
| 583 | } |
| 584 | |
| 585 | if (strlen(arg) > 6) |
| 586 | { |
| 587 | if (memcmp(arg, "TYPE=\"", 6) == 0) break; |
| 588 | } |
| 589 | |
| 590 | if (*ptr == 0) |
| 591 | { |
| 592 | arg = NULL; |
| 593 | break; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | if (arg && strlen(arg) > 7) |
| 598 | { |
| 599 | arg += 6; // Skip the TYPE=" portion |
| 600 | arg[strlen(arg)-1] = '\0'; // Drop the tail quote |
| 601 | } |
| 602 | else |
| 603 | continue; |
| 604 | |
| 605 | if (strcmp(Current_File_System.c_str(), arg) != 0) { |
| 606 | LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg); |
| 607 | Current_File_System = arg; |
| 608 | } |
| 609 | } |
| 610 | __pclose(fp); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 611 | return; |
| 612 | } |
| 613 | |
| 614 | bool TWPartition::Wipe_EXT23() { |
| 615 | LOGI("STUB TWPartition::Wipe_EXT23\n"); |
| 616 | return 1; |
| 617 | } |
| 618 | |
| 619 | bool TWPartition::Wipe_EXT4() { |
| 620 | LOGI("STUB TWPartition::Wipe_EXT4\n"); |
| 621 | return 1; |
| 622 | } |
| 623 | |
| 624 | bool TWPartition::Wipe_FAT() { |
| 625 | LOGI("STUB TWPartition::Wipe_FAT\n"); |
| 626 | return 1; |
| 627 | } |
| 628 | |
| 629 | bool TWPartition::Wipe_YAFFS2() { |
| 630 | LOGI("STUB TWPartition::Wipe_YAFFS2\n"); |
| 631 | return 1; |
| 632 | } |
| 633 | |
| 634 | bool TWPartition::Wipe_RMRF() { |
| 635 | LOGI("STUB TWPartition::Wipe_RMRF\n"); |
| 636 | return 1; |
| 637 | } |
| 638 | |
| 639 | bool TWPartition::Wipe_Data_Without_Wiping_Media() { |
| 640 | LOGI("STUB TWPartition::Wipe_Data_Without_Wiping_Media\n"); |
| 641 | return 1; |
| 642 | } |
| 643 | |
| 644 | bool TWPartition::Backup_Tar(string backup_folder) { |
| 645 | LOGI("STUB TWPartition::Backup_Tar, backup_folder: '%s'\n", backup_folder.c_str()); |
| 646 | return 1; |
| 647 | } |
| 648 | |
| 649 | bool TWPartition::Backup_DD(string backup_folder) { |
| 650 | LOGI("STUB TWPartition::Backup_DD, backup_folder: '%s'\n", backup_folder.c_str()); |
| 651 | return 1; |
| 652 | } |
| 653 | |
| 654 | bool TWPartition::Backup_Dump_Image(string backup_folder) { |
| 655 | LOGI("STUB TWPartition::Backup_Dump_Image, backup_folder: '%s'\n", backup_folder.c_str()); |
| 656 | return 1; |
| 657 | } |
| 658 | |
| 659 | bool TWPartition::Restore_Tar(string restore_folder) { |
| 660 | LOGI("STUB TWPartition::Restore_Tar, backup_folder: '%s'\n", restore_folder.c_str()); |
| 661 | return 1; |
| 662 | } |
| 663 | |
| 664 | bool TWPartition::Restore_DD(string restore_folder) { |
| 665 | LOGI("STUB TWPartition::Restore_DD, backup_folder: '%s'\n", restore_folder.c_str()); |
| 666 | return 1; |
| 667 | } |
| 668 | |
| 669 | bool TWPartition::Restore_Flash_Image(string restore_folder) { |
| 670 | LOGI("STUB TWPartition::Restore_Flash_Image, backup_folder: '%s'\n", restore_folder.c_str()); |
| 671 | return 1; |
| 672 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame^] | 673 | |
| 674 | bool TWPartition::Update_Size(bool Display_Error) { |
| 675 | if (!Can_Be_Mounted) |
| 676 | return false; |
| 677 | |
| 678 | if (!Get_Size_Via_df(Mount_Point, Display_Error)) |
| 679 | return false; |
| 680 | if (Has_Data_Media) { |
| 681 | if (Mount(Display_Error)) { |
| 682 | unsigned long long data_used, data_media_used, actual_data; |
| 683 | data_used = Get_Size_Via_du("/data/", Display_Error); |
| 684 | data_media_used = Get_Size_Via_du("/data/media/", Display_Error); |
| 685 | actual_data = data_used - data_media_used; |
| 686 | Backup_Size = actual_data; |
| 687 | } else |
| 688 | return false; |
| 689 | } |
| 690 | return true; |
| 691 | } |