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