Home | History | Annotate | Line # | Download | only in makefs
makefs.c revision 1.5
      1 /*	$NetBSD: makefs.c,v 1.5 2001/11/08 12:24:12 simonb Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 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 #ifndef __lint
     40 __RCSID("$NetBSD: makefs.c,v 1.5 2001/11/08 12:24:12 simonb Exp $");
     41 #endif	/* !__lint */
     42 
     43 #include <assert.h>
     44 #include <ctype.h>
     45 #include <err.h>
     46 #include <errno.h>
     47 #include <limits.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <unistd.h>
     52 
     53 #include "makefs.h"
     54 
     55 
     56 /*
     57  * list of supported file systems and dispatch functions
     58  */
     59 typedef struct {
     60 	const char	*type;
     61 	int		(*parse_options)(const char *, fsinfo_t *);
     62 	void		(*make_fs)(const char *, const char *, fsnode *,
     63 				fsinfo_t *);
     64 } fstype_t;
     65 
     66 static fstype_t fstypes[] = {
     67 	{ "ffs",	ffs_parse_opts,		ffs_makefs },
     68 	{ NULL	},
     69 };
     70 
     71 int		debug;
     72 struct timespec	start_time;
     73 
     74 
     75 static	fstype_t *get_fstype(const char *);
     76 static	long long strsuftoll(const char *, const char *, long long, long long);
     77 static	void	usage(void);
     78 int		main(int, char *[]);
     79 
     80 int
     81 main(int argc, char *argv[])
     82 {
     83 	struct timeval	 start;
     84 	fstype_t	*fstype;
     85 	fsinfo_t	 fsoptions;
     86 	fsnode		*root;
     87 	int	 	 ch, len;
     88 	char		*specfile;
     89 
     90 	debug = 0;
     91 	if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
     92 		errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
     93 	(void)memset(&fsoptions, 0, sizeof(fsoptions));
     94 	fsoptions.fd = -1;
     95 	specfile = NULL;
     96 	if (gettimeofday(&start, NULL) == -1)
     97 		err(1, "Unable to get system time");
     98 	TIMEVAL_TO_TIMESPEC(&start, &start_time);
     99 
    100 	while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:o:s:S:t:")) != -1) {
    101 		switch (ch) {
    102 
    103 		case 'B':
    104 			if (strcmp(optarg, "be") == 0 ||
    105 			    strcmp(optarg, "big") == 0) {
    106 #if BYTE_ORDER == LITTLE_ENDIAN
    107 				fsoptions.needswap = 1;
    108 #endif
    109 			} else if (strcmp(optarg, "le") == 0 ||
    110 			    strcmp(optarg, "little") == 0) {
    111 #if BYTE_ORDER == BIG_ENDIAN
    112 				fsoptions.needswap = 1;
    113 #endif
    114 			} else {
    115 				warnx("Invalid endian `%s'.", optarg);
    116 				usage();
    117 			}
    118 			break;
    119 
    120 		case 'b':
    121 			len = strlen(optarg) - 1;
    122 			if (optarg[len] == '%') {
    123 				optarg[len] = '\0';
    124 				fsoptions.freeblockpc =
    125 				    strsuftoll("free block percentage",
    126 				    optarg, 0, LLONG_MAX);
    127 			} else {
    128 				fsoptions.freeblocks = strsuftoll("free blocks",
    129 				    optarg, 0, LLONG_MAX);
    130 			}
    131 			break;
    132 
    133 		case 'd':
    134 			debug = (int)strsuftoll("debug mask", optarg,
    135 			    0, LLONG_MAX);
    136 			break;
    137 
    138 		case 'f':
    139 			len = strlen(optarg) - 1;
    140 			if (optarg[len] == '%') {
    141 				optarg[len] = '\0';
    142 				fsoptions.freefilepc =
    143 				    strsuftoll("free file percentage",
    144 				    optarg, 0, LLONG_MAX);
    145 			} else {
    146 				fsoptions.freefiles = strsuftoll("free files",
    147 				    optarg, 0, LLONG_MAX);
    148 			}
    149 			break;
    150 
    151 		case 'F':
    152 			specfile = optarg;
    153 			break;
    154 
    155 		case 'M':
    156 			fsoptions.minsize = strsuftoll("minimum size",
    157 			    optarg, 1LL, LLONG_MAX);
    158 			break;
    159 
    160 		case 'm':
    161 			fsoptions.maxsize = strsuftoll("maximum size",
    162 			    optarg, 1LL, LLONG_MAX);
    163 			break;
    164 
    165 		case 'o':
    166 		{
    167 			char *p;
    168 
    169 			while ((p = strsep(&optarg, ",")) != NULL) {
    170 				if (*p == '\0')
    171 					errx(1, "Empty option");
    172 				if (! fstype->parse_options(p, &fsoptions))
    173 					usage();
    174 			}
    175 			break;
    176 		}
    177 
    178 		case 's':
    179 			fsoptions.minsize = fsoptions.maxsize =
    180 			    strsuftoll("size", optarg, 1LL, LLONG_MAX);
    181 			break;
    182 
    183 		case 'S':
    184 			fsoptions.sectorsize = strsuftoll("sector size",
    185 			    optarg, 1LL, LLONG_MAX);
    186 			break;
    187 
    188 		case 't':
    189 			if ((fstype = get_fstype(optarg)) == NULL)
    190 				errx(1, "Unknown fs type `%s'.", optarg);
    191 			break;
    192 
    193 		case '?':
    194 		default:
    195 			usage();
    196 			/* NOTREACHED */
    197 
    198 		}
    199 	}
    200 	if (debug) {
    201 		printf("debug mask: 0x%08x\n", debug);
    202 		printf("start time: %ld.%ld, %s",
    203 		    (long)start_time.tv_sec, (long)start_time.tv_nsec,
    204 		    ctime(&start_time.tv_sec));
    205 	}
    206 	argc -= optind;
    207 	argv += optind;
    208 
    209 	if (argc != 2)
    210 		usage();
    211 
    212 				/* walk the tree */
    213 	TIMER_START(start);
    214 	root = walk_dir(argv[1], NULL);
    215 	TIMER_RESULTS(start, "walk_dir");
    216 
    217 	if (specfile) {		/* apply a specfile */
    218 		TIMER_START(start);
    219 		apply_specfile(specfile, argv[1], root);
    220 		TIMER_RESULTS(start, "apply_specfile");
    221 	}
    222 
    223 	if (debug & DEBUG_DUMP_FSNODES) {
    224 		putchar('\n');
    225 		dump_fsnodes(argv[1], root);
    226 		putchar('\n');
    227 	}
    228 
    229 				/* build the file system */
    230 	TIMER_START(start);
    231 	fstype->make_fs(argv[0], argv[1], root, &fsoptions);
    232 	TIMER_RESULTS(start, "make_fs");
    233 
    234 	exit(0);
    235 	/* NOTREACHED */
    236 }
    237 
    238 
    239 int
    240 set_option(option_t *options, const char *var, const char *val)
    241 {
    242 	int	i;
    243 
    244 	for (i = 0; options[i].name != NULL; i++) {
    245 		if (strcmp(options[i].name, var) != 0)
    246 			continue;
    247 		*options[i].value = (int)strsuftoll(options[i].desc, val,
    248 		    options[i].minimum, options[i].maximum);
    249 		return (1);
    250 	}
    251 	warnx("Unknown option `%s'", var);
    252 	return (0);
    253 }
    254 
    255 
    256 static fstype_t *
    257 get_fstype(const char *type)
    258 {
    259 	int i;
    260 
    261 	for (i = 0; fstypes[i].type != NULL; i++)
    262 		if (strcmp(fstypes[i].type, type) == 0)
    263 			return (&fstypes[i]);
    264 	return (NULL);
    265 }
    266 
    267 
    268 static long long
    269 strsuftoll(const char *desc, const char *arg, long long min, long long max)
    270 {
    271 	long long	result;
    272 	char		*ep;
    273 
    274 	assert(desc != NULL);
    275 	assert(arg != NULL);
    276 
    277 	errno = 0;
    278 	result = strtoll(arg, &ep, 0);
    279 	if ((result == LLONG_MIN || result == LLONG_MAX) && errno == ERANGE) {
    280 		warn("%s `%s'", desc, arg);
    281 		usage();
    282 	}
    283 	if (ep[0] != '\0' && ep[1] != '\0') {
    284 		warnx("`%s' is not a valid number for %s.", optarg, desc);
    285 		usage();
    286 	}
    287 	switch (tolower((unsigned char)ep[0])) {
    288 	case '\0':
    289 	case 'b':
    290 		break;
    291 	case 'k':
    292 		result <<= 10;
    293 		break;
    294 	case 'm':
    295 		result <<= 20;
    296 		break;
    297 	case 'g':
    298 		result <<= 30;
    299 		break;
    300 	default:
    301 		warnx("`%s' is not a valid suffix for %s.", ep, desc);
    302 		usage();
    303 	}
    304 
    305 	if (result < min) {
    306 		warnx("%s `%s' (%lld) is less than %lld.",
    307 		    desc, optarg, result, min);
    308 		usage();
    309 	}
    310 	if (result > max) {
    311 		warnx("%s `%s' (%lld) is greater than %lld.",
    312 		    desc, optarg, result, max);
    313 		usage();
    314 	}
    315 	if (debug & DEBUG_STRSUFTOLL)
    316 		printf("strsuftoll: got %lld for %s %s\n",
    317 		    result, desc, arg);
    318 	return (result);
    319 }
    320 
    321 
    322 static void
    323 usage(void)
    324 {
    325 	const char *prog;
    326 
    327 	prog = getprogname();
    328 	fprintf(stderr,
    329 "Usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
    330 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n"
    331 "\t[-b free-blocks] [-f free-files] [-F mtree-specfile]\n"
    332 "\timage-file directory\n",
    333 	    prog);
    334 	exit(1);
    335 }
    336