blob: e52762e9bbd9e6a9c361b9be7abaaaf7b42aaf0f [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001
2#include <stdio.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <string.h>
Doug Zongkerf2c3a832012-05-03 15:55:56 -07006#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08007
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>
16
17/* NOTES
18**
19** - see buffer-format.txt from the linux kernel docs for
20** an explanation of this file format
21** - dotfiles are ignored
22** - directories named 'root' are ignored
23** - device notes, pipes, etc are not supported (error)
24*/
25
26void die(const char *why, ...)
27{
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 Zongkerf2c3a832012-05-03 15:55:56 -070038struct fs_config_entry {
39 char* name;
40 int uid, gid, mode;
41};
42
43static struct fs_config_entry* canned_config = NULL;
Thierry Strudeldf33ffa2015-07-09 09:50:31 -070044static char *target_out_path = NULL;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070045
46/* Each line in the canned file should be a path plus three ints (uid,
47 * gid, mode). */
Doug Zongkera865f6d2012-05-04 16:45:35 -070048#ifdef PATH_MAX
Doug Zongkerf2c3a832012-05-03 15:55:56 -070049#define CANNED_LINE_LENGTH (PATH_MAX+100)
Doug Zongkera865f6d2012-05-04 16:45:35 -070050#else
51#define CANNED_LINE_LENGTH (1024)
52#endif
Doug Zongkerf2c3a832012-05-03 15:55:56 -070053
Tao Bao3b5fbd82016-10-07 10:20:52 -070054#define TRAILER "TRAILER!!!"
55
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056static int verbose = 0;
57static int total_size = 0;
58
59static void fix_stat(const char *path, struct stat *s)
60{
Nick Kraleviche9e74f32013-02-07 14:22:12 -080061 uint64_t capabilities;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070062 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 Salyzyn5ce75752014-05-15 15:05:25 -070084 unsigned st_mode = s->st_mode;
Tao Bao3b5fbd82016-10-07 10:20:52 -070085 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 Salyzyn5ce75752014-05-15 15:05:25 -070087 s->st_mode = (typeof(s->st_mode)) st_mode;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070088 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089}
90
91static 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 Zongkerf2c3a832012-05-03 15:55:56 -0700128 if(strlen(out) != (unsigned int)olen) die("ACK!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129
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
141static void _eject_trailer()
142{
143 struct stat s;
144 memset(&s, 0, sizeof(s));
Tao Bao3b5fbd82016-10-07 10:20:52 -0700145 _eject(&s, TRAILER, 10, 0, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146
147 while(total_size & 0xff) {
148 total_size++;
149 putchar(0);
150 }
151}
152
153static void _archive(char *in, char *out, int ilen, int olen);
154
155static int compare(const void* a, const void* b) {
156 return strcmp(*(const char**)a, *(const char**)b);
157}
158
159static 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 Hughes14e28d32013-10-29 14:12:46 -0700228
229 closedir(d);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230}
231
232static 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
275void archive(const char *start, const char *prefix)
276{
277 char in[8192];
278 char out[8192];
279
280 strcpy(in, start);
281 strcpy(out, prefix);
282
283 _archive_dir(in, out, strlen(in), strlen(out));
284}
285
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700286static void read_canned_config(char* filename)
287{
288 int allocated = 8;
289 int used = 0;
290
291 canned_config =
292 (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry));
293
294 char line[CANNED_LINE_LENGTH];
295 FILE* f = fopen(filename, "r");
296 if (f == NULL) die("failed to open canned file");
297
298 while (fgets(line, CANNED_LINE_LENGTH, f) != NULL) {
299 if (!line[0]) break;
300 if (used >= allocated) {
301 allocated *= 2;
302 canned_config = (struct fs_config_entry*)realloc(
303 canned_config, allocated * sizeof(struct fs_config_entry));
Mikhail Lappo26464912017-03-23 22:17:27 +0100304 if (canned_config == NULL) die("failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700305 }
306
307 struct fs_config_entry* cc = canned_config + used;
308
309 if (isspace(line[0])) {
310 cc->name = strdup("");
311 cc->uid = atoi(strtok(line, " \n"));
312 } else {
313 cc->name = strdup(strtok(line, " \n"));
314 cc->uid = atoi(strtok(NULL, " \n"));
315 }
316 cc->gid = atoi(strtok(NULL, " \n"));
317 cc->mode = strtol(strtok(NULL, " \n"), NULL, 8);
318 ++used;
319 }
320 if (used >= allocated) {
321 ++allocated;
322 canned_config = (struct fs_config_entry*)realloc(
323 canned_config, allocated * sizeof(struct fs_config_entry));
Mikhail Lappo26464912017-03-23 22:17:27 +0100324 if (canned_config == NULL) die("failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700325 }
326 canned_config[used].name = NULL;
327
328 fclose(f);
329}
330
331
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332int main(int argc, char *argv[])
333{
334 argc--;
335 argv++;
336
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700337 if (argc > 1 && strcmp(argv[0], "-d") == 0) {
338 target_out_path = argv[1];
339 argc -= 2;
340 argv += 2;
341 }
342
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700343 if (argc > 1 && strcmp(argv[0], "-f") == 0) {
344 read_canned_config(argv[1]);
345 argc -= 2;
346 argv += 2;
347 }
348
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 if(argc == 0) die("no directories to process?!");
350
351 while(argc-- > 0){
352 char *x = strchr(*argv, '=');
353 if(x != 0) {
354 *x++ = 0;
355 } else {
356 x = "";
357 }
358
359 archive(*argv, x);
360
361 argv++;
362 }
363
364 _eject_trailer();
365
366 return 0;
367}