blob: 552e542a62619e5101976c65392014fb27e79f8b [file] [log] [blame]
Mike Frysinger1fd98e02005-05-09 22:10:42 +00001/*
2 * getsize.c --- get the size of a partition.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 *
Mike Frysinger1fd98e02005-05-09 22:10:42 +00004 * Copyright (C) 1995, 1995 Theodore Ts'o.
5 * Copyright (C) 2003 VMware, Inc.
6 *
7 * Windows version of ext2fs_get_device_size by Chris Li, VMware.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00008 *
Mike Frysinger1fd98e02005-05-09 22:10:42 +00009 * %Begin-Header%
10 * This file may be redistributed under the terms of the GNU Public
11 * License.
12 * %End-Header%
13 */
14
15#include <stdio.h>
16#if HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19#if HAVE_ERRNO_H
20#include <errno.h>
21#endif
22#include <fcntl.h>
23#ifdef HAVE_SYS_IOCTL_H
24#include <sys/ioctl.h>
25#endif
26#ifdef HAVE_LINUX_FD_H
27#include <linux/fd.h>
28#endif
29#ifdef HAVE_SYS_DISKLABEL_H
30#include <sys/disklabel.h>
31#endif
32#ifdef HAVE_SYS_DISK_H
33#ifdef HAVE_SYS_QUEUE_H
34#include <sys/queue.h> /* for LIST_HEAD */
35#endif
36#include <sys/disk.h>
37#endif
38#ifdef __linux__
39#include <sys/utsname.h>
40#endif
41
42#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
43#define BLKGETSIZE _IO(0x12,96) /* return device size */
44#endif
45
46#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
47#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
48#endif
49
50#ifdef APPLE_DARWIN
51#define BLKGETSIZE DKIOCGETBLOCKCOUNT32
52#endif /* APPLE_DARWIN */
53
54#include "ext2_fs.h"
55#include "ext2fs.h"
56
57#if defined(__CYGWIN__) || defined (WIN32)
Rob Landley75cc0ca2006-02-24 03:15:34 +000058#include <windows.h>
59#include <winioctl.h>
Mike Frysinger1fd98e02005-05-09 22:10:42 +000060
61#if (_WIN32_WINNT >= 0x0500)
62#define HAVE_GET_FILE_SIZE_EX 1
63#endif
64
65errcode_t ext2fs_get_device_size(const char *file, int blocksize,
66 blk_t *retblocks)
67{
68 HANDLE dev;
69 PARTITION_INFORMATION pi;
70 DISK_GEOMETRY gi;
71 DWORD retbytes;
72#ifdef HAVE_GET_FILE_SIZE_EX
73 LARGE_INTEGER filesize;
74#else
75 DWORD filesize;
76#endif /* HAVE_GET_FILE_SIZE_EX */
77
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000078 dev = CreateFile(file, GENERIC_READ,
Mike Frysinger1fd98e02005-05-09 22:10:42 +000079 FILE_SHARE_READ | FILE_SHARE_WRITE ,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000080 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
81
Mike Frysinger1fd98e02005-05-09 22:10:42 +000082 if (dev == INVALID_HANDLE_VALUE)
83 return EBADF;
84 if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
85 &pi, sizeof(PARTITION_INFORMATION),
86 &pi, sizeof(PARTITION_INFORMATION),
87 &retbytes, NULL)) {
88
89 *retblocks = pi.PartitionLength.QuadPart / blocksize;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000090
Mike Frysinger1fd98e02005-05-09 22:10:42 +000091 } else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
92 &gi, sizeof(DISK_GEOMETRY),
93 &gi, sizeof(DISK_GEOMETRY),
94 &retbytes, NULL)) {
95
96 *retblocks = gi.BytesPerSector *
97 gi.SectorsPerTrack *
98 gi.TracksPerCylinder *
99 gi.Cylinders.QuadPart / blocksize;
100
101#ifdef HAVE_GET_FILE_SIZE_EX
102 } else if (GetFileSizeEx(dev, &filesize)) {
103 *retblocks = filesize.QuadPart / blocksize;
104 }
105#else
106 } else {
107 filesize = GetFileSize(dev, NULL);
108 if (INVALID_FILE_SIZE != filesize) {
109 *retblocks = filesize / blocksize;
110 }
111 }
112#endif /* HAVE_GET_FILE_SIZE_EX */
113
114 CloseHandle(dev);
115 return 0;
116}
117
118#else
119
120static int valid_offset (int fd, ext2_loff_t offset)
121{
122 char ch;
123
124 if (ext2fs_llseek (fd, offset, 0) < 0)
125 return 0;
126 if (read (fd, &ch, 1) < 1)
127 return 0;
128 return 1;
129}
130
131/*
132 * Returns the number of blocks in a partition
133 */
134errcode_t ext2fs_get_device_size(const char *file, int blocksize,
135 blk_t *retblocks)
136{
137 int fd;
138 int valid_blkgetsize64 = 1;
139#ifdef __linux__
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000140 struct utsname ut;
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000141#endif
142 unsigned long long size64;
143 unsigned long size;
144 ext2_loff_t high, low;
145#ifdef FDGETPRM
146 struct floppy_struct this_floppy;
147#endif
148#ifdef HAVE_SYS_DISKLABEL_H
149 int part;
150 struct disklabel lab;
151 struct partition *pp;
152 char ch;
153#endif /* HAVE_SYS_DISKLABEL_H */
154
155#ifdef CONFIG_LFS
156 fd = open64(file, O_RDONLY);
157#else
158 fd = open(file, O_RDONLY);
159#endif
160 if (fd < 0)
161 return errno;
162
163#ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
164 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
165 if ((sizeof(*retblocks) < sizeof(unsigned long long))
166 && ((size64 / (blocksize / 512)) > 0xFFFFFFFF))
167 return EFBIG;
168 close(fd);
169 *retblocks = size64 / (blocksize / 512);
170 return 0;
171 }
172#endif
173
174#ifdef BLKGETSIZE64
175#ifdef __linux__
176 if ((uname(&ut) == 0) &&
177 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
178 (ut.release[2] < '6') && (ut.release[3] == '.')))
179 valid_blkgetsize64 = 0;
180#endif
181 if (valid_blkgetsize64 &&
182 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
183 if ((sizeof(*retblocks) < sizeof(unsigned long long))
184 && ((size64 / blocksize) > 0xFFFFFFFF))
185 return EFBIG;
186 close(fd);
187 *retblocks = size64 / blocksize;
188 return 0;
189 }
190#endif
191
192#ifdef BLKGETSIZE
193 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
194 close(fd);
195 *retblocks = size / (blocksize / 512);
196 return 0;
197 }
198#endif
199
200#ifdef FDGETPRM
201 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
202 close(fd);
203 *retblocks = this_floppy.size / (blocksize / 512);
204 return 0;
205 }
206#endif
207
208#ifdef HAVE_SYS_DISKLABEL_H
209#if defined(DIOCGMEDIASIZE)
210 {
211 off_t ms;
212 u_int bs;
213 if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
214 *retblocks = ms / blocksize;
215 return 0;
216 }
217 }
218#elif defined(DIOCGDINFO)
219 /* old disklabel interface */
220 part = strlen(file) - 1;
221 if (part >= 0) {
222 ch = file[part];
223 if (isdigit(ch))
224 part = 0;
225 else if (ch >= 'a' && ch <= 'h')
226 part = ch - 'a';
227 else
228 part = -1;
229 }
230 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
231 pp = &lab.d_partitions[part];
232 if (pp->p_size) {
233 close(fd);
234 *retblocks = pp->p_size / (blocksize / 512);
235 return 0;
236 }
237 }
238#endif /* defined(DIOCG*) */
239#endif /* HAVE_SYS_DISKLABEL_H */
240
241 /*
242 * OK, we couldn't figure it out by using a specialized ioctl,
243 * which is generally the best way. So do binary search to
244 * find the size of the partition.
245 */
246 low = 0;
247 for (high = 1024; valid_offset (fd, high); high *= 2)
248 low = high;
249 while (low < high - 1)
250 {
251 const ext2_loff_t mid = (low + high) / 2;
252
253 if (valid_offset (fd, mid))
254 low = mid;
255 else
256 high = mid;
257 }
258 valid_offset (fd, 0);
259 close(fd);
260 size64 = low + 1;
261 if ((sizeof(*retblocks) < sizeof(unsigned long long))
262 && ((size64 / blocksize) > 0xFFFFFFFF))
263 return EFBIG;
264 *retblocks = size64 / blocksize;
265 return 0;
266}
267
268#endif /* WIN32 */
269
270#ifdef DEBUG
271int main(int argc, char **argv)
272{
273 blk_t blocks;
274 int retval;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000275
Mike Frysinger1fd98e02005-05-09 22:10:42 +0000276 if (argc < 2) {
277 fprintf(stderr, "Usage: %s device\n", argv[0]);
278 exit(1);
279 }
280
281 retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
282 if (retval) {
283 com_err(argv[0], retval,
284 "while calling ext2fs_get_device_size");
285 exit(1);
286 }
287 printf("Device %s has %d 1k blocks.\n", argv[1], blocks);
288 exit(0);
289}
290#endif