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 file which is block, character and fifo */
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;
122 if (!(fs->flags & EXT2_FLAG_RW)) {
123 com_err(__func__, 0, "Filesystem opened read/only");
126 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
128 com_err(__func__, retval, 0);
133 printf("Allocated inode: %u\n", ino);
135 retval = ext2fs_link(fs, cwd, name, ino, filetype);
136 if (retval == EXT2_ET_DIR_NO_SPACE) {
137 retval = ext2fs_expand_dir(fs, cwd);
139 com_err(__func__, retval, "while expanding directory");
142 retval = ext2fs_link(fs, cwd, name, ino, filetype);
145 com_err(name, retval, 0);
148 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino))
149 com_err(__func__, 0, "Warning: inode already set");
150 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
151 memset(&inode, 0, sizeof(inode));
153 inode.i_atime = inode.i_ctime = inode.i_mtime =
154 fs->now ? 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(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_filsys fs, ext2_ino_t cwd, const char *name,
178 char *target, ext2_ino_t root)
181 ext2_ino_t parent_ino;
183 struct ext2_inode inode;
186 cp = strrchr(name, '/');
189 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
191 com_err(name, retval, 0);
199 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
200 if (retval == EXT2_ET_DIR_NO_SPACE) {
201 retval = ext2fs_expand_dir(fs, parent_ino);
203 com_err("do_symlink_internal", retval,
204 "while expanding directory");
210 com_err("ext2fs_symlink", retval, 0);
214 /* Make a directory in the fs */
215 errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
216 struct stat *st, ext2_ino_t root)
219 ext2_ino_t parent_ino, ino;
221 struct ext2_inode inode;
224 cp = strrchr(name, '/');
227 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
229 com_err(name, retval, 0);
237 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
238 if (retval == EXT2_ET_DIR_NO_SPACE) {
239 retval = ext2fs_expand_dir(fs, parent_ino);
241 com_err(__func__, retval, "while expanding directory");
247 com_err("ext2fs_mkdir", retval, 0);
251 static errcode_t copy_file(ext2_filsys fs, int fd, ext2_ino_t newfile,
252 int bufsize, int make_holes)
257 unsigned int written;
263 retval = ext2fs_file_open(fs, newfile,
264 EXT2_FILE_WRITE, &e2_file);
268 retval = ext2fs_get_mem(bufsize, &buf);
270 com_err("copy_file", retval, "can't allocate buffer\n");
274 /* This is used for checking whether the whole block is zero */
275 retval = ext2fs_get_memzero(bufsize, &zero_buf);
277 com_err("copy_file", retval, "can't allocate buffer\n");
278 ext2fs_free_mem(&buf);
283 got = read(fd, buf, bufsize);
294 /* Check whether all is zero */
295 cmp = memcmp(ptr, zero_buf, got);
297 /* The whole block is zero, make a hole */
298 retval = ext2fs_file_lseek(e2_file, got,
309 retval = ext2fs_file_write(e2_file, ptr,
318 ext2fs_free_mem(&buf);
319 ext2fs_free_mem(&zero_buf);
320 retval = ext2fs_file_close(e2_file);
324 ext2fs_free_mem(&buf);
325 ext2fs_free_mem(&zero_buf);
326 (void) ext2fs_file_close(e2_file);
330 static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino)
334 for (i = 0; i < hdlinks->count; i++) {
335 if (hdlinks->hdl[i].src_dev == dev &&
336 hdlinks->hdl[i].src_ino == ino)
342 /* Copy the native file to the fs */
343 errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
344 const char *dest, ext2_ino_t root)
350 struct ext2_inode inode;
351 int bufsize = IO_BUFSIZE;
354 fd = open(src, O_RDONLY);
356 com_err(src, errno, 0);
359 if (fstat(fd, &statbuf) < 0) {
360 com_err(src, errno, 0);
365 retval = ext2fs_namei(fs, root, cwd, dest, &newfile);
368 return EXT2_ET_FILE_EXISTS;
371 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile);
373 com_err(__func__, retval, 0);
378 printf("Allocated inode: %u\n", newfile);
380 retval = ext2fs_link(fs, cwd, dest, newfile,
382 if (retval == EXT2_ET_DIR_NO_SPACE) {
383 retval = ext2fs_expand_dir(fs, cwd);
385 com_err(__func__, retval, "while expanding directory");
389 retval = ext2fs_link(fs, cwd, dest, newfile,
393 com_err(dest, retval, 0);
397 if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile))
398 com_err(__func__, 0, "Warning: inode already set");
399 ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
400 memset(&inode, 0, sizeof(inode));
401 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
402 inode.i_atime = inode.i_ctime = inode.i_mtime =
403 fs->now ? fs->now : time(0);
404 inode.i_links_count = 1;
405 inode.i_size = statbuf.st_size;
406 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
407 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
408 inode.i_flags |= EXT4_INLINE_DATA_FL;
409 } else if (fs->super->s_feature_incompat &
410 EXT3_FEATURE_INCOMPAT_EXTENTS) {
412 struct ext3_extent_header *eh;
414 eh = (struct ext3_extent_header *) &inode.i_block[0];
417 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
418 i = (sizeof(inode.i_block) - sizeof(*eh)) /
419 sizeof(struct ext3_extent);
420 eh->eh_max = ext2fs_cpu_to_le16(i);
421 inode.i_flags |= EXT4_EXTENTS_FL;
424 retval = ext2fs_write_new_inode(fs, newfile, &inode);
426 com_err(__func__, retval, "while creating inode %u", newfile);
430 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
431 retval = ext2fs_inline_data_init(fs, newfile);
433 com_err("copy_file", retval, 0);
438 if (LINUX_S_ISREG(inode.i_mode)) {
439 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
442 * Use I/O blocksize as buffer size when
443 * copying sparse files.
445 bufsize = statbuf.st_blksize;
447 retval = copy_file(fs, fd, newfile, bufsize, make_holes);
449 com_err("copy_file", retval, 0);
456 /* Copy files from source_dir to fs */
457 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
458 const char *source_dir, ext2_ino_t root,
459 struct hdlinks_s *hdlinks)
465 char ln_target[PATH_MAX];
466 unsigned int save_inode;
472 if (chdir(source_dir) < 0) {
473 com_err(__func__, errno,
474 _("while changing working directory to \"%s\""),
479 if (!(dh = opendir("."))) {
480 com_err(__func__, errno,
481 _("while opening directory \"%s\""), source_dir);
485 while ((dent = readdir(dh))) {
486 if ((!strcmp(dent->d_name, ".")) ||
487 (!strcmp(dent->d_name, "..")))
489 lstat(dent->d_name, &st);
492 /* Check for hardlinks */
494 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
496 hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino);
498 retval = add_link(fs, parent_ino,
499 hdlinks->hdl[hdlink].dst_ino,
502 com_err(__func__, retval,
503 "while linking %s", name);
511 switch(st.st_mode & S_IFMT) {
515 retval = do_mknod_internal(fs, parent_ino, name, &st);
517 com_err(__func__, retval,
518 _("while creating special file "
524 /* FIXME: there is no make socket function atm. */
526 _("ignoring socket file \"%s\""), name);
529 read_cnt = readlink(name, ln_target,
531 if (read_cnt == -1) {
532 com_err(__func__, errno,
533 _("while trying to readlink \"%s\""),
537 ln_target[read_cnt] = '\0';
538 retval = do_symlink_internal(fs, parent_ino, name,
541 com_err(__func__, retval,
542 _("while writing symlink\"%s\""),
548 retval = do_write_internal(fs, parent_ino, name, name,
551 com_err(__func__, retval,
552 _("while writing file \"%s\""), name);
557 retval = do_mkdir_internal(fs, parent_ino, name, &st,
560 com_err(__func__, retval,
561 _("while making dir \"%s\""), name);
564 retval = ext2fs_namei(fs, root, parent_ino,
567 com_err(name, retval, 0);
570 /* Populate the dir recursively*/
571 retval = __populate_fs(fs, ino, name, root, hdlinks);
573 com_err(__func__, retval,
574 _("while adding dir \"%s\""), name);
578 com_err(__func__, errno,
585 _("ignoring entry \"%s\""), name);
588 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
590 com_err(name, retval, 0);
594 retval = set_inode_extra(fs, parent_ino, ino, &st);
596 com_err(__func__, retval,
597 _("while setting inode for \"%s\""), name);
601 /* Save the hardlink ino */
604 * Check whether need more memory, and we don't need
605 * free() since the lifespan will be over after the fs
608 if (hdlinks->count == hdlinks->size) {
609 void *p = realloc(hdlinks->hdl,
610 (hdlinks->size + HDLINK_CNT) *
611 sizeof(struct hdlink_s));
614 _("Not enough memory"));
618 hdlinks->size += HDLINK_CNT;
620 hdlinks->hdl[hdlinks->count].src_dev = st.st_dev;
621 hdlinks->hdl[hdlinks->count].src_ino = st.st_ino;
622 hdlinks->hdl[hdlinks->count].dst_ino = ino;
630 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
631 const char *source_dir, ext2_ino_t root)
633 struct hdlinks_s hdlinks;
637 hdlinks.size = HDLINK_CNT;
638 hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s));
639 if (hdlinks.hdl == NULL) {
640 com_err(__func__, errno, "Not enough memory");
644 retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks);