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