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