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