makefs.c revision 1.53 1 /* $NetBSD: makefs.c,v 1.53 2015/11/27 15:10:32 joerg 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.53 2015/11/27 15:10:32 joerg 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
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:M: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 'M':
191 fsoptions.minsize =
192 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
193 break;
194
195 case 'N':
196 if (! setup_getid(optarg))
197 errx(1,
198 "Unable to use user and group databases in `%s'",
199 optarg);
200 break;
201
202 case 'm':
203 fsoptions.maxsize =
204 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
205 break;
206
207 case 'O':
208 fsoptions.offset =
209 strsuftoll("offset", optarg, 0LL, LLONG_MAX);
210 break;
211
212 case 'o':
213 {
214 char *p;
215
216 while ((p = strsep(&optarg, ",")) != NULL) {
217 if (*p == '\0')
218 errx(1, "Empty option");
219 if (! fstype->parse_options(p, &fsoptions))
220 usage(fstype, &fsoptions);
221 }
222 break;
223 }
224
225 case 'r':
226 fsoptions.replace = 1;
227 break;
228
229 case 's':
230 fsoptions.minsize = fsoptions.maxsize =
231 strsuftoll("size", optarg, 1LL, LLONG_MAX);
232 break;
233
234 case 'S':
235 fsoptions.sectorsize =
236 (int)strsuftoll("sector size", optarg,
237 1LL, INT_MAX);
238 break;
239
240 case 't':
241 /* Check current one and cleanup if necessary. */
242 if (fstype->cleanup_options)
243 fstype->cleanup_options(&fsoptions);
244 fsoptions.fs_specific = NULL;
245 if ((fstype = get_fstype(optarg)) == NULL)
246 errx(1, "Unknown fs type `%s'.", optarg);
247 fstype->prepare_options(&fsoptions);
248 break;
249
250 case 'T':
251 if (get_tstamp(optarg, &stampst) == -1)
252 errx(1, "Cannot get timestamp from `%s'",
253 optarg);
254 break;
255
256 case 'x':
257 fsoptions.onlyspec = 1;
258 break;
259
260 case 'Z':
261 fsoptions.sparse = 1;
262 break;
263
264 case '?':
265 default:
266 usage(fstype, &fsoptions);
267 /* NOTREACHED */
268
269 }
270 }
271 if (debug) {
272 printf("debug mask: 0x%08x\n", debug);
273 printf("start time: %ld.%ld, %s",
274 (long)start_time.tv_sec, (long)start_time.tv_nsec,
275 ctime(&start_time.tv_sec));
276 }
277 argc -= optind;
278 argv += optind;
279
280 if (argc < 2)
281 usage(fstype, &fsoptions);
282
283 /* -x must be accompanied by -F */
284 if (fsoptions.onlyspec != 0 && specfile == NULL)
285 errx(1, "-x requires -F mtree-specfile.");
286
287 /* walk the tree */
288 TIMER_START(start);
289 root = walk_dir(argv[1], ".", NULL, NULL, fsoptions.replace);
290 TIMER_RESULTS(start, "walk_dir");
291
292 /* append extra directory */
293 for (i = 2; i < argc; i++) {
294 struct stat sb;
295 if (stat(argv[i], &sb) == -1)
296 err(1, "Can't stat `%s'", argv[i]);
297 if (!S_ISDIR(sb.st_mode))
298 errx(1, "%s: not a directory", argv[i]);
299 TIMER_START(start);
300 root = walk_dir(argv[i], ".", NULL, root, fsoptions.replace);
301 TIMER_RESULTS(start, "walk_dir2");
302 }
303
304 if (specfile) { /* apply a specfile */
305 TIMER_START(start);
306 apply_specfile(specfile, argv[1], root, fsoptions.onlyspec);
307 TIMER_RESULTS(start, "apply_specfile");
308 }
309
310 if (debug & DEBUG_DUMP_FSNODES) {
311 printf("\nparent: %s\n", argv[1]);
312 dump_fsnodes(root);
313 putchar('\n');
314 }
315
316 /* build the file system */
317 TIMER_START(start);
318 fstype->make_fs(argv[0], argv[1], root, &fsoptions);
319 TIMER_RESULTS(start, "make_fs");
320
321 free_fsnodes(root);
322
323 exit(0);
324 /* NOTREACHED */
325 }
326
327 int
328 set_option(const option_t *options, const char *option, char *buf, size_t len)
329 {
330 char *var, *val;
331 int retval;
332
333 assert(option != NULL);
334
335 var = estrdup(option);
336 for (val = var; *val; val++)
337 if (*val == '=') {
338 *val++ = '\0';
339 break;
340 }
341 retval = set_option_var(options, var, val, buf, len);
342 free(var);
343 return retval;
344 }
345
346 int
347 set_option_var(const option_t *options, const char *var, const char *val,
348 char *buf, size_t len)
349 {
350 char *s;
351 size_t i;
352
353 #define NUM(type) \
354 if (!*val) { \
355 *(type *)options[i].value = 1; \
356 break; \
357 } \
358 *(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \
359 options[i].minimum, options[i].maximum); break
360
361 for (i = 0; options[i].name != NULL; i++) {
362 if (var[1] == '\0') {
363 if (options[i].letter != var[0])
364 continue;
365 } else if (strcmp(options[i].name, var) != 0)
366 continue;
367 switch (options[i].type) {
368 case OPT_BOOL:
369 *(bool *)options[i].value = 1;
370 break;
371 case OPT_STRARRAY:
372 strlcpy((void *)options[i].value, val, (size_t)
373 options[i].maximum);
374 break;
375 case OPT_STRPTR:
376 s = estrdup(val);
377 *(char **)options[i].value = s;
378 break;
379 case OPT_STRBUF:
380 if (buf == NULL)
381 abort();
382 strlcpy(buf, val, len);
383 break;
384 case OPT_INT64:
385 NUM(uint64_t);
386 case OPT_INT32:
387 NUM(uint32_t);
388 case OPT_INT16:
389 NUM(uint16_t);
390 case OPT_INT8:
391 NUM(uint8_t);
392 default:
393 warnx("Unknown type %d in option %s", options[i].type,
394 val);
395 return 0;
396 }
397 return i;
398 }
399 warnx("Unknown option `%s'", var);
400 return -1;
401 }
402
403
404 static fstype_t *
405 get_fstype(const char *type)
406 {
407 int i;
408
409 for (i = 0; fstypes[i].type != NULL; i++)
410 if (strcmp(fstypes[i].type, type) == 0)
411 return (&fstypes[i]);
412 return (NULL);
413 }
414
415 option_t *
416 copy_opts(const option_t *o)
417 {
418 size_t i;
419 for (i = 0; o[i].name; i++)
420 continue;
421 i++;
422 return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
423 }
424
425 static int
426 get_tstamp(const char *b, struct stat *st)
427 {
428 time_t when;
429 char *eb;
430 long long l;
431
432 if (stat(b, st) != -1)
433 return 0;
434
435 #ifndef HAVE_NBTOOL_CONFIG_H
436 errno = 0;
437 if ((when = parsedate(b, NULL, NULL)) == -1 && errno != 0)
438 #endif
439 {
440 errno = 0;
441 l = strtoll(b, &eb, 0);
442 if (b == eb || *eb || errno)
443 return -1;
444 when = (time_t)l;
445 }
446
447 st->st_ino = 1;
448 #if HAVE_STRUCT_STAT_BIRTHTIME
449 st->st_birthtime =
450 #endif
451 st->st_mtime = st->st_ctime = st->st_atime = when;
452 return 0;
453 }
454
455 static void
456 usage(fstype_t *fstype, fsinfo_t *fsoptions)
457 {
458 const char *prog;
459
460 prog = getprogname();
461 fprintf(stderr,
462 "Usage: %s [-rxZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
463 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n"
464 "\t[-N userdb-dir] [-O offset] [-o fs-options] [-S sector-size]\n"
465 "\t[-s image-size] [-T <timestamp/file>] [-t fs-type]"
466 " image-file directory [extra-directory ...]\n",
467 prog);
468
469 if (fstype) {
470 size_t i;
471 option_t *o = fsoptions->fs_options;
472
473 fprintf(stderr, "\n%s specific options:\n", fstype->type);
474 for (i = 0; o[i].name != NULL; i++)
475 fprintf(stderr, "\t%c%c%20.20s\t%s\n",
476 o[i].letter ? o[i].letter : ' ',
477 o[i].letter ? ',' : ' ',
478 o[i].name, o[i].desc);
479 }
480 exit(1);
481 }
482