Skip to contentSkip to conversation

libext2fs: fix ext2fs_get_device_size2() return value on Windows

PATCH Applied Eric Biggers 2 participants 3 messages Mar 1, 2023 – Apr 17, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libext2fs: fix ext2fs_get_device_size2() return value on Windows

From: Eric Biggers <[email protected]>

Creating a file system on Windows without a pre-existing file stopped working because the Windows version of ext2fs_get_device_size2() doesn't return ENOENT if the file doesn't exist. Fix this.

Fixes 53464654bd33 ("mke2fs: fix creating a file system image w/o a pre-existing file")
---
 .github/workflows/ci.yml |  1 -
 lib/ext2fs/getsize.c     | 31 +++++++++++--------------------
 lib/ext2fs/windows_io.c  | 11 -----------
 3 files changed, 11 insertions(+), 32 deletions(-)
.github/workflows/ci.yml +0 -1
index 51b27c88d..35496c573 100644
@@ -112,5 +112,4 @@ jobs:
     - run: make -j8 -C lib/support/ all V=1 CFLAGS_WARN="-Werror"
     - run: make -j8 -C lib/e2p/ all V=1 CFLAGS_WARN="-Werror"
     - run: make -j8 -C misc/ mke2fs V=1 CFLAGS_WARN="-Werror"
-    - run: touch image.ext4
     - run: misc/mke2fs.exe -T ext4 image.ext4 128M
lib/ext2fs/getsize.c +11 -20
index bcf30208e..a02863443 100644
@@ -71,12 +71,11 @@
 #define HAVE_GET_FILE_SIZE_EX 1
 #endif
 
-HANDLE windows_get_handle(io_channel channel);
-
 errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
 				  blk64_t *retblocks)
 {
-	HANDLE dev;
+	int fd;
+	HANDLE h;
 	PARTITION_INFORMATION pi;
 	DISK_GEOMETRY gi;
 	DWORD retbytes;
@@ -86,25 +85,18 @@ errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
 	DWORD filesize;
 #endif /* HAVE_GET_FILE_SIZE_EX */
 
-	io_channel data_io = 0;
-	int retval;
-
-	retval = windows_io_manager->open(file, 0, &data_io);
-	if (retval)
-		return retval;
-
-	dev = windows_get_handle(data_io);
-	if (dev == INVALID_HANDLE_VALUE)
-		return EBADF;
-
-	if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
+	fd = ext2fs_open_file(file, O_RDONLY, 0);
+	if (fd < 0)
+		return errno;
+	h = (HANDLE)_get_osfhandle(fd);
+	if (DeviceIoControl(h, IOCTL_DISK_GET_PARTITION_INFO,
 			    &pi, sizeof(PARTITION_INFORMATION),
 			    &pi, sizeof(PARTITION_INFORMATION),
 			    &retbytes, NULL)) {
 
 		*retblocks = pi.PartitionLength.QuadPart / blocksize;
 
-	} else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
+	} else if (DeviceIoControl(h, IOCTL_DISK_GET_DRIVE_GEOMETRY,
 				&gi, sizeof(DISK_GEOMETRY),
 				&gi, sizeof(DISK_GEOMETRY),
 				&retbytes, NULL)) {
@@ -115,20 +107,19 @@ errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
 			     gi.Cylinders.QuadPart / blocksize;
 
 #ifdef HAVE_GET_FILE_SIZE_EX
-	} else if (GetFileSizeEx(dev, &filesize)) {
+	} else if (GetFileSizeEx(h, &filesize)) {
 		*retblocks = filesize.QuadPart / blocksize;
 	}
 #else
 	} else {
-		filesize = GetFileSize(dev, NULL);
+		filesize = GetFileSize(h, NULL);
 		if (INVALID_FILE_SIZE != filesize) {
 			*retblocks = filesize / blocksize;
 		}
 	}
 #endif /* HAVE_GET_FILE_SIZE_EX */
 
-	windows_io_manager->close(data_io);
-
+	close(fd);
 	return 0;
 }
 
lib/ext2fs/windows_io.c +0 -11
index 83aea68b6..f01bbb6ad 100644
@@ -857,17 +857,6 @@ static errcode_t windows_write_byte(io_channel channel, unsigned long offset,
 	return EXT2_ET_UNIMPLEMENTED;
 }
 
-HANDLE windows_get_handle(io_channel channel)
-{
-	struct windows_private_data *data;
-
-	EXT2_CHECK_MAGIC_RETURN(channel, EXT2_ET_MAGIC_IO_CHANNEL, INVALID_HANDLE_VALUE);
-	data = (struct windows_private_data *) channel->private_data;
-	EXT2_CHECK_MAGIC_RETURN(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL, INVALID_HANDLE_VALUE);
-
-	return data->handle;
-}
-
 /*
  * Flush data buffers to disk.
  */

base-commit: 25ad8a431331b4d1d444a70b6079456cc612ac40

Signature · 1 line
2.39.2
#1 lore raw
Quoted 6 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Creating a file system on Windows without a pre-existing file stopped working because the Windows version of ext2fs_get_device_size2() doesn't return ENOENT if the file doesn't exist. Fix this.

Quoted 7 lines
Fixes 53464654bd33 ("mke2fs: fix creating a file system image w/o a pre-existing file")
---
 .github/workflows/ci.yml |  1 -
 lib/ext2fs/getsize.c     | 31 +++++++++++--------------------
 lib/ext2fs/windows_io.c  | 11 -----------
 3 files changed, 11 insertions(+), 32 deletions(-)

Ping.

#2 lore raw
In reply to Eric Biggers
Quoted 5 lines · Eric Biggers

Creating a file system on Windows without a pre-existing file stopped working because the Windows version of ext2fs_get_device_size2() doesn't return ENOENT if the file doesn't exist. Fix this.

Applied, thanks!

[1/1] libext2fs: fix ext2fs_get_device_size2() return value on Windows

      commit: 72e30620b1ebd4742a8cda6cd4220a5423ca3180

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>