OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.22/] [disk/] [tools/] [fs-NetBSD/] [makefs/] [makefs.c] - Blame information for rev 185

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 hellwig
/*      $NetBSD: makefs.c,v 1.31 2012/01/28 02:35:46 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
#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.31 2012/01/28 02:35:46 christos 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
#include <time.h>
56
#include <sys/time.h>
57
 
58
#include "common.h"
59
#include "makefs.h"
60
#include "mtree.h"
61
 
62
/*
63
 * list of supported file systems and dispatch functions
64
 */
65
typedef struct {
66
        const char      *type;
67
        void            (*prepare_options)(fsinfo_t *);
68
        int             (*parse_options)(const char *, fsinfo_t *);
69
        void            (*cleanup_options)(fsinfo_t *);
70
        void            (*make_fs)(const char *, const char *, fsnode *,
71
                                fsinfo_t *);
72
} fstype_t;
73
 
74
static fstype_t fstypes[] = {
75
        { "ffs", ffs_prep_opts, ffs_parse_opts, ffs_cleanup_opts, ffs_makefs },
76
        { .type = 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
static const char *progname = NULL;
87
 
88
void setprogname(const char *name) {
89
  progname = name;
90
}
91
 
92
const char *getprogname(void) {
93
  return progname;
94
}
95
 
96
long getValue(const char *desc, const char *val, long min, long max) {
97
  long result;
98
  char *endp;
99
 
100
  result = strtol(val, &endp, 0);
101
  if (*endp != '\0') {
102
    if (strcmp(endp, "b") == 0) {
103
      result *= 512;
104
    } else
105
    if (strcmp(endp, "k") == 0) {
106
      result *= 1024;
107
    } else
108
    if (strcmp(endp, "m") == 0) {
109
      result *= 1048576;
110
    } else
111
    if (strcmp(endp, "w") == 0) {
112
      result *= sizeof(int);
113
    } else {
114
      errx(1, "%s, unknown suffix '%s'", desc, endp);
115
    }
116
  }
117
  if (result < min || result > max) {
118
    errx(1, "%s, value out of range", desc);
119
  }
120
  return result;
121
}
122
 
123
u_int nodetoino(u_int type) {
124
  switch (type) {
125
    case F_BLOCK:
126
      return S_IFBLK;
127
    case F_CHAR:
128
      return S_IFCHR;
129
    case F_DIR:
130
      return S_IFDIR;
131
    case F_FIFO:
132
      return S_IFIFO;
133
    case F_FILE:
134
      return S_IFREG;
135
    case F_LINK:
136
      return S_IFLNK;
137
    default:
138
      printf("unknown type %d", type);
139
      abort();
140
  }
141
}
142
 
143
const char *inotype(u_int type) {
144
  switch (type & S_IFMT) {
145
    case S_IFBLK:
146
      return "block";
147
    case S_IFCHR:
148
      return "char";
149
    case S_IFDIR:
150
      return "dir";
151
    case S_IFIFO:
152
      return "fifo";
153
    case S_IFREG:
154
      return "file";
155
    case S_IFLNK:
156
      return "link";
157
    default:
158
      return "unknown";
159
  }
160
}
161
 
162
int setup_getid(const char *dir) {
163
  return 0;
164
}
165
 
166
int
167
main(int argc, char *argv[])
168
{
169
        struct timeval   start;
170
        fstype_t        *fstype;
171
        fsinfo_t         fsoptions;
172
        fsnode          *root;
173
        int              ch, i, len;
174
        char            *specfile;
175
 
176
        setprogname(argv[0]);
177
 
178
        debug = 0;
179
        if ((fstype = get_fstype(DEFAULT_FSTYPE)) == NULL)
180
                errx(1, "Unknown default fs type `%s'.", DEFAULT_FSTYPE);
181
 
182
                /* set default fsoptions */
183
        (void)memset(&fsoptions, 0, sizeof(fsoptions));
184
        fsoptions.fd = -1;
185
        fsoptions.sectorsize = -1;
186
 
187
        if (fstype->prepare_options)
188
                fstype->prepare_options(&fsoptions);
189
 
190
        specfile = NULL;
191
        if (gettimeofday(&start, NULL) == -1)
192
                err(1, "Unable to get system time");
193
 
194
        start_time.tv_sec = start.tv_sec;
195
        start_time.tv_nsec = start.tv_usec * 1000;
196
 
197
        while ((ch = getopt(argc, argv, "B:b:d:f:F:M:m:N:o:s:S:t:x")) != -1) {
198
                switch (ch) {
199
 
200
                case 'B':
201
                        if (strcmp(optarg, "be") == 0 ||
202
                            strcmp(optarg, "4321") == 0 ||
203
                            strcmp(optarg, "big") == 0) {
204
#if BYTE_ORDER == LITTLE_ENDIAN
205
                                fsoptions.needswap = 1;
206
#endif
207
                        } else if (strcmp(optarg, "le") == 0 ||
208
                            strcmp(optarg, "1234") == 0 ||
209
                            strcmp(optarg, "little") == 0) {
210
#if BYTE_ORDER == BIG_ENDIAN
211
                                fsoptions.needswap = 1;
212
#endif
213
                        } else {
214
                                warnx("Invalid endian `%s'.", optarg);
215
                                usage();
216
                        }
217
                        break;
218
 
219
                case 'b':
220
                        len = strlen(optarg) - 1;
221
                        if (optarg[len] == '%') {
222
                                optarg[len] = '\0';
223
                                fsoptions.freeblockpc =
224
                                    getValue("free block percentage",
225
                                        optarg, 0, 99);
226
                        } else {
227
                                fsoptions.freeblocks =
228
                                    getValue("free blocks",
229
                                        optarg, 0, LONG_MAX);
230
                        }
231
                        break;
232
 
233
                case 'd':
234
                        debug = strtoll(optarg, NULL, 0);
235
                        break;
236
 
237
                case 'f':
238
                        len = strlen(optarg) - 1;
239
                        if (optarg[len] == '%') {
240
                                optarg[len] = '\0';
241
                                fsoptions.freefilepc =
242
                                    getValue("free file percentage",
243
                                        optarg, 0, 99);
244
                        } else {
245
                                fsoptions.freefiles =
246
                                    getValue("free files",
247
                                        optarg, 0, LONG_MAX);
248
                        }
249
                        break;
250
 
251
                case 'F':
252
                        specfile = optarg;
253
                        break;
254
 
255
                case 'M':
256
                        fsoptions.minsize =
257
                            getValue("minimum size", optarg, 1L, LONG_MAX);
258
                        break;
259
 
260
                case 'N':
261
                        if (! setup_getid(optarg))
262
                                errx(1,
263
                            "Unable to use user and group databases in `%s'",
264
                                    optarg);
265
                        break;
266
 
267
                case 'm':
268
                        fsoptions.maxsize =
269
                            getValue("maximum size", optarg, 1L, LONG_MAX);
270
                        break;
271
 
272
                case 'o':
273
                {
274
                        char *p;
275
 
276
                        while ((p = strsep(&optarg, ",")) != NULL) {
277
                                if (*p == '\0')
278
                                        errx(1, "Empty option");
279
                                if (! fstype->parse_options(p, &fsoptions))
280
                                        usage();
281
                        }
282
                        break;
283
                }
284
 
285
                case 's':
286
                        fsoptions.minsize = fsoptions.maxsize =
287
                            getValue("size", optarg, 1L, LONG_MAX);
288
                        break;
289
 
290
                case 'S':
291
                        fsoptions.sectorsize =
292
                            (int)getValue("sector size", optarg,
293
                                1LL, INT_MAX);
294
                        break;
295
 
296
                case 't':
297
                        /* Check current one and cleanup if necessary. */
298
                        if (fstype->cleanup_options)
299
                                fstype->cleanup_options(&fsoptions);
300
                        fsoptions.fs_specific = NULL;
301
                        if ((fstype = get_fstype(optarg)) == NULL)
302
                                errx(1, "Unknown fs type `%s'.", optarg);
303
                        fstype->prepare_options(&fsoptions);
304
                        break;
305
 
306
                case 'x':
307
                        fsoptions.onlyspec = 1;
308
                        break;
309
 
310
                case '?':
311
                default:
312
                        usage();
313
                        /* NOTREACHED */
314
 
315
                }
316
        }
317
        if (debug) {
318
                printf("debug mask: 0x%08x\n", debug);
319
                printf("start time: %ld.%ld, %s",
320
                    (long)start_time.tv_sec, (long)start_time.tv_nsec,
321
                    ctime(&start_time.tv_sec));
322
        }
323
        argc -= optind;
324
        argv += optind;
325
 
326
        if (argc < 2)
327
                usage();
328
 
329
        /* -x must be accompanied by -F */
330
        if (fsoptions.onlyspec != 0 && specfile == NULL)
331
                errx(1, "-x requires -F mtree-specfile.");
332
 
333
                                /* walk the tree */
334
        TIMER_START(start);
335
        root = walk_dir(argv[1], ".", NULL, NULL);
336
        TIMER_RESULTS(start, "walk_dir");
337
 
338
        /* append extra directory */
339
        for (i = 2; i < argc; i++) {
340
                struct stat sb;
341
                if (stat(argv[i], &sb) == -1)
342
                        err(1, "Can't stat `%s'", argv[i]);
343
                if (!S_ISDIR(sb.st_mode))
344
                        errx(1, "%s: not a directory", argv[i]);
345
                TIMER_START(start);
346
                root = walk_dir(argv[i], ".", NULL, root);
347
                TIMER_RESULTS(start, "walk_dir2");
348
        }
349
 
350
        if (specfile) {         /* apply a specfile */
351
                TIMER_START(start);
352
                apply_specfile(specfile, argv[1], root, fsoptions.onlyspec);
353
                TIMER_RESULTS(start, "apply_specfile");
354
        }
355
 
356
        if (debug & DEBUG_DUMP_FSNODES) {
357
                printf("\nparent: %s\n", argv[1]);
358
                dump_fsnodes(root);
359
                putchar('\n');
360
        }
361
 
362
                                /* build the file system */
363
        TIMER_START(start);
364
        fstype->make_fs(argv[0], argv[1], root, &fsoptions);
365
        TIMER_RESULTS(start, "make_fs");
366
 
367
        free_fsnodes(root);
368
 
369
        exit(0);
370
        /* NOTREACHED */
371
}
372
 
373
 
374
int
375
set_option(option_t *options, const char *var, const char *val)
376
{
377
        int     i;
378
 
379
        for (i = 0; options[i].name != NULL; i++) {
380
                if (strcmp(options[i].name, var) != 0)
381
                        continue;
382
                *options[i].value = (int)getValue(options[i].desc, val,
383
                    options[i].minimum, options[i].maximum);
384
                return (1);
385
        }
386
        warnx("Unknown option `%s'", var);
387
        return (0);
388
}
389
 
390
 
391
static fstype_t *
392
get_fstype(const char *type)
393
{
394
        int i;
395
 
396
        for (i = 0; fstypes[i].type != NULL; i++)
397
                if (strcmp(fstypes[i].type, type) == 0)
398
                        return (&fstypes[i]);
399
        return (NULL);
400
}
401
 
402
static void
403
usage(void)
404
{
405
        const char *prog;
406
 
407
        prog = getprogname();
408
        fprintf(stderr,
409
"usage: %s [-x] [-B endian] [-b free-blocks] [-d debug-mask]\n"
410
"\t[-F mtree-specfile] [-f free-files] [-M minimum-size]\n"
411
"\t[-m maximum-size] [-N userdb-dir] [-o fs-options] [-S sector-size]\n"
412
"\t[-s image-size] [-t fs-type] image-file directory [extra-directory ...]\n",
413
            prog);
414
        exit(1);
415
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.