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