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