1*514b0270Shannken /* $NetBSD: v7fs_populate.c,v 1.4 2022/02/11 10:55:15 hannken Exp $ */
2dd9e8309Such
3dd9e8309Such /*-
4dd9e8309Such * Copyright (c) 2011 The NetBSD Foundation, Inc.
5dd9e8309Such * All rights reserved.
6dd9e8309Such *
7dd9e8309Such * This code is derived from software contributed to The NetBSD Foundation
8dd9e8309Such * by UCHIYAMA Yasushi.
9dd9e8309Such *
10dd9e8309Such * Redistribution and use in source and binary forms, with or without
11dd9e8309Such * modification, are permitted provided that the following conditions
12dd9e8309Such * are met:
13dd9e8309Such * 1. Redistributions of source code must retain the above copyright
14dd9e8309Such * notice, this list of conditions and the following disclaimer.
15dd9e8309Such * 2. Redistributions in binary form must reproduce the above copyright
16dd9e8309Such * notice, this list of conditions and the following disclaimer in the
17dd9e8309Such * documentation and/or other materials provided with the distribution.
18dd9e8309Such *
19dd9e8309Such * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20dd9e8309Such * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21dd9e8309Such * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22dd9e8309Such * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23dd9e8309Such * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24dd9e8309Such * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25dd9e8309Such * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26dd9e8309Such * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27dd9e8309Such * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28dd9e8309Such * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29dd9e8309Such * POSSIBILITY OF SUCH DAMAGE.
30dd9e8309Such */
31dd9e8309Such
32dd9e8309Such #if HAVE_NBTOOL_CONFIG_H
33dd9e8309Such #include "nbtool_config.h"
34dd9e8309Such #endif
35dd9e8309Such
36dd9e8309Such #include <sys/cdefs.h>
37dd9e8309Such #if defined(__RCSID) && !defined(__lint)
38*514b0270Shannken __RCSID("$NetBSD: v7fs_populate.c,v 1.4 2022/02/11 10:55:15 hannken Exp $");
39dd9e8309Such #endif /* !__lint */
40dd9e8309Such
41dd9e8309Such #include <stdio.h>
42dd9e8309Such #include <stdlib.h>
43dd9e8309Such #include <string.h>
44dd9e8309Such #include <unistd.h>
45dd9e8309Such #include <fcntl.h>
46dd9e8309Such #include <errno.h>
47dd9e8309Such #include <err.h>
48dd9e8309Such
49dd9e8309Such #if !HAVE_NBTOOL_CONFIG_H
50dd9e8309Such #include <sys/mount.h>
51dd9e8309Such #endif
52dd9e8309Such
53dd9e8309Such #include "makefs.h"
54dd9e8309Such #include "v7fs.h"
55dd9e8309Such #include "v7fs_impl.h"
56dd9e8309Such #include "v7fs_inode.h"
57dd9e8309Such #include "v7fs_superblock.h"
58dd9e8309Such #include "v7fs_datablock.h"
59dd9e8309Such #include "v7fs_endian.h"
60dd9e8309Such #include "v7fs_file.h"
61dd9e8309Such #include "v7fs_makefs.h"
62dd9e8309Such #include "newfs_v7fs.h"
63dd9e8309Such
647d182262Such #define VPRINTF(fmt, args...) { if (v7fs_newfs_verbose) printf(fmt, ##args); }
65dd9e8309Such
66dd9e8309Such static void
attr_setup(fsnode * node,struct v7fs_fileattr * attr)67dd9e8309Such attr_setup(fsnode *node, struct v7fs_fileattr *attr)
68dd9e8309Such {
69dd9e8309Such struct stat *st = &node->inode->st;
70dd9e8309Such
71dd9e8309Such attr->mode = node->type | st->st_mode;
72dd9e8309Such attr->uid = st->st_uid;
73dd9e8309Such attr->gid = st->st_gid;
74dd9e8309Such attr->device = 0;
75dd9e8309Such attr->ctime = st->st_ctime;
76dd9e8309Such attr->atime = st->st_atime;
77dd9e8309Such attr->mtime = st->st_mtime;
78dd9e8309Such }
79dd9e8309Such
80dd9e8309Such static int
allocate(struct v7fs_self * fs,struct v7fs_inode * parent_inode,fsnode * node,dev_t dev,struct v7fs_inode * inode)81dd9e8309Such allocate(struct v7fs_self *fs, struct v7fs_inode *parent_inode, fsnode *node,
82dd9e8309Such dev_t dev, struct v7fs_inode *inode)
83dd9e8309Such {
84dd9e8309Such int error;
85dd9e8309Such v7fs_ino_t ino;
86dd9e8309Such struct v7fs_fileattr attr;
87dd9e8309Such
88dd9e8309Such memset(inode, 0, sizeof(*inode));
89dd9e8309Such
90dd9e8309Such attr_setup(node, &attr);
91dd9e8309Such attr.device = dev;
92*514b0270Shannken if ((error = v7fs_file_allocate(fs, parent_inode, node->name,
93*514b0270Shannken strlen(node->name), &attr, &ino))) {
94dd9e8309Such errno = error;
95dd9e8309Such warn("%s", node->name);
96dd9e8309Such return error;
97dd9e8309Such }
98dd9e8309Such node->inode->ino = ino;
99dd9e8309Such node->inode->flags |= FI_ALLOCATED;
100dd9e8309Such if ((error = v7fs_inode_load(fs, inode, ino))) {
101dd9e8309Such errno = error;
102dd9e8309Such warn("%s", node->name);
103dd9e8309Such return error;
104dd9e8309Such }
105dd9e8309Such
106dd9e8309Such return 0;
107dd9e8309Such }
108dd9e8309Such
109dd9e8309Such struct copy_arg {
110dd9e8309Such int fd;
111dd9e8309Such uint8_t buf[V7FS_BSIZE];
112dd9e8309Such };
113dd9e8309Such
114dd9e8309Such static int
copy_subr(struct v7fs_self * fs,void * ctx,v7fs_daddr_t blk,size_t sz)115dd9e8309Such copy_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
116dd9e8309Such {
117dd9e8309Such struct copy_arg *p = ctx;
118dd9e8309Such
1192c0999f3Stron if (read(p->fd, p->buf, sz) != (ssize_t)sz) {
120dd9e8309Such return V7FS_ITERATOR_ERROR;
121dd9e8309Such }
122dd9e8309Such
123dd9e8309Such if (!fs->io.write(fs->io.cookie, p->buf, blk)) {
124dd9e8309Such errno = EIO;
125dd9e8309Such return V7FS_ITERATOR_ERROR;
126dd9e8309Such }
127dd9e8309Such progress(0);
128dd9e8309Such
129dd9e8309Such return 0;
130dd9e8309Such }
131dd9e8309Such
132dd9e8309Such static int
file_copy(struct v7fs_self * fs,struct v7fs_inode * parent_inode,fsnode * node,const char * filepath)133dd9e8309Such file_copy(struct v7fs_self *fs, struct v7fs_inode *parent_inode, fsnode *node,
134dd9e8309Such const char *filepath)
135dd9e8309Such {
136dd9e8309Such struct v7fs_inode inode;
137dd9e8309Such const char *errmsg;
138dd9e8309Such fsinode *fnode = node->inode;
139dd9e8309Such int error = 0;
140dd9e8309Such int fd;
141dd9e8309Such
142dd9e8309Such /* Check hard-link */
143dd9e8309Such if ((fnode->nlink > 1) && (fnode->flags & FI_ALLOCATED)) {
144dd9e8309Such if ((error = v7fs_inode_load(fs, &inode, fnode->ino))) {
145dd9e8309Such errmsg = "inode load";
146dd9e8309Such goto err_exit;
147dd9e8309Such }
148dd9e8309Such if ((error = v7fs_file_link(fs, parent_inode, &inode,
149*514b0270Shannken node->name, strlen(node->name)))) {
150dd9e8309Such errmsg = "hard link";
151dd9e8309Such goto err_exit;
152dd9e8309Such }
153dd9e8309Such return 0;
154dd9e8309Such }
155dd9e8309Such
156dd9e8309Such /* Allocate file */
157dd9e8309Such if ((error = allocate(fs, parent_inode, node, 0, &inode))) {
158dd9e8309Such errmsg = "file allocate";
159dd9e8309Such goto err_exit;
160dd9e8309Such }
161dd9e8309Such if ((error = v7fs_datablock_expand(fs, &inode, fnode->st.st_size))) {
162dd9e8309Such errmsg = "datablock expand";
163dd9e8309Such goto err_exit;
164dd9e8309Such }
165dd9e8309Such
166dd9e8309Such /* Data copy */
167dd9e8309Such if ((fd = open(filepath, O_RDONLY)) == -1) {
168dd9e8309Such error = errno;
169dd9e8309Such errmsg = "source file";
170dd9e8309Such goto err_exit;
171dd9e8309Such }
172dd9e8309Such
173dd9e8309Such error = v7fs_datablock_foreach(fs, &inode, copy_subr,
174dd9e8309Such &(struct copy_arg){ .fd = fd });
175dd9e8309Such if (error != V7FS_ITERATOR_END) {
176dd9e8309Such errmsg = "data copy";
177dd9e8309Such close(fd);
178dd9e8309Such goto err_exit;
179dd9e8309Such } else {
180dd9e8309Such error = 0;
181dd9e8309Such }
182dd9e8309Such close(fd);
183dd9e8309Such
184dd9e8309Such return error;
185dd9e8309Such
186dd9e8309Such err_exit:
187dd9e8309Such errno = error;
188dd9e8309Such warn("%s %s", node->name, errmsg);
189dd9e8309Such
190dd9e8309Such return error;
191dd9e8309Such }
192dd9e8309Such
193dd9e8309Such static int
populate_walk(struct v7fs_self * fs,struct v7fs_inode * parent_inode,fsnode * root,char * dir)194dd9e8309Such populate_walk(struct v7fs_self *fs, struct v7fs_inode *parent_inode,
195dd9e8309Such fsnode *root, char *dir)
196dd9e8309Such {
197dd9e8309Such fsnode *cur;
198dd9e8309Such char *mydir = dir + strlen(dir);
199dd9e8309Such char srcpath[MAXPATHLEN + 1];
200dd9e8309Such struct v7fs_inode inode;
201dd9e8309Such int error = 0;
202dd9e8309Such bool failed = false;
203dd9e8309Such
204dd9e8309Such for (cur = root; cur != NULL; cur = cur->next) {
205dd9e8309Such switch (cur->type & S_IFMT) {
206dd9e8309Such default:
207dd9e8309Such VPRINTF("%x\n", cur->flags & S_IFMT);
208dd9e8309Such break;
209dd9e8309Such case S_IFCHR:
210dd9e8309Such /*FALLTHROUGH*/
211dd9e8309Such case S_IFBLK:
212dd9e8309Such if ((error = allocate(fs, parent_inode, cur,
213dd9e8309Such cur->inode->st.st_rdev, &inode))) {
214dd9e8309Such errno = error;
215dd9e8309Such warn("%s", cur->name);
216dd9e8309Such }
217dd9e8309Such break;
218dd9e8309Such case S_IFDIR:
219dd9e8309Such if (!cur->child) /*'.'*/
220dd9e8309Such break;
221dd9e8309Such /* Allocate this directory. */
222dd9e8309Such if ((error = allocate(fs, parent_inode, cur, 0,
223dd9e8309Such &inode))) {
224dd9e8309Such errno = error;
225dd9e8309Such warn("%s", cur->name);
226dd9e8309Such } else {
227dd9e8309Such /* Populate children. */
228dd9e8309Such mydir[0] = '/';
229dd9e8309Such strcpy(&mydir[1], cur->name);
230dd9e8309Such error = populate_walk(fs, &inode, cur->child,
231dd9e8309Such dir);
232dd9e8309Such mydir[0] = '\0';
233dd9e8309Such }
234dd9e8309Such break;
235dd9e8309Such case S_IFREG:
236dd9e8309Such snprintf(srcpath, sizeof(srcpath), "%s/%s", dir,
237dd9e8309Such cur->name);
238dd9e8309Such error = file_copy(fs, parent_inode, cur, srcpath);
239dd9e8309Such break;
240dd9e8309Such case S_IFLNK:
241dd9e8309Such if ((error = allocate(fs, parent_inode, cur, 0,
242dd9e8309Such &inode))) {
243dd9e8309Such errno = error;
244dd9e8309Such warn("%s", cur->name);
245dd9e8309Such } else {
246dd9e8309Such v7fs_file_symlink(fs, &inode, cur->symlink);
247dd9e8309Such }
248dd9e8309Such break;
249dd9e8309Such }
250dd9e8309Such if (error)
251dd9e8309Such failed = true;
252dd9e8309Such }
253dd9e8309Such
254dd9e8309Such return failed ? 2 : 0;
255dd9e8309Such }
256dd9e8309Such
257dd9e8309Such int
v7fs_populate(const char * dir,fsnode * root,fsinfo_t * fsopts,const struct v7fs_mount_device * device)258dd9e8309Such v7fs_populate(const char *dir, fsnode *root, fsinfo_t *fsopts,
259dd9e8309Such const struct v7fs_mount_device *device)
260dd9e8309Such {
261dd9e8309Such v7fs_opt_t *v7fs_opts = fsopts->fs_specific;
262dd9e8309Such static char path[MAXPATHLEN + 1];
263dd9e8309Such struct v7fs_inode root_inode;
264dd9e8309Such struct v7fs_self *fs;
265dd9e8309Such int error;
266dd9e8309Such
267dd9e8309Such if ((error = v7fs_io_init(&fs, device, V7FS_BSIZE))) {
268dd9e8309Such errno = error;
269dd9e8309Such warn("I/O setup failed.");
270dd9e8309Such return error;
271dd9e8309Such }
272dd9e8309Such fs->endian = device->endian;
273dd9e8309Such v7fs_endian_init(fs);
274dd9e8309Such
275dd9e8309Such if ((error = v7fs_superblock_load(fs))) {
276dd9e8309Such errno = error;
277dd9e8309Such warn("Can't load superblock.");
278dd9e8309Such return error;
279dd9e8309Such }
280dd9e8309Such fsopts->superblock = &fs->superblock; /* not used. */
281dd9e8309Such
282dd9e8309Such if ((error = v7fs_inode_load(fs, &root_inode, V7FS_ROOT_INODE))) {
283dd9e8309Such errno = error;
284dd9e8309Such warn("Can't load root inode.");
285dd9e8309Such return error;
286dd9e8309Such }
287dd9e8309Such
288dd9e8309Such progress(&(struct progress_arg){ .label = "populate", .tick =
289dd9e8309Such v7fs_opts->npuredatablk / PROGRESS_BAR_GRANULE });
290dd9e8309Such
291dd9e8309Such strncpy(path, dir, sizeof(path));
292dd9e8309Such error = populate_walk(fs, &root_inode, root, path);
293dd9e8309Such
294dd9e8309Such v7fs_inode_writeback(fs, &root_inode);
295dd9e8309Such v7fs_superblock_writeback(fs);
296dd9e8309Such
297dd9e8309Such return error;
298dd9e8309Such }
299