Remove building with NOCRYPTO option
[minix.git] / usr.sbin / makefs / v7fs / v7fs_populate.c
blob5b6215858248fce5ad3ae0310bd3720ff580b1b8
1 /* $NetBSD: v7fs_populate.c,v 1.3 2011/08/10 11:31:49 uch 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_populate.c,v 1.3 2011/08/10 11:31:49 uch Exp $");
39 #endif /* !__lint */
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <err.h>
49 #if !HAVE_NBTOOL_CONFIG_H
50 #include <sys/mount.h>
51 #endif
53 #include "makefs.h"
54 #include "v7fs.h"
55 #include "v7fs_impl.h"
56 #include "v7fs_inode.h"
57 #include "v7fs_superblock.h"
58 #include "v7fs_datablock.h"
59 #include "v7fs_endian.h"
60 #include "v7fs_file.h"
61 #include "v7fs_makefs.h"
62 #include "newfs_v7fs.h"
64 #define VPRINTF(fmt, args...) { if (v7fs_newfs_verbose) printf(fmt, ##args); }
66 static void
67 attr_setup(fsnode *node, struct v7fs_fileattr *attr)
69 struct stat *st = &node->inode->st;
71 attr->mode = node->type | st->st_mode;
72 attr->uid = st->st_uid;
73 attr->gid = st->st_gid;
74 attr->device = 0;
75 attr->ctime = st->st_ctime;
76 attr->atime = st->st_atime;
77 attr->mtime = st->st_mtime;
80 static int
81 allocate(struct v7fs_self *fs, struct v7fs_inode *parent_inode, fsnode *node,
82 dev_t dev, struct v7fs_inode *inode)
84 int error;
85 v7fs_ino_t ino;
86 struct v7fs_fileattr attr;
88 memset(inode, 0, sizeof(*inode));
90 attr_setup(node, &attr);
91 attr.device = dev;
92 if ((error = v7fs_file_allocate(fs, parent_inode, node->name, &attr,
93 &ino))) {
94 errno = error;
95 warn("%s", node->name);
96 return error;
98 node->inode->ino = ino;
99 node->inode->flags |= FI_ALLOCATED;
100 if ((error = v7fs_inode_load(fs, inode, ino))) {
101 errno = error;
102 warn("%s", node->name);
103 return error;
106 return 0;
109 struct copy_arg {
110 int fd;
111 uint8_t buf[V7FS_BSIZE];
114 static int
115 copy_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
117 struct copy_arg *p = ctx;
119 if (read(p->fd, p->buf, sz) != (ssize_t)sz) {
120 return V7FS_ITERATOR_ERROR;
123 if (!fs->io.write(fs->io.cookie, p->buf, blk)) {
124 errno = EIO;
125 return V7FS_ITERATOR_ERROR;
127 progress(0);
129 return 0;
132 static int
133 file_copy(struct v7fs_self *fs, struct v7fs_inode *parent_inode, fsnode *node,
134 const char *filepath)
136 struct v7fs_inode inode;
137 const char *errmsg;
138 fsinode *fnode = node->inode;
139 int error = 0;
140 int fd;
142 /* Check hard-link */
143 if ((fnode->nlink > 1) && (fnode->flags & FI_ALLOCATED)) {
144 if ((error = v7fs_inode_load(fs, &inode, fnode->ino))) {
145 errmsg = "inode load";
146 goto err_exit;
148 if ((error = v7fs_file_link(fs, parent_inode, &inode,
149 node->name))) {
150 errmsg = "hard link";
151 goto err_exit;
153 return 0;
156 /* Allocate file */
157 if ((error = allocate(fs, parent_inode, node, 0, &inode))) {
158 errmsg = "file allocate";
159 goto err_exit;
161 if ((error = v7fs_datablock_expand(fs, &inode, fnode->st.st_size))) {
162 errmsg = "datablock expand";
163 goto err_exit;
166 /* Data copy */
167 if ((fd = open(filepath, O_RDONLY)) == -1) {
168 error = errno;
169 errmsg = "source file";
170 goto err_exit;
173 error = v7fs_datablock_foreach(fs, &inode, copy_subr,
174 &(struct copy_arg){ .fd = fd });
175 if (error != V7FS_ITERATOR_END) {
176 errmsg = "data copy";
177 close(fd);
178 goto err_exit;
179 } else {
180 error = 0;
182 close(fd);
184 return error;
186 err_exit:
187 errno = error;
188 warn("%s %s", node->name, errmsg);
190 return error;
193 static int
194 populate_walk(struct v7fs_self *fs, struct v7fs_inode *parent_inode,
195 fsnode *root, char *dir)
197 fsnode *cur;
198 char *mydir = dir + strlen(dir);
199 char srcpath[MAXPATHLEN + 1];
200 struct v7fs_inode inode;
201 int error = 0;
202 bool failed = false;
204 for (cur = root; cur != NULL; cur = cur->next) {
205 switch (cur->type & S_IFMT) {
206 default:
207 VPRINTF("%x\n", cur->flags & S_IFMT);
208 break;
209 case S_IFCHR:
210 /*FALLTHROUGH*/
211 case S_IFBLK:
212 if ((error = allocate(fs, parent_inode, cur,
213 cur->inode->st.st_rdev, &inode))) {
214 errno = error;
215 warn("%s", cur->name);
217 break;
218 case S_IFDIR:
219 if (!cur->child) /*'.'*/
220 break;
221 /* Allocate this directory. */
222 if ((error = allocate(fs, parent_inode, cur, 0,
223 &inode))) {
224 errno = error;
225 warn("%s", cur->name);
226 } else {
227 /* Populate children. */
228 mydir[0] = '/';
229 strcpy(&mydir[1], cur->name);
230 error = populate_walk(fs, &inode, cur->child,
231 dir);
232 mydir[0] = '\0';
234 break;
235 case S_IFREG:
236 snprintf(srcpath, sizeof(srcpath), "%s/%s", dir,
237 cur->name);
238 error = file_copy(fs, parent_inode, cur, srcpath);
239 break;
240 case S_IFLNK:
241 if ((error = allocate(fs, parent_inode, cur, 0,
242 &inode))) {
243 errno = error;
244 warn("%s", cur->name);
245 } else {
246 v7fs_file_symlink(fs, &inode, cur->symlink);
248 break;
250 if (error)
251 failed = true;
254 return failed ? 2 : 0;
258 v7fs_populate(const char *dir, fsnode *root, fsinfo_t *fsopts,
259 const struct v7fs_mount_device *device)
261 v7fs_opt_t *v7fs_opts = fsopts->fs_specific;
262 static char path[MAXPATHLEN + 1];
263 struct v7fs_inode root_inode;
264 struct v7fs_self *fs;
265 int error;
267 if ((error = v7fs_io_init(&fs, device, V7FS_BSIZE))) {
268 errno = error;
269 warn("I/O setup failed.");
270 return error;
272 fs->endian = device->endian;
273 v7fs_endian_init(fs);
275 if ((error = v7fs_superblock_load(fs))) {
276 errno = error;
277 warn("Can't load superblock.");
278 return error;
280 fsopts->superblock = &fs->superblock; /* not used. */
282 if ((error = v7fs_inode_load(fs, &root_inode, V7FS_ROOT_INODE))) {
283 errno = error;
284 warn("Can't load root inode.");
285 return error;
288 progress(&(struct progress_arg){ .label = "populate", .tick =
289 v7fs_opts->npuredatablk / PROGRESS_BAR_GRANULE });
291 strncpy(path, dir, sizeof(path));
292 error = populate_walk(fs, &root_inode, root, path);
294 v7fs_inode_writeback(fs, &root_inode);
295 v7fs_superblock_writeback(fs);
297 return error;