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