]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - misc/create_inode.c
AOSP: Fix file offset overflow issue when file's size > 4G
[thirdparty/e2fsprogs.git] / misc / create_inode.c
index 8f7445d027349c0449dc08f48fcac8c031337a77..b288935d79f4f288638ac5d54b27a6824a2fbe61 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
+#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
@@ -33,7 +38,7 @@
 #include "create_inode.h"
 #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)
@@ -137,6 +142,9 @@ 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) {
                retval = errno;
@@ -227,9 +235,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;
@@ -237,7 +246,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;
@@ -250,10 +259,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;
        }
@@ -291,8 +302,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;
@@ -311,6 +322,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,
@@ -396,7 +408,7 @@ static ssize_t my_pread(int fd, 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)
 {
@@ -426,8 +438,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) {
@@ -468,9 +480,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;
@@ -521,7 +534,7 @@ static errcode_t try_fiemap_copy(ext2_filsys fs, int fd, ext2_file_t e2_file,
                        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)
@@ -574,7 +587,7 @@ static errcode_t copy_file(ext2_filsys fs, int fd, struct stat *statbuf,
                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);
@@ -692,10 +705,12 @@ 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);
@@ -769,8 +784,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,
@@ -784,8 +802,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 "
@@ -828,6 +848,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);