Whamcloud - gitweb
filefrag: Lustre changes to filefrag FIEMAP handling
[tools/e2fsprogs.git] / misc / create_inode.c
index 51d25f8..a3a34cd 100644 (file)
 
 #include "config.h"
 #include <time.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <limits.h> /* for PATH_MAX */
+#include <dirent.h> /* for scandir() and alphasort() */
 #if defined HAVE_SYS_XATTR_H
 #include <sys/xattr.h>
 #elif defined HAVE_ATTR_XATTR_H
 #include <attr/xattr.h>
 #endif
+#ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
+#endif
 #ifdef HAVE_SYS_SYSMACROS_H
 #include <sys/sysmacros.h>
 #endif
@@ -117,8 +121,10 @@ static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t ino,
        }
 
        inode.i_uid = st->st_uid;
+       ext2fs_set_i_uid_high(inode, st->st_uid >> 16);
        inode.i_gid = st->st_gid;
-       inode.i_mode |= st->st_mode;
+       ext2fs_set_i_gid_high(inode, st->st_gid >> 16);
+       inode.i_mode = (LINUX_S_IFMT & inode.i_mode) | (~S_IFMT & st->st_mode);
        inode.i_atime = st->st_atime;
        inode.i_mtime = st->st_mtime;
        inode.i_ctime = st->st_ctime;
@@ -139,8 +145,13 @@ static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino,
        char                            *list = NULL;
        int                             i;
 
+       if (no_copy_xattrs)
+               return 0;
+
        size = llistxattr(filename, NULL, 0);
        if (size == -1) {
+               if (errno == ENOTSUP)
+                       return 0;
                retval = errno;
                com_err(__func__, retval, _("while listing attributes of \"%s\""),
                        filename);
@@ -157,6 +168,13 @@ static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino,
                return retval;
        }
 
+       retval = ext2fs_xattrs_read(handle);
+       if (retval) {
+               com_err(__func__, retval,
+                       _("while reading xattrs for inode %u"), ino);
+               goto out;
+       }
+
        retval = ext2fs_get_mem(size, &list);
        if (retval) {
                com_err(__func__, retval, _("while allocating memory"));
@@ -218,7 +236,6 @@ static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino,
                retval = retval ? retval : close_retval;
        }
        return retval;
