getsize.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * getsize.c --- get the size of a partition.
  4. *
  5. * Copyright (C) 1995, 1995 Theodore Ts'o.
  6. * Copyright (C) 2003 VMware, Inc.
  7. *
  8. * Windows version of ext2fs_get_device_size by Chris Li, VMware.
  9. *
  10. * %Begin-Header%
  11. * This file may be redistributed under the terms of the GNU Public
  12. * License.
  13. * %End-Header%
  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. #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
  42. #define BLKGETSIZE _IO(0x12,96) /* return device size */
  43. #endif
  44. #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
  45. #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
  46. #endif
  47. #ifdef APPLE_DARWIN
  48. #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
  49. #endif /* APPLE_DARWIN */
  50. #include "ext2_fs.h"
  51. #include "ext2fs.h"
  52. #if defined(__CYGWIN__) || defined (WIN32)
  53. #include <windows.h>
  54. #include <winioctl.h>
  55. #if (_WIN32_WINNT >= 0x0500)
  56. #define HAVE_GET_FILE_SIZE_EX 1
  57. #endif
  58. errcode_t ext2fs_get_device_size(const char *file, int blocksize,
  59. blk_t *retblocks)
  60. {
  61. HANDLE dev;
  62. PARTITION_INFORMATION pi;
  63. DISK_GEOMETRY gi;
  64. DWORD retbytes;
  65. #ifdef HAVE_GET_FILE_SIZE_EX
  66. LARGE_INTEGER filesize;
  67. #else
  68. DWORD filesize;
  69. #endif /* HAVE_GET_FILE_SIZE_EX */
  70. dev = CreateFile(file, GENERIC_READ,
  71. FILE_SHARE_READ | FILE_SHARE_WRITE ,
  72. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  73. if (dev == INVALID_HANDLE_VALUE)
  74. return EBADF;
  75. if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
  76. &pi, sizeof(PARTITION_INFORMATION),
  77. &pi, sizeof(PARTITION_INFORMATION),
  78. &retbytes, NULL)) {
  79. *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),
  83. &retbytes, NULL)) {
  84. *retblocks = gi.BytesPerSector *
  85. gi.SectorsPerTrack *
  86. gi.TracksPerCylinder *
  87. gi.Cylinders.QuadPart / blocksize;
  88. #ifdef HAVE_GET_FILE_SIZE_EX
  89. } else if (GetFileSizeEx(dev, &filesize)) {
  90. *retblocks = filesize.QuadPart / blocksize;
  91. }
  92. #else
  93. } else {
  94. filesize = GetFileSize(dev, NULL);
  95. if (INVALID_FILE_SIZE != filesize) {
  96. *retblocks = filesize / blocksize;
  97. }
  98. }
  99. #endif /* HAVE_GET_FILE_SIZE_EX */
  100. CloseHandle(dev);
  101. return 0;
  102. }
  103. #else
  104. static int valid_offset (int fd, ext2_loff_t offset)
  105. {
  106. char ch;
  107. if (ext2fs_llseek (fd, offset, 0) < 0)
  108. return 0;
  109. if (read (fd, &ch, 1) < 1)
  110. return 0;
  111. return 1;
  112. }
  113. /*
  114. * Returns the number of blocks in a partition
  115. */
  116. errcode_t ext2fs_get_device_size(const char *file, int blocksize,
  117. blk_t *retblocks)
  118. {
  119. int fd;
  120. int valid_blkgetsize64 = 1;
  121. #ifdef __linux__
  122. struct utsname ut;
  123. #endif
  124. unsigned long long size64;
  125. unsigned long size;
  126. ext2_loff_t high, low;
  127. #ifdef FDGETPRM
  128. struct floppy_struct this_floppy;
  129. #endif
  130. #ifdef HAVE_SYS_DISKLABEL_H
  131. int part;
  132. struct disklabel lab;
  133. struct partition *pp;
  134. char ch;
  135. #endif /* HAVE_SYS_DISKLABEL_H */
  136. #ifdef CONFIG_LFS
  137. fd = open64(file, O_RDONLY);
  138. #else
  139. fd = open(file, O_RDONLY);
  140. #endif
  141. if (fd < 0)
  142. return errno;
  143. #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
  144. if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
  145. if ((sizeof(*retblocks) < sizeof(unsigned long long))
  146. && ((size64 / (blocksize / 512)) > 0xFFFFFFFF))
  147. return EFBIG;
  148. close(fd);
  149. *retblocks = size64 / (blocksize / 512);
  150. return 0;
  151. }
  152. #endif
  153. #ifdef BLKGETSIZE64
  154. #ifdef __linux__
  155. if ((uname(&ut) == 0) &&
  156. ((ut.release[0] == '2') && (ut.release[1] == '.') &&
  157. (ut.release[2] < '6') && (ut.release[3] == '.')))
  158. valid_blkgetsize64 = 0;
  159. #endif
  160. if (valid_blkgetsize64 &&
  161. ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
  162. if ((sizeof(*retblocks) < sizeof(unsigned long long))
  163. && ((size64 / blocksize) > 0xFFFFFFFF))
  164. return EFBIG;
  165. close(fd);
  166. *retblocks = size64 / blocksize;
  167. return 0;
  168. }
  169. #endif
  170. #ifdef BLKGETSIZE
  171. if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
  172. close(fd);
  173. *retblocks = size / (blocksize / 512);
  174. return 0;
  175. }
  176. #endif
  177. #ifdef FDGETPRM
  178. if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
  179. close(fd);
  180. *retblocks = this_floppy.size / (blocksize / 512);
  181. return 0;
  182. }
  183. #endif
  184. #ifdef HAVE_SYS_DISKLABEL_H
  185. #if defined(DIOCGMEDIASIZE)
  186. {
  187. off_t ms;
  188. u_int bs;
  189. if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
  190. *retblocks = ms / blocksize;
  191. return 0;
  192. }
  193. }
  194. #elif defined(DIOCGDINFO)
  195. /* old disklabel interface */
  196. part = strlen(file) - 1;
  197. if (part >= 0) {
  198. ch = file[part];
  199. if (isdigit(ch))
  200. part = 0;
  201. else if (ch >= 'a' && ch <= 'h')
  202. part = ch - 'a';
  203. else
  204. part = -1;
  205. }
  206. if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
  207. pp = &lab.d_partitions[part];
  208. if (pp->p_size) {
  209. close(fd);
  210. *retblocks = pp->p_size / (blocksize / 512);
  211. return 0;
  212. }
  213. }
  214. #endif /* defined(DIOCG*) */
  215. #endif /* HAVE_SYS_DISKLABEL_H */
  216. /*
  217. * OK, we couldn't figure it out by using a specialized ioctl,
  218. * which is generally the best way. So do binary search to
  219. * find the size of the partition.
  220. */
  221. low = 0;
  222. for (high = 1024; valid_offset (fd, high); high *= 2)
  223. low = high;
  224. while (low < high - 1)
  225. {
  226. const ext2_loff_t mid = (low + high) / 2;
  227. if (valid_offset (fd, mid))
  228. low = mid;
  229. else
  230. high = mid;
  231. }
  232. valid_offset (fd, 0);
  233. close(fd);
  234. size64 = low + 1;
  235. if ((sizeof(*retblocks) < sizeof(unsigned long long))
  236. && ((size64 / blocksize) > 0xFFFFFFFF))
  237. return EFBIG;
  238. *retblocks = size64 / blocksize;
  239. return 0;
  240. }
  241. #endif /* WIN32 */
  242. #ifdef DEBUG
  243. int main(int argc, char **argv)
  244. {
  245. blk_t blocks;
  246. int retval;
  247. if (argc < 2) {
  248. fprintf(stderr, "Usage: %s device\n", argv[0]);
  249. exit(1);
  250. }
  251. retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
  252. if (retval) {
  253. com_err(argv[0], retval,
  254. "while calling ext2fs_get_device_size");
  255. exit(1);
  256. }
  257. printf("Device %s has %d 1k blocks.\n", argv[1], blocks);
  258. exit(0);
  259. }
  260. #endif