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