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