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_filsys fs, ext2_ino_t parent_ino,
29 ext2_ino_t ino, const char *name)
31 struct ext2_inode inode;
34 retval = ext2fs_read_inode(fs, ino, &inode);
36 com_err(__func__, retval, "while reading inode %u", ino);
40 retval = ext2fs_link(fs, parent_ino, name, ino, inode.i_flags);
41 if (retval == EXT2_ET_DIR_NO_SPACE) {
42 retval = ext2fs_expand_dir(fs, parent_ino);
44 com_err(__func__, retval, "while expanding directory");
47 retval = ext2fs_link(fs, parent_ino, name, ino, inode.i_flags);
50 com_err(__func__, retval, "while linking %s", name);
54 inode.i_links_count++;
56 retval = ext2fs_write_inode(fs, ino, &inode);
58 com_err(__func__, retval, "while writing inode %u", ino);
63 /* Fill the uid, gid, mode and time for the inode */
64 static void fill_inode(struct ext2_inode *inode, struct stat *st)
67 inode->i_uid = st->st_uid;
68 inode->i_gid = st->st_gid;
69 inode->i_mode |= st->st_mode;
70 inode->i_atime = st->st_atime;
71 inode->i_mtime = st->st_mtime;
72 inode->i_ctime = st->st_ctime;
76 /* Set the uid, gid, mode and time for the inode */
77 static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd,
78 ext2_ino_t ino, struct stat *st)
81 struct ext2_inode inode;
83 retval = ext2fs_read_inode(fs, ino, &inode);
85 com_err(__func__, retval, "while reading inode %u", ino);
89 fill_inode(&inode, st);
91 retval = ext2fs_write_inode(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_filsys fs, ext2_ino_t cwd, const char *name,
104 struct ext2_inode inode;
105 unsigned long major, minor, mode;
108 switch(st->st_mode & S_IFMT) {
110 mode = LINUX_S_IFCHR;
111 filetype = EXT2_FT_CHRDEV;
114 mode = LINUX_S_IFBLK;
115 filetype = EXT2_FT_BLKDEV;
118 mode = LINUX_S_IFIFO;
119 filetype = EXT2_FT_FIFO;
123 if (!(fs->flags & EXT2_FLAG_RW)) {
124 com_err(__func__, 0, "Filesystem opened read/only");
127 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
129 com_err(__func__, retval, 0);
134 printf("Allocated inode: %u\n", ino);
136 retval = ext2fs_link(fs, cwd, name, ino, filetype);
137 if (retval == EXT2_ET_DIR_NO_SPACE) {
138 retval = ext2fs_expand_dir(fs, cwd);
140 com_err(__func__, retval, "while expanding directory");
143 retval = ext2fs_link(fs, cwd, name, ino, filetype);
146 com_err(name, retval, 0);
149 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino))
150 com_err(__func__, 0, "Warning: inode already set");
151 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
152 memset(&inode, 0, sizeof(inode));
154 inode.i_atime = inode.i_ctime = inode.i_mtime =
155 fs->now ? fs->now : time(0);
157 major = major(st->st_rdev);
158 minor = minor(st->st_rdev);
160 if ((major < 256) && (minor < 256)) {
161 inode.i_block[0] = major * 256 + minor;
162 inode.i_block[1] = 0;
164 inode.i_block[0] = 0;
165 inode.i_block[1] = (minor & 0xff) | (major << 8) |
166 ((minor & ~0xff) << 12);
168 inode.i_links_count = 1;
170 retval = ext2fs_write_new_inode(fs, ino, &inode);
172 com_err(__func__, retval, "while creating inode %u", ino);
177 /* Make a symlink name -> target */
178 errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
179 char *target, ext2_ino_t root)
182 ext2_ino_t parent_ino;
184 struct ext2_inode inode;
187 cp = strrchr(name, '/');
190 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
192 com_err(name, retval, 0);
200 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
201 if (retval == EXT2_ET_DIR_NO_SPACE) {
202 retval = ext2fs_expand_dir(fs, parent_ino);
204 com_err("do_symlink_internal", retval,
205 "while expanding directory");
211 com_err("ext2fs_symlink", retval, 0);
217 /* Make a directory in the fs */
218 errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
219 struct stat *st, ext2_ino_t root)
222 ext2_ino_t parent_ino, ino;
224 struct ext2_inode inode;
227 cp = strrchr(name, '/');
230 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
232 com_err(name, retval, 0);
240 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
241 if (retval == EXT2_ET_DIR_NO_SPACE) {
242 retval = ext2fs_expand_dir(fs, parent_ino);
244 com_err(__func__, retval, "while expanding directory");
250 com_err("ext2fs_mkdir", retval, 0);
255 static errcode_t copy_file(ext2_filsys fs, int fd, ext2_ino_t newfile,
256 int bufsize, int make_holes)
261 unsigned int written;
267 retval = ext2fs_file_open(fs, newfile,
268 EXT2_FILE_WRITE, &e2_file);
272 retval = ext2fs_get_mem(bufsize, &buf);
274 com_err("copy_file", retval, "can't allocate buffer\n");
278 /* This is used for checking whether the whole block is zero */
279 retval = ext2fs_get_memzero(bufsize, &zero_buf);
281 com_err("copy_file", retval, "can't allocate buffer\n");
282 ext2fs_free_mem(&buf);
287 got = read(fd, buf, bufsize);
298 /* Check whether all is zero */
299 cmp = memcmp(ptr, zero_buf, got);
301 /* The whole block is zero, make a hole */
302 retval = ext2fs_file_lseek(e2_file, got,
313 retval = ext2fs_file_write(e2_file, ptr,
322 ext2fs_free_mem(&buf);
323 ext2fs_free_mem(&zero_buf);
324 retval = ext2fs_file_close(e2_file);
328 ext2fs_free_mem(&buf);
329 ext2fs_free_mem(&zero_buf);
330 (void) ext2fs_file_close(e2_file);
334 static int is_hardlink(ext2_ino_t ino)
338 for(i = 0; i < hdlinks.count; i++) {
339 if(hdlinks.hdl[i].src_ino == ino)
345 /* Copy the native file to the fs */
346 errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
347 const char *dest, ext2_ino_t root)
353 struct ext2_inode inode;
354 int bufsize = IO_BUFSIZE;
357 fd = open(src, O_RDONLY);
359 com_err(src, errno, 0);
362 if (fstat(fd, &statbuf) < 0) {
363 com_err(src, errno, 0);
368 retval = ext2fs_namei(fs, root, cwd, dest, &newfile);
371 return EXT2_ET_FILE_EXISTS;
374 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile);
376 com_err(__func__, retval, 0);
381 printf("Allocated inode: %u\n", newfile);
383 retval = ext2fs_link(fs, cwd, dest, newfile,
385 if (retval == EXT2_ET_DIR_NO_SPACE) {
386 retval = ext2fs_expand_dir(fs, cwd);
388 com_err(__func__, retval, "while expanding directory");
392 retval = ext2fs_link(fs, cwd, dest, newfile,
396 com_err(dest, retval, 0);
400 if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile))
401 com_err(__func__, 0, "Warning: inode already set");
402 ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
403 memset(&inode, 0, sizeof(inode));
404 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
405 inode.i_atime = inode.i_ctime = inode.i_mtime =
406 fs->now ? fs->now : time(0);
407 inode.i_links_count = 1;
408 inode.i_size = statbuf.st_size;
409 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
410 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
411 inode.i_flags |= EXT4_INLINE_DATA_FL;
412 } else if (fs->super->s_feature_incompat &
413 EXT3_FEATURE_INCOMPAT_EXTENTS) {
415 struct ext3_extent_header *eh;
417 eh = (struct ext3_extent_header *) &inode.i_block[0];
420 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
421 i = (sizeof(inode.i_block) - sizeof(*eh)) /
422 sizeof(struct ext3_extent);
423 eh->eh_max = ext2fs_cpu_to_le16(i);
424 inode.i_flags |= EXT4_EXTENTS_FL;
427 retval = ext2fs_write_new_inode(fs, newfile, &inode);
429 com_err(__func__, retval, "while creating inode %u", newfile);
433 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
434 retval = ext2fs_inline_data_init(fs, newfile);
436 com_err("copy_file", retval, 0);
441 if (LINUX_S_ISREG(inode.i_mode)) {
442 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
445 * Use I/O blocksize as buffer size when
446 * copying sparse files.
448 bufsize = statbuf.st_blksize;
450 retval = copy_file(fs, fd, newfile, bufsize, make_holes);
452 com_err("copy_file", retval, 0);
459 /* Copy files from source_dir to fs */
460 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
461 const char *source_dir, ext2_ino_t root)
467 char ln_target[PATH_MAX];
468 unsigned int save_inode;
474 if (chdir(source_dir) < 0) {
475 com_err(__func__, errno,
476 _("while changing working directory to \"%s\""),
481 if (!(dh = opendir("."))) {
482 com_err(__func__, errno,
483 _("while opening directory \"%s\""), source_dir);
487 while ((dent = readdir(dh))) {
488 if ((!strcmp(dent->d_name, ".")) ||
489 (!strcmp(dent->d_name, "..")))
491 lstat(dent->d_name, &st);
494 /* Check for hardlinks */
496 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
498 hdlink = is_hardlink(st.st_ino);
500 retval = add_link(fs, parent_ino,
501 hdlinks.hdl[hdlink].dst_ino,
504 com_err(__func__, retval,
505 "while linking %s", name);
513 switch(st.st_mode & S_IFMT) {
517 retval = do_mknod_internal(fs, parent_ino, name, &st);
519 com_err(__func__, retval,
520 _("while creating special file "
526 /* FIXME: there is no make socket function atm. */
528 _("ignoring socket file \"%s\""), name);
531 read_cnt = readlink(name, ln_target,
533 if (read_cnt == -1) {
534 com_err(__func__, errno,
535 _("while trying to readlink \"%s\""),
539 ln_target[read_cnt] = '\0';
540 retval = do_symlink_internal(fs, parent_ino, name,
543 com_err(__func__, retval,
544 _("while writing symlink\"%s\""),
550 retval = do_write_internal(fs, parent_ino, name, name,
553 com_err(__func__, retval,
554 _("while writing file \"%s\""), name);
559 retval = do_mkdir_internal(fs, parent_ino, name, &st,
562 com_err(__func__, retval,
563 _("while making dir \"%s\""), name);
566 retval = ext2fs_namei(fs, root, parent_ino,
569 com_err(name, retval, 0);
572 /* Populate the dir recursively*/
573 retval = populate_fs(fs, ino, name, root);
575 com_err(__func__, retval,
576 _("while adding dir \"%s\""), name);
580 com_err(__func__, errno,
587 _("ignoring entry \"%s\""), name);
590 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
592 com_err(name, retval, 0);
596 retval = set_inode_extra(fs, parent_ino, ino, &st);
598 com_err(__func__, retval,
599 _("while setting inode for \"%s\""), name);
603 /* Save the hardlink ino */
606 * Check whether need more memory, and we don't need
607 * free() since the lifespan will be over after the fs
610 if (hdlinks.count == hdlink_cnt) {
611 if ((hdlinks.hdl = realloc (hdlinks.hdl,
612 (hdlink_cnt + HDLINK_CNT) *
613 sizeof (struct hdlink_s))) == NULL) {
614 com_err(name, errno, "Not enough memory");
617 hdlink_cnt += HDLINK_CNT;
619 hdlinks.hdl[hdlinks.count].src_ino = st.st_ino;
620 hdlinks.hdl[hdlinks.count].dst_ino = ino;