Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <stdarg.h> |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/mount.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 26 | #include <sys/wait.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 29 | #include "cutils/misc.h" |
| 30 | #include "cutils/properties.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 31 | #include "edify/expr.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 32 | #include "mincrypt/sha.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 33 | #include "minzip/DirUtil.h" |
Koushik Dutta | 7c1bffe | 2010-12-18 19:44:33 -0800 | [diff] [blame] | 34 | #include "mounts.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 35 | #include "mtdutils/mtdutils.h" |
| 36 | #include "updater.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 37 | #include "applypatch/applypatch.h" |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 38 | |
Koushik Dutta | cf5195a | 2011-05-29 18:45:42 -0700 | [diff] [blame] | 39 | #include "flashutils/flashutils.h" |
| 40 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 41 | #ifdef USE_EXT4 |
| 42 | #include "make_ext4fs.h" |
| 43 | #endif |
| 44 | |
| 45 | // mount(fs_type, partition_type, location, mount_point) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 46 | // |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 47 | // fs_type="yaffs2" partition_type="MTD" location=partition |
| 48 | // fs_type="ext4" partition_type="EMMC" location=device |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 49 | Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 50 | char* result = NULL; |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 51 | if (argc != 4) { |
| 52 | return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 53 | } |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 54 | char* fs_type; |
| 55 | char* partition_type; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 56 | char* location; |
| 57 | char* mount_point; |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 58 | if (ReadArgs(state, argv, 4, &fs_type, &partition_type, |
| 59 | &location, &mount_point) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 60 | return NULL; |
| 61 | } |
| 62 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 63 | if (strlen(fs_type) == 0) { |
| 64 | ErrorAbort(state, "fs_type argument to %s() can't be empty", name); |
| 65 | goto done; |
| 66 | } |
| 67 | if (strlen(partition_type) == 0) { |
| 68 | ErrorAbort(state, "partition_type argument to %s() can't be empty", |
| 69 | name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 70 | goto done; |
| 71 | } |
| 72 | if (strlen(location) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 73 | ErrorAbort(state, "location argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 74 | goto done; |
| 75 | } |
| 76 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 77 | ErrorAbort(state, "mount_point argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 78 | goto done; |
| 79 | } |
| 80 | |
| 81 | mkdir(mount_point, 0755); |
| 82 | |
Koushik Dutta | 8637c73 | 2010-12-18 18:18:44 -0800 | [diff] [blame] | 83 | if (strcmp(partition_type, "MTD") == 0) { |
| 84 | mtd_scan_partitions(); |
| 85 | const MtdPartition* mtd; |
| 86 | mtd = mtd_find_partition_by_name(location); |
| 87 | if (mtd == NULL) { |
| 88 | fprintf(stderr, "%s: no mtd partition named \"%s\"", |
| 89 | name, location); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 90 | result = strdup(""); |
Koushik Dutta | 8637c73 | 2010-12-18 18:18:44 -0800 | [diff] [blame] | 91 | goto done; |
| 92 | } |
| 93 | if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) { |
| 94 | fprintf(stderr, "mtd mount of %s failed: %s\n", |
| 95 | location, strerror(errno)); |
| 96 | result = strdup(""); |
| 97 | goto done; |
| 98 | } |
| 99 | result = mount_point; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 100 | } else { |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 101 | if (mount(location, mount_point, fs_type, |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 102 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 103 | fprintf(stderr, "%s: failed to mount %s at %s: %s\n", |
| 104 | name, location, mount_point, strerror(errno)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 105 | result = strdup(""); |
| 106 | } else { |
| 107 | result = mount_point; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | done: |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 112 | free(fs_type); |
| 113 | free(partition_type); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 114 | free(location); |
| 115 | if (result != mount_point) free(mount_point); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 116 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 119 | |
| 120 | // is_mounted(mount_point) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 121 | Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 122 | char* result = NULL; |
| 123 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 124 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 125 | } |
| 126 | char* mount_point; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 127 | if (ReadArgs(state, argv, 1, &mount_point) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 128 | return NULL; |
| 129 | } |
| 130 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 131 | ErrorAbort(state, "mount_point argument to unmount() can't be empty"); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 132 | goto done; |
| 133 | } |
| 134 | |
| 135 | scan_mounted_volumes(); |
| 136 | const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); |
| 137 | if (vol == NULL) { |
| 138 | result = strdup(""); |
| 139 | } else { |
| 140 | result = mount_point; |
| 141 | } |
| 142 | |
| 143 | done: |
| 144 | if (result != mount_point) free(mount_point); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 145 | return StringValue(result); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 149 | Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 150 | char* result = NULL; |
| 151 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 152 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 153 | } |
| 154 | char* mount_point; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 155 | if (ReadArgs(state, argv, 1, &mount_point) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 156 | return NULL; |
| 157 | } |
| 158 | if (strlen(mount_point) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 159 | ErrorAbort(state, "mount_point argument to unmount() can't be empty"); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 160 | goto done; |
| 161 | } |
| 162 | |
| 163 | scan_mounted_volumes(); |
| 164 | const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point); |
| 165 | if (vol == NULL) { |
| 166 | fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point); |
| 167 | result = strdup(""); |
| 168 | } else { |
| 169 | unmount_mounted_volume(vol); |
| 170 | result = mount_point; |
| 171 | } |
| 172 | |
| 173 | done: |
| 174 | if (result != mount_point) free(mount_point); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 175 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 176 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 177 | |
| 178 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 179 | // format(fs_type, partition_type, location) |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 180 | // |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 181 | // fs_type="yaffs2" partition_type="MTD" location=partition |
| 182 | // fs_type="ext4" partition_type="EMMC" location=device |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 183 | Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 184 | char* result = NULL; |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 185 | if (argc != 3) { |
| 186 | return ErrorAbort(state, "%s() expects 3 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 187 | } |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 188 | char* fs_type; |
| 189 | char* partition_type; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 190 | char* location; |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 191 | if (ReadArgs(state, argv, 3, &fs_type, &partition_type, &location) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 192 | return NULL; |
| 193 | } |
| 194 | |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 195 | if (strlen(fs_type) == 0) { |
| 196 | ErrorAbort(state, "fs_type argument to %s() can't be empty", name); |
| 197 | goto done; |
| 198 | } |
| 199 | if (strlen(partition_type) == 0) { |
| 200 | ErrorAbort(state, "partition_type argument to %s() can't be empty", |
| 201 | name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 202 | goto done; |
| 203 | } |
| 204 | if (strlen(location) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 205 | ErrorAbort(state, "location argument to %s() can't be empty", name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 206 | goto done; |
| 207 | } |
| 208 | |
Koushik Dutta | 8637c73 | 2010-12-18 18:18:44 -0800 | [diff] [blame] | 209 | if (strcmp(partition_type, "MTD") == 0) { |
| 210 | mtd_scan_partitions(); |
| 211 | const MtdPartition* mtd = mtd_find_partition_by_name(location); |
| 212 | if (mtd == NULL) { |
| 213 | fprintf(stderr, "%s: no mtd partition named \"%s\"", |
| 214 | name, location); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 215 | result = strdup(""); |
| 216 | goto done; |
| 217 | } |
Koushik Dutta | 8637c73 | 2010-12-18 18:18:44 -0800 | [diff] [blame] | 218 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 219 | if (ctx == NULL) { |
| 220 | fprintf(stderr, "%s: can't write \"%s\"", name, location); |
| 221 | result = strdup(""); |
| 222 | goto done; |
| 223 | } |
| 224 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 225 | mtd_write_close(ctx); |
| 226 | fprintf(stderr, "%s: failed to erase \"%s\"", name, location); |
| 227 | result = strdup(""); |
| 228 | goto done; |
| 229 | } |
| 230 | if (mtd_write_close(ctx) != 0) { |
| 231 | fprintf(stderr, "%s: failed to close \"%s\"", name, location); |
| 232 | result = strdup(""); |
| 233 | goto done; |
| 234 | } |
| 235 | result = location; |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 236 | #ifdef USE_EXT4 |
| 237 | } else if (strcmp(fs_type, "ext4") == 0) { |
| 238 | reset_ext4fs_info(); |
Brian Swetland | 792b007 | 2010-09-15 18:03:58 -0700 | [diff] [blame] | 239 | int status = make_ext4fs(location, NULL, NULL, 0, 0, 0); |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 240 | if (status != 0) { |
| 241 | fprintf(stderr, "%s: make_ext4fs failed (%d) on %s", |
| 242 | name, status, location); |
| 243 | result = strdup(""); |
| 244 | goto done; |
| 245 | } |
| 246 | result = location; |
| 247 | #endif |
Koushik Dutta | b4c5fd6 | 2011-01-02 22:54:31 -0800 | [diff] [blame] | 248 | } else if (strcmp(fs_type, "ext2") == 0) { |
| 249 | int status = format_ext2_device(location); |
| 250 | if (status != 0) { |
| 251 | fprintf(stderr, "%s: format_ext2_device failed (%d) on %s", |
| 252 | name, status, location); |
| 253 | result = strdup(""); |
| 254 | goto done; |
| 255 | } |
| 256 | result = location; |
| 257 | } else if (strcmp(fs_type, "ext3") == 0) { |
| 258 | int status = format_ext3_device(location); |
| 259 | if (status != 0) { |
| 260 | fprintf(stderr, "%s: format_ext3_device failed (%d) on %s", |
| 261 | name, status, location); |
| 262 | result = strdup(""); |
| 263 | goto done; |
| 264 | } |
| 265 | result = location; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 266 | } else { |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 267 | fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"", |
| 268 | name, fs_type, partition_type); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 269 | } |
Koushik Dutta | 8637c73 | 2010-12-18 18:18:44 -0800 | [diff] [blame] | 270 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 271 | done: |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 272 | free(fs_type); |
| 273 | free(partition_type); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 274 | if (result != location) free(location); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 275 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 278 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 279 | Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 280 | char** paths = malloc(argc * sizeof(char*)); |
| 281 | int i; |
| 282 | for (i = 0; i < argc; ++i) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 283 | paths[i] = Evaluate(state, argv[i]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 284 | if (paths[i] == NULL) { |
| 285 | int j; |
| 286 | for (j = 0; j < i; ++i) { |
| 287 | free(paths[j]); |
| 288 | } |
| 289 | free(paths); |
| 290 | return NULL; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | bool recursive = (strcmp(name, "delete_recursive") == 0); |
| 295 | |
| 296 | int success = 0; |
| 297 | for (i = 0; i < argc; ++i) { |
| 298 | if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0) |
| 299 | ++success; |
| 300 | free(paths[i]); |
| 301 | } |
| 302 | free(paths); |
| 303 | |
| 304 | char buffer[10]; |
| 305 | sprintf(buffer, "%d", success); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 306 | return StringValue(strdup(buffer)); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 309 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 310 | Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 311 | if (argc != 2) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 312 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 313 | } |
| 314 | char* frac_str; |
| 315 | char* sec_str; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 316 | if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | double frac = strtod(frac_str, NULL); |
| 321 | int sec = strtol(sec_str, NULL, 10); |
| 322 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 323 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 324 | fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec); |
| 325 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 326 | free(sec_str); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 327 | return StringValue(frac_str); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 330 | Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 331 | if (argc != 1) { |
| 332 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 333 | } |
| 334 | char* frac_str; |
| 335 | if (ReadArgs(state, argv, 1, &frac_str) < 0) { |
| 336 | return NULL; |
| 337 | } |
| 338 | |
| 339 | double frac = strtod(frac_str, NULL); |
| 340 | |
| 341 | UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); |
| 342 | fprintf(ui->cmd_pipe, "set_progress %f\n", frac); |
| 343 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 344 | return StringValue(frac_str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 347 | // package_extract_dir(package_path, destination_path) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 348 | Value* PackageExtractDirFn(const char* name, State* state, |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 349 | int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 350 | if (argc != 2) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 351 | return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 352 | } |
| 353 | char* zip_path; |
| 354 | char* dest_path; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 355 | if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 356 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 357 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 358 | |
| 359 | // To create a consistent system image, never use the clock for timestamps. |
| 360 | struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default |
| 361 | |
| 362 | bool success = mzExtractRecursive(za, zip_path, dest_path, |
| 363 | MZ_EXTRACT_FILES_ONLY, ×tamp, |
| 364 | NULL, NULL); |
| 365 | free(zip_path); |
| 366 | free(dest_path); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 367 | return StringValue(strdup(success ? "t" : "")); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 370 | |
| 371 | // package_extract_file(package_path, destination_path) |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 372 | // or |
| 373 | // package_extract_file(package_path) |
| 374 | // to return the entire contents of the file as the result of this |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 375 | // function (the char* returned is actually a FileContents*). |
| 376 | Value* PackageExtractFileFn(const char* name, State* state, |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 377 | int argc, Expr* argv[]) { |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 378 | if (argc != 1 && argc != 2) { |
| 379 | return ErrorAbort(state, "%s() expects 1 or 2 args, got %d", |
| 380 | name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 381 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 382 | bool success = false; |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 383 | if (argc == 2) { |
| 384 | // The two-argument version extracts to a file. |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 385 | |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 386 | char* zip_path; |
| 387 | char* dest_path; |
| 388 | if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; |
| 389 | |
| 390 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
| 391 | const ZipEntry* entry = mzFindZipEntry(za, zip_path); |
| 392 | if (entry == NULL) { |
| 393 | fprintf(stderr, "%s: no %s in package\n", name, zip_path); |
| 394 | goto done2; |
| 395 | } |
| 396 | |
| 397 | FILE* f = fopen(dest_path, "wb"); |
| 398 | if (f == NULL) { |
| 399 | fprintf(stderr, "%s: can't open %s for write: %s\n", |
| 400 | name, dest_path, strerror(errno)); |
| 401 | goto done2; |
| 402 | } |
| 403 | success = mzExtractZipEntryToFile(za, entry, fileno(f)); |
| 404 | fclose(f); |
| 405 | |
| 406 | done2: |
| 407 | free(zip_path); |
| 408 | free(dest_path); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 409 | return StringValue(strdup(success ? "t" : "")); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 410 | } else { |
| 411 | // The one-argument version returns the contents of the file |
| 412 | // as the result. |
| 413 | |
| 414 | char* zip_path; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 415 | Value* v = malloc(sizeof(Value)); |
| 416 | v->type = VAL_BLOB; |
| 417 | v->size = -1; |
| 418 | v->data = NULL; |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 419 | |
| 420 | if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL; |
| 421 | |
| 422 | ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; |
| 423 | const ZipEntry* entry = mzFindZipEntry(za, zip_path); |
| 424 | if (entry == NULL) { |
| 425 | fprintf(stderr, "%s: no %s in package\n", name, zip_path); |
| 426 | goto done1; |
| 427 | } |
| 428 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 429 | v->size = mzGetZipEntryUncompLen(entry); |
| 430 | v->data = malloc(v->size); |
| 431 | if (v->data == NULL) { |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 432 | fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n", |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 433 | name, (long)v->size, zip_path); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 434 | goto done1; |
| 435 | } |
| 436 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 437 | success = mzExtractZipEntryToBuffer(za, entry, |
| 438 | (unsigned char *)v->data); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 439 | |
| 440 | done1: |
| 441 | free(zip_path); |
| 442 | if (!success) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 443 | free(v->data); |
| 444 | v->data = NULL; |
| 445 | v->size = -1; |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 446 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 447 | return v; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 448 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 452 | // symlink target src1 src2 ... |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 453 | // unlinks any previously existing src1, src2, etc before creating symlinks. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 454 | Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 455 | if (argc == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 456 | return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 457 | } |
| 458 | char* target; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 459 | target = Evaluate(state, argv[0]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 460 | if (target == NULL) return NULL; |
| 461 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 462 | char** srcs = ReadVarArgs(state, argc-1, argv+1); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 463 | if (srcs == NULL) { |
| 464 | free(target); |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | int i; |
| 469 | for (i = 0; i < argc-1; ++i) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 470 | if (unlink(srcs[i]) < 0) { |
| 471 | if (errno != ENOENT) { |
| 472 | fprintf(stderr, "%s: failed to remove %s: %s\n", |
| 473 | name, srcs[i], strerror(errno)); |
| 474 | } |
| 475 | } |
| 476 | if (symlink(target, srcs[i]) < 0) { |
| 477 | fprintf(stderr, "%s: failed to symlink %s to %s: %s\n", |
| 478 | name, srcs[i], target, strerror(errno)); |
| 479 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 480 | free(srcs[i]); |
| 481 | } |
| 482 | free(srcs); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 483 | return StringValue(strdup("")); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 486 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 487 | Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 488 | char* result = NULL; |
| 489 | bool recursive = (strcmp(name, "set_perm_recursive") == 0); |
| 490 | |
| 491 | int min_args = 4 + (recursive ? 1 : 0); |
| 492 | if (argc < min_args) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 493 | return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 496 | char** args = ReadVarArgs(state, argc, argv); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 497 | if (args == NULL) return NULL; |
| 498 | |
| 499 | char* end; |
| 500 | int i; |
| 501 | |
| 502 | int uid = strtoul(args[0], &end, 0); |
| 503 | if (*end != '\0' || args[0][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 504 | ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 505 | goto done; |
| 506 | } |
| 507 | |
| 508 | int gid = strtoul(args[1], &end, 0); |
| 509 | if (*end != '\0' || args[1][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 510 | ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 511 | goto done; |
| 512 | } |
| 513 | |
| 514 | if (recursive) { |
| 515 | int dir_mode = strtoul(args[2], &end, 0); |
| 516 | if (*end != '\0' || args[2][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 517 | ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 518 | goto done; |
| 519 | } |
| 520 | |
| 521 | int file_mode = strtoul(args[3], &end, 0); |
| 522 | if (*end != '\0' || args[3][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 523 | ErrorAbort(state, "%s: \"%s\" not a valid filemode", |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 524 | name, args[3]); |
| 525 | goto done; |
| 526 | } |
| 527 | |
| 528 | for (i = 4; i < argc; ++i) { |
| 529 | dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode); |
| 530 | } |
| 531 | } else { |
| 532 | int mode = strtoul(args[2], &end, 0); |
| 533 | if (*end != '\0' || args[2][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 534 | ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 535 | goto done; |
| 536 | } |
| 537 | |
Doug Zongker | 0bbfe3d | 2009-06-25 13:37:31 -0700 | [diff] [blame] | 538 | for (i = 3; i < argc; ++i) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 539 | if (chown(args[i], uid, gid) < 0) { |
| 540 | fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n", |
| 541 | name, args[i], uid, gid, strerror(errno)); |
| 542 | } |
| 543 | if (chmod(args[i], mode) < 0) { |
| 544 | fprintf(stderr, "%s: chmod of %s to %o failed: %s\n", |
| 545 | name, args[i], mode, strerror(errno)); |
| 546 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | result = strdup(""); |
| 550 | |
| 551 | done: |
| 552 | for (i = 0; i < argc; ++i) { |
| 553 | free(args[i]); |
| 554 | } |
| 555 | free(args); |
| 556 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 557 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 560 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 561 | Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 562 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 563 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 564 | } |
| 565 | char* key; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 566 | key = Evaluate(state, argv[0]); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 567 | if (key == NULL) return NULL; |
| 568 | |
| 569 | char value[PROPERTY_VALUE_MAX]; |
| 570 | property_get(key, value, ""); |
| 571 | free(key); |
| 572 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 573 | return StringValue(strdup(value)); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 577 | // file_getprop(file, key) |
| 578 | // |
| 579 | // interprets 'file' as a getprop-style file (key=value pairs, one |
| 580 | // per line, # comment lines and blank lines okay), and returns the value |
| 581 | // for 'key' (or "" if it isn't defined). |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 582 | Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 583 | char* result = NULL; |
| 584 | char* buffer = NULL; |
| 585 | char* filename; |
| 586 | char* key; |
| 587 | if (ReadArgs(state, argv, 2, &filename, &key) < 0) { |
| 588 | return NULL; |
| 589 | } |
| 590 | |
| 591 | struct stat st; |
| 592 | if (stat(filename, &st) < 0) { |
| 593 | ErrorAbort(state, "%s: failed to stat \"%s\": %s", |
| 594 | name, filename, strerror(errno)); |
| 595 | goto done; |
| 596 | } |
| 597 | |
| 598 | #define MAX_FILE_GETPROP_SIZE 65536 |
| 599 | |
| 600 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
| 601 | ErrorAbort(state, "%s too large for %s (max %d)", |
| 602 | filename, name, MAX_FILE_GETPROP_SIZE); |
| 603 | goto done; |
| 604 | } |
| 605 | |
| 606 | buffer = malloc(st.st_size+1); |
| 607 | if (buffer == NULL) { |
| 608 | ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1); |
| 609 | goto done; |
| 610 | } |
| 611 | |
| 612 | FILE* f = fopen(filename, "rb"); |
| 613 | if (f == NULL) { |
| 614 | ErrorAbort(state, "%s: failed to open %s: %s", |
| 615 | name, filename, strerror(errno)); |
| 616 | goto done; |
| 617 | } |
| 618 | |
| 619 | if (fread(buffer, 1, st.st_size, f) != st.st_size) { |
| 620 | ErrorAbort(state, "%s: failed to read %d bytes from %s", |
| 621 | name, st.st_size+1, filename); |
| 622 | fclose(f); |
| 623 | goto done; |
| 624 | } |
| 625 | buffer[st.st_size] = '\0'; |
| 626 | |
| 627 | fclose(f); |
| 628 | |
| 629 | char* line = strtok(buffer, "\n"); |
| 630 | do { |
| 631 | // skip whitespace at start of line |
| 632 | while (*line && isspace(*line)) ++line; |
| 633 | |
| 634 | // comment or blank line: skip to next line |
| 635 | if (*line == '\0' || *line == '#') continue; |
| 636 | |
| 637 | char* equal = strchr(line, '='); |
| 638 | if (equal == NULL) { |
| 639 | ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?", |
| 640 | name, line, filename); |
| 641 | goto done; |
| 642 | } |
| 643 | |
| 644 | // trim whitespace between key and '=' |
| 645 | char* key_end = equal-1; |
| 646 | while (key_end > line && isspace(*key_end)) --key_end; |
| 647 | key_end[1] = '\0'; |
| 648 | |
| 649 | // not the key we're looking for |
| 650 | if (strcmp(key, line) != 0) continue; |
| 651 | |
| 652 | // skip whitespace after the '=' to the start of the value |
| 653 | char* val_start = equal+1; |
| 654 | while(*val_start && isspace(*val_start)) ++val_start; |
| 655 | |
| 656 | // trim trailing whitespace |
| 657 | char* val_end = val_start + strlen(val_start)-1; |
| 658 | while (val_end > val_start && isspace(*val_end)) --val_end; |
| 659 | val_end[1] = '\0'; |
| 660 | |
| 661 | result = strdup(val_start); |
| 662 | break; |
| 663 | |
| 664 | } while ((line = strtok(NULL, "\n"))); |
| 665 | |
| 666 | if (result == NULL) result = strdup(""); |
| 667 | |
| 668 | done: |
| 669 | free(filename); |
| 670 | free(key); |
| 671 | free(buffer); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 672 | return StringValue(result); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 676 | static bool write_raw_image_cb(const unsigned char* data, |
| 677 | int data_len, void* ctx) { |
| 678 | int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len); |
| 679 | if (r == data_len) return true; |
| 680 | fprintf(stderr, "%s\n", strerror(errno)); |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | // write_raw_image(file, partition) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 685 | Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 686 | char* result = NULL; |
| 687 | |
| 688 | char* partition; |
| 689 | char* filename; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 690 | if (ReadArgs(state, argv, 2, &filename, &partition) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 691 | return NULL; |
| 692 | } |
| 693 | |
| 694 | if (strlen(partition) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 695 | ErrorAbort(state, "partition argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 696 | goto done; |
| 697 | } |
| 698 | if (strlen(filename) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 699 | ErrorAbort(state, "file argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 700 | goto done; |
| 701 | } |
| 702 | |
Koushik Dutta | cf5195a | 2011-05-29 18:45:42 -0700 | [diff] [blame] | 703 | if (0 == restore_raw_partition(NULL, partition, filename)) |
Koushik Dutta | 1b86754 | 2010-11-10 23:52:09 -0800 | [diff] [blame] | 704 | result = strdup(partition); |
| 705 | else |
Koushik Dutta | 7c1bffe | 2010-12-18 19:44:33 -0800 | [diff] [blame] | 706 | result = strdup(""); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 707 | |
| 708 | done: |
| 709 | if (result != partition) free(partition); |
| 710 | free(filename); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 711 | return StringValue(result); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 714 | // apply_patch_space(bytes) |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 715 | Value* ApplyPatchSpaceFn(const char* name, State* state, |
| 716 | int argc, Expr* argv[]) { |
| 717 | char* bytes_str; |
| 718 | if (ReadArgs(state, argv, 1, &bytes_str) < 0) { |
| 719 | return NULL; |
| 720 | } |
| 721 | |
| 722 | char* endptr; |
| 723 | size_t bytes = strtol(bytes_str, &endptr, 10); |
| 724 | if (bytes == 0 && endptr == bytes_str) { |
| 725 | ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", |
| 726 | name, bytes_str); |
| 727 | free(bytes_str); |
| 728 | return NULL; |
| 729 | } |
| 730 | |
| 731 | return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t")); |
| 732 | } |
| 733 | |
| 734 | |
| 735 | // apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 736 | Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 737 | if (argc < 6 || (argc % 2) == 1) { |
| 738 | return ErrorAbort(state, "%s(): expected at least 6 args and an " |
| 739 | "even number, got %d", |
| 740 | name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 743 | char* source_filename; |
| 744 | char* target_filename; |
| 745 | char* target_sha1; |
| 746 | char* target_size_str; |
| 747 | if (ReadArgs(state, argv, 4, &source_filename, &target_filename, |
| 748 | &target_sha1, &target_size_str) < 0) { |
| 749 | return NULL; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 750 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 751 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 752 | char* endptr; |
| 753 | size_t target_size = strtol(target_size_str, &endptr, 10); |
| 754 | if (target_size == 0 && endptr == target_size_str) { |
| 755 | ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", |
| 756 | name, target_size_str); |
| 757 | free(source_filename); |
| 758 | free(target_filename); |
| 759 | free(target_sha1); |
| 760 | free(target_size_str); |
| 761 | return NULL; |
| 762 | } |
| 763 | |
| 764 | int patchcount = (argc-4) / 2; |
| 765 | Value** patches = ReadValueVarArgs(state, argc-4, argv+4); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 766 | |
| 767 | int i; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 768 | for (i = 0; i < patchcount; ++i) { |
| 769 | if (patches[i*2]->type != VAL_STRING) { |
| 770 | ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i); |
| 771 | break; |
| 772 | } |
| 773 | if (patches[i*2+1]->type != VAL_BLOB) { |
| 774 | ErrorAbort(state, "%s(): patch #%d is not blob", name, i); |
| 775 | break; |
| 776 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 777 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 778 | if (i != patchcount) { |
| 779 | for (i = 0; i < patchcount*2; ++i) { |
| 780 | FreeValue(patches[i]); |
| 781 | } |
| 782 | free(patches); |
| 783 | return NULL; |
| 784 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 785 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 786 | char** patch_sha_str = malloc(patchcount * sizeof(char*)); |
| 787 | for (i = 0; i < patchcount; ++i) { |
| 788 | patch_sha_str[i] = patches[i*2]->data; |
| 789 | patches[i*2]->data = NULL; |
| 790 | FreeValue(patches[i*2]); |
| 791 | patches[i] = patches[i*2+1]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 792 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 793 | |
| 794 | int result = applypatch(source_filename, target_filename, |
| 795 | target_sha1, target_size, |
| 796 | patchcount, patch_sha_str, patches); |
| 797 | |
| 798 | for (i = 0; i < patchcount; ++i) { |
| 799 | FreeValue(patches[i]); |
| 800 | } |
| 801 | free(patch_sha_str); |
| 802 | free(patches); |
| 803 | |
| 804 | return StringValue(strdup(result == 0 ? "t" : "")); |
| 805 | } |
| 806 | |
| 807 | // apply_patch_check(file, [sha1_1, ...]) |
| 808 | Value* ApplyPatchCheckFn(const char* name, State* state, |
| 809 | int argc, Expr* argv[]) { |
| 810 | if (argc < 1) { |
| 811 | return ErrorAbort(state, "%s(): expected at least 1 arg, got %d", |
| 812 | name, argc); |
| 813 | } |
| 814 | |
| 815 | char* filename; |
| 816 | if (ReadArgs(state, argv, 1, &filename) < 0) { |
| 817 | return NULL; |
| 818 | } |
| 819 | |
| 820 | int patchcount = argc-1; |
| 821 | char** sha1s = ReadVarArgs(state, argc-1, argv+1); |
| 822 | |
| 823 | int result = applypatch_check(filename, patchcount, sha1s); |
| 824 | |
| 825 | int i; |
| 826 | for (i = 0; i < patchcount; ++i) { |
| 827 | free(sha1s[i]); |
| 828 | } |
| 829 | free(sha1s); |
| 830 | |
| 831 | return StringValue(strdup(result == 0 ? "t" : "")); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 834 | Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 835 | char** args = ReadVarArgs(state, argc, argv); |
| 836 | if (args == NULL) { |
| 837 | return NULL; |
| 838 | } |
| 839 | |
| 840 | int size = 0; |
| 841 | int i; |
| 842 | for (i = 0; i < argc; ++i) { |
| 843 | size += strlen(args[i]); |
| 844 | } |
| 845 | char* buffer = malloc(size+1); |
| 846 | size = 0; |
| 847 | for (i = 0; i < argc; ++i) { |
| 848 | strcpy(buffer+size, args[i]); |
| 849 | size += strlen(args[i]); |
| 850 | free(args[i]); |
| 851 | } |
| 852 | free(args); |
| 853 | buffer[size] = '\0'; |
| 854 | |
| 855 | char* line = strtok(buffer, "\n"); |
| 856 | while (line) { |
| 857 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, |
| 858 | "ui_print %s\n", line); |
| 859 | line = strtok(NULL, "\n"); |
| 860 | } |
| 861 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n"); |
| 862 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 863 | return StringValue(buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 866 | Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 867 | if (argc < 1) { |
| 868 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 869 | } |
| 870 | char** args = ReadVarArgs(state, argc, argv); |
| 871 | if (args == NULL) { |
| 872 | return NULL; |
| 873 | } |
| 874 | |
| 875 | char** args2 = malloc(sizeof(char*) * (argc+1)); |
| 876 | memcpy(args2, args, sizeof(char*) * argc); |
| 877 | args2[argc] = NULL; |
| 878 | |
| 879 | fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc); |
| 880 | |
| 881 | pid_t child = fork(); |
| 882 | if (child == 0) { |
| 883 | execv(args2[0], args2); |
| 884 | fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno)); |
| 885 | _exit(1); |
| 886 | } |
| 887 | int status; |
| 888 | waitpid(child, &status, 0); |
| 889 | if (WIFEXITED(status)) { |
| 890 | if (WEXITSTATUS(status) != 0) { |
| 891 | fprintf(stderr, "run_program: child exited with status %d\n", |
| 892 | WEXITSTATUS(status)); |
| 893 | } |
| 894 | } else if (WIFSIGNALED(status)) { |
| 895 | fprintf(stderr, "run_program: child terminated by signal %d\n", |
| 896 | WTERMSIG(status)); |
| 897 | } |
| 898 | |
| 899 | int i; |
| 900 | for (i = 0; i < argc; ++i) { |
| 901 | free(args[i]); |
| 902 | } |
| 903 | free(args); |
| 904 | free(args2); |
| 905 | |
| 906 | char buffer[20]; |
| 907 | sprintf(buffer, "%d", status); |
| 908 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 909 | return StringValue(strdup(buffer)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 912 | // Take a sha-1 digest and return it as a newly-allocated hex string. |
| 913 | static char* PrintSha1(uint8_t* digest) { |
| 914 | char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1); |
| 915 | int i; |
| 916 | const char* alphabet = "0123456789abcdef"; |
| 917 | for (i = 0; i < SHA_DIGEST_SIZE; ++i) { |
| 918 | buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf]; |
| 919 | buffer[i*2+1] = alphabet[digest[i] & 0xf]; |
| 920 | } |
| 921 | buffer[i*2] = '\0'; |
| 922 | return buffer; |
| 923 | } |
| 924 | |
| 925 | // sha1_check(data) |
| 926 | // to return the sha1 of the data (given in the format returned by |
| 927 | // read_file). |
| 928 | // |
| 929 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 930 | // returns the sha1 of the file if it matches any of the hex |
| 931 | // strings passed, or "" if it does not equal any of them. |
| 932 | // |
| 933 | Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 934 | if (argc < 1) { |
| 935 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 936 | } |
| 937 | |
| 938 | Value** args = ReadValueVarArgs(state, argc, argv); |
| 939 | if (args == NULL) { |
| 940 | return NULL; |
| 941 | } |
| 942 | |
| 943 | if (args[0]->size < 0) { |
| 944 | fprintf(stderr, "%s(): no file contents received", name); |
| 945 | return StringValue(strdup("")); |
| 946 | } |
| 947 | uint8_t digest[SHA_DIGEST_SIZE]; |
| 948 | SHA(args[0]->data, args[0]->size, digest); |
| 949 | FreeValue(args[0]); |
| 950 | |
| 951 | if (argc == 1) { |
| 952 | return StringValue(PrintSha1(digest)); |
| 953 | } |
| 954 | |
| 955 | int i; |
| 956 | uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE); |
| 957 | for (i = 1; i < argc; ++i) { |
| 958 | if (args[i]->type != VAL_STRING) { |
| 959 | fprintf(stderr, "%s(): arg %d is not a string; skipping", |
| 960 | name, i); |
| 961 | } else if (ParseSha1(args[i]->data, arg_digest) != 0) { |
| 962 | // Warn about bad args and skip them. |
| 963 | fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping", |
| 964 | name, args[i]->data); |
| 965 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) { |
| 966 | break; |
| 967 | } |
| 968 | FreeValue(args[i]); |
| 969 | } |
| 970 | if (i >= argc) { |
| 971 | // Didn't match any of the hex strings; return false. |
| 972 | return StringValue(strdup("")); |
| 973 | } |
| 974 | // Found a match; free all the remaining arguments and return the |
| 975 | // matched one. |
| 976 | int j; |
| 977 | for (j = i+1; j < argc; ++j) { |
| 978 | FreeValue(args[j]); |
| 979 | } |
| 980 | return args[i]; |
| 981 | } |
| 982 | |
| 983 | // Read a local file and return its contents (the char* returned |
| 984 | // is actually a FileContents*). |
| 985 | Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 986 | if (argc != 1) { |
| 987 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 988 | } |
| 989 | char* filename; |
| 990 | if (ReadArgs(state, argv, 1, &filename) < 0) return NULL; |
| 991 | |
| 992 | Value* v = malloc(sizeof(Value)); |
| 993 | v->type = VAL_BLOB; |
| 994 | |
| 995 | FileContents fc; |
| 996 | if (LoadFileContents(filename, &fc) != 0) { |
| 997 | ErrorAbort(state, "%s() loading \"%s\" failed: %s", |
| 998 | name, filename, strerror(errno)); |
| 999 | free(filename); |
| 1000 | free(v); |
| 1001 | free(fc.data); |
| 1002 | return NULL; |
| 1003 | } |
| 1004 | |
| 1005 | v->size = fc.size; |
| 1006 | v->data = (char*)fc.data; |
| 1007 | |
| 1008 | free(filename); |
| 1009 | return v; |
| 1010 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1011 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1012 | void RegisterInstallFunctions() { |
| 1013 | RegisterFunction("mount", MountFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1014 | RegisterFunction("is_mounted", IsMountedFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1015 | RegisterFunction("unmount", UnmountFn); |
| 1016 | RegisterFunction("format", FormatFn); |
| 1017 | RegisterFunction("show_progress", ShowProgressFn); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 1018 | RegisterFunction("set_progress", SetProgressFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1019 | RegisterFunction("delete", DeleteFn); |
| 1020 | RegisterFunction("delete_recursive", DeleteFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1021 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 1022 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1023 | RegisterFunction("symlink", SymlinkFn); |
| 1024 | RegisterFunction("set_perm", SetPermFn); |
| 1025 | RegisterFunction("set_perm_recursive", SetPermFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1026 | |
| 1027 | RegisterFunction("getprop", GetPropFn); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1028 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1029 | RegisterFunction("write_raw_image", WriteRawImageFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1030 | |
| 1031 | RegisterFunction("apply_patch", ApplyPatchFn); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1032 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1033 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1034 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1035 | RegisterFunction("read_file", ReadFileFn); |
| 1036 | RegisterFunction("sha1_check", Sha1CheckFn); |
| 1037 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1038 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1039 | |
| 1040 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1041 | } |