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