]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - misc/create_inode.c
AOSP: misc: use scandir with alphasort instead of readdir for consistency
[thirdparty/e2fsprogs.git] / misc / create_inode.c
index 13e96d03862e422681c009390c0471a3d9d6dbc7..0091b7237f414ca15d5670ba7aacaf17c4fd5c48 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 */
-#ifdef HAVE_ATTR_XATTR_H
+#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
+
 #include <ext2fs/ext2fs.h>
 #include <ext2fs/ext2_types.h>
 #include <ext2fs/fiemap.h>
 
 #include "create_inode.h"
-#include "nls-enable.h"
-
-#if __STDC_VERSION__ < 199901L
-# if __GNUC__ >= 2
-#  define __func__ __FUNCTION__
-# else
-#  define __func__ "<unknown>"
-# endif
-#endif
+#include "support/nls-enable.h"
 
-/* 64KiB is the minimium blksize to best minimize system call overhead. */
+/* 64KiB is the minimum blksize to best minimize system call overhead. */
 #define COPY_FILE_BUFLEN       65536
 
 static int ext2_file_type(unsigned int mode)
@@ -106,8 +108,8 @@ static errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino,
 }
 
 /* Set the uid, gid, mode and time for the inode */
-static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd,
-                                ext2_ino_t ino, struct stat *st)
+static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t ino,
+                                struct stat *st)
 {
        errcode_t               retval;
        struct ext2_inode       inode;
@@ -131,16 +133,19 @@ static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t cwd,
        return retval;
 }
 
+#ifdef HAVE_LLISTXATTR
 static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino,
                                 const char *filename)
 {
-#ifdef HAVE_LLISTXATTR
        errcode_t                       retval, close_retval;
        struct ext2_xattr_handle        *handle;
        ssize_t                         size, value_size;
        char                            *list = NULL;
        int                             i;
 
+       if (no_copy_xattrs)
+               return 0;
+
        size = llistxattr(filename, NULL, 0);
        if (size == -1) {
                retval = errno;
@@ -220,14 +225,21 @@ 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)),
+                                ext2_ino_t ino EXT2FS_ATTR((unused)),
+                                const char *filename EXT2FS_ATTR((unused)))
+{
        return 0;
-#endif  /* HAVE_LLISTXATTR */
 }
+#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;
@@ -235,7 +247,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;
@@ -248,10 +260,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;
        }
@@ -289,8 +303,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;
@@ -309,6 +323,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,
@@ -348,7 +363,7 @@ errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
 
 /* Make a directory in the fs */
 errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
-                           struct stat *st, ext2_ino_t root)
+                           ext2_ino_t root)
 {
        char                    *cp;
        ext2_ino_t              parent_ino;
@@ -385,7 +400,7 @@ errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
 }
 
 #if !defined HAVE_PREAD64 && !defined HAVE_PREAD
-static ssize_t my_pread(int fd, const void *buf, size_t count, off_t offset)
+static ssize_t my_pread(int fd, void *buf, size_t count, off_t offset)
 {
        if (lseek(fd, offset, SEEK_SET) < 0)
                return 0;
@@ -394,7 +409,7 @@ static ssize_t my_pread(int fd, const void *buf, size_t count, off_t offset)
 }
 #endif /* !defined HAVE_PREAD64 && !defined HAVE_PREAD */
 
-static errcode_t copy_file_range(ext2_filsys fs, int fd, ext2_file_t e2_file,
+static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
                                 off_t start, off_t end, char *buf,
                                 char *zerobuf)
 {
@@ -424,8 +439,8 @@ static errcode_t copy_file_range(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) {
@@ -446,13 +461,13 @@ fail:
        return err;
 }
 
+#if defined(SEEK_DATA) && defined(SEEK_HOLE)
 static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
                                ext2_file_t e2_file, char *buf, char *zerobuf)
 {
-#if defined(SEEK_DATA) && defined(SEEK_HOLE)
        off_t data = 0, hole;
        off_t data_blk, hole_blk;
-       errcode_t err;
+       errcode_t err = 0;
 
        /* Try to use SEEK_DATA and SEEK_HOLE */
        while (data < statbuf->st_size) {
@@ -466,9 +481,10 @@ 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);
-               err = copy_file_range(fs, fd, e2_file, data_blk, hole_blk, buf,
+               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)
                        return err;
@@ -477,15 +493,13 @@ static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
        }
 
        return err;
-#else
-       return EXT2_ET_UNIMPLEMENTED;
-#endif /* SEEK_DATA and SEEK_HOLE */
 }
+#endif /* SEEK_DATA and SEEK_HOLE */
 
