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