Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | From 08dfbaf4e3f704232ff46d78c0758a6cfe3961c8 Mon Sep 17 00:00:00 2001 |
| 2 | From: Robert Yang <liezhi.yang@windriver.com> |
| 3 | Date: Mon, 23 Dec 2013 02:59:10 -0500 |
| 4 | Subject: [PATCH 02/11] misc/create_inode.c: copy files recursively |
| 5 | |
| 6 | Use opendir() and readdir() to read the native directory, then use |
| 7 | lstat() to identify the file type and call the corresponding function to |
| 8 | add the file to the filesystem, call the populate_fs() recursively if it |
| 9 | is a directory. |
| 10 | |
| 11 | NOTE: the libext2fs can't create the socket file. |
| 12 | |
| 13 | Upstream-Status: Backport |
| 14 | |
| 15 | Signed-off-by: Robert Yang <liezhi.yang@windriver.com> |
| 16 | Reviewed-by: Darren Hart <dvhart@linux.intel.com> |
| 17 | --- |
| 18 | misc/create_inode.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 19 | 1 file changed, 97 insertions(+) |
| 20 | |
| 21 | diff --git a/misc/create_inode.c b/misc/create_inode.c |
| 22 | index 46aaa60..b68b910 100644 |
| 23 | --- a/misc/create_inode.c |
| 24 | +++ b/misc/create_inode.c |
| 25 | @@ -1,5 +1,13 @@ |
| 26 | #include "create_inode.h" |
| 27 | |
| 28 | +#if __STDC_VERSION__ < 199901L |
| 29 | +# if __GNUC__ >= 2 |
| 30 | +# define __func__ __FUNCTION__ |
| 31 | +# else |
| 32 | +# define __func__ "<unknown>" |
| 33 | +# endif |
| 34 | +#endif |
| 35 | + |
| 36 | /* Make a special file which is block, character and fifo */ |
| 37 | errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st) |
| 38 | { |
| 39 | @@ -23,4 +31,93 @@ errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest) |
| 40 | /* Copy files from source_dir to fs */ |
| 41 | errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir) |
| 42 | { |
| 43 | + const char *name; |
| 44 | + DIR *dh; |
| 45 | + struct dirent *dent; |
| 46 | + struct stat st; |
| 47 | + char ln_target[PATH_MAX]; |
| 48 | + ext2_ino_t ino; |
| 49 | + errcode_t retval; |
| 50 | + int read_cnt; |
| 51 | + |
| 52 | + root = EXT2_ROOT_INO; |
| 53 | + |
| 54 | + if (chdir(source_dir) < 0) { |
| 55 | + com_err(__func__, errno, |
| 56 | + _("while changing working directory to \"%s\""), source_dir); |
| 57 | + return errno; |
| 58 | + } |
| 59 | + |
| 60 | + if (!(dh = opendir("."))) { |
| 61 | + com_err(__func__, errno, |
| 62 | + _("while openning directory \"%s\""), source_dir); |
| 63 | + return errno; |
| 64 | + } |
| 65 | + |
| 66 | + while((dent = readdir(dh))) { |
| 67 | + if((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, ".."))) |
| 68 | + continue; |
| 69 | + lstat(dent->d_name, &st); |
| 70 | + name = dent->d_name; |
| 71 | + |
| 72 | + switch(st.st_mode & S_IFMT) { |
| 73 | + case S_IFCHR: |
| 74 | + case S_IFBLK: |
| 75 | + case S_IFIFO: |
| 76 | + if ((retval = do_mknod_internal(parent_ino, name, &st))) { |
| 77 | + com_err(__func__, retval, |
| 78 | + _("while creating special file \"%s\""), name); |
| 79 | + return retval; |
| 80 | + } |
| 81 | + break; |
| 82 | + case S_IFSOCK: |
| 83 | + /* FIXME: there is no make socket function atm. */ |
| 84 | + com_err(__func__, 0, |
| 85 | + _("ignoring socket file \"%s\""), name); |
| 86 | + continue; |
| 87 | + case S_IFLNK: |
| 88 | + if((read_cnt = readlink(name, ln_target, sizeof(ln_target))) == -1) { |
| 89 | + com_err(__func__, errno, |
| 90 | + _("while trying to readlink \"%s\""), name); |
| 91 | + return errno; |
| 92 | + } |
| 93 | + ln_target[read_cnt] = '\0'; |
| 94 | + if ((retval = do_symlink_internal(parent_ino, name, ln_target))) { |
| 95 | + com_err(__func__, retval, |
| 96 | + _("while writing symlink\"%s\""), name); |
| 97 | + return retval; |
| 98 | + } |
| 99 | + break; |
| 100 | + case S_IFREG: |
| 101 | + if ((retval = do_write_internal(parent_ino, name, name))) { |
| 102 | + com_err(__func__, retval, |
| 103 | + _("while writing file \"%s\""), name); |
| 104 | + return retval; |
| 105 | + } |
| 106 | + break; |
| 107 | + case S_IFDIR: |
| 108 | + if ((retval = do_mkdir_internal(parent_ino, name, &st))) { |
| 109 | + com_err(__func__, retval, |
| 110 | + _("while making dir \"%s\""), name); |
| 111 | + return retval; |
| 112 | + } |
| 113 | + if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))) { |
| 114 | + com_err(name, retval, 0); |
| 115 | + return retval; |
| 116 | + } |
| 117 | + /* Populate the dir recursively*/ |
| 118 | + retval = populate_fs(ino, name); |
| 119 | + if (retval) { |
| 120 | + com_err(__func__, retval, _("while adding dir \"%s\""), name); |
| 121 | + return retval; |
| 122 | + } |
| 123 | + chdir(".."); |
| 124 | + break; |
| 125 | + default: |
| 126 | + com_err(__func__, 0, |
| 127 | + _("ignoring entry \"%s\""), name); |
| 128 | + } |
| 129 | + } |
| 130 | + closedir(dh); |
| 131 | + return retval; |
| 132 | } |
| 133 | -- |
| 134 | 1.7.10.4 |
| 135 | |