+#if defined(FS_IOC_FIEMAP)
 static errcode_t try_fiemap_copy(ext2_filsys fs, int fd, ext2_file_t e2_file,
                                 char *buf, char *zerobuf)
 {
-#if defined(FS_IOC_FIEMAP)
 #define EXTENT_MAX_COUNT 512
        struct fiemap *fiemap_buf;
        struct fiemap_extent *ext_buf, *ext;
@@ -514,13 +528,14 @@ static errcode_t try_fiemap_copy(ext2_filsys fs, int fd, ext2_file_t e2_file,
                if (err < 0 && (errno == EOPNOTSUPP || errno == ENOTTY)) {
                        err = EXT2_ET_UNIMPLEMENTED;
                        goto out;
-               } else if (err < 0 || fiemap_buf->fm_mapped_extents == 0) {
+               } else if (err < 0) {
                        err = errno;
                        goto out;
-               }
+               } else if (fiemap_buf->fm_mapped_extents == 0)
+                       goto out;
                for (i = 0, ext = ext_buf; i < fiemap_buf->fm_mapped_extents;
                     i++, ext++) {
-                       err = copy_file_range(fs, fd, e2_file, ext->fe_logical,
+                       err = copy_file_chunk(fs, fd, e2_file, ext->fe_logical,
                                              ext->fe_logical + ext->fe_length,
                                              buf, zerobuf);
                        if (err)
@@ -539,10 +554,8 @@ static errcode_t try_fiemap_copy(ext2_filsys fs, int fd, ext2_file_t e2_file,
 out:
        ext2fs_free_mem(&fiemap_buf);
        return err;
-#else
-       return EXT2_ET_UNIMPLEMENTED;
-#endif /* FS_IOC_FIEMAP */
 }
+#endif /* FS_IOC_FIEMAP */
 
 static errcode_t copy_file(ext2_filsys fs, int fd, struct stat *statbuf,
                           ext2_ino_t ino)
@@ -563,15 +576,19 @@ static errcode_t copy_file(ext2_filsys fs, int fd, struct stat *statbuf,
        if (err)
                goto out;
 
+#if defined(SEEK_DATA) && defined(SEEK_HOLE)
        err = try_lseek_copy(fs, fd, statbuf, e2_file, buf, zerobuf);
        if (err != EXT2_ET_UNIMPLEMENTED)
                goto out;
+#endif
 
+#if defined(FS_IOC_FIEMAP)
        err = try_fiemap_copy(fs, fd, e2_file, buf, zerobuf);
        if (err != EXT2_ET_UNIMPLEMENTED)
                goto out;
+#endif
 
-       err = copy_file_range(fs, fd, e2_file, 0, statbuf->st_size, buf,
+       err = copy_file_chunk(fs, fd, e2_file, 0, statbuf->st_size, buf,
                              zerobuf);
 out:
        ext2fs_free_mem(&zerobuf);
@@ -650,11 +667,9 @@ errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
        retval = ext2fs_inode_size_set(fs, &inode, statbuf.st_size);
        if (retval)
                goto out;
-       if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
-                                     EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
+       if (ext2fs_has_feature_inline_data(fs->super)) {
                inode.i_flags |= EXT4_INLINE_DATA_FL;
-       } else if (fs->super->s_feature_incompat &
-                  EXT3_FEATURE_INCOMPAT_EXTENTS) {
+       } else if (ext2fs_has_feature_extents(fs->super)) {
                ext2_extent_handle_t handle;
 
                inode.i_flags &= ~EXT4_EXTENTS_FL;
@@ -682,21 +697,107 @@ out:
        return retval;
 }
 
-/* Copy files from source_dir to fs */
+struct file_info {
+       char *path;
+       size_t path_len;
+       size_t path_max_len;
+};
+
+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;
+               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;
+}
+
+#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;
+                       }
+                       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);
+               memcpy(temp_list[num_dent], dent, dent->d_reclen);
+               num_dent++;
+       }
+
+       if (compar != NULL) {
+               qsort(temp_list, num_dent, sizeof(struct dirent*),
+                     (int (*)(const void*, const void*))compar);
+       }
+
+        // release the temp list
+       *name_list = temp_list;
+       temp_list = NULL;
+
+out:
+       if (temp_list != NULL) {
+               while (num_dent > 0) {
+                       free(temp_list[--num_dent]);
+               }
+               free(temp_list);
+               num_dent = -1;
+       }
+       closedir(dir);
+       return num_dent;
+}
+
+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)
+                              struct hdlinks_s *hdlinks,
+                              struct file_info *target,
+                              struct fs_ops_callbacks *fs_callbacks)
 {
        const char      *name;
-       DIR             *dh;
-       struct dirent   *dent;
+       struct dirent   **dent;
        struct stat     st;
-       char            ln_target[PATH_MAX];
+       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;
@@ -706,24 +807,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;
@@ -744,12 +846,30 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                                save_inode = 1;
                }
 
