makefs.c revision 1.41 1 /* $NetBSD: makefs.c,v 1.41 2013/01/28 21:03:27 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.41 2013/01/28 21:03:27 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 { .type = NULL },
85 };
86
87 u_int debug;
88 struct timespec start_time;
89
90 static fstype_t *get_fstype(const char *);
91 static void usage(fstype_t *, fsinfo_t *) __dead;
92
93 int
94 main(int argc, char *argv[])
95 {
96 struct timeval start;
97 fstype_t *fstype;
98 fsinfo_t fsoptions;
99 fsnode *root;
100 int ch, i, len;
101 char *specfile;
102
103 setprogname(argv[0]);
104
105 debug = 0;
106 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
107 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
108
109 /* set default fsoptions */
110 (void)memset(&fsoptions, 0, sizeof(fsoptions));
111 fsoptions.fd = -1;
112 fsoptions.sectorsize = -1;
113
114 if (fstype->prepare_options)
115 fstype->prepare_options(&fsoptions);
116
117 specfile = NULL;
118 if (gettimeofday(&start, NULL) == -1)
119 err(1, "Unable to get system time");
120
121 start_time.tv_sec = start.tv_sec;
122 start_time.tv_nsec = start.tv_usec * 1000;
123
124 while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:s:S:t:xZ")) != -1) {
125 switch (ch) {
126
127 case 'B':
128 if (strcmp(optarg, "be") == 0 ||
129 strcmp(optarg, "4321") == 0 ||
130 strcmp(optarg, "big") == 0) {
131 #if BYTE_ORDER == LITTLE_ENDIAN
132 fsoptions.needswap = 1;
133 #endif
134 } else if (strcmp(optarg, "le") == 0 ||
135 strcmp(optarg, "1234") == 0 ||
136 strcmp(optarg, "little") == 0) {
137 #if BYTE_ORDER == BIG_ENDIAN
138 fsoptions.needswap = 1;
139 #endif
140 } else {
141 warnx("Invalid endian `%s'.", optarg);
142 usage(fstype, &fsoptions);
143 }
144 break;
145
146 case 'b':
147 len = strlen(optarg) - 1;
148 if (optarg[len] == '%') {
149 optarg[len] = '\0';
150 fsoptions.freeblockpc =
151 strsuftoll("free block percentage",
152 optarg, 0, 99);
153 } else {
154 fsoptions.freeblocks =
155 strsuftoll("free blocks",
156 optarg, 0, LLONG_MAX);
157 }
158 break;
159
160 case 'd':
161 debug = strtoll(optarg, NULL, 0);
162 break;
163
164 case 'f':
165 len = strlen(optarg) - 1;
166 if (optarg[len] == '%') {
167 optarg[len] = '\0';
168 fsoptions.freefilepc =
169 strsuftoll("free file percentage",
170 optarg, 0, 99);
171 } else {
172 fsoptions.freefiles =
173 strsuftoll("free files",
174 optarg, 0, LLONG_MAX);
175 }
176 break;
177
178 case 'F':
179 specfile = optarg;
180 break;
181
182 case 'M':
183 fsoptions.minsize =
184 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
185 break;
186
187 case 'N':
188 if (! setup_getid(optarg))
189 errx(1,
190 "Unable to use user and group databases in `%s'",
191 optarg);
192 break;
193
194 case 'm':
195 fsoptions.maxsize =
196 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
197 break;
198
199 case 'o':
200 {
201 char *p;
202
203 while ((p = strsep(&optarg, ",")) != NULL) {
204 if (*p == '\0')
205 errx(1, "Empty option");
206 if (! fstype->parse_options(p, &fsoptions))
207 usage(fstype, &fsoptions);
208 }
209 break;
210 }
211
212 case 's':
213 fsoptions.minsize = fsoptions.maxsize =
214 strsuftoll("size", optarg, 1LL, LLONG_MAX);
215 break;
216
217 case 'S':
218 fsoptions.sectorsize =
219 (int)strsuftoll("sector size", optarg,
220 1LL, INT_MAX);
221 break;
222
223 case 't':
224 /* Check current one and cleanup if necessary. */
225 if (fstype->cleanup_options)
226 fstype->cleanup_options(&fsoptions);
227 fsoptions.fs_specific = NULL;
228 if ((fstype = get_fstype(optarg)) == NULL)
229 errx(1, "Unknown fs type `%s'.", optarg);
230 fstype->prepare_options(&fsoptions);
231 break;
232
233 case 'x':
234 fsoptions.onlyspec = 1;
235 break;
236
237 case 'Z':
238 fsoptions.sparse = 1;
239 break;
240
241 case '?':
242 default:
243 usage(fstype, &fsoptions);
244 /* NOTREACHED */
245
246 }
247 }
248 if (debug) {
249 printf("debug mask: 0x%08x\n", debug);
250 printf("start time: %ld.%ld, %s",
251 (long)start_time.tv_sec, (long)start_time.tv_nsec,
252 ctime(&start_time.tv_sec));
253 }
254 argc -= optind;
255 argv += optind;
256
257 if (argc < 2)
258 usage(fstype, &fsoptions);
259
260 /* -x must be accompanied by -F */
261 if (fsoptions.onlyspec != 0 && specfile == NULL)
262 errx(1, "-x requires -F mtree-specfile.");
263
264 /* walk the tree */
265 TIMER_START(start);
266 root = walk_dir(argv[1], ".", NULL, NULL);
267 TIMER_RESULTS(start, "walk_dir");
268
269 /* append extra directory */
270 for (i = 2; i < argc; i++) {
271 struct stat sb;
272 if (stat(argv[i], &sb) == -1)
273 err(1, "Can't stat `%s'", argv[i]);
274 if (!S_ISDIR(sb.st_mode))
275 errx(1, "%s: not a directory", argv[i]);
276 TIMER_START(start);
277 root = walk_dir(argv[i], ".", NULL, root);
278 TIMER_RESULTS(start, "walk_dir2");
279 }
280
281 if (specfile) { /* apply a specfile */
282 TIMER_START(start);
283 apply_specfile(specfile, argv[1], root, fsoptions.onlyspec);
284 TIMER_RESULTS(start, "apply_specfile");
285 }
286
287 if (debug & DEBUG_DUMP_FSNODES) {
288 printf("\nparent: %s\n", argv[1]);
289 dump_fsnodes(root);
290 putchar('\n');
291 }
292
293 /* build the file system */
294 TIMER_START(start);
295 fstype->make_fs(argv[0], argv[1], root, &fsoptions);
296 TIMER_RESULTS(start, "make_fs");
297
298 free_fsnodes(root);
299
300 exit(0);
301 /* NOTREACHED */
302 }
303
304 int
305 set_option(const option_t *options, const char *option)
306 {
307 char *var, *val;
308 int retval;
309
310 assert(option != NULL);
311
312 var = estrdup(option);
313 retval = -1;
314 if ((val = strchr(var, '=')) == NULL) {
315 warnx("Option `%s' doesn't contain a value", var);
316 goto out;
317 }
318 *val++ = '\0';
319
320 retval = set_option_var(options, var, val);
321
322 out:
323 free(var);
324 return retval;
325 }
326
327 int
328 set_option_var(const option_t *options, const char *var, const char *val)
329 {
330 char *s;
331 size_t i;
332
333 #define NUM(width) *(uint ## width ## _t *)options[i].value = \
334 (uint ## width ## _t)strsuftoll(options[i].desc, val, \
335 options[i].minimum, options[i].maximum); break
336
337 for (i = 0; options[i].name != NULL; i++) {
338 if (var[1] == '\0') {
339 if (options[i].letter != var[0])
340 continue;
341 } else if (strcmp(options[i].name, var) != 0)
342 continue;
343 switch (options[i].type) {
344 case OPT_BOOL:
345 *(bool *)options[i].value = 1;
346 break;
347 case OPT_STRARRAY:
348 strlcpy((void *)options[i].value, val, (size_t)
349 options[i].maximum);
350 break;
351 case OPT_STRPTR:
352 s = estrdup(val);
353 err(1, NULL);
354 *(char **)options[i].value = s;
355 break;
356
357 case OPT_INT64:
358 NUM(64);
359 case OPT_INT32:
360 NUM(32);
361 case OPT_INT16:
362 NUM(16);
363 case OPT_INT8:
364 NUM(8);
365 default:
366 warnx("Unknown type %d in option %s", options[i].type,
367 val);
368 return 0;
369 }
370 return i;
371 }
372 warnx("Unknown option `%s'", var);
373 return -1;
374 }
375
376
377 static fstype_t *
378 get_fstype(const char *type)
379 {
380 int i;
381
382 for (i = 0; fstypes[i].type != NULL; i++)
383 if (strcmp(fstypes[i].type, type) == 0)
384 return (&fstypes[i]);
385 return (NULL);
386 }
387
388 option_t *
389 copy_opts(const option_t *o)
390 {
391 size_t i;
392 for (i = 0; o[i].name; i++)
393 continue;
394 i++;
395 return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
396 }
397
398 static void
399 usage(fstype_t *fstype, fsinfo_t *fsoptions)
400 {
401 const char *prog;
402
403 prog = getprogname();
404 fprintf(stderr,
405 "Usage: %s [-xZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
406 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size]\n"
407 "\t[-m maximum-size] [-N userdb-dir] [-o fs-options] [-S sector-size]\n"
408 "\t[-s image-size] [-t fs-type] image-file directory [extra-directory ...]\n",
409 prog);
410
411 if (fstype) {
412 size_t i;
413 option_t *o = fsoptions->fs_options;
414
415 fprintf(stderr, "\n%s specific options:\n", fstype->type);
416 for (i = 0; o[i].name != NULL; i++)
417 fprintf(stderr, "\t%c,%20.20s\t%s\n", o[i].letter,
418 o[i].name, o[i].desc);
419 }
420 exit(1);
421 }
422