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