From: Jin Qian Date: Mon, 24 Jul 2017 21:18:16 +0000 (-0700) Subject: AOSP: mke2fs, libext2fs: fix bugs on windows X-Git-Tag: v1.44.0-rc1~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2d545e3762636a1f5d4be5ee831c3d188d007ed5;p=thirdparty%2Fe2fsprogs.git AOSP: mke2fs, libext2fs: fix bugs on windows Added O_BINARY to open output files on windows, otherwise they're written as text files and have invalid data. Use '(filename):block_count:block_size' for sparse file name because windows file name can contain ':', e.g. 'c:\output_file'. Bug: 23686092 Change-Id: I731c13e5df0be8c831464c431b8949d33438fb24 From AOSP commit: 0dcf8ec6a429ce4f024fe7838fee2d5636e8ba4d --- diff --git a/contrib/android/e2fsdroid.c b/contrib/android/e2fsdroid.c index 1ae133d5e..223ea6874 100644 --- a/contrib/android/e2fsdroid.c +++ b/contrib/android/e2fsdroid.c @@ -118,9 +118,17 @@ int main(int argc, char *argv[]) fprintf(stderr, "Expected filename after options\n"); exit(EXIT_FAILURE); } - in_file = strdup(argv[optind]); - io_mgr = android_sparse_file ? sparse_io_manager: unix_io_manager; + if (android_sparse_file) { + io_mgr = sparse_io_manager; + if (asprintf(&in_file, "(%s)", argv[optind]) == -1) { + fprintf(stderr, "Failed to allocate file name\n"); + exit(EXIT_FAILURE); + } + } else { + io_mgr = unix_io_manager; + in_file = strdup(argv[optind]); + } retval = ext2fs_open(in_file, flags, 0, 0, io_mgr, &fs); if (retval) { com_err(prog_name, retval, "while opening file %s\n", in_file); diff --git a/lib/ext2fs/initialize.c b/lib/ext2fs/initialize.c index 32f43210a..99fedcd36 100644 --- a/lib/ext2fs/initialize.c +++ b/lib/ext2fs/initialize.c @@ -28,6 +28,10 @@ #include "ext2_fs.h" #include "ext2fs.h" +#ifndef O_BINARY +#define O_BINARY 0 +#endif + #if defined(__linux__) && defined(EXT2_OS_LINUX) #define CREATOR_OS EXT2_OS_LINUX #else @@ -124,6 +128,7 @@ errcode_t ext2fs_initialize(const char *name, int flags, io_flags |= IO_FLAG_EXCLUSIVE; if (flags & EXT2_FLAG_DIRECT_IO) io_flags |= IO_FLAG_DIRECT_IO; + io_flags |= O_BINARY; retval = manager->open(name, io_flags, &fs->io); if (retval) goto cleanup; diff --git a/lib/ext2fs/sparse_io.c b/lib/ext2fs/sparse_io.c index a307859f8..a83baf1f5 100644 --- a/lib/ext2fs/sparse_io.c +++ b/lib/ext2fs/sparse_io.c @@ -7,6 +7,10 @@ #include "ext2_fs.h" #include "ext2fs.h" +#ifndef O_BINARY +#define O_BINARY 0 +#endif + #if !defined(ENABLE_LIBSPARSE) static errcode_t sparse_open(const char *name EXT2FS_ATTR((unused)), int flags EXT2FS_ATTR((unused)), @@ -155,7 +159,7 @@ static errcode_t io_manager_configure(struct sparse_io_params *params, goto err_alloc; } if (params->fd < 0) { - sm->fd = open(params->file, O_CREAT | O_RDWR | O_TRUNC, + sm->fd = open(params->file, O_CREAT | O_RDWR | O_TRUNC | O_BINARY, 0644); if (sm->fd < 0) { retval = errno; @@ -206,11 +210,11 @@ static errcode_t read_sparse_argv(const char *name, bool is_fd, } if (is_fd) { - ret = sscanf(name, "%d:%llu:%u", &sparse_params->fd, + ret = sscanf(name, "(%d):%llu:%u", &sparse_params->fd, (unsigned long long *)&sparse_params->blocks_count, &sparse_params->block_size); } else { - ret = sscanf(name, "%[^:]%*[:]%llu%*[:]%u", sparse_params->file, + ret = sscanf(name, "(%[^)])%*[:]%llu%*[:]%u", sparse_params->file, (unsigned long long *)&sparse_params->blocks_count, &sparse_params->block_size); } diff --git a/misc/mke2fs.c b/misc/mke2fs.c index cfb10bc45..7e8248236 100644 --- a/misc/mke2fs.c +++ b/misc/mke2fs.c @@ -2854,7 +2854,7 @@ int main (int argc, char *argv[]) _("in malloc for android_sparse_params")); exit(1); } - snprintf(android_sparse_params, PATH_MAX + 32, "%s:%u:%u", + snprintf(android_sparse_params, PATH_MAX + 32, "(%s):%u:%u", device_name, fs_param.s_blocks_count, 1024 << fs_param.s_log_block_size); retval = ext2fs_initialize(android_sparse_params, flags,