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 /* Make a special file which is block, character and fifo */
22 errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st)
26 struct ext2_inode inode;
27 unsigned long major, minor, mode;
30 switch(st->st_mode & S_IFMT) {
33 filetype = EXT2_FT_CHRDEV;
37 filetype = EXT2_FT_BLKDEV;
41 filetype = EXT2_FT_FIFO;
45 if (!(current_fs->flags & EXT2_FLAG_RW)) {
46 com_err(__func__, 0, "Filesystem opened read/only");
49 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino);
51 com_err(__func__, retval, 0);
56 printf("Allocated inode: %u\n", ino);
58 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
59 if (retval == EXT2_ET_DIR_NO_SPACE) {
60 retval = ext2fs_expand_dir(current_fs, cwd);
62 com_err(__func__, retval, "while expanding directory");
65 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
68 com_err(name, retval, 0);
71 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino))
72 com_err(__func__, 0, "Warning: inode already set");
73 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
74 memset(&inode, 0, sizeof(inode));
76 inode.i_atime = inode.i_ctime = inode.i_mtime =
77 current_fs->now ? current_fs->now : time(0);
79 major = major(st->st_rdev);
80 minor = minor(st->st_rdev);
82 if ((major < 256) && (minor < 256)) {
83 inode.i_block[0] = major * 256 + minor;
87 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
89 inode.i_links_count = 1;
91 retval = ext2fs_write_new_inode(current_fs, ino, &inode);
93 com_err(__func__, retval, "while creating inode %u", ino);
98 /* Make a symlink name -> target */
99 errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
102 ext2_ino_t parent_ino;
104 struct ext2_inode inode;
107 cp = strrchr(name, '/');
110 if ((retval = ext2fs_namei(current_fs, root, cwd, name, &parent_ino))){
111 com_err(name, retval, 0);
119 retval = ext2fs_symlink(current_fs, parent_ino, 0, name, target);
120 if (retval == EXT2_ET_DIR_NO_SPACE) {
121 retval = ext2fs_expand_dir(current_fs, parent_ino);
123 com_err("do_symlink_internal", retval, "while expanding directory");
129 com_err("ext2fs_symlink", retval, 0);
135 /* Make a directory in the fs */
136 errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
140 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
145 unsigned int written;
151 retval = ext2fs_file_open(current_fs, newfile,
152 EXT2_FILE_WRITE, &e2_file);
156 retval = ext2fs_get_mem(bufsize, &buf);
158 com_err("copy_file", retval, "can't allocate buffer\n");
162 /* This is used for checking whether the whole block is zero */
163 retval = ext2fs_get_memzero(bufsize, &zero_buf);
165 com_err("copy_file", retval, "can't allocate buffer\n");
166 ext2fs_free_mem(&buf);
171 got = read(fd, buf, bufsize);
182 /* Check whether all is zero */
183 cmp = memcmp(ptr, zero_buf, got);
185 /* The whole block is zero, make a hole */
186 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
195 retval = ext2fs_file_write(e2_file, ptr,
204 ext2fs_free_mem(&buf);
205 ext2fs_free_mem(&zero_buf);
206 retval = ext2fs_file_close(e2_file);
210 ext2fs_free_mem(&buf);
211 ext2fs_free_mem(&zero_buf);
212 (void) ext2fs_file_close(e2_file);
216 /* Copy the native file to the fs */
217 errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
223 struct ext2_inode inode;
224 int bufsize = IO_BUFSIZE;
227 fd = open(src, O_RDONLY);
229 com_err(src, errno, 0);
232 if (fstat(fd, &statbuf) < 0) {
233 com_err(src, errno, 0);
238 retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile);
240 com_err(__func__, 0, "The file '%s' already exists\n", dest);
245 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
247 com_err(__func__, retval, 0);
252 printf("Allocated inode: %u\n", newfile);
254 retval = ext2fs_link(current_fs, cwd, dest, newfile,
256 if (retval == EXT2_ET_DIR_NO_SPACE) {
257 retval = ext2fs_expand_dir(current_fs, cwd);
259 com_err(__func__, retval, "while expanding directory");
263 retval = ext2fs_link(current_fs, cwd, dest, newfile,
267 com_err(dest, retval, 0);
271 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile))
272 com_err(__func__, 0, "Warning: inode already set");
273 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
274 memset(&inode, 0, sizeof(inode));
275 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
276 inode.i_atime = inode.i_ctime = inode.i_mtime =
277 current_fs->now ? current_fs->now : time(0);
278 inode.i_links_count = 1;
279 inode.i_size = statbuf.st_size;
280 if (current_fs->super->s_feature_incompat &
281 EXT3_FEATURE_INCOMPAT_EXTENTS) {
283 struct ext3_extent_header *eh;
285 eh = (struct ext3_extent_header *) &inode.i_block[0];
288 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
289 i = (sizeof(inode.i_block) - sizeof(*eh)) /
290 sizeof(struct ext3_extent);
291 eh->eh_max = ext2fs_cpu_to_le16(i);
292 inode.i_flags |= EXT4_EXTENTS_FL;
295 if ((retval = ext2fs_write_new_inode(current_fs, newfile, &inode))) {
296 com_err(__func__, retval, "while creating inode %u", newfile);
300 if (LINUX_S_ISREG(inode.i_mode)) {
301 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
304 * Use I/O blocksize as buffer size when
305 * copying sparse files.
307 bufsize = statbuf.st_blksize;
309 retval = copy_file(fd, newfile, bufsize, make_holes);
311 com_err("copy_file", retval, 0);
318 /* Copy files from source_dir to fs */
319 errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
325 char ln_target[PATH_MAX];
330 root = EXT2_ROOT_INO;
332 if (chdir(source_dir) < 0) {
333 com_err(__func__, errno,
334 _("while changing working directory to \"%s\""), source_dir);
338 if (!(dh = opendir("."))) {
339 com_err(__func__, errno,
340 _("while openning directory \"%s\""), source_dir);
344 while((dent = readdir(dh))) {
345 if((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
347 lstat(dent->d_name, &st);
350 switch(st.st_mode & S_IFMT) {
354 if ((retval = do_mknod_internal(parent_ino, name, &st))) {
355 com_err(__func__, retval,
356 _("while creating special file \"%s\""), name);
361 /* FIXME: there is no make socket function atm. */
363 _("ignoring socket file \"%s\""), name);
366 if((read_cnt = readlink(name, ln_target, sizeof(ln_target))) == -1) {
367 com_err(__func__, errno,
368 _("while trying to readlink \"%s\""), name);
371 ln_target[read_cnt] = '\0';
372 if ((retval = do_symlink_internal(parent_ino, name, ln_target))) {
373 com_err(__func__, retval,
374 _("while writing symlink\"%s\""), name);
379 if ((retval = do_write_internal(parent_ino, name, name))) {
380 com_err(__func__, retval,
381 _("while writing file \"%s\""), name);
386 if ((retval = do_mkdir_internal(parent_ino, name, &st))) {
387 com_err(__func__, retval,
388 _("while making dir \"%s\""), name);
391 if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))) {
392 com_err(name, retval, 0);
395 /* Populate the dir recursively*/
396 retval = populate_fs(ino, name);
398 com_err(__func__, retval, _("while adding dir \"%s\""), name);
405 _("ignoring entry \"%s\""), name);