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