xref: /dragonfly/usr.sbin/makefs/makefs.c (revision 631c21f2)
1 /*	$NetBSD: makefs.c,v 1.26 2006/10/22 21:11:56 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 2001-2003 Wasabi Systems, Inc.
7  * All rights reserved.
8  *
9  * Written by Luke Mewburn for Wasabi Systems, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed for the NetBSD Project by
22  *      Wasabi Systems, Inc.
23  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24  *    or promote products derived from this software without specific prior
25  *    written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * $FreeBSD: head/usr.sbin/makefs/makefs.c 326276 2017-11-27 15:37:16Z pfg $
40  */
41 
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <assert.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <limits.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <time.h>
52 #include <unistd.h>
53 #include <stdbool.h>
54 #include <util.h>
55 
56 #include "makefs.h"
57 #include "mtree.h"
58 
59 /*
60  * list of supported file systems and dispatch functions
61  */
62 typedef struct {
63 	const char	*type;
64 	void		(*prepare_options)(fsinfo_t *);
65 	int		(*parse_options)(const char *, fsinfo_t *);
66 	void		(*cleanup_options)(fsinfo_t *);
67 	void		(*make_fs)(const char *, const char *, fsnode *,
68 				fsinfo_t *);
69 } fstype_t;
70 
71 static fstype_t fstypes[] = {
72 #define ENTRY(name) { \
73 	# name, name ## _prep_opts, name ## _parse_opts, \
74 	name ## _cleanup_opts, name ## _makefs  \
75 }
76 	ENTRY(ffs),
77 	ENTRY(cd9660),
78 	{ .type = NULL	},
79 };
80 
81 u_int		debug;
82 int		dupsok;
83 struct timespec	start_time;
84 struct stat stampst;
85 
86 static	fstype_t *get_fstype(const char *);
87 static int get_tstamp(const char *, struct stat *);
88 static	void	usage(fstype_t *, fsinfo_t *);
89 
90 int
91 main(int argc, char *argv[])
92 {
93 	struct stat	 sb;
94 	struct timeval	 start;
95 	fstype_t	*fstype;
96 	fsinfo_t	 fsoptions;
97 	fsnode		*root;
98 	int	 	 ch, i, len;
99 	const char	*subtree;
100 	const char	*specfile;
101 
102 	setprogname(argv[0]);
103 
104 	debug = 0;
105 	if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
106 		errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
107 
108 		/* set default fsoptions */
109 	(void)memset(&fsoptions, 0, sizeof(fsoptions));
110 	fsoptions.fd = -1;
111 	fsoptions.sectorsize = -1;
112 
113 	if (fstype->prepare_options)
114 		fstype->prepare_options(&fsoptions);
115 
116 	specfile = NULL;
117 #ifdef CLOCK_REALTIME
118 	ch = clock_gettime(CLOCK_REALTIME, &start_time);
119 #else
120 	ch = gettimeofday(&start, NULL);
121 	start_time.tv_sec = start.tv_sec;
122 	start_time.tv_nsec = start.tv_usec * 1000;
123 #endif
124 	if (ch == -1)
125 		err(1, "Unable to get system time");
126 
127 
128 	while ((ch = getopt(argc, argv, "B:b:Dd:f:F:M:m:N:O:o:pR:s:S:t:T:xZ")) != -1) {
129 		switch (ch) {
130 
131 		case 'B':
132 			if (strcmp(optarg, "be") == 0 ||
133 			    strcmp(optarg, "4321") == 0 ||
134 			    strcmp(optarg, "big") == 0) {
135 #if BYTE_ORDER == LITTLE_ENDIAN
136 				fsoptions.needswap = 1;
137 #endif
138 			} else if (strcmp(optarg, "le") == 0 ||
139 			    strcmp(optarg, "1234") == 0 ||
140 			    strcmp(optarg, "little") == 0) {
141 #if BYTE_ORDER == BIG_ENDIAN
142 				fsoptions.needswap = 1;
143 #endif
144 			} else {
145 				warnx("Invalid endian `%s'.", optarg);
146 				usage(fstype, &fsoptions);
147 			}
148 			break;
149 
150 		case 'b':
151 			len = strlen(optarg) - 1;
152 			if (optarg[len] == '%') {
153 				optarg[len] = '\0';
154 				fsoptions.freeblockpc =
155 				    strsuftoll("free block percentage",
156 					optarg, 0, 99);
157 			} else {
158 				fsoptions.freeblocks =
159 				    strsuftoll("free blocks",
160 					optarg, 0, LLONG_MAX);
161 			}
162 			break;
163 
164 		case 'D':
165 			dupsok = 1;
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 		case 'p':
225 			/* Deprecated in favor of 'Z' */
226 			fsoptions.sparse = 1;
227 			break;
228 
229 		case 'R':
230 			/* Round image size up to specified block size */
231 			fsoptions.roundup =
232 			    strsuftoll("roundup-size", optarg, 0, LLONG_MAX);
233 			break;
234 
235 		case 's':
236 			fsoptions.minsize = fsoptions.maxsize =
237 			    strsuftoll("size", optarg, 1LL, LLONG_MAX);
238 			break;
239 
240 		case 'S':
241 			fsoptions.sectorsize =
242 			    (int)strsuftoll("sector size", optarg,
243 				1LL, INT_MAX);
244 			break;
245 
246 		case 't':
247 			/* Check current one and cleanup if necessary. */
248 			if (fstype->cleanup_options)
249 				fstype->cleanup_options(&fsoptions);
250 			fsoptions.fs_specific = NULL;
251 			if ((fstype = get_fstype(optarg)) == NULL)
252 				errx(1, "Unknown fs type `%s'.", optarg);
253 			fstype->prepare_options(&fsoptions);
254 			break;
255 
256 		case 'T':
257 			if (get_tstamp(optarg, &stampst) == -1)
258 				errx(1, "Cannot get timestamp from `%s'",
259 				    optarg);
260 			break;
261 
262 		case 'x':
263 			fsoptions.onlyspec = 1;
264 			break;
265 
266 		case 'Z':
267 			/* Superscedes 'p' for compatibility with NetBSD makefs(8) */
268 			fsoptions.sparse = 1;
269 			break;
270 
271 		case '?':
272 		default:
273 			usage(fstype, &fsoptions);
274 			/* NOTREACHED */
275 
276 		}
277 	}
278 	if (debug) {
279 		printf("debug mask: 0x%08x\n", debug);
280 		printf("start time: %ld.%ld, %s",
281 		    (long)start_time.tv_sec, (long)start_time.tv_nsec,
282 		    ctime(&start_time.tv_sec));
283 	}
284 	argc -= optind;
285 	argv += optind;
286 
287 	if (argc < 2)
288 		usage(fstype, &fsoptions);
289 
290 	/* -x must be accompanied by -F */
291 	if (fsoptions.onlyspec != 0 && specfile == NULL)
292 		errx(1, "-x requires -F mtree-specfile.");
293 
294 	/* Accept '-' as meaning "read from standard input". */
295 	if (strcmp(argv[1], "-") == 0)
296 		sb.st_mode = S_IFREG;
297 	else {
298 		if (stat(argv[1], &sb) == -1)
299 			err(1, "Can't stat `%s'", argv[1]);
300 	}
301 
302 	switch (sb.st_mode & S_IFMT) {
303 	case S_IFDIR:		/* walk the tree */
304 		subtree = argv[1];
305 		TIMER_START(start);
306 		root = walk_dir(subtree, ".", NULL, NULL);
307 		TIMER_RESULTS(start, "walk_dir");
308 		break;
309 	case S_IFREG:		/* read the manifest file */
310 		subtree = ".";
311 		TIMER_START(start);
312 		root = read_mtree(argv[1], NULL);
313 		TIMER_RESULTS(start, "manifest");
314 		break;
315 	default:
316 		errx(1, "%s: not a file or directory", argv[1]);
317 		/* NOTREACHED */
318 	}
319 
320 	/* append extra directory */
321 	for (i = 2; i < argc; i++) {
322 		if (stat(argv[i], &sb) == -1)
323 			err(1, "Can't stat `%s'", argv[i]);
324 		if (!S_ISDIR(sb.st_mode))
325 			errx(1, "%s: not a directory", argv[i]);
326 		TIMER_START(start);
327 		root = walk_dir(argv[i], ".", NULL, root);
328 		TIMER_RESULTS(start, "walk_dir2");
329 	}
330 
331 	if (specfile) {		/* apply a specfile */
332 		TIMER_START(start);
333 		apply_specfile(specfile, subtree, root, fsoptions.onlyspec);
334 		TIMER_RESULTS(start, "apply_specfile");
335 	}
336 
337 	if (debug & DEBUG_DUMP_FSNODES) {
338 		printf("\nparent: %s\n", subtree);
339 		dump_fsnodes(root);
340 		putchar('\n');
341 	}
342 
343 				/* build the file system */
344 	TIMER_START(start);
345 	fstype->make_fs(argv[0], subtree, root, &fsoptions);
346 	TIMER_RESULTS(start, "make_fs");
347 
348 	free_fsnodes(root);
349 
350 	exit(0);
351 	/* NOTREACHED */
352 }
353 
354 int
355 set_option(const option_t *options, const char *option, char *buf, size_t len)
356 {
357 	char *var, *val;
358 	int retval;
359 
360 	assert(option != NULL);
361 
362 	var = estrdup(option);
363 	for (val = var; *val; val++)
364 		if (*val == '=') {
365 			*val++ = '\0';
366 			break;
367 		}
368 	retval = set_option_var(options, var, val, buf, len);
369 	free(var);
370 	return retval;
371 }
372 
373 int
374 set_option_var(const option_t *options, const char *var, const char *val,
375     char *buf, size_t len)
376 {
377 	char *s;
378 	size_t i;
379 
380 #define NUM(type) \
381 	if (!*val) { \
382 		*(type *)options[i].value = 1; \
383 		break; \
384 	} \
385 	*(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \
386 	    options[i].minimum, options[i].maximum); break
387 
388 	for (i = 0; options[i].name != NULL; i++) {
389 		if (var[1] == '\0') {
390 			if (options[i].letter != var[0])
391 				continue;
392 		} else if (strcmp(options[i].name, var) != 0)
393 			continue;
394 		switch (options[i].type) {
395 		case OPT_BOOL:
396 			*(bool *)options[i].value = 1;
397 			break;
398 		case OPT_STRARRAY:
399 			strlcpy((void *)options[i].value, val, (size_t)
400 			    options[i].maximum);
401 			break;
402 		case OPT_STRPTR:
403 			s = estrdup(val);
404 			*(char **)options[i].value = s;
405 			break;
406 		case OPT_STRBUF:
407 			if (buf == NULL)
408 				abort();
409 			strlcpy(buf, val, len);
410 			break;
411 		case OPT_INT64:
412 			NUM(uint64_t);
413 		case OPT_INT32:
414 			NUM(uint32_t);
415 		case OPT_INT16:
416 			NUM(uint16_t);
417 		case OPT_INT8:
418 			NUM(uint8_t);
419 		default:
420 			warnx("Unknown type %d in option %s", options[i].type,
421 			    val);
422 			return 0;
423 		}
424 		return i;
425 	}
426 	warnx("Unknown option `%s'", var);
427 	return -1;
428 }
429 
430 
431 static fstype_t *
432 get_fstype(const char *type)
433 {
434 	int i;
435 
436 	for (i = 0; fstypes[i].type != NULL; i++)
437 		if (strcmp(fstypes[i].type, type) == 0)
438 			return (&fstypes[i]);
439 	return (NULL);
440 }
441 
442 option_t *
443 copy_opts(const option_t *o)
444 {
445 	size_t i;
446 
447 	for (i = 0; o[i].name; i++)
448 		continue;
449 	i++;
450 	return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
451 }
452 
453 static int
454 get_tstamp(const char *b, struct stat *st)
455 {
456 	time_t when;
457 	char *eb;
458 	long long l;
459 
460 	if (stat(b, st) != -1)
461 		return 0;
462 
463 	{
464 		errno = 0;
465 		l = strtoll(b, &eb, 0);
466 		if (b == eb || *eb || errno)
467 			return -1;
468 		when = (time_t)l;
469 	}
470 
471 	st->st_ino = 1;
472 #if HAVE_STRUCT_STAT_BIRTHTIME
473 	st->st_birthtime =
474 #endif
475 	st->st_mtime = st->st_ctime = st->st_atime = when;
476 	return 0;
477 }
478 
479 static void
480 usage(fstype_t *fstype, fsinfo_t *fsoptions)
481 {
482 	const char *prog;
483 
484 	prog = getprogname();
485 	fprintf(stderr,
486 "Usage: %s [-xZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
487 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n"
488 "\t[-N userdb-dir] [-O offset] [-o fs-options] [-R roundup-size]\n"
489 "\t[-S sector-size] [-s image-size] [-T <timestamp/file>] [-t fs-type]\n"
490 "\timage-file directory | manifest [extra-directory ...]\n",
491 	    prog);
492 
493 	if (fstype) {
494 		size_t i;
495 		option_t *o = fsoptions->fs_options;
496 
497 		fprintf(stderr, "\n%s specific options:\n", fstype->type);
498 		for (i = 0; o[i].name != NULL; i++)
499 			fprintf(stderr, "\t%c%c%20.20s\t%s\n",
500 			    o[i].letter ? o[i].letter : ' ',
501 			    o[i].letter ? ',' : ' ',
502 			    o[i].name, o[i].desc);
503 	}
504 	exit(1);
505 }
506