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