1 /* $NetBSD: makefs.c,v 1.50 2013/08/05 14:41:57 reinoud 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.50 2013/08/05 14:41:57 reinoud 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 91 static fstype_t *get_fstype(const char *); 92 static void usage(fstype_t *, fsinfo_t *) __dead; 93 94 int 95 main(int argc, char *argv[]) 96 { 97 struct timeval start; 98 fstype_t *fstype; 99 fsinfo_t fsoptions; 100 fsnode *root; 101 int ch, i, len; 102 char *specfile; 103 104 setprogname(argv[0]); 105 106 debug = 0; 107 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL) 108 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE); 109 110 /* set default fsoptions */ 111 (void)memset(&fsoptions, 0, sizeof(fsoptions)); 112 fsoptions.fd = -1; 113 fsoptions.sectorsize = -1; 114 115 if (fstype->prepare_options) 116 fstype->prepare_options(&fsoptions); 117 118 specfile = NULL; 119 if (gettimeofday(&start, NULL) == -1) 120 err(1, "Unable to get system time"); 121 122 start_time.tv_sec = start.tv_sec; 123 start_time.tv_nsec = start.tv_usec * 1000; 124 125 while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:O:o:rs:S:t:xZ")) != -1) { 126 switch (ch) { 127 128 case 'B': 129 if (strcmp(optarg, "be") == 0 || 130 strcmp(optarg, "4321") == 0 || 131 strcmp(optarg, "big") == 0) { 132 #if BYTE_ORDER == LITTLE_ENDIAN 133 fsoptions.needswap = 1; 134 #endif 135 } else if (strcmp(optarg, "le") == 0 || 136 strcmp(optarg, "1234") == 0 || 137 strcmp(optarg, "little") == 0) { 138 #if BYTE_ORDER == BIG_ENDIAN 139 fsoptions.needswap = 1; 140 #endif 141 } else { 142 warnx("Invalid endian `%s'.", optarg); 143 usage(fstype, &fsoptions); 144 } 145 break; 146 147 case 'b': 148 len = strlen(optarg) - 1; 149 if (optarg[len] == '%') { 150 optarg[len] = '\0'; 151 fsoptions.freeblockpc = 152 strsuftoll("free block percentage", 153 optarg, 0, 99); 154 } else { 155 fsoptions.freeblocks = 156 strsuftoll("free blocks", 157 optarg, 0, LLONG_MAX); 158 } 159 break; 160 161 case 'd': 162 debug = strtoll(optarg, NULL, 0); 163 break; 164 165 case 'f': 166 len = strlen(optarg) - 1; 167 if (optarg[len] == '%') { 168 optarg[len] = '\0'; 169 fsoptions.freefilepc = 170 strsuftoll("free file percentage", 171 optarg, 0, 99); 172 } else { 173 fsoptions.freefiles = 174 strsuftoll("free files", 175 optarg, 0, LLONG_MAX); 176 } 177 break; 178 179 case 'F': 180 specfile = optarg; 181 break; 182 183 case 'M': 184 fsoptions.minsize = 185 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX); 186 break; 187 188 case 'N': 189 if (! setup_getid(optarg)) 190 errx(1, 191 "Unable to use user and group databases in `%s'", 192 optarg); 193 break; 194 195 case 'm': 196 fsoptions.maxsize = 197 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX); 198 break; 199 200 case 'O': 201 fsoptions.offset = 202 strsuftoll("offset", optarg, 0LL, LLONG_MAX); 203 break; 204 205 case 'o': 206 { 207 char *p; 208 209 while ((p = strsep(&optarg, ",")) != NULL) { 210 if (*p == '\0') 211 errx(1, "Empty option"); 212 if (! fstype->parse_options(p, &fsoptions)) 213 usage(fstype, &fsoptions); 214 } 215 break; 216 } 217 218 case 'r': 219 fsoptions.replace = 1; 220 break; 221 222 case 's': 223 fsoptions.minsize = fsoptions.maxsize = 224 strsuftoll("size", optarg, 1LL, LLONG_MAX); 225 break; 226 227 case 'S': 228 fsoptions.sectorsize = 229 (int)strsuftoll("sector size", optarg, 230 1LL, INT_MAX); 231 break; 232 233 case 't': 234 /* Check current one and cleanup if necessary. */ 235 if (fstype->cleanup_options) 236 fstype->cleanup_options(&fsoptions); 237 fsoptions.fs_specific = NULL; 238 if ((fstype = get_fstype(optarg)) == NULL) 239 errx(1, "Unknown fs type `%s'.", optarg); 240 fstype->prepare_options(&fsoptions); 241 break; 242 243 case 'x': 244 fsoptions.onlyspec = 1; 245 break; 246 247 case 'Z': 248 fsoptions.sparse = 1; 249 break; 250 251 case '?': 252 default: 253 usage(fstype, &fsoptions); 254 /* NOTREACHED */ 255 256 } 257 } 258 if (debug) { 259 printf("debug mask: 0x%08x\n", debug); 260 printf("start time: %ld.%ld, %s", 261 (long)start_time.tv_sec, (long)start_time.tv_nsec, 262 ctime(&start_time.tv_sec)); 263 } 264 argc -= optind; 265 argv += optind; 266 267 if (argc < 2) 268 usage(fstype, &fsoptions); 269 270 /* -x must be accompanied by -F */ 271 if (fsoptions.onlyspec != 0 && specfile == NULL) 272 errx(1, "-x requires -F mtree-specfile."); 273 274 /* walk the tree */ 275 TIMER_START(start); 276 root = walk_dir(argv[1], ".", NULL, NULL, fsoptions.replace); 277 TIMER_RESULTS(start, "walk_dir"); 278 279 /* append extra directory */ 280 for (i = 2; i < argc; i++) { 281 struct stat sb; 282 if (stat(argv[i], &sb) == -1) 283 err(1, "Can't stat `%s'", argv[i]); 284 if (!S_ISDIR(sb.st_mode)) 285 errx(1, "%s: not a directory", argv[i]); 286 TIMER_START(start); 287 root = walk_dir(argv[i], ".", NULL, root, fsoptions.replace); 288 TIMER_RESULTS(start, "walk_dir2"); 289 } 290 291 if (specfile) { /* apply a specfile */ 292 TIMER_START(start); 293 apply_specfile(specfile, argv[1], root, fsoptions.onlyspec); 294 TIMER_RESULTS(start, "apply_specfile"); 295 } 296 297 if (debug & DEBUG_DUMP_FSNODES) { 298 printf("\nparent: %s\n", argv[1]); 299 dump_fsnodes(root); 300 putchar('\n'); 301 } 302 303 /* build the file system */ 304 TIMER_START(start); 305 fstype->make_fs(argv[0], argv[1], root, &fsoptions); 306 TIMER_RESULTS(start, "make_fs"); 307 308 free_fsnodes(root); 309 310 exit(0); 311 /* NOTREACHED */ 312 } 313 314 int 315 set_option(const option_t *options, const char *option, char *buf, size_t len) 316 { 317 char *var, *val; 318 int retval; 319 320 assert(option != NULL); 321 322 var = estrdup(option); 323 for (val = var; *val; val++) 324 if (*val == '=') { 325 *val++ = '\0'; 326 break; 327 } 328 retval = set_option_var(options, var, val, buf, len); 329 free(var); 330 return retval; 331 } 332 333 int 334 set_option_var(const option_t *options, const char *var, const char *val, 335 char *buf, size_t len) 336 { 337 char *s; 338 size_t i; 339 340 #define NUM(type) \ 341 if (!*val) { \ 342 *(type *)options[i].value = 1; \ 343 break; \ 344 } \ 345 *(type *)options[i].value = (type)strsuftoll(options[i].desc, val, \ 346 options[i].minimum, options[i].maximum); break 347 348 for (i = 0; options[i].name != NULL; i++) { 349 if (var[1] == '\0') { 350 if (options[i].letter != var[0]) 351 continue; 352 } else if (strcmp(options[i].name, var) != 0) 353 continue; 354 switch (options[i].type) { 355 case OPT_BOOL: 356 *(bool *)options[i].value = 1; 357 break; 358 case OPT_STRARRAY: 359 strlcpy((void *)options[i].value, val, (size_t) 360 options[i].maximum); 361 break; 362 case OPT_STRPTR: 363 s = estrdup(val); 364 *(char **)options[i].value = s; 365 break; 366 case OPT_STRBUF: 367 if (buf == NULL) 368 abort(); 369 strlcpy(buf, val, len); 370 break; 371 case OPT_INT64: 372 NUM(uint64_t); 373 case OPT_INT32: 374 NUM(uint32_t); 375 case OPT_INT16: 376 NUM(uint16_t); 377 case OPT_INT8: 378 NUM(uint8_t); 379 default: 380 warnx("Unknown type %d in option %s", options[i].type, 381 val); 382 return 0; 383 } 384 return i; 385 } 386 warnx("Unknown option `%s'", var); 387 return -1; 388 } 389 390 391 static fstype_t * 392 get_fstype(const char *type) 393 { 394 int i; 395 396 for (i = 0; fstypes[i].type != NULL; i++) 397 if (strcmp(fstypes[i].type, type) == 0) 398 return (&fstypes[i]); 399 return (NULL); 400 } 401 402 option_t * 403 copy_opts(const option_t *o) 404 { 405 size_t i; 406 for (i = 0; o[i].name; i++) 407 continue; 408 i++; 409 return memcpy(ecalloc(i, sizeof(*o)), o, i * sizeof(*o)); 410 } 411 412 static void 413 usage(fstype_t *fstype, fsinfo_t *fsoptions) 414 { 415 const char *prog; 416 417 prog = getprogname(); 418 fprintf(stderr, 419 "Usage: %s [-rxZ] [-B endian] [-b free-blocks] [-d debug-mask]\n" 420 "\t[-F mtree-specfile] [-f free-files] [-M minimum-size] [-m maximum-size]\n" 421 "\t[-N userdb-dir] [-O offset] [-o fs-options] [-S sector-size]\n" 422 "\t[-s image-size] [-t fs-type] image-file directory [extra-directory ...]\n", 423 prog); 424 425 if (fstype) { 426 size_t i; 427 option_t *o = fsoptions->fs_options; 428 429 fprintf(stderr, "\n%s specific options:\n", fstype->type); 430 for (i = 0; o[i].name != NULL; i++) 431 fprintf(stderr, "\t%c%c%20.20s\t%s\n", 432 o[i].letter ? o[i].letter : ' ', 433 o[i].letter ? ',' : ' ', 434 o[i].name, o[i].desc); 435 } 436 exit(1); 437 } 438