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