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