4 #include "create_inode.h"
6 #if __STDC_VERSION__ < 199901L
8 # define __func__ __FUNCTION__
10 # define __func__ "<unknown>"
14 /* 64KiB is the minimium blksize to best minimize system call overhead. */
16 #define IO_BUFSIZE 64*1024
19 /* Block size for `st_blocks' */
24 /* For saving the hard links */
25 int hdlink_cnt = HDLINK_CNT;
27 /* Link an inode number to a directory */
28 static errcode_t add_link(ext2_ino_t parent_ino, ext2_ino_t ino,
31 struct ext2_inode inode;
34 retval = ext2fs_read_inode(current_fs, ino, &inode);
36 com_err(__func__, retval, "while reading inode %u", ino);
40 retval = ext2fs_link(current_fs, parent_ino, name, ino, inode.i_flags);
41 if (retval == EXT2_ET_DIR_NO_SPACE) {
42 retval = ext2fs_expand_dir(current_fs, parent_ino);
44 com_err(__func__, retval, "while expanding directory");
47 retval = ext2fs_link(current_fs, parent_ino, name, ino,
51 com_err(__func__, retval, "while linking %s", name);
55 inode.i_links_count++;
57 retval = ext2fs_write_inode(current_fs, ino, &inode);
59 com_err(__func__, retval, "while writing inode %u", ino);
64 /* Fill the uid, gid, mode and time for the inode */
65 static void fill_inode(struct ext2_inode *inode, struct stat *st)
68 inode->i_uid = st->st_uid;
69 inode->i_gid = st->st_gid;
70 inode->i_mode |= st->st_mode;
71 inode->i_atime = st->st_atime;
72 inode->i_mtime = st->st_mtime;
73 inode->i_ctime = st->st_ctime;
77 /* Set the uid, gid, mode and time for the inode */
78 errcode_t set_inode_extra(ext2_ino_t cwd, ext2_ino_t ino, struct stat *st)
81 struct ext2_inode inode;
83 retval = ext2fs_read_inode(current_fs, ino, &inode);
85 com_err(__func__, retval, "while reading inode %u", ino);
89 fill_inode(&inode, st);
91 retval = ext2fs_write_inode(current_fs, ino, &inode);
93 com_err(__func__, retval, "while writing inode %u", ino);
98 /* Make a special file which is block, character and fifo */
99 errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st)
103 struct ext2_inode inode;
104 unsigned long major, minor, mode;
107 switch(st->st_mode & S_IFMT) {
109 mode = LINUX_S_IFCHR;
110 filetype = EXT2_FT_CHRDEV;
113 mode = LINUX_S_IFBLK;
114 filetype = EXT2_FT_BLKDEV;
117 mode = LINUX_S_IFIFO;
118 filetype = EXT2_FT_FIFO;
122 if (!(current_fs->flags & EXT2_FLAG_RW)) {
123 com_err(__func__, 0, "Filesystem opened read/only");
126 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino);
128 com_err(__func__, retval, 0);
133 printf("Allocated inode: %u\n", ino);
135 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
136 if (retval == EXT2_ET_DIR_NO_SPACE) {
137 retval = ext2fs_expand_dir(current_fs, cwd);
139 com_err(__func__, retval, "while expanding directory");
142 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
145 com_err(name, retval, 0);
148 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino))
149 com_err(__func__, 0, "Warning: inode already set");
150 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
151 memset(&inode, 0, sizeof(inode));
153 inode.i_atime = inode.i_ctime = inode.i_mtime =
154 current_fs->now ? current_fs->now : time(0);
156 major = major(st->st_rdev);
157 minor = minor(st->st_rdev);
159 if ((major < 256) && (minor < 256)) {
160 inode.i_block[0] = major * 256 + minor;
161 inode.i_block[1] = 0;
163 inode.i_block[0] = 0;
164 inode.i_block[1] = (minor & 0xff) | (major << 8) |
165 ((minor & ~0xff) << 12);
167 inode.i_links_count = 1;
169 retval = ext2fs_write_new_inode(current_fs, ino, &inode);
171 com_err(__func__, retval, "while creating inode %u", ino);
176 /* Make a symlink name -> target */
177 errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
180 ext2_ino_t parent_ino;
182 struct ext2_inode inode;
185 cp = strrchr(name, '/');
188 retval = ext2fs_namei(current_fs, root, cwd, name,
191 com_err(name, retval, 0);
199 retval = ext2fs_symlink(current_fs, parent_ino, 0, name, target);
200 if (retval == EXT2_ET_DIR_NO_SPACE) {
201 retval = ext2fs_expand_dir(current_fs, parent_ino);
203 com_err("do_symlink_internal", retval,
204 "while expanding directory");
210 com_err("ext2fs_symlink", retval, 0);
216 /* Make a directory in the fs */
217 errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
220 ext2_ino_t parent_ino, ino;
222 struct ext2_inode inode;
225 cp = strrchr(name, '/');
228 retval = ext2fs_namei(current_fs, root, cwd, name,
231 com_err(name, retval, 0);
239 retval = ext2fs_mkdir(current_fs, parent_ino, 0, name);
240 if (retval == EXT2_ET_DIR_NO_SPACE) {
241 retval = ext2fs_expand_dir(current_fs, parent_ino);
243 com_err(__func__, retval, "while expanding directory");
249 com_err("ext2fs_mkdir", retval, 0);
254 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize,
260 unsigned int written;
266 retval = ext2fs_file_open(current_fs, newfile,
267 EXT2_FILE_WRITE, &e2_file);
271 retval = ext2fs_get_mem(bufsize, &buf);
273 com_err("copy_file", retval, "can't allocate buffer\n");
277 /* This is used for checking whether the whole block is zero */
278 retval = ext2fs_get_memzero(bufsize, &zero_buf);
280 com_err("copy_file", retval, "can't allocate buffer\n");
281 ext2fs_free_mem(&buf);
286 got = read(fd, buf, bufsize);
297 /* Check whether all is zero */
298 cmp = memcmp(ptr, zero_buf, got);
300 /* The whole block is zero, make a hole */
301 retval = ext2fs_file_lseek(e2_file, got,
312 retval = ext2fs_file_write(e2_file, ptr,
321 ext2fs_free_mem(&buf);
322 ext2fs_free_mem(&zero_buf);
323 retval = ext2fs_file_close(e2_file);
327 ext2fs_free_mem(&buf);
328 ext2fs_free_mem(&zero_buf);
329 (void) ext2fs_file_close(e2_file);
333 int is_hardlink(ext2_ino_t ino)
337 for(i = 0; i < hdlinks.count; i++) {
338 if(hdlinks.hdl[i].src_ino == ino)
344 /* Copy the native file to the fs */
345 errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
351 struct ext2_inode inode;
352 int bufsize = IO_BUFSIZE;
355 fd = open(src, O_RDONLY);
357 com_err(src, errno, 0);
360 if (fstat(fd, &statbuf) < 0) {
361 com_err(src, errno, 0);
366 retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile);
369 return EXT2_ET_FILE_EXISTS;
372 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
374 com_err(__func__, retval, 0);
379 printf("Allocated inode: %u\n", newfile);
381 retval = ext2fs_link(current_fs, cwd, dest, newfile,
383 if (retval == EXT2_ET_DIR_NO_SPACE) {
384 retval = ext2fs_expand_dir(current_fs, cwd);
386 com_err(__func__, retval, "while expanding directory");
390 retval = ext2fs_link(current_fs, cwd, dest, newfile,
394 com_err(dest, retval, 0);
398 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile))
399 com_err(__func__, 0, "Warning: inode already set");
400 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
401 memset(&inode, 0, sizeof(inode));
402 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
403 inode.i_atime = inode.i_ctime = inode.i_mtime =
404 current_fs->now ? current_fs->now : time(0);
405 inode.i_links_count = 1;
406 inode.i_size = statbuf.st_size;
407 if (EXT2_HAS_INCOMPAT_FEATURE(current_fs->super,
408 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
409 inode.i_flags |= EXT4_INLINE_DATA_FL;
410 } else if (current_fs->super->s_feature_incompat &
411 EXT3_FEATURE_INCOMPAT_EXTENTS) {
413 struct ext3_extent_header *eh;
415 eh = (struct ext3_extent_header *) &inode.i_block[0];
418 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
419 i = (sizeof(inode.i_block) - sizeof(*eh)) /
420 sizeof(struct ext3_extent);
421 eh->eh_max = ext2fs_cpu_to_le16(i);
422 inode.i_flags |= EXT4_EXTENTS_FL;
425 retval = ext2fs_write_new_inode(current_fs, newfile, &inode);
427 com_err(__func__, retval, "while creating inode %u", newfile);
431 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
432 retval = ext2fs_inline_data_init(current_fs, newfile);
434 com_err("copy_file", retval, 0);
439 if (LINUX_S_ISREG(inode.i_mode)) {
440 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
443 * Use I/O blocksize as buffer size when
444 * copying sparse files.
446 bufsize = statbuf.st_blksize;
448 retval = copy_file(fd, newfile, bufsize, make_holes);
450 com_err("copy_file", retval, 0);
457 /* Copy files from source_dir to fs */
458 errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
464 char ln_target[PATH_MAX];
465 unsigned int save_inode;
471 root = EXT2_ROOT_INO;
473 if (chdir(source_dir) < 0) {
474 com_err(__func__, errno,
475 _("while changing working directory to \"%s\""),
480 if (!(dh = opendir("."))) {
481 com_err(__func__, errno,
482 _("while opening directory \"%s\""), source_dir);
486 while ((dent = readdir(dh))) {
487 if ((!strcmp(dent->d_name, ".")) ||
488 (!strcmp(dent->d_name, "..")))
490 lstat(dent->d_name, &st);
493 /* Check for hardlinks */
495 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
497 hdlink = is_hardlink(st.st_ino);
499 retval = add_link(parent_ino,
500 hdlinks.hdl[hdlink].dst_ino,
503 com_err(__func__, retval,
504 "while linking %s", name);
512 switch(st.st_mode & S_IFMT) {
516 retval = do_mknod_internal(parent_ino, name, &st);
518 com_err(__func__, retval,
519 _("while creating special file "
525 /* FIXME: there is no make socket function atm. */
527 _("ignoring socket file \"%s\""), name);
530 read_cnt = readlink(name, ln_target,
532 if (read_cnt == -1) {
533 com_err(__func__, errno,
534 _("while trying to readlink \"%s\""),
538 ln_target[read_cnt] = '\0';
539 retval = do_symlink_internal(parent_ino, name,
542 com_err(__func__, retval,
543 _("while writing symlink\"%s\""),
549 retval = do_write_internal(parent_ino, name, name);
551 com_err(__func__, retval,
552 _("while writing file \"%s\""), name);
557 retval = do_mkdir_internal(parent_ino, name, &st);
559 com_err(__func__, retval,
560 _("while making dir \"%s\""), name);
563 retval = ext2fs_namei(current_fs, root, parent_ino,
566 com_err(name, retval, 0);
569 /* Populate the dir recursively*/
570 retval = populate_fs(ino, name);
572 com_err(__func__, retval,
573 _("while adding dir \"%s\""), name);
580 _("ignoring entry \"%s\""), name);
583 retval = ext2fs_namei(current_fs, root, parent_ino,
586 com_err(name, retval, 0);
590 retval = set_inode_extra(parent_ino, ino, &st);
592 com_err(__func__, retval,
593 _("while setting inode for \"%s\""), name);
597 /* Save the hardlink ino */
600 * Check whether need more memory, and we don't need
601 * free() since the lifespan will be over after the fs
604 if (hdlinks.count == hdlink_cnt) {
605 if ((hdlinks.hdl = realloc (hdlinks.hdl,
606 (hdlink_cnt + HDLINK_CNT) *
607 sizeof (struct hdlink_s))) == NULL) {
608 com_err(name, errno, "Not enough memory");
611 hdlink_cnt += HDLINK_CNT;
613 hdlinks.hdl[hdlinks.count].src_ino = st.st_ino;
614 hdlinks.hdl[hdlinks.count].dst_ino = ino;