From c6928406b3d8b1437a379ab66da2b8fa389febbc Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Thu, 17 Apr 2003 22:06:46 -0400 Subject: [PATCH] Add Cygwin/Windows version of ext2fs_get_device_size() --- lib/ext2fs/ChangeLog | 4 ++++ lib/ext2fs/getsize.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/lib/ext2fs/ChangeLog b/lib/ext2fs/ChangeLog index 13423ed..8d79805 100644 --- a/lib/ext2fs/ChangeLog +++ b/lib/ext2fs/ChangeLog @@ -1,3 +1,7 @@ +2003-04-17 Theodore Ts'o + + * getsize.c: Add Cygwin/Windows version of ext2fs_get_device_size() + 2003-04-12 Theodore Ts'o * unix_io.c (raw_read_blk): Add Cygwin support (the Windows block diff --git a/lib/ext2fs/getsize.c b/lib/ext2fs/getsize.c index 5b5c633..39e471f 100644 --- a/lib/ext2fs/getsize.c +++ b/lib/ext2fs/getsize.c @@ -2,7 +2,10 @@ * getsize.c --- get the size of a partition. * * Copyright (C) 1995, 1995 Theodore Ts'o. + * Copyright (C) 2003 VMware, Inc. * + * Windows version of ext2fs_get_device_size by Chris Li, VMware. + * * %Begin-Header% * This file may be redistributed under the terms of the GNU Public * License. @@ -43,6 +46,48 @@ #include "ext2_fs.h" #include "ext2fs.h" +#if defined(__CYGWIN__) || defined (WIN32) +#include "windows.h" +#include "winioctl.h" + +errcode_t ext2fs_get_device_size(const char *file, int blocksize, + blk_t *retblocks) +{ + HANDLE dev; + PARTITION_INFORMATION pi; + DISK_GEOMETRY gi; + DWORD retbytes; + + dev = CreateFile(file, GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE , + NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + + if (dev == INVALID_HANDLE_VALUE) + return EBADF; + if (DeviceIoControl(dev, 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, + &gi, sizeof(DISK_GEOMETRY), + &gi, sizeof(DISK_GEOMETRY), + &retbytes, NULL)) { + + *retblocks = gi.BytesPerSector * + gi.SectorsPerTrack * + gi.TracksPerCylinder * + gi.Cylinders.QuadPart / blocksize; + } + + CloseHandle(dev); + return 0; +} + +#else + static int valid_offset (int fd, ext2_loff_t offset) { char ch; @@ -141,6 +186,8 @@ errcode_t ext2fs_get_device_size(const char *file, int blocksize, return 0; } +#endif /* WIN32 */ + #ifdef DEBUG int main(int argc, char **argv) { -- 1.8.3.1