1 /* $NetBSD: v7fs.c,v 1.8 2013/01/29 15:52:25 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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"
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 $");
50 #include "v7fs_impl.h"
51 #include "v7fs_makefs.h"
52 #include "newfs_v7fs.h"
55 #ifndef HAVE_NBTOOL_CONFIG_H
57 static bool progress_bar_enable
;
59 int v7fs_newfs_verbose
;
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,
68 { 'P', "progress", &v7fs_opts
->progress
, OPT_INT32
, false, true,
73 fsopts
->fs_specific
= v7fs_opts
;
74 fsopts
->fs_options
= copy_opts(v7fs_options
);
78 v7fs_cleanup_opts(fsinfo_t
*fsopts
)
80 free(fsopts
->fs_specific
);
81 free(fsopts
->fs_options
);
85 v7fs_parse_opts(const char *option
, fsinfo_t
*fsopts
)
88 return set_option_var(fsopts
->fs_options
, option
, "1", NULL
, 0) != -1;
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
);
103 progress(&(struct progress_arg
){ .cdev
= image
});
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) {
118 if (write(fd
, &fd
, 1) != 1) {
121 if (lseek(fd
, 0, SEEK_SET
) == -1) {
125 v7fs_mount
.device
.fd
= fd
;
127 #if !defined BYTE_ORDER
130 #if BYTE_ORDER == LITTLE_ENDIAN
131 if (fsopts
->needswap
)
134 endian
= LITTLE_ENDIAN
;
136 if (fsopts
->needswap
)
137 endian
= LITTLE_ENDIAN
;
141 if (v7fs_opts
->pdp_endian
) {
145 v7fs_mount
.endian
= endian
;
146 v7fs_mount
.sectors
= fsopts
->size
>> V7FS_BSHIFT
;
147 if (v7fs_newfs(&v7fs_mount
, fsopts
->inodes
) != 0) {
151 if (v7fs_populate(dir
, root
, fsopts
, &v7fs_mount
) != 0) {
152 error
= 2; /* some files couldn't add */
161 err(error
, "%s", image
);
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
)
178 strcpy(cdev
, p
->cdev
);
180 strcpy(label
, p
->label
);
185 if (++Progress
.cnt
> Progress
.tick
) {
188 progress_bar(cdev
, label
, Progress
.total
, PROGRESS_BAR_GRANULE
);