1 #include "create_inode.h"
3 #if __STDC_VERSION__ < 199901L
5 # define __func__ __FUNCTION__
7 # define __func__ "<unknown>"
11 /* 64KiB is the minimium blksize to best minimize system call overhead. */
13 #define IO_BUFSIZE 64*1024
16 /* Block size for `st_blocks' */
21 /* For saving the hard links */
22 int hdlink_cnt = HDLINK_CNT;
24 /* Link an inode number to a directory */
25 static errcode_t add_link(ext2_ino_t parent_ino, ext2_ino_t ino, const char *name)
27 struct ext2_inode inode;
30 retval = ext2fs_read_inode(current_fs, ino, &inode);
32 com_err(__func__, retval, "while reading inode %u", ino);
36 retval = ext2fs_link(current_fs, parent_ino, name, ino, inode.i_flags);
37 if (retval == EXT2_ET_DIR_NO_SPACE) {
38 retval = ext2fs_expand_dir(current_fs, parent_ino);
40 com_err(__func__, retval, "while expanding directory");
43 retval = ext2fs_link(current_fs, parent_ino, name, ino, inode.i_flags);
46 com_err(__func__, retval, "while linking %s", name);
50 inode.i_links_count++;
52 retval = ext2fs_write_inode(current_fs, ino, &inode);
54 com_err(__func__, retval, "while writing inode %u", ino);
59 /* Fill the uid, gid, mode and time for the inode */
60 static void fill_inode(struct ext2_inode *inode, struct stat *st)
63 inode->i_uid = st->st_uid;
64 inode->i_gid = st->st_gid;
65 inode->i_mode |= st->st_mode;
66 inode->i_atime = st->st_atime;
67 inode->i_mtime = st->st_mtime;
68 inode->i_ctime = st->st_ctime;
72 /* Set the uid, gid, mode and time for the inode */
73 errcode_t set_inode_extra(ext2_ino_t cwd, ext2_ino_t ino, struct stat *st)
76 struct ext2_inode inode;
78 retval = ext2fs_read_inode(current_fs, ino, &inode);
80 com_err(__func__, retval, "while reading inode %u", ino);
84 fill_inode(&inode, st);
86 retval = ext2fs_write_inode(current_fs, ino, &inode);
88 com_err(__func__, retval, "while writing inode %u", ino);
93 /* Make a special file which is block, character and fifo */
94 errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st)
98 struct ext2_inode inode;
99 unsigned long major, minor, mode;
102 switch(st->st_mode & S_IFMT) {
104 mode = LINUX_S_IFCHR;
105 filetype = EXT2_FT_CHRDEV;
108 mode = LINUX_S_IFBLK;
109 filetype = EXT2_FT_BLKDEV;
112 mode = LINUX_S_IFIFO;
113 filetype = EXT2_FT_FIFO;
117 if (!(current_fs->flags & EXT2_FLAG_RW)) {
118 com_err(__func__, 0, "Filesystem opened read/only");
121 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino);
123 com_err(__func__, retval, 0);
128 printf("Allocated inode: %u\n", ino);
130 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
131 if (retval == EXT2_ET_DIR_NO_SPACE) {
132 retval = ext2fs_expand_dir(current_fs, cwd);
134 com_err(__func__, retval, "while expanding directory");
137 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
140 com_err(name, retval, 0);
143 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino))
144 com_err(__func__, 0, "Warning: inode already set");
145 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
146 memset(&inode, 0, sizeof(inode));
148 inode.i_atime = inode.i_ctime = inode.i_mtime =
149 current_fs->now ? current_fs->now : time(0);
151 major = major(st->st_rdev);
152 minor = minor(st->st_rdev);
154 if ((major < 256) && (minor < 256)) {
155 inode.i_block[0] = major * 256 + minor;
156 inode.i_block[1] = 0;
158 inode.i_block[0] = 0;
159 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
161 inode.i_links_count = 1;
163 retval = ext2fs_write_new_inode(current_fs, ino, &inode);
165 com_err(__func__, retval, "while creating inode %u", ino);
170 /* Make a symlink name -> target */
171 errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
174 ext2_ino_t parent_ino;
176 struct ext2_inode inode;
179 cp = strrchr(name, '/');
182 if ((retval = ext2fs_namei(current_fs, root, cwd, name, &parent_ino))){
183 com_err(name, retval, 0);
191 retval = ext2fs_symlink(current_fs, parent_ino, 0, name, target);
192 if (retval == EXT2_ET_DIR_NO_SPACE) {
193 retval = ext2fs_expand_dir(current_fs, parent_ino);
195 com_err("do_symlink_internal", retval, "while expanding directory");
201 com_err("ext2fs_symlink", retval, 0);
207 /* Make a directory in the fs */
208 errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
211 ext2_ino_t parent_ino, ino;
213 struct ext2_inode inode;
216 cp = strrchr(name, '/');
219 if ((retval = ext2fs_namei(current_fs, root, cwd, name, &parent_ino))){
220 com_err(name, retval, 0);
228 retval = ext2fs_mkdir(current_fs, parent_ino, 0, name);
229 if (retval == EXT2_ET_DIR_NO_SPACE) {
230 retval = ext2fs_expand_dir(current_fs, parent_ino);
232 com_err(__func__, retval, "while expanding directory");
238 com_err("ext2fs_mkdir", retval, 0);
243 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
248 unsigned int written;
254 retval = ext2fs_file_open(current_fs, newfile,
255 EXT2_FILE_WRITE, &e2_file);
259 retval = ext2fs_get_mem(bufsize, &buf);
261 com_err("copy_file", retval, "can't allocate buffer\n");
265 /* This is used for checking whether the whole block is zero */
266 retval = ext2fs_get_memzero(bufsize, &zero_buf);
268 com_err("copy_file", retval, "can't allocate buffer\n");
269 ext2fs_free_mem(&buf);
274 got = read(fd, buf, bufsize);
285 /* Check whether all is zero */
286 cmp = memcmp(ptr, zero_buf, got);
288 /* The whole block is zero, make a hole */
289 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
298 retval = ext2fs_file_write(e2_file, ptr,
307 ext2fs_free_mem(&buf);
308 ext2fs_free_mem(&zero_buf);
309 retval = ext2fs_file_close(e2_file);
313 ext2fs_free_mem(&buf);
314 ext2fs_free_mem(&zero_buf);
315 (void) ext2fs_file_close(e2_file);
319 int is_hardlink(ext2_ino_t ino)
323 for(i = 0; i < hdlinks.count; i++) {
324 if(hdlinks.hdl[i].src_ino == ino)
330 /* Copy the native file to the fs */
331 errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
337 struct ext2_inode inode;
338 int bufsize = IO_BUFSIZE;
341 fd = open(src, O_RDONLY);
343 com_err(src, errno, 0);
346 if (fstat(fd, &statbuf) < 0) {
347 com_err(src, errno, 0);
352 retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile);
354 com_err(__func__, 0, "The file '%s' already exists\n", dest);
359 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
361 com_err(__func__, retval, 0);
366 printf("Allocated inode: %u\n", newfile);
368 retval = ext2fs_link(current_fs, cwd, dest, newfile,
370 if (retval == EXT2_ET_DIR_NO_SPACE) {
371 retval = ext2fs_expand_dir(current_fs, cwd);
373 com_err(__func__, retval, "while expanding directory");
377 retval = ext2fs_link(current_fs, cwd, dest, newfile,
381 com_err(dest, retval, 0);
385 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile))
386 com_err(__func__, 0, "Warning: inode already set");
387 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
388 memset(&inode, 0, sizeof(inode));
389 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
390 inode.i_atime = inode.i_ctime = inode.i_mtime =
391 current_fs->now ? current_fs->now : time(0);
392 inode.i_links_count = 1;
393 inode.i_size = statbuf.st_size;
394 if (EXT2_HAS_INCOMPAT_FEATURE(current_fs->super,
395 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
396 inode.i_flags |= EXT4_INLINE_DATA_FL;
397 } else if (current_fs->super->s_feature_incompat &
398 EXT3_FEATURE_INCOMPAT_EXTENTS) {
400 struct ext3_extent_header *eh;
402 eh = (struct ext3_extent_header *) &inode.i_block[0];
405 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
406 i = (sizeof(inode.i_block) - sizeof(*eh)) /
407 sizeof(struct ext3_extent);
408 eh->eh_max = ext2fs_cpu_to_le16(i);
409 inode.i_flags |= EXT4_EXTENTS_FL;
412 if ((retval = ext2fs_write_new_inode(current_fs, newfile, &inode))) {
413 com_err(__func__, retval, "while creating inode %u", newfile);
417 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
418 retval = ext2fs_inline_data_init(current_fs, newfile);
422 if (LINUX_S_ISREG(inode.i_mode)) {
423 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
426 * Use I/O blocksize as buffer size when
427 * copying sparse files.
429 bufsize = statbuf.st_blksize;
431 retval = copy_file(fd, newfile, bufsize, make_holes);
433 com_err("copy_file", retval, 0);
440 /* Copy files from source_dir to fs */
441 errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
447 char ln_target[PATH_MAX];
448 unsigned int save_inode;
454 root = EXT2_ROOT_INO;
456 if (chdir(source_dir) < 0) {
457 com_err(__func__, errno,
458 _("while changing working directory to \"%s\""), source_dir);
462 if (!(dh = opendir("."))) {
463 com_err(__func__, errno,
464 _("while openning directory \"%s\""), source_dir);
468 while((dent = readdir(dh))) {
469 if((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
471 lstat(dent->d_name, &st);
474 /* Check for hardlinks */
476 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) && st.st_nlink > 1) {
477 hdlink = is_hardlink(st.st_ino);
479 retval = add_link(parent_ino,
480 hdlinks.hdl[hdlink].dst_ino, name);
482 com_err(__func__, retval, "while linking %s", name);
490 switch(st.st_mode & S_IFMT) {
494 if ((retval = do_mknod_internal(parent_ino, name, &st))) {
495 com_err(__func__, retval,
496 _("while creating special file \"%s\""), name);
501 /* FIXME: there is no make socket function atm. */
503 _("ignoring socket file \"%s\""), name);
506 if((read_cnt = readlink(name, ln_target, sizeof(ln_target))) == -1) {
507 com_err(__func__, errno,
508 _("while trying to readlink \"%s\""), name);
511 ln_target[read_cnt] = '\0';
512 if ((retval = do_symlink_internal(parent_ino, name, ln_target))) {
513 com_err(__func__, retval,
514 _("while writing symlink\"%s\""), name);
519 if ((retval = do_write_internal(parent_ino, name, name))) {
520 com_err(__func__, retval,
521 _("while writing file \"%s\""), name);
526 if ((retval = do_mkdir_internal(parent_ino, name, &st))) {
527 com_err(__func__, retval,
528 _("while making dir \"%s\""), name);
531 if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))) {
532 com_err(name, retval, 0);
535 /* Populate the dir recursively*/
536 retval = populate_fs(ino, name);
538 com_err(__func__, retval, _("while adding dir \"%s\""), name);
545 _("ignoring entry \"%s\""), name);
548 if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))){
549 com_err(name, retval, 0);
553 if ((retval = set_inode_extra(parent_ino, ino, &st))) {
554 com_err(__func__, retval,
555 _("while setting inode for \"%s\""), name);
559 /* Save the hardlink ino */
562 * Check whether need more memory, and we don't need
563 * free() since the lifespan will be over after the fs
566 if (hdlinks.count == hdlink_cnt) {
567 if ((hdlinks.hdl = realloc (hdlinks.hdl,
568 (hdlink_cnt + HDLINK_CNT) *
569 sizeof (struct hdlink_s))) == NULL) {
570 com_err(name, errno, "Not enough memory");
573 hdlink_cnt += HDLINK_CNT;
575 hdlinks.hdl[hdlinks.count].src_ino = st.st_ino;
576 hdlinks.hdl[hdlinks.count].dst_ino = ino;