Home | History | Annotate | Line # | Download | only in makefs
makefs.c revision 1.1
      1  1.1  lukem /*	$NetBSD: makefs.c,v 1.1 2001/10/26 06:59:02 lukem Exp $	*/
      2  1.1  lukem 
      3  1.1  lukem /*
      4  1.1  lukem  * Copyright 2001 Wasabi Systems, Inc.
      5  1.1  lukem  * All rights reserved.
      6  1.1  lukem  *
      7  1.1  lukem  * Written by Luke Mewburn for Wasabi Systems, Inc.
      8  1.1  lukem  *
      9  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     10  1.1  lukem  * modification, are permitted provided that the following conditions
     11  1.1  lukem  * are met:
     12  1.1  lukem  * 1. Redistributions of source code must retain the above copyright
     13  1.1  lukem  *    notice, this list of conditions and the following disclaimer.
     14  1.1  lukem  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  lukem  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  lukem  *    documentation and/or other materials provided with the distribution.
     17  1.1  lukem  * 3. All advertising materials mentioning features or use of this software
     18  1.1  lukem  *    must display the following acknowledgement:
     19  1.1  lukem  *      This product includes software developed for the NetBSD Project by
     20  1.1  lukem  *      Wasabi Systems, Inc.
     21  1.1  lukem  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1  lukem  *    or promote products derived from this software without specific prior
     23  1.1  lukem  *    written permission.
     24  1.1  lukem  *
     25  1.1  lukem  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1  lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1  lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1  lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1  lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1  lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1  lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1  lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1  lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1  lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1  lukem  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1  lukem  */
     37  1.1  lukem 
     38  1.1  lukem #include <assert.h>
     39  1.1  lukem #include <ctype.h>
     40  1.1  lukem #include <err.h>
     41  1.1  lukem #include <errno.h>
     42  1.1  lukem #include <stdio.h>
     43  1.1  lukem #include <stdlib.h>
     44  1.1  lukem #include <string.h>
     45  1.1  lukem #include <unistd.h>
     46  1.1  lukem 
     47  1.1  lukem #include "makefs.h"
     48  1.1  lukem 
     49  1.1  lukem 
     50  1.1  lukem /*
     51  1.1  lukem  * list of supported file systems and dispatch functions
     52  1.1  lukem  */
     53  1.1  lukem typedef struct {
     54  1.1  lukem 	const char	*type;
     55  1.1  lukem 	int		(*parse_options)(const char *, fsinfo_t *);
     56  1.1  lukem 	void		(*make_fs)(const char *, const char *, fsnode *,
     57  1.1  lukem 				fsinfo_t *);
     58  1.1  lukem } fstype_t;
     59  1.1  lukem 
     60  1.1  lukem static fstype_t fstypes[] = {
     61  1.1  lukem 	{ "ffs",	ffs_parse_opts,		ffs_makefs },
     62  1.1  lukem 	{ NULL	},
     63  1.1  lukem };
     64  1.1  lukem 
     65  1.1  lukem int		debug;
     66  1.1  lukem struct timespec	start_time;
     67  1.1  lukem 
     68  1.1  lukem 
     69  1.1  lukem static	fstype_t *get_fstype(const char *);
     70  1.1  lukem static	long long strsuftoll(const char *, const char *, long long, long long);
     71  1.1  lukem static	void	usage(void);
     72  1.1  lukem int		main(int, char *[]);
     73  1.1  lukem 
     74  1.1  lukem int
     75  1.1  lukem main(int argc, char *argv[])
     76  1.1  lukem {
     77  1.1  lukem 	struct timeval	 start;
     78  1.1  lukem 	fstype_t	*fstype;
     79  1.1  lukem 	fsinfo_t	 fsoptions;
     80  1.1  lukem 	fsnode		*root;
     81  1.1  lukem 	int	 	 ch, len;
     82  1.1  lukem 	char		*specfile;
     83  1.1  lukem 
     84  1.1  lukem 	debug = 0;
     85  1.1  lukem 	if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
     86  1.1  lukem 		errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
     87  1.1  lukem 	(void)memset(&fsoptions, 0, sizeof(fsoptions));
     88  1.1  lukem 	fsoptions.fd = -1;
     89  1.1  lukem 	specfile = NULL;
     90  1.1  lukem 	if (gettimeofday(&start, NULL) == -1)
     91  1.1  lukem 		err(1, "Unable to get system time");
     92  1.1  lukem 	TIMEVAL_TO_TIMESPEC(&start, &start_time);
     93  1.1  lukem 
     94  1.1  lukem 	while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:o:s:S:t:")) != -1) {
     95  1.1  lukem 		switch (ch) {
     96  1.1  lukem 
     97  1.1  lukem 		case 'B':
     98  1.1  lukem 			if (strcmp(optarg, "be") == 0 ||
     99  1.1  lukem 			    strcmp(optarg, "big") == 0) {
    100  1.1  lukem #if BYTE_ORDER == LITTLE_ENDIAN
    101  1.1  lukem 				fsoptions.needswap = 1;
    102  1.1  lukem #endif
    103  1.1  lukem 			} else if (strcmp(optarg, "le") == 0 ||
    104  1.1  lukem 			    strcmp(optarg, "little") == 0) {
    105  1.1  lukem #if BYTE_ORDER == BIG_ENDIAN
    106  1.1  lukem 				fsoptions.needswap = 1;
    107  1.1  lukem #endif
    108  1.1  lukem 			} else {
    109  1.1  lukem 				warnx("Invalid endian `%s'.", optarg);
    110  1.1  lukem 				usage();
    111  1.1  lukem 			}
    112  1.1  lukem 			break;
    113  1.1  lukem 
    114  1.1  lukem 		case 'b':
    115  1.1  lukem 			len = strlen(optarg) - 1;
    116  1.1  lukem 			if (optarg[len] == '%') {
    117  1.1  lukem 				optarg[len] = '\0';
    118  1.1  lukem 				fsoptions.freeblockpc =
    119  1.1  lukem 				    strsuftoll("free block percentage",
    120  1.1  lukem 				    optarg, 0, LLONG_MAX);
    121  1.1  lukem 			} else {
    122  1.1  lukem 				fsoptions.freeblocks = strsuftoll("free blocks",
    123  1.1  lukem 				    optarg, 0, LLONG_MAX);
    124  1.1  lukem 			}
    125  1.1  lukem 			break;
    126  1.1  lukem 
    127  1.1  lukem 		case 'd':
    128  1.1  lukem 			debug = (int)strsuftoll("debug mask", optarg,
    129  1.1  lukem 			    0, LLONG_MAX);
    130  1.1  lukem 			break;
    131  1.1  lukem 
    132  1.1  lukem 		case 'f':
    133  1.1  lukem 			len = strlen(optarg) - 1;
    134  1.1  lukem 			if (optarg[len] == '%') {
    135  1.1  lukem 				optarg[len] = '\0';
    136  1.1  lukem 				fsoptions.freefilepc =
    137  1.1  lukem 				    strsuftoll("free file percentage",
    138  1.1  lukem 				    optarg, 0, LLONG_MAX);
    139  1.1  lukem 			} else {
    140  1.1  lukem 				fsoptions.freefiles = strsuftoll("free files",
    141  1.1  lukem 				    optarg, 0, LLONG_MAX);
    142  1.1  lukem 			}
    143  1.1  lukem 			break;
    144  1.1  lukem 
    145  1.1  lukem 		case 'F':
    146  1.1  lukem 			specfile = optarg;
    147  1.1  lukem 			break;
    148  1.1  lukem 
    149  1.1  lukem 		case 'M':
    150  1.1  lukem 			fsoptions.minsize = strsuftoll("minimum size",
    151  1.1  lukem 			    optarg, 1LL, LLONG_MAX);
    152  1.1  lukem 			break;
    153  1.1  lukem 
    154  1.1  lukem 		case 'm':
    155  1.1  lukem 			fsoptions.maxsize = strsuftoll("maximum size",
    156  1.1  lukem 			    optarg, 1LL, LLONG_MAX);
    157  1.1  lukem 			break;
    158  1.1  lukem 
    159  1.1  lukem 		case 'o':
    160  1.1  lukem 		{
    161  1.1  lukem 			char *p;
    162  1.1  lukem 
    163  1.1  lukem 			while ((p = strsep(&optarg, ",")) != NULL) {
    164  1.1  lukem 				if (*p == '\0')
    165  1.1  lukem 					errx(1, "Empty option");
    166  1.1  lukem 				if (! fstype->parse_options(p, &fsoptions))
    167  1.1  lukem 					usage();
    168  1.1  lukem 			}
    169  1.1  lukem 			break;
    170  1.1  lukem 		}
    171  1.1  lukem 
    172  1.1  lukem 		case 's':
    173  1.1  lukem 			fsoptions.minsize = fsoptions.maxsize =
    174  1.1  lukem 			    strsuftoll("size", optarg, 1LL, LLONG_MAX);
    175  1.1  lukem 			break;
    176  1.1  lukem 
    177  1.1  lukem 		case 'S':
    178  1.1  lukem 			fsoptions.sectorsize = strsuftoll("sector size",
    179  1.1  lukem 			    optarg, 1LL, LLONG_MAX);
    180  1.1  lukem 			break;
    181  1.1  lukem 
    182  1.1  lukem 		case 't':
    183  1.1  lukem 			if ((fstype = get_fstype(optarg)) == NULL)
    184  1.1  lukem 				errx(1, "Unknown fs type `%s'.", optarg);
    185  1.1  lukem 			break;
    186  1.1  lukem 
    187  1.1  lukem 		case '?':
    188  1.1  lukem 		default:
    189  1.1  lukem 			usage();
    190  1.1  lukem 			/* NOTREACHED */
    191  1.1  lukem 
    192  1.1  lukem 		}
    193  1.1  lukem 	}
    194  1.1  lukem 	if (debug) {
    195  1.1  lukem 		printf("debug mask: 0x%08x\n", debug);
    196  1.1  lukem 		printf("start time: %ld.%ld, %s",
    197  1.1  lukem 		    start_time.tv_sec, start_time.tv_nsec,
    198  1.1  lukem 		    ctime(&start_time.tv_sec));
    199  1.1  lukem 	}
    200  1.1  lukem 	argc -= optind;
    201  1.1  lukem 	argv += optind;
    202  1.1  lukem 
    203  1.1  lukem 	if (argc != 2)
    204  1.1  lukem 		usage();
    205  1.1  lukem 
    206  1.1  lukem 				/* walk the tree */
    207  1.1  lukem 	TIMER_START(start);
    208  1.1  lukem 	root = walk_dir(argv[1], NULL);
    209  1.1  lukem 	TIMER_RESULTS(start, "walk_dir");
    210  1.1  lukem 
    211  1.1  lukem 	if (specfile) {		/* apply a specfile */
    212  1.1  lukem 		TIMER_START(start);
    213  1.1  lukem 		apply_specfile(specfile, argv[1], root);
    214  1.1  lukem 		TIMER_RESULTS(start, "apply_specfile");
    215  1.1  lukem 	}
    216  1.1  lukem 
    217  1.1  lukem 	if (debug & DEBUG_DUMP_FSNODES) {
    218  1.1  lukem 		putchar('\n');
    219  1.1  lukem 		dump_fsnodes(argv[1], root);
    220  1.1  lukem 		putchar('\n');
    221  1.1  lukem 	}
    222  1.1  lukem 
    223  1.1  lukem 				/* build the file system */
    224  1.1  lukem 	TIMER_START(start);
    225  1.1  lukem 	fstype->make_fs(argv[0], argv[1], root, &fsoptions);
    226  1.1  lukem 	TIMER_RESULTS(start, "make_fs");
    227  1.1  lukem 
    228  1.1  lukem 	exit(0);
    229  1.1  lukem 	/* NOTREACHED */
    230  1.1  lukem }
    231  1.1  lukem 
    232  1.1  lukem 
    233  1.1  lukem int
    234  1.1  lukem set_option(option_t *options, const char *var, const char *val)
    235  1.1  lukem {
    236  1.1  lukem 	int	i;
    237  1.1  lukem 
    238  1.1  lukem 	for (i = 0; options[i].name != NULL; i++) {
    239  1.1  lukem 		if (strcmp(options[i].name, var) != 0)
    240  1.1  lukem 			continue;
    241  1.1  lukem 		*options[i].value = (int)strsuftoll(options[i].desc, val,
    242  1.1  lukem 		    options[i].minimum, options[i].maximum);
    243  1.1  lukem 		return (1);
    244  1.1  lukem 	}
    245  1.1  lukem 	warnx("Unknown option `%s'", var);
    246  1.1  lukem 	return (0);
    247  1.1  lukem }
    248  1.1  lukem 
    249  1.1  lukem 
    250  1.1  lukem static fstype_t *
    251  1.1  lukem get_fstype(const char *type)
    252  1.1  lukem {
    253  1.1  lukem 	int i;
    254  1.1  lukem 
    255  1.1  lukem 	for (i = 0; fstypes[i].type != NULL; i++)
    256  1.1  lukem 		if (strcmp(fstypes[i].type, type) == 0)
    257  1.1  lukem 			return (&fstypes[i]);
    258  1.1  lukem 	return (NULL);
    259  1.1  lukem }
    260  1.1  lukem 
    261  1.1  lukem 
    262  1.1  lukem static long long
    263  1.1  lukem strsuftoll(const char *desc, const char *arg, long long min, long long max)
    264  1.1  lukem {
    265  1.1  lukem 	long long	result;
    266  1.1  lukem 	char		*ep;
    267  1.1  lukem 
    268  1.1  lukem 	assert(desc != NULL);
    269  1.1  lukem 	assert(arg != NULL);
    270  1.1  lukem 
    271  1.1  lukem 	errno = 0;
    272  1.1  lukem 	result = strtoll(arg, &ep, 0);
    273  1.1  lukem 	if ((result == LLONG_MIN || result == LLONG_MAX) && errno == ERANGE) {
    274  1.1  lukem 		warn("%s `%s'", desc, arg);
    275  1.1  lukem 		usage();
    276  1.1  lukem 	}
    277  1.1  lukem 	if (ep[0] != '\0' && ep[1] != '\0') {
    278  1.1  lukem 		warnx("`%s' is not a valid number for %s.", optarg, desc);
    279  1.1  lukem 		usage();
    280  1.1  lukem 	}
    281  1.1  lukem 	switch (tolower((unsigned char)ep[0])) {
    282  1.1  lukem 	case '\0':
    283  1.1  lukem 	case 'b':
    284  1.1  lukem 		break;
    285  1.1  lukem 	case 'k':
    286  1.1  lukem 		result <<= 10;
    287  1.1  lukem 		break;
    288  1.1  lukem 	case 'm':
    289  1.1  lukem 		result <<= 20;
    290  1.1  lukem 		break;
    291  1.1  lukem 	case 'g':
    292  1.1  lukem 		result <<= 30;
    293  1.1  lukem 		break;
    294  1.1  lukem 	default:
    295  1.1  lukem 		warnx("`%s' is not a valid suffix for %s.", ep, desc);
    296  1.1  lukem 		usage();
    297  1.1  lukem 	}
    298  1.1  lukem 
    299  1.1  lukem 	if (result < min) {
    300  1.1  lukem 		warnx("%s `%s' (%lld) is less than %lld.",
    301  1.1  lukem 		    desc, optarg, result, min);
    302  1.1  lukem 		usage();
    303  1.1  lukem 	}
    304  1.1  lukem 	if (result > max) {
    305  1.1  lukem 		warnx("%s `%s' (%lld) is greater than %lld.",
    306  1.1  lukem 		    desc, optarg, result, max);
    307  1.1  lukem 		usage();
    308  1.1  lukem 	}
    309  1.1  lukem 	if (debug & DEBUG_STRSUFTOLL)
    310  1.1  lukem 		printf("strsuftoll: got %lld for %s %s\n",
    311  1.1  lukem 		    result, desc, arg);
    312  1.1  lukem 	return (result);
    313  1.1  lukem }
    314  1.1  lukem 
    315  1.1  lukem 
    316  1.1  lukem static void
    317  1.1  lukem usage(void)
    318  1.1  lukem {
    319  1.1  lukem 	const char *prog;
    320  1.1  lukem 
    321  1.1  lukem 	prog = getprogname();
    322  1.1  lukem 	fprintf(stderr,
    323  1.1  lukem "Usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
    324  1.1  lukem "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n"
    325  1.1  lukem "\t[-b free-blocks] [-f free-files] [-F mtree-specfile]\n"
    326  1.1  lukem "\timage-file directory\n",
    327  1.1  lukem 	    prog);
    328  1.1  lukem 	exit(1);
    329  1.1  lukem }
    330