/
new_builder.c
600 lines (507 loc) · 11.9 KB
/
new_builder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
* Copyright 2015 Joyent, Inc.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <err.h>
#include <strings.h>
#include "users.h"
#include "copyfile.h"
#include "strlist.h"
#include "strset.h"
#include "manifest.h"
#include "custr.h"
#define KILOBYTES 1024
#define MEGABYTES (1024 * KILOBYTES)
#define GIGABYTES (1024 * MEGABYTES)
/*
* Maximum number of input directories to search:
*/
#define MAX_DIRS 32
typedef enum {
BPS_CHECK = 0x1,
BPS_COPY = 0x2
} builder_pass_sets_t;
typedef struct builder_stats {
uint64_t bs_inodes;
uint64_t bs_bytes;
uint64_t bs_cnt_dirs;
uint64_t bs_cnt_files;
uint64_t bs_cnt_symlinks;
uint64_t bs_cnt_hardlinks;
} builder_stats_t;
typedef struct builder {
char *b_passwd_dir;
char *b_output_dir;
char *b_manifest_file;
strset_t *b_paths;
strlist_t *b_search_dirs;
builder_ids_t *b_ids;
boolean_t b_quiet;
char *b_errbuf;
custr_t *b_error;
builder_pass_sets_t b_pass_sets;
builder_stats_t b_stats;
} builder_t;
static void
usage(int status, char *msg)
{
if (msg != NULL) {
(void) fprintf(status == 0 ? stdout : stderr, "%s\n", msg);
}
(void) fprintf(status == 0 ? stdout : stderr,
"Usage: builder -p passwd_dir <manifest_file> <output_dir>\n"
" <input_dir>...\n"
"\n");
exit(status);
}
static void
emit(builder_t *b, const char *fmt, ...)
{
va_list ap;
if (b->b_quiet) {
return;
}
va_start(ap, fmt);
(void) vfprintf(stdout, fmt, ap);
va_end(ap);
}
static void
parse_opts(builder_t *b, int argc, char **argv)
{
int c, i;
char *pflag = NULL;
b->b_pass_sets = BPS_COPY | BPS_CHECK;
while ((c = getopt(argc, argv, ":np:q")) != -1) {
switch (c) {
case 'n':
/*
* Dry-run.
*/
b->b_pass_sets &= ~BPS_COPY;
break;
case 'p':
pflag = optarg;
break;
case 'q':
b->b_quiet = B_TRUE;
break;
case ':':
(void) fprintf(stderr, "Option -%c requires an "
"operand\n", optopt);
usage(1, NULL);
break;
case '?':
(void) fprintf(stderr, "Unrecognised option: -%c\n",
optopt);
usage(1, NULL);
break;
}
}
if (pflag == NULL) {
usage(1, "must specify passwd directory with -p");
} else if ((b->b_passwd_dir = strdup(pflag)) == NULL) {
err(1, "strdup failure");
}
if ((argc - optind) < 3) {
usage(1, "must provide manifest, output directory and input"
"directories");
}
if (argv[optind][0] != '/') {
usage(1, "manifest file must be an absolute path");
} else if ((b->b_manifest_file = strdup(argv[optind])) == NULL) {
err(1, "strdup failure");
}
if (argv[optind + 1][0] != '/') {
usage(1, "output directory must be an absolute path");
} else if ((b->b_output_dir = strdup(argv[optind + 1])) == NULL) {
err(1, "strdup failure");
}
for (i = optind + 2; i < argc; i++) {
if (argv[i][0] != '/') {
usage(1, "input directory must be an absolute path");
}
if (strlist_set_tail(b->b_search_dirs, argv[i]) != 0) {
err(1, "strlist_set_tail failure");
}
}
if (strlist_contig_count(b->b_search_dirs) < 1) {
usage(1, "must provide at least one input directory");
}
}
static int
map_user_and_group(builder_t *b, const char *user, uid_t *u,
const char *group, gid_t *g)
{
int e;
if (uid_from_name(b->b_ids, user, u) != 0) {
if (errno != ENOENT) {
e = errno;
goto fail;
}
(void) custr_append_printf(b->b_error, "user \"%s\" not find "
"in passwd file", user);
errno = ENOENT;
return (-1);
}
if (gid_from_name(b->b_ids, group, g) != 0) {
if (errno != ENOENT) {
e = errno;
goto fail;
}
(void) custr_append_printf(b->b_error, "group \"%s\" not find "
"in group file", group);
errno = ENOENT;
return (-1);
}
return (0);
fail:
(void) custr_append_printf(b->b_error, "id lookup failure: %s",
strerror(errno));
errno = e;
return (-1);
}
static me_cb_ret_t
handle_link_common(manifest_ent_t *me, void *arg, manifest_ent_type_t type)
{
int e;
builder_t *b = arg;
const char *op = NULL;
if (me->me_type != type) {
return (MECB_NEXT);
}
emit(b, "LINK(%s): %s => %s: ",
me->me_type == ME_TYPE_HARDLINK ? "link" : "symlink",
me->me_name, me->me_target);
if ((b->b_pass_sets & BPS_COPY) == 0) {
goto done;
}
op = "unlinking target";
if (unlink(me->me_name) != 0 && errno != ENOENT) {
e = errno;
goto fail;
}
if (me->me_type == ME_TYPE_HARDLINK) {
op = "hardlinking";
if (link(me->me_target, me->me_name) != 0) {
e = errno;
goto fail;
}
} else {
op = "symlinking";
if (symlink(me->me_target, me->me_name) != 0) {
e = errno;
goto fail;
}
}
done:
if (me->me_type == ME_TYPE_HARDLINK) {
b->b_stats.bs_cnt_hardlinks++;
} else {
/*
* Assume a block for each symlink:
*/
b->b_stats.bs_bytes += 512;
b->b_stats.bs_cnt_symlinks++;
b->b_stats.bs_inodes++;
}
emit(b, "OK\n");
return (MECB_NEXT);
fail:
emit(b, "FAILED\n");
(void) custr_append_printf(b->b_error, "%s failed for \"%s\": %s",
op, me->me_name, strerror(errno));
errno = e;
return (MECB_CANCEL);
}
static me_cb_ret_t
handle_hardlink(manifest_ent_t *me, void *arg)
{
return (handle_link_common(me, arg, ME_TYPE_HARDLINK));
}
static me_cb_ret_t
handle_symlink(manifest_ent_t *me, void *arg)
{
return (handle_link_common(me, arg, ME_TYPE_SYMLINK));
}
static uint64_t
round_up(uint64_t input, uint64_t alignment)
{
while (input % alignment != 0) {
input++;
}
return (input);
}
static me_cb_ret_t
handle_file(manifest_ent_t *me, void *arg)
{
int e;
builder_t *b = arg;
const char *op = NULL;
char *p = NULL;
boolean_t found = B_FALSE;
uid_t u;
gid_t g;
struct stat st;
if (me->me_type != ME_TYPE_FILE) {
return (MECB_NEXT);
}
op = "mapping user/group";
if (map_user_and_group(b, me->me_user, &u, me->me_group, &g) != 0) {
e = errno;
goto fail;
}
emit(b, "FILE: [%s][%04o][%s/%d][%s/%d]: ", me->me_name,
(unsigned int)me->me_mode, me->me_user, u, me->me_group, g);
/*
* Look in each search directory and find the first one that this
* file appears in:
*/
op = "locating source file";
for (unsigned int i = 0; i < strlist_contig_count(b->b_search_dirs);
i++) {
free(p);
if (asprintf(&p, "%s/%s", strlist_get(b->b_search_dirs, i),
me->me_name) < 0) {
err(1, "asprintf failure");
}
if (stat(p, &st) == 0 && S_ISREG(st.st_mode)) {
found = B_TRUE;
break;
}
}
if (!found) {
e = ENOENT;
goto fail;
}
if ((b->b_pass_sets & BPS_COPY) == 0) {
goto done;
}
op = "unlinking target";
if (unlink(me->me_name) != 0 && errno != ENOENT) {
e = errno;
goto fail;
}
op = "copying file";
if (builder_copy_file(p, me->me_name) != 0) {
e = errno;
goto fail;
}
op = "chown";
if (chown(me->me_name, u, g) != 0) {
e = errno;
goto fail;
}
op = "chmod";
if (chmod(me->me_name, me->me_mode) != 0) {
e = errno;
goto fail;
}
done:
b->b_stats.bs_cnt_files++;
b->b_stats.bs_inodes++;
b->b_stats.bs_bytes += round_up(st.st_size, 512);
emit(b, "OK (%s)\n", p);
free(p);
return (MECB_NEXT);
fail:
free(p);
emit(b, "FAILED\n");
(void) custr_append_printf(b->b_error, "%s failed for \"%s\": %s",
op, me->me_name, strerror(errno));
errno = e;
return (MECB_CANCEL);
}
static me_cb_ret_t
handle_directory(manifest_ent_t *me, void *arg)
{
int e;
const char *op = NULL;
builder_t *b = arg;
uid_t u;
gid_t g;
if (me->me_type != ME_TYPE_DIRECTORY) {
return (MECB_NEXT);
}
op = "mapping user/group";
if (map_user_and_group(b, me->me_user, &u, me->me_group, &g) != 0) {
e = errno;
goto fail;
}
emit(b, "DIR: [%s][%04o][%s/%d][%s/%d]: ", me->me_name,
(unsigned int)me->me_mode, me->me_user, u, me->me_group, g);
if ((b->b_pass_sets & BPS_COPY) == 0) {
goto done;
}
op = "mkdir";
if (mkdir(me->me_name, me->me_mode) != 0 && errno != EEXIST) {
e = errno;
goto fail;
}
op = "chown";
if (chown(me->me_name, u, g) != 0) {
e = errno;
goto fail;
}
op = "chmod";
if (chmod(me->me_name, me->me_mode) != 0) {
e = errno;
goto fail;
}
done:
/*
* Assume four blocks for each directory:
*/
b->b_stats.bs_bytes += 4096;
b->b_stats.bs_inodes++;
b->b_stats.bs_cnt_dirs++;
emit(b, "OK\n");
return (MECB_NEXT);
fail:
emit(b, "FAILED\n");
(void) custr_append_printf(b->b_error, "%s failed for \"%s\": %s",
op, me->me_name, strerror(errno));
errno = e;
return (MECB_CANCEL);
}
static me_cb_ret_t
sanity_check(manifest_ent_t *me, void *arg)
{
builder_t *b = arg;
uid_t u;
gid_t g;
/*
* Check for duplicate entries in the manifest file.
*/
if (strset_add(b->b_paths, me->me_name) != 0) {
if (errno != EEXIST) {
err(1, "strset_add failure");
}
(void) custr_append_printf(b->b_error, "duplicate entry \"%s\"",
me->me_name);
return (MECB_CANCEL);
}
/*
* Check that all user and group names map to valid entries in the
* shipped /etc/passwd and /etc/group databases.
*/
switch (me->me_type) {
case ME_TYPE_DIRECTORY:
case ME_TYPE_FILE:
if (map_user_and_group(b, me->me_user, &u, me->me_group,
&g) != 0) {
return (MECB_CANCEL);
}
break;
default:
break;
}
return (MECB_NEXT);
}
struct builder_pass {
char *bp_name;
manifest_ent_cb_t *bp_func;
} builder_passes[] = {
{ "checking manifest", sanity_check },
{ "creating directories", handle_directory },
{ "copying files", handle_file },
{ "creating symlinks", handle_symlink },
{ "creating hardlinks", handle_hardlink },
{ NULL, NULL }
};
static void
builder_free(builder_t *b)
{
if (b == NULL) {
return;
}
strlist_free(b->b_search_dirs);
strset_free(b->b_paths);
custr_free(b->b_error);
free(b->b_errbuf);
free(b->b_passwd_dir);
free(b->b_output_dir);
free(b->b_manifest_file);
free(b);
}
static int
builder_alloc(builder_t **bpp)
{
size_t errsz = 2048;
builder_t *b;
if ((b = calloc(1, sizeof (*b))) == NULL) {
*bpp = NULL;
return (-1);
}
if ((b->b_errbuf = calloc(errsz, sizeof (char))) == NULL ||
strlist_alloc(&b->b_search_dirs, MAX_DIRS) != 0 ||
custr_alloc_buf(&b->b_error, b->b_errbuf, errsz) != 0 ||
strset_alloc(&b->b_paths, 0) != 0) {
builder_free(b);
*bpp = NULL;
return (-1);
}
*bpp = b;
return (0);
}
int
main(int argc, char **argv)
{
builder_t *b = NULL;
const char *c;
if (builder_alloc(&b) != 0) {
err(1, "builder_t alloc failure");
}
parse_opts(b, argc, argv);
if ((b->b_pass_sets & BPS_COPY) != 0 && geteuid() != 0) {
errx(1, "must be root to use this tool in wet-run mode");
}
if (builder_ids_init(&b->b_ids, b->b_passwd_dir) != 0) {
err(1, "failed to read passwd/group files");
}
(void) fprintf(stdout, "MANIFEST: %s\n", b->b_manifest_file);
(void) fprintf(stdout, "OUTPUT: %s\n", b->b_output_dir);
for (int i = 0; (c = strlist_get(b->b_search_dirs, i)) != NULL;
i++) {
(void) fprintf(stdout, "SEARCH[%02d]: %s\n", i, c);
}
if (chdir(b->b_output_dir) != 0) {
err(1, "failed to change to output directory (%s)",
b->b_output_dir);
}
for (int i = 0; builder_passes[i].bp_name != NULL; i++) {
struct builder_pass *bp = &builder_passes[i];
(void) fprintf(stdout, "builder pass: %s\n", bp->bp_name);
if (read_manifest_file(b->b_manifest_file, bp->bp_func,
b) != 0) {
if (errno == ECANCELED) {
errx(1, "builder pass \"%s\" failed: %s",
bp->bp_name, custr_cstr(b->b_error));
} else {
err(1, "reading manifest \"%s\" failed",
b->b_manifest_file);
}
}
}
/*
* Print statistics:
*/
(void) fprintf(stdout, "STATS:");
(void) fprintf(stdout, "\tBYTES: %llu\n", b->b_stats.bs_bytes);
(void) fprintf(stdout, "\tMEGABYTES: %llu\n", (b->b_stats.bs_bytes / MEGABYTES));
(void) fprintf(stdout, "\tINODES: %llu\n", b->b_stats.bs_inodes);
(void) fprintf(stdout, "\n");
(void) fprintf(stdout, "\tDIRS: %llu\n", b->b_stats.bs_cnt_dirs);
(void) fprintf(stdout, "\tFILES: %llu\n", b->b_stats.bs_cnt_files);
(void) fprintf(stdout, "\tSYMLINKS: %llu\n", b->b_stats.bs_cnt_symlinks);
(void) fprintf(stdout, "\tHARDLINKS: %llu\n", b->b_stats.bs_cnt_hardlinks);
(void) fprintf(stdout, "\n");
builder_free(b);
return (0);
}