Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * getsize.c --- get the size of a partition. |
| 3 | * |
| 4 | * Copyright (C) 1995, 1995 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the |
| 8 | * GNU Lesser General Public License. |
| 9 | * %End-Header% |
| 10 | */ |
| 11 | |
| 12 | /* include this before sys/queues.h! */ |
| 13 | #include "blkidP.h" |
| 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 | #include <sys/stat.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_SYS_DISK_H |
| 34 | #ifdef HAVE_SYS_QUEUE_H |
| 35 | #include <sys/queue.h> /* for LIST_HEAD */ |
| 36 | #endif |
| 37 | #include <sys/disk.h> |
| 38 | #endif |
| 39 | #ifdef __linux__ |
| 40 | #include <sys/utsname.h> |
| 41 | #endif |
| 42 | |
| 43 | #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE) |
| 44 | #define BLKGETSIZE _IO(0x12,96) /* return device size */ |
| 45 | #endif |
| 46 | |
| 47 | #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64) |
| 48 | #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */ |
| 49 | #endif |
| 50 | |
| 51 | #ifdef APPLE_DARWIN |
| 52 | #define BLKGETSIZE DKIOCGETBLOCKCOUNT32 |
| 53 | #endif /* APPLE_DARWIN */ |
| 54 | |
| 55 | static int valid_offset(int fd, blkid_loff_t offset) |
| 56 | { |
| 57 | char ch; |
| 58 | |
| 59 | if (blkid_llseek(fd, offset, 0) < 0) |
| 60 | return 0; |
| 61 | if (read(fd, &ch, 1) < 1) |
| 62 | return 0; |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Returns the number of blocks in a partition |
| 68 | */ |
| 69 | blkid_loff_t blkid_get_dev_size(int fd) |
| 70 | { |
| 71 | int valid_blkgetsize64 = 1; |
| 72 | #ifdef __linux__ |
| 73 | struct utsname ut; |
| 74 | #endif |
| 75 | unsigned long long size64; |
| 76 | unsigned long size; |
| 77 | blkid_loff_t high, low; |
| 78 | #ifdef FDGETPRM |
| 79 | struct floppy_struct this_floppy; |
| 80 | #endif |
| 81 | #ifdef HAVE_SYS_DISKLABEL_H |
| 82 | int part = -1; |
| 83 | struct disklabel lab; |
| 84 | struct partition *pp; |
| 85 | char ch; |
| 86 | struct stat st; |
| 87 | #endif /* HAVE_SYS_DISKLABEL_H */ |
| 88 | |
| 89 | #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */ |
| 90 | if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) { |
| 91 | if ((sizeof(blkid_loff_t) < sizeof(unsigned long long)) |
| 92 | && (size64 << 9 > 0xFFFFFFFF)) |
| 93 | return 0; /* EFBIG */ |
| 94 | return (blkid_loff_t) size64 << 9; |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | #ifdef BLKGETSIZE64 |
| 99 | #ifdef __linux__ |
| 100 | if ((uname(&ut) == 0) && |
| 101 | ((ut.release[0] == '2') && (ut.release[1] == '.') && |
| 102 | (ut.release[2] < '6') && (ut.release[3] == '.'))) |
| 103 | valid_blkgetsize64 = 0; |
| 104 | #endif |
| 105 | if (valid_blkgetsize64 && |
| 106 | ioctl(fd, BLKGETSIZE64, &size64) >= 0) { |
| 107 | if ((sizeof(blkid_loff_t) < sizeof(unsigned long long)) |
| 108 | && ((size64) > 0xFFFFFFFF)) |
| 109 | return 0; /* EFBIG */ |
| 110 | return size64; |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | #ifdef BLKGETSIZE |
| 115 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) |
| 116 | return (blkid_loff_t)size << 9; |
| 117 | #endif |
| 118 | |
| 119 | #ifdef FDGETPRM |
| 120 | if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) |
| 121 | return (blkid_loff_t)this_floppy.size << 9; |
| 122 | #endif |
| 123 | #ifdef HAVE_SYS_DISKLABEL_H |
| 124 | #if 0 |
| 125 | /* |
| 126 | * This should work in theory but I haven't tested it. Anyone |
| 127 | * on a BSD system want to test this for me? In the meantime, |
| 128 | * binary search mechanism should work just fine. |
| 129 | */ |
| 130 | if ((fstat(fd, &st) >= 0) && S_ISBLK(st.st_mode)) |
| 131 | part = st.st_rdev & 7; |
| 132 | if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) { |
| 133 | pp = &lab.d_partitions[part]; |
| 134 | if (pp->p_size) |
| 135 | return pp->p_size << 9; |
| 136 | } |
| 137 | #endif |
| 138 | #endif /* HAVE_SYS_DISKLABEL_H */ |
| 139 | |
| 140 | /* |
| 141 | * OK, we couldn't figure it out by using a specialized ioctl, |
| 142 | * which is generally the best way. So do binary search to |
| 143 | * find the size of the partition. |
| 144 | */ |
| 145 | low = 0; |
| 146 | for (high = 1024; valid_offset(fd, high); high *= 2) |
| 147 | low = high; |
| 148 | while (low < high - 1) |
| 149 | { |
| 150 | const blkid_loff_t mid = (low + high) / 2; |
| 151 | |
| 152 | if (valid_offset(fd, mid)) |
| 153 | low = mid; |
| 154 | else |
| 155 | high = mid; |
| 156 | } |
| 157 | return low + 1; |
| 158 | } |
| 159 | |
| 160 | #ifdef TEST_PROGRAM |
| 161 | int main(int argc, char **argv) |
| 162 | { |
| 163 | blkid_loff_t bytes; |
| 164 | int fd; |
| 165 | |
| 166 | if (argc < 2) { |
| 167 | fprintf(stderr, "Usage: %s device\n" |
| 168 | "Determine the size of a device\n", argv[0]); |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | if ((fd = open(argv[1], O_RDONLY)) < 0) |
| 173 | perror(argv[0]); |
| 174 | |
| 175 | bytes = blkid_get_dev_size(fd); |
| 176 | printf("Device %s has %Ld 1k blocks.\n", argv[1], bytes >> 10); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | #endif |