makefs.c revision 1.10 1 /* $NetBSD: makefs.c,v 1.10 2002/01/22 13:03:57 is Exp $ */
2
3 /*
4 * Copyright (c) 2001 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 #ifndef __lint
40 __RCSID("$NetBSD: makefs.c,v 1.10 2002/01/22 13:03:57 is Exp $");
41 #endif /* !__lint */
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 #include <assert.h>
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <limits.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #include "makefs.h"
58 #include "strsuftoull.h"
59
60
61 /*
62 * list of supported file systems and dispatch functions
63 */
64 typedef struct {
65 const char *type;
66 int (*parse_options)(const char *, fsinfo_t *);
67 void (*make_fs)(const char *, const char *, fsnode *,
68 fsinfo_t *);
69 } fstype_t;
70
71 static fstype_t fstypes[] = {
72 { "ffs", ffs_parse_opts, ffs_makefs },
73 { NULL },
74 };
75
76 uint debug;
77 struct timespec start_time;
78
79
80 static fstype_t *get_fstype(const char *);
81 static void usage(void);
82 int main(int, char *[]);
83
84 int
85 main(int argc, char *argv[])
86 {
87 struct timeval start;
88 fstype_t *fstype;
89 fsinfo_t fsoptions;
90 fsnode *root;
91 int ch, len;
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 fsoptions.bsize= -1;
105 fsoptions.fsize= -1;
106 fsoptions.cpg= -1;
107 fsoptions.density= -1;
108 fsoptions.ntracks= -1;
109 fsoptions.nsectors= -1;
110 fsoptions.rpm= -1;
111 fsoptions.minfree= -1;
112 fsoptions.optimization= -1;
113 fsoptions.maxcontig= -1;
114 fsoptions.rotdelay= -1;
115 fsoptions.maxbpg= -1;
116 fsoptions.nrpos= -1;
117 fsoptions.avgfilesize= -1;
118 fsoptions.avgfpdir= -1;
119
120 specfile = NULL;
121 if (gettimeofday(&start, NULL) == -1)
122 err(1, "Unable to get system time");
123 TIMEVAL_TO_TIMESPEC(&start, &start_time);
124
125 while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:o:s:S:t:")) != -1) {
126 switch (ch) {
127
128 case 'B':
129 if (strcmp(optarg, "be") == 0 ||
130 strcmp(optarg, "big") == 0) {
131 #if BYTE_ORDER == LITTLE_ENDIAN
132 fsoptions.needswap = 1;
133 #endif
134 } else if (strcmp(optarg, "le") == 0 ||
135 strcmp(optarg, "little") == 0) {
136 #if BYTE_ORDER == BIG_ENDIAN
137 fsoptions.needswap = 1;
138 #endif
139 } else {
140 warnx("Invalid endian `%s'.", optarg);
141 usage();
142 }
143 break;
144
145 case 'b':
146 len = strlen(optarg) - 1;
147 if (optarg[len] == '%') {
148 optarg[len] = '\0';
149 fsoptions.freeblockpc =
150 strsuftoull("free block percentage",
151 optarg, 0, 99);
152 } else {
153 fsoptions.freeblocks =
154 strsuftoull("free blocks",
155 optarg, 0, LLONG_MAX);
156 }
157 break;
158
159 case 'd':
160 debug =
161 (int)strsuftoull("debug mask", optarg, 0, UINT_MAX);
162 break;
163
164 case 'f':
165 len = strlen(optarg) - 1;
166 if (optarg[len] == '%') {
167 optarg[len] = '\0';
168 fsoptions.freefilepc =
169 strsuftoull("free file percentage",
170 optarg, 0, 99);
171 } else {
172 fsoptions.freefiles =
173 strsuftoull("free files",
174 optarg, 0, LLONG_MAX);
175 }
176 break;
177
178 case 'F':
179 specfile = optarg;
180 break;
181
182 case 'M':
183 fsoptions.minsize =
184 strsuftoull("minimum size", optarg, 1LL, LLONG_MAX);
185 break;
186
187 case 'm':
188 fsoptions.maxsize =
189 strsuftoull("maximum size", optarg, 1LL, LLONG_MAX);
190 break;
191
192 case 'o':
193 {
194 char *p;
195
196 while ((p = strsep(&optarg, ",")) != NULL) {
197 if (*p == '\0')
198 errx(1, "Empty option");
199 if (! fstype->parse_options(p, &fsoptions))
200 usage();
201 }
202 break;
203 }
204
205 case 's':
206 fsoptions.minsize = fsoptions.maxsize =
207 strsuftoull("size", optarg, 1LL, LLONG_MAX);
208 break;
209
210 case 'S':
211 fsoptions.sectorsize =
212 (int)strsuftoull("sector size", optarg,
213 1LL, INT_MAX);
214 break;
215
216 case 't':
217 if ((fstype = get_fstype(optarg)) == NULL)
218 errx(1, "Unknown fs type `%s'.", optarg);
219 break;
220
221 case '?':
222 default:
223 usage();
224 /* NOTREACHED */
225
226 }
227 }
228 if (debug) {
229 printf("debug mask: 0x%08x\n", debug);
230 printf("start time: %ld.%ld, %s",
231 (long)start_time.tv_sec, (long)start_time.tv_nsec,
232 ctime(&start_time.tv_sec));
233 }
234 argc -= optind;
235 argv += optind;
236
237 if (argc != 2)
238 usage();
239
240 /* walk the tree */
241 TIMER_START(start);
242 root = walk_dir(argv[1], NULL);
243 TIMER_RESULTS(start, "walk_dir");
244
245 if (specfile) { /* apply a specfile */
246 TIMER_START(start);
247 apply_specfile(specfile, argv[1], root);
248 TIMER_RESULTS(start, "apply_specfile");
249 }
250
251 if (debug & DEBUG_DUMP_FSNODES) {
252 putchar('\n');
253 dump_fsnodes(argv[1], root);
254 putchar('\n');
255 }
256
257 /* build the file system */
258 TIMER_START(start);
259 fstype->make_fs(argv[0], argv[1], root, &fsoptions);
260 TIMER_RESULTS(start, "make_fs");
261
262 exit(0);
263 /* NOTREACHED */
264 }
265
266
267 int
268 set_option(option_t *options, const char *var, const char *val)
269 {
270 int i;
271
272 for (i = 0; options[i].name != NULL; i++) {
273 if (strcmp(options[i].name, var) != 0)
274 continue;
275 *options[i].value = (int)strsuftoull(options[i].desc, val,
276 options[i].minimum, options[i].maximum);
277 return (1);
278 }
279 warnx("Unknown option `%s'", var);
280 return (0);
281 }
282
283
284 static fstype_t *
285 get_fstype(const char *type)
286 {
287 int i;
288
289 for (i = 0; fstypes[i].type != NULL; i++)
290 if (strcmp(fstypes[i].type, type) == 0)
291 return (&fstypes[i]);
292 return (NULL);
293 }
294
295 static void
296 usage(void)
297 {
298 const char *prog;
299
300 prog = getprogname();
301 fprintf(stderr,
302 "Usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n"
303 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n"
304 "\t[-b free-blocks] [-f free-files] [-F mtree-specfile]\n"
305 "\timage-file directory\n",
306 prog);
307 exit(1);
308 }
309