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