1 /* $NetBSD: makefs.c,v 1.55 2022/04/09 10:05:35 riastradh 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.55 2022/04/09 10:05:35 riastradh 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
96 int
main(int argc,char * argv[])97 main(int argc, char *argv[])
98 {
99 struct timeval start;
100 fstype_t *fstype;
101 fsinfo_t fsoptions;
102 fsnode *root;
103 int ch, i, len;
104 char *specfile;
105
106 setprogname(argv[0]);
107
108 debug = 0;
109 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
110 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
111
112 /* set default fsoptions */
113 (void)memset(&fsoptions, 0, sizeof(fsoptions));
114 fsoptions.fd = -1;
115 fsoptions.sectorsize = -1;
116
117 if (fstype->prepare_options)
118 fstype->prepare_options(&fsoptions);
119
120 specfile = NULL;
121 #ifdef CLOCK_REALTIME
122 ch = clock_gettime(CLOCK_REALTIME, &start_time);
123 #else
124 ch = gettimeofday(&start, NULL);
125 start_time.tv_sec = start.tv_sec;
126 start_time.tv_nsec = start.tv_usec * 1000;
127 #endif
128 if (ch == -1)
129 err(1, "Unable to get system time");
130
131
132 while ((ch = getopt(argc, argv, "B:b:d:f:F:LM:m:N:O:o:rs:S:t:T:xZ")) != -1) {
133 switch (ch) {
134
135 case 'B':
136 if (strcmp(optarg, "be") == 0 ||
137 strcmp(optarg, "4321") == 0 ||
138 strcmp(optarg, "big") == 0) {
139 #if BYTE_ORDER == LITTLE_ENDIAN
140 fsoptions.needswap = 1;
141 #endif
142 } else if (strcmp(optarg, "le") == 0 ||
143 strcmp(optarg, "1234") == 0 ||
144 strcmp(optarg, "little") == 0) {
145 #if BYTE_ORDER == BIG_ENDIAN
146 fsoptions.needswap = 1;
147 #endif
148 } else {
149 warnx("Invalid endian `%s'.", optarg);
150 usage(fstype, &fsoptions);
151 }
152 break;
153
154 case 'b':
155 len = strlen(optarg) - 1;
156 if (optarg[len] == '%') {
157 optarg[len] = '\0';
158 fsoptions.freeblockpc =
159 strsuftoll("free block percentage",
160 optarg, 0, 99);
161 } else {
162 fsoptions.freeblocks =
163 strsuftoll("free blocks",
164 optarg, 0, LLONG_MAX);
165 }
166 break;
167
168 case 'd':
169 debug = strtoll(optarg, NULL, 0);
170 break;
171
172 case 'f':
173 len = strlen(optarg) - 1;
174 if (optarg[len] == '%') {
175 optarg[len] = '\0';
176 fsoptions.freefilepc =
177 strsuftoll("free file percentage",
178 optarg, 0, 99);
179 } else {
180 fsoptions.freefiles =
181 strsuftoll("free files",
182 optarg, 0, LLONG_MAX);
183 }
184 break;
185
186 case 'F':
187 specfile = optarg;
188 break;
189
190 case 'L':
191 fsoptions.follow = true;
192 break;
193
194 case 'M':
195 fsoptions.minsize =
196 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
197 break;
198
199 case 'N':
200 if (! setup_getid(optarg))
201 errx(1,
202 "Unable to use user and group databases in `%s'",
203 optarg);
204 break;
205
206 case 'm':
207 fsoptions.maxsize =
208 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
209 break;
210
211 case 'O':
212 fsoptions.offset =
213 strsuftoll("offset", optarg, 0LL, LLONG_MAX);
214 break;
215
216 case 'o':
217 {
218 char *p;
219
220 while ((p = strsep(&optarg, ",")) != NULL) {
221 if (*p == '\0')
222 errx(1, "Empty option");
223 if (! fstype->parse_options(p, &fsoptions))
224 usage(fstype, &fsoptions);
225 }
226 break;
227 }
228
229 case 'r':
230 fsoptions.replace = 1;
231 break;
232
233 case 's':
234 fsoptions.minsize = fsoptions.maxsize =
235 strsuftoll("size", optarg, 1LL, LLONG_MAX);
236 break;
237
238 case 'S':
239 fsoptions.sectorsize =
240 (int)strsuftoll("sector size", optarg,
241 1LL, INT_MAX);
242 break;
243
244 case 't':
245 /* Check current one and cleanup if necessary. */
246 if (fstype->cleanup_options)
247 fstype->cleanup_options(&fsoptions);
248 fsoptions.fs_specific = NULL;
249 if ((fstype = get_fstype(optarg)) == NULL)
250 errx(1, "Unknown fs type `%s'.", optarg);
251 fstype->prepare_options(&fsoptions);
252 break;
253
254 case 'T':
255 if (get_tstamp(optarg, &stampst) == -1)
256 errx(1, "Cannot get timestamp from `%s'",
257 optarg);
258 break;
259
260 case 'x':
261 fsoptions.onlyspec = 1;
262 break;
263
264 case 'Z':
265 fsoptions.sparse = 1;
266 break;
267
268 case '?':
269 default:
270 usage(fstype, &fsoptions);
271 /* NOTREACHED */
272
273 }
274 }
275 if (debug) {
276 printf("debug mask: 0x%08x\n", debug);
277 printf("start time: %ld.%ld, %s",
278 (long)start_time.tv_sec, (long)start_time.tv_nsec,
279 ctime(&start_time.tv_sec));
280 }
281 argc -= optind;
282 argv += optind;
283
284 if (argc < 2)
285 usage(fstype, &fsoptions);
286
287 /* -x must be accompanied by -F */
288 if (fsoptions.onlyspec != 0 && specfile == NULL)
289 errx(1, "-x requires -F mtree-specfile.");
290
291 /* walk the tree */
292 TIMER_START(start);
293 root = walk_dir(argv[1], ".", NULL, NULL, fsoptions.replace,
294 fsoptions.follow);
295 TIMER_RESULTS(start, "walk_dir");
296
297 /* append extra directory */
298 for (i = 2; i < argc; i++) {
299 struct stat sb;
300 if (stat(argv[i], &sb) == -1)
301 err(1, "Can't stat `%s'", argv[i]);
302 if (!S_ISDIR(sb.st_mode))
303 errx(1, "%s: not a directory", argv[i]);
304 TIMER_START(start);
305 root = walk_dir(argv[i], ".", NULL, root, fsoptions.replace,
306 fsoptions.follow);
307 TIMER_RESULTS(start, "walk_dir2");
308 }
309
310 if (specfile) { /* apply a specfile */
311 TIMER_START(start);
312 apply_specfile(specfile, argv[1], root, fsoptions.onlyspec);
313 TIMER_RESULTS(start, "apply_specfile");
314 }
315
316 if (debug & DEBUG_DUMP_FSNODES) {
317 printf("\nparent: %s\n", argv[1]);
318 dump_fsnodes(root);
319 putchar('\n');
320 }
321
322 /* build the file system */
323 TIMER_START(start);
324 fstype->make_fs(argv[0], argv[1], root, &fsoptions);
325 TIMER_RESULTS(start, "make_fs");
326
327 free_fsnodes(root);
328
329 exit(0);
330 /* NOTREACHED */
331 }
332
333 int
set_option(const option_t * options,const char * option,char * buf,size_t len)334 set_option(const option_t *options, const char *option, char *buf, size_t len)
335 {
336 char *var, *val;
337 int retval;
338
339 assert(option != NULL);
340
341 var = estrdup(option);
342 for (val = var; *val; val++)
343 if (*val == '=') {
344 *val++ = '\0';
345 break;
346 }
347 retval = set_option_var(options, var, val, buf, len);
348 free(var);
349 return retval;
350 }
351
352 int
set_option_var(const option_t * options,const char * var,const char * val,char * buf,size_t len)353 set_option_var(const option_t *options, const char *var, const char *val,
354 char *buf, size_t len)
355 {
356 char *s;
357 size_t i;
358
359 #define NUM(type) \
360 if (!*val) { \
361 *(type *)options[i].value = 1; \
362 break; \
363 } \
364 *(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \
365 options[i].minimum, options[i].maximum); break
366
367 for (i = 0; options[i].name != NULL; i++) {
368 if (var[1] == '\0') {
369 if (options[i].letter != var[0])
370 continue;
371 } else if (strcmp(options[i].name, var) != 0)
372 continue;
373 switch (options[i].type) {
374 case OPT_BOOL:
375 *(bool *)options[i].value = 1;
376 break;
377 case OPT_STRARRAY:
378 strlcpy((void *)options[i].value, val, (size_t)
379 options[i].maximum);
380 break;
381 case OPT_STRPTR:
382 s = estrdup(val);
383 *(char **)options[i].value = s;
384 break;
385 case OPT_STRBUF:
386 if (buf == NULL)
387 abort();
388 strlcpy(buf, val, len);
389 break;
390 case OPT_INT64:
391 NUM(uint64_t);
392 case OPT_INT32:
393 NUM(uint32_t);
394 case OPT_INT16:
395 NUM(uint16_t);
396 case OPT_INT8:
397 NUM(uint8_t);
398 default:
399 warnx("Unknown type %d in option %s", options[i].type,
400 val);
401 return 0;
402 }
403 return i;
404 }
405 warnx("Unknown option `%s'", var);
406 return -1;
407 }
408
409
410 static fstype_t *
get_fstype(const char * type)411 get_fstype(const char *type)
412 {
413 int i;
414
415 for (i = 0; fstypes[i].type != NULL; i++)
416 if (strcmp(fstypes[i].type, type) == 0)
417 return (&fstypes[i]);
418 return (NULL);
419 }
420
421 option_t *
copy_opts(const option_t * o)422 copy_opts(const option_t *o)
423 {
424 size_t i;
425 for (i = 0; o[i].name; i++)
426 continue;
427 i++;
428 return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
429 }
430
431 static int
get_tstamp(const char * b,struct stat * st)432 get_tstamp(const char *b, struct stat *st)
433 {
434 time_t when;
435 char *eb;
436 long long l;
437
438 if (stat(b, st) != -1)
439 return 0;
440
441 #ifndef HAVE_NBTOOL_CONFIG_H
442 errno = 0;
443 if ((when = parsedate(b, NULL, NULL)) == -1 && errno != 0)
444 #endif
445 {
446 errno = 0;
447 l = strtoll(b, &eb, 0);
448 if (b == eb || *eb || errno)
449 return -1;
450 when = (time_t)l;
451 }
452
453 st->st_ino = 1;
454 #if HAVE_STRUCT_STAT_BIRTHTIME
455 st->st_birthtime =
456 #endif
457 st->st_mtime = st->st_ctime = st->st_atime = when;
458 return 0;
459 }
460
461 static void
usage(fstype_t * fstype,fsinfo_t * fsoptions)462 usage(fstype_t *fstype, fsinfo_t *fsoptions)
463 {
464 const char *prog;
465
466 prog = getprogname();
467 fprintf(stderr,
468 "Usage: %s [-rxZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
469 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n"
470 "\t[-N userdb-dir] [-O offset] [-o fs-options] [-S sector-size]\n"
471 "\t[-s image-size] [-T <timestamp/file>] [-t fs-type]"
472 " image-file directory [extra-directory ...]\n",
473 prog);
474
475 if (fstype) {
476 size_t i;
477 option_t *o = fsoptions->fs_options;
478
479 fprintf(stderr, "\n%s specific options:\n", fstype->type);
480 for (i = 0; o[i].name != NULL; i++)
481 fprintf(stderr, "\t%c%c%20.20s\t%s\n",
482 o[i].letter ? o[i].letter : ' ',
483 o[i].letter ? ',' : ' ',
484 o[i].name, o[i].desc);
485 }
486 exit(1);
487 }
488