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 /* Link an inode number to a directory */
25 static errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino,
26 ext2_ino_t ino, const char *name)
28 struct ext2_inode inode;
31 retval = ext2fs_read_inode(fs, ino, &inode);
33 com_err(__func__, retval, "while reading inode %u", ino);
37 retval = ext2fs_link(fs, parent_ino, name, ino, inode.i_flags);
38 if (retval == EXT2_ET_DIR_NO_SPACE) {
39 retval = ext2fs_expand_dir(fs, parent_ino);
41 com_err(__func__, retval, "while expanding directory");
44 retval = ext2fs_link(fs, parent_ino, name, ino, inode.i_flags);
47 com_err(__func__, retval, "while linking %s", name);
51 inode.i_links_count++;
53 retval = ext2fs_write_inode(fs, ino, &inode);
55 com_err(__func__, retval, "while writing inode %u", ino);
60 /* Fill the uid, gid, mode and time for the inode */
61 static void fill_inode(struct ext2_inode *inode, struct stat *st)
64 inode->i_uid = st->st_uid;
65 inode->i_gid = st->st_gid;
66 inode->i_mode |= st->st_mode;
67 inode->i_atime = st->st_atime;
68 inode->i_mtime = st->st_mtime;
69 inode->i_ctime = st->st_ctime;
73 /* Set the uid, gid, mode and time for the inode */
74 static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd,
75 ext2_ino_t ino, struct stat *st)
78 struct ext2_inode inode;
80 retval = ext2fs_read_inode(fs, ino, &inode);
82 com_err(__func__, retval, "while reading inode %u", ino);
86 fill_inode(&inode, st);
88 retval = ext2fs_write_inode(fs, ino, &inode);
90 com_err(__func__, retval, "while writing inode %u", ino);
94 /* Make a special files (block and character devices), fifo's, and sockets */
95 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
100 struct ext2_inode inode;
101 unsigned long major, minor, mode;
104 switch(st->st_mode & S_IFMT) {
106 mode = LINUX_S_IFCHR;
107 filetype = EXT2_FT_CHRDEV;
110 mode = LINUX_S_IFBLK;
111 filetype = EXT2_FT_BLKDEV;
114 mode = LINUX_S_IFIFO;
115 filetype = EXT2_FT_FIFO;
118 mode = LINUX_S_IFSOCK;
119 filetype = EXT2_FT_SOCK;
125 if (!(fs->flags & EXT2_FLAG_RW)) {
126 com_err(__func__, 0, "Filesystem opened read/only");
129 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
131 com_err(__func__, retval, 0);
136 printf("Allocated inode: %u\n", ino);
138 retval = ext2fs_link(fs, cwd, name, ino, filetype);
139 if (retval == EXT2_ET_DIR_NO_SPACE) {
140 retval = ext2fs_expand_dir(fs, cwd);
142 com_err(__func__, retval, "while expanding directory");
145 retval = ext2fs_link(fs, cwd, name, ino, filetype);
148 com_err(name, retval, 0);
151 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino))
152 com_err(__func__, 0, "Warning: inode already set");
153 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
154 memset(&inode, 0, sizeof(inode));
156 inode.i_atime = inode.i_ctime = inode.i_mtime =
157 fs->now ? fs->now : time(0);
159 major = major(st->st_rdev);
160 minor = minor(st->st_rdev);
162 if ((major < 256) && (minor < 256)) {
163 inode.i_block[0] = major * 256 + minor;
164 inode.i_block[1] = 0;
166 inode.i_block[0] = 0;
167 inode.i_block[1] = (minor & 0xff) | (major << 8) |
168 ((minor & ~0xff) << 12);
170 inode.i_links_count = 1;
172 retval = ext2fs_write_new_inode(fs, ino, &inode);
174 com_err(__func__, retval, "while creating inode %u", ino);
179 /* Make a symlink name -> target */
180 errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
181 char *target, ext2_ino_t root)
184 ext2_ino_t parent_ino;
186 struct ext2_inode inode;
189 cp = strrchr(name, '/');
192 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
194 com_err(name, retval, 0);
202 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
203 if (retval == EXT2_ET_DIR_NO_SPACE) {
204 retval = ext2fs_expand_dir(fs, parent_ino);
206 com_err("do_symlink_internal", retval,
207 "while expanding directory");
213 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);
254 static errcode_t copy_file(ext2_filsys fs, int fd, ext2_ino_t newfile,
255 int bufsize, int make_holes)
260 unsigned int written;
266 retval = ext2fs_file_open(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 static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino)
337 for (i = 0; i < hdlinks->count; i++) {
338 if (hdlinks->hdl[i].src_dev == dev &&
339 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 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
461 const char *source_dir, ext2_ino_t root,
462 struct hdlinks_s *hdlinks)
468 char ln_target[PATH_MAX];
469 unsigned int save_inode;
471 errcode_t retval = 0;
475 if (chdir(source_dir) < 0) {
476 com_err(__func__, errno,
477 _("while changing working directory to \"%s\""),
482 if (!(dh = opendir("."))) {
483 com_err(__func__, errno,
484 _("while opening directory \"%s\""), source_dir);
488 while ((dent = readdir(dh))) {
489 if ((!strcmp(dent->d_name, ".")) ||
490 (!strcmp(dent->d_name, "..")))
492 if (lstat(dent->d_name, &st)) {
493 com_err(__func__, errno, _("while lstat \"%s\""),
499 /* Check for hardlinks */
501 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
503 hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino);
505 retval = add_link(fs, parent_ino,
506 hdlinks->hdl[hdlink].dst_ino,
509 com_err(__func__, retval,
510 "while linking %s", name);
518 switch(st.st_mode & S_IFMT) {
523 retval = do_mknod_internal(fs, parent_ino, name, &st);
525 com_err(__func__, retval,
526 _("while creating special file "
532 read_cnt = readlink(name, ln_target,
533 sizeof(ln_target) - 1);
534 if (read_cnt == -1) {
535 com_err(__func__, errno,
536 _("while trying to readlink \"%s\""),
540 ln_target[read_cnt] = '\0';
541 retval = do_symlink_internal(fs, parent_ino, name,
544 com_err(__func__, retval,
545 _("while writing symlink\"%s\""),
551 retval = do_write_internal(fs, parent_ino, name, name,
554 com_err(__func__, retval,
555 _("while writing file \"%s\""), name);
560 retval = do_mkdir_internal(fs, parent_ino, name, &st,
563 com_err(__func__, retval,
564 _("while making dir \"%s\""), name);
567 retval = ext2fs_namei(fs, root, parent_ino,
570 com_err(name, retval, 0);
573 /* Populate the dir recursively*/
574 retval = __populate_fs(fs, ino, name, root, hdlinks);
576 com_err(__func__, retval,
577 _("while adding dir \"%s\""), name);
581 com_err(__func__, errno, _("during cd .."));
588 _("ignoring entry \"%s\""), name);
591 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
593 com_err(name, retval, 0);
597 retval = set_inode_extra(fs, parent_ino, ino, &st);
599 com_err(__func__, retval,
600 _("while setting inode for \"%s\""), name);
604 /* Save the hardlink ino */
607 * Check whether need more memory, and we don't need
608 * free() since the lifespan will be over after the fs
611 if (hdlinks->count == hdlinks->size) {
612 void *p = realloc(hdlinks->hdl,
613 (hdlinks->size + HDLINK_CNT) *
614 sizeof(struct hdlink_s));
617 _("Not enough memory"));
618 retval = EXT2_ET_NO_MEMORY;
622 hdlinks->size += HDLINK_CNT;
624 hdlinks->hdl[hdlinks->count].src_dev = st.st_dev;
625 hdlinks->hdl[hdlinks->count].src_ino = st.st_ino;
626 hdlinks->hdl[hdlinks->count].dst_ino = ino;
636 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
637 const char *source_dir, ext2_ino_t root)
639 struct hdlinks_s hdlinks;
643 hdlinks.size = HDLINK_CNT;
644 hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s));
645 if (hdlinks.hdl == NULL) {
646 com_err(__func__, errno, "Not enough memory");
650 retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks);