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);
153 if (errno
== ENOTSUP
)
156 com_err(__func__
, retval
, _("while listing attributes of \"%s\""),
159 } else if (size
== 0) {
163 retval
= ext2fs_xattrs_open(fs
, ino
, &handle
);
165 if (retval
== EXT2_ET_MISSING_EA_FEATURE
)
167 com_err(__func__
, retval
, _("while opening inode %u"), ino
);
171 retval
= ext2fs_xattrs_read(handle
);
173 com_err(__func__
, retval
,
174 _("while reading xattrs for inode %u"), ino
);
178 retval
= ext2fs_get_mem(size
, &list
);
180 com_err(__func__
, retval
, _("while allocating memory"));
184 size
= llistxattr(filename
, list
, size
);
187 com_err(__func__
, retval
, _("while listing attributes of \"%s\""),
192 for (i
= 0; i
< size
; i
+= strlen(&list
[i
]) + 1) {
193 const char *name
= &list
[i
];
196 value_size
= lgetxattr(filename
, name
, NULL
, 0);
197 if (value_size
== -1) {
199 com_err(__func__
, retval
,
200 _("while reading attribute \"%s\" of \"%s\""),
205 retval
= ext2fs_get_mem(value_size
, &value
);
207 com_err(__func__
, retval
, _("while allocating memory"));
211 value_size
= lgetxattr(filename
, name
, value
, value_size
);
212 if (value_size
== -1) {
213 ext2fs_free_mem(&value
);
215 com_err(__func__
, retval
,
216 _("while reading attribute \"%s\" of \"%s\""),
221 retval
= ext2fs_xattr_set(handle
, name
, value
, value_size
);
222 ext2fs_free_mem(&value
);
224 com_err(__func__
, retval
,
225 _("while writing attribute \"%s\" to inode %u"),
232 ext2fs_free_mem(&list
);
233 close_retval
= ext2fs_xattrs_close(&handle
);
235 com_err(__func__
, retval
, _("while closing inode %u"), ino
);
236 retval
= retval
? retval
: close_retval
;
240 #else /* HAVE_LLISTXATTR */
241 static errcode_t
set_inode_xattr(ext2_filsys fs
EXT2FS_ATTR((unused
)),
242 ext2_ino_t ino
EXT2FS_ATTR((unused
)),
243 const char *filename
EXT2FS_ATTR((unused
)))
247 #endif /* HAVE_LLISTXATTR */
250 /* Make a special files (block and character devices), fifo's, and sockets */
251 errcode_t
do_mknod_internal(ext2_filsys fs
, ext2_ino_t cwd
, const char *name
,
252 unsigned int st_mode
, unsigned int st_rdev
)
256 struct ext2_inode inode
;
257 unsigned long devmajor
, devminor
, mode
;
260 switch(st_mode
& S_IFMT
) {
262 mode
= LINUX_S_IFCHR
;
263 filetype
= EXT2_FT_CHRDEV
;
266 mode
= LINUX_S_IFBLK
;
267 filetype
= EXT2_FT_BLKDEV
;
270 mode
= LINUX_S_IFIFO
;
271 filetype
= EXT2_FT_FIFO
;
275 mode
= LINUX_S_IFSOCK
;
276 filetype
= EXT2_FT_SOCK
;
280 return EXT2_ET_INVALID_ARGUMENT
;
283 retval
= ext2fs_new_inode(fs
, cwd
, 010755, 0, &ino
);
285 com_err(__func__
, retval
, _("while allocating inode \"%s\""),
291 printf("Allocated inode: %u\n", ino
);
293 retval
= ext2fs_link(fs
, cwd
, name
, ino
, filetype
);
294 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
295 retval
= ext2fs_expand_dir(fs
, cwd
);
297 com_err(__func__
, retval
,
298 _("while expanding directory"));
301 retval
= ext2fs_link(fs
, cwd
, name
, ino
, filetype
);
304 com_err(name
, retval
, _("while creating inode \"%s\""), name
);
307 if (ext2fs_test_inode_bitmap2(fs
->inode_map
, ino
))
308 com_err(__func__
, 0, "Warning: inode already set");
309 ext2fs_inode_alloc_stats2(fs
, ino
, +1, 0);
310 memset(&inode
, 0, sizeof(inode
));
312 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
313 fs
->now
? fs
->now
: time(0);
315 if (filetype
!= S_IFIFO
) {
316 devmajor
= major(st_rdev
);
317 devminor
= minor(st_rdev
);
319 if ((devmajor
< 256) && (devminor
< 256)) {
320 inode
.i_block
[0] = devmajor
* 256 + devminor
;
321 inode
.i_block
[1] = 0;
323 inode
.i_block
[0] = 0;
324 inode
.i_block
[1] = (devminor
& 0xff) | (devmajor
<< 8) |
325 ((devminor
& ~0xff) << 12);
328 inode
.i_links_count
= 1;
330 retval
= ext2fs_write_new_inode(fs
, ino
, &inode
);
332 com_err(__func__
, retval
, _("while writing inode %u"), ino
);
338 /* Make a symlink name -> target */
339 errcode_t
do_symlink_internal(ext2_filsys fs
, ext2_ino_t cwd
, const char *name
,
340 char *target
, ext2_ino_t root
)
343 ext2_ino_t parent_ino
;
346 cp
= strrchr(name
, '/');
349 retval
= ext2fs_namei(fs
, root
, cwd
, name
, &parent_ino
);
351 com_err(name
, retval
, 0);
358 retval
= ext2fs_symlink(fs
, parent_ino
, 0, name
, target
);
359 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
360 retval
= ext2fs_expand_dir(fs
, parent_ino
);
362 com_err("do_symlink_internal", retval
,
363 _("while expanding directory"));
366 retval
= ext2fs_symlink(fs
, parent_ino
, 0, name
, target
);
369 com_err("ext2fs_symlink", retval
,
370 _("while creating symlink \"%s\""), name
);
374 /* Make a directory in the fs */
375 errcode_t
do_mkdir_internal(ext2_filsys fs
, ext2_ino_t cwd
, const char *name
,
379 ext2_ino_t parent_ino
;
383 cp
= strrchr(name
, '/');
386 retval
= ext2fs_namei(fs
, root
, cwd
, name
, &parent_ino
);
388 com_err(name
, retval
, _("while looking up \"%s\""),
396 retval
= ext2fs_mkdir(fs
, parent_ino
, 0, name
);
397 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
398 retval
= ext2fs_expand_dir(fs
, parent_ino
);
400 com_err(__func__
, retval
,
401 _("while expanding directory"));
404 retval
= ext2fs_mkdir(fs
, parent_ino
, 0, name
);
407 com_err("ext2fs_mkdir", retval
,
408 _("while creating directory \"%s\""), name
);
412 #if !defined HAVE_PREAD64 && !defined HAVE_PREAD
413 static ssize_t
my_pread(int fd
, void *buf
, size_t count
, off_t offset
)
415 if (lseek(fd
, offset
, SEEK_SET
) < 0)
418 return read(fd
, buf
, count
);
420 #endif /* !defined HAVE_PREAD64 && !defined HAVE_PREAD */
422 static errcode_t
copy_file_chunk(ext2_filsys fs
, int fd
, ext2_file_t e2_file
,
423 off_t start
, off_t end
, char *buf
,
428 unsigned int written
;
432 for (off
= start
; off
< end
; off
+= COPY_FILE_BUFLEN
) {
434 got
= pread64(fd
, buf
, COPY_FILE_BUFLEN
, off
);
436 got
= pread(fd
, buf
, COPY_FILE_BUFLEN
, off
);
438 got
= my_pread(fd
, buf
, COPY_FILE_BUFLEN
, off
);
444 for (bpos
= 0, ptr
= buf
; bpos
< got
; bpos
+= fs
->blocksize
) {
445 blen
= fs
->blocksize
;
446 if (blen
> got
- bpos
)
448 if (memcmp(ptr
, zerobuf
, blen
) == 0) {
452 err
= ext2fs_file_llseek(e2_file
, off
+ bpos
,
453 EXT2_SEEK_SET
, NULL
);
457 err
= ext2fs_file_write(e2_file
, ptr
, blen
,
474 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
475 static errcode_t
try_lseek_copy(ext2_filsys fs
, int fd
, struct stat
*statbuf
,
476 ext2_file_t e2_file
, char *buf
, char *zerobuf
)
478 off_t data
= 0, hole
;
479 off_t data_blk
, hole_blk
;
482 /* Try to use SEEK_DATA and SEEK_HOLE */
483 while (data
< statbuf
->st_size
) {
484 data
= lseek(fd
, data
, SEEK_DATA
);
488 return EXT2_ET_UNIMPLEMENTED
;
490 hole
= lseek(fd
, data
, SEEK_HOLE
);
492 return EXT2_ET_UNIMPLEMENTED
;
494 data_blk
= data
& ~(off_t
)(fs
->blocksize
- 1);
495 hole_blk
= ((hole
+ (off_t
)(fs
->blocksize
- 1)) &
496 ~(off_t
)(fs
->blocksize
- 1));
497 err
= copy_file_chunk(fs
, fd
, e2_file
, data_blk
, hole_blk
, buf
,
507 #endif /* SEEK_DATA and SEEK_HOLE */
509 #if defined(FS_IOC_FIEMAP)
510 static errcode_t
try_fiemap_copy(ext2_filsys fs
, int fd
, ext2_file_t e2_file
,
511 char *buf
, char *zerobuf
)
513 #define EXTENT_MAX_COUNT 512
514 struct fiemap
*fiemap_buf
;
515 struct fiemap_extent
*ext_buf
, *ext
;
516 int ext_buf_size
, fie_buf_size
;
521 ext_buf_size
= EXTENT_MAX_COUNT
* sizeof(struct fiemap_extent
);
522 fie_buf_size
= sizeof(struct fiemap
) + ext_buf_size
;
524 err
= ext2fs_get_memzero(fie_buf_size
, &fiemap_buf
);
528 ext_buf
= fiemap_buf
->fm_extents
;
529 memset(fiemap_buf
, 0, fie_buf_size
);
530 fiemap_buf
->fm_length
= FIEMAP_MAX_OFFSET
;
531 fiemap_buf
->fm_flags
|= FIEMAP_FLAG_SYNC
;
532 fiemap_buf
->fm_extent_count
= EXTENT_MAX_COUNT
;
535 fiemap_buf
->fm_start
= pos
;
536 memset(ext_buf
, 0, ext_buf_size
);
537 err
= ioctl(fd
, FS_IOC_FIEMAP
, fiemap_buf
);
538 if (err
< 0 && (errno
== EOPNOTSUPP
|| errno
== ENOTTY
)) {
539 err
= EXT2_ET_UNIMPLEMENTED
;
541 } else if (err
< 0) {
544 } else if (fiemap_buf
->fm_mapped_extents
== 0)
546 for (i
= 0, ext
= ext_buf
; i
< fiemap_buf
->fm_mapped_extents
;
548 err
= copy_file_chunk(fs
, fd
, e2_file
, ext
->fe_logical
,
549 ext
->fe_logical
+ ext
->fe_length
,
556 /* Record file's logical offset this time */
557 pos
= ext
->fe_logical
+ ext
->fe_length
;
559 * If fm_extents array has been filled and
560 * there are extents left, continue to cycle.
562 } while (fiemap_buf
->fm_mapped_extents
== EXTENT_MAX_COUNT
&&
563 !(ext
->fe_flags
& FIEMAP_EXTENT_LAST
));
565 ext2fs_free_mem(&fiemap_buf
);
568 #endif /* FS_IOC_FIEMAP */
570 static errcode_t
copy_file(ext2_filsys fs
, int fd
, struct stat
*statbuf
,
574 char *buf
= NULL
, *zerobuf
= NULL
;
575 errcode_t err
, close_err
;
577 err
= ext2fs_file_open(fs
, ino
, EXT2_FILE_WRITE
, &e2_file
);
581 err
= ext2fs_get_mem(COPY_FILE_BUFLEN
, &buf
);
585 err
= ext2fs_get_memzero(fs
->blocksize
, &zerobuf
);
589 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
590 err
= try_lseek_copy(fs
, fd
, statbuf
, e2_file
, buf
, zerobuf
);
591 if (err
!= EXT2_ET_UNIMPLEMENTED
)
595 #if defined(FS_IOC_FIEMAP)
596 err
= try_fiemap_copy(fs
, fd
, e2_file
, buf
, zerobuf
);
597 if (err
!= EXT2_ET_UNIMPLEMENTED
)
601 err
= copy_file_chunk(fs
, fd
, e2_file
, 0, statbuf
->st_size
, buf
,
604 ext2fs_free_mem(&zerobuf
);
605 ext2fs_free_mem(&buf
);
606 close_err
= ext2fs_file_close(e2_file
);
612 static int is_hardlink(struct hdlinks_s
*hdlinks
, dev_t dev
, ino_t ino
)
616 for (i
= 0; i
< hdlinks
->count
; i
++) {
617 if (hdlinks
->hdl
[i
].src_dev
== dev
&&
618 hdlinks
->hdl
[i
].src_ino
== ino
)
624 /* Copy the native file to the fs */
625 errcode_t
do_write_internal(ext2_filsys fs
, ext2_ino_t cwd
, const char *src
,
626 const char *dest
, ext2_ino_t root
)
630 ext2_ino_t newfile
, parent_ino
;
632 struct ext2_inode inode
;
635 fd
= ext2fs_open_file(src
, O_RDONLY
, 0);
638 com_err(__func__
, retval
, _("while opening \"%s\" to copy"),
642 if (fstat(fd
, &statbuf
) < 0) {
647 cp
= strrchr(dest
, '/');
650 retval
= ext2fs_namei(fs
, root
, cwd
, dest
, &parent_ino
);
652 com_err(dest
, retval
, _("while looking up \"%s\""),
660 retval
= ext2fs_namei(fs
, root
, parent_ino
, dest
, &newfile
);
662 retval
= EXT2_ET_FILE_EXISTS
;
666 retval
= ext2fs_new_inode(fs
, parent_ino
, 010755, 0, &newfile
);
670 printf("Allocated inode: %u\n", newfile
);
672 retval
= ext2fs_link(fs
, parent_ino
, dest
, newfile
, EXT2_FT_REG_FILE
);
673 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
674 retval
= ext2fs_expand_dir(fs
, parent_ino
);
677 retval
= ext2fs_link(fs
, parent_ino
, dest
, newfile
,
682 if (ext2fs_test_inode_bitmap2(fs
->inode_map
, newfile
))
683 com_err(__func__
, 0, "Warning: inode already set");
684 ext2fs_inode_alloc_stats2(fs
, newfile
, +1, 0);
685 memset(&inode
, 0, sizeof(inode
));
686 inode
.i_mode
= (statbuf
.st_mode
& ~S_IFMT
) | LINUX_S_IFREG
;
687 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
688 fs
->now
? fs
->now
: time(0);
689 inode
.i_links_count
= 1;
690 retval
= ext2fs_inode_size_set(fs
, &inode
, statbuf
.st_size
);
693 if (ext2fs_has_feature_inline_data(fs
->super
)) {
694 inode
.i_flags
|= EXT4_INLINE_DATA_FL
;
695 } else if (ext2fs_has_feature_extents(fs
->super
)) {
696 ext2_extent_handle_t handle
;
698 inode
.i_flags
&= ~EXT4_EXTENTS_FL
;
699 retval
= ext2fs_extent_open2(fs
, newfile
, &inode
, &handle
);
702 ext2fs_extent_free(handle
);
705 retval
= ext2fs_write_new_inode(fs
, newfile
, &inode
);
708 if (inode
.i_flags
& EXT4_INLINE_DATA_FL
) {
709 retval
= ext2fs_inline_data_init(fs
, newfile
);
713 if (LINUX_S_ISREG(inode
.i_mode
)) {
714 retval
= copy_file(fs
, fd
, &statbuf
, newfile
);
729 static errcode_t
path_append(struct file_info
*target
, const char *file
)
731 if (strlen(file
) + target
->path_len
+ 1 > target
->path_max_len
) {
733 target
->path_max_len
*= 2;
734 p
= realloc(target
->path
, target
->path_max_len
);
736 return EXT2_ET_NO_MEMORY
;
739 target
->path_len
+= sprintf(target
->path
+ target
->path_len
, "/%s",
745 static int scandir(const char *dir_name
, struct dirent
***name_list
,
746 int (*filter
)(const struct dirent
*),
747 int (*compar
)(const struct dirent
**, const struct dirent
**)) {
750 struct dirent
**temp_list
= NULL
;
751 size_t temp_list_size
= 0; // unit: num of dirent
754 dir
= opendir(dir_name
);
759 while ((dent
= readdir(dir
))) {
760 if (filter
!= NULL
&& !(*filter
)(dent
))
763 // re-allocate the list
764 if (num_dent
== temp_list_size
) {
765 size_t new_list_size
= temp_list_size
+ 32;
766 struct dirent
**new_list
= (struct dirent
**)realloc(
767 temp_list
, new_list_size
* sizeof(struct dirent
*));
768 if (new_list
== NULL
)
770 temp_list_size
= new_list_size
;
771 temp_list
= new_list
;
773 // add the copy of dirent to the list
774 temp_list
[num_dent
] = (struct dirent
*)malloc((dent
->d_reclen
+ 3) & ~3);
775 if (!temp_list
[num_dent
])
777 memcpy(temp_list
[num_dent
], dent
, dent
->d_reclen
);
782 if (compar
!= NULL
) {
783 qsort(temp_list
, num_dent
, sizeof(struct dirent
*),
784 (int (*)(const void*, const void*))compar
);
786 *name_list
= temp_list
;
792 free(temp_list
[--num_dent
]);
797 static int alphasort(const struct dirent
**a
, const struct dirent
**b
) {
798 return strcoll((*a
)->d_name
, (*b
)->d_name
);
802 /* Copy files from source_dir to fs in alphabetical order */
803 static errcode_t
__populate_fs(ext2_filsys fs
, ext2_ino_t parent_ino
,
804 const char *source_dir
, ext2_ino_t root
,
805 struct hdlinks_s
*hdlinks
,
806 struct file_info
*target
,
807 struct fs_ops_callbacks
*fs_callbacks
)
810 struct dirent
**dent
;
812 unsigned int save_inode
;
814 errcode_t retval
= 0;
816 size_t cur_dir_path_len
;
819 if (chdir(source_dir
) < 0) {
821 com_err(__func__
, retval
,
822 _("while changing working directory to \"%s\""),
827 num_dents
= scandir(".", &dent
, NULL
, alphasort
);
831 com_err(__func__
, retval
,
832 _("while scanning directory \"%s\""), source_dir
);
836 for (i
= 0; i
< num_dents
; free(dent
[i
]), i
++) {
837 name
= dent
[i
]->d_name
;
838 if ((!strcmp(name
, ".")) || (!strcmp(name
, "..")))
840 if (lstat(name
, &st
)) {
842 com_err(__func__
, retval
, _("while lstat \"%s\""),
847 /* Check for hardlinks */
849 if (!S_ISDIR(st
.st_mode
) && !S_ISLNK(st
.st_mode
) &&
851 hdlink
= is_hardlink(hdlinks
, st
.st_dev
, st
.st_ino
);
853 retval
= add_link(fs
, parent_ino
,
854 hdlinks
->hdl
[hdlink
].dst_ino
,
857 com_err(__func__
, retval
,
858 "while linking %s", name
);
866 cur_dir_path_len
= target
->path_len
;
867 retval
= path_append(target
, name
);
869 com_err(__func__
, retval
,
870 "while appending %s", name
);
874 if (fs_callbacks
&& fs_callbacks
->create_new_inode
) {
875 retval
= fs_callbacks
->create_new_inode(fs
,
876 target
->path
, name
, parent_ino
, root
,
877 st
.st_mode
& S_IFMT
);
882 switch(st
.st_mode
& S_IFMT
) {
888 retval
= do_mknod_internal(fs
, parent_ino
, name
,
889 st
.st_mode
, st
.st_rdev
);
891 com_err(__func__
, retval
,
892 _("while creating special file "
901 ln_target
= malloc(st
.st_size
+ 1);
902 if (ln_target
== NULL
) {
903 com_err(__func__
, retval
,
907 read_cnt
= readlink(name
, ln_target
,
909 if (read_cnt
== -1) {
911 com_err(__func__
, retval
,
912 _("while trying to read link \"%s\""),
917 if (read_cnt
> st
.st_size
) {
918 com_err(__func__
, retval
,
919 _("symlink increased in size "
920 "between lstat() and readlink()"));
924 ln_target
[read_cnt
] = '\0';
925 retval
= do_symlink_internal(fs
, parent_ino
, name
,
929 com_err(__func__
, retval
,
930 _("while writing symlink\"%s\""),
938 retval
= do_write_internal(fs
, parent_ino
, name
, name
,
941 com_err(__func__
, retval
,
942 _("while writing file \"%s\""), name
);
947 /* Don't choke on /lost+found */
948 if (parent_ino
== EXT2_ROOT_INO
&&
949 strcmp(name
, "lost+found") == 0)
951 retval
= do_mkdir_internal(fs
, parent_ino
, name
,
954 com_err(__func__
, retval
,
955 _("while making dir \"%s\""), name
);
959 retval
= ext2fs_namei(fs
, root
, parent_ino
,
962 com_err(name
, retval
, 0);
965 /* Populate the dir recursively*/
966 retval
= __populate_fs(fs
, ino
, name
, root
, hdlinks
,
967 target
, fs_callbacks
);
972 com_err(__func__
, retval
,
973 _("while changing directory"));
979 _("ignoring entry \"%s\""), name
);
982 retval
= ext2fs_namei(fs
, root
, parent_ino
, name
, &ino
);
984 com_err(name
, retval
, _("while looking up \"%s\""),
989 retval
= set_inode_extra(fs
, ino
, &st
);
991 com_err(__func__
, retval
,
992 _("while setting inode for \"%s\""), name
);
996 retval
= set_inode_xattr(fs
, ino
, name
);
998 com_err(__func__
, retval
,
999 _("while setting xattrs for \"%s\""), name
);
1003 if (fs_callbacks
&& fs_callbacks
->end_create_new_inode
) {
1004 retval
= fs_callbacks
->end_create_new_inode(fs
,
1005 target
->path
, name
, parent_ino
, root
,
1006 st
.st_mode
& S_IFMT
);
1011 /* Save the hardlink ino */
1014 * Check whether need more memory, and we don't need
1015 * free() since the lifespan will be over after the fs
1018 if (hdlinks
->count
== hdlinks
->size
) {
1019 void *p
= realloc(hdlinks
->hdl
,
1020 (hdlinks
->size
+ HDLINK_CNT
) *
1021 sizeof(struct hdlink_s
));
1023 retval
= EXT2_ET_NO_MEMORY
;
1024 com_err(name
, retval
,
1025 _("while saving inode data"));
1029 hdlinks
->size
+= HDLINK_CNT
;
1031 hdlinks
->hdl
[hdlinks
->count
].src_dev
= st
.st_dev
;
1032 hdlinks
->hdl
[hdlinks
->count
].src_ino
= st
.st_ino
;
1033 hdlinks
->hdl
[hdlinks
->count
].dst_ino
= ino
;
1036 target
->path_len
= cur_dir_path_len
;
1037 target
->path
[target
->path_len
] = 0;
1041 for (; i
< num_dents
; free(dent
[i
]), i
++);
1046 errcode_t
populate_fs2(ext2_filsys fs
, ext2_ino_t parent_ino
,
1047 const char *source_dir
, ext2_ino_t root
,
1048 struct fs_ops_callbacks
*fs_callbacks
)
1050 struct file_info file_info
;
1051 struct hdlinks_s hdlinks
;
1054 if (!(fs
->flags
& EXT2_FLAG_RW
)) {
1055 com_err(__func__
, 0, "Filesystem opened readonly");
1060 hdlinks
.size
= HDLINK_CNT
;
1061 hdlinks
.hdl
= realloc(NULL
, hdlinks
.size
* sizeof(struct hdlink_s
));
1062 if (hdlinks
.hdl
== NULL
) {
1064 com_err(__func__
, retval
, _("while allocating memory"));
1068 file_info
.path_len
= 0;
1069 file_info
.path_max_len
= 255;
1070 file_info
.path
= calloc(file_info
.path_max_len
, 1);
1072 retval
= set_inode_xattr(fs
, root
, source_dir
);
1074 com_err(__func__
, retval
,
1075 _("while copying xattrs on root directory"));
1079 retval
= __populate_fs(fs
, parent_ino
, source_dir
, root
, &hdlinks
,
1080 &file_info
, fs_callbacks
);
1083 free(file_info
.path
);
1088 errcode_t
populate_fs(ext2_filsys fs
, ext2_ino_t parent_ino
,
1089 const char *source_dir
, ext2_ino_t root
)
1091 return populate_fs2(fs
, parent_ino
, source_dir
, root
, NULL
);