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, const char *name)
30 struct ext2_inode inode;
33 retval = ext2fs_read_inode(current_fs, ino, &inode);
35 com_err(__func__, retval, "while reading inode %u", ino);
39 retval = ext2fs_link(current_fs, parent_ino, name, ino, inode.i_flags);
40 if (retval == EXT2_ET_DIR_NO_SPACE) {
41 retval = ext2fs_expand_dir(current_fs, parent_ino);
43 com_err(__func__, retval, "while expanding directory");
46 retval = ext2fs_link(current_fs, parent_ino, name, ino, inode.i_flags);
49 com_err(__func__, retval, "while linking %s", name);
53 inode.i_links_count++;
55 retval = ext2fs_write_inode(current_fs, ino, &inode);
57 com_err(__func__, retval, "while writing inode %u", ino);
62 /* Fill the uid, gid, mode and time for the inode */
63 static void fill_inode(struct ext2_inode *inode, struct stat *st)
66 inode->i_uid = st->st_uid;
67 inode->i_gid = st->st_gid;
68 inode->i_mode |= st->st_mode;
69 inode->i_atime = st->st_atime;
70 inode->i_mtime = st->st_mtime;
71 inode->i_ctime = st->st_ctime;
75 /* Set the uid, gid, mode and time for the inode */
76 errcode_t set_inode_extra(ext2_ino_t cwd, ext2_ino_t ino, struct stat *st)
79 struct ext2_inode inode;
81 retval = ext2fs_read_inode(current_fs, ino, &inode);
83 com_err(__func__, retval, "while reading inode %u", ino);
87 fill_inode(&inode, st);
89 retval = ext2fs_write_inode(current_fs, ino, &inode);
91 com_err(__func__, retval, "while writing inode %u", ino);
96 /* Make a special file which is block, character and fifo */
97 errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st)
101 struct ext2_inode inode;
102 unsigned long major, minor, mode;
105 switch(st->st_mode & S_IFMT) {
107 mode = LINUX_S_IFCHR;
108 filetype = EXT2_FT_CHRDEV;
111 mode = LINUX_S_IFBLK;
112 filetype = EXT2_FT_BLKDEV;
115 mode = LINUX_S_IFIFO;
116 filetype = EXT2_FT_FIFO;
120 if (!(current_fs->flags & EXT2_FLAG_RW)) {
121 com_err(__func__, 0, "Filesystem opened read/only");
124 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino);
126 com_err(__func__, retval, 0);
131 printf("Allocated inode: %u\n", ino);
133 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
134 if (retval == EXT2_ET_DIR_NO_SPACE) {
135 retval = ext2fs_expand_dir(current_fs, cwd);
137 com_err(__func__, retval, "while expanding directory");
140 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
143 com_err(name, retval, 0);
146 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino))
147 com_err(__func__, 0, "Warning: inode already set");
148 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
149 memset(&inode, 0, sizeof(inode));
151 inode.i_atime = inode.i_ctime = inode.i_mtime =
152 current_fs->now ? current_fs->now : time(0);
154 major = major(st->st_rdev);
155 minor = minor(st->st_rdev);
157 if ((major < 256) && (minor < 256)) {
158 inode.i_block[0] = major * 256 + minor;
159 inode.i_block[1] = 0;
161 inode.i_block[0] = 0;
162 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
164 inode.i_links_count = 1;
166 retval = ext2fs_write_new_inode(current_fs, ino, &inode);
168 com_err(__func__, retval, "while creating inode %u", ino);
173 /* Make a symlink name -> target */
174 errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
177 ext2_ino_t parent_ino;
179 struct ext2_inode inode;
182 cp = strrchr(name, '/');
185 retval = ext2fs_namei(current_fs, root, cwd, name, &parent_ino);
187 com_err(name, retval, 0);
195 retval = ext2fs_symlink(current_fs, parent_ino, 0, name, target);
196 if (retval == EXT2_ET_DIR_NO_SPACE) {
197 retval = ext2fs_expand_dir(current_fs, parent_ino);
199 com_err("do_symlink_internal", retval, "while expanding directory");
205 com_err("ext2fs_symlink", retval, 0);
211 /* Make a directory in the fs */
212 errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
215 ext2_ino_t parent_ino, ino;
217 struct ext2_inode inode;
220 cp = strrchr(name, '/');
223 retval = ext2fs_namei(current_fs, root, cwd, name, &parent_ino);
225 com_err(name, retval, 0);
233 retval = ext2fs_mkdir(current_fs, parent_ino, 0, name);
234 if (retval == EXT2_ET_DIR_NO_SPACE) {
235 retval = ext2fs_expand_dir(current_fs, parent_ino);
237 com_err(__func__, retval, "while expanding directory");
243 com_err("ext2fs_mkdir", retval, 0);
248 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
253 unsigned int written;
259 retval = ext2fs_file_open(current_fs, newfile,
260 EXT2_FILE_WRITE, &e2_file);
264 retval = ext2fs_get_mem(bufsize, &buf);
266 com_err("copy_file", retval, "can't allocate buffer\n");
270 /* This is used for checking whether the whole block is zero */
271 retval = ext2fs_get_memzero(bufsize, &zero_buf);
273 com_err("copy_file", retval, "can't allocate buffer\n");
274 ext2fs_free_mem(&buf);
279 got = read(fd, buf, bufsize);
290 /* Check whether all is zero */
291 cmp = memcmp(ptr, zero_buf, got);
293 /* The whole block is zero, make a hole */
294 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
303 retval = ext2fs_file_write(e2_file, ptr,
312 ext2fs_free_mem(&buf);
313 ext2fs_free_mem(&zero_buf);
314 retval = ext2fs_file_close(e2_file);
318 ext2fs_free_mem(&buf);
319 ext2fs_free_mem(&zero_buf);
320 (void) ext2fs_file_close(e2_file);
324 int is_hardlink(ext2_ino_t ino)
328 for(i = 0; i < hdlinks.count; i++) {
329 if(hdlinks.hdl[i].src_ino == ino)
335 /* Copy the native file to the fs */
336 errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
342 struct ext2_inode inode;
343 int bufsize = IO_BUFSIZE;
346 fd = open(src, O_RDONLY);
348 com_err(src, errno, 0);
351 if (fstat(fd, &statbuf) < 0) {
352 com_err(src, errno, 0);
357 retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile);
360 return EXT2_ET_FILE_EXISTS;
363 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
365 com_err(__func__, retval, 0);
370 printf("Allocated inode: %u\n", newfile);
372 retval = ext2fs_link(current_fs, cwd, dest, newfile,
374 if (retval == EXT2_ET_DIR_NO_SPACE) {
375 retval = ext2fs_expand_dir(current_fs, cwd);
377 com_err(__func__, retval, "while expanding directory");
381 retval = ext2fs_link(current_fs, cwd, dest, newfile,
385 com_err(dest, retval, 0);
389 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile))
390 com_err(__func__, 0, "Warning: inode already set");
391 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
392 memset(&inode, 0, sizeof(inode));
393 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
394 inode.i_atime = inode.i_ctime = inode.i_mtime =
395 current_fs->now ? current_fs->now : time(0);
396 inode.i_links_count = 1;
397 inode.i_size = statbuf.st_size;
398 if (EXT2_HAS_INCOMPAT_FEATURE(current_fs->super,
399 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
400 inode.i_flags |= EXT4_INLINE_DATA_FL;
401 } else if (current_fs->super->s_feature_incompat &
402 EXT3_FEATURE_INCOMPAT_EXTENTS) {
404 struct ext3_extent_header *eh;
406 eh = (struct ext3_extent_header *) &inode.i_block[0];
409 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
410 i = (sizeof(inode.i_block) - sizeof(*eh)) /
411 sizeof(struct ext3_extent);
412 eh->eh_max = ext2fs_cpu_to_le16(i);
413 inode.i_flags |= EXT4_EXTENTS_FL;
416 retval = ext2fs_write_new_inode(current_fs, newfile, &inode);
418 com_err(__func__, retval, "while creating inode %u", newfile);
422 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
423 retval = ext2fs_inline_data_init(current_fs, newfile);
425 com_err("copy_file", retval, 0);
430 if (LINUX_S_ISREG(inode.i_mode)) {
431 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
434 * Use I/O blocksize as buffer size when
435 * copying sparse files.
437 bufsize = statbuf.st_blksize;
439 retval = copy_file(fd, newfile, bufsize, make_holes);
441 com_err("copy_file", retval, 0);
448 /* Copy files from source_dir to fs */
449 errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
455 char ln_target[PATH_MAX];
456 unsigned int save_inode;
462 root = EXT2_ROOT_INO;
464 if (chdir(source_dir) < 0) {
465 com_err(__func__, errno,
466 _("while changing working directory to \"%s\""), source_dir);
470 if (!(dh = opendir("."))) {
471 com_err(__func__, errno,
472 _("while opening directory \"%s\""), source_dir);
476 while ((dent = readdir(dh))) {
477 if ((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
479 lstat(dent->d_name, &st);
482 /* Check for hardlinks */
484 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) && st.st_nlink > 1) {
485 hdlink = is_hardlink(st.st_ino);
487 retval = add_link(parent_ino,
488 hdlinks.hdl[hdlink].dst_ino, name);
490 com_err(__func__, retval, "while linking %s", name);
498 switch(st.st_mode & S_IFMT) {
502 retval = do_mknod_internal(parent_ino, name, &st);
504 com_err(__func__, retval,
505 _("while creating special file \"%s\""), name);
510 /* FIXME: there is no make socket function atm. */
512 _("ignoring socket file \"%s\""), name);
515 read_cnt = readlink(name, ln_target, sizeof(ln_target));
516 if (read_cnt == -1) {
517 com_err(__func__, errno,
518 _("while trying to readlink \"%s\""), name);
521 ln_target[read_cnt] = '\0';
522 retval = do_symlink_internal(parent_ino, name, ln_target);
524 com_err(__func__, retval,
525 _("while writing symlink\"%s\""), name);
530 retval = do_write_internal(parent_ino, name, name);
532 com_err(__func__, retval,
533 _("while writing file \"%s\""), name);
538 retval = do_mkdir_internal(parent_ino, name, &st);
540 com_err(__func__, retval,
541 _("while making dir \"%s\""), name);
544 retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino);
546 com_err(name, retval, 0);
549 /* Populate the dir recursively*/
550 retval = populate_fs(ino, name);
552 com_err(__func__, retval, _("while adding dir \"%s\""), name);
559 _("ignoring entry \"%s\""), name);
562 retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino);
564 com_err(name, retval, 0);
568 retval = set_inode_extra(parent_ino, ino, &st);
570 com_err(__func__, retval,
571 _("while setting inode for \"%s\""), name);
575 /* Save the hardlink ino */
578 * Check whether need more memory, and we don't need
579 * free() since the lifespan will be over after the fs
582 if (hdlinks.count == hdlink_cnt) {
583 if ((hdlinks.hdl = realloc (hdlinks.hdl,
584 (hdlink_cnt + HDLINK_CNT) *
585 sizeof (struct hdlink_s))) == NULL) {
586 com_err(name, errno, "Not enough memory");
589 hdlink_cnt += HDLINK_CNT;
591 hdlinks.hdl[hdlinks.count].src_ino = st.st_ino;
592 hdlinks.hdl[hdlinks.count].dst_ino = ino;