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> |
Nick Kralevich | 627eb30 | 2013-07-17 19:01:37 -0700 | [diff] [blame] | 30 | #include <selinux/selinux.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 31 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 32 | #include "cutils/misc.h" |
| 33 | #include "cutils/properties.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 34 | #include "edify/expr.h" |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 35 | #include "mincrypt/sha.h" |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 36 | #include "minzip/DirUtil.h" |
| 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 | 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) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 101 | printf("%s: no mtd partition named \"%s\"", |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 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 | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 107 | printf("mtd mount of %s failed: %s\n", |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 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 | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 116 | printf("%s: failed to mount %s at %s: %s\n", |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 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) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 179 | printf("unmount of %s failed; no such volume\n", mount_point); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 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) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 237 | printf("%s: no mtd partition named \"%s\"", |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 238 | name, location); |
| 239 | result = strdup(""); |
| 240 | goto done; |
| 241 | } |
| 242 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 243 | if (ctx == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 244 | printf("%s: can't write \"%s\"", name, location); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 245 | result = strdup(""); |
| 246 | goto done; |
| 247 | } |
| 248 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 249 | mtd_write_close(ctx); |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 250 | printf("%s: failed to erase \"%s\"", name, location); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 251 | result = strdup(""); |
| 252 | goto done; |
| 253 | } |
| 254 | if (mtd_write_close(ctx) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 255 | printf("%s: failed to close \"%s\"", name, location); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 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) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 264 | printf("%s: make_ext4fs failed (%d) on %s", |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 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 | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 272 | printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"", |
Doug Zongker | 3d177d0 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 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) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 398 | printf("%s: no %s in package\n", name, zip_path); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 399 | goto done2; |
| 400 | } |
| 401 | |
| 402 | FILE* f = fopen(dest_path, "wb"); |
| 403 | if (f == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 404 | printf("%s: can't open %s for write: %s\n", |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 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) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 430 | printf("%s: no %s in package\n", name, zip_path); |
Doug Zongker | 6aece33 | 2010-02-01 14:40:12 -0800 | [diff] [blame] | 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 | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 437 | printf("%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); |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 464 | if (result == 0) printf("symlink(): created [%s]\n", name); |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 465 | *p = '/'; |
| 466 | if (result == 0 || errno == EEXIST) { |
| 467 | // successfully created or already existed; we're done |
| 468 | return 0; |
| 469 | } else { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 470 | printf("failed to mkdir %s: %s\n", name, strerror(errno)); |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 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) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 498 | printf("%s: failed to remove %s: %s\n", |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 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])) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 504 | printf("%s: failed to symlink %s to %s: making parents failed\n", |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 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) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 509 | printf("%s: failed to symlink %s to %s: %s\n", |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 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; |
Nick Kralevich | 627eb30 | 2013-07-17 19:01:37 -0700 | [diff] [blame] | 525 | bool recursive = (strcmp(name, "set_perm_recursive") == 0) || (strcmp(name, "set_perm2_recursive") == 0); |
| 526 | bool has_selabel = (strcmp(name, "set_perm2") == 0) || (strcmp(name, "set_perm2_recursive") == 0); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 527 | |
Nick Kralevich | 627eb30 | 2013-07-17 19:01:37 -0700 | [diff] [blame] | 528 | int min_args = 4 + (has_selabel ? 1 : 0) + (recursive ? 1 : 0); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 529 | if (argc < min_args) { |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 530 | return ErrorAbort(state, "%s() expects %d+ args, got %d", |
| 531 | name, min_args, argc); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 534 | char** args = ReadVarArgs(state, argc, argv); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 535 | if (args == NULL) return NULL; |
| 536 | |
| 537 | char* end; |
| 538 | int i; |
Doug Zongker | acd73ed | 2012-03-22 14:32:52 -0700 | [diff] [blame] | 539 | int bad = 0; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 540 | |
| 541 | int uid = strtoul(args[0], &end, 0); |
| 542 | if (*end != '\0' || args[0][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 543 | ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 544 | goto done; |
| 545 | } |
| 546 | |
| 547 | int gid = strtoul(args[1], &end, 0); |
| 548 | if (*end != '\0' || args[1][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 549 | ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 550 | goto done; |
| 551 | } |
| 552 | |
| 553 | if (recursive) { |
| 554 | int dir_mode = strtoul(args[2], &end, 0); |
| 555 | if (*end != '\0' || args[2][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 556 | ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 557 | goto done; |
| 558 | } |
| 559 | |
| 560 | int file_mode = strtoul(args[3], &end, 0); |
| 561 | if (*end != '\0' || args[3][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 562 | ErrorAbort(state, "%s: \"%s\" not a valid filemode", |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 563 | name, args[3]); |
| 564 | goto done; |
| 565 | } |
| 566 | |
Nick Kralevich | 627eb30 | 2013-07-17 19:01:37 -0700 | [diff] [blame] | 567 | char* secontext = NULL; |
| 568 | if (has_selabel) { |
| 569 | secontext = args[4]; |
| 570 | } |
| 571 | |
| 572 | for (i = 4 + (has_selabel ? 1 : 0); i < argc; ++i) { |
| 573 | dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode, secontext); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 574 | } |
| 575 | } else { |
| 576 | int mode = strtoul(args[2], &end, 0); |
| 577 | if (*end != '\0' || args[2][0] == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 578 | ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 579 | goto done; |
| 580 | } |
| 581 | |
Nick Kralevich | 627eb30 | 2013-07-17 19:01:37 -0700 | [diff] [blame] | 582 | char* secontext = NULL; |
| 583 | if (has_selabel) { |
| 584 | secontext = args[3]; |
| 585 | } |
| 586 | |
| 587 | for (i = 3 + (has_selabel ? 1 : 0); i < argc; ++i) { |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 588 | if (chown(args[i], uid, gid) < 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 589 | printf("%s: chown of %s to %d %d failed: %s\n", |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 590 | name, args[i], uid, gid, strerror(errno)); |
Doug Zongker | acd73ed | 2012-03-22 14:32:52 -0700 | [diff] [blame] | 591 | ++bad; |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 592 | } |
| 593 | if (chmod(args[i], mode) < 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 594 | printf("%s: chmod of %s to %o failed: %s\n", |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 595 | name, args[i], mode, strerror(errno)); |
Doug Zongker | acd73ed | 2012-03-22 14:32:52 -0700 | [diff] [blame] | 596 | ++bad; |
Doug Zongker | 60babf8 | 2009-09-18 15:11:24 -0700 | [diff] [blame] | 597 | } |
Nick Kralevich | 627eb30 | 2013-07-17 19:01:37 -0700 | [diff] [blame] | 598 | if (has_selabel && lsetfilecon(args[i], secontext) && (errno != ENOTSUP)) { |
| 599 | printf("%s: lsetfilecon of %s to %s failed: %s\n", |
| 600 | name, args[i], secontext, strerror(errno)); |
| 601 | ++bad; |
| 602 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | result = strdup(""); |
| 606 | |
| 607 | done: |
| 608 | for (i = 0; i < argc; ++i) { |
| 609 | free(args[i]); |
| 610 | } |
| 611 | free(args); |
| 612 | |
Doug Zongker | acd73ed | 2012-03-22 14:32:52 -0700 | [diff] [blame] | 613 | if (bad) { |
| 614 | free(result); |
| 615 | return ErrorAbort(state, "%s: some changes failed", name); |
| 616 | } |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 617 | return StringValue(result); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 620 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 621 | Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 622 | if (argc != 1) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 623 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 624 | } |
| 625 | char* key; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 626 | key = Evaluate(state, argv[0]); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 627 | if (key == NULL) return NULL; |
| 628 | |
| 629 | char value[PROPERTY_VALUE_MAX]; |
| 630 | property_get(key, value, ""); |
| 631 | free(key); |
| 632 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 633 | return StringValue(strdup(value)); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 637 | // file_getprop(file, key) |
| 638 | // |
| 639 | // interprets 'file' as a getprop-style file (key=value pairs, one |
| 640 | // per line, # comment lines and blank lines okay), and returns the value |
| 641 | // for 'key' (or "" if it isn't defined). |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 642 | Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 643 | char* result = NULL; |
| 644 | char* buffer = NULL; |
| 645 | char* filename; |
| 646 | char* key; |
| 647 | if (ReadArgs(state, argv, 2, &filename, &key) < 0) { |
| 648 | return NULL; |
| 649 | } |
| 650 | |
| 651 | struct stat st; |
| 652 | if (stat(filename, &st) < 0) { |
| 653 | ErrorAbort(state, "%s: failed to stat \"%s\": %s", |
| 654 | name, filename, strerror(errno)); |
| 655 | goto done; |
| 656 | } |
| 657 | |
| 658 | #define MAX_FILE_GETPROP_SIZE 65536 |
| 659 | |
| 660 | if (st.st_size > MAX_FILE_GETPROP_SIZE) { |
| 661 | ErrorAbort(state, "%s too large for %s (max %d)", |
| 662 | filename, name, MAX_FILE_GETPROP_SIZE); |
| 663 | goto done; |
| 664 | } |
| 665 | |
| 666 | buffer = malloc(st.st_size+1); |
| 667 | if (buffer == NULL) { |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 668 | 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] | 669 | goto done; |
| 670 | } |
| 671 | |
| 672 | FILE* f = fopen(filename, "rb"); |
| 673 | if (f == NULL) { |
| 674 | ErrorAbort(state, "%s: failed to open %s: %s", |
| 675 | name, filename, strerror(errno)); |
| 676 | goto done; |
| 677 | } |
| 678 | |
| 679 | if (fread(buffer, 1, st.st_size, f) != st.st_size) { |
Doug Zongker | a23075f | 2012-08-06 16:19:09 -0700 | [diff] [blame] | 680 | ErrorAbort(state, "%s: failed to read %lld bytes from %s", |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 681 | name, st.st_size+1, filename); |
| 682 | fclose(f); |
| 683 | goto done; |
| 684 | } |
| 685 | buffer[st.st_size] = '\0'; |
| 686 | |
| 687 | fclose(f); |
| 688 | |
| 689 | char* line = strtok(buffer, "\n"); |
| 690 | do { |
| 691 | // skip whitespace at start of line |
| 692 | while (*line && isspace(*line)) ++line; |
| 693 | |
| 694 | // comment or blank line: skip to next line |
| 695 | if (*line == '\0' || *line == '#') continue; |
| 696 | |
| 697 | char* equal = strchr(line, '='); |
| 698 | if (equal == NULL) { |
| 699 | ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?", |
| 700 | name, line, filename); |
| 701 | goto done; |
| 702 | } |
| 703 | |
| 704 | // trim whitespace between key and '=' |
| 705 | char* key_end = equal-1; |
| 706 | while (key_end > line && isspace(*key_end)) --key_end; |
| 707 | key_end[1] = '\0'; |
| 708 | |
| 709 | // not the key we're looking for |
| 710 | if (strcmp(key, line) != 0) continue; |
| 711 | |
| 712 | // skip whitespace after the '=' to the start of the value |
| 713 | char* val_start = equal+1; |
| 714 | while(*val_start && isspace(*val_start)) ++val_start; |
| 715 | |
| 716 | // trim trailing whitespace |
| 717 | char* val_end = val_start + strlen(val_start)-1; |
| 718 | while (val_end > val_start && isspace(*val_end)) --val_end; |
| 719 | val_end[1] = '\0'; |
| 720 | |
| 721 | result = strdup(val_start); |
| 722 | break; |
| 723 | |
| 724 | } while ((line = strtok(NULL, "\n"))); |
| 725 | |
| 726 | if (result == NULL) result = strdup(""); |
| 727 | |
| 728 | done: |
| 729 | free(filename); |
| 730 | free(key); |
| 731 | free(buffer); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 732 | return StringValue(result); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 736 | static bool write_raw_image_cb(const unsigned char* data, |
| 737 | int data_len, void* ctx) { |
| 738 | int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len); |
| 739 | if (r == data_len) return true; |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 740 | printf("%s\n", strerror(errno)); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 744 | // write_raw_image(filename_or_blob, partition) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 745 | Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 746 | char* result = NULL; |
| 747 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 748 | Value* partition_value; |
| 749 | Value* contents; |
| 750 | if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) { |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 751 | return NULL; |
| 752 | } |
| 753 | |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 754 | char* partition = NULL; |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 755 | if (partition_value->type != VAL_STRING) { |
| 756 | ErrorAbort(state, "partition argument to %s must be string", name); |
| 757 | goto done; |
| 758 | } |
Doug Zongker | daefc1d | 2011-10-31 09:34:15 -0700 | [diff] [blame] | 759 | partition = partition_value->data; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 760 | if (strlen(partition) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 761 | ErrorAbort(state, "partition argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 762 | goto done; |
| 763 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 764 | if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 765 | ErrorAbort(state, "file argument to %s can't be empty", name); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 766 | goto done; |
| 767 | } |
| 768 | |
| 769 | mtd_scan_partitions(); |
| 770 | const MtdPartition* mtd = mtd_find_partition_by_name(partition); |
| 771 | if (mtd == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 772 | printf("%s: no mtd partition named \"%s\"\n", name, partition); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 773 | result = strdup(""); |
| 774 | goto done; |
| 775 | } |
| 776 | |
| 777 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 778 | if (ctx == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 779 | printf("%s: can't write mtd partition \"%s\"\n", |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 780 | name, partition); |
| 781 | result = strdup(""); |
| 782 | goto done; |
| 783 | } |
| 784 | |
| 785 | bool success; |
| 786 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 787 | if (contents->type == VAL_STRING) { |
| 788 | // we're given a filename as the contents |
| 789 | char* filename = contents->data; |
| 790 | FILE* f = fopen(filename, "rb"); |
| 791 | if (f == NULL) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 792 | printf("%s: can't open %s: %s\n", |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 793 | name, filename, strerror(errno)); |
| 794 | result = strdup(""); |
| 795 | goto done; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 796 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 797 | |
| 798 | success = true; |
| 799 | char* buffer = malloc(BUFSIZ); |
| 800 | int read; |
| 801 | while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) { |
| 802 | int wrote = mtd_write_data(ctx, buffer, read); |
| 803 | success = success && (wrote == read); |
| 804 | } |
| 805 | free(buffer); |
| 806 | fclose(f); |
| 807 | } else { |
| 808 | // we're given a blob as the contents |
| 809 | ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size); |
| 810 | success = (wrote == contents->size); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 811 | } |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 812 | if (!success) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 813 | printf("mtd_write_data to %s failed: %s\n", |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 814 | partition, strerror(errno)); |
| 815 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 816 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 817 | if (mtd_erase_blocks(ctx, -1) == -1) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 818 | printf("%s: error erasing blocks of %s\n", name, partition); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 819 | } |
| 820 | if (mtd_write_close(ctx) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 821 | printf("%s: error closing write of %s\n", name, partition); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 824 | printf("%s %s partition\n", |
| 825 | success ? "wrote" : "failed to write", partition); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 826 | |
| 827 | result = success ? partition : strdup(""); |
| 828 | |
| 829 | done: |
Doug Zongker | 179b2d9 | 2011-04-12 15:49:04 -0700 | [diff] [blame] | 830 | if (result != partition) FreeValue(partition_value); |
| 831 | FreeValue(contents); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 832 | return StringValue(result); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 835 | // apply_patch_space(bytes) |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 836 | Value* ApplyPatchSpaceFn(const char* name, State* state, |
| 837 | int argc, Expr* argv[]) { |
| 838 | char* bytes_str; |
| 839 | if (ReadArgs(state, argv, 1, &bytes_str) < 0) { |
| 840 | return NULL; |
| 841 | } |
| 842 | |
| 843 | char* endptr; |
| 844 | size_t bytes = strtol(bytes_str, &endptr, 10); |
| 845 | if (bytes == 0 && endptr == bytes_str) { |
| 846 | ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", |
| 847 | name, bytes_str); |
| 848 | free(bytes_str); |
| 849 | return NULL; |
| 850 | } |
| 851 | |
| 852 | return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t")); |
| 853 | } |
| 854 | |
| 855 | |
| 856 | // apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 857 | Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 858 | if (argc < 6 || (argc % 2) == 1) { |
| 859 | return ErrorAbort(state, "%s(): expected at least 6 args and an " |
| 860 | "even number, got %d", |
| 861 | name, argc); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 864 | char* source_filename; |
| 865 | char* target_filename; |
| 866 | char* target_sha1; |
| 867 | char* target_size_str; |
| 868 | if (ReadArgs(state, argv, 4, &source_filename, &target_filename, |
| 869 | &target_sha1, &target_size_str) < 0) { |
| 870 | return NULL; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 871 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 872 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 873 | char* endptr; |
| 874 | size_t target_size = strtol(target_size_str, &endptr, 10); |
| 875 | if (target_size == 0 && endptr == target_size_str) { |
| 876 | ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", |
| 877 | name, target_size_str); |
| 878 | free(source_filename); |
| 879 | free(target_filename); |
| 880 | free(target_sha1); |
| 881 | free(target_size_str); |
| 882 | return NULL; |
| 883 | } |
| 884 | |
| 885 | int patchcount = (argc-4) / 2; |
| 886 | Value** patches = ReadValueVarArgs(state, argc-4, argv+4); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 887 | |
| 888 | int i; |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 889 | for (i = 0; i < patchcount; ++i) { |
| 890 | if (patches[i*2]->type != VAL_STRING) { |
| 891 | ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i); |
| 892 | break; |
| 893 | } |
| 894 | if (patches[i*2+1]->type != VAL_BLOB) { |
| 895 | ErrorAbort(state, "%s(): patch #%d is not blob", name, i); |
| 896 | break; |
| 897 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 898 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 899 | if (i != patchcount) { |
| 900 | for (i = 0; i < patchcount*2; ++i) { |
| 901 | FreeValue(patches[i]); |
| 902 | } |
| 903 | free(patches); |
| 904 | return NULL; |
| 905 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 906 | |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 907 | char** patch_sha_str = malloc(patchcount * sizeof(char*)); |
| 908 | for (i = 0; i < patchcount; ++i) { |
| 909 | patch_sha_str[i] = patches[i*2]->data; |
| 910 | patches[i*2]->data = NULL; |
| 911 | FreeValue(patches[i*2]); |
| 912 | patches[i] = patches[i*2+1]; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 913 | } |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 914 | |
| 915 | int result = applypatch(source_filename, target_filename, |
| 916 | target_sha1, target_size, |
Doug Zongker | a3ccba6 | 2012-08-20 15:28:02 -0700 | [diff] [blame] | 917 | patchcount, patch_sha_str, patches, NULL); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 918 | |
| 919 | for (i = 0; i < patchcount; ++i) { |
| 920 | FreeValue(patches[i]); |
| 921 | } |
| 922 | free(patch_sha_str); |
| 923 | free(patches); |
| 924 | |
| 925 | return StringValue(strdup(result == 0 ? "t" : "")); |
| 926 | } |
| 927 | |
| 928 | // apply_patch_check(file, [sha1_1, ...]) |
| 929 | Value* ApplyPatchCheckFn(const char* name, State* state, |
| 930 | int argc, Expr* argv[]) { |
| 931 | if (argc < 1) { |
| 932 | return ErrorAbort(state, "%s(): expected at least 1 arg, got %d", |
| 933 | name, argc); |
| 934 | } |
| 935 | |
| 936 | char* filename; |
| 937 | if (ReadArgs(state, argv, 1, &filename) < 0) { |
| 938 | return NULL; |
| 939 | } |
| 940 | |
| 941 | int patchcount = argc-1; |
| 942 | char** sha1s = ReadVarArgs(state, argc-1, argv+1); |
| 943 | |
| 944 | int result = applypatch_check(filename, patchcount, sha1s); |
| 945 | |
| 946 | int i; |
| 947 | for (i = 0; i < patchcount; ++i) { |
| 948 | free(sha1s[i]); |
| 949 | } |
| 950 | free(sha1s); |
| 951 | |
| 952 | return StringValue(strdup(result == 0 ? "t" : "")); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 955 | Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 956 | char** args = ReadVarArgs(state, argc, argv); |
| 957 | if (args == NULL) { |
| 958 | return NULL; |
| 959 | } |
| 960 | |
| 961 | int size = 0; |
| 962 | int i; |
| 963 | for (i = 0; i < argc; ++i) { |
| 964 | size += strlen(args[i]); |
| 965 | } |
| 966 | char* buffer = malloc(size+1); |
| 967 | size = 0; |
| 968 | for (i = 0; i < argc; ++i) { |
| 969 | strcpy(buffer+size, args[i]); |
| 970 | size += strlen(args[i]); |
| 971 | free(args[i]); |
| 972 | } |
| 973 | free(args); |
| 974 | buffer[size] = '\0'; |
| 975 | |
| 976 | char* line = strtok(buffer, "\n"); |
| 977 | while (line) { |
| 978 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, |
| 979 | "ui_print %s\n", line); |
| 980 | line = strtok(NULL, "\n"); |
| 981 | } |
| 982 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n"); |
| 983 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 984 | return StringValue(buffer); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 987 | Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 988 | if (argc != 0) { |
| 989 | return ErrorAbort(state, "%s() expects no args, got %d", name, argc); |
| 990 | } |
| 991 | fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n"); |
| 992 | return StringValue(strdup("t")); |
| 993 | } |
| 994 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 995 | Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) { |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 996 | if (argc < 1) { |
| 997 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 998 | } |
| 999 | char** args = ReadVarArgs(state, argc, argv); |
| 1000 | if (args == NULL) { |
| 1001 | return NULL; |
| 1002 | } |
| 1003 | |
| 1004 | char** args2 = malloc(sizeof(char*) * (argc+1)); |
| 1005 | memcpy(args2, args, sizeof(char*) * argc); |
| 1006 | args2[argc] = NULL; |
| 1007 | |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1008 | printf("about to run program [%s] with %d args\n", args2[0], argc); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1009 | |
| 1010 | pid_t child = fork(); |
| 1011 | if (child == 0) { |
| 1012 | execv(args2[0], args2); |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1013 | printf("run_program: execv failed: %s\n", strerror(errno)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1014 | _exit(1); |
| 1015 | } |
| 1016 | int status; |
| 1017 | waitpid(child, &status, 0); |
| 1018 | if (WIFEXITED(status)) { |
| 1019 | if (WEXITSTATUS(status) != 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1020 | printf("run_program: child exited with status %d\n", |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1021 | WEXITSTATUS(status)); |
| 1022 | } |
| 1023 | } else if (WIFSIGNALED(status)) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1024 | printf("run_program: child terminated by signal %d\n", |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1025 | WTERMSIG(status)); |
| 1026 | } |
| 1027 | |
| 1028 | int i; |
| 1029 | for (i = 0; i < argc; ++i) { |
| 1030 | free(args[i]); |
| 1031 | } |
| 1032 | free(args); |
| 1033 | free(args2); |
| 1034 | |
| 1035 | char buffer[20]; |
| 1036 | sprintf(buffer, "%d", status); |
| 1037 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1038 | return StringValue(strdup(buffer)); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1041 | // Take a sha-1 digest and return it as a newly-allocated hex string. |
| 1042 | static char* PrintSha1(uint8_t* digest) { |
| 1043 | char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1); |
| 1044 | int i; |
| 1045 | const char* alphabet = "0123456789abcdef"; |
| 1046 | for (i = 0; i < SHA_DIGEST_SIZE; ++i) { |
| 1047 | buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf]; |
| 1048 | buffer[i*2+1] = alphabet[digest[i] & 0xf]; |
| 1049 | } |
| 1050 | buffer[i*2] = '\0'; |
| 1051 | return buffer; |
| 1052 | } |
| 1053 | |
| 1054 | // sha1_check(data) |
| 1055 | // to return the sha1 of the data (given in the format returned by |
| 1056 | // read_file). |
| 1057 | // |
| 1058 | // sha1_check(data, sha1_hex, [sha1_hex, ...]) |
| 1059 | // returns the sha1 of the file if it matches any of the hex |
| 1060 | // strings passed, or "" if it does not equal any of them. |
| 1061 | // |
| 1062 | Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1063 | if (argc < 1) { |
| 1064 | return ErrorAbort(state, "%s() expects at least 1 arg", name); |
| 1065 | } |
| 1066 | |
| 1067 | Value** args = ReadValueVarArgs(state, argc, argv); |
| 1068 | if (args == NULL) { |
| 1069 | return NULL; |
| 1070 | } |
| 1071 | |
| 1072 | if (args[0]->size < 0) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1073 | printf("%s(): no file contents received", name); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1074 | return StringValue(strdup("")); |
| 1075 | } |
| 1076 | uint8_t digest[SHA_DIGEST_SIZE]; |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 1077 | SHA_hash(args[0]->data, args[0]->size, digest); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1078 | FreeValue(args[0]); |
| 1079 | |
| 1080 | if (argc == 1) { |
| 1081 | return StringValue(PrintSha1(digest)); |
| 1082 | } |
| 1083 | |
| 1084 | int i; |
| 1085 | uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE); |
| 1086 | for (i = 1; i < argc; ++i) { |
| 1087 | if (args[i]->type != VAL_STRING) { |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1088 | printf("%s(): arg %d is not a string; skipping", |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1089 | name, i); |
| 1090 | } else if (ParseSha1(args[i]->data, arg_digest) != 0) { |
| 1091 | // Warn about bad args and skip them. |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 1092 | printf("%s(): error parsing \"%s\" as sha-1; skipping", |
| 1093 | name, args[i]->data); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1094 | } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) { |
| 1095 | break; |
| 1096 | } |
| 1097 | FreeValue(args[i]); |
| 1098 | } |
| 1099 | if (i >= argc) { |
| 1100 | // Didn't match any of the hex strings; return false. |
| 1101 | return StringValue(strdup("")); |
| 1102 | } |
| 1103 | // Found a match; free all the remaining arguments and return the |
| 1104 | // matched one. |
| 1105 | int j; |
| 1106 | for (j = i+1; j < argc; ++j) { |
| 1107 | FreeValue(args[j]); |
| 1108 | } |
| 1109 | return args[i]; |
| 1110 | } |
| 1111 | |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 1112 | // Read a local file and return its contents (the Value* returned |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1113 | // is actually a FileContents*). |
| 1114 | Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 1115 | if (argc != 1) { |
| 1116 | return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); |
| 1117 | } |
| 1118 | char* filename; |
| 1119 | if (ReadArgs(state, argv, 1, &filename) < 0) return NULL; |
| 1120 | |
| 1121 | Value* v = malloc(sizeof(Value)); |
| 1122 | v->type = VAL_BLOB; |
| 1123 | |
| 1124 | FileContents fc; |
Hristo Bojinov | db314d6 | 2010-08-02 10:29:49 -0700 | [diff] [blame] | 1125 | if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1126 | ErrorAbort(state, "%s() loading \"%s\" failed: %s", |
| 1127 | name, filename, strerror(errno)); |
| 1128 | free(filename); |
| 1129 | free(v); |
| 1130 | free(fc.data); |
| 1131 | return NULL; |
| 1132 | } |
| 1133 | |
| 1134 | v->size = fc.size; |
| 1135 | v->data = (char*)fc.data; |
| 1136 | |
| 1137 | free(filename); |
| 1138 | return v; |
| 1139 | } |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1140 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1141 | void RegisterInstallFunctions() { |
| 1142 | RegisterFunction("mount", MountFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1143 | RegisterFunction("is_mounted", IsMountedFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1144 | RegisterFunction("unmount", UnmountFn); |
| 1145 | RegisterFunction("format", FormatFn); |
| 1146 | RegisterFunction("show_progress", ShowProgressFn); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 1147 | RegisterFunction("set_progress", SetProgressFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1148 | RegisterFunction("delete", DeleteFn); |
| 1149 | RegisterFunction("delete_recursive", DeleteFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1150 | RegisterFunction("package_extract_dir", PackageExtractDirFn); |
| 1151 | RegisterFunction("package_extract_file", PackageExtractFileFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1152 | RegisterFunction("symlink", SymlinkFn); |
| 1153 | RegisterFunction("set_perm", SetPermFn); |
| 1154 | RegisterFunction("set_perm_recursive", SetPermFn); |
Nick Kralevich | 627eb30 | 2013-07-17 19:01:37 -0700 | [diff] [blame] | 1155 | RegisterFunction("set_perm2", SetPermFn); |
| 1156 | RegisterFunction("set_perm2_recursive", SetPermFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1157 | |
| 1158 | RegisterFunction("getprop", GetPropFn); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 1159 | RegisterFunction("file_getprop", FileGetPropFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1160 | RegisterFunction("write_raw_image", WriteRawImageFn); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 1161 | |
| 1162 | RegisterFunction("apply_patch", ApplyPatchFn); |
Doug Zongker | c4351c7 | 2010-02-22 14:46:32 -0800 | [diff] [blame] | 1163 | RegisterFunction("apply_patch_check", ApplyPatchCheckFn); |
| 1164 | RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1165 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 1166 | RegisterFunction("read_file", ReadFileFn); |
| 1167 | RegisterFunction("sha1_check", Sha1CheckFn); |
| 1168 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 1169 | RegisterFunction("wipe_cache", WipeCacheFn); |
| 1170 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 1171 | RegisterFunction("ui_print", UIPrintFn); |
Doug Zongker | a3f89ea | 2009-09-10 14:10:48 -0700 | [diff] [blame] | 1172 | |
| 1173 | RegisterFunction("run_program", RunProgramFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 1174 | } |