Whamcloud - gitweb
Add Cygwin/Windows version of ext2fs_get_device_size()
authorTheodore Ts'o <tytso@mit.edu>
Fri, 18 Apr 2003 02:06:46 +0000 (22:06 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 18 Apr 2003 02:06:46 +0000 (22:06 -0400)
lib/ext2fs/ChangeLog
lib/ext2fs/getsize.c

index 13423ed..8d79805 100644 (file)
@@ -1,3 +1,7 @@
+2003-04-17  Theodore Ts'o  <tytso@mit.edu>
+
+       * getsize.c: Add Cygwin/Windows version of ext2fs_get_device_size()
+
 2003-04-12  Theodore Ts'o  <tytso@mit.edu>
 
        * unix_io.c (raw_read_blk): Add Cygwin support (the Windows block
index 5b5c633..39e471f 100644 (file)
@@ -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.
 #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)
 {