int hdlink_cnt = HDLINK_CNT;
/* Link an inode number to a directory */
-static errcode_t add_link(ext2_ino_t parent_ino, ext2_ino_t ino,
- const char *name)
+static errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino,
+ ext2_ino_t ino, const char *name)
{
struct ext2_inode inode;
errcode_t retval;
- retval = ext2fs_read_inode(current_fs, ino, &inode);
+ retval = ext2fs_read_inode(fs, ino, &inode);
if (retval) {
com_err(__func__, retval, "while reading inode %u", ino);
return retval;
}
- retval = ext2fs_link(current_fs, parent_ino, name, ino, inode.i_flags);
+ retval = ext2fs_link(fs, parent_ino, name, ino, inode.i_flags);
if (retval == EXT2_ET_DIR_NO_SPACE) {
- retval = ext2fs_expand_dir(current_fs, parent_ino);
+ retval = ext2fs_expand_dir(fs, parent_ino);
if (retval) {
com_err(__func__, retval, "while expanding directory");
return retval;
}
- retval = ext2fs_link(current_fs, parent_ino, name, ino,
- inode.i_flags);
+ retval = ext2fs_link(fs, parent_ino, name, ino, inode.i_flags);
}
if (retval) {
com_err(__func__, retval, "while linking %s", name);
inode.i_links_count++;
- retval = ext2fs_write_inode(current_fs, ino, &inode);
+ retval = ext2fs_write_inode(fs, ino, &inode);
if (retval)
com_err(__func__, retval, "while writing inode %u", ino);
}
/* Set the uid, gid, mode and time for the inode */
-errcode_t set_inode_extra(ext2_ino_t cwd, ext2_ino_t ino, struct stat *st)
+static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd,
+ ext2_ino_t ino, struct stat *st)
{
errcode_t retval;
struct ext2_inode inode;
- retval = ext2fs_read_inode(current_fs, ino, &inode);
+ retval = ext2fs_read_inode(fs, ino, &inode);
if (retval) {
com_err(__func__, retval, "while reading inode %u", ino);
return retval;
fill_inode(&inode, st);
- retval = ext2fs_write_inode(current_fs, ino, &inode);
+ retval = ext2fs_write_inode(fs, ino, &inode);
if (retval) {
com_err(__func__, retval, "while writing inode %u", ino);
return retval;
}
/* Make a special file which is block, character and fifo */
-errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st)
+errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
+ struct stat *st)
{
ext2_ino_t ino;
errcode_t retval;
break;
}
- if (!(current_fs->flags & EXT2_FLAG_RW)) {
+ if (!(fs->flags & EXT2_FLAG_RW)) {
com_err(__func__, 0, "Filesystem opened read/only");
return -1;
}
- retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino);
+ retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
if (retval) {
com_err(__func__, retval, 0);
return retval;
#ifdef DEBUGFS
printf("Allocated inode: %u\n", ino);
#endif
- retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
+ retval = ext2fs_link(fs, cwd, name, ino, filetype);
if (retval == EXT2_ET_DIR_NO_SPACE) {
- retval = ext2fs_expand_dir(current_fs, cwd);
+ retval = ext2fs_expand_dir(fs, cwd);
if (retval) {
com_err(__func__, retval, "while expanding directory");
return retval;
}
- retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
+ retval = ext2fs_link(fs, cwd, name, ino, filetype);
}
if (retval) {
com_err(name, retval, 0);
return -1;
}
- if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino))
+ if (ext2fs_test_inode_bitmap2(fs->inode_map, ino))
com_err(__func__, 0, "Warning: inode already set");
- ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
+ ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
memset(&inode, 0, sizeof(inode));
inode.i_mode = mode;
inode.i_atime = inode.i_ctime = inode.i_mtime =
- current_fs->now ? current_fs->now : time(0);
+ fs->now ? fs->now : time(0);
major = major(st->st_rdev);
minor = minor(st->st_rdev);
}
inode.i_links_count = 1;
- retval = ext2fs_write_new_inode(current_fs, ino, &inode);
+ retval = ext2fs_write_new_inode(fs, ino, &inode);
if (retval)
com_err(__func__, retval, "while creating inode %u", ino);
}
/* Make a symlink name -> target */
-errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
+errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
+ char *target, ext2_ino_t root)
{
char *cp;
ext2_ino_t parent_ino;
cp = strrchr(name, '/');
if (cp) {
*cp = 0;
- retval = ext2fs_namei(current_fs, root, cwd, name,
- &parent_ino);
+ retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
if (retval) {
com_err(name, retval, 0);
return retval;
parent_ino = cwd;
try_again:
- retval = ext2fs_symlink(current_fs, parent_ino, 0, name, target);
+ retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
if (retval == EXT2_ET_DIR_NO_SPACE) {
- retval = ext2fs_expand_dir(current_fs, parent_ino);
+ retval = ext2fs_expand_dir(fs, parent_ino);
if (retval) {
com_err("do_symlink_internal", retval,
"while expanding directory");
}
/* Make a directory in the fs */
-errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
+errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
+ struct stat *st, ext2_ino_t root)
{
char *cp;
ext2_ino_t parent_ino, ino;
cp = strrchr(name, '/');
if (cp) {
*cp = 0;
- retval = ext2fs_namei(current_fs, root, cwd, name,
- &parent_ino);
+ retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
if (retval) {
com_err(name, retval, 0);
return retval;
parent_ino = cwd;
try_again:
- retval = ext2fs_mkdir(current_fs, parent_ino, 0, name);
+ retval = ext2fs_mkdir(fs, parent_ino, 0, name);
if (retval == EXT2_ET_DIR_NO_SPACE) {
- retval = ext2fs_expand_dir(current_fs, parent_ino);
+ retval = ext2fs_expand_dir(fs, parent_ino);
if (retval) {
com_err(__func__, retval, "while expanding directory");
return retval;
}
}
-static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize,
- int make_holes)
+static errcode_t copy_file(ext2_filsys fs, int fd, ext2_ino_t newfile,
+ int bufsize, int make_holes)
{
ext2_file_t e2_file;
errcode_t retval;
char *zero_buf;
int cmp;
- retval = ext2fs_file_open(current_fs, newfile,
+ retval = ext2fs_file_open(fs, newfile,
EXT2_FILE_WRITE, &e2_file);
if (retval)
return retval;
return retval;
}
-int is_hardlink(ext2_ino_t ino)
+static int is_hardlink(ext2_ino_t ino)
{
int i;
}
/* Copy the native file to the fs */
-errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
+errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
+ const char *dest, ext2_ino_t root)
{
int fd;
struct stat statbuf;
return errno;
}
- retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile);
+ retval = ext2fs_namei(fs, root, cwd, dest, &newfile);
if (retval == 0) {
close(fd);
return EXT2_ET_FILE_EXISTS;
}
- retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
+ retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile);
if (retval) {
com_err(__func__, retval, 0);
close(fd);
#ifdef DEBUGFS
printf("Allocated inode: %u\n", newfile);
#endif
- retval = ext2fs_link(current_fs, cwd, dest, newfile,
+ retval = ext2fs_link(fs, cwd, dest, newfile,
EXT2_FT_REG_FILE);
if (retval == EXT2_ET_DIR_NO_SPACE) {
- retval = ext2fs_expand_dir(current_fs, cwd);
+ retval = ext2fs_expand_dir(fs, cwd);
if (retval) {
com_err(__func__, retval, "while expanding directory");
close(fd);
return retval;
}
- retval = ext2fs_link(current_fs, cwd, dest, newfile,
+ retval = ext2fs_link(fs, cwd, dest, newfile,
EXT2_FT_REG_FILE);
}
if (retval) {
close(fd);
return errno;
}
- if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile))
+ if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile))
com_err(__func__, 0, "Warning: inode already set");
- ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
+ ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
memset(&inode, 0, sizeof(inode));
inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
inode.i_atime = inode.i_ctime = inode.i_mtime =
- current_fs->now ? current_fs->now : time(0);
+ fs->now ? fs->now : time(0);
inode.i_links_count = 1;
inode.i_size = statbuf.st_size;
- if (EXT2_HAS_INCOMPAT_FEATURE(current_fs->super,
+ if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
inode.i_flags |= EXT4_INLINE_DATA_FL;
- } else if (current_fs->super->s_feature_incompat &
+ } else if (fs->super->s_feature_incompat &
EXT3_FEATURE_INCOMPAT_EXTENTS) {
int i;
struct ext3_extent_header *eh;
inode.i_flags |= EXT4_EXTENTS_FL;
}
- retval = ext2fs_write_new_inode(current_fs, newfile, &inode);
+ retval = ext2fs_write_new_inode(fs, newfile, &inode);
if (retval) {
com_err(__func__, retval, "while creating inode %u", newfile);
close(fd);
return retval;
}
if (inode.i_flags & EXT4_INLINE_DATA_FL) {
- retval = ext2fs_inline_data_init(current_fs, newfile);
+ retval = ext2fs_inline_data_init(fs, newfile);
if (retval) {
com_err("copy_file", retval, 0);
close(fd);
*/
bufsize = statbuf.st_blksize;
}
- retval = copy_file(fd, newfile, bufsize, make_holes);
+ retval = copy_file(fs, fd, newfile, bufsize, make_holes);
if (retval)
com_err("copy_file", retval, 0);
}
}
/* Copy files from source_dir to fs */
-errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
+errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
+ const char *source_dir, ext2_ino_t root)
{
const char *name;
DIR *dh;
int read_cnt;
int hdlink;
- root = EXT2_ROOT_INO;
-
if (chdir(source_dir) < 0) {
com_err(__func__, errno,
_("while changing working directory to \"%s\""),
st.st_nlink > 1) {
hdlink = is_hardlink(st.st_ino);
if (hdlink >= 0) {
- retval = add_link(parent_ino,
+ retval = add_link(fs, parent_ino,
hdlinks.hdl[hdlink].dst_ino,
name);
if (retval) {
case S_IFCHR:
case S_IFBLK:
case S_IFIFO:
- retval = do_mknod_internal(parent_ino, name, &st);
+ retval = do_mknod_internal(fs, parent_ino, name, &st);
if (retval) {
com_err(__func__, retval,
_("while creating special file "
return errno;
}
ln_target[read_cnt] = '\0';
- retval = do_symlink_internal(parent_ino, name,
- ln_target);
+ retval = do_symlink_internal(fs, parent_ino, name,
+ ln_target, root);
if (retval) {
com_err(__func__, retval,
_("while writing symlink\"%s\""),
}
break;
case S_IFREG:
- retval = do_write_internal(parent_ino, name, name);
+ retval = do_write_internal(fs, parent_ino, name, name,
+ root);
if (retval) {
com_err(__func__, retval,
_("while writing file \"%s\""), name);
}
break;
case S_IFDIR:
- retval = do_mkdir_internal(parent_ino, name, &st);
+ retval = do_mkdir_internal(fs, parent_ino, name, &st,
+ root);
if (retval) {
com_err(__func__, retval,
_("while making dir \"%s\""), name);
return retval;
}
- retval = ext2fs_namei(current_fs, root, parent_ino,
+ retval = ext2fs_namei(fs, root, parent_ino,
name, &ino);
if (retval) {
com_err(name, retval, 0);
return retval;
}
/* Populate the dir recursively*/
- retval = populate_fs(ino, name);
+ retval = populate_fs(fs, ino, name, root);
if (retval) {
com_err(__func__, retval,
_("while adding dir \"%s\""), name);
return retval;
}
- chdir("..");
+ if (chdir("..")) {
+ com_err(__func__, errno,
+ _("during cd .."));
+ return errno;
+ }
break;
default:
com_err(__func__, 0,
_("ignoring entry \"%s\""), name);
}
- retval = ext2fs_namei(current_fs, root, parent_ino,
- name, &ino);
+ retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
if (retval) {
com_err(name, retval, 0);
return retval;
}
- retval = set_inode_extra(parent_ino, ino, &st);
+ retval = set_inode_extra(fs, parent_ino, ino, &st);
if (retval) {
com_err(__func__, retval,
_("while setting inode for \"%s\""), name);