makefs.c revision 1.37 1 1.37 christos /* $NetBSD: makefs.c,v 1.37 2013/01/23 21:32:32 christos Exp $ */
2 1.1 lukem
3 1.1 lukem /*
4 1.15 lukem * Copyright (c) 2001-2003 Wasabi Systems, Inc.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * Written by Luke Mewburn for Wasabi Systems, Inc.
8 1.1 lukem *
9 1.1 lukem * Redistribution and use in source and binary forms, with or without
10 1.1 lukem * modification, are permitted provided that the following conditions
11 1.1 lukem * are met:
12 1.1 lukem * 1. Redistributions of source code must retain the above copyright
13 1.1 lukem * notice, this list of conditions and the following disclaimer.
14 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 lukem * notice, this list of conditions and the following disclaimer in the
16 1.1 lukem * documentation and/or other materials provided with the distribution.
17 1.1 lukem * 3. All advertising materials mentioning features or use of this software
18 1.1 lukem * must display the following acknowledgement:
19 1.1 lukem * This product includes software developed for the NetBSD Project by
20 1.1 lukem * Wasabi Systems, Inc.
21 1.1 lukem * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 lukem * or promote products derived from this software without specific prior
23 1.1 lukem * written permission.
24 1.1 lukem *
25 1.1 lukem * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 lukem * POSSIBILITY OF SUCH DAMAGE.
36 1.1 lukem */
37 1.2 lukem
38 1.20 jmc #if HAVE_NBTOOL_CONFIG_H
39 1.20 jmc #include "nbtool_config.h"
40 1.20 jmc #endif
41 1.20 jmc
42 1.2 lukem #include <sys/cdefs.h>
43 1.13 tv #if defined(__RCSID) && !defined(__lint)
44 1.37 christos __RCSID("$NetBSD: makefs.c,v 1.37 2013/01/23 21:32:32 christos Exp $");
45 1.2 lukem #endif /* !__lint */
46 1.10 is
47 1.1 lukem #include <assert.h>
48 1.1 lukem #include <ctype.h>
49 1.1 lukem #include <errno.h>
50 1.5 simonb #include <limits.h>
51 1.1 lukem #include <stdio.h>
52 1.1 lukem #include <stdlib.h>
53 1.1 lukem #include <string.h>
54 1.1 lukem #include <unistd.h>
55 1.36 christos #include <stdbool.h>
56 1.1 lukem
57 1.1 lukem #include "makefs.h"
58 1.11 lukem #include "mtree.h"
59 1.22 fvdl #include "cd9660.h"
60 1.1 lukem
61 1.1 lukem /*
62 1.1 lukem * list of supported file systems and dispatch functions
63 1.1 lukem */
64 1.1 lukem typedef struct {
65 1.1 lukem const char *type;
66 1.21 jmc void (*prepare_options)(fsinfo_t *);
67 1.1 lukem int (*parse_options)(const char *, fsinfo_t *);
68 1.21 jmc void (*cleanup_options)(fsinfo_t *);
69 1.1 lukem void (*make_fs)(const char *, const char *, fsnode *,
70 1.1 lukem fsinfo_t *);
71 1.1 lukem } fstype_t;
72 1.1 lukem
73 1.1 lukem static fstype_t fstypes[] = {
74 1.36 christos #define ENTRY(name) { \
75 1.36 christos # name, name ## _prep_opts, name ## _parse_opts, \
76 1.36 christos name ## _cleanup_opts, name ## _makefs \
77 1.36 christos }
78 1.36 christos ENTRY(ffs),
79 1.36 christos ENTRY(cd9660),
80 1.36 christos ENTRY(chfs),
81 1.36 christos ENTRY(v7fs),
82 1.36 christos ENTRY(msdos),
83 1.26 christos { .type = NULL },
84 1.1 lukem };
85 1.1 lukem
86 1.20 jmc u_int debug;
87 1.1 lukem struct timespec start_time;
88 1.1 lukem
89 1.1 lukem static fstype_t *get_fstype(const char *);
90 1.34 joerg static void usage(void) __dead;
91 1.1 lukem
92 1.1 lukem int
93 1.1 lukem main(int argc, char *argv[])
94 1.1 lukem {
95 1.1 lukem struct timeval start;
96 1.1 lukem fstype_t *fstype;
97 1.1 lukem fsinfo_t fsoptions;
98 1.1 lukem fsnode *root;
99 1.31 christos int ch, i, len;
100 1.1 lukem char *specfile;
101 1.8 lukem
102 1.8 lukem setprogname(argv[0]);
103 1.1 lukem
104 1.1 lukem debug = 0;
105 1.1 lukem if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
106 1.1 lukem errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
107 1.9 lukem
108 1.9 lukem /* set default fsoptions */
109 1.1 lukem (void)memset(&fsoptions, 0, sizeof(fsoptions));
110 1.1 lukem fsoptions.fd = -1;
111 1.9 lukem fsoptions.sectorsize = -1;
112 1.21 jmc
113 1.21 jmc if (fstype->prepare_options)
114 1.21 jmc fstype->prepare_options(&fsoptions);
115 1.9 lukem
116 1.1 lukem specfile = NULL;
117 1.1 lukem if (gettimeofday(&start, NULL) == -1)
118 1.1 lukem err(1, "Unable to get system time");
119 1.13 tv
120 1.13 tv start_time.tv_sec = start.tv_sec;
121 1.13 tv start_time.tv_nsec = start.tv_usec * 1000;
122 1.1 lukem
123 1.35 sjg while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:s:S:t:xZ")) != -1) {
124 1.1 lukem switch (ch) {
125 1.1 lukem
126 1.1 lukem case 'B':
127 1.1 lukem if (strcmp(optarg, "be") == 0 ||
128 1.15 lukem strcmp(optarg, "4321") == 0 ||
129 1.1 lukem strcmp(optarg, "big") == 0) {
130 1.1 lukem #if BYTE_ORDER == LITTLE_ENDIAN
131 1.1 lukem fsoptions.needswap = 1;
132 1.1 lukem #endif
133 1.1 lukem } else if (strcmp(optarg, "le") == 0 ||
134 1.15 lukem strcmp(optarg, "1234") == 0 ||
135 1.1 lukem strcmp(optarg, "little") == 0) {
136 1.1 lukem #if BYTE_ORDER == BIG_ENDIAN
137 1.1 lukem fsoptions.needswap = 1;
138 1.1 lukem #endif
139 1.1 lukem } else {
140 1.1 lukem warnx("Invalid endian `%s'.", optarg);
141 1.1 lukem usage();
142 1.1 lukem }
143 1.1 lukem break;
144 1.1 lukem
145 1.1 lukem case 'b':
146 1.1 lukem len = strlen(optarg) - 1;
147 1.1 lukem if (optarg[len] == '%') {
148 1.1 lukem optarg[len] = '\0';
149 1.1 lukem fsoptions.freeblockpc =
150 1.14 lukem strsuftoll("free block percentage",
151 1.6 lukem optarg, 0, 99);
152 1.1 lukem } else {
153 1.6 lukem fsoptions.freeblocks =
154 1.14 lukem strsuftoll("free blocks",
155 1.6 lukem optarg, 0, LLONG_MAX);
156 1.1 lukem }
157 1.1 lukem break;
158 1.1 lukem
159 1.1 lukem case 'd':
160 1.23 dbj debug = strtoll(optarg, NULL, 0);
161 1.1 lukem break;
162 1.1 lukem
163 1.1 lukem case 'f':
164 1.1 lukem len = strlen(optarg) - 1;
165 1.1 lukem if (optarg[len] == '%') {
166 1.1 lukem optarg[len] = '\0';
167 1.1 lukem fsoptions.freefilepc =
168 1.14 lukem strsuftoll("free file percentage",
169 1.6 lukem optarg, 0, 99);
170 1.1 lukem } else {
171 1.6 lukem fsoptions.freefiles =
172 1.14 lukem strsuftoll("free files",
173 1.6 lukem optarg, 0, LLONG_MAX);
174 1.1 lukem }
175 1.1 lukem break;
176 1.1 lukem
177 1.1 lukem case 'F':
178 1.1 lukem specfile = optarg;
179 1.1 lukem break;
180 1.1 lukem
181 1.1 lukem case 'M':
182 1.6 lukem fsoptions.minsize =
183 1.14 lukem strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
184 1.1 lukem break;
185 1.1 lukem
186 1.11 lukem case 'N':
187 1.11 lukem if (! setup_getid(optarg))
188 1.11 lukem errx(1,
189 1.11 lukem "Unable to use user and group databases in `%s'",
190 1.11 lukem optarg);
191 1.11 lukem break;
192 1.11 lukem
193 1.1 lukem case 'm':
194 1.6 lukem fsoptions.maxsize =
195 1.14 lukem strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
196 1.1 lukem break;
197 1.1 lukem
198 1.1 lukem case 'o':
199 1.1 lukem {
200 1.1 lukem char *p;
201 1.1 lukem
202 1.1 lukem while ((p = strsep(&optarg, ",")) != NULL) {
203 1.1 lukem if (*p == '\0')
204 1.1 lukem errx(1, "Empty option");
205 1.1 lukem if (! fstype->parse_options(p, &fsoptions))
206 1.1 lukem usage();
207 1.1 lukem }
208 1.1 lukem break;
209 1.1 lukem }
210 1.1 lukem
211 1.1 lukem case 's':
212 1.1 lukem fsoptions.minsize = fsoptions.maxsize =
213 1.14 lukem strsuftoll("size", optarg, 1LL, LLONG_MAX);
214 1.1 lukem break;
215 1.1 lukem
216 1.1 lukem case 'S':
217 1.6 lukem fsoptions.sectorsize =
218 1.14 lukem (int)strsuftoll("sector size", optarg,
219 1.6 lukem 1LL, INT_MAX);
220 1.1 lukem break;
221 1.1 lukem
222 1.1 lukem case 't':
223 1.21 jmc /* Check current one and cleanup if necessary. */
224 1.21 jmc if (fstype->cleanup_options)
225 1.21 jmc fstype->cleanup_options(&fsoptions);
226 1.21 jmc fsoptions.fs_specific = NULL;
227 1.1 lukem if ((fstype = get_fstype(optarg)) == NULL)
228 1.1 lukem errx(1, "Unknown fs type `%s'.", optarg);
229 1.21 jmc fstype->prepare_options(&fsoptions);
230 1.1 lukem break;
231 1.1 lukem
232 1.16 thorpej case 'x':
233 1.17 lukem fsoptions.onlyspec = 1;
234 1.16 thorpej break;
235 1.16 thorpej
236 1.35 sjg case 'Z':
237 1.35 sjg fsoptions.sparse = 1;
238 1.35 sjg break;
239 1.35 sjg
240 1.1 lukem case '?':
241 1.1 lukem default:
242 1.1 lukem usage();
243 1.1 lukem /* NOTREACHED */
244 1.1 lukem
245 1.1 lukem }
246 1.1 lukem }
247 1.1 lukem if (debug) {
248 1.1 lukem printf("debug mask: 0x%08x\n", debug);
249 1.1 lukem printf("start time: %ld.%ld, %s",
250 1.3 lukem (long)start_time.tv_sec, (long)start_time.tv_nsec,
251 1.1 lukem ctime(&start_time.tv_sec));
252 1.1 lukem }
253 1.1 lukem argc -= optind;
254 1.1 lukem argv += optind;
255 1.1 lukem
256 1.31 christos if (argc < 2)
257 1.1 lukem usage();
258 1.16 thorpej
259 1.16 thorpej /* -x must be accompanied by -F */
260 1.17 lukem if (fsoptions.onlyspec != 0 && specfile == NULL)
261 1.16 thorpej errx(1, "-x requires -F mtree-specfile.");
262 1.1 lukem
263 1.1 lukem /* walk the tree */
264 1.1 lukem TIMER_START(start);
265 1.31 christos root = walk_dir(argv[1], ".", NULL, NULL);
266 1.1 lukem TIMER_RESULTS(start, "walk_dir");
267 1.1 lukem
268 1.31 christos /* append extra directory */
269 1.31 christos for (i = 2; i < argc; i++) {
270 1.31 christos struct stat sb;
271 1.31 christos if (stat(argv[i], &sb) == -1)
272 1.31 christos err(1, "Can't stat `%s'", argv[i]);
273 1.31 christos if (!S_ISDIR(sb.st_mode))
274 1.31 christos errx(1, "%s: not a directory", argv[i]);
275 1.31 christos TIMER_START(start);
276 1.31 christos root = walk_dir(argv[i], ".", NULL, root);
277 1.31 christos TIMER_RESULTS(start, "walk_dir2");
278 1.31 christos }
279 1.31 christos
280 1.1 lukem if (specfile) { /* apply a specfile */
281 1.1 lukem TIMER_START(start);
282 1.25 dbj apply_specfile(specfile, argv[1], root, fsoptions.onlyspec);
283 1.1 lukem TIMER_RESULTS(start, "apply_specfile");
284 1.1 lukem }
285 1.1 lukem
286 1.1 lukem if (debug & DEBUG_DUMP_FSNODES) {
287 1.12 lukem printf("\nparent: %s\n", argv[1]);
288 1.31 christos dump_fsnodes(root);
289 1.1 lukem putchar('\n');
290 1.1 lukem }
291 1.1 lukem
292 1.1 lukem /* build the file system */
293 1.1 lukem TIMER_START(start);
294 1.1 lukem fstype->make_fs(argv[0], argv[1], root, &fsoptions);
295 1.1 lukem TIMER_RESULTS(start, "make_fs");
296 1.1 lukem
297 1.24 dbj free_fsnodes(root);
298 1.24 dbj
299 1.1 lukem exit(0);
300 1.1 lukem /* NOTREACHED */
301 1.1 lukem }
302 1.1 lukem
303 1.37 christos int
304 1.37 christos set_option(const option_t *options, const char *option)
305 1.37 christos {
306 1.37 christos char *var, *val;
307 1.37 christos int retval;
308 1.37 christos
309 1.37 christos assert(option != NULL);
310 1.37 christos
311 1.37 christos if ((var = strdup(option)) == NULL) {
312 1.37 christos err(EXIT_FAILURE, "Allocating memory for copy of option string");
313 1.37 christos }
314 1.37 christos retval = 0;
315 1.37 christos if ((val = strchr(var, '=')) == NULL) {
316 1.37 christos warnx("Option `%s' doesn't contain a value", var);
317 1.37 christos goto out;
318 1.37 christos }
319 1.37 christos *val++ = '\0';
320 1.37 christos
321 1.37 christos retval = set_option_var(options, var, val);
322 1.37 christos
323 1.37 christos out:
324 1.37 christos free(var);
325 1.37 christos return retval;
326 1.37 christos }
327 1.1 lukem
328 1.1 lukem int
329 1.37 christos set_option_var(const option_t *options, const char *var, const char *val)
330 1.1 lukem {
331 1.36 christos char *s;
332 1.36 christos size_t i;
333 1.36 christos
334 1.36 christos #define NUM(width) *(uint ## width ## _t *)options[i].value = \
335 1.36 christos (uint ## width ## _t)strsuftoll(options[i].desc, val, \
336 1.36 christos options[i].minimum, options[i].maximum); break
337 1.1 lukem
338 1.1 lukem for (i = 0; options[i].name != NULL; i++) {
339 1.36 christos if (options[i].letter != var[0] || var[1] != '\0')
340 1.36 christos continue;
341 1.36 christos else if (strcmp(options[i].name, var) != 0)
342 1.1 lukem continue;
343 1.36 christos switch (options[i].type) {
344 1.36 christos case OPT_BOOL:
345 1.36 christos *(bool *)options[i].value = 1;
346 1.36 christos break;
347 1.36 christos case OPT_STRARRAY:
348 1.36 christos strlcpy((void *)options[i].value, val, (size_t)
349 1.36 christos options[i].maximum);
350 1.36 christos break;
351 1.36 christos case OPT_STRPTR:
352 1.36 christos if ((s = strdup(val)) == NULL)
353 1.36 christos err(1, NULL);
354 1.36 christos *(char **)options[i].value = s;
355 1.36 christos break;
356 1.36 christos
357 1.36 christos case OPT_INT64:
358 1.36 christos NUM(64);
359 1.36 christos case OPT_INT32:
360 1.36 christos NUM(32);
361 1.36 christos case OPT_INT16:
362 1.36 christos NUM(16);
363 1.36 christos case OPT_INT8:
364 1.36 christos NUM(8);
365 1.36 christos default:
366 1.36 christos warnx("Unknown type %d in option %s", options[i].type,
367 1.36 christos val);
368 1.36 christos return 0;
369 1.36 christos }
370 1.36 christos return 1;
371 1.1 lukem }
372 1.1 lukem warnx("Unknown option `%s'", var);
373 1.1 lukem return (0);
374 1.1 lukem }
375 1.1 lukem
376 1.1 lukem
377 1.1 lukem static fstype_t *
378 1.1 lukem get_fstype(const char *type)
379 1.1 lukem {
380 1.1 lukem int i;
381 1.1 lukem
382 1.1 lukem for (i = 0; fstypes[i].type != NULL; i++)
383 1.1 lukem if (strcmp(fstypes[i].type, type) == 0)
384 1.1 lukem return (&fstypes[i]);
385 1.1 lukem return (NULL);
386 1.1 lukem }
387 1.1 lukem
388 1.1 lukem static void
389 1.1 lukem usage(void)
390 1.1 lukem {
391 1.1 lukem const char *prog;
392 1.1 lukem
393 1.1 lukem prog = getprogname();
394 1.1 lukem fprintf(stderr,
395 1.35 sjg "usage: %s [-xZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
396 1.30 wiz "\t[-F mtree-specfile] [-f free-files] [-M minimum-size]\n"
397 1.30 wiz "\t[-m maximum-size] [-N userdb-dir] [-o fs-options] [-S sector-size]\n"
398 1.31 christos "\t[-s image-size] [-t fs-type] image-file directory [extra-directory ...]\n",
399 1.1 lukem prog);
400 1.1 lukem exit(1);
401 1.1 lukem }
402