Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 1 | /* OpenRecoveryScript 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> |
| 28 | #include <unistd.h> |
| 29 | #include <vector> |
| 30 | #include <dirent.h> |
| 31 | #include <time.h> |
| 32 | #include <errno.h> |
| 33 | |
| 34 | #include "twrp-functions.hpp" |
| 35 | #include "partitions.hpp" |
| 36 | #include "common.h" |
| 37 | #include "openrecoveryscript.hpp" |
| 38 | #include "variables.h" |
| 39 | extern "C" { |
| 40 | #include "data.h" |
| 41 | #include "twinstall.h" |
| 42 | int TWinstall_zip(const char* path, int* wipe_cache); |
| 43 | } |
| 44 | |
| 45 | static const char *SCRIPT_FILE_CACHE = "/cache/recovery/openrecoveryscript"; |
| 46 | static const char *SCRIPT_FILE_TMP = "/tmp/openrecoveryscript"; |
| 47 | #define SCRIPT_COMMAND_SIZE 512 |
| 48 | |
| 49 | int OpenRecoveryScript::check_for_script_file(void) { |
| 50 | char exec[512]; |
| 51 | |
| 52 | if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) { |
| 53 | LOGE("Unable to mount /cache for OpenRecoveryScript support.\n"); |
| 54 | return 0; |
| 55 | } |
| 56 | if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) { |
| 57 | LOGI("Script file found: '%s'\n", SCRIPT_FILE_CACHE); |
| 58 | // Copy script file to /tmp |
| 59 | strcpy(exec, "cp "); |
| 60 | strcat(exec, SCRIPT_FILE_CACHE); |
| 61 | strcat(exec, " "); |
| 62 | strcat(exec, SCRIPT_FILE_TMP); |
| 63 | system(exec); |
| 64 | // Delete the file from /cache |
| 65 | strcpy(exec, "rm "); |
| 66 | strcat(exec, SCRIPT_FILE_CACHE); |
| 67 | system(exec); |
| 68 | return 1; |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int OpenRecoveryScript::run_script_file(void) { |
| 74 | FILE *fp = fopen(SCRIPT_FILE_TMP, "r"); |
| 75 | int ret_val = 0, cindex, line_len, i, remove_nl; |
| 76 | char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE], |
| 77 | value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE], |
| 78 | value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE]; |
| 79 | char *val_start, *tok; |
| 80 | |
| 81 | if (fp != NULL) { |
Dees_Troy | 3ac71a5 | 2012-10-01 19:46:11 -0400 | [diff] [blame] | 82 | DataManager_SetIntValue(TW_SIMULATE_ACTIONS, 0); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 83 | while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) { |
| 84 | cindex = 0; |
| 85 | line_len = strlen(script_line); |
| 86 | if (line_len < 2) |
| 87 | continue; // there's a blank line or line is too short to contain a command |
| 88 | //ui_print("script line: '%s'\n", script_line); |
| 89 | for (i=0; i<line_len; i++) { |
| 90 | if ((int)script_line[i] == 32) { |
| 91 | cindex = i; |
| 92 | i = line_len; |
| 93 | } |
| 94 | } |
| 95 | memset(command, 0, sizeof(command)); |
| 96 | memset(value, 0, sizeof(value)); |
| 97 | if ((int)script_line[line_len - 1] == 10) |
| 98 | remove_nl = 2; |
| 99 | else |
| 100 | remove_nl = 1; |
| 101 | if (cindex != 0) { |
| 102 | strncpy(command, script_line, cindex); |
| 103 | LOGI("command is: '%s' and ", command); |
| 104 | val_start = script_line; |
| 105 | val_start += cindex + 1; |
| 106 | strncpy(value, val_start, line_len - cindex - remove_nl); |
| 107 | LOGI("value is: '%s'\n", value); |
| 108 | } else { |
| 109 | strncpy(command, script_line, line_len - remove_nl + 1); |
| 110 | ui_print("command is: '%s' and there is no value\n", command); |
| 111 | } |
| 112 | if (strcmp(command, "install") == 0) { |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 113 | // Install Zip |
| 114 | ret_val = Install_Command(value); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 115 | } else if (strcmp(command, "wipe") == 0) { |
| 116 | // Wipe |
| 117 | if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) { |
| 118 | ui_print("-- Wiping Cache Partition...\n"); |
| 119 | PartitionManager.Wipe_By_Path("/cache"); |
| 120 | ui_print("-- Cache Partition Wipe Complete!\n"); |
| 121 | } else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) { |
| 122 | ui_print("-- Wiping Dalvik Cache...\n"); |
| 123 | PartitionManager.Wipe_Dalvik_Cache(); |
| 124 | ui_print("-- Dalvik Cache Wipe Complete!\n"); |
| 125 | } else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) { |
| 126 | ui_print("-- Wiping Data Partition...\n"); |
| 127 | PartitionManager.Factory_Reset(); |
| 128 | ui_print("-- Data Partition Wipe Complete!\n"); |
| 129 | } else { |
| 130 | LOGE("Error with wipe command value: '%s'\n", value); |
| 131 | ret_val = 1; |
| 132 | } |
| 133 | } else if (strcmp(command, "backup") == 0) { |
| 134 | // Backup |
| 135 | tok = strtok(value, " "); |
| 136 | strcpy(value1, tok); |
| 137 | tok = strtok(NULL, " "); |
| 138 | if (tok != NULL) { |
| 139 | memset(value2, 0, sizeof(value2)); |
| 140 | strcpy(value2, tok); |
| 141 | line_len = strlen(tok); |
| 142 | if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) { |
| 143 | if ((int)value2[line_len - 1] == 10 || (int)value2[line_len - 1] == 13) |
| 144 | remove_nl = 2; |
| 145 | else |
| 146 | remove_nl = 1; |
| 147 | } else |
| 148 | remove_nl = 0; |
| 149 | strncpy(value2, tok, line_len - remove_nl); |
| 150 | DataManager_SetStrValue(TW_BACKUP_NAME, value2); |
| 151 | ui_print("Backup folder set to '%s'\n", value2); |
Dees_Troy | c9ff7a3 | 2012-09-27 10:09:41 -0400 | [diff] [blame] | 152 | if (PartitionManager.Check_Backup_Name(true) != 0) { |
| 153 | ret_val = 1; |
| 154 | continue; |
| 155 | } |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 156 | } else { |
| 157 | char empt[50]; |
| 158 | strcpy(empt, "(Current Date)"); |
| 159 | DataManager_SetStrValue(TW_BACKUP_NAME, empt); |
| 160 | } |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 161 | ret_val = Backup_Command(value1); |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 162 | } else if (strcmp(command, "restore") == 0) { |
| 163 | // Restore |
| 164 | PartitionManager.Mount_All_Storage(); |
| 165 | DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 0); |
| 166 | |
| 167 | string val = value, restore_folder, restore_partitions; |
| 168 | size_t pos = val.find_last_of(" "); |
| 169 | if (pos == string::npos) { |
| 170 | ui_print("Malformed restore parameter: '%s'\n", value1); |
| 171 | ret_val = 1; |
| 172 | continue; |
| 173 | } |
| 174 | restore_folder = val.substr(0, pos); |
| 175 | char folder_path[512], partitions[512]; |
| 176 | strcpy(folder_path, restore_folder.c_str()); |
| 177 | restore_partitions = val.substr(pos + 1, val.size() - pos - 1); |
| 178 | strcpy(partitions, restore_partitions.c_str()); |
| 179 | LOGI("Restore folder is: '%s' and partitions: '%s'\n", folder_path, partitions); |
| 180 | ui_print("Restoring '%s'\n", folder_path); |
| 181 | |
| 182 | if (folder_path[0] != '/') { |
| 183 | char backup_folder[512]; |
| 184 | sprintf(backup_folder, "%s/%s", DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR), folder_path); |
| 185 | LOGI("Restoring relative path: '%s'\n", backup_folder); |
| 186 | if (!TWFunc::Path_Exists(backup_folder)) { |
| 187 | if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) { |
| 188 | if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE)) { |
| 189 | LOGI("Backup folder '%s' not found on external storage, trying internal...\n", folder_path); |
| 190 | DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 191 | } else { |
| 192 | LOGI("Backup folder '%s' not found on internal storage, trying external...\n", folder_path); |
| 193 | DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 194 | } |
| 195 | sprintf(backup_folder, "%s/%s", DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR), folder_path); |
| 196 | LOGI("2Restoring relative path: '%s'\n", backup_folder); |
| 197 | } |
| 198 | } |
| 199 | strcpy(folder_path, backup_folder); |
| 200 | } else { |
| 201 | if (folder_path[strlen(folder_path) - 1] == '/') |
| 202 | strcat(folder_path, "."); |
| 203 | else |
| 204 | strcat(folder_path, "/."); |
| 205 | } |
| 206 | if (!TWFunc::Path_Exists(folder_path)) { |
| 207 | ui_print("Unable to locate backup '%s'\n", folder_path); |
| 208 | ret_val = 1; |
| 209 | continue; |
| 210 | } |
| 211 | DataManager_SetStrValue("tw_restore", folder_path); |
| 212 | |
| 213 | PartitionManager.Set_Restore_Files(folder_path); |
| 214 | if (strlen(partitions) != 0) { |
| 215 | int tw_restore_system = 0; |
| 216 | int tw_restore_data = 0; |
| 217 | int tw_restore_cache = 0; |
| 218 | int tw_restore_recovery = 0; |
| 219 | int tw_restore_boot = 0; |
| 220 | int tw_restore_andsec = 0; |
| 221 | int tw_restore_sdext = 0; |
| 222 | int tw_restore_sp1 = 0; |
| 223 | int tw_restore_sp2 = 0; |
| 224 | int tw_restore_sp3 = 0; |
| 225 | |
| 226 | memset(value2, 0, sizeof(value2)); |
| 227 | strcpy(value2, partitions); |
| 228 | ui_print("Setting restore options: '%s':\n", value2); |
| 229 | line_len = strlen(value2); |
| 230 | for (i=0; i<line_len; i++) { |
| 231 | if ((value2[i] == 'S' || value2[i] == 's') && DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) > 0) { |
| 232 | tw_restore_system = 1; |
| 233 | ui_print("System\n"); |
| 234 | } else if ((value2[i] == 'D' || value2[i] == 'd') && DataManager_GetIntValue(TW_RESTORE_DATA_VAR) > 0) { |
| 235 | tw_restore_data = 1; |
| 236 | ui_print("Data\n"); |
| 237 | } else if ((value2[i] == 'C' || value2[i] == 'c') && DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) > 0) { |
| 238 | tw_restore_cache = 1; |
| 239 | ui_print("Cache\n"); |
| 240 | } else if ((value2[i] == 'R' || value2[i] == 'r') && DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) > 0) { |
| 241 | tw_restore_recovery = 1; |
| 242 | ui_print("Recovery\n"); |
| 243 | } else if (value2[i] == '1' && DataManager_GetIntValue(TW_RESTORE_SP1_VAR) > 0) { |
| 244 | tw_restore_sp1 = 1; |
| 245 | ui_print("%s\n", "Special1"); |
| 246 | } else if (value2[i] == '2' && DataManager_GetIntValue(TW_RESTORE_SP2_VAR) > 0) { |
| 247 | tw_restore_sp2 = 1; |
| 248 | ui_print("%s\n", "Special2"); |
| 249 | } else if (value2[i] == '3' && DataManager_GetIntValue(TW_RESTORE_SP3_VAR) > 0) { |
| 250 | tw_restore_sp3 = 1; |
| 251 | ui_print("%s\n", "Special3"); |
| 252 | } else if ((value2[i] == 'B' || value2[i] == 'b') && DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) > 0) { |
| 253 | tw_restore_boot = 1; |
| 254 | ui_print("Boot\n"); |
| 255 | } else if ((value2[i] == 'A' || value2[i] == 'a') && DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) > 0) { |
| 256 | tw_restore_andsec = 1; |
| 257 | ui_print("Android Secure\n"); |
| 258 | } else if ((value2[i] == 'E' || value2[i] == 'e') && DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) > 0) { |
| 259 | tw_restore_sdext = 1; |
| 260 | ui_print("SD-Ext\n"); |
| 261 | } else if (value2[i] == 'M' || value2[i] == 'm') { |
| 262 | DataManager_SetIntValue(TW_SKIP_MD5_CHECK_VAR, 1); |
| 263 | ui_print("MD5 check skip is on\n"); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (DataManager_GetIntValue(TW_RESTORE_SYSTEM_VAR) && !tw_restore_system) |
| 268 | DataManager_SetIntValue(TW_RESTORE_SYSTEM_VAR, 0); |
| 269 | if (DataManager_GetIntValue(TW_RESTORE_DATA_VAR) && !tw_restore_data) |
| 270 | DataManager_SetIntValue(TW_RESTORE_DATA_VAR, 0); |
| 271 | if (DataManager_GetIntValue(TW_RESTORE_CACHE_VAR) && !tw_restore_cache) |
| 272 | DataManager_SetIntValue(TW_RESTORE_CACHE_VAR, 0); |
| 273 | if (DataManager_GetIntValue(TW_RESTORE_RECOVERY_VAR) && !tw_restore_recovery) |
| 274 | DataManager_SetIntValue(TW_RESTORE_RECOVERY_VAR, 0); |
| 275 | if (DataManager_GetIntValue(TW_RESTORE_BOOT_VAR) && !tw_restore_boot) |
| 276 | DataManager_SetIntValue(TW_RESTORE_BOOT_VAR, 0); |
| 277 | if (DataManager_GetIntValue(TW_RESTORE_ANDSEC_VAR) && !tw_restore_andsec) |
| 278 | DataManager_SetIntValue(TW_RESTORE_ANDSEC_VAR, 0); |
| 279 | if (DataManager_GetIntValue(TW_RESTORE_SDEXT_VAR) && !tw_restore_sdext) |
| 280 | DataManager_SetIntValue(TW_RESTORE_SDEXT_VAR, 0); |
| 281 | if (DataManager_GetIntValue(TW_RESTORE_SP1_VAR) && !tw_restore_sp1) |
| 282 | DataManager_SetIntValue(TW_RESTORE_SP1_VAR, 0); |
| 283 | if (DataManager_GetIntValue(TW_RESTORE_SP2_VAR) && !tw_restore_sp2) |
| 284 | DataManager_SetIntValue(TW_RESTORE_SP2_VAR, 0); |
| 285 | if (DataManager_GetIntValue(TW_RESTORE_SP3_VAR) && !tw_restore_sp3) |
| 286 | DataManager_SetIntValue(TW_RESTORE_SP3_VAR, 0); |
| 287 | } else { |
| 288 | ui_print("No restore options set.\n"); |
| 289 | ret_val = 1; |
| 290 | continue; |
| 291 | } |
| 292 | PartitionManager.Run_Restore(folder_path); |
| 293 | ui_print("Restore complete!\n"); |
| 294 | } else if (strcmp(command, "mount") == 0) { |
| 295 | // Mount |
| 296 | if (value[0] != '/') { |
| 297 | strcpy(mount, "/"); |
| 298 | strcat(mount, value); |
| 299 | } else |
| 300 | strcpy(mount, value); |
| 301 | if (PartitionManager.Mount_By_Path(mount, true)) |
| 302 | ui_print("Mounted '%s'\n", mount); |
| 303 | } else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) { |
| 304 | // Unmount |
| 305 | if (value[0] != '/') { |
| 306 | strcpy(mount, "/"); |
| 307 | strcat(mount, value); |
| 308 | } else |
| 309 | strcpy(mount, value); |
| 310 | if (PartitionManager.UnMount_By_Path(mount, true)) |
| 311 | ui_print("Unmounted '%s'\n", mount); |
| 312 | } else if (strcmp(command, "set") == 0) { |
| 313 | // Set value |
| 314 | tok = strtok(value, " "); |
| 315 | strcpy(value1, tok); |
| 316 | tok = strtok(NULL, " "); |
| 317 | strcpy(value2, tok); |
| 318 | ui_print("Setting '%s' to '%s'\n", value1, value2); |
| 319 | DataManager_SetStrValue(value1, value2); |
| 320 | } else if (strcmp(command, "mkdir") == 0) { |
| 321 | // Make directory (recursive) |
| 322 | ui_print("Making directory (recursive): '%s'\n", value); |
| 323 | if (TWFunc::Recursive_Mkdir(value)) { |
| 324 | LOGE("Unable to create folder: '%s'\n", value); |
| 325 | ret_val = 1; |
| 326 | } |
| 327 | } else if (strcmp(command, "reboot") == 0) { |
| 328 | // Reboot |
| 329 | } else if (strcmp(command, "cmd") == 0) { |
| 330 | if (cindex != 0) { |
| 331 | system(value); |
| 332 | } else { |
| 333 | LOGE("No value given for cmd\n"); |
| 334 | } |
| 335 | } else if (strcmp(command, "print") == 0) { |
| 336 | ui_print("%s\n", value); |
| 337 | } else { |
| 338 | LOGE("Unrecognized script command: '%s'\n", command); |
| 339 | ret_val = 1; |
| 340 | } |
| 341 | } |
| 342 | fclose(fp); |
| 343 | ui_print("Done processing script file\n"); |
| 344 | } else { |
| 345 | LOGE("Error opening script file '%s'\n", SCRIPT_FILE_TMP); |
| 346 | return 1; |
| 347 | } |
| 348 | return ret_val; |
| 349 | } |
| 350 | |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 351 | int OpenRecoveryScript::Install_Command(string Zip) { |
| 352 | // Install zip |
| 353 | string ret_string; |
| 354 | int ret_val = 0, wipe_cache = 0; |
| 355 | |
| 356 | PartitionManager.Mount_All_Storage(); |
| 357 | if (Zip.substr(0, 1) != "/") { |
| 358 | // Relative path given |
| 359 | string Full_Path; |
| 360 | |
| 361 | Full_Path = DataManager_GetCurrentStoragePath(); |
| 362 | Full_Path += "/" + Zip; |
| 363 | LOGI("Full zip path: '%s'\n", Full_Path.c_str()); |
| 364 | if (!TWFunc::Path_Exists(Full_Path)) { |
| 365 | ret_string = Locate_Zip_File(Full_Path, DataManager_GetCurrentStoragePath()); |
| 366 | if (!ret_string.empty()) { |
| 367 | Full_Path = ret_string; |
| 368 | } else if (DataManager_GetIntValue(TW_HAS_DUAL_STORAGE)) { |
| 369 | if (DataManager_GetIntValue(TW_USE_EXTERNAL_STORAGE)) { |
| 370 | LOGI("Zip file not found on external storage, trying internal...\n"); |
| 371 | DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 0); |
| 372 | } else { |
| 373 | LOGI("Zip file not found on internal storage, trying external...\n"); |
| 374 | DataManager_SetIntValue(TW_USE_EXTERNAL_STORAGE, 1); |
| 375 | } |
| 376 | Full_Path = DataManager_GetCurrentStoragePath(); |
| 377 | Full_Path += "/" + Zip; |
| 378 | LOGI("Full zip path: '%s'\n", Full_Path.c_str()); |
| 379 | ret_string = Locate_Zip_File(Full_Path, DataManager_GetCurrentStoragePath()); |
| 380 | if (!ret_string.empty()) |
| 381 | Full_Path = ret_string; |
| 382 | } |
| 383 | } |
| 384 | Zip = Full_Path; |
| 385 | } else { |
| 386 | // Full path given |
| 387 | if (!TWFunc::Path_Exists(Zip)) { |
| 388 | ret_string = Locate_Zip_File(Zip, DataManager_GetCurrentStoragePath()); |
| 389 | if (!ret_string.empty()) |
| 390 | Zip = ret_string; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (!TWFunc::Path_Exists(Zip)) { |
| 395 | // zip file doesn't exist |
| 396 | ui_print("Unable to locate zip file '%s'.\n", Zip.c_str()); |
| 397 | ret_val = 1; |
| 398 | } else { |
| 399 | ui_print("Installing zip file '%s'\n", Zip.c_str()); |
| 400 | ret_val = TWinstall_zip(Zip.c_str(), &wipe_cache); |
| 401 | } |
| 402 | if (ret_val != 0) { |
| 403 | LOGE("Error installing zip file '%s'\n", Zip.c_str()); |
| 404 | ret_val = 1; |
| 405 | } else if (wipe_cache) |
| 406 | PartitionManager.Wipe_By_Path("/cache"); |
| 407 | |
| 408 | return ret_val; |
| 409 | } |
| 410 | |
Dees_Troy | 812660f | 2012-09-20 09:55:17 -0400 | [diff] [blame] | 411 | string OpenRecoveryScript::Locate_Zip_File(string Zip, string Storage_Root) { |
| 412 | string Path = TWFunc::Get_Path(Zip); |
| 413 | string File = TWFunc::Get_Filename(Zip); |
| 414 | string pathCpy = Path; |
| 415 | string wholePath; |
| 416 | size_t pos = Path.find("/", 1); |
| 417 | |
| 418 | while (pos != string::npos) |
| 419 | { |
| 420 | pathCpy = Path.substr(pos, Path.size() - pos); |
| 421 | wholePath = pathCpy + "/" + File; |
| 422 | if (TWFunc::Path_Exists(wholePath)) |
| 423 | return wholePath; |
| 424 | wholePath = Storage_Root + "/" + wholePath; |
| 425 | if (TWFunc::Path_Exists(wholePath)) |
| 426 | return wholePath; |
| 427 | |
| 428 | pos = Path.find("/", pos + 1); |
| 429 | } |
| 430 | return ""; |
Dees_Troy | 83a3b12 | 2012-10-01 14:16:43 -0400 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | int OpenRecoveryScript::Backup_Command(string Options) { |
| 434 | char value1[SCRIPT_COMMAND_SIZE]; |
| 435 | int line_len, i; |
| 436 | |
| 437 | strcpy(value1, Options.c_str()); |
| 438 | |
| 439 | DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 0); |
| 440 | DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 0); |
| 441 | DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 0); |
| 442 | DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 0); |
| 443 | DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 0); |
| 444 | DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 0); |
| 445 | DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 0); |
| 446 | DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 0); |
| 447 | DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 0); |
| 448 | DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0); |
| 449 | DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 0); |
| 450 | DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 0); |
| 451 | DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 0); |
| 452 | |
| 453 | ui_print("Setting backup options:\n"); |
| 454 | line_len = Options.size(); |
| 455 | for (i=0; i<line_len; i++) { |
| 456 | if (Options.substr(i, 1) == "S" || Options.substr(i, 1) == "s") { |
| 457 | DataManager_SetIntValue(TW_BACKUP_SYSTEM_VAR, 1); |
| 458 | ui_print("System\n"); |
| 459 | } else if (Options.substr(i, 1) == "D" || Options.substr(i, 1) == "d") { |
| 460 | DataManager_SetIntValue(TW_BACKUP_DATA_VAR, 1); |
| 461 | ui_print("Data\n"); |
| 462 | } else if (Options.substr(i, 1) == "C" || Options.substr(i, 1) == "c") { |
| 463 | DataManager_SetIntValue(TW_BACKUP_CACHE_VAR, 1); |
| 464 | ui_print("Cache\n"); |
| 465 | } else if (Options.substr(i, 1) == "R" || Options.substr(i, 1) == "r") { |
| 466 | DataManager_SetIntValue(TW_BACKUP_RECOVERY_VAR, 1); |
| 467 | ui_print("Recovery\n"); |
| 468 | } else if (Options.substr(i, 1) == "1") { |
| 469 | DataManager_SetIntValue(TW_BACKUP_SP1_VAR, 1); |
| 470 | ui_print("%s\n", "Special1"); |
| 471 | } else if (Options.substr(i, 1) == "2") { |
| 472 | DataManager_SetIntValue(TW_BACKUP_SP2_VAR, 1); |
| 473 | ui_print("%s\n", "Special2"); |
| 474 | } else if (Options.substr(i, 1) == "3") { |
| 475 | DataManager_SetIntValue(TW_BACKUP_SP3_VAR, 1); |
| 476 | ui_print("%s\n", "Special3"); |
| 477 | } else if (Options.substr(i, 1) == "B" || Options.substr(i, 1) == "b") { |
| 478 | DataManager_SetIntValue(TW_BACKUP_BOOT_VAR, 1); |
| 479 | ui_print("Boot\n"); |
| 480 | } else if (Options.substr(i, 1) == "A" || Options.substr(i, 1) == "a") { |
| 481 | DataManager_SetIntValue(TW_BACKUP_ANDSEC_VAR, 1); |
| 482 | ui_print("Android Secure\n"); |
| 483 | } else if (Options.substr(i, 1) == "E" || Options.substr(i, 1) == "e") { |
| 484 | DataManager_SetIntValue(TW_BACKUP_SDEXT_VAR, 1); |
| 485 | ui_print("SD-Ext\n"); |
| 486 | } else if (Options.substr(i, 1) == "O" || Options.substr(i, 1) == "o") { |
| 487 | DataManager_SetIntValue(TW_USE_COMPRESSION_VAR, 1); |
| 488 | ui_print("Compression is on\n"); |
| 489 | } else if (Options.substr(i, 1) == "M" || Options.substr(i, 1) == "m") { |
| 490 | DataManager_SetIntValue(TW_SKIP_MD5_GENERATE_VAR, 1); |
| 491 | ui_print("MD5 Generation is off\n"); |
| 492 | } |
| 493 | } |
| 494 | if (!PartitionManager.Run_Backup()) { |
| 495 | LOGE("Backup failed!\n"); |
| 496 | return 1; |
| 497 | } |
| 498 | ui_print("Backup complete!\n"); |
| 499 | return 0; |
| 500 | } |