blob: f202aff3376db12be99b295c13fee633740e411e [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
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_Troy51a0e822012-09-05 15:24:24 -040017#include <errno.h>
18#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040019#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_Troy51a0e822012-09-05 15:24:24 -040026#include <linux/input.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040027
Dees_Troy51a0e822012-09-05 15:24:24 -040028#include "bootloader.h"
29#include "common.h"
30#include "extra-functions.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040031#include "data.h"
32#include "variables.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040033
34void 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_Troy8170a922012-09-18 15:40:25 -040037 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_Troy51a0e822012-09-05 15:24:24 -040056}
57
Dees_Troy51a0e822012-09-05 15:24:24 -040058int 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_Troy8170a922012-09-18 15:40:25 -040065 struct stat st;
Dees_Troy51a0e822012-09-05 15:24:24 -040066
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_Troyb9d1c6d2012-09-26 10:07:14 -040080 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_Troy51a0e822012-09-05 15:24:24 -040081 // These are valid characters
82 // Numbers
83 // Upper case letters
84 // Lower case letters
Dees_Troyb9d1c6d2012-09-26 10:07:14 -040085 // Space
Dees_Troy51a0e822012-09-05 15:24:24 -040086 // 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_Troy8170a922012-09-18 15:40:25 -040097 if (stat(tw_image_dir, &st) == 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -040098 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}