1 /* $NetBSD: makefs.c,v 1.56 2023/12/28 12:13:55 tsutsui 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 #if HAVE_NBTOOL_CONFIG_H 39 #include "nbtool_config.h" 40 #endif 41 42 #include <sys/cdefs.h> 43 #if defined(__RCSID) && !defined(__lint) 44 __RCSID("$NetBSD: makefs.c,v 1.56 2023/12/28 12:13:55 tsutsui Exp $"); 45 #endif /* !__lint */ 46 47 #include <assert.h> 48 #include <ctype.h> 49 #include <errno.h> 50 #include <limits.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 #include <stdbool.h> 56 #include <util.h> 57 58 #include "makefs.h" 59 #include "mtree.h" 60 #include "cd9660.h" 61 62 /* 63 * list of supported file systems and dispatch functions 64 */ 65 typedef struct { 66 const char *type; 67 void (*prepare_options)(fsinfo_t *); 68 int (*parse_options)(const char *, fsinfo_t *); 69 void (*cleanup_options)(fsinfo_t *); 70 void (*make_fs)(const char *, const char *, fsnode *, 71 fsinfo_t *); 72 } fstype_t; 73 74 static fstype_t fstypes[] = { 75 #define ENTRY(name) { \ 76 # name, name ## _prep_opts, name ## _parse_opts, \ 77 name ## _cleanup_opts, name ## _makefs \ 78 } 79 ENTRY(ffs), 80 ENTRY(cd9660), 81 ENTRY(chfs), 82 ENTRY(v7fs), 83 ENTRY(msdos), 84 ENTRY(udf), 85 { .type = NULL }, 86 }; 87 88 u_int debug; 89 struct timespec start_time; 90 struct stat stampst; 91 92 static fstype_t *get_fstype(const char *); 93 static int get_tstamp(const char *, struct stat *); 94 static void usage(fstype_t *, fsinfo_t *) __dead; 95 96 int 97 main(int argc, char *argv[]) 98 { 99 struct timeval start; 100 fstype_t *fstype; 101 fsinfo_t fsoptions; 102 fsnode *root; 103 int ch, i, len; 104 char *specfile; 105 106 setprogname(argv[0]); 107 108 debug = 0; 109 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL) 110 errx(EXIT_FAILURE, 111 "Unknown default fs type `%s'.", DEFAULT_FSTYPE); 112 113 /* set default fsoptions */ 114 (void)memset(&fsoptions, 0, sizeof(fsoptions)); 115 fsoptions.fd = -1; 116 fsoptions.sectorsize = -1; 117 118 if (fstype->prepare_options) 119 fstype->prepare_options(&fsoptions); 120 121 specfile = NULL; 122 #ifdef CLOCK_REALTIME 123 ch = clock_gettime(CLOCK_REALTIME, &start_time); 124 #else 125 ch = gettimeofday(&start, NULL); 126 start_time.tv_sec = start.tv_sec; 127 start_time.tv_nsec = start.tv_usec * 1000; 128 #endif 129 if (ch == -1) 130 err(EXIT_FAILURE, "Unable to get system time"); 131 132 133 while ((ch = getopt(argc, argv, "B:b:d:f:F:LM:m:N:O:o:rs:S:t:T:xZ")) != -1) { 134 switch (ch) { 135 136 case 'B': 137 if (strcmp(optarg, "be") == 0 || 138 strcmp(optarg, "4321") == 0 || 139 strcmp(optarg, "big") == 0) { 140 #if BYTE_ORDER == LITTLE_ENDIAN 141 fsoptions.needswap = 1; 142 #endif 143 } else if (strcmp(optarg, "le") == 0 || 144 strcmp(optarg, "1234") == 0 || 145 strcmp(optarg, "little") == 0) { 146 #if BYTE_ORDER == BIG_ENDIAN 147 fsoptions.needswap = 1; 148 #endif 149 } else { 150 warnx("Invalid endian `%s'.", optarg); 151 usage(fstype, &fsoptions); 152 } 153 break; 154 155 case 'b': 156 len = strlen(optarg) - 1; 157 if (optarg[len] == '%') { 158 optarg[len] = '\0'; 159 fsoptions.freeblockpc = 160 strsuftoll("free block percentage", 161 optarg, 0, 99); 162 } else { 163 fsoptions.freeblocks = 164 strsuftoll("free blocks", 165 optarg, 0, LLONG_MAX); 166 } 167 break; 168 169 case 'd': 170 debug = strtoll(optarg, NULL, 0); 171 break; 172 173 case 'f': 174 len = strlen(optarg) - 1; 175 if (optarg[len] == '%') { 176 optarg[len] = '\0'; 177 fsoptions.freefilepc = 178 strsuftoll("free file percentage", 179 optarg, 0, 99); 180 } else { 181 fsoptions.freefiles = 182 strsuftoll("free files", 183 optarg, 0, LLONG_MAX); 184 } 185 break; 186 187 case 'F': 188 specfile = optarg; 189 break; 190 191 case 'L': 192 fsoptions.follow = true; 193 break; 194 195 case 'M': 196 fsoptions.minsize = 197 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX); 198 break; 199 200 case 'N': 201 if (! setup_getid(optarg)) 202 errx(EXIT_FAILURE, 203 "Unable to use user and group databases in `%s'", 204 optarg); 205 break; 206 207 case 'm': 208 fsoptions.maxsize = 209 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX); 210 break; 211 212 case 'O': 213 fsoptions.offset = 214 strsuftoll("offset", optarg, 0LL, LLONG_MAX); 215 break; 216 217 case 'o': 218 { 219 char *p; 220 221 while ((p = strsep(&optarg, ",")) != NULL) { 222 if (*p == '\0') 223 errx(EXIT_FAILURE, "Empty option"); 224 if (! fstype->parse_options(p, &fsoptions)) 225 usage(fstype, &fsoptions); 226 } 227 break; 228 } 229 230 case 'r': 231 fsoptions.replace = 1; 232 break; 233 234 case 's': 235 fsoptions.minsize = fsoptions.maxsize = 236 strsuftoll("size", optarg, 1LL, LLONG_MAX); 237 break; 238 239 case 'S': 240 fsoptions.sectorsize = 241 (int)strsuftoll("sector size", optarg, 242 1LL, INT_MAX); 243 break; 244 245 case 't': 246 /* Check current one and cleanup if necessary. */ 247 if (fstype->cleanup_options) 248 fstype->cleanup_options(&fsoptions); 249 fsoptions.fs_specific = NULL; 250 if ((fstype = get_fstype(optarg)) == NULL) 251 errx(EXIT_FAILURE, 252 "Unknown fs type `%s'.", optarg); 253 fstype->prepare_options(&fsoptions); 254 break; 255 256 case 'T': 257 if (get_tstamp(optarg, &stampst) == -1) 258 errx(EXIT_FAILURE, 259 "Cannot get timestamp from `%s'", optarg); 260 break; 261 262 case 'x': 263 fsoptions.onlyspec = 1; 264 break; 265 266 case 'Z': 267 fsoptions.sparse = 1; 268 break; 269 270 case '?': 271 default: 272 usage(fstype, &fsoptions); 273 /* NOTREACHED */ 274 275 } 276 } 277 if (debug) { 278 printf("debug mask: 0x%08x\n", debug); 279 printf("start time: %ld.%ld, %s", 280 (long)start_time.tv_sec, (long)start_time.tv_nsec, 281 ctime(&start_time.tv_sec)); 282 } 283 argc -= optind; 284 argv += optind; 285 286 if (argc < 2) 287 usage(fstype, &fsoptions); 288 289 /* -x must be accompanied by -F */ 290 if (fsoptions.onlyspec != 0 && specfile == NULL) 291 errx(EXIT_FAILURE, "-x requires -F mtree-specfile."); 292 293 /* walk the tree */ 294 TIMER_START(start); 295 root = walk_dir(argv[1], ".", NULL, NULL, fsoptions.replace, 296 fsoptions.follow); 297 TIMER_RESULTS(start, "walk_dir"); 298 299 /* append extra directory */ 300 for (i = 2; i < argc; i++) { 301 struct stat sb; 302 if (stat(argv[i], &sb) == -1) 303 err(EXIT_FAILURE, "Can't stat `%s'", argv[i]); 304 if (!S_ISDIR(sb.st_mode)) 305 errx(EXIT_FAILURE, "%s: not a directory", argv[i]); 306 TIMER_START(start); 307 root = walk_dir(argv[i], ".", NULL, root, fsoptions.replace, 308 fsoptions.follow); 309 TIMER_RESULTS(start, "walk_dir2"); 310 } 311 312 if (specfile) { /* apply a specfile */ 313 TIMER_START(start); 314 apply_specfile(specfile, argv[1], root, fsoptions.onlyspec); 315 TIMER_RESULTS(start, "apply_specfile"); 316 } 317 318 if (debug & DEBUG_DUMP_FSNODES) { 319 printf("\nparent: %s\n", argv[1]); 320 dump_fsnodes(root); 321 putchar('\n'); 322 } 323 324 /* build the file system */ 325 TIMER_START(start); 326 fstype->make_fs(argv[0], argv[1], root, &fsoptions); 327 TIMER_RESULTS(start, "make_fs"); 328 329 free_fsnodes(root); 330 331 exit(EXIT_SUCCESS); 332 /* NOTREACHED */ 333 } 334 335 int 336 set_option(const option_t *options, const char *option, char *buf, size_t len) 337 { 338 char *var, *val; 339 int retval; 340 341 assert(option != NULL); 342 343 var = estrdup(option); 344 for (val = var; *val; val++) 345 if (*val == '=') { 346 *val++ = '\0'; 347 break; 348 } 349 retval = set_option_var(options, var, val, buf, len); 350 free(var); 351 return retval; 352 } 353 354 int 355 set_option_var(const option_t *options, const char *var, const char *val, 356 char *buf, size_t len) 357 { 358 char *s; 359 size_t i; 360 361 #define NUM(type) \ 362 if (!*val) { \ 363 *(type *)options[i].value = 1; \ 364 break; \ 365 } \ 366 *(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \ 367 options[i].minimum, options[i].maximum); break 368 369 for (i = 0; options[i].name != NULL; i++) { 370 if (var[1] == '\0') { 371 if (options[i].letter != var[0]) 372 continue; 373 } else if (strcmp(options[i].name, var) != 0) 374 continue; 375 switch (options[i].type) { 376 case OPT_BOOL: 377 *(bool *)options[i].value = 1; 378 break; 379 case OPT_STRARRAY: 380 strlcpy((void *)options[i].value, val, (size_t) 381 options[i].maximum); 382 break; 383 case OPT_STRPTR: 384 s = estrdup(val); 385 *(char **)options[i].value = s; 386 break; 387 case OPT_STRBUF: 388 if (buf == NULL) 389 abort(); 390 strlcpy(buf, val, len); 391 break; 392 case OPT_INT64: 393 NUM(uint64_t); 394 case OPT_INT32: 395 NUM(uint32_t); 396 case OPT_INT16: 397 NUM(uint16_t); 398 case OPT_INT8: 399 NUM(uint8_t); 400 default: 401 warnx("Unknown type %d in option %s", options[i].type, 402 val); 403 return 0; 404 } 405 return i; 406 } 407 warnx("Unknown option `%s'", var); 408 return -1; 409 } 410 411 412 static fstype_t * 413 get_fstype(const char *type) 414 { 415 int i; 416 417 for (i = 0; fstypes[i].type != NULL; i++) 418 if (strcmp(fstypes[i].type, type) == 0) 419 return (&fstypes[i]); 420 return (NULL); 421 } 422 423 option_t * 424 copy_opts(const option_t *o) 425 { 426 size_t i; 427 for (i = 0; o[i].name; i++) 428 continue; 429 i++; 430 return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o)); 431 } 432 433 static int 434 get_tstamp(const char *b, struct stat *st) 435 { 436 time_t when; 437 char *eb; 438 long long l; 439 440 if (stat(b, st) != -1) 441 return 0; 442 443 #ifndef HAVE_NBTOOL_CONFIG_H 444 errno = 0; 445 if ((when = parsedate(b, NULL, NULL)) == -1 && errno != 0) 446 #endif 447 { 448 errno = 0; 449 l = strtoll(b, &eb, 0); 450 if (b == eb || *eb || errno) 451 return -1; 452 when = (time_t)l; 453 } 454 455 st->st_ino = 1; 456 #if HAVE_STRUCT_STAT_BIRTHTIME 457 st->st_birthtime = 458 #endif 459 st->st_mtime = st->st_ctime = st->st_atime = when; 460 return 0; 461 } 462 463 static void 464 usage(fstype_t *fstype, fsinfo_t *fsoptions) 465 { 466 const char *prog; 467 468 prog = getprogname(); 469 fprintf(stderr, 470 "Usage: %s [-rxZ] [-B endian] [-b free-blocks] [-d debug-mask]\n" 471 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n" 472 "\t[-N userdb-dir] [-O offset] [-o fs-options] [-S sector-size]\n" 473 "\t[-s image-size] [-T <timestamp/file>] [-t fs-type]" 474 " image-file directory [extra-directory ...]\n", 475 prog); 476 477 if (fstype) { 478 size_t i; 479 option_t *o = fsoptions->fs_options; 480 481 fprintf(stderr, "\n%s specific options:\n", fstype->type); 482 for (i = 0; o[i].name != NULL; i++) 483 fprintf(stderr, "\t%c%c%20.20s\t%s\n", 484 o[i].letter ? o[i].letter : ' ', 485 o[i].letter ? ',' : ' ', 486 o[i].name, o[i].desc); 487 } 488 exit(EXIT_FAILURE); 489 } 490