1 /* $NetBSD: makefs.c,v 1.8 2002/01/10 05:31:07 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 2001 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 #ifndef __lint 40 __RCSID("$NetBSD: makefs.c,v 1.8 2002/01/10 05:31:07 lukem Exp $"); 41 #endif /* !__lint */ 42 43 #include <assert.h> 44 #include <ctype.h> 45 #include <err.h> 46 #include <errno.h> 47 #include <limits.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 53 #include "makefs.h" 54 #include "strsuftoull.h" 55 56 57 /* 58 * list of supported file systems and dispatch functions 59 */ 60 typedef struct { 61 const char *type; 62 int (*parse_options)(const char *, fsinfo_t *); 63 void (*make_fs)(const char *, const char *, fsnode *, 64 fsinfo_t *); 65 } fstype_t; 66 67 static fstype_t fstypes[] = { 68 { "ffs", ffs_parse_opts, ffs_makefs }, 69 { NULL }, 70 }; 71 72 uint debug; 73 struct timespec start_time; 74 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 (void)memset(&fsoptions, 0, sizeof(fsoptions)); 96 fsoptions.fd = -1; 97 specfile = NULL; 98 if (gettimeofday(&start, NULL) == -1) 99 err(1, "Unable to get system time"); 100 TIMEVAL_TO_TIMESPEC(&start, &start_time); 101 102 while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:o:s:S:t:")) != -1) { 103 switch (ch) { 104 105 case 'B': 106 if (strcmp(optarg, "be") == 0 || 107 strcmp(optarg, "big") == 0) { 108 #if BYTE_ORDER == LITTLE_ENDIAN 109 fsoptions.needswap = 1; 110 #endif 111 } else if (strcmp(optarg, "le") == 0 || 112 strcmp(optarg, "little") == 0) { 113 #if BYTE_ORDER == BIG_ENDIAN 114 fsoptions.needswap = 1; 115 #endif 116 } else { 117 warnx("Invalid endian `%s'.", optarg); 118 usage(); 119 } 120 break; 121 122 case 'b': 123 len = strlen(optarg) - 1; 124 if (optarg[len] == '%') { 125 optarg[len] = '\0'; 126 fsoptions.freeblockpc = 127 strsuftoull("free block percentage", 128 optarg, 0, 99); 129 } else { 130 fsoptions.freeblocks = 131 strsuftoull("free blocks", 132 optarg, 0, LLONG_MAX); 133 } 134 break; 135 136 case 'd': 137 debug = 138 (int)strsuftoull("debug mask", optarg, 0, UINT_MAX); 139 break; 140 141 case 'f': 142 len = strlen(optarg) - 1; 143 if (optarg[len] == '%') { 144 optarg[len] = '\0'; 145 fsoptions.freefilepc = 146 strsuftoull("free file percentage", 147 optarg, 0, 99); 148 } else { 149 fsoptions.freefiles = 150 strsuftoull("free files", 151 optarg, 0, LLONG_MAX); 152 } 153 break; 154 155 case 'F': 156 specfile = optarg; 157 break; 158 159 case 'M': 160 fsoptions.minsize = 161 strsuftoull("minimum size", optarg, 1LL, LLONG_MAX); 162 break; 163 164 case 'm': 165 fsoptions.maxsize = 166 strsuftoull("maximum size", optarg, 1LL, LLONG_MAX); 167 break; 168 169 case 'o': 170 { 171 char *p; 172 173 while ((p = strsep(&optarg, ",")) != NULL) { 174 if (*p == '\0') 175 errx(1, "Empty option"); 176 if (! fstype->parse_options(p, &fsoptions)) 177 usage(); 178 } 179 break; 180 } 181 182 case 's': 183 fsoptions.minsize = fsoptions.maxsize = 184 strsuftoull("size", optarg, 1LL, LLONG_MAX); 185 break; 186 187 case 'S': 188 fsoptions.sectorsize = 189 (int)strsuftoull("sector size", optarg, 190 1LL, INT_MAX); 191 break; 192 193 case 't': 194 if ((fstype = get_fstype(optarg)) == NULL) 195 errx(1, "Unknown fs type `%s'.", optarg); 196 break; 197 198 case '?': 199 default: 200 usage(); 201 /* NOTREACHED */ 202 203 } 204 } 205 if (debug) { 206 printf("debug mask: 0x%08x\n", debug); 207 printf("start time: %ld.%ld, %s", 208 (long)start_time.tv_sec, (long)start_time.tv_nsec, 209 ctime(&start_time.tv_sec)); 210 } 211 argc -= optind; 212 argv += optind; 213 214 if (argc != 2) 215 usage(); 216 217 /* walk the tree */ 218 TIMER_START(start); 219 root = walk_dir(argv[1], NULL); 220 TIMER_RESULTS(start, "walk_dir"); 221 222 if (specfile) { /* apply a specfile */ 223 TIMER_START(start); 224 apply_specfile(specfile, argv[1], root); 225 TIMER_RESULTS(start, "apply_specfile"); 226 } 227 228 if (debug & DEBUG_DUMP_FSNODES) { 229 putchar('\n'); 230 dump_fsnodes(argv[1], root); 231 putchar('\n'); 232 } 233 234 /* build the file system */ 235 TIMER_START(start); 236 fstype->make_fs(argv[0], argv[1], root, &fsoptions); 237 TIMER_RESULTS(start, "make_fs"); 238 239 exit(0); 240 /* NOTREACHED */ 241 } 242 243 244 int 245 set_option(option_t *options, const char *var, const char *val) 246 { 247 int i; 248 249 for (i = 0; options[i].name != NULL; i++) { 250 if (strcmp(options[i].name, var) != 0) 251 continue; 252 *options[i].value = (int)strsuftoull(options[i].desc, val, 253 options[i].minimum, options[i].maximum); 254 return (1); 255 } 256 warnx("Unknown option `%s'", var); 257 return (0); 258 } 259 260 261 static fstype_t * 262 get_fstype(const char *type) 263 { 264 int i; 265 266 for (i = 0; fstypes[i].type != NULL; i++) 267 if (strcmp(fstypes[i].type, type) == 0) 268 return (&fstypes[i]); 269 return (NULL); 270 } 271 272 static void 273 usage(void) 274 { 275 const char *prog; 276 277 prog = getprogname(); 278 fprintf(stderr, 279 "Usage: %s [-t fs-type] [-o fs-options] [-d debug-mask] [-B endian]\n" 280 "\t[-S sector-size] [-M minimum-size] [-m maximum-size] [-s image-size]\n" 281 "\t[-b free-blocks] [-f free-files] [-F mtree-specfile]\n" 282 "\timage-file directory\n", 283 prog); 284 exit(1); 285 } 286