2 * create_inode.c --- create an inode
4 * Copyright (C) 2014 Robert Yang <liezhi.yang@windriver.com>
7 * This file may be redistributed under the terms of the GNU library
8 * General Public License, version 2.
12 #define _FILE_OFFSET_BITS 64
13 #define _LARGEFILE64_SOURCE 1
19 #include <sys/types.h>
21 #include <limits.h> /* for PATH_MAX */
22 #include <dirent.h> /* for scandir() and alphasort() */
23 #if defined HAVE_SYS_XATTR_H
24 #include <sys/xattr.h>
25 #elif defined HAVE_ATTR_XATTR_H
26 #include <attr/xattr.h>
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
31 #ifdef HAVE_SYS_SYSMACROS_H
32 #include <sys/sysmacros.h>
35 #include <ext2fs/ext2fs.h>
36 #include <ext2fs/ext2_types.h>
37 #include <ext2fs/fiemap.h>
39 #include "create_inode.h"
40 #include "support/nls-enable.h"
42 #include "create_inode_libarchive.h"
44 /* 64KiB is the minimum blksize to best minimize system call overhead. */
45 #define COPY_FILE_BUFLEN 65536
47 static int ext2_file_type(unsigned int mode)
49 if (LINUX_S_ISREG(mode))
50 return EXT2_FT_REG_FILE;
52 if (LINUX_S_ISDIR(mode))
55 if (LINUX_S_ISCHR(mode))
56 return EXT2_FT_CHRDEV;
58 if (LINUX_S_ISBLK(mode))
59 return EXT2_FT_BLKDEV;
61 if (LINUX_S_ISLNK(mode))
62 return EXT2_FT_SYMLINK;
64 if (LINUX_S_ISFIFO(mode))
67 if (LINUX_S_ISSOCK(mode))
73 /* Link an inode number to a directory */
74 errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino,
75 ext2_ino_t ino, const char *name)
77 struct ext2_inode inode;
80 retval = ext2fs_read_inode(fs, ino, &inode);
82 com_err(__func__, retval, _("while reading inode %u"), ino);
86 retval = ext2fs_link(fs, parent_ino, name, ino,
87 ext2_file_type(inode.i_mode));
88 if (retval == EXT2_ET_DIR_NO_SPACE) {
89 retval = ext2fs_expand_dir(fs, parent_ino);
91 com_err(__func__, retval,
92 _("while expanding directory"));
95 retval = ext2fs_link(fs, parent_ino, name, ino,
96 ext2_file_type(inode.i_mode));
99 com_err(__func__, retval, _("while linking \"%s\""), name);
103 inode.i_links_count++;
105 retval = ext2fs_write_inode(fs, ino, &inode);
107 com_err(__func__, retval, _("while writing inode %u"), ino);
112 static time_t clamped_time(ext2_filsys fs, time_t t)
114 if ((fs->flags2 & EXT2_FLAG2_USE_FAKE_TIME) && t > fs->now)
119 /* Set the uid, gid, mode and time for the inode */
120 errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t ino,
121 const struct stat *st)
124 struct ext2_inode inode;
126 retval = ext2fs_read_inode(fs, ino, &inode);
128 com_err(__func__, retval, _("while reading inode %u"), ino);
132 inode.i_uid = st->st_uid;
133 ext2fs_set_i_uid_high(inode, st->st_uid >> 16);
134 inode.i_gid = st->st_gid;
135 ext2fs_set_i_gid_high(inode, st->st_gid >> 16);
136 inode.i_mode = (LINUX_S_IFMT & inode.i_mode) | (~S_IFMT & st->st_mode);
137 ext2fs_inode_xtime_set(&inode, i_atime, clamped_time(fs, st->st_atime));
138 ext2fs_inode_xtime_set(&inode, i_ctime, clamped_time(fs, st->st_ctime));
139 ext2fs_inode_xtime_set(&inode, i_mtime, clamped_time(fs, st->st_mtime));
141 retval = ext2fs_write_inode(fs, ino, &inode);
143 com_err(__func__, retval, _("while writing inode %u"), ino);
147 #ifdef HAVE_LLISTXATTR
148 static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino,
149 const char *filename)
151 errcode_t retval, close_retval;
152 struct ext2_xattr_handle *handle;
153 ssize_t size, value_size;
160 size = llistxattr(filename, NULL, 0);
162 if (errno == ENOTSUP)
165 com_err(__func__, retval, _("while listing attributes of \"%s\""),
168 } else if (size == 0) {
172 retval = ext2fs_xattrs_open(fs, ino, &handle);
174 if (retval == EXT2_ET_MISSING_EA_FEATURE)
176 com_err(__func__, retval, _("while opening inode %u"), ino);
180 retval = ext2fs_xattrs_read(handle);
182 com_err(__func__, retval,
183 _("while reading xattrs for inode %u"), ino);
187 retval = ext2fs_get_mem(size, &list);
189 com_err(__func__, retval, _("while allocating memory"));
193 size = llistxattr(filename, list, size);
196 com_err(__func__, retval, _("while listing attributes of \"%s\""),
201 for (i = 0; i < size; i += strlen(&list[i]) + 1) {
202 const char *name = &list[i];
205 value_size = lgetxattr(filename, name, NULL, 0);
206 if (value_size == -1) {
208 com_err(__func__, retval,
209 _("while reading attribute \"%s\" of \"%s\""),
214 retval = ext2fs_get_mem(value_size, &value);
216 com_err(__func__, retval, _("while allocating memory"));
220 value_size = lgetxattr(filename, name, value, value_size);
221 if (value_size == -1) {
222 ext2fs_free_mem(&value);
224 com_err(__func__, retval,
225 _("while reading attribute \"%s\" of \"%s\""),
230 retval = ext2fs_xattr_set(handle, name, value, value_size);
231 ext2fs_free_mem(&value);
233 com_err(__func__, retval,
234 _("while writing attribute \"%s\" to inode %u"),
241 ext2fs_free_mem(&list);
242 close_retval = ext2fs_xattrs_close(&handle);
244 com_err(__func__, retval, _("while closing inode %u"), ino);
245 retval = retval ? retval : close_retval;
249 #else /* HAVE_LLISTXATTR */
250 static errcode_t set_inode_xattr(ext2_filsys fs EXT2FS_ATTR((unused)),
251 ext2_ino_t ino EXT2FS_ATTR((unused)),
252 const char *filename EXT2FS_ATTR((unused)))
256 #endif /* HAVE_LLISTXATTR */
259 /* Make a special files (block and character devices), fifo's, and sockets */
260 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
261 unsigned int st_mode, unsigned int st_rdev)
265 struct ext2_inode inode;
266 unsigned long devmajor, devminor, mode;
270 switch(st_mode & S_IFMT) {
272 mode = LINUX_S_IFCHR;
273 filetype = EXT2_FT_CHRDEV;
276 mode = LINUX_S_IFBLK;
277 filetype = EXT2_FT_BLKDEV;
280 mode = LINUX_S_IFIFO;
281 filetype = EXT2_FT_FIFO;
285 mode = LINUX_S_IFSOCK;
286 filetype = EXT2_FT_SOCK;
290 return EXT2_ET_INVALID_ARGUMENT;
293 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
295 com_err(__func__, retval, _("while allocating inode \"%s\""),
301 printf("Allocated inode: %u\n", ino);
303 retval = ext2fs_link(fs, cwd, name, ino, filetype);
304 if (retval == EXT2_ET_DIR_NO_SPACE) {
305 retval = ext2fs_expand_dir(fs, cwd);
307 com_err(__func__, retval,
308 _("while expanding directory"));
311 retval = ext2fs_link(fs, cwd, name, ino, filetype);
314 com_err(name, retval, _("while creating inode \"%s\""), name);
317 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino))
318 com_err(__func__, 0, "Warning: inode already set");
319 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
320 memset(&inode, 0, sizeof(inode));
322 now = fs->now ? fs->now : time(0);
323 ext2fs_inode_xtime_set(&inode, i_atime, now);
324 ext2fs_inode_xtime_set(&inode, i_ctime, now);
325 ext2fs_inode_xtime_set(&inode, i_mtime, now);
327 if (filetype != S_IFIFO) {
328 devmajor = major(st_rdev);
329 devminor = minor(st_rdev);
331 if ((devmajor < 256) && (devminor < 256)) {
332 inode.i_block[0] = devmajor * 256 + devminor;
333 inode.i_block[1] = 0;
335 inode.i_block[0] = 0;
336 inode.i_block[1] = (devminor & 0xff) | (devmajor << 8) |
337 ((devminor & ~0xff) << 12);
340 inode.i_links_count = 1;
342 retval = ext2fs_write_new_inode(fs, ino, &inode);
344 com_err(__func__, retval, _("while writing inode %u"), ino);
350 /* Make a symlink name -> target */
351 errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
352 char *target, ext2_ino_t root)
355 ext2_ino_t parent_ino;
358 cp = strrchr(name, '/');
361 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
363 com_err(name, retval, 0);
370 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
371 if (retval == EXT2_ET_DIR_NO_SPACE) {
372 retval = ext2fs_expand_dir(fs, parent_ino);
374 com_err("do_symlink_internal", retval,
375 _("while expanding directory"));
378 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
381 com_err("ext2fs_symlink", retval,
382 _("while creating symlink \"%s\""), name);
386 /* Make a directory in the fs */
387 errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
391 ext2_ino_t parent_ino;
395 cp = strrchr(name, '/');
398 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
400 com_err(name, retval, _("while looking up \"%s\""),
408 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
409 if (retval == EXT2_ET_DIR_NO_SPACE) {
410 retval = ext2fs_expand_dir(fs, parent_ino);
412 com_err(__func__, retval,
413 _("while expanding directory"));
416 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
419 com_err("ext2fs_mkdir", retval,
420 _("while creating directory \"%s\""), name);
424 #if !defined HAVE_PREAD64 && !defined HAVE_PREAD
425 static ssize_t my_pread(int fd, void *buf, size_t count, off_t offset)
427 if (lseek(fd, offset, SEEK_SET) < 0)
430 return read(fd, buf, count);
432 #endif /* !defined HAVE_PREAD64 && !defined HAVE_PREAD */
434 static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
435 off_t start, off_t end, char *buf,
440 unsigned int written;
444 for (off = start; off < end; off += COPY_FILE_BUFLEN) {
446 got = pread64(fd, buf, COPY_FILE_BUFLEN, off);
448 got = pread(fd, buf, COPY_FILE_BUFLEN, off);
450 got = my_pread(fd, buf, COPY_FILE_BUFLEN, off);
456 for (bpos = 0, ptr = buf; bpos < got; bpos += fs->blocksize) {
457 blen = fs->blocksize;
458 if (blen > got - bpos)
460 if (memcmp(ptr, zerobuf, blen) == 0) {
464 err = ext2fs_file_llseek(e2_file, off + bpos,
465 EXT2_SEEK_SET, NULL);
469 err = ext2fs_file_write(e2_file, ptr, blen,
486 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
487 static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
488 ext2_file_t e2_file, char *buf, char *zerobuf)
490 off_t data = 0, hole;
491 off_t data_blk, hole_blk;
494 /* Try to use SEEK_DATA and SEEK_HOLE */
495 while (data < statbuf->st_size) {
496 data = lseek(fd, data, SEEK_DATA);
500 return EXT2_ET_UNIMPLEMENTED;
502 hole = lseek(fd, data, SEEK_HOLE);
504 return EXT2_ET_UNIMPLEMENTED;
506 data_blk = data & ~(off_t)(fs->blocksize - 1);
507 hole_blk = ((hole + (off_t)(fs->blocksize - 1)) &
508 ~(off_t)(fs->blocksize - 1));
509 err = copy_file_chunk(fs, fd, e2_file, data_blk, hole_blk, buf,
519 #endif /* SEEK_DATA and SEEK_HOLE */
521 #if defined(FS_IOC_FIEMAP)
522 static errcode_t try_fiemap_copy(ext2_filsys fs, int fd, ext2_file_t e2_file,
523 char *buf, char *zerobuf)
525 #define EXTENT_MAX_COUNT 512
526 struct fiemap *fiemap_buf;
527 struct fiemap_extent *ext_buf, *ext;
528 int ext_buf_size, fie_buf_size;
533 ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
534 fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
536 err = ext2fs_get_memzero(fie_buf_size, &fiemap_buf);
540 ext_buf = fiemap_buf->fm_extents;
541 memset(fiemap_buf, 0, fie_buf_size);
542 fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
543 fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
544 fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
547 fiemap_buf->fm_start = pos;
548 memset(ext_buf, 0, ext_buf_size);
549 err = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
550 if (err < 0 && (errno == EOPNOTSUPP || errno == ENOTTY)) {
551 err = EXT2_ET_UNIMPLEMENTED;
553 } else if (err < 0) {
556 } else if (fiemap_buf->fm_mapped_extents == 0)
558 for (i = 0, ext = ext_buf; i < fiemap_buf->fm_mapped_extents;
560 err = copy_file_chunk(fs, fd, e2_file, ext->fe_logical,
561 ext->fe_logical + ext->fe_length,
568 /* Record file's logical offset this time */
569 pos = ext->fe_logical + ext->fe_length;
571 * If fm_extents array has been filled and
572 * there are extents left, continue to cycle.
574 } while (fiemap_buf->fm_mapped_extents == EXTENT_MAX_COUNT &&
575 !(ext->fe_flags & FIEMAP_EXTENT_LAST));
577 ext2fs_free_mem(&fiemap_buf);
580 #endif /* FS_IOC_FIEMAP */
582 static errcode_t copy_file(ext2_filsys fs, int fd, struct stat *statbuf,
586 char *buf = NULL, *zerobuf = NULL;
587 errcode_t err, close_err;
589 err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &e2_file);
593 err = ext2fs_get_mem(COPY_FILE_BUFLEN, &buf);
597 err = ext2fs_get_memzero(fs->blocksize, &zerobuf);
601 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
602 err = try_lseek_copy(fs, fd, statbuf, e2_file, buf, zerobuf);
603 if (err != EXT2_ET_UNIMPLEMENTED)
607 #if defined(FS_IOC_FIEMAP)
608 err = try_fiemap_copy(fs, fd, e2_file, buf, zerobuf);
609 if (err != EXT2_ET_UNIMPLEMENTED)
613 err = copy_file_chunk(fs, fd, e2_file, 0, statbuf->st_size, buf,
616 ext2fs_free_mem(&zerobuf);
617 ext2fs_free_mem(&buf);
618 close_err = ext2fs_file_close(e2_file);
624 static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino)
628 for (i = 0; i < hdlinks->count; i++) {
629 if (hdlinks->hdl[i].src_dev == dev &&
630 hdlinks->hdl[i].src_ino == ino)
636 /* Copy the native file to the fs */
637 errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
638 const char *dest, ext2_ino_t root)
642 ext2_ino_t newfile, parent_ino;
644 struct ext2_inode inode;
648 fd = ext2fs_open_file(src, O_RDONLY, 0);
651 com_err(__func__, retval, _("while opening \"%s\" to copy"),
655 if (fstat(fd, &statbuf) < 0) {
660 cp = strrchr(dest, '/');
663 retval = ext2fs_namei(fs, root, cwd, dest, &parent_ino);
665 com_err(dest, retval, _("while looking up \"%s\""),
673 retval = ext2fs_namei(fs, root, parent_ino, dest, &newfile);
675 retval = EXT2_ET_FILE_EXISTS;
679 retval = ext2fs_new_inode(fs, parent_ino, 010755, 0, &newfile);
683 printf("Allocated inode: %u\n", newfile);
685 retval = ext2fs_link(fs, parent_ino, dest, newfile, EXT2_FT_REG_FILE);
686 if (retval == EXT2_ET_DIR_NO_SPACE) {
687 retval = ext2fs_expand_dir(fs, parent_ino);
690 retval = ext2fs_link(fs, parent_ino, dest, newfile,
695 if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile))
696 com_err(__func__, 0, "Warning: inode already set");
697 ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
698 memset(&inode, 0, sizeof(inode));
699 inode.i_mode = (statbuf.st_mode & ~S_IFMT) | LINUX_S_IFREG;
700 now = fs->now ? fs->now : time(0);
701 ext2fs_inode_xtime_set(&inode, i_atime, now);
702 ext2fs_inode_xtime_set(&inode, i_ctime, now);
703 ext2fs_inode_xtime_set(&inode, i_mtime, now);
704 inode.i_links_count = 1;
705 retval = ext2fs_inode_size_set(fs, &inode, statbuf.st_size);
708 if (ext2fs_has_feature_inline_data(fs->super)) {
709 inode.i_flags |= EXT4_INLINE_DATA_FL;
710 } else if (ext2fs_has_feature_extents(fs->super)) {
711 ext2_extent_handle_t handle;
713 inode.i_flags &= ~EXT4_EXTENTS_FL;
714 retval = ext2fs_extent_open2(fs, newfile, &inode, &handle);
717 ext2fs_extent_free(handle);
720 retval = ext2fs_write_new_inode(fs, newfile, &inode);
723 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
724 retval = ext2fs_inline_data_init(fs, newfile);
728 if (LINUX_S_ISREG(inode.i_mode)) {
729 retval = copy_file(fs, fd, &statbuf, newfile);
738 static errcode_t path_append(struct file_info *target, const char *file)
740 if (strlen(file) + target->path_len + 1 > target->path_max_len) {
742 target->path_max_len *= 2;
743 p = realloc(target->path, target->path_max_len);
745 return EXT2_ET_NO_MEMORY;
748 target->path_len += sprintf(target->path + target->path_len, "/%s",
754 static int scandir(const char *dir_name, struct dirent ***name_list,
755 int (*filter)(const struct dirent*),
756 int (*compar)(const struct dirent**, const struct dirent**)) {
759 struct dirent **temp_list = NULL;
760 size_t temp_list_size = 0; // unit: num of dirent
763 dir = opendir(dir_name);
768 while ((dent = readdir(dir))) {
769 if (filter != NULL && !(*filter)(dent))
772 // re-allocate the list
773 if (num_dent == temp_list_size) {
774 size_t new_list_size = temp_list_size + 32;
775 struct dirent **new_list = (struct dirent**)realloc(
776 temp_list, new_list_size * sizeof(struct dirent*));
777 if (new_list == NULL)
779 temp_list_size = new_list_size;
780 temp_list = new_list;
782 // add the copy of dirent to the list
783 temp_list[num_dent] = (struct dirent*)malloc((dent->d_reclen + 3) & ~3);
784 if (!temp_list[num_dent])
786 memcpy(temp_list[num_dent], dent, dent->d_reclen);
791 if (compar != NULL) {
792 qsort(temp_list, num_dent, sizeof(struct dirent*),
793 (int (*)(const void*, const void*))compar);
795 *name_list = temp_list;
801 free(temp_list[--num_dent]);
806 static int alphasort(const struct dirent **a, const struct dirent **b) {
807 return strcoll((*a)->d_name, (*b)->d_name);
811 /* Copy files from source_dir to fs in alphabetical order */
812 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
813 const char *source_dir, ext2_ino_t root,
814 struct hdlinks_s *hdlinks,
815 struct file_info *target,
816 struct fs_ops_callbacks *fs_callbacks)
819 struct dirent **dent;
821 unsigned int save_inode;
823 errcode_t retval = 0;
825 size_t cur_dir_path_len;
828 if (chdir(source_dir) < 0) {
830 com_err(__func__, retval,
831 _("while changing working directory to \"%s\""),
836 num_dents = scandir(".", &dent, NULL, alphasort);
840 com_err(__func__, retval,
841 _("while scanning directory \"%s\""), source_dir);
845 for (i = 0; i < num_dents; free(dent[i]), i++) {
846 name = dent[i]->d_name;
847 if ((!strcmp(name, ".")) || (!strcmp(name, "..")))
849 if (lstat(name, &st)) {
851 com_err(__func__, retval, _("while lstat \"%s\""),
856 /* Check for hardlinks */
858 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
860 hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino);
862 retval = add_link(fs, parent_ino,
863 hdlinks->hdl[hdlink].dst_ino,
866 com_err(__func__, retval,
867 "while linking %s", name);
875 cur_dir_path_len = target->path_len;
876 retval = path_append(target, name);
878 com_err(__func__, retval,
879 "while appending %s", name);
883 if (fs_callbacks && fs_callbacks->create_new_inode) {
884 retval = fs_callbacks->create_new_inode(fs,
885 target->path, name, parent_ino, root,
886 st.st_mode & S_IFMT);
891 switch(st.st_mode & S_IFMT) {
897 retval = do_mknod_internal(fs, parent_ino, name,
898 st.st_mode, st.st_rdev);
900 com_err(__func__, retval,
901 _("while creating special file "
910 ln_target = malloc(st.st_size + 1);
911 if (ln_target == NULL) {
912 com_err(__func__, retval,
916 read_cnt = readlink(name, ln_target,
918 if (read_cnt == -1) {
920 com_err(__func__, retval,
921 _("while trying to read link \"%s\""),
926 if (read_cnt > st.st_size) {
927 com_err(__func__, retval,
928 _("symlink increased in size "
929 "between lstat() and readlink()"));
933 ln_target[read_cnt] = '\0';
934 retval = do_symlink_internal(fs, parent_ino, name,
938 com_err(__func__, retval,
939 _("while writing symlink\"%s\""),
947 retval = do_write_internal(fs, parent_ino, name, name,
950 com_err(__func__, retval,
951 _("while writing file \"%s\""), name);
956 /* Don't choke on /lost+found */
957 if (parent_ino == EXT2_ROOT_INO &&
958 strcmp(name, "lost+found") == 0)
960 retval = do_mkdir_internal(fs, parent_ino, name,
963 com_err(__func__, retval,
964 _("while making dir \"%s\""), name);
968 retval = ext2fs_namei(fs, root, parent_ino,
971 com_err(name, retval, 0);
974 /* Populate the dir recursively*/
975 retval = __populate_fs(fs, ino, name, root, hdlinks,
976 target, fs_callbacks);
981 com_err(__func__, retval,
982 _("while changing directory"));
988 _("ignoring entry \"%s\""), name);
991 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
993 com_err(name, retval, _("while looking up \"%s\""),
998 retval = set_inode_extra(fs, ino, &st);
1000 com_err(__func__, retval,
1001 _("while setting inode for \"%s\""), name);
1005 retval = set_inode_xattr(fs, ino, name);
1007 com_err(__func__, retval,
1008 _("while setting xattrs for \"%s\""), name);
1012 if (fs_callbacks && fs_callbacks->end_create_new_inode) {
1013 retval = fs_callbacks->end_create_new_inode(fs,
1014 target->path, name, parent_ino, root,
1015 st.st_mode & S_IFMT);
1020 /* Save the hardlink ino */
1023 * Check whether need more memory, and we don't need
1024 * free() since the lifespan will be over after the fs
1027 if (hdlinks->count == hdlinks->size) {
1028 void *p = realloc(hdlinks->hdl,
1029 (hdlinks->size + HDLINK_CNT) *
1030 sizeof(struct hdlink_s));
1032 retval = EXT2_ET_NO_MEMORY;
1033 com_err(name, retval,
1034 _("while saving inode data"));
1038 hdlinks->size += HDLINK_CNT;
1040 hdlinks->hdl[hdlinks->count].src_dev = st.st_dev;
1041 hdlinks->hdl[hdlinks->count].src_ino = st.st_ino;
1042 hdlinks->hdl[hdlinks->count].dst_ino = ino;
1045 target->path_len = cur_dir_path_len;
1046 target->path[target->path_len] = 0;
1050 for (; i < num_dents; free(dent[i]), i++);
1055 errcode_t populate_fs2(ext2_filsys fs, ext2_ino_t parent_ino,
1056 const char *source, ext2_ino_t root,
1057 struct fs_ops_callbacks *fs_callbacks)
1059 struct file_info file_info;
1060 struct hdlinks_s hdlinks;
1063 if (!(fs->flags & EXT2_FLAG_RW)) {
1064 com_err(__func__, 0, "Filesystem opened readonly");
1069 hdlinks.size = HDLINK_CNT;
1070 hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s));
1071 if (hdlinks.hdl == NULL) {
1073 com_err(__func__, retval, _("while allocating memory"));
1077 file_info.path_len = 0;
1078 file_info.path_max_len = 255;
1079 file_info.path = calloc(file_info.path_max_len, 1);
1081 /* interpret input as tarball either if it's "-" (stdin) or if it's
1082 * a regular file (or a symlink pointing to a regular file)
1084 if (strcmp(source, "-") == 0) {
1085 retval = __populate_fs_from_tar(fs, parent_ino, NULL, root, &hdlinks,
1086 &file_info, fs_callbacks);
1091 if (stat(source, &st)) {
1093 com_err(__func__, retval, _("while calling stat"));
1096 if (S_ISREG(st.st_mode)) {
1097 retval = __populate_fs_from_tar(fs, parent_ino, source, root, &hdlinks,
1098 &file_info, fs_callbacks);
1102 retval = set_inode_xattr(fs, root, source);
1104 com_err(__func__, retval,
1105 _("while copying xattrs on root directory"));
1109 retval = __populate_fs(fs, parent_ino, source, root, &hdlinks,
1110 &file_info, fs_callbacks);
1113 free(file_info.path);
1118 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
1119 const char *source, ext2_ino_t root)
1121 return populate_fs2(fs, parent_ino, source, root, NULL);