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