2
2
* getsize.c --- get the size of a partition.
4
4
* Copyright (C) 1995, 1995 Theodore Ts'o.
5
* Copyright (C) 2003 VMware, Inc.
7
* Windows version of ext2fs_get_device_size by Chris Li, VMware.
7
10
* This file may be redistributed under the terms of the GNU Public
25
28
#include <linux/fd.h>
27
30
#ifdef HAVE_SYS_DISKLABEL_H
31
#include <sys/param.h> /* for __FreeBSD_version */
28
32
#include <sys/ioctl.h>
29
33
#include <sys/disklabel.h>
30
34
#endif /* HAVE_SYS_DISKLABEL_H */
35
#ifdef HAVE_SYS_DISK_H
36
#include <sys/queue.h> /* for LIST_HEAD */
38
#endif /* HAVE_SYS_DISK_H */
32
40
#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
33
41
#define BLKGETSIZE _IO(0x12,96) /* return device size */
45
#include <sys/ioctl.h>
48
#define BLKGETSIZE DKIOCGETBLOCKCOUNT32
49
#endif /* APPLE_DARWIN */
36
51
#include "ext2_fs.h"
37
52
#include "ext2fs.h"
54
#if defined(__CYGWIN__) || defined (WIN32)
58
errcode_t ext2fs_get_device_size(const char *file, int blocksize,
62
PARTITION_INFORMATION pi;
65
LARGE_INTEGER filesize;
67
dev = CreateFile(file, GENERIC_READ,
68
FILE_SHARE_READ | FILE_SHARE_WRITE ,
69
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
71
if (dev == INVALID_HANDLE_VALUE)
73
if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
74
&pi, sizeof(PARTITION_INFORMATION),
75
&pi, sizeof(PARTITION_INFORMATION),
78
*retblocks = pi.PartitionLength.QuadPart / blocksize;
80
} else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
81
&gi, sizeof(DISK_GEOMETRY),
82
&gi, sizeof(DISK_GEOMETRY),
85
*retblocks = gi.BytesPerSector *
87
gi.TracksPerCylinder *
88
gi.Cylinders.QuadPart / blocksize;
90
} else if (GetFileSizeEx(dev, &filesize)) {
91
*retblocks = filesize.QuadPart / blocksize;
39
100
static int valid_offset (int fd, ext2_loff_t offset)
175
#else /* __FreeBSD_version < 500040 */
179
if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
180
*retblocks = ms / blocksize;
184
#endif /* __FreeBSD_version < 500040 */
112
185
#endif /* HAVE_SYS_DISKLABEL_H */