Drop main() prototype. Syncs with NetBSD-8
[minix.git] / usr.sbin / makefs / v7fs.c
blob7c479a35e31123345910397c9aa620a2bb893c31
1 /* $NetBSD: v7fs.c,v 1.8 2013/01/29 15:52:25 christos Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(__lint)
38 __RCSID("$NetBSD: v7fs.c,v 1.8 2013/01/29 15:52:25 christos Exp $");
39 #endif /* !__lint */
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <fcntl.h>
46 #include <util.h>
48 #include "makefs.h"
49 #include "v7fs.h"
50 #include "v7fs_impl.h"
51 #include "v7fs_makefs.h"
52 #include "newfs_v7fs.h"
55 #ifndef HAVE_NBTOOL_CONFIG_H
56 #include "progress.h"
57 static bool progress_bar_enable;
58 #endif
59 int v7fs_newfs_verbose;
61 void
62 v7fs_prep_opts(fsinfo_t *fsopts)
64 v7fs_opt_t *v7fs_opts = ecalloc(1, sizeof(*v7fs_opts));
65 const option_t v7fs_options[] = {
66 { 'p', "pdp", &v7fs_opts->pdp_endian, OPT_INT32, false, true,
67 "PDP endian" },
68 { 'P', "progress", &v7fs_opts->progress, OPT_INT32, false, true,
69 "Progress bar" },
70 { .name = NULL }
73 fsopts->fs_specific = v7fs_opts;
74 fsopts->fs_options = copy_opts(v7fs_options);
77 void
78 v7fs_cleanup_opts(fsinfo_t *fsopts)
80 free(fsopts->fs_specific);
81 free(fsopts->fs_options);
84 int
85 v7fs_parse_opts(const char *option, fsinfo_t *fsopts)
88 return set_option_var(fsopts->fs_options, option, "1", NULL, 0) != -1;
91 void
92 v7fs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
94 struct v7fs_mount_device v7fs_mount;
95 int fd, endian, error = 1;
96 v7fs_opt_t *v7fs_opts = fsopts->fs_specific;
98 v7fs_newfs_verbose = debug;
99 #ifndef HAVE_NBTOOL_CONFIG_H
100 if ((progress_bar_enable = v7fs_opts->progress)) {
101 progress_switch(progress_bar_enable);
102 progress_init();
103 progress(&(struct progress_arg){ .cdev = image });
105 #endif
107 /* Determine filesystem image size */
108 v7fs_estimate(dir, root, fsopts);
109 printf("Calculated size of `%s': %lld bytes, %ld inodes\n",
110 image, (long long)fsopts->size, (long)fsopts->inodes);
112 if ((fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
113 err(EXIT_FAILURE, "%s", image);
115 if (lseek(fd, fsopts->size - 1, SEEK_SET) == -1) {
116 goto err_exit;
118 if (write(fd, &fd, 1) != 1) {
119 goto err_exit;
121 if (lseek(fd, 0, SEEK_SET) == -1) {
122 goto err_exit;
124 fsopts->fd = fd;
125 v7fs_mount.device.fd = fd;
127 #if !defined BYTE_ORDER
128 #error
129 #endif
130 #if BYTE_ORDER == LITTLE_ENDIAN
131 if (fsopts->needswap)
132 endian = BIG_ENDIAN;
133 else
134 endian = LITTLE_ENDIAN;
135 #else
136 if (fsopts->needswap)
137 endian = LITTLE_ENDIAN;
138 else
139 endian = BIG_ENDIAN;
140 #endif
141 if (v7fs_opts->pdp_endian) {
142 endian = PDP_ENDIAN;
145 v7fs_mount.endian = endian;
146 v7fs_mount.sectors = fsopts->size >> V7FS_BSHIFT;
147 if (v7fs_newfs(&v7fs_mount, fsopts->inodes) != 0) {
148 goto err_exit;
151 if (v7fs_populate(dir, root, fsopts, &v7fs_mount) != 0) {
152 error = 2; /* some files couldn't add */
153 goto err_exit;
156 close(fd);
157 return;
159 err_exit:
160 close(fd);
161 err(error, "%s", image);
164 void
165 progress(const struct progress_arg *p)
167 #ifndef HAVE_NBTOOL_CONFIG_H
168 static struct progress_arg Progress;
169 static char cdev[32];
170 static char label[32];
172 if (!progress_bar_enable)
173 return;
175 if (p) {
176 Progress = *p;
177 if (p->cdev)
178 strcpy(cdev, p->cdev);
179 if (p->label)
180 strcpy(label, p->label);
183 if (!Progress.tick)
184 return;
185 if (++Progress.cnt > Progress.tick) {
186 Progress.cnt = 0;
187 Progress.total++;
188 progress_bar(cdev, label, Progress.total, PROGRESS_BAR_GRANULE);
190 #endif