1 #include "create_inode.h"
3 #if __STDC_VERSION__ < 199901L
5 # define __func__ __FUNCTION__
7 # define __func__ "<unknown>"
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)
16 struct ext2_inode inode;
17 unsigned long major, minor, mode;
20 switch(st->st_mode & S_IFMT) {
23 filetype = EXT2_FT_CHRDEV;
27 filetype = EXT2_FT_BLKDEV;
31 filetype = EXT2_FT_FIFO;
35 if (!(current_fs->flags & EXT2_FLAG_RW)) {
36 com_err(__func__, 0, "Filesystem opened read/only");
39 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino);
41 com_err(__func__, retval, 0);
46 printf("Allocated inode: %u\n", ino);
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);
52 com_err(__func__, retval, "while expanding directory");
55 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
58 com_err(name, retval, 0);
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));
66 inode.i_atime = inode.i_ctime = inode.i_mtime =
67 current_fs->now ? current_fs->now : time(0);
69 major = major(st->st_rdev);
70 minor = minor(st->st_rdev);
72 if ((major < 256) && (minor < 256)) {
73 inode.i_block[0] = major * 256 + minor;
77 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
79 inode.i_links_count = 1;
81 retval = ext2fs_write_new_inode(current_fs, ino, &inode);
83 com_err(__func__, retval, "while creating inode %u", ino);
88 /* Make a symlink name -> target */
89 errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
93 /* Make a directory in the fs */
94 errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
98 /* Copy the native file to the fs */
99 errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
103 /* Copy files from source_dir to fs */
104 errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
110 char ln_target[PATH_MAX];
115 root = EXT2_ROOT_INO;
117 if (chdir(source_dir) < 0) {
118 com_err(__func__, errno,
119 _("while changing working directory to \"%s\""), source_dir);
123 if (!(dh = opendir("."))) {
124 com_err(__func__, errno,
125 _("while openning directory \"%s\""), source_dir);
129 while((dent = readdir(dh))) {
130 if((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
132 lstat(dent->d_name, &st);
135 switch(st.st_mode & S_IFMT) {
139 if ((retval = do_mknod_internal(parent_ino, name, &st))) {
140 com_err(__func__, retval,
141 _("while creating special file \"%s\""), name);
146 /* FIXME: there is no make socket function atm. */
148 _("ignoring socket file \"%s\""), name);
151 if((read_cnt = readlink(name, ln_target, sizeof(ln_target))) == -1) {
152 com_err(__func__, errno,
153 _("while trying to readlink \"%s\""), name);
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);
164 if ((retval = do_write_internal(parent_ino, name, name))) {
165 com_err(__func__, retval,
166 _("while writing file \"%s\""), name);
171 if ((retval = do_mkdir_internal(parent_ino, name, &st))) {
172 com_err(__func__, retval,
173 _("while making dir \"%s\""), name);
176 if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))) {
177 com_err(name, retval, 0);
180 /* Populate the dir recursively*/
181 retval = populate_fs(ino, name);
183 com_err(__func__, retval, _("while adding dir \"%s\""), name);
190 _("ignoring entry \"%s\""), name);