+               cur_dir_path_len = target->path_len;
+               retval = path_append(target, name);
+               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,
+                               target->path, name, parent_ino, root,
+                               st.st_mode & S_IFMT);
+                       if (retval)
+                               goto out;
+               }
+
                switch(st.st_mode & S_IFMT) {
                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 "
@@ -758,18 +878,33 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                        }
                        break;
                case S_IFLNK:
+                       ln_target = malloc(st.st_size + 1);
+                       if (ln_target == NULL) {
+                               com_err(__func__, retval,
+                                       _("malloc failed"));
+                               goto out;
+                       }
                        read_cnt = readlink(name, ln_target,
-                                           sizeof(ln_target) - 1);
+                                           st.st_size + 1);
                        if (read_cnt == -1) {
                                retval = errno;
                                com_err(__func__, retval,
                                        _("while trying to read link \"%s\""),
                                        name);
+                               free(ln_target);
+                               goto out;
+                       }
+                       if (read_cnt > st.st_size) {
+                               com_err(__func__, retval,
+                                       _("symlink increased in size "
+                                         "between lstat() and readlink()"));
+                               free(ln_target);
                                goto out;
                        }
                        ln_target[read_cnt] = '\0';
                        retval = do_symlink_internal(fs, parent_ino, name,
                                                     ln_target, root);
+                       free(ln_target);
                        if (retval) {
                                com_err(__func__, retval,
                                        _("while writing symlink\"%s\""),
@@ -777,6 +912,7 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                                goto out;
                        }
                        break;
+#endif
                case S_IFREG:
                        retval = do_write_internal(fs, parent_ino, name, name,
                                                   root);
@@ -791,7 +927,7 @@ static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                        if (parent_ino == EXT2_ROOT_INO &&
                            strcmp(name, "lost+found") == 0)
                                goto find_lnf;
-                       retval = do_mkdir_internal(fs, parent_ino, name, &st,
+                       retval = do_mkdir_internal(fs, parent_ino, name,
                                                   root);
                        if (retval) {
                                com_err(__func__, retval,
@@ -806,7 +942,8 @@ find_lnf:
                                        goto out;
                        }
                        /* Populate the dir recursively*/
-                       retval = __populate_fs(fs, ino, name, root, hdlinks);
+                       retval = __populate_fs(fs, ino, name, root, hdlinks,
+                                              target, fs_callbacks);
                        if (retval)
                                goto out;
                        if (chdir("..")) {
@@ -828,7 +965,7 @@ find_lnf:
                        goto out;
                }
 
-               retval = set_inode_extra(fs, parent_ino, ino, &st);
+               retval = set_inode_extra(fs, ino, &st);
                if (retval) {
                        com_err(__func__, retval,
                                _("while setting inode for \"%s\""), name);
@@ -842,6 +979,14 @@ find_lnf:
                        goto out;
                }
 
+               if (fs_callbacks && fs_callbacks->end_create_new_inode) {
+                       retval = fs_callbacks->end_create_new_inode(fs,
+                               target->path, name, parent_ino, root,
+                               st.st_mode & S_IFMT);
+                       if (retval)
+                               goto out;
+               }
+
                /* Save the hardlink ino */
                if (save_inode) {
                        /*
@@ -867,16 +1012,21 @@ find_lnf:
                        hdlinks->hdl[hdlinks->count].dst_ino = ino;
                        hdlinks->count++;
                }
+               target->path_len = cur_dir_path_len;
+               target->path[target->path_len] = 0;
        }
 
 out:
-       closedir(dh);
+       for (; i < num_dents; free(dent[i]), i++);
+       free(dent);
        return retval;
 }
 
-errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
-                     const char *source_dir, ext2_ino_t root)
+errcode_t populate_fs2(ext2_filsys fs, ext2_ino_t parent_ino,
+                      const char *source_dir, ext2_ino_t root,
+                      struct fs_ops_callbacks *fs_callbacks)
 {
+       struct file_info file_info;
        struct hdlinks_s hdlinks;
        errcode_t retval;
 
@@ -894,8 +1044,20 @@ errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
                return retval;
        }
 
-       retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks);
+       file_info.path_len = 0;
+       file_info.path_max_len = 255;
+       file_info.path = calloc(file_info.path_max_len, 1);
+
+       retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks,
+                              &file_info, fs_callbacks);
 
+       free(file_info.path);
        free(hdlinks.hdl);
        return retval;
 }
+
+errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
+                     const char *source_dir, ext2_ino_t root)
+{
+       return populate_fs2(fs, parent_ino, source_dir, root, NULL);
+}