makefs.c revision 1.59 1 /* $NetBSD: makefs.c,v 1.59 2024/10/27 18:35:52 christos Exp $ */
2
3 /*
4 * Copyright (c) 2001-2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Luke Mewburn for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #if HAVE_NBTOOL_CONFIG_H
39 #include "nbtool_config.h"
40 #endif
41
42 #include <sys/cdefs.h>
43 #if defined(__RCSID) && !defined(__lint)
44 __RCSID("$NetBSD: makefs.c,v 1.59 2024/10/27 18:35:52 christos Exp $");
45 #endif /* !__lint */
46
47 #include <assert.h>
48 #include <ctype.h>
49 #include <errno.h>
50 #include <limits.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <stdbool.h>
56 #include <util.h>
57
58 #include "makefs.h"
59 #include "mtree.h"
60 #include "cd9660.h"
61
62 /*
63 * list of supported file systems and dispatch functions
64 */
65 typedef struct {
66 const char *type;
67 void (*prepare_options)(fsinfo_t *);
68 int (*parse_options)(const char *, fsinfo_t *);
69 void (*cleanup_options)(fsinfo_t *);
70 void (*make_fs)(const char *, const char *, fsnode *,
71 fsinfo_t *);
72 } fstype_t;
73
74 static fstype_t fstypes[] = {
75 #define ENTRY(name) { \
76 # name, name ## _prep_opts, name ## _parse_opts, \
77 name ## _cleanup_opts, name ## _makefs \
78 }
79 ENTRY(ffs),
80 ENTRY(cd9660),
81 ENTRY(chfs),
82 ENTRY(v7fs),
83 ENTRY(msdos),
84 ENTRY(udf),
85 { .type = NULL },
86 };
87
88 u_int debug;
89 struct timespec start_time;
90 struct stat stampst;
91
92 static fstype_t *get_fstype(const char *);
93 static int get_tstamp(const char *, struct stat *);
94 static void usage(fstype_t *, fsinfo_t *) __dead;
95 static u_int parse_debug(char *);
96
97 int
98 main(int argc, char *argv[])
99 {
100 struct timeval start;
101 fstype_t *fstype;
102 fsinfo_t fsoptions;
103 fsnode *root;
104 int ch, i;
105 size_t len;
106 char *specfile;
107
108 setprogname(argv[0]);
109
110 debug = 0;
111 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
112 errx(EXIT_FAILURE,
113 "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
114
115 /* set default fsoptions */
116 (void)memset(&fsoptions, 0, sizeof(fsoptions));
117 fsoptions.fd = -1;
118 fsoptions.sectorsize = -1;
119
120 if (fstype->prepare_options)
121 fstype->prepare_options(&fsoptions);
122
123 specfile = NULL;
124 #ifdef CLOCK_REALTIME
125 ch = clock_gettime(CLOCK_REALTIME, &start_time);
126 #else
127 ch = gettimeofday(&start, NULL);
128 start_time.tv_sec = start.tv_sec;
129 start_time.tv_nsec = start.tv_usec * 1000;
130 #endif
131 if (ch == -1)
132 err(EXIT_FAILURE, "Unable to get system time");
133
134
135 while ((ch = getopt(argc, argv, "B:b:d:f:F:LM:m:N:O:o:rs:S:t:T:xZ")) != -1) {
136 switch (ch) {
137
138 case 'B':
139 if (strcmp(optarg, "be") == 0 ||
140 strcmp(optarg, "4321") == 0 ||
141 strcmp(optarg, "big") == 0) {
142 #if BYTE_ORDER == LITTLE_ENDIAN
143 fsoptions.needswap = 1;
144 #endif
145 } else if (strcmp(optarg, "le") == 0 ||
146 strcmp(optarg, "1234") == 0 ||
147 strcmp(optarg, "little") == 0) {
148 #if BYTE_ORDER == BIG_ENDIAN
149 fsoptions.needswap = 1;
150 #endif
151 } else {
152 warnx("Invalid endian `%s'.", optarg);
153 usage(fstype, &fsoptions);
154 }
155 break;
156
157 case 'b':
158 len = strlen(optarg) - 1;
159 if (optarg[len] == '%') {
160 optarg[len] = '\0';
161 fsoptions.freeblockpc = (int)
162 strsuftoll("free block percentage",
163 optarg, 0, 99);
164 } else {
165 fsoptions.freeblocks =
166 strsuftoll("free blocks",
167 optarg, 0, LLONG_MAX);
168 }
169 break;
170
171 case 'd':
172 debug = parse_debug(optarg);
173 break;
174
175 case 'f':
176 len = strlen(optarg) - 1;
177 if (optarg[len] == '%') {
178 optarg[len] = '\0';
179 fsoptions.freefilepc = (int)
180 strsuftoll("free file percentage",
181 optarg, 0, 99);
182 } else {
183 fsoptions.freefiles =
184 strsuftoll("free files",
185 optarg, 0, LLONG_MAX);
186 }
187 break;
188
189 case 'F':
190 specfile = optarg;
191 break;
192
193 case 'L':
194 fsoptions.follow = true;
195 break;
196
197 case 'M':
198 fsoptions.minsize =
199 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
200 break;
201
202 case 'N':
203 if (! setup_getid(optarg))
204 errx(EXIT_FAILURE,
205 "Unable to use user and group databases in `%s'",
206 optarg);
207 break;
208
209 case 'm':
210 fsoptions.maxsize =
211 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
212 break;
213
214 case 'O':
215 fsoptions.offset =
216 strsuftoll("offset", optarg, 0LL, LLONG_MAX);
217 break;
218
219 case 'o':
220 {
221 char *p;
222
223 while ((p = strsep(&optarg, ",")) != NULL) {
224 if (*p == '\0')
225 errx(EXIT_FAILURE, "Empty option");
226 if (! fstype->parse_options(p, &fsoptions))
227 usage(fstype, &fsoptions);
228 }
229 break;
230 }
231
232 case 'r':
233 fsoptions.replace = 1;
234 break;
235
236 case 's':
237 fsoptions.minsize = fsoptions.maxsize =
238 strsuftoll("size", optarg, 1LL, LLONG_MAX);
239 break;
240
241 case 'S':
242 fsoptions.sectorsize =
243 (int)strsuftoll("sector size", optarg,
244 1LL, INT_MAX);
245 break;
246
247 case 't':
248 /* Check current one and cleanup if necessary. */
249 if (fstype->cleanup_options)
250 fstype->cleanup_options(&fsoptions);
251 fsoptions.fs_specific = NULL;
252 if ((fstype = get_fstype(optarg)) == NULL)
253 errx(EXIT_FAILURE,
254 "Unknown fs type `%s'.", optarg);
255 fstype->prepare_options(&fsoptions);
256 break;
257
258 case 'T':
259 if (get_tstamp(optarg, &stampst) == -1)
260 errx(EXIT_FAILURE,
261 "Cannot get timestamp from `%s'", optarg);
262 break;
263
264 case 'x':
265 fsoptions.onlyspec++;
266 break;
267
268 case 'Z':
269 fsoptions.sparse = 1;
270 break;
271
272 case '?':
273 default:
274 usage(fstype, &fsoptions);
275 /* NOTREACHED */
276
277 }
278 }
279 if (debug) {
280 printf("debug mask: 0x%08x\n", debug);
281 printf("start time: %ld.%ld, %s",
282 (long)start_time.tv_sec, (long)start_time.tv_nsec,
283 ctime(&start_time.tv_sec));
284 }
285 argc -= optind;
286 argv += optind;
287
288 if (argc < 2)
289 usage(fstype, &fsoptions);
290
291 /* -x must be accompanied by -F */
292 if (fsoptions.onlyspec != 0 && specfile == NULL)
293 errx(EXIT_FAILURE, "-x requires -F mtree-specfile.");
294
295 /* walk the tree */
296 TIMER_START(start);
297 root = walk_dir(argv[1], ".", NULL, NULL, fsoptions.replace,
298 fsoptions.follow);
299 TIMER_RESULTS(start, "walk_dir");
300
301 /* append extra directory */
302 for (i = 2; i < argc; i++) {
303 struct stat sb;
304 if (stat(argv[i], &sb) == -1)
305 err(EXIT_FAILURE, "Can't stat `%s'", argv[i]);
306 if (!S_ISDIR(sb.st_mode))
307 errx(EXIT_FAILURE, "%s: not a directory", argv[i]);
308 TIMER_START(start);
309 root = walk_dir(argv[i], ".", NULL, root, fsoptions.replace,
310 fsoptions.follow);
311 TIMER_RESULTS(start, "walk_dir2");
312 }
313
314 if (specfile) { /* apply a specfile */
315 TIMER_START(start);
316 apply_specfile(specfile, argv[1], root, fsoptions.onlyspec);
317 TIMER_RESULTS(start, "apply_specfile");
318 }
319
320 if (debug & DEBUG_DUMP_FSNODES) {
321 printf("\nparent: %s\n", argv[1]);
322 dump_fsnodes(root);
323 putchar('\n');
324 }
325
326 /* build the file system */
327 TIMER_START(start);
328 fstype->make_fs(argv[0], argv[1], root, &fsoptions);
329 TIMER_RESULTS(start, "make_fs");
330
331 free_fsnodes(root);
332
333 exit(EXIT_SUCCESS);
334 /* NOTREACHED */
335 }
336
337 int
338 set_option(const option_t *options, const char *option, char *buf, size_t len)
339 {
340 char *var, *val;
341 int retval;
342
343 assert(option != NULL);
344
345 var = estrdup(option);
346 for (val = var; *val; val++)
347 if (*val == '=') {
348 *val++ = '\0';
349 break;
350 }
351 retval = set_option_var(options, var, val, buf, len);
352 free(var);
353 return retval;
354 }
355
356 void
357 print_options(FILE *fp, const option_t *options)
358 {
359 for (size_t i = 0; options[i].name != NULL; i++) {
360 fprintf(fp, "%s=", options[i].name);
361 switch (options[i].type) {
362 case OPT_BOOL:
363 fputs(*(bool *)options[i].value ? "true\n" : "false\n",
364 fp);
365 break;
366 case OPT_STRARRAY:
367 case OPT_STRPTR:
368 case OPT_STRBUF:
369 fprintf(fp, "%s\n", *(const char **)options[i].value);
370 break;
371 case OPT_INT64:
372 fprintf(fp, "%" PRIu64 "\n",
373 *(uint64_t *)options[i].value);
374 break;
375 case OPT_INT32:
376 fprintf(fp, "%" PRIu32 "\n",
377 *(uint32_t *)options[i].value);
378 break;
379 case OPT_INT16:
380 fprintf(fp, "%" PRIu16 "\n",
381 *(uint16_t *)options[i].value);
382 break;
383 case OPT_INT8:
384 fprintf(fp, "%" PRIu8 "\n",
385 *(uint8_t *)options[i].value);
386 break;
387 default:
388 warnx("Unknown type %d in option %s", options[i].type,
389 options[i].name);
390 return;
391 }
392 }
393 }
394
395 int
396 set_option_var(const option_t *options, const char *var, const char *val,
397 char *buf, size_t len)
398 {
399 char *s;
400 size_t i;
401
402 #define NUM(type) \
403 if (!*val) { \
404 *(type *)options[i].value = 1; \
405 break; \
406 } \
407 *(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \
408 options[i].minimum, options[i].maximum); break
409
410 for (i = 0; options[i].name != NULL; i++) {
411 if (var[1] == '\0') {
412 if (options[i].letter != var[0])
413 continue;
414 } else if (strcmp(options[i].name, var) != 0)
415 continue;
416 switch (options[i].type) {
417 case OPT_BOOL:
418 *(bool *)options[i].value = 1;
419 break;
420 case OPT_STRARRAY:
421 strlcpy((void *)options[i].value, val, (size_t)
422 options[i].maximum);
423 break;
424 case OPT_STRPTR:
425 s = estrdup(val);
426 *(char **)options[i].value = s;
427 break;
428 case OPT_STRBUF:
429 if (buf == NULL)
430 abort();
431 strlcpy(buf, val, len);
432 break;
433 case OPT_INT64:
434 NUM(uint64_t);
435 case OPT_INT32:
436 NUM(uint32_t);
437 case OPT_INT16:
438 NUM(uint16_t);
439 case OPT_INT8:
440 NUM(uint8_t);
441 default:
442 warnx("Unknown type %d in option %s", options[i].type,
443 val);
444 return 0;
445 }
446 return (int)i;
447 }
448 warnx("Unknown option `%s'", var);
449 return -1;
450 }
451
452
453 static fstype_t *
454 get_fstype(const char *type)
455 {
456 int i;
457
458 for (i = 0; fstypes[i].type != NULL; i++)
459 if (strcmp(fstypes[i].type, type) == 0)
460 return (&fstypes[i]);
461 return (NULL);
462 }
463
464 option_t *
465 copy_opts(const option_t *o)
466 {
467 size_t i;
468 for (i = 0; o[i].name; i++)
469 continue;
470 i++;
471 return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
472 }
473
474 static int
475 get_tstamp(const char *b, struct stat *st)
476 {
477 time_t when;
478 char *eb;
479 long long l;
480
481 if (stat(b, st) != -1)
482 return 0;
483
484 #ifndef HAVE_NBTOOL_CONFIG_H
485 errno = 0;
486 if ((when = parsedate(b, NULL, NULL)) == -1 && errno != 0)
487 #endif
488 {
489 errno = 0;
490 l = strtoll(b, &eb, 0);
491 if (b == eb || *eb || errno)
492 return -1;
493 when = (time_t)l;
494 }
495
496 st->st_ino = 1;
497 #if HAVE_STRUCT_STAT_BIRTHTIME
498 st->st_birthtime =
499 #endif
500 st->st_mtime = st->st_ctime = st->st_atime = when;
501 return 0;
502 }
503
504 static struct {
505 const char *n;
506 u_int v;
507 } nv[] = {
508 DEBUG_STRINGS
509 };
510
511 static void
512 usage(fstype_t *fstype, fsinfo_t *fsoptions)
513 {
514 const char *prog;
515
516 prog = getprogname();
517 fprintf(stderr,
518 "Usage: %s [-rxZ] [-B endian] [-b free-blocks] [-d debug-mask|comma-separated-option]\n"
519 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n"
520 "\t[-N userdb-dir] [-O offset] [-o fs-options] [-S sector-size]\n"
521 "\t[-s image-size] [-T <timestamp/file>] [-t fs-type]"
522 " image-file directory [extra-directory ...]\n",
523 prog);
524
525 fprintf(stderr, "\nDebugging options:\n");
526 for (size_t i = 0; i < __arraycount(nv); i++)
527 fprintf(stderr, "\t0x%8.8x\t%s\n", nv[i].v, nv[i].n);
528
529 if (fstype) {
530 size_t i;
531 option_t *o = fsoptions->fs_options;
532
533 fprintf(stderr, "\n%s specific options:\n", fstype->type);
534 for (i = 0; o[i].name != NULL; i++)
535 fprintf(stderr, "\t%c%c%20.20s\t%s\n",
536 o[i].letter ? o[i].letter : ' ',
537 o[i].letter ? ',' : ' ',
538 o[i].name, o[i].desc);
539 }
540 exit(EXIT_FAILURE);
541 }
542
543
544 static u_int
545 parse_debug(char *str)
546 {
547 char *ep;
548 u_int d;
549 size_t i;
550
551 errno = 0;
552 d = (u_int)strtoul(str, &ep, 0);
553 if (str != ep && !*ep && errno == 0)
554 return d;
555 d = 0;
556 for (char *a = strtok(str, ","); a != NULL; a = strtok(NULL, ",")) {
557 for (i = 0; i < __arraycount(nv); i++)
558 if (strcmp(nv[i].n, a) == 0) {
559 d |= nv[i].v;
560 break;
561 }
562 if (i == __arraycount(nv))
563 errx(EXIT_FAILURE, "Unknown debug option `%s'", a);
564 }
565 return d;
566 }
567
568