The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <string.h> |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 6 | #include <ctype.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 7 | |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <dirent.h> |
| 11 | |
| 12 | #include <stdarg.h> |
| 13 | #include <fcntl.h> |
| 14 | |
| 15 | #include <private/android_filesystem_config.h> |
Tom Cherry | 6ad4d0a | 2020-03-04 13:35:28 -0800 | [diff] [blame] | 16 | #include <private/fs_config.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | |
| 18 | /* NOTES |
| 19 | ** |
| 20 | ** - see buffer-format.txt from the linux kernel docs for |
| 21 | ** an explanation of this file format |
| 22 | ** - dotfiles are ignored |
| 23 | ** - directories named 'root' are ignored |
| 24 | ** - device notes, pipes, etc are not supported (error) |
| 25 | */ |
| 26 | |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame^] | 27 | static void die(const char* why, ...) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | va_list ap; |
| 29 | |
| 30 | va_start(ap, why); |
| 31 | fprintf(stderr,"error: "); |
| 32 | vfprintf(stderr, why, ap); |
| 33 | fprintf(stderr,"\n"); |
| 34 | va_end(ap); |
| 35 | exit(1); |
| 36 | } |
| 37 | |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 38 | struct fs_config_entry { |
| 39 | char* name; |
| 40 | int uid, gid, mode; |
| 41 | }; |
| 42 | |
| 43 | static struct fs_config_entry* canned_config = NULL; |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame^] | 44 | static const char* target_out_path = NULL; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 45 | |
| 46 | /* Each line in the canned file should be a path plus three ints (uid, |
| 47 | * gid, mode). */ |
Doug Zongker | a865f6d | 2012-05-04 16:45:35 -0700 | [diff] [blame] | 48 | #ifdef PATH_MAX |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 49 | #define CANNED_LINE_LENGTH (PATH_MAX+100) |
Doug Zongker | a865f6d | 2012-05-04 16:45:35 -0700 | [diff] [blame] | 50 | #else |
| 51 | #define CANNED_LINE_LENGTH (1024) |
| 52 | #endif |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 53 | |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 54 | #define TRAILER "TRAILER!!!" |
| 55 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | static int verbose = 0; |
| 57 | static int total_size = 0; |
| 58 | |
| 59 | static void fix_stat(const char *path, struct stat *s) |
| 60 | { |
Nick Kralevich | e9e74f3 | 2013-02-07 14:22:12 -0800 | [diff] [blame] | 61 | uint64_t capabilities; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 62 | if (canned_config) { |
| 63 | // Use the list of file uid/gid/modes loaded from the file |
| 64 | // given with -f. |
| 65 | |
| 66 | struct fs_config_entry* empty_path_config = NULL; |
| 67 | struct fs_config_entry* p; |
| 68 | for (p = canned_config; p->name; ++p) { |
| 69 | if (!p->name[0]) { |
| 70 | empty_path_config = p; |
| 71 | } |
| 72 | if (strcmp(p->name, path) == 0) { |
| 73 | s->st_uid = p->uid; |
| 74 | s->st_gid = p->gid; |
| 75 | s->st_mode = p->mode | (s->st_mode & ~07777); |
| 76 | return; |
| 77 | } |
| 78 | } |
| 79 | s->st_uid = empty_path_config->uid; |
| 80 | s->st_gid = empty_path_config->gid; |
| 81 | s->st_mode = empty_path_config->mode | (s->st_mode & ~07777); |
| 82 | } else { |
| 83 | // Use the compiled-in fs_config() function. |
Mark Salyzyn | 5ce7575 | 2014-05-15 15:05:25 -0700 | [diff] [blame] | 84 | unsigned st_mode = s->st_mode; |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 85 | int is_dir = S_ISDIR(s->st_mode) || strcmp(path, TRAILER) == 0; |
| 86 | fs_config(path, is_dir, target_out_path, &s->st_uid, &s->st_gid, &st_mode, &capabilities); |
Mark Salyzyn | 5ce7575 | 2014-05-15 15:05:25 -0700 | [diff] [blame] | 87 | s->st_mode = (typeof(s->st_mode)) st_mode; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 88 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void _eject(struct stat *s, char *out, int olen, char *data, unsigned datasize) |
| 92 | { |
| 93 | // Nothing is special about this value, just picked something in the |
| 94 | // approximate range that was being used already, and avoiding small |
| 95 | // values which may be special. |
| 96 | static unsigned next_inode = 300000; |
| 97 | |
| 98 | while(total_size & 3) { |
| 99 | total_size++; |
| 100 | putchar(0); |
| 101 | } |
| 102 | |
| 103 | fix_stat(out, s); |
| 104 | // fprintf(stderr, "_eject %s: mode=0%o\n", out, s->st_mode); |
| 105 | |
| 106 | printf("%06x%08x%08x%08x%08x%08x%08x" |
| 107 | "%08x%08x%08x%08x%08x%08x%08x%s%c", |
| 108 | 0x070701, |
| 109 | next_inode++, // s.st_ino, |
| 110 | s->st_mode, |
| 111 | 0, // s.st_uid, |
| 112 | 0, // s.st_gid, |
| 113 | 1, // s.st_nlink, |
| 114 | 0, // s.st_mtime, |
| 115 | datasize, |
| 116 | 0, // volmajor |
| 117 | 0, // volminor |
| 118 | 0, // devmajor |
| 119 | 0, // devminor, |
| 120 | olen + 1, |
| 121 | 0, |
| 122 | out, |
| 123 | 0 |
| 124 | ); |
| 125 | |
| 126 | total_size += 6 + 8*13 + olen + 1; |
| 127 | |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 128 | if(strlen(out) != (unsigned int)olen) die("ACK!"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | |
| 130 | while(total_size & 3) { |
| 131 | total_size++; |
| 132 | putchar(0); |
| 133 | } |
| 134 | |
| 135 | if(datasize) { |
| 136 | fwrite(data, datasize, 1, stdout); |
| 137 | total_size += datasize; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static void _eject_trailer() |
| 142 | { |
| 143 | struct stat s; |
| 144 | memset(&s, 0, sizeof(s)); |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 145 | _eject(&s, TRAILER, 10, 0, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | |
| 147 | while(total_size & 0xff) { |
| 148 | total_size++; |
| 149 | putchar(0); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static void _archive(char *in, char *out, int ilen, int olen); |
| 154 | |
| 155 | static int compare(const void* a, const void* b) { |
| 156 | return strcmp(*(const char**)a, *(const char**)b); |
| 157 | } |
| 158 | |
| 159 | static void _archive_dir(char *in, char *out, int ilen, int olen) |
| 160 | { |
| 161 | int i, t; |
| 162 | DIR *d; |
| 163 | struct dirent *de; |
| 164 | |
| 165 | if(verbose) { |
| 166 | fprintf(stderr,"_archive_dir('%s','%s',%d,%d)\n", |
| 167 | in, out, ilen, olen); |
| 168 | } |
| 169 | |
| 170 | d = opendir(in); |
| 171 | if(d == 0) die("cannot open directory '%s'", in); |
| 172 | |
| 173 | int size = 32; |
| 174 | int entries = 0; |
| 175 | char** names = malloc(size * sizeof(char*)); |
| 176 | if (names == NULL) { |
| 177 | fprintf(stderr, "failed to allocate dir names array (size %d)\n", size); |
| 178 | exit(1); |
| 179 | } |
| 180 | |
| 181 | while((de = readdir(d)) != 0){ |
| 182 | /* xxx: feature? maybe some dotfiles are okay */ |
| 183 | if(de->d_name[0] == '.') continue; |
| 184 | |
| 185 | /* xxx: hack. use a real exclude list */ |
| 186 | if(!strcmp(de->d_name, "root")) continue; |
| 187 | |
| 188 | if (entries >= size) { |
| 189 | size *= 2; |
| 190 | names = realloc(names, size * sizeof(char*)); |
| 191 | if (names == NULL) { |
| 192 | fprintf(stderr, "failed to reallocate dir names array (size %d)\n", |
| 193 | size); |
| 194 | exit(1); |
| 195 | } |
| 196 | } |
| 197 | names[entries] = strdup(de->d_name); |
| 198 | if (names[entries] == NULL) { |
| 199 | fprintf(stderr, "failed to strdup name \"%s\"\n", |
| 200 | de->d_name); |
| 201 | exit(1); |
| 202 | } |
| 203 | ++entries; |
| 204 | } |
| 205 | |
| 206 | qsort(names, entries, sizeof(char*), compare); |
| 207 | |
| 208 | for (i = 0; i < entries; ++i) { |
| 209 | t = strlen(names[i]); |
| 210 | in[ilen] = '/'; |
| 211 | memcpy(in + ilen + 1, names[i], t + 1); |
| 212 | |
| 213 | if(olen > 0) { |
| 214 | out[olen] = '/'; |
| 215 | memcpy(out + olen + 1, names[i], t + 1); |
| 216 | _archive(in, out, ilen + t + 1, olen + t + 1); |
| 217 | } else { |
| 218 | memcpy(out, names[i], t + 1); |
| 219 | _archive(in, out, ilen + t + 1, t); |
| 220 | } |
| 221 | |
| 222 | in[ilen] = 0; |
| 223 | out[olen] = 0; |
| 224 | |
| 225 | free(names[i]); |
| 226 | } |
| 227 | free(names); |
Elliott Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 228 | |
| 229 | closedir(d); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static void _archive(char *in, char *out, int ilen, int olen) |
| 233 | { |
| 234 | struct stat s; |
| 235 | |
| 236 | if(verbose) { |
| 237 | fprintf(stderr,"_archive('%s','%s',%d,%d)\n", |
| 238 | in, out, ilen, olen); |
| 239 | } |
| 240 | |
| 241 | if(lstat(in, &s)) die("could not stat '%s'\n", in); |
| 242 | |
| 243 | if(S_ISREG(s.st_mode)){ |
| 244 | char *tmp; |
| 245 | int fd; |
| 246 | |
| 247 | fd = open(in, O_RDONLY); |
| 248 | if(fd < 0) die("cannot open '%s' for read", in); |
| 249 | |
| 250 | tmp = (char*) malloc(s.st_size); |
| 251 | if(tmp == 0) die("cannot allocate %d bytes", s.st_size); |
| 252 | |
| 253 | if(read(fd, tmp, s.st_size) != s.st_size) { |
| 254 | die("cannot read %d bytes", s.st_size); |
| 255 | } |
| 256 | |
| 257 | _eject(&s, out, olen, tmp, s.st_size); |
| 258 | |
| 259 | free(tmp); |
| 260 | close(fd); |
| 261 | } else if(S_ISDIR(s.st_mode)) { |
| 262 | _eject(&s, out, olen, 0, 0); |
| 263 | _archive_dir(in, out, ilen, olen); |
| 264 | } else if(S_ISLNK(s.st_mode)) { |
| 265 | char buf[1024]; |
| 266 | int size; |
| 267 | size = readlink(in, buf, 1024); |
| 268 | if(size < 0) die("cannot read symlink '%s'", in); |
| 269 | _eject(&s, out, olen, buf, size); |
| 270 | } else { |
| 271 | die("Unknown '%s' (mode %d)?\n", in, s.st_mode); |
| 272 | } |
| 273 | } |
| 274 | |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame^] | 275 | static void archive(const char* start, const char* prefix) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 276 | char in[8192]; |
| 277 | char out[8192]; |
| 278 | |
| 279 | strcpy(in, start); |
| 280 | strcpy(out, prefix); |
| 281 | |
| 282 | _archive_dir(in, out, strlen(in), strlen(out)); |
| 283 | } |
| 284 | |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 285 | static void read_canned_config(char* filename) |
| 286 | { |
| 287 | int allocated = 8; |
| 288 | int used = 0; |
| 289 | |
| 290 | canned_config = |
| 291 | (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry)); |
| 292 | |
| 293 | char line[CANNED_LINE_LENGTH]; |
| 294 | FILE* f = fopen(filename, "r"); |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame^] | 295 | if (f == NULL) die("failed to open canned file '%s'", filename); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 296 | |
| 297 | while (fgets(line, CANNED_LINE_LENGTH, f) != NULL) { |
| 298 | if (!line[0]) break; |
| 299 | if (used >= allocated) { |
| 300 | allocated *= 2; |
| 301 | canned_config = (struct fs_config_entry*)realloc( |
| 302 | canned_config, allocated * sizeof(struct fs_config_entry)); |
Mikhail Lappo | 2646491 | 2017-03-23 22:17:27 +0100 | [diff] [blame] | 303 | if (canned_config == NULL) die("failed to reallocate memory"); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | struct fs_config_entry* cc = canned_config + used; |
| 307 | |
| 308 | if (isspace(line[0])) { |
| 309 | cc->name = strdup(""); |
| 310 | cc->uid = atoi(strtok(line, " \n")); |
| 311 | } else { |
| 312 | cc->name = strdup(strtok(line, " \n")); |
| 313 | cc->uid = atoi(strtok(NULL, " \n")); |
| 314 | } |
| 315 | cc->gid = atoi(strtok(NULL, " \n")); |
| 316 | cc->mode = strtol(strtok(NULL, " \n"), NULL, 8); |
| 317 | ++used; |
| 318 | } |
| 319 | if (used >= allocated) { |
| 320 | ++allocated; |
| 321 | canned_config = (struct fs_config_entry*)realloc( |
| 322 | canned_config, allocated * sizeof(struct fs_config_entry)); |
Mikhail Lappo | 2646491 | 2017-03-23 22:17:27 +0100 | [diff] [blame] | 323 | if (canned_config == NULL) die("failed to reallocate memory"); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 324 | } |
| 325 | canned_config[used].name = NULL; |
| 326 | |
| 327 | fclose(f); |
| 328 | } |
| 329 | |
| 330 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 331 | int main(int argc, char *argv[]) |
| 332 | { |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame^] | 333 | if (argc == 1) { |
| 334 | fprintf(stderr, |
| 335 | "usage: %s [-d TARGET_OUTPUT_PATH] [-f CANNED_CONFIGURATION_PATH] DIRECTORIES...\n", |
| 336 | argv[0]); |
| 337 | exit(1); |
| 338 | } |
| 339 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 340 | argc--; |
| 341 | argv++; |
| 342 | |
Thierry Strudel | df33ffa | 2015-07-09 09:50:31 -0700 | [diff] [blame] | 343 | if (argc > 1 && strcmp(argv[0], "-d") == 0) { |
| 344 | target_out_path = argv[1]; |
| 345 | argc -= 2; |
| 346 | argv += 2; |
| 347 | } |
| 348 | |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 349 | if (argc > 1 && strcmp(argv[0], "-f") == 0) { |
| 350 | read_canned_config(argv[1]); |
| 351 | argc -= 2; |
| 352 | argv += 2; |
| 353 | } |
| 354 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 355 | if(argc == 0) die("no directories to process?!"); |
| 356 | |
| 357 | while(argc-- > 0){ |
| 358 | char *x = strchr(*argv, '='); |
| 359 | if(x != 0) { |
| 360 | *x++ = 0; |
| 361 | } else { |
| 362 | x = ""; |
| 363 | } |
| 364 | |
| 365 | archive(*argv, x); |
| 366 | |
| 367 | argv++; |
| 368 | } |
| 369 | |
| 370 | _eject_trailer(); |
| 371 | |
| 372 | return 0; |
| 373 | } |