Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/wait.h> |
| 25 | #include <unistd.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 26 | #include <linux/input.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 27 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 28 | #include "bootloader.h" |
| 29 | #include "common.h" |
| 30 | #include "extra-functions.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 31 | #include "data.h" |
| 32 | #include "variables.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 33 | |
| 34 | void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm) |
| 35 | { |
| 36 | ui_print("%s", str1); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 37 | ui_print("%s", str2); |
| 38 | pid_t pid = fork(); |
| 39 | if (pid == 0) { |
| 40 | char *args[] = { "/sbin/sh", "-c", (char*)str3, "1>&2", NULL }; |
| 41 | execv("/sbin/sh", args); |
| 42 | fprintf(stderr, str4, strerror(errno)); |
| 43 | _exit(-1); |
| 44 | } |
| 45 | int status; |
| 46 | while (waitpid(pid, &status, WNOHANG) == 0) { |
| 47 | ui_print("."); |
| 48 | sleep(1); |
| 49 | } |
| 50 | ui_print("\n"); |
| 51 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
| 52 | ui_print("%s", str5); |
| 53 | } else { |
| 54 | ui_print("%s", str6); |
| 55 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | int check_backup_name(int show_error) { |
| 59 | // Check the backup name to ensure that it is the correct size and contains only valid characters |
| 60 | // and that a backup with that name doesn't already exist |
| 61 | char backup_name[MAX_BACKUP_NAME_LEN]; |
| 62 | char backup_loc[255], tw_image_dir[255]; |
| 63 | int copy_size = strlen(DataManager_GetStrValue(TW_BACKUP_NAME)); |
| 64 | int index, cur_char; |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 65 | struct stat st; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 66 | |
| 67 | // Check size |
| 68 | if (copy_size > MAX_BACKUP_NAME_LEN) { |
| 69 | if (show_error) |
| 70 | LOGE("Backup name is too long.\n"); |
| 71 | return -2; |
| 72 | } |
| 73 | |
| 74 | // Check characters |
| 75 | strncpy(backup_name, DataManager_GetStrValue(TW_BACKUP_NAME), copy_size); |
| 76 | if (strcmp(backup_name, "0") == 0) |
| 77 | return 0; // A "0" (zero) means to use the current timestamp for the backup name |
| 78 | for (index=0; index<copy_size; index++) { |
| 79 | cur_char = (int)backup_name[index]; |
Dees_Troy | b9d1c6d | 2012-09-26 10:07:14 -0400 | [diff] [blame] | 80 | if (cur_char == 32 || (cur_char >= 48 && cur_char <= 57) || (cur_char >= 65 && cur_char <= 91) || cur_char == 93 || cur_char == 95 || (cur_char >= 97 && cur_char <= 123) || cur_char == 125 || cur_char == 45 || cur_char == 46) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 81 | // These are valid characters |
| 82 | // Numbers |
| 83 | // Upper case letters |
| 84 | // Lower case letters |
Dees_Troy | b9d1c6d | 2012-09-26 10:07:14 -0400 | [diff] [blame] | 85 | // Space |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 86 | // and -_.{}[] |
| 87 | } else { |
| 88 | if (show_error) |
| 89 | LOGE("Backup name '%s' contains invalid character: '%c'\n", backup_name, (char)cur_char); |
| 90 | return -3; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Check to make sure that a backup with this name doesn't already exist |
| 95 | strcpy(backup_loc, DataManager_GetStrValue(TW_BACKUPS_FOLDER_VAR)); |
| 96 | sprintf(tw_image_dir,"%s/%s/.", backup_loc, backup_name); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 97 | if (stat(tw_image_dir, &st) == 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 98 | if (show_error) |
| 99 | LOGE("A backup with this name already exists.\n"); |
| 100 | return -4; |
| 101 | } |
| 102 | |
| 103 | // No problems found, return 0 |
| 104 | return 0; |
| 105 | } |