getsize.c revision c859cb1de0d624caa0779fb17d1a53766143136e
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 <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_size2(const char *file, int blocksize, 142 blk64_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 fd = ext2fs_open_file(file, O_RDONLY); 163 if (fd < 0) 164 return errno; 165 166#ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */ 167 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) { 168 *retblocks = size64 / (blocksize / 512); 169 goto out; 170 } 171#endif 172 173#ifdef BLKGETSIZE64 174#ifdef __linux__ 175 if ((uname(&ut) == 0) && 176 ((ut.release[0] == '2') && (ut.release[1] == '.') && 177 (ut.release[2] < '6') && (ut.release[3] == '.'))) 178 valid_blkgetsize64 = 0; 179#endif 180 if (valid_blkgetsize64 && 181 ioctl(fd, BLKGETSIZE64, &size64) >= 0) { 182 *retblocks = size64 / blocksize; 183 goto out; 184 } 185#endif 186 187#ifdef BLKGETSIZE 188 if (ioctl(fd, BLKGETSIZE, &size) >= 0) { 189 *retblocks = size / (blocksize / 512); 190 goto out; 191 } 192#endif 193 194#ifdef FDGETPRM 195 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) { 196 *retblocks = this_floppy.size / (blocksize / 512); 197 goto out; 198 } 199#endif 200 201#ifdef HAVE_SYS_DISKLABEL_H 202#if defined(DIOCGMEDIASIZE) 203 { 204 off_t ms; 205 u_int bs; 206 if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) { 207 *retblocks = ms / blocksize; 208 goto out; 209 } 210 } 211#elif defined(DIOCGDINFO) 212 /* old disklabel interface */ 213 part = strlen(file) - 1; 214 if (part >= 0) { 215 ch = file[part]; 216 if (isdigit(ch)) 217 part = 0; 218 else if (ch >= 'a' && ch <= 'h') 219 part = ch - 'a'; 220 else 221 part = -1; 222 } 223 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) { 224 pp = &lab.d_partitions[part]; 225 if (pp->p_size) { 226 *retblocks = pp->p_size / (blocksize / 512); 227 goto out; 228 } 229 } 230#endif /* defined(DIOCG*) */ 231#endif /* HAVE_SYS_DISKLABEL_H */ 232 233 { 234#if defined(HAVE_FSTAT64) && !defined(__OSX__) 235 struct stat64 st; 236 if (fstat64(fd, &st) == 0) 237#else 238 struct stat st; 239 if (fstat(fd, &st) == 0) 240#endif 241 if (S_ISREG(st.st_mode)) { 242 *retblocks = st.st_size / blocksize; 243 goto out; 244 } 245 } 246 247 /* 248 * OK, we couldn't figure it out by using a specialized ioctl, 249 * which is generally the best way. So do binary search to 250 * find the size of the partition. 251 */ 252 low = 0; 253 for (high = 1024; valid_offset (fd, high); high *= 2) 254 low = high; 255 while (low < high - 1) 256 { 257 const ext2_loff_t mid = (low + high) / 2; 258 259 if (valid_offset (fd, mid)) 260 low = mid; 261 else 262 high = mid; 263 } 264 valid_offset (fd, 0); 265 size64 = low + 1; 266 *retblocks = size64 / blocksize; 267out: 268 close(fd); 269 return rc; 270} 271 272errcode_t ext2fs_get_device_size(const char *file, int blocksize, 273 blk_t *retblocks) 274{ 275 errcode_t retval; 276 blk64_t blocks; 277 278 retval = ext2fs_get_device_size2(file, blocksize, &blocks); 279 if (retval) 280 return retval; 281 if (blocks >= (1ULL << 32)) 282 return EFBIG; 283 *retblocks = (blk_t) blocks; 284 return 0; 285} 286 287#endif /* WIN32 */ 288 289#ifdef DEBUG 290int main(int argc, char **argv) 291{ 292 blk_t blocks; 293 int retval; 294 295 if (argc < 2) { 296 fprintf(stderr, "Usage: %s device\n", argv[0]); 297 exit(1); 298 } 299 300 retval = ext2fs_get_device_size(argv[1], 1024, &blocks); 301 if (retval) { 302 com_err(argv[0], retval, 303 "while calling ext2fs_get_device_size"); 304 exit(1); 305 } 306 printf("Device %s has %u 1k blocks.\n", argv[1], blocks); 307 exit(0); 308} 309#endif 310