-
Notifications
You must be signed in to change notification settings - Fork 2
/
makefs.c
441 lines (381 loc) · 10.3 KB
/
makefs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* $NetBSD: makefs.c,v 1.50 2013/08/05 14:41:57 reinoud Exp $ */
/*
* Copyright (c) 2001-2003 Wasabi Systems, Inc.
* All rights reserved.
*
* Written by Luke Mewburn for Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project by
* Wasabi Systems, Inc.
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(__lint)
__RCSID("$NetBSD: makefs.c,v 1.50 2013/08/05 14:41:57 reinoud Exp $");
#endif /* !__lint */
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include "util.h"
#include <sys/time.h>
#include "makefs.h"
#include "mtree.h"
//#include "cd9660.h"
/*
* list of supported file systems and dispatch functions
*/
typedef struct {
const char *type;
void (*prepare_options)(fsinfo_t *);
int (*parse_options)(const char *, fsinfo_t *);
void (*cleanup_options)(fsinfo_t *);
void (*make_fs)(const char *, const char *, fsnode *,
fsinfo_t *);
} fstype_t;
static fstype_t fstypes[] = {
#define ENTRY(name) { \
# name, name ## _prep_opts, name ## _parse_opts, \
name ## _cleanup_opts, name ## _makefs \
}
#if 0
ENTRY(ffs),
ENTRY(cd9660),
ENTRY(chfs),
ENTRY(v7fs),
ENTRY(msdos),
#endif
ENTRY(udf),
{ .type = NULL },
};
u_int debug;
struct timespec start_time;
static fstype_t *get_fstype(const char *);
static void usage(fstype_t *, fsinfo_t *);
int
main(int argc, char *argv[])
{
struct timeval start;
fstype_t *fstype;
fsinfo_t fsoptions;
fsnode *root;
int ch, i, len;
char *specfile;
setprogname(argv[0]);
debug = 0;
if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
/* set default fsoptions */
(void)memset(&fsoptions, 0, sizeof(fsoptions));
fsoptions.fd = -1;
fsoptions.sectorsize = -1;
if (fstype->prepare_options)
fstype->prepare_options(&fsoptions);
specfile = NULL;
if (gettimeofday(&start, NULL) == -1)
err(1, "Unable to get system time");
start_time.tv_sec = start.tv_sec;
start_time.tv_nsec = start.tv_usec * 1000;
while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:O:o:rs:S:t:xZ")) != -1) {
switch (ch) {
case 'B':
if (strcmp(optarg, "be") == 0 ||
strcmp(optarg, "4321") == 0 ||
strcmp(optarg, "big") == 0) {
#if BYTE_ORDER == LITTLE_ENDIAN
fsoptions.needswap = 1;
#endif
} else if (strcmp(optarg, "le") == 0 ||
strcmp(optarg, "1234") == 0 ||
strcmp(optarg, "little") == 0) {
#if BYTE_ORDER == BIG_ENDIAN
fsoptions.needswap = 1;
#endif
} else {
warnx("Invalid endian `%s'.", optarg);
usage(fstype, &fsoptions);
}
break;
case 'b':
len = strlen(optarg) - 1;
if (optarg[len] == '%') {
optarg[len] = '\0';
fsoptions.freeblockpc =
strsuftoll("free block percentage",
optarg, 0LL, 99);
} else {
fsoptions.freeblocks =
strsuftoll("free blocks",
optarg, 0LL, LLONG_MAX);
}
break;
case 'd':
debug = strtoll(optarg, NULL, 0);
break;
case 'f':
len = strlen(optarg) - 1;
if (optarg[len] == '%') {
optarg[len] = '\0';
fsoptions.freefilepc =
strsuftoll("free file percentage",
optarg, 0, 99);
} else {
fsoptions.freefiles =
strsuftoll("free files",
optarg, 0, LLONG_MAX);
}
break;
case 'F':
specfile = optarg;
break;
case 'M':
fsoptions.minsize =
strsuftoll("minimum size", optarg, 1LL, LLONG_MAX);
break;
case 'N':
// if (! setup_getid(optarg))
errx(1,
"Unable to use user and group databases in `%s'",
optarg);
break;
case 'm':
fsoptions.maxsize =
strsuftoll("maximum size", optarg, 1LL, LLONG_MAX);
break;
case 'O':
fsoptions.offset =
strsuftoll("offset", optarg, 0LL, LLONG_MAX);
break;
case 'o':
{
char *p;
while ((p = strsep(&optarg, ",")) != NULL) {
if (*p == '\0')
errx(1, "Empty option");
if (! fstype->parse_options(p, &fsoptions))
usage(fstype, &fsoptions);
}
break;
}
case 'r':
fsoptions.replace = 1;
break;
case 's':
fsoptions.minsize = fsoptions.maxsize =
strsuftoll("size", optarg, 1LL, LLONG_MAX);
break;
case 'S':
fsoptions.sectorsize =
(int)strsuftoll("sector size", optarg,
1LL, INT_MAX);
break;
case 't':
/* Check current one and cleanup if necessary. */
if (fstype->cleanup_options)
fstype->cleanup_options(&fsoptions);
fsoptions.fs_specific = NULL;
if ((fstype = get_fstype(optarg)) == NULL)
errx(1, "Unknown fs type `%s'.", optarg);
fstype->prepare_options(&fsoptions);
break;
case 'x':
fsoptions.onlyspec = 1;
break;
case 'Z':
fsoptions.sparse = 1;
break;
case '?':
default:
usage(fstype, &fsoptions);
/* NOTREACHED */
}
}
if (debug) {
printf("debug mask: 0x%08x\n", debug);
printf("start time: %ld.%ld, %s",
(long)start_time.tv_sec, (long)start_time.tv_nsec,
ctime(&start_time.tv_sec));
}
argc -= optind;
argv += optind;
if (argc < 2)
usage(fstype, &fsoptions);
/* -x must be accompanied by -F */
if (fsoptions.onlyspec != 0 && specfile == NULL)
errx(1, "-x requires -F mtree-specfile.");
/* walk the tree */
TIMER_START(start);
root = walk_dir(argv[1], ".", NULL, NULL, fsoptions.replace);
TIMER_RESULTS(start, "walk_dir");
/* append extra directory */
for (i = 2; i < argc; i++) {
struct stat sb;
if (stat(argv[i], &sb) == -1)
err(1, "Can't stat `%s'", argv[i]);
if (!S_ISDIR(sb.st_mode))
errx(1, "%s: not a directory", argv[i]);
TIMER_START(start);
root = walk_dir(argv[i], ".", NULL, root, fsoptions.replace);
TIMER_RESULTS(start, "walk_dir2");
}
if (specfile) { /* apply a specfile */
TIMER_START(start);
// apply_specfile(specfile, argv[1], root, fsoptions.onlyspec);
TIMER_RESULTS(start, "apply_specfile");
}
if (debug & DEBUG_DUMP_FSNODES) {
printf("\nparent: %s\n", argv[1]);
dump_fsnodes(root);
putchar('\n');
}
/* build the file system */
TIMER_START(start);
fstype->make_fs(argv[0], argv[1], root, &fsoptions);
TIMER_RESULTS(start, "make_fs");
free_fsnodes(root);
exit(0);
/* NOTREACHED */
}
int
set_option(const option_t *options, const char *option, char *buf, size_t len)
{
char *var, *val;
int retval;
assert(option != NULL);
var = strdup(option);
for (val = var; *val; val++)
if (*val == '=') {
*val++ = '\0';
break;
}
retval = set_option_var(options, var, val, buf, len);
free(var);
return retval;
}
int
set_option_var(const option_t *options, const char *var, const char *val,
char *buf, size_t len)
{
char *s;
size_t i;
#define NUM(type) \
if (!*val) { \
*(type *)options[i].value = 1; \
break; \
} \
*(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \
options[i].minimum, options[i].maximum); break
for (i = 0; options[i].name != NULL; i++) {
if (var[1] == '\0') {
if (options[i].letter != var[0])
continue;
} else if (strcmp(options[i].name, var) != 0)
continue;
switch (options[i].type) {
case OPT_BOOL:
*(bool *)options[i].value = 1;
break;
case OPT_STRARRAY:
strlcpy((void *)options[i].value, val, (size_t)
options[i].maximum);
break;
case OPT_STRPTR:
s = strdup(val);
*(char **)options[i].value = s;
break;
case OPT_STRBUF:
if (buf == NULL)
abort();
strlcpy(buf, val, len);
break;
case OPT_INT64:
NUM(uint64_t);
case OPT_INT32:
NUM(uint32_t);
case OPT_INT16:
NUM(uint16_t);
case OPT_INT8:
NUM(uint8_t);
default:
warnx("Unknown type %d in option %s", options[i].type,
val);
return 0;
}
return i;
}
warnx("Unknown option `%s'", var);
return -1;
}
static fstype_t *
get_fstype(const char *type)
{
int i;
for (i = 0; fstypes[i].type != NULL; i++)
if (strcmp(fstypes[i].type, type) == 0)
return (&fstypes[i]);
return (NULL);
}
option_t *
copy_opts(const option_t *o)
{
size_t i;
for (i = 0; o[i].name; i++)
continue;
i++;
return memcpy(calloc(i, sizeof(*o)), o, i * sizeof(*o));
}
static void
usage(fstype_t *fstype, fsinfo_t *fsoptions)
{
const char *prog;
prog = getprogname();
fprintf(stderr,
"Usage: %s [-rxZ] [-B endian] [-b free-blocks] [-d debug-mask]\n"
"\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n"
"\t[-N userdb-dir] [-O offset] [-o fs-options] [-S sector-size]\n"
"\t[-s image-size] [-t fs-type] image-file directory [extra-directory ...]\n",
"");
if (fstype) {
size_t i;
option_t *o = fsoptions->fs_options;
fprintf(stderr, "\n%s specific options:\n", fstype->type);
for (i = 0; o[i].name != NULL; i++)
fprintf(stderr, "\t%c%c%20.20s\t%s\n",
o[i].letter ? o[i].letter : ' ',
o[i].letter ? ',' : ' ',
o[i].name, o[i].desc);
}
exit(1);
}