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