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