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