xref: /minix/usr.sbin/makefs/v7fs.c (revision ebfedea0)
1 /*	$NetBSD: v7fs.c,v 1.8 2013/01/29 15:52:25 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
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.
18  *
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.
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
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 */
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <fcntl.h>
46 #include <util.h>
47 
48 #include "makefs.h"
49 #include "v7fs.h"
50 #include "v7fs_impl.h"
51 #include "v7fs_makefs.h"
52 #include "newfs_v7fs.h"
53 
54 
55 #ifndef HAVE_NBTOOL_CONFIG_H
56 #include "progress.h"
57 static bool progress_bar_enable;
58 #endif
59 int v7fs_newfs_verbose;
60 
61 void
62 v7fs_prep_opts(fsinfo_t *fsopts)
63 {
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 }
71 	};
72 
73 	fsopts->fs_specific = v7fs_opts;
74 	fsopts->fs_options = copy_opts(v7fs_options);
75 }
76 
77 void
78 v7fs_cleanup_opts(fsinfo_t *fsopts)
79 {
80 	free(fsopts->fs_specific);
81 	free(fsopts->fs_options);
82 }
83 
84 int
85 v7fs_parse_opts(const char *option, fsinfo_t *fsopts)
86 {
87 
88 	return set_option_var(fsopts->fs_options, option, "1", NULL, 0) != -1;
89 }
90 
91 void
92 v7fs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
93 {
94 	struct v7fs_mount_device v7fs_mount;
95 	int fd, endian, error = 1;
96 	v7fs_opt_t *v7fs_opts = fsopts->fs_specific;
97 
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 });
104 	}
105 #endif
106 
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);
111 
112 	if ((fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
113 		err(EXIT_FAILURE, "%s", image);
114 	}
115 	if (lseek(fd, fsopts->size - 1, SEEK_SET) == -1) {
116 		goto err_exit;
117 	}
118 	if (write(fd, &fd, 1) != 1) {
119 		goto err_exit;
120 	}
121 	if (lseek(fd, 0, SEEK_SET) == -1) {
122 		goto err_exit;
123 	}
124 	fsopts->fd = fd;
125 	v7fs_mount.device.fd = fd;
126 
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;
143 	}
144 
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;
149 	}
150 
151 	if (v7fs_populate(dir, root, fsopts, &v7fs_mount) != 0) {
152 		error = 2;	/* some files couldn't add */
153 		goto err_exit;
154 	}
155 
156 	close(fd);
157 	return;
158 
159  err_exit:
160 	close(fd);
161 	err(error, "%s", image);
162 }
163 
164 void
165 progress(const struct progress_arg *p)
166 {
167 #ifndef HAVE_NBTOOL_CONFIG_H
168 	static struct progress_arg Progress;
169 	static char cdev[32];
170 	static char label[32];
171 
172 	if (!progress_bar_enable)
173 		return;
174 
175 	if (p) {
176 		Progress = *p;
177 		if (p->cdev)
178 			strcpy(cdev, p->cdev);
179 		if (p->label)
180 			strcpy(label, p->label);
181 	}
182 
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);
189 	}
190 #endif
191 }
192