1 #include "create_inode.h"
3 #if __STDC_VERSION__ < 199901L
5 # define __func__ __FUNCTION__
7 # define __func__ "<unknown>"
11 /* 64KiB is the minimium blksize to best minimize system call overhead. */
13 #define IO_BUFSIZE 64*1024
16 /* Block size for `st_blocks' */
21 /* Fill the uid, gid, mode and time for the inode */
22 static void fill_inode(struct ext2_inode *inode, struct stat *st)
25 inode->i_uid = st->st_uid;
26 inode->i_gid = st->st_gid;
27 inode->i_mode |= st->st_mode;
28 inode->i_atime = st->st_atime;
29 inode->i_mtime = st->st_mtime;
30 inode->i_ctime = st->st_ctime;
34 /* Set the uid, gid, mode and time for the inode */
35 errcode_t set_inode_extra(ext2_ino_t cwd, ext2_ino_t ino, struct stat *st)
38 struct ext2_inode inode;
40 retval = ext2fs_read_inode(current_fs, ino, &inode);
42 com_err(__func__, retval, "while reading inode %u", ino);
46 fill_inode(&inode, st);
48 retval = ext2fs_write_inode(current_fs, ino, &inode);
50 com_err(__func__, retval, "while writing inode %u", ino);
55 /* Make a special file which is block, character and fifo */
56 errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st)
60 struct ext2_inode inode;
61 unsigned long major, minor, mode;
64 switch(st->st_mode & S_IFMT) {
67 filetype = EXT2_FT_CHRDEV;
71 filetype = EXT2_FT_BLKDEV;
75 filetype = EXT2_FT_FIFO;
79 if (!(current_fs->flags & EXT2_FLAG_RW)) {
80 com_err(__func__, 0, "Filesystem opened read/only");
83 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino);
85 com_err(__func__, retval, 0);
90 printf("Allocated inode: %u\n", ino);
92 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
93 if (retval == EXT2_ET_DIR_NO_SPACE) {
94 retval = ext2fs_expand_dir(current_fs, cwd);
96 com_err(__func__, retval, "while expanding directory");
99 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
102 com_err(name, retval, 0);
105 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino))
106 com_err(__func__, 0, "Warning: inode already set");
107 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
108 memset(&inode, 0, sizeof(inode));
110 inode.i_atime = inode.i_ctime = inode.i_mtime =
111 current_fs->now ? current_fs->now : time(0);
113 major = major(st->st_rdev);
114 minor = minor(st->st_rdev);
116 if ((major < 256) && (minor < 256)) {
117 inode.i_block[0] = major * 256 + minor;
118 inode.i_block[1] = 0;
120 inode.i_block[0] = 0;
121 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
123 inode.i_links_count = 1;
125 retval = ext2fs_write_new_inode(current_fs, ino, &inode);
127 com_err(__func__, retval, "while creating inode %u", ino);
132 /* Make a symlink name -> target */
133 errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
136 ext2_ino_t parent_ino;
138 struct ext2_inode inode;
141 cp = strrchr(name, '/');
144 if ((retval = ext2fs_namei(current_fs, root, cwd, name, &parent_ino))){
145 com_err(name, retval, 0);
153 retval = ext2fs_symlink(current_fs, parent_ino, 0, name, target);
154 if (retval == EXT2_ET_DIR_NO_SPACE) {
155 retval = ext2fs_expand_dir(current_fs, parent_ino);
157 com_err("do_symlink_internal", retval, "while expanding directory");
163 com_err("ext2fs_symlink", retval, 0);
169 /* Make a directory in the fs */
170 errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
173 ext2_ino_t parent_ino, ino;
175 struct ext2_inode inode;
178 cp = strrchr(name, '/');
181 if ((retval = ext2fs_namei(current_fs, root, cwd, name, &parent_ino))){
182 com_err(name, retval, 0);
190 retval = ext2fs_mkdir(current_fs, parent_ino, 0, name);
191 if (retval == EXT2_ET_DIR_NO_SPACE) {
192 retval = ext2fs_expand_dir(current_fs, parent_ino);
194 com_err(__func__, retval, "while expanding directory");
200 com_err("ext2fs_mkdir", retval, 0);
205 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
210 unsigned int written;
216 retval = ext2fs_file_open(current_fs, newfile,
217 EXT2_FILE_WRITE, &e2_file);
221 retval = ext2fs_get_mem(bufsize, &buf);
223 com_err("copy_file", retval, "can't allocate buffer\n");
227 /* This is used for checking whether the whole block is zero */
228 retval = ext2fs_get_memzero(bufsize, &zero_buf);
230 com_err("copy_file", retval, "can't allocate buffer\n");
231 ext2fs_free_mem(&buf);
236 got = read(fd, buf, bufsize);
247 /* Check whether all is zero */
248 cmp = memcmp(ptr, zero_buf, got);
250 /* The whole block is zero, make a hole */
251 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
260 retval = ext2fs_file_write(e2_file, ptr,
269 ext2fs_free_mem(&buf);
270 ext2fs_free_mem(&zero_buf);
271 retval = ext2fs_file_close(e2_file);
275 ext2fs_free_mem(&buf);
276 ext2fs_free_mem(&zero_buf);
277 (void) ext2fs_file_close(e2_file);
281 /* Copy the native file to the fs */
282 errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
288 struct ext2_inode inode;
289 int bufsize = IO_BUFSIZE;
292 fd = open(src, O_RDONLY);
294 com_err(src, errno, 0);
297 if (fstat(fd, &statbuf) < 0) {
298 com_err(src, errno, 0);
303 retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile);
305 com_err(__func__, 0, "The file '%s' already exists\n", dest);
310 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
312 com_err(__func__, retval, 0);
317 printf("Allocated inode: %u\n", newfile);
319 retval = ext2fs_link(current_fs, cwd, dest, newfile,
321 if (retval == EXT2_ET_DIR_NO_SPACE) {
322 retval = ext2fs_expand_dir(current_fs, cwd);
324 com_err(__func__, retval, "while expanding directory");
328 retval = ext2fs_link(current_fs, cwd, dest, newfile,
332 com_err(dest, retval, 0);
336 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile))
337 com_err(__func__, 0, "Warning: inode already set");
338 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
339 memset(&inode, 0, sizeof(inode));
340 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
341 inode.i_atime = inode.i_ctime = inode.i_mtime =
342 current_fs->now ? current_fs->now : time(0);
343 inode.i_links_count = 1;
344 inode.i_size = statbuf.st_size;
345 if (current_fs->super->s_feature_incompat &
346 EXT3_FEATURE_INCOMPAT_EXTENTS) {
348 struct ext3_extent_header *eh;
350 eh = (struct ext3_extent_header *) &inode.i_block[0];
353 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
354 i = (sizeof(inode.i_block) - sizeof(*eh)) /
355 sizeof(struct ext3_extent);
356 eh->eh_max = ext2fs_cpu_to_le16(i);
357 inode.i_flags |= EXT4_EXTENTS_FL;
360 if ((retval = ext2fs_write_new_inode(current_fs, newfile, &inode))) {
361 com_err(__func__, retval, "while creating inode %u", newfile);
365 if (LINUX_S_ISREG(inode.i_mode)) {
366 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
369 * Use I/O blocksize as buffer size when
370 * copying sparse files.
372 bufsize = statbuf.st_blksize;
374 retval = copy_file(fd, newfile, bufsize, make_holes);
376 com_err("copy_file", retval, 0);
383 /* Copy files from source_dir to fs */
384 errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
390 char ln_target[PATH_MAX];
395 root = EXT2_ROOT_INO;
397 if (chdir(source_dir) < 0) {
398 com_err(__func__, errno,
399 _("while changing working directory to \"%s\""), source_dir);
403 if (!(dh = opendir("."))) {
404 com_err(__func__, errno,
405 _("while openning directory \"%s\""), source_dir);
409 while((dent = readdir(dh))) {
410 if((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
412 lstat(dent->d_name, &st);
415 switch(st.st_mode & S_IFMT) {
419 if ((retval = do_mknod_internal(parent_ino, name, &st))) {
420 com_err(__func__, retval,
421 _("while creating special file \"%s\""), name);
426 /* FIXME: there is no make socket function atm. */
428 _("ignoring socket file \"%s\""), name);
431 if((read_cnt = readlink(name, ln_target, sizeof(ln_target))) == -1) {
432 com_err(__func__, errno,
433 _("while trying to readlink \"%s\""), name);
436 ln_target[read_cnt] = '\0';
437 if ((retval = do_symlink_internal(parent_ino, name, ln_target))) {
438 com_err(__func__, retval,
439 _("while writing symlink\"%s\""), name);
444 if ((retval = do_write_internal(parent_ino, name, name))) {
445 com_err(__func__, retval,
446 _("while writing file \"%s\""), name);
451 if ((retval = do_mkdir_internal(parent_ino, name, &st))) {
452 com_err(__func__, retval,
453 _("while making dir \"%s\""), name);
456 if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))) {
457 com_err(name, retval, 0);
460 /* Populate the dir recursively*/
461 retval = populate_fs(ino, name);
463 com_err(__func__, retval, _("while adding dir \"%s\""), name);
470 _("ignoring entry \"%s\""), name);
473 if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))){
474 com_err(name, retval, 0);
478 if ((retval = set_inode_extra(parent_ino, ino, &st))) {
479 com_err(__func__, retval,
480 _("while setting inode for \"%s\""), name);