Whamcloud - gitweb
misc/create_inode.c: create special file
[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         ext2_ino_t              ino;
15         errcode_t               retval;
16         struct ext2_inode       inode;
17         unsigned long           major, minor, mode;
18         int                     filetype;
19
20         switch(st->st_mode & S_IFMT) {
21                 case S_IFCHR:
22                         mode = LINUX_S_IFCHR;
23                         filetype = EXT2_FT_CHRDEV;
24                         break;
25                 case S_IFBLK:
26                         mode = LINUX_S_IFBLK;
27                         filetype =  EXT2_FT_BLKDEV;
28                         break;
29                 case S_IFIFO:
30                         mode = LINUX_S_IFIFO;
31                         filetype = EXT2_FT_FIFO;
32                         break;
33         }
34
35         if (!(current_fs->flags & EXT2_FLAG_RW)) {
36                 com_err(__func__, 0, "Filesystem opened read/only");
37                 return -1;
38         }
39         retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino);
40         if (retval) {
41                 com_err(__func__, retval, 0);
42                 return retval;
43         }
44
45 #ifdef DEBUGFS
46         printf("Allocated inode: %u\n", ino);
47 #endif
48         retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
49         if (retval == EXT2_ET_DIR_NO_SPACE) {
50                 retval = ext2fs_expand_dir(current_fs, cwd);
51                 if (retval) {
52                         com_err(__func__, retval, "while expanding directory");
53                         return retval;
54                 }
55                 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
56         }
57         if (retval) {
58                 com_err(name, retval, 0);
59                 return -1;
60         }
61         if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino))
62                 com_err(__func__, 0, "Warning: inode already set");
63         ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
64         memset(&inode, 0, sizeof(inode));
65         inode.i_mode = mode;
66         inode.i_atime = inode.i_ctime = inode.i_mtime =
67                 current_fs->now ? current_fs->now : time(0);
68
69         major = major(st->st_rdev);
70         minor = minor(st->st_rdev);
71
72         if ((major < 256) && (minor < 256)) {
73                 inode.i_block[0] = major * 256 + minor;
74                 inode.i_block[1] = 0;
75         } else {
76                 inode.i_block[0] = 0;
77                 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
78         }
79         inode.i_links_count = 1;
80
81         retval = ext2fs_write_new_inode(current_fs, ino, &inode);
82         if (retval)
83                 com_err(__func__, retval, "while creating inode %u", ino);
84
85         return retval;
86 }
87
88 /* Make a symlink name -> target */
89 errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
90 {
91 }
92
93 /* Make a directory in the fs */
94 errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
95 {
96 }
97
98 /* Copy the native file to the fs */
99 errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
100 {
101 }
102
103 /* Copy files from source_dir to fs */
104 errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
105 {
106         const char      *name;
107         DIR             *dh;
108         struct dirent   *dent;
109         struct stat     st;
110         char            ln_target[PATH_MAX];
111         ext2_ino_t      ino;
112         errcode_t       retval;
113         int             read_cnt;
114
115         root = EXT2_ROOT_INO;
116
117         if (chdir(source_dir) < 0) {
118                 com_err(__func__, errno,
119                         _("while changing working directory to \"%s\""), source_dir);
120                 return errno;
121         }
122
123         if (!(dh = opendir("."))) {
124                 com_err(__func__, errno,
125                         _("while openning directory \"%s\""), source_dir);
126                 return errno;
127         }
128
129         while((dent = readdir(dh))) {
130                 if((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
131                         continue;
132                 lstat(dent->d_name, &st);
133                 name = dent->d_name;
134
135                 switch(st.st_mode & S_IFMT) {
136                         case S_IFCHR:
137                         case S_IFBLK:
138                         case S_IFIFO:
139                                 if ((retval = do_mknod_internal(parent_ino, name, &st))) {
140                                         com_err(__func__, retval,
141                                                 _("while creating special file \"%s\""), name);
142                                         return retval;
143                                 }
144                                 break;
145                         case S_IFSOCK:
146                                 /* FIXME: there is no make socket function atm. */
147                                 com_err(__func__, 0,
148                                         _("ignoring socket file \"%s\""), name);
149                                 continue;
150                         case S_IFLNK:
151                                 if((read_cnt = readlink(name, ln_target, sizeof(ln_target))) == -1) {
152                                         com_err(__func__, errno,
153                                                 _("while trying to readlink \"%s\""), name);
154                                         return errno;
155                                 }
156                                 ln_target[read_cnt] = '\0';
157                                 if ((retval = do_symlink_internal(parent_ino, name, ln_target))) {
158                                         com_err(__func__, retval,
159                                                 _("while writing symlink\"%s\""), name);
160                                         return retval;
161                                 }
162                                 break;
163                         case S_IFREG:
164                                 if ((retval = do_write_internal(parent_ino, name, name))) {
165                                         com_err(__func__, retval,
166                                                 _("while writing file \"%s\""), name);
167                                         return retval;
168                                 }
169                                 break;
170                         case S_IFDIR:
171                                 if ((retval = do_mkdir_internal(parent_ino, name, &st))) {
172                                         com_err(__func__, retval,
173                                                 _("while making dir \"%s\""), name);
174                                         return retval;
175                                 }
176                                 if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))) {
177                                         com_err(name, retval, 0);
178                                                 return retval;
179                                 }
180                                 /* Populate the dir recursively*/
181                                 retval = populate_fs(ino, name);
182                                 if (retval) {
183                                         com_err(__func__, retval, _("while adding dir \"%s\""), name);
184                                         return retval;
185                                 }
186                                 chdir("..");
187                                 break;
188                         default:
189                                 com_err(__func__, 0,
190                                         _("ignoring entry \"%s\""), name);
191                 }
192         }
193         closedir(dh);
194         return retval;
195 }