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 /* 64KiB is the minimum blksize to best minimize system call overhead. */
43 #define COPY_FILE_BUFLEN 65536
45 static int ext2_file_type(unsigned int mode)
47 if (LINUX_S_ISREG(mode))
48 return EXT2_FT_REG_FILE;
50 if (LINUX_S_ISDIR(mode))
53 if (LINUX_S_ISCHR(mode))
54 return EXT2_FT_CHRDEV;
56 if (LINUX_S_ISBLK(mode))
57 return EXT2_FT_BLKDEV;
59 if (LINUX_S_ISLNK(mode))
60 return EXT2_FT_SYMLINK;
62 if (LINUX_S_ISFIFO(mode))
65 if (LINUX_S_ISSOCK(mode))
71 /* Link an inode number to a directory */
72 static errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino,
73 ext2_ino_t ino, const char *name)
75 struct ext2_inode inode;
78 retval = ext2fs_read_inode(fs, ino, &inode);
80 com_err(__func__, retval, _("while reading inode %u"), ino);
84 retval = ext2fs_link(fs, parent_ino, name, ino,
85 ext2_file_type(inode.i_mode));
86 if (retval == EXT2_ET_DIR_NO_SPACE) {
87 retval = ext2fs_expand_dir(fs, parent_ino);
89 com_err(__func__, retval,
90 _("while expanding directory"));
93 retval = ext2fs_link(fs, parent_ino, name, ino,
94 ext2_file_type(inode.i_mode));
97 com_err(__func__, retval, _("while linking \"%s\""), name);
101 inode.i_links_count++;
103 retval = ext2fs_write_inode(fs, ino, &inode);
105 com_err(__func__, retval, _("while writing inode %u"), ino);
110 /* Set the uid, gid, mode and time for the inode */
111 static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t ino,
115 struct ext2_inode inode;
117 retval = ext2fs_read_inode(fs, ino, &inode);
119 com_err(__func__, retval, _("while reading inode %u"), ino);
123 inode.i_uid = st->st_uid;
124 ext2fs_set_i_uid_high(inode, st->st_uid >> 16);
125 inode.i_gid = st->st_gid;
126 ext2fs_set_i_gid_high(inode, st->st_gid >> 16);
127 inode.i_mode = (LINUX_S_IFMT & inode.i_mode) | (~S_IFMT & st->st_mode);
128 inode.i_atime = st->st_atime;
129 inode.i_mtime = st->st_mtime;
130 inode.i_ctime = st->st_ctime;
132 retval = ext2fs_write_inode(fs, ino, &inode);
134 com_err(__func__, retval, _("while writing inode %u"), ino);
138 #ifdef HAVE_LLISTXATTR
139 static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino,
140 const char *filename)
142 errcode_t retval, close_retval;
143 struct ext2_xattr_handle *handle;
144 ssize_t size, value_size;
151 size = llistxattr(filename, NULL, 0);
154 com_err(__func__, retval, _("while listing attributes of \"%s\""),
157 } else if (size == 0) {
161 retval = ext2fs_xattrs_open(fs, ino, &handle);
163 if (retval == EXT2_ET_MISSING_EA_FEATURE)
165 com_err(__func__, retval, _("while opening inode %u"), ino);
169 retval = ext2fs_xattrs_read(handle);
171 com_err(__func__, retval,
172 _("while reading xattrs for inode %u"), ino);
176 retval = ext2fs_get_mem(size, &list);
178 com_err(__func__, retval, _("while allocating memory"));
182 size = llistxattr(filename, list, size);
185 com_err(__func__, retval, _("while listing attributes of \"%s\""),
190 for (i = 0; i < size; i += strlen(&list[i]) + 1) {
191 const char *name = &list[i];
194 value_size = lgetxattr(filename, name, NULL, 0);
195 if (value_size == -1) {
197 com_err(__func__, retval,
198 _("while reading attribute \"%s\" of \"%s\""),
203 retval = ext2fs_get_mem(value_size, &value);
205 com_err(__func__, retval, _("while allocating memory"));
209 value_size = lgetxattr(filename, name, value, value_size);
210 if (value_size == -1) {
211 ext2fs_free_mem(&value);
213 com_err(__func__, retval,
214 _("while reading attribute \"%s\" of \"%s\""),
219 retval = ext2fs_xattr_set(handle, name, value, value_size);
220 ext2fs_free_mem(&value);
222 com_err(__func__, retval,
223 _("while writing attribute \"%s\" to inode %u"),
230 ext2fs_free_mem(&list);
231 close_retval = ext2fs_xattrs_close(&handle);
233 com_err(__func__, retval, _("while closing inode %u"), ino);
234 retval = retval ? retval : close_retval;
239 #else /* HAVE_LLISTXATTR */
240 static errcode_t set_inode_xattr(ext2_filsys fs EXT2FS_ATTR((unused)),
241 ext2_ino_t ino EXT2FS_ATTR((unused)),
242 const char *filename EXT2FS_ATTR((unused)))
246 #endif /* HAVE_LLISTXATTR */
249 /* Make a special files (block and character devices), fifo's, and sockets */
250 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
251 unsigned int st_mode, unsigned int st_rdev)
255 struct ext2_inode inode;
256 unsigned long devmajor, devminor, mode;
259 switch(st_mode & S_IFMT) {
261 mode = LINUX_S_IFCHR;
262 filetype = EXT2_FT_CHRDEV;
265 mode = LINUX_S_IFBLK;
266 filetype = EXT2_FT_BLKDEV;
269 mode = LINUX_S_IFIFO;
270 filetype = EXT2_FT_FIFO;
274 mode = LINUX_S_IFSOCK;
275 filetype = EXT2_FT_SOCK;
279 return EXT2_ET_INVALID_ARGUMENT;
282 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
284 com_err(__func__, retval, _("while allocating inode \"%s\""),
290 printf("Allocated inode: %u\n", ino);
292 retval = ext2fs_link(fs, cwd, name, ino, filetype);
293 if (retval == EXT2_ET_DIR_NO_SPACE) {
294 retval = ext2fs_expand_dir(fs, cwd);
296 com_err(__func__, retval,
297 _("while expanding directory"));
300 retval = ext2fs_link(fs, cwd, name, ino, filetype);
303 com_err(name, retval, _("while creating inode \"%s\""), name);
306 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino))
307 com_err(__func__, 0, "Warning: inode already set");
308 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
309 memset(&inode, 0, sizeof(inode));
311 inode.i_atime = inode.i_ctime = inode.i_mtime =
312 fs->now ? fs->now : time(0);
314 if (filetype != S_IFIFO) {
315 devmajor = major(st_rdev);
316 devminor = minor(st_rdev);
318 if ((devmajor < 256) && (devminor < 256)) {
319 inode.i_block[0] = devmajor * 256 + devminor;
320 inode.i_block[1] = 0;
322 inode.i_block[0] = 0;
323 inode.i_block[1] = (devminor & 0xff) | (devmajor << 8) |
324 ((devminor & ~0xff) << 12);
327 inode.i_links_count = 1;
329 retval = ext2fs_write_new_inode(fs, ino, &inode);
331 com_err(__func__, retval, _("while writing inode %u"), ino);
337 /* Make a symlink name -> target */
338 errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
339 char *target, ext2_ino_t root)
342 ext2_ino_t parent_ino;
345 cp = strrchr(name, '/');
348 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
350 com_err(name, retval, 0);
357 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
358 if (retval == EXT2_ET_DIR_NO_SPACE) {
359 retval = ext2fs_expand_dir(fs, parent_ino);
361 com_err("do_symlink_internal", retval,
362 _("while expanding directory"));
365 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
368 com_err("ext2fs_symlink", retval,
369 _("while creating symlink \"%s\""), name);
373 /* Make a directory in the fs */
374 errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
378 ext2_ino_t parent_ino;
382 cp = strrchr(name, '/');
385 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
387 com_err(name, retval, _("while looking up \"%s\""),
395 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
396 if (retval == EXT2_ET_DIR_NO_SPACE) {
397 retval = ext2fs_expand_dir(fs, parent_ino);
399 com_err(__func__, retval,
400 _("while expanding directory"));
403 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
406 com_err("ext2fs_mkdir", retval,
407 _("while creating directory \"%s\""), name);
411 #if !defined HAVE_PREAD64 && !defined HAVE_PREAD
412 static ssize_t my_pread(int fd, void *buf, size_t count, off_t offset)
414 if (lseek(fd, offset, SEEK_SET) < 0)
417 return read(fd, buf, count);
419 #endif /* !defined HAVE_PREAD64 && !defined HAVE_PREAD */
421 static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
422 off_t start, off_t end, char *buf,
427 unsigned int written;
431 for (off = start; off < end; off += COPY_FILE_BUFLEN) {
433 got = pread64(fd, buf, COPY_FILE_BUFLEN, off);
435 got = pread(fd, buf, COPY_FILE_BUFLEN, off);
437 got = my_pread(fd, buf, COPY_FILE_BUFLEN, off);
443 for (bpos = 0, ptr = buf; bpos < got; bpos += fs->blocksize) {
444 blen = fs->blocksize;
445 if (blen > got - bpos)
447 if (memcmp(ptr, zerobuf, blen) == 0) {
451 err = ext2fs_file_llseek(e2_file, off + bpos,
452 EXT2_SEEK_SET, NULL);
456 err = ext2fs_file_write(e2_file, ptr, blen,
473 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
474 static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
475 ext2_file_t e2_file, char *buf, char *zerobuf)
477 off_t data = 0, hole;
478 off_t data_blk, hole_blk;
481 /* Try to use SEEK_DATA and SEEK_HOLE */
482 while (data < statbuf->st_size) {
483 data = lseek(fd, data, SEEK_DATA);
487 return EXT2_ET_UNIMPLEMENTED;
489 hole = lseek(fd, data, SEEK_HOLE);
491 return EXT2_ET_UNIMPLEMENTED;
493 data_blk = data & ~(off_t)(fs->blocksize - 1);
494 hole_blk = ((hole + (off_t)(fs->blocksize - 1)) &
495 ~(off_t)(fs->blocksize - 1));
496 err = copy_file_chunk(fs, fd, e2_file, data_blk, hole_blk, buf,
506 #endif /* SEEK_DATA and SEEK_HOLE */
508 #if defined(FS_IOC_FIEMAP)
509 static errcode_t try_fiemap_copy(ext2_filsys fs, int fd, ext2_file_t e2_file,
510 char *buf, char *zerobuf)
512 #define EXTENT_MAX_COUNT 512
513 struct fiemap *fiemap_buf;
514 struct fiemap_extent *ext_buf, *ext;
515 int ext_buf_size, fie_buf_size;
520 ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
521 fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
523 err = ext2fs_get_memzero(fie_buf_size, &fiemap_buf);
527 ext_buf = fiemap_buf->fm_extents;
528 memset(fiemap_buf, 0, fie_buf_size);
529 fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
530 fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
531 fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
534 fiemap_buf->fm_start = pos;
535 memset(ext_buf, 0, ext_buf_size);
536 err = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
537 if (err < 0 && (errno == EOPNOTSUPP || errno == ENOTTY)) {
538 err = EXT2_ET_UNIMPLEMENTED;
540 } else if (err < 0) {
543 } else if (fiemap_buf->fm_mapped_extents == 0)
545 for (i = 0, ext = ext_buf; i < fiemap_buf->fm_mapped_extents;
547 err = copy_file_chunk(fs, fd, e2_file, ext->fe_logical,
548 ext->fe_logical + ext->fe_length,
555 /* Record file's logical offset this time */
556 pos = ext->fe_logical + ext->fe_length;
558 * If fm_extents array has been filled and
559 * there are extents left, continue to cycle.
561 } while (fiemap_buf->fm_mapped_extents == EXTENT_MAX_COUNT &&
562 !(ext->fe_flags & FIEMAP_EXTENT_LAST));
564 ext2fs_free_mem(&fiemap_buf);
567 #endif /* FS_IOC_FIEMAP */
569 static errcode_t copy_file(ext2_filsys fs, int fd, struct stat *statbuf,
573 char *buf = NULL, *zerobuf = NULL;
574 errcode_t err, close_err;
576 err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &e2_file);
580 err = ext2fs_get_mem(COPY_FILE_BUFLEN, &buf);
584 err = ext2fs_get_memzero(fs->blocksize, &zerobuf);
588 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
589 err = try_lseek_copy(fs, fd, statbuf, e2_file, buf, zerobuf);
590 if (err != EXT2_ET_UNIMPLEMENTED)
594 #if defined(FS_IOC_FIEMAP)
595 err = try_fiemap_copy(fs, fd, e2_file, buf, zerobuf);
596 if (err != EXT2_ET_UNIMPLEMENTED)
600 err = copy_file_chunk(fs, fd, e2_file, 0, statbuf->st_size, buf,
603 ext2fs_free_mem(&zerobuf);
604 ext2fs_free_mem(&buf);
605 close_err = ext2fs_file_close(e2_file);
611 static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino)
615 for (i = 0; i < hdlinks->count; i++) {
616 if (hdlinks->hdl[i].src_dev == dev &&
617 hdlinks->hdl[i].src_ino == ino)
623 /* Copy the native file to the fs */
624 errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
625 const char *dest, ext2_ino_t root)
631 struct ext2_inode inode;
633 fd = ext2fs_open_file(src, O_RDONLY, 0);
636 com_err(__func__, retval, _("while opening \"%s\" to copy"),
640 if (fstat(fd, &statbuf) < 0) {
645 retval = ext2fs_namei(fs, root, cwd, dest, &newfile);
647 retval = EXT2_ET_FILE_EXISTS;
651 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile);
655 printf("Allocated inode: %u\n", newfile);
657 retval = ext2fs_link(fs, cwd, dest, newfile,
659 if (retval == EXT2_ET_DIR_NO_SPACE) {
660 retval = ext2fs_expand_dir(fs, cwd);
663 retval = ext2fs_link(fs, cwd, dest, newfile,
668 if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile))
669 com_err(__func__, 0, "Warning: inode already set");
670 ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
671 memset(&inode, 0, sizeof(inode));
672 inode.i_mode = (statbuf.st_mode & ~S_IFMT) | LINUX_S_IFREG;
673 inode.i_atime = inode.i_ctime = inode.i_mtime =
674 fs->now ? fs->now : time(0);
675 inode.i_links_count = 1;
676 retval = ext2fs_inode_size_set(fs, &inode, statbuf.st_size);
679 if (ext2fs_has_feature_inline_data(fs->super)) {
680 inode.i_flags |= EXT4_INLINE_DATA_FL;
681 } else if (ext2fs_has_feature_extents(fs->super)) {
682 ext2_extent_handle_t handle;
684 inode.i_flags &= ~EXT4_EXTENTS_FL;
685 retval = ext2fs_extent_open2(fs, newfile, &inode, &handle);
688 ext2fs_extent_free(handle);
691 retval = ext2fs_write_new_inode(fs, newfile, &inode);
694 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
695 retval = ext2fs_inline_data_init(fs, newfile);
699 if (LINUX_S_ISREG(inode.i_mode)) {
700 retval = copy_file(fs, fd, &statbuf, newfile);
715 static errcode_t path_append(struct file_info *target, const char *file)
717 if (strlen(file) + target->path_len + 1 > target->path_max_len) {
719 target->path_max_len *= 2;
720 p = realloc(target->path, target->path_max_len);
722 return EXT2_ET_NO_MEMORY;
725 target->path_len += sprintf(target->path + target->path_len, "/%s",
731 static int scandir(const char *dir_name, struct dirent ***name_list,
732 int (*filter)(const struct dirent*),
733 int (*compar)(const struct dirent**, const struct dirent**)) {
736 struct dirent **temp_list = NULL;
737 size_t temp_list_size = 0; // unit: num of dirent
740 dir = opendir(dir_name);
745 while ((dent = readdir(dir))) {
746 if (filter != NULL && !(*filter)(dent))
749 // re-allocate the list
750 if (num_dent == temp_list_size) {
751 size_t new_list_size = temp_list_size + 32;
752 struct dirent **new_list = (struct dirent**)realloc(
753 temp_list, new_list_size * sizeof(struct dirent*));
754 if (new_list == NULL) {
757 temp_list_size = new_list_size;
758 temp_list = new_list;
760 // add the copy of dirent to the list
761 temp_list[num_dent] = (struct dirent*)malloc((dent->d_reclen + 3) & ~3);
762 memcpy(temp_list[num_dent], dent, dent->d_reclen);
766 if (compar != NULL) {
767 qsort(temp_list, num_dent, sizeof(struct dirent*),
768 (int (*)(const void*, const void*))compar);
771 // release the temp list
772 *name_list = temp_list;
776 if (temp_list != NULL) {
777 while (num_dent > 0) {
778 free(temp_list[--num_dent]);
787 static int alphasort(const struct dirent **a, const struct dirent **b) {
788 return strcoll((*a)->d_name, (*b)->d_name);
792 /* Copy files from source_dir to fs in alphabetical order */
793 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
794 const char *source_dir, ext2_ino_t root,
795 struct hdlinks_s *hdlinks,
796 struct file_info *target,
797 struct fs_ops_callbacks *fs_callbacks)
800 struct dirent **dent;
802 char *ln_target = NULL;
803 unsigned int save_inode;
805 errcode_t retval = 0;
808 size_t cur_dir_path_len;
811 if (chdir(source_dir) < 0) {
813 com_err(__func__, retval,
814 _("while changing working directory to \"%s\""),
819 num_dents = scandir(".", &dent, NULL, alphasort);
823 com_err(__func__, retval,
824 _("while scanning directory \"%s\""), source_dir);
828 for (i = 0; i < num_dents; free(dent[i]), i++) {
829 name = dent[i]->d_name;
830 if ((!strcmp(name, ".")) || (!strcmp(name, "..")))
832 if (lstat(name, &st)) {
834 com_err(__func__, retval, _("while lstat \"%s\""),
839 /* Check for hardlinks */
841 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
843 hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino);
845 retval = add_link(fs, parent_ino,
846 hdlinks->hdl[hdlink].dst_ino,
849 com_err(__func__, retval,
850 "while linking %s", name);
858 cur_dir_path_len = target->path_len;
859 retval = path_append(target, name);
861 com_err(__func__, retval,
862 "while appending %s", name);
866 if (fs_callbacks && fs_callbacks->create_new_inode) {
867 retval = fs_callbacks->create_new_inode(fs,
868 target->path, name, parent_ino, root,
869 st.st_mode & S_IFMT);
874 switch(st.st_mode & S_IFMT) {
880 retval = do_mknod_internal(fs, parent_ino, name,
881 st.st_mode, st.st_rdev);
883 com_err(__func__, retval,
884 _("while creating special file "
890 ln_target = malloc(st.st_size + 1);
891 if (ln_target == NULL) {
892 com_err(__func__, retval,
896 read_cnt = readlink(name, ln_target,
898 if (read_cnt == -1) {
900 com_err(__func__, retval,
901 _("while trying to read link \"%s\""),
906 if (read_cnt > st.st_size) {
907 com_err(__func__, retval,
908 _("symlink increased in size "
909 "between lstat() and readlink()"));
913 ln_target[read_cnt] = '\0';
914 retval = do_symlink_internal(fs, parent_ino, name,
918 com_err(__func__, retval,
919 _("while writing symlink\"%s\""),
926 retval = do_write_internal(fs, parent_ino, name, name,
929 com_err(__func__, retval,
930 _("while writing file \"%s\""), name);
935 /* Don't choke on /lost+found */
936 if (parent_ino == EXT2_ROOT_INO &&
937 strcmp(name, "lost+found") == 0)
939 retval = do_mkdir_internal(fs, parent_ino, name,
942 com_err(__func__, retval,
943 _("while making dir \"%s\""), name);
947 retval = ext2fs_namei(fs, root, parent_ino,
950 com_err(name, retval, 0);
953 /* Populate the dir recursively*/
954 retval = __populate_fs(fs, ino, name, root, hdlinks,
955 target, fs_callbacks);
960 com_err(__func__, retval,
961 _("while changing directory"));
967 _("ignoring entry \"%s\""), name);
970 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
972 com_err(name, retval, _("while looking up \"%s\""),
977 retval = set_inode_extra(fs, ino, &st);
979 com_err(__func__, retval,
980 _("while setting inode for \"%s\""), name);
984 retval = set_inode_xattr(fs, ino, name);
986 com_err(__func__, retval,
987 _("while setting xattrs for \"%s\""), name);
991 if (fs_callbacks && fs_callbacks->end_create_new_inode) {
992 retval = fs_callbacks->end_create_new_inode(fs,
993 target->path, name, parent_ino, root,
994 st.st_mode & S_IFMT);
999 /* Save the hardlink ino */
1002 * Check whether need more memory, and we don't need
1003 * free() since the lifespan will be over after the fs
1006 if (hdlinks->count == hdlinks->size) {
1007 void *p = realloc(hdlinks->hdl,
1008 (hdlinks->size + HDLINK_CNT) *
1009 sizeof(struct hdlink_s));
1011 retval = EXT2_ET_NO_MEMORY;
1012 com_err(name, retval,
1013 _("while saving inode data"));
1017 hdlinks->size += HDLINK_CNT;
1019 hdlinks->hdl[hdlinks->count].src_dev = st.st_dev;
1020 hdlinks->hdl[hdlinks->count].src_ino = st.st_ino;
1021 hdlinks->hdl[hdlinks->count].dst_ino = ino;
1024 target->path_len = cur_dir_path_len;
1025 target->path[target->path_len] = 0;
1029 for (; i < num_dents; free(dent[i]), i++);
1034 errcode_t populate_fs2(ext2_filsys fs, ext2_ino_t parent_ino,
1035 const char *source_dir, ext2_ino_t root,
1036 struct fs_ops_callbacks *fs_callbacks)
1038 struct file_info file_info;
1039 struct hdlinks_s hdlinks;
1042 if (!(fs->flags & EXT2_FLAG_RW)) {
1043 com_err(__func__, 0, "Filesystem opened readonly");
1048 hdlinks.size = HDLINK_CNT;
1049 hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s));
1050 if (hdlinks.hdl == NULL) {
1052 com_err(__func__, retval, _("while allocating memory"));
1056 file_info.path_len = 0;
1057 file_info.path_max_len = 255;
1058 file_info.path = calloc(file_info.path_max_len, 1);
1060 retval = set_inode_xattr(fs, root, source_dir);
1062 com_err(__func__, retval,
1063 _("while copying xattrs on root directory"));
1067 retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks,
1068 &file_info, fs_callbacks);
1071 free(file_info.path);
1076 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
1077 const char *source_dir, ext2_ino_t root)
1079 return populate_fs2(fs, parent_ino, source_dir, root, NULL);