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