Home | History | Annotate | Line # | Download | only in makefs
makefs.c revision 1.9
      1 /*	$NetBSD: makefs.c,v 1.9 2002/01/18 08:39:24 lukem 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.9 2002/01/18 08:39:24 lukem 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 #include "strsuftoull.h"
     55 
     56 
     57 /*
     58  * list of supported file systems and dispatch functions
     59  */
     60 typedef struct {
     61 	const char	*type;
     62 	int		(*parse_options)(const char *, fsinfo_t *);
     63 	void		(*make_fs)(const char *, const char *, fsnode *,
     64 				fsinfo_t *);
     65 } fstype_t;
     66 
     67 static fstype_t fstypes[] = {
     68 	{ "ffs",	ffs_parse_opts,		ffs_makefs },
     69 	{ NULL	},
     70 };
     71 
     72 uint		debug;
     73 struct timespec	start_time;
     74 
     75 
     76 static	fstype_t *get_fstype(const char *);
     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 	setprogname(argv[0]);
     91 
     92 	debug = 0;
     93 	if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
     94 		errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
     95 
     96 		/* set default fsoptions */
     97 	(void)memset(&fsoptions, 0, sizeof(fsoptions));
     98 	fsoptions.fd = -1;
     99 	fsoptions.sectorsize = -1;
    100 	fsoptions.bsize= -1;
    101 	fsoptions.fsize= -1;
    102 	fsoptions.cpg= -1;
    103 	fsoptions.density= -1;
    104 	fsoptions.ntracks= -1;
    105 	fsoptions.nsectors= -1;
    106 	fsoptions.rpm= -1;
    107 	fsoptions.minfree= -1;
    108 	fsoptions.optimization= -1;
    109 	fsoptions.maxcontig= -1;
    110 	fsoptions.rotdelay= -1;
    111 	fsoptions.maxbpg= -1;
    112 	fsoptions.nrpos= -1;
    113 	fsoptions.avgfilesize= -1;
    114 	fsoptions.avgfpdir= -1;
    115 
    116 	specfile = NULL;
    117 	if (gettimeofday(&start, NULL) == -1)
    118 		err(1, "Unable to get system time");
    119 	TIMEVAL_TO_TIMESPEC(&start, &start_time);
    120 
    121 	while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:o:s:S:t:")) != -1) {
    122 		switch (ch) {
    123 
    124 		case 'B':
    125 			if (strcmp(optarg, "be") == 0 ||
    126 			    strcmp(optarg, "big") == 0) {
    127 #if BYTE_ORDER == LITTLE_ENDIAN
    128 				fsoptions.needswap = 1;
    129 #endif
    130 			} else if (strcmp(optarg, "le") == 0 ||
    131 			    strcmp(optarg, "little") == 0) {
    132 #if BYTE_ORDER == BIG_ENDIAN
    133 				fsoptions.needswap = 1;
    134 #endif
    135 			} else {
    136 				warnx("Invalid endian `%s'.", optarg);
    137 				usage();
    138 			}
    139 			break;
    140 
    141 		case 'b':
    142 			len = strlen(optarg) - 1;
    143 			if (optarg[len] == '%') {
    144 				optarg[len] = '\0';
    145 				fsoptions.freeblockpc =
    146 				    strsuftoull("free block percentage",
    147 					optarg, 0, 99);
    148 			} else {
    149 				fsoptions.freeblocks =
    150 				    strsuftoull("free blocks",
    151 					optarg, 0, LLONG_MAX);
    152 			}
    153 			break;
    154 
    155 		case 'd':
    156 			debug =
    157 			    (int)strsuftoull("debug mask", optarg, 0, UINT_MAX);
    158 			break;
    159 
    160 		case 'f':
    161 			len = strlen(optarg) - 1;
    162 			if (optarg[len] == '%') {
    163 				optarg[len] = '\0';
    164 				fsoptions.freefilepc =
    165 				    strsuftoull("free file percentage",
    166 					optarg, 0, 99);
    167 			} else {
    168 				fsoptions.freefiles =
    169 				    strsuftoull("free files",
    170 					optarg, 0, LLONG_MAX);
    171 			}
    172 			break;
    173 
    174 		case 'F':
    175 			specfile = optarg;
    176 			break;
    177 
    178 		case 'M':
    179 			fsoptions.minsize =
    180 			    strsuftoull("minimum size", optarg, 1LL, LLONG_MAX);
    181 			break;
    182 
    183 		case 'm':
    184 			fsoptions.maxsize =
    185 			    strsuftoull("maximum size", optarg, 1LL, LLONG_MAX);
    186 			break;
    187 
    188 		case 'o':
    189 		{
    190 			char *p;
    191 
    192 			while ((p = strsep(&optarg, ",")) != NULL) {
    193 				if (*p == '\0')
    194 					errx(1, "Empty option");
    195 				if (! fstype->parse_options(p, &fsoptions))
    196 					usage();
    197 			}
    198 			break;
    199 		}
    200 
    201 		case 's':
    202 			fsoptions.minsize = fsoptions.maxsize =
    203 			    strsuftoull("size", optarg, 1LL, LLONG_MAX);
    204 			break;
    205 
    206 		case 'S':
    207 			fsoptions.sectorsize =
    208 			    (int)strsuftoull("sector size", optarg,
    209 				1LL, INT_MAX);
    210 			break;
    211 
    212 		case 't':
    213 			if ((fstype = get_fstype(optarg)) == NULL)
    214 				errx(1, "Unknown fs type `%s'.", optarg);
    215 			break;
    216 
    217 		case '?':
    218 		default:
    219 			usage();
    220 			/* NOTREACHED */
    221 
    222 		}
    223 	}
    224 	if (debug) {
    225 		printf("debug mask: 0x%08x\n", debug);
    226 		printf("start time: %ld.%ld, %s",
    227 		    (long)start_time.tv_sec, (long)start_time.tv_nsec,
    228 		    ctime(&start_time.tv_sec));
    229 	}
    230 	argc -= optind;
    231 	argv += optind;
    232 
    233 	if (argc != 2)
    234 		usage();
    235 
    236 				/* walk the tree */
    237 	TIMER_START(start);
    238 	root = walk_dir(argv[1], NULL);
    239 	TIMER_RESULTS(start, "walk_dir");
    240 
    241 	if (specfile) {		/* apply a specfile */
    242 		TIMER_START(start);
    243 		apply_specfile(specfile, argv[1], root);
    244 		TIMER_RESULTS(start, "apply_specfile");
    245 	}
    246 
    247 	if (debug & DEBUG_DUMP_FSNODES) {
    248 		putchar('\n');
    249 		dump_fsnodes(argv[1], root);
    250 		putchar('\n');
    251 	}
    252 
    253 				/* build the file system */
    254 	TIMER_START(start);
    255 	fstype->make_fs(argv[0], argv[1], root, &fsoptions);
    256 	TIMER_RESULTS(start, "make_fs");
    257 
    258 	exit(0);
    259 	/* NOTREACHED */
    260 }
    261 
    262 
    263 int
    264 set_option(option_t *options, const char *var, const char *val)
    265 {
    266 	int	i;
    267 
    268 	for (i = 0; options[i].name != NULL; i++) {
    269 		if (strcmp(options[i].name, var) != 0)
    270 			continue;
    271 		*options[i].value = (int)strsuftoull(options[i].desc, val,
    272 		    options[i].minimum, options[i].maximum);
    273 		return (1);
    274 	}
    275 	warnx("Unknown option `%s'", var);
    276 	return (0);
    277 }
    278 
    279 
    280 static fstype_t *
    281 get_fstype(const char *type)
    282 {
    283 	int i;
    284 
    285 	for (i = 0; fstypes[i].type != NULL; i++)
    286 		if (strcmp(fstypes[i].type, type) == 0)
    287 			return (&fstypes[i]);
    288 	return (NULL);
    289 }
    290 
    291 static void
    292 usage(void)
    293 {
    294 	const char *prog;
    295 
    296 	prog = getprogname();
    297 	fprintf(stderr,
    298 "Usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
    299 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n"
    300 "\t[-b free-blocks] [-f free-files] [-F mtree-specfile]\n"
    301 "\timage-file directory\n",
    302 	    prog);
    303 	exit(1);
    304 }
    305