1 /* $OpenBSD: makefs.c,v 1.22 2022/12/04 23:50:51 cheloha Exp $ */
2 /* $NetBSD: makefs.c,v 1.53 2015/11/27 15:10:32 joerg Exp $ */
3
4 /*
5 * Copyright (c) 2001-2003 Wasabi Systems, Inc.
6 * All rights reserved.
7 *
8 * Written by Luke Mewburn for Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the NetBSD Project by
21 * Wasabi Systems, Inc.
22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <util.h>
47
48 #include "makefs.h"
49
50 /*
51 * list of supported file systems and dispatch functions
52 */
53 typedef struct {
54 const char *type;
55 void (*prepare_options)(fsinfo_t *);
56 int (*parse_options)(const char *, fsinfo_t *);
57 void (*cleanup_options)(fsinfo_t *);
58 void (*make_fs)(const char *, const char *, fsnode *,
59 fsinfo_t *);
60 } fstype_t;
61
62 static fstype_t fstypes[] = {
63 #define ENTRY(name) { \
64 # name, name ## _prep_opts, name ## _parse_opts, \
65 name ## _cleanup_opts, name ## _makefs \
66 }
67 ENTRY(ffs),
68 ENTRY(cd9660),
69 ENTRY(msdos),
70 { .type = NULL },
71 };
72
73 int Tflag;
74 time_t stampts;
75 struct timespec start_time;
76
77 static fstype_t *get_fstype(const char *);
78 static time_t get_tstamp(const char *);
79 static long long strsuftoll(const char *, const char *, long long, long long);
80 static __dead void usage(void);
81
82 int
main(int argc,char * argv[])83 main(int argc, char *argv[])
84 {
85 fstype_t *fstype;
86 fsinfo_t fsoptions;
87 fsnode *root;
88 int ch, len;
89
90 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
91 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
92
93 /* set default fsoptions */
94 (void)memset(&fsoptions, 0, sizeof(fsoptions));
95 fsoptions.fd = -1;
96 fsoptions.sectorsize = -1;
97
98 if (fstype->prepare_options)
99 fstype->prepare_options(&fsoptions);
100
101 ch = clock_gettime(CLOCK_REALTIME, &start_time);
102 if (ch == -1)
103 err(1, "Unable to get system time");
104
105
106 while ((ch = getopt(argc, argv, "b:f:M:m:O:o:s:S:t:T:")) != -1) {
107 switch (ch) {
108 case 'b':
109 len = strlen(optarg) - 1;
110 if (optarg[len] == '%') {
111 optarg[len] = '\0';
112 fsoptions.freeblockpc =
113 strsuftoll("free block percentage",
114 optarg, 0, 99);
115 } else {
116 fsoptions.freeblocks =
117 strsuftoll("free blocks",
118 optarg, 0, LLONG_MAX);
119 }
120 break;
121
122 case 'f':
123 len = strlen(optarg) - 1;
124 if (optarg[len] == '%') {
125 optarg[len] = '\0';
126 fsoptions.freefilepc =
127 strsuftoll("free file percentage",
128 optarg, 0, 99);
129 } else {
130 fsoptions.freefiles =
131 strsuftoll("free files",
132 optarg, 0, LLONG_MAX);
133 }
134 break;
135
136 case 'M':
137 fsoptions.minsize =
138 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
139 break;
140
141 case 'm':
142 fsoptions.maxsize =
143 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
144 break;
145
146 case 'O':
147 fsoptions.offset =
148 strsuftoll("offset", optarg, 0LL, LLONG_MAX);
149 break;
150
151 case 'o':
152 {
153 char *p;
154
155 while ((p = strsep(&optarg, ",")) != NULL) {
156 if (*p == '\0')
157 errx(1, "Empty option");
158 if (! fstype->parse_options(p, &fsoptions))
159 usage();
160 }
161 break;
162 }
163
164 case 's':
165 fsoptions.minsize = fsoptions.maxsize =
166 strsuftoll("size", optarg, 1LL, LLONG_MAX);
167 break;
168
169 case 'S':
170 fsoptions.sectorsize =
171 (int)strsuftoll("sector size", optarg,
172 1LL, INT_MAX);
173 break;
174
175 case 't':
176 /* Check current one and cleanup if necessary. */
177 if (fstype->cleanup_options)
178 fstype->cleanup_options(&fsoptions);
179 fsoptions.fs_specific = NULL;
180 if ((fstype = get_fstype(optarg)) == NULL)
181 errx(1, "Unknown fs type `%s'.", optarg);
182 fstype->prepare_options(&fsoptions);
183 break;
184
185 case 'T':
186 Tflag = 1;
187 stampts = get_tstamp(optarg);
188 break;
189
190 default:
191 usage();
192 }
193 }
194 argc -= optind;
195 argv += optind;
196
197 if (argc != 2)
198 usage();
199
200 if (unveil(argv[0], "rwc") == -1)
201 err(1, "unveil %s", argv[0]);
202 if (unveil(argv[1], "rw") == -1)
203 err(1, "unveil %s", argv[1]);
204 if (pledge("stdio rpath wpath cpath", NULL) == -1)
205 err(1, "pledge");
206
207 /* walk the tree */
208 root = walk_dir(argv[1], ".", NULL, NULL);
209
210 /* build the file system */
211 fstype->make_fs(argv[0], argv[1], root, &fsoptions);
212
213 free_fsnodes(root);
214
215 exit(0);
216 }
217
218 int
set_option(const option_t * options,const char * option,char * buf,size_t len)219 set_option(const option_t *options, const char *option, char *buf, size_t len)
220 {
221 char *var, *val;
222 int retval;
223
224 assert(option != NULL);
225
226 var = estrdup(option);
227 for (val = var; *val; val++)
228 if (*val == '=') {
229 *val++ = '\0';
230 break;
231 }
232 retval = set_option_var(options, var, val, buf, len);
233 free(var);
234 return retval;
235 }
236
237 int
set_option_var(const option_t * options,const char * var,const char * val,char * buf,size_t len)238 set_option_var(const option_t *options, const char *var, const char *val,
239 char *buf, size_t len)
240 {
241 char *s;
242 size_t i;
243
244 #define NUM(type) \
245 if (!*val) { \
246 *(type *)options[i].value = 1; \
247 break; \
248 } \
249 *(type *)options[i].value = (type)strsuftoll(options[i].name, val, \
250 options[i].minimum, options[i].maximum); break
251
252 for (i = 0; options[i].name != NULL; i++) {
253 if (strcmp(options[i].name, var) != 0)
254 continue;
255 switch (options[i].type) {
256 case OPT_BOOL:
257 *(int *)options[i].value = 1;
258 break;
259 case OPT_STRARRAY:
260 strlcpy((void *)options[i].value, val, (size_t)
261 options[i].maximum);
262 break;
263 case OPT_STRPTR:
264 s = estrdup(val);
265 *(char **)options[i].value = s;
266 break;
267 case OPT_STRBUF:
268 if (buf == NULL)
269 abort();
270 strlcpy(buf, val, len);
271 break;
272 case OPT_INT64:
273 NUM(uint64_t);
274 case OPT_INT32:
275 NUM(uint32_t);
276 case OPT_INT16:
277 NUM(uint16_t);
278 case OPT_INT8:
279 NUM(uint8_t);
280 default:
281 warnx("Unknown type %d in option %s", options[i].type,
282 val);
283 return 0;
284 }
285 return i;
286 }
287 warnx("Unknown option `%s'", var);
288 return -1;
289 }
290
291
292 static fstype_t *
get_fstype(const char * type)293 get_fstype(const char *type)
294 {
295 int i;
296
297 for (i = 0; fstypes[i].type != NULL; i++)
298 if (strcmp(fstypes[i].type, type) == 0)
299 return (&fstypes[i]);
300 return (NULL);
301 }
302
303 option_t *
copy_opts(const option_t * o)304 copy_opts(const option_t *o)
305 {
306 size_t i;
307 for (i = 0; o[i].name; i++)
308 continue;
309 i++;
310 return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o));
311 }
312
313 static time_t
get_tstamp(const char * b)314 get_tstamp(const char *b)
315 {
316 time_t when;
317 char *eb;
318
319 errno = 0;
320 when = strtoll(b, &eb, 0);
321 if (b == eb || *eb || errno) {
322 errx(1, "Cannot get timestamp from `%s'",
323 optarg);
324 }
325 return when;
326 }
327
328 /* XXX */
329 static long long
strsuftoll(const char * desc,const char * val,long long min,long long max)330 strsuftoll(const char *desc, const char *val, long long min, long long max)
331 {
332 long long res;
333
334 if (scan_scaled((char *)val, &res) == -1)
335 err(1, "%s", desc);
336 if (res < min || res > max)
337 errc(1, ERANGE, "%s", desc);
338 return res;
339 }
340
341 static void
usage(void)342 usage(void)
343 {
344 extern char *__progname;
345
346 fprintf(stderr,
347 "usage: %s [-b free-blocks] [-f free-files] [-M minimum-size]\n"
348 "\t[-m maximum-size] [-O offset] [-o fs-options] [-S sector-size]\n"
349 "\t[-s image-size] [-T timestamp] [-t fs-type] image-file directory\n",
350 __progname);
351
352 exit(1);
353 }
354