xref: /freebsd/usr.sbin/makefs/makefs.c (revision c6ec7d31830ab1c80edae95ad5e4b9dba10c47ac)
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 	{ "ffs", ffs_prep_opts,	ffs_parse_opts,	ffs_cleanup_opts, ffs_makefs },
70 	{ "cd9660", cd9660_prep_opts, cd9660_parse_opts, cd9660_cleanup_opts,
71 	  cd9660_makefs},
72 	{ .type = NULL	},
73 };
74 
75 u_int		debug;
76 struct timespec	start_time;
77 
78 static	fstype_t *get_fstype(const char *);
79 static	void	usage(void);
80 int		main(int, char *[]);
81 
82 int
83 main(int argc, char *argv[])
84 {
85 	struct stat	 sb;
86 	struct timeval	 start;
87 	fstype_t	*fstype;
88 	fsinfo_t	 fsoptions;
89 	fsnode		*root;
90 	int	 	 ch, i, len;
91 	char		*subtree;
92 	char		*specfile;
93 
94 	setprogname(argv[0]);
95 
96 	debug = 0;
97 	if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
98 		errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
99 
100 		/* set default fsoptions */
101 	(void)memset(&fsoptions, 0, sizeof(fsoptions));
102 	fsoptions.fd = -1;
103 	fsoptions.sectorsize = -1;
104 
105 	if (fstype->prepare_options)
106 		fstype->prepare_options(&fsoptions);
107 
108 	specfile = NULL;
109 	if (gettimeofday(&start, NULL) == -1)
110 		err(1, "Unable to get system time");
111 
112 	start_time.tv_sec = start.tv_sec;
113 	start_time.tv_nsec = start.tv_usec * 1000;
114 
115 	while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:ps:S:t:x")) != -1) {
116 		switch (ch) {
117 
118 		case 'B':
119 			if (strcmp(optarg, "be") == 0 ||
120 			    strcmp(optarg, "4321") == 0 ||
121 			    strcmp(optarg, "big") == 0) {
122 #if BYTE_ORDER == LITTLE_ENDIAN
123 				fsoptions.needswap = 1;
124 #endif
125 			} else if (strcmp(optarg, "le") == 0 ||
126 			    strcmp(optarg, "1234") == 0 ||
127 			    strcmp(optarg, "little") == 0) {
128 #if BYTE_ORDER == BIG_ENDIAN
129 				fsoptions.needswap = 1;
130 #endif
131 			} else {
132 				warnx("Invalid endian `%s'.", optarg);
133 				usage();
134 			}
135 			break;
136 
137 		case 'b':
138 			len = strlen(optarg) - 1;
139 			if (optarg[len] == '%') {
140 				optarg[len] = '\0';
141 				fsoptions.freeblockpc =
142 				    strsuftoll("free block percentage",
143 					optarg, 0, 99);
144 			} else {
145 				fsoptions.freeblocks =
146 				    strsuftoll("free blocks",
147 					optarg, 0, LLONG_MAX);
148 			}
149 			break;
150 
151 		case 'd':
152 			debug = strtoll(optarg, NULL, 0);
153 			break;
154 
155 		case 'f':
156 			len = strlen(optarg) - 1;
157 			if (optarg[len] == '%') {
158 				optarg[len] = '\0';
159 				fsoptions.freefilepc =
160 				    strsuftoll("free file percentage",
161 					optarg, 0, 99);
162 			} else {
163 				fsoptions.freefiles =
164 				    strsuftoll("free files",
165 					optarg, 0, LLONG_MAX);
166 			}
167 			break;
168 
169 		case 'F':
170 			specfile = optarg;
171 			break;
172 
173 		case 'M':
174 			fsoptions.minsize =
175 			    strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
176 			break;
177 
178 		case 'N':
179 			if (! setup_getid(optarg))
180 				errx(1,
181 			    "Unable to use user and group databases in `%s'",
182 				    optarg);
183 			break;
184 
185 		case 'm':
186 			fsoptions.maxsize =
187 			    strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
188 			break;
189 
190 		case 'o':
191 		{
192 			char *p;
193 
194 			while ((p = strsep(&optarg, ",")) != NULL) {
195 				if (*p == '\0')
196 					errx(1, "Empty option");
197 				if (! fstype->parse_options(p, &fsoptions))
198 					usage();
199 			}
200 			break;
201 		}
202 		case 'p':
203 			fsoptions.sparse = 1;
204 			break;
205 
206 		case 's':
207 			fsoptions.minsize = fsoptions.maxsize =
208 			    strsuftoll("size", optarg, 1LL, LLONG_MAX);
209 			break;
210 
211 		case 'S':
212 			fsoptions.sectorsize =
213 			    (int)strsuftoll("sector size", optarg,
214 				1LL, INT_MAX);
215 			break;
216 
217 		case 't':
218 			/* Check current one and cleanup if necessary. */
219 			if (fstype->cleanup_options)
220 				fstype->cleanup_options(&fsoptions);
221 			fsoptions.fs_specific = NULL;
222 			if ((fstype = get_fstype(optarg)) == NULL)
223 				errx(1, "Unknown fs type `%s'.", optarg);
224 			fstype->prepare_options(&fsoptions);
225 			break;
226 
227 		case 'x':
228 			fsoptions.onlyspec = 1;
229 			break;
230 
231 		case '?':
232 		default:
233 			usage();
234 			/* NOTREACHED */
235 
236 		}
237 	}
238 	if (debug) {
239 		printf("debug mask: 0x%08x\n", debug);
240 		printf("start time: %ld.%ld, %s",
241 		    (long)start_time.tv_sec, (long)start_time.tv_nsec,
242 		    ctime(&start_time.tv_sec));
243 	}
244 	argc -= optind;
245 	argv += optind;
246 
247 	if (argc < 2)
248 		usage();
249 
250 	/* -x must be accompanied by -F */
251 	if (fsoptions.onlyspec != 0 && specfile == NULL)
252 		errx(1, "-x requires -F mtree-specfile.");
253 
254 	/* Accept '-' as meaning "read from standard input". */
255 	if (strcmp(argv[1], "-") == 0)
256 		sb.st_mode = S_IFREG;
257 	else {
258 		if (stat(argv[1], &sb) == -1)
259 			err(1, "Can't stat `%s'", argv[1]);
260 	}
261 
262 	switch (sb.st_mode & S_IFMT) {
263 	case S_IFDIR:		/* walk the tree */
264 		subtree = argv[1];
265 		TIMER_START(start);
266 		root = walk_dir(subtree, ".", NULL, NULL);
267 		TIMER_RESULTS(start, "walk_dir");
268 		break;
269 	case S_IFREG:		/* read the manifest file */
270 		subtree = ".";
271 		TIMER_START(start);
272 		root = read_mtree(argv[1], NULL);
273 		TIMER_RESULTS(start, "manifest");
274 		break;
275 	default:
276 		errx(1, "%s: not a file or directory", argv[1]);
277 		/* NOTREACHED */
278 	}
279 
280 	/* append extra directory */
281 	for (i = 2; i < argc; i++) {
282 		if (stat(argv[i], &sb) == -1)
283 			err(1, "Can't stat `%s'", argv[i]);
284 		if (!S_ISDIR(sb.st_mode))
285 			errx(1, "%s: not a directory", argv[i]);
286 		TIMER_START(start);
287 		root = walk_dir(argv[i], ".", NULL, root);
288 		TIMER_RESULTS(start, "walk_dir2");
289 	}
290 
291 	if (specfile) {		/* apply a specfile */
292 		TIMER_START(start);
293 		apply_specfile(specfile, subtree, root, fsoptions.onlyspec);
294 		TIMER_RESULTS(start, "apply_specfile");
295 	}
296 
297 	if (debug & DEBUG_DUMP_FSNODES) {
298 		printf("\nparent: %s\n", subtree);
299 		dump_fsnodes(root);
300 		putchar('\n');
301 	}
302 
303 				/* build the file system */
304 	TIMER_START(start);
305 	fstype->make_fs(argv[0], subtree, root, &fsoptions);
306 	TIMER_RESULTS(start, "make_fs");
307 
308 	free_fsnodes(root);
309 
310 	exit(0);
311 	/* NOTREACHED */
312 }
313 
314 
315 int
316 set_option(option_t *options, const char *var, const char *val)
317 {
318 	int	i;
319 
320 	for (i = 0; options[i].name != NULL; i++) {
321 		if (strcmp(options[i].name, var) != 0)
322 			continue;
323 		*options[i].value = (int)strsuftoll(options[i].desc, val,
324 		    options[i].minimum, options[i].maximum);
325 		return (1);
326 	}
327 	warnx("Unknown option `%s'", var);
328 	return (0);
329 }
330 
331 
332 static fstype_t *
333 get_fstype(const char *type)
334 {
335 	int i;
336 
337 	for (i = 0; fstypes[i].type != NULL; i++)
338 		if (strcmp(fstypes[i].type, type) == 0)
339 			return (&fstypes[i]);
340 	return (NULL);
341 }
342 
343 static void
344 usage(void)
345 {
346 	const char *prog;
347 
348 	prog = getprogname();
349 	fprintf(stderr,
350 "usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
351 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n"
352 "\t[-b free-blocks] [-f free-files] [-F mtree-specfile] [-px]\n"
353 "\t[-N userdb-dir] image-file directory | manifest [extra-directory ...]\n",
354 	    prog);
355 	exit(1);
356 }
357