-       return 0;
 }
 #else /* HAVE_LLISTXATTR */
 static errcode_t set_inode_xattr(ext2_filsys fs EXT2FS_ATTR((unused)),
@@ -229,9 +246,10 @@ static errcode_t set_inode_xattr(ext2_filsys fs EXT2FS_ATTR((unused)),
 }
 #endif  /* HAVE_LLISTXATTR */
 
+#ifndef _WIN32
 /* Make a special files (block and character devices), fifo's, and sockets  */
 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
-                           struct stat *st)
+                           unsigned int st_mode, unsigned int st_rdev)
 {
        ext2_ino_t              ino;
        errcode_t               retval;
@@ -239,7 +257,7 @@ errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
        unsigned long           devmajor, devminor, mode;
        int                     filetype;
 
-       switch(st->st_mode & S_IFMT) {
+       switch(st_mode & S_IFMT) {
        case S_IFCHR:
                mode = LINUX_S_IFCHR;
                filetype = EXT2_FT_CHRDEV;
@@ -252,10 +270,12 @@ errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
                mode = LINUX_S_IFIFO;
                filetype = EXT2_FT_FIFO;
                break;
+#ifndef _WIN32
        case S_IFSOCK:
                mode = LINUX_S_IFSOCK;
                filetype = EXT2_FT_SOCK;
                break;
+#endif
        default:
                return EXT2_ET_INVALID_ARGUMENT;
        }
@@ -293,8 +313,8 @@ errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
                fs->now ? fs->now : time(0);
 
        if (filetype != S_IFIFO) {
-               devmajor = major(st->st_rdev);
-               devminor = minor(st->st_rdev);
+               devmajor = major(st_rdev);
+               devminor = minor(st_rdev);
 
                if ((devmajor < 256) && (devminor < 256)) {
                        inode.i_block[0] = devmajor * 256 + devminor;
@@ -313,6 +333,7 @@ errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
 
        return retval;
 }
+#endif
 
 /* Make a symlink name -> target */
 errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
@@ -428,8 +449,8 @@ static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
                                ptr += blen;
                                continue;
                        }
-                       err = ext2fs_file_lseek(e2_file, off + bpos,
-                                               EXT2_SEEK_SET, NULL);
+                       err = ext2fs_file_llseek(e2_file, off + bpos,
+                                                EXT2_SEEK_SET, NULL);
                        if (err)
                                goto fail;
                        while (blen > 0) {
@@ -470,8 +491,9 @@ static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
                if (hole < 0)
                        return EXT2_ET_UNIMPLEMENTED;
 
-               data_blk = data & ~(fs->blocksize - 1);
-               hole_blk = (hole + (fs->blocksize - 1)) & ~(fs->blocksize - 1);
+               data_blk = data & ~(off_t)(fs->blocksize - 1);
+               hole_blk = ((hole + (off_t)(fs->blocksize - 1)) &
+                           ~(off_t)(fs->blocksize - 1));
                err = copy_file_chunk(fs, fd, e2_file, data_blk, hole_blk, buf,
                                      zerobuf);
                if (err)
@@ -605,9 +627,10 @@ errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
 {
        int             fd;
        struct stat     statbuf;
-       ext2_ino_t      newfile;
+       ext2_ino_t      newfile, parent_ino;
        errcode_t       retval;
        struct ext2_inode inode;
+       char            *cp;
 
        fd = ext2fs_open_file(src, O_RDONLY, 0);
        if (fd < 0) {
@@ -621,25 +644,37 @@ errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
                goto out;
        }
 
-       retval = ext2fs_namei(fs, root, cwd, dest, &newfile);
+       cp = strrchr(dest, '/');
+       if (cp) {
+               *cp = 0;
+               retval = ext2fs_namei(fs, root, cwd, dest, &parent_ino);
+               if (retval) {
+                       com_err(dest, retval, _("while looking up \"%s\""),
+                               dest);
+                       goto out;
+               }
+               dest = cp+1;
+       } else
+               parent_ino = cwd;
+
+       retval = ext2fs_namei(fs, root, parent_ino, dest, &newfile);
        if (retval == 0) {
                retval = EXT2_ET_FILE_EXISTS;
                goto out;
        }
 
-       retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile);
+       retval = ext2fs_new_inode(fs, parent_ino, 010755, 0, &newfile);
        if (retval)
                goto out;
 #ifdef DEBUGFS
        printf("Allocated inode: %u\n", newfile);
 #endif
-       retval = ext2fs_link(fs, cwd, dest, newfile,
-                               EXT2_FT_REG_FILE);
+       retval = ext2fs_link(fs, parent_ino, dest, newfile, EXT2_FT_REG_FILE);
        if (retval == EXT2_ET_DIR_NO_SPACE) {
-               retval = ext2fs_expand_dir(fs, cwd);
+               retval = ext2fs_expand_dir(fs, parent_ino);
                if (retval)
                        goto out;
-               retval = ext2fs_link(fs, cwd, dest, newfile,
+               retval = ext2fs_link(fs, parent_ino, dest, newfile,
                                        EXT2_FT_REG_FILE);
        }
        if (retval)
@@ -648,7 +683,7 @@ errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
                com_err(__func__, 0, "Warning: inode already set");
        ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
        memset(&inode, 0, sizeof(inode));
-       inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
+       inode.i_mode = (statbuf.st_mode & ~S_IFMT) | LINUX_S_IFREG;
        inode.i_atime = inode.i_ctime = inode.i_mtime =
                fs->now ? fs->now : time(0);
        inode.i_links_count = 1;
@@ -694,17 +729,77 @@ struct file_info {
 static errcode_t path_append(struct file_info *target, const char *file)
 {
        if (strlen(file) + target->path_len + 1 > target->path_max_len) {
+               void *p;
                target->path_max_len *= 2;
-               target->path = realloc(target->path, target->path_max_len);
-               if (!target->path)
+               p = realloc(target->path, target->path_max_len);
+               if (p == NULL)
                        return EXT2_ET_NO_MEMORY;
+               target->path = p;
        }
        target->path_len += sprintf(target->path + target->path_len, "/%s",
                                    file);
        return 0;
 }
 
-/* Copy files from source_dir to fs */
+#ifdef _WIN32
+static int scandir(const char *dir_name, struct dirent ***name_list,
+                  int (*filter)(const struct dirent*),
+                  int (*compar)(const struct dirent**, const struct dirent**)) {
+       DIR *dir;
+       struct dirent *dent;
+       struct dirent **temp_list = NULL;
+       size_t temp_list_size = 0; // unit: num of dirent
+       size_t num_dent = 0;
+
+       dir = opendir(dir_name);
+       if (dir == NULL) {
+               return -1;
+       }
+
+       while ((dent = readdir(dir))) {
+               if (filter != NULL && !(*filter)(dent))
+                       continue;
+
+               // re-allocate the list
+               if (num_dent == temp_list_size) {
+                       size_t new_list_size = temp_list_size + 32;
+                       struct dirent **new_list = (struct dirent**)realloc(
+                               temp_list, new_list_size * sizeof(struct dirent*));
+                       if (new_list == NULL)
+                               goto out_err;
+                       temp_list_size = new_list_size;
+                       temp_list = new_list;
+               }
+               // add the copy of dirent to the list
+               temp_list[num_dent] = (struct dirent*)malloc((dent->d_reclen + 3) & ~3);
+               if (!temp_list[num_dent])
+                       goto out_err;
+               memcpy(temp_list[num_dent], dent, dent->d_reclen);
+               num_dent++;
+       }
+       closedir(dir);
+
+       if (compar != NULL) {
+               qsort(temp_list, num_dent, sizeof(struct dirent*),
+                     (int (*)(const void*, const void*))compar);
+       }
+       *name_list = temp_list;
+       return num_dent;
+
+out_err:
+       closedir(dir);
+       while (num_dent > 0)
+               free(temp_list[--num_dent]);
+       free(temp_list);
+       return -1;
+}
+
+static int alphasort(const struct dirent **a, const struct dirent **b) {
+       return strcoll((*a)->d_name, (*b)->d_name);
+}
+#endif
+
+/* Copy files from source_dir to fs in alphabetical order */
 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                               const char *source_dir, ext2_ino_t root,
                               struct hdlinks_s *hdlinks,
@@ -712,16 +807,14 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                               struct fs_ops_callbacks *fs_callbacks)
 {
        const char      *name;
-       DIR             *dh;
-       struct dirent   *dent;
+       struct dirent   **dent;
        struct stat     st;
-       char            *ln_target = NULL;
        unsigned int    save_inode;
        ext2_ino_t      ino;
        errcode_t       retval = 0;
-       int             read_cnt;
        int             hdlink;
        size_t          cur_dir_path_len;
+       int             i, num_dents;
 
        if (chdir(source_dir) < 0) {
                retval = errno;
@@ -731,24 +824,25 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                return retval;
        }
 
-       if (!(dh = opendir("."))) {
+       num_dents = scandir(".", &dent, NULL, alphasort);
+
+       if (num_dents < 0) {
                retval = errno;
                com_err(__func__, retval,
-                       _("while opening directory \"%s\""), source_dir);
+                       _("while scanning directory \"%s\""), source_dir);
                return retval;
        }
 
-       while ((dent = readdir(dh))) {
-               if ((!strcmp(dent->d_name, ".")) ||
-                   (!strcmp(dent->d_name, "..")))
+       for (i = 0; i < num_dents; free(dent[i]), i++) {
+               name = dent[i]->d_name;
+               if ((!strcmp(name, ".")) || (!strcmp(name, "..")))
                        continue;
-               if (lstat(dent->d_name, &st)) {
+               if (lstat(name, &st)) {
                        retval = errno;
                        com_err(__func__, retval, _("while lstat \"%s\""),
-                               dent->d_name);
+                               name);
                        goto out;
                }
-               name = dent->d_name;
 
                /* Check for hardlinks */
                save_inode = 0;
@@ -771,8 +865,11 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
 
                cur_dir_path_len = target->path_len;
                retval = path_append(target, name);
-               if (retval)
-                       return retval;
+               if (retval) {
+                       com_err(__func__, retval,
+                               "while appending %s", name);
+                       goto out;
+               }
 
                if (fs_callbacks && fs_callbacks->create_new_inode) {
                        retval = fs_callbacks->create_new_inode(fs,
@@ -786,8 +883,10 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                case S_IFCHR:
                case S_IFBLK:
                case S_IFIFO:
+#ifndef _WIN32
                case S_IFSOCK:
-                       retval = do_mknod_internal(fs, parent_ino, name, &st);
+                       retval = do_mknod_internal(fs, parent_ino, name,
+                                                  st.st_mode, st.st_rdev);
                        if (retval) {
                                com_err(__func__, retval,
                                        _("while creating special file "
@@ -795,7 +894,10 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                                goto out;
                        }
                        break;
-               case S_IFLNK:
+               case S_IFLNK: {
+                       char *ln_target;
+                       int read_cnt;
+
                        ln_target = malloc(st.st_size + 1);
                        if (ln_target == NULL) {
                                com_err(__func__, retval,
@@ -830,6 +932,8 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                                goto out;
                        }
                        break;
+               }
+#endif /* !_WIN32 */
                case S_IFREG:
                        retval = do_write_internal(fs, parent_ino, name, name,
                                                   root);
@@ -934,7 +1038,8 @@ find_lnf:
        }
 
 out:
-       closedir(dh);
+       for (; i < num_dents; free(dent[i]), i++);
+       free(dent);
        return retval;
 }
 
@@ -964,9 +1069,17 @@ errcode_t populate_fs2(ext2_filsys fs, ext2_ino_t parent_ino,
        file_info.path_max_len = 255;
        file_info.path = calloc(file_info.path_max_len, 1);
 
+       retval = set_inode_xattr(fs, root, source_dir);
+       if (retval) {
+               com_err(__func__, retval,
+                       _("while copying xattrs on root directory"));
+               goto out;
+       }
+
        retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks,
                               &file_info, fs_callbacks);
 
+out:
        free(file_info.path);
        free(hdlinks.hdl);
        return retval;