xref: /freebsd/usr.sbin/makefs/makefs.c (revision bd18fd57db1df29da1a3adf94d47924a977a29c2)
1 /*	$NetBSD: makefs.c,v 1.26 2006/10/22 21:11:56 christos 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 __FBSDID("$FreeBSD$");
40 
41 #include <sys/types.h>
42 #include <sys/stat.h>
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 <time.h>
51 #include <unistd.h>
52 
53 #include "makefs.h"
54 #include "mtree.h"
55 
56 /*
57  * list of supported file systems and dispatch functions
58  */
59 typedef struct {
60 	const char	*type;
61 	void		(*prepare_options)(fsinfo_t *);
62 	int		(*parse_options)(const char *, fsinfo_t *);
63 	void		(*cleanup_options)(fsinfo_t *);
64 	void		(*make_fs)(const char *, const char *, fsnode *,
65 				fsinfo_t *);
66 } fstype_t;
67 
68 static fstype_t fstypes[] = {
69 #define ENTRY(name) { \
70 	# name, name ## _prep_opts, name ## _parse_opts, \
71 	name ## _cleanup_opts, name ## _makefs  \
72 }
73 	ENTRY(ffs),
74 	ENTRY(cd9660),
75 	{ .type = NULL	},
76 };
77 
78 u_int		debug;
79 int		dupsok;
80 struct timespec	start_time;
81 
82 static	fstype_t *get_fstype(const char *);
83 static	void	usage(void);
84 int		main(int, char *[]);
85 
86 int
87 main(int argc, char *argv[])
88 {
89 	struct stat	 sb;
90 	struct timeval	 start;
91 	fstype_t	*fstype;
92 	fsinfo_t	 fsoptions;
93 	fsnode		*root;
94 	int	 	 ch, i, len;
95 	char		*subtree;
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:Dd:f:F:M:m:N:o:pR:s:S:t:xZ")) != -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 			dupsok = 1;
157 			break;
158 
159 		case 'd':
160 			debug = strtoll(optarg, NULL, 0);
161 			break;
162 
163 		case 'f':
164 			len = strlen(optarg) - 1;
165 			if (optarg[len] == '%') {
166 				optarg[len] = '\0';
167 				fsoptions.freefilepc =
168 				    strsuftoll("free file percentage",
169 					optarg, 0, 99);
170 			} else {
171 				fsoptions.freefiles =
172 				    strsuftoll("free files",
173 					optarg, 0, LLONG_MAX);
174 			}
175 			break;
176 
177 		case 'F':
178 			specfile = optarg;
179 			break;
180 
181 		case 'M':
182 			fsoptions.minsize =
183 			    strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
184 			break;
185 
186 		case 'N':
187 			if (! setup_getid(optarg))
188 				errx(1,
189 			    "Unable to use user and group databases in `%s'",
190 				    optarg);
191 			break;
192 
193 		case 'm':
194 			fsoptions.maxsize =
195 			    strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
196 			break;
197 
198 		case 'o':
199 		{
200 			char *p;
201 
202 			while ((p = strsep(&optarg, ",")) != NULL) {
203 				if (*p == '\0')
204 					errx(1, "Empty option");
205 				if (! fstype->parse_options(p, &fsoptions))
206 					usage();
207 			}
208 			break;
209 		}
210 		case 'p':
211 			/* Deprecated in favor of 'Z' */
212 			fsoptions.sparse = 1;
213 			break;
214 
215 		case 'R':
216 			/* Round image size up to specified block size */
217 			fsoptions.roundup =
218 			    strsuftoll("roundup-size", optarg, 0, LLONG_MAX);
219 			break;
220 
221 		case 's':
222 			fsoptions.minsize = fsoptions.maxsize =
223 			    strsuftoll("size", optarg, 1LL, LLONG_MAX);
224 			break;
225 
226 		case 'S':
227 			fsoptions.sectorsize =
228 			    (int)strsuftoll("sector size", optarg,
229 				1LL, INT_MAX);
230 			break;
231 
232 		case 't':
233 			/* Check current one and cleanup if necessary. */
234 			if (fstype->cleanup_options)
235 				fstype->cleanup_options(&fsoptions);
236 			fsoptions.fs_specific = NULL;
237 			if ((fstype = get_fstype(optarg)) == NULL)
238 				errx(1, "Unknown fs type `%s'.", optarg);
239 			fstype->prepare_options(&fsoptions);
240 			break;
241 
242 		case 'x':
243 			fsoptions.onlyspec = 1;
244 			break;
245 
246 		case 'Z':
247 			/* Superscedes 'p' for compatibility with NetBSD makefs(8) */
248 			fsoptions.sparse = 1;
249 			break;
250 
251 		case '?':
252 		default:
253 			usage();
254 			/* NOTREACHED */
255 
256 		}
257 	}
258 	if (debug) {
259 		printf("debug mask: 0x%08x\n", debug);
260 		printf("start time: %ld.%ld, %s",
261 		    (long)start_time.tv_sec, (long)start_time.tv_nsec,
262 		    ctime(&start_time.tv_sec));
263 	}
264 	argc -= optind;
265 	argv += optind;
266 
267 	if (argc < 2)
268 		usage();
269 
270 	/* -x must be accompanied by -F */
271 	if (fsoptions.onlyspec != 0 && specfile == NULL)
272 		errx(1, "-x requires -F mtree-specfile.");
273 
274 	/* Accept '-' as meaning "read from standard input". */
275 	if (strcmp(argv[1], "-") == 0)
276 		sb.st_mode = S_IFREG;
277 	else {
278 		if (stat(argv[1], &sb) == -1)
279 			err(1, "Can't stat `%s'", argv[1]);
280 	}
281 
282 	switch (sb.st_mode & S_IFMT) {
283 	case S_IFDIR:		/* walk the tree */
284 		subtree = argv[1];
285 		TIMER_START(start);
286 		root = walk_dir(subtree, ".", NULL, NULL);
287 		TIMER_RESULTS(start, "walk_dir");
288 		break;
289 	case S_IFREG:		/* read the manifest file */
290 		subtree = ".";
291 		TIMER_START(start);
292 		root = read_mtree(argv[1], NULL);
293 		TIMER_RESULTS(start, "manifest");
294 		break;
295 	default:
296 		errx(1, "%s: not a file or directory", argv[1]);
297 		/* NOTREACHED */
298 	}
299 
300 	/* append extra directory */
301 	for (i = 2; i < argc; i++) {
302 		if (stat(argv[i], &sb) == -1)
303 			err(1, "Can't stat `%s'", argv[i]);
304 		if (!S_ISDIR(sb.st_mode))
305 			errx(1, "%s: not a directory", argv[i]);
306 		TIMER_START(start);
307 		root = walk_dir(argv[i], ".", NULL, root);
308 		TIMER_RESULTS(start, "walk_dir2");
309 	}
310 
311 	if (specfile) {		/* apply a specfile */
312 		TIMER_START(start);
313 		apply_specfile(specfile, subtree, root, fsoptions.onlyspec);
314 		TIMER_RESULTS(start, "apply_specfile");
315 	}
316 
317 	if (debug & DEBUG_DUMP_FSNODES) {
318 		printf("\nparent: %s\n", subtree);
319 		dump_fsnodes(root);
320 		putchar('\n');
321 	}
322 
323 				/* build the file system */
324 	TIMER_START(start);
325 	fstype->make_fs(argv[0], subtree, root, &fsoptions);
326 	TIMER_RESULTS(start, "make_fs");
327 
328 	free_fsnodes(root);
329 
330 	exit(0);
331 	/* NOTREACHED */
332 }
333 
334 
335 int
336 set_option(option_t *options, const char *var, const char *val)
337 {
338 	int	i;
339 
340 	for (i = 0; options[i].name != NULL; i++) {
341 		if (strcmp(options[i].name, var) != 0)
342 			continue;
343 		*options[i].value = (int)strsuftoll(options[i].desc, val,
344 		    options[i].minimum, options[i].maximum);
345 		return (1);
346 	}
347 	warnx("Unknown option `%s'", var);
348 	return (0);
349 }
350 
351 
352 static fstype_t *
353 get_fstype(const char *type)
354 {
355 	int i;
356 
357 	for (i = 0; fstypes[i].type != NULL; i++)
358 		if (strcmp(fstypes[i].type, type) == 0)
359 			return (&fstypes[i]);
360 	return (NULL);
361 }
362 
363 static void
364 usage(void)
365 {
366 	const char *prog;
367 
368 	prog = getprogname();
369 	fprintf(stderr,
370 "usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
371 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-R roundup-size]\n"
372 "\t[-s image-size] [-b free-blocks] [-f free-files] [-F mtree-specfile]\n"
373 "\t[-xZ] [-N userdb-dir] image-file directory | manifest [extra-directory ...]\n",
374 	    prog);
375 	exit(1);
376 }
377