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