1 /* $NetBSD: makefs.c,v 1.26 2006/10/22 21:11:56 christos 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 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <assert.h> 42 #include <ctype.h> 43 #include <errno.h> 44 #include <limits.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <time.h> 49 #include <unistd.h> 50 51 #include "makefs.h" 52 #include "mtree.h" 53 54 /* 55 * list of supported file systems and dispatch functions 56 */ 57 typedef struct { 58 const char *type; 59 void (*prepare_options)(fsinfo_t *); 60 int (*parse_options)(const char *, fsinfo_t *); 61 void (*cleanup_options)(fsinfo_t *); 62 void (*make_fs)(const char *, const char *, fsnode *, 63 fsinfo_t *); 64 } fstype_t; 65 66 static fstype_t fstypes[] = { 67 { "ffs", ffs_prep_opts, ffs_parse_opts, ffs_cleanup_opts, ffs_makefs }, 68 { "cd9660", cd9660_prep_opts, cd9660_parse_opts, cd9660_cleanup_opts, 69 cd9660_makefs}, 70 { .type = NULL }, 71 }; 72 73 u_int debug; 74 struct timespec start_time; 75 76 static fstype_t *get_fstype(const char *); 77 static void usage(void); 78 int main(int, char *[]); 79 80 int 81 main(int argc, char *argv[]) 82 { 83 struct timeval start; 84 fstype_t *fstype; 85 fsinfo_t fsoptions; 86 fsnode *root; 87 int ch, len; 88 char *specfile; 89 90 setprogname(argv[0]); 91 92 debug = 0; 93 if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL) 94 errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE); 95 96 /* set default fsoptions */ 97 (void)memset(&fsoptions, 0, sizeof(fsoptions)); 98 fsoptions.fd = -1; 99 fsoptions.sectorsize = -1; 100 101 if (fstype->prepare_options) 102 fstype->prepare_options(&fsoptions); 103 104 specfile = NULL; 105 if (gettimeofday(&start, NULL) == -1) 106 err(1, "Unable to get system time"); 107 108 start_time.tv_sec = start.tv_sec; 109 start_time.tv_nsec = start.tv_usec * 1000; 110 111 while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:s:S:t:x")) != -1) { 112 switch (ch) { 113 114 case 'B': 115 if (strcmp(optarg, "be") == 0 || 116 strcmp(optarg, "4321") == 0 || 117 strcmp(optarg, "big") == 0) { 118 #if BYTE_ORDER == LITTLE_ENDIAN 119 fsoptions.needswap = 1; 120 #endif 121 } else if (strcmp(optarg, "le") == 0 || 122 strcmp(optarg, "1234") == 0 || 123 strcmp(optarg, "little") == 0) { 124 #if BYTE_ORDER == BIG_ENDIAN 125 fsoptions.needswap = 1; 126 #endif 127 } else { 128 warnx("Invalid endian `%s'.", optarg); 129 usage(); 130 } 131 break; 132 133 case 'b': 134 len = strlen(optarg) - 1; 135 if (optarg[len] == '%') { 136 optarg[len] = '\0'; 137 fsoptions.freeblockpc = 138 strsuftoll("free block percentage", 139 optarg, 0, 99); 140 } else { 141 fsoptions.freeblocks = 142 strsuftoll("free blocks", 143 optarg, 0, LLONG_MAX); 144 } 145 break; 146 147 case 'd': 148 debug = strtoll(optarg, NULL, 0); 149 break; 150 151 case 'f': 152 len = strlen(optarg) - 1; 153 if (optarg[len] == '%') { 154 optarg[len] = '\0'; 155 fsoptions.freefilepc = 156 strsuftoll("free file percentage", 157 optarg, 0, 99); 158 } else { 159 fsoptions.freefiles = 160 strsuftoll("free files", 161 optarg, 0, LLONG_MAX); 162 } 163 break; 164 165 case 'F': 166 specfile = optarg; 167 break; 168 169 case 'M': 170 fsoptions.minsize = 171 strsuftoll("minimum size", optarg, 1LL, LLONG_MAX); 172 break; 173 174 case 'N': 175 if (! setup_getid(optarg)) 176 errx(1, 177 "Unable to use user and group databases in `%s'", 178 optarg); 179 break; 180 181 case 'm': 182 fsoptions.maxsize = 183 strsuftoll("maximum size", optarg, 1LL, LLONG_MAX); 184 break; 185 186 case 'o': 187 { 188 char *p; 189 190 while ((p = strsep(&optarg, ",")) != NULL) { 191 if (*p == '\0') 192 errx(1, "Empty option"); 193 if (! fstype->parse_options(p, &fsoptions)) 194 usage(); 195 } 196 break; 197 } 198 199 case 's': 200 fsoptions.minsize = fsoptions.maxsize = 201 strsuftoll("size", optarg, 1LL, LLONG_MAX); 202 break; 203 204 case 'S': 205 fsoptions.sectorsize = 206 (int)strsuftoll("sector size", optarg, 207 1LL, INT_MAX); 208 break; 209 210 case 't': 211 /* Check current one and cleanup if necessary. */ 212 if (fstype->cleanup_options) 213 fstype->cleanup_options(&fsoptions); 214 fsoptions.fs_specific = NULL; 215 if ((fstype = get_fstype(optarg)) == NULL) 216 errx(1, "Unknown fs type `%s'.", optarg); 217 fstype->prepare_options(&fsoptions); 218 break; 219 220 case 'x': 221 fsoptions.onlyspec = 1; 222 break; 223 224 case '?': 225 default: 226 usage(); 227 /* NOTREACHED */ 228 229 } 230 } 231 if (debug) { 232 printf("debug mask: 0x%08x\n", debug); 233 printf("start time: %ld.%ld, %s", 234 (long)start_time.tv_sec, (long)start_time.tv_nsec, 235 ctime(&start_time.tv_sec)); 236 } 237 argc -= optind; 238 argv += optind; 239 240 if (argc != 2) 241 usage(); 242 243 /* -x must be accompanied by -F */ 244 if (fsoptions.onlyspec != 0 && specfile == NULL) 245 errx(1, "-x requires -F mtree-specfile."); 246 247 /* walk the tree */ 248 TIMER_START(start); 249 root = walk_dir(argv[1], NULL); 250 TIMER_RESULTS(start, "walk_dir"); 251 252 if (specfile) { /* apply a specfile */ 253 TIMER_START(start); 254 apply_specfile(specfile, argv[1], root, fsoptions.onlyspec); 255 TIMER_RESULTS(start, "apply_specfile"); 256 } 257 258 if (debug & DEBUG_DUMP_FSNODES) { 259 printf("\nparent: %s\n", argv[1]); 260 dump_fsnodes(".", root); 261 putchar('\n'); 262 } 263 264 /* build the file system */ 265 TIMER_START(start); 266 fstype->make_fs(argv[0], argv[1], root, &fsoptions); 267 TIMER_RESULTS(start, "make_fs"); 268 269 free_fsnodes(root); 270 271 exit(0); 272 /* NOTREACHED */ 273 } 274 275 276 int 277 set_option(option_t *options, const char *var, const char *val) 278 { 279 int i; 280 281 for (i = 0; options[i].name != NULL; i++) { 282 if (strcmp(options[i].name, var) != 0) 283 continue; 284 *options[i].value = (int)strsuftoll(options[i].desc, val, 285 options[i].minimum, options[i].maximum); 286 return (1); 287 } 288 warnx("Unknown option `%s'", var); 289 return (0); 290 } 291 292 293 static fstype_t * 294 get_fstype(const char *type) 295 { 296 int i; 297 298 for (i = 0; fstypes[i].type != NULL; i++) 299 if (strcmp(fstypes[i].type, type) == 0) 300 return (&fstypes[i]); 301 return (NULL); 302 } 303 304 static void 305 usage(void) 306 { 307 const char *prog; 308 309 prog = getprogname(); 310 fprintf(stderr, 311 "usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n" 312 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n" 313 "\t[-b free-blocks] [-f free-files] [-F mtree-specfile] [-x]\n" 314 "\t[-N userdb-dir] image-file directory\n", 315 prog); 316 exit(1); 317 } 318