Whamcloud - gitweb
misc/create_inode.c: copy files recursively
[tools/e2fsprogs.git] / misc / create_inode.c
1 #include "create_inode.h"
2
3 #if __STDC_VERSION__ < 199901L
4 # if __GNUC__ >= 2
5 #  define __func__ __FUNCTION__
6 # else
7 #  define __func__ "<unknown>"
8 # endif
9 #endif
10
11 /* Make a special file which is block, character and fifo */
12 errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st)
13 {
14 }
15
16 /* Make a symlink name -> target */
17 errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
18 {
19 }
20
21 /* Make a directory in the fs */
22 errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
23 {
24 }
25
26 /* Copy the native file to the fs */
27 errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
28 {
29 }
30
31 /* Copy files from source_dir to fs */
32 errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
33 {
34         const char      *name;
35         DIR             *dh;
36         struct dirent   *dent;
37         struct stat     st;
38         char            ln_target[PATH_MAX];
39         ext2_ino_t      ino;
40         errcode_t       retval;
41         int             read_cnt;
42
43         root = EXT2_ROOT_INO;
44
45         if (chdir(source_dir) < 0) {
46                 com_err(__func__, errno,
47                         _("while changing working directory to \"%s\""), source_dir);
48                 return errno;
49         }
50
51         if (!(dh = opendir("."))) {
52                 com_err(__func__, errno,
53                         _("while openning directory \"%s\""), source_dir);
54                 return errno;
55         }
56
57         while((dent = readdir(dh))) {
58                 if((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
59                         continue;
60                 lstat(dent->d_name, &st);
61                 name = dent->d_name;
62
63                 switch(st.st_mode & S_IFMT) {
64                         case S_IFCHR:
65                         case S_IFBLK:
66                         case S_IFIFO:
67                                 if ((retval = do_mknod_internal(parent_ino, name, &st))) {
68                                         com_err(__func__, retval,
69                                                 _("while creating special file \"%s\""), name);
70                                         return retval;
71                                 }
72                                 break;
73                         case S_IFSOCK:
74                                 /* FIXME: there is no make socket function atm. */
75                                 com_err(__func__, 0,
76                                         _("ignoring socket file \"%s\""), name);
77                                 continue;
78                         case S_IFLNK:
79                                 if((read_cnt = readlink(name, ln_target, sizeof(ln_target))) == -1) {
80                                         com_err(__func__, errno,
81                                                 _("while trying to readlink \"%s\""), name);
82                                         return errno;
83                                 }
84                                 ln_target[read_cnt] = '\0';
85                                 if ((retval = do_symlink_internal(parent_ino, name, ln_target))) {
86                                         com_err(__func__, retval,
87                                                 _("while writing symlink\"%s\""), name);
88                                         return retval;
89                                 }
90                                 break;
91                         case S_IFREG:
92                                 if ((retval = do_write_internal(parent_ino, name, name))) {
93                                         com_err(__func__, retval,
94                                                 _("while writing file \"%s\""), name);
95                                         return retval;
96                                 }
97                                 break;
98                         case S_IFDIR:
99                                 if ((retval = do_mkdir_internal(parent_ino, name, &st))) {
100                                         com_err(__func__, retval,
101                                                 _("while making dir \"%s\""), name);
102                                         return retval;
103                                 }
104                                 if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))) {
105                                         com_err(name, retval, 0);
106                                                 return retval;
107                                 }
108                                 /* Populate the dir recursively*/
109                                 retval = populate_fs(ino, name);
110                                 if (retval) {
111                                         com_err(__func__, retval, _("while adding dir \"%s\""), name);
112                                         return retval;
113                                 }
114                                 chdir("..");
115                                 break;
116                         default:
117                                 com_err(__func__, 0,
118                                         _("ignoring entry \"%s\""), name);
119                 }
120         }
121         closedir(dh);
122         return retval;
123 }