Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / e2fsprogs / ext2fs / getsize.c
blob923334facc849be41b62c51fb758735dd412dcdb
1 /* vi: set sw=4 ts=4: */
2 /*
3 * getsize.c --- get the size of a partition.
5 * Copyright (C) 1995, 1995 Theodore Ts'o.
6 * Copyright (C) 2003 VMware, Inc.
8 * Windows version of ext2fs_get_device_size by Chris Li, VMware.
10 * %Begin-Header%
11 * This file may be redistributed under the terms of the GNU Public
12 * License.
13 * %End-Header%
16 #include <stdio.h>
17 #if HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 #if HAVE_ERRNO_H
21 #include <errno.h>
22 #endif
23 #include <fcntl.h>
24 #ifdef HAVE_SYS_IOCTL_H
25 #include <sys/ioctl.h>
26 #endif
27 #ifdef HAVE_LINUX_FD_H
28 #include <linux/fd.h>
29 #endif
30 #ifdef HAVE_SYS_DISKLABEL_H
31 #include <sys/disklabel.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
43 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
44 #define BLKGETSIZE _IO(0x12,96) /* return device size */
45 #endif
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
51 #ifdef APPLE_DARWIN
52 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
53 #endif /* APPLE_DARWIN */
55 #include "ext2_fs.h"
56 #include "ext2fs.h"
58 #if defined(__CYGWIN__) || defined(WIN32)
59 #include <windows.h>
60 #include <winioctl.h>
62 #if (_WIN32_WINNT >= 0x0500)
63 #define HAVE_GET_FILE_SIZE_EX 1
64 #endif
66 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
67 blk_t *retblocks)
69 HANDLE dev;
70 PARTITION_INFORMATION pi;
71 DISK_GEOMETRY gi;
72 DWORD retbytes;
73 #ifdef HAVE_GET_FILE_SIZE_EX
74 LARGE_INTEGER filesize;
75 #else
76 DWORD filesize;
77 #endif /* HAVE_GET_FILE_SIZE_EX */
79 dev = CreateFile(file, GENERIC_READ,
80 FILE_SHARE_READ | FILE_SHARE_WRITE ,
81 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
83 if (dev == INVALID_HANDLE_VALUE)
84 return EBADF;
85 if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
86 &pi, sizeof(PARTITION_INFORMATION),
87 &pi, sizeof(PARTITION_INFORMATION),
88 &retbytes, NULL)) {
90 *retblocks = pi.PartitionLength.QuadPart / blocksize;
92 } else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
93 &gi, sizeof(DISK_GEOMETRY),
94 &gi, sizeof(DISK_GEOMETRY),
95 &retbytes, NULL)) {
97 *retblocks = gi.BytesPerSector *
98 gi.SectorsPerTrack *
99 gi.TracksPerCylinder *
100 gi.Cylinders.QuadPart / blocksize;
102 #ifdef HAVE_GET_FILE_SIZE_EX
103 } else if (GetFileSizeEx(dev, &filesize)) {
104 *retblocks = filesize.QuadPart / blocksize;
106 #else
107 } else {
108 filesize = GetFileSize(dev, NULL);
109 if (INVALID_FILE_SIZE != filesize) {
110 *retblocks = filesize / blocksize;
113 #endif /* HAVE_GET_FILE_SIZE_EX */
115 CloseHandle(dev);
116 return 0;
119 #else
121 static int valid_offset (int fd, ext2_loff_t offset)
123 char ch;
125 if (ext2fs_llseek (fd, offset, 0) < 0)
126 return 0;
127 if (read (fd, &ch, 1) < 1)
128 return 0;
129 return 1;
133 * Returns the number of blocks in a partition
135 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
136 blk_t *retblocks)
138 int fd;
139 int valid_blkgetsize64 = 1;
140 #ifdef __linux__
141 struct utsname ut;
142 #endif
143 unsigned long long size64;
144 unsigned long size;
145 ext2_loff_t high, low;
146 #ifdef FDGETPRM
147 struct floppy_struct this_floppy;
148 #endif
149 #ifdef HAVE_SYS_DISKLABEL_H
150 int part;
151 struct disklabel lab;
152 struct partition *pp;
153 char ch;
154 #endif /* HAVE_SYS_DISKLABEL_H */
156 #ifdef CONFIG_LFS
157 fd = open64(file, O_RDONLY);
158 #else
159 fd = open(file, O_RDONLY);
160 #endif
161 if (fd < 0)
162 return errno;
164 #ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
165 if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
166 if ((sizeof(*retblocks) < sizeof(unsigned long long))
167 && ((size64 / (blocksize / 512)) > 0xFFFFFFFF))
168 return EFBIG;
169 close(fd);
170 *retblocks = size64 / (blocksize / 512);
171 return 0;
173 #endif
175 #ifdef BLKGETSIZE64
176 #ifdef __linux__
177 uname(&ut);
178 if ((ut.release[0] == '2') && (ut.release[1] == '.') &&
179 (ut.release[2] < '6') && (ut.release[3] == '.'))
180 valid_blkgetsize64 = 0;
181 #endif
182 if (valid_blkgetsize64 &&
183 ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
184 if ((sizeof(*retblocks) < sizeof(unsigned long long))
185 && ((size64 / blocksize) > 0xFFFFFFFF))
186 return EFBIG;
187 close(fd);
188 *retblocks = size64 / blocksize;
189 return 0;
191 #endif
193 #ifdef BLKGETSIZE
194 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
195 close(fd);
196 *retblocks = size / (blocksize / 512);
197 return 0;
199 #endif
201 #ifdef FDGETPRM
202 if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
203 close(fd);
204 *retblocks = this_floppy.size / (blocksize / 512);
205 return 0;
207 #endif
209 #ifdef HAVE_SYS_DISKLABEL_H
210 #if defined(DIOCGMEDIASIZE)
212 off_t ms;
213 u_int bs;
214 if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
215 close(fd);
216 *retblocks = ms / blocksize;
217 return 0;
220 #elif defined(DIOCGDINFO)
221 /* old disklabel interface */
222 part = strlen(file) - 1;
223 if (part >= 0) {
224 ch = file[part];
225 if (isdigit(ch))
226 part = 0;
227 else if (ch >= 'a' && ch <= 'h')
228 part = ch - 'a';
229 else
230 part = -1;
232 if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
233 pp = &lab.d_partitions[part];
234 if (pp->p_size) {
235 close(fd);
236 *retblocks = pp->p_size / (blocksize / 512);
237 return 0;
240 #endif /* defined(DIOCG*) */
241 #endif /* HAVE_SYS_DISKLABEL_H */
244 * OK, we couldn't figure it out by using a specialized ioctl,
245 * which is generally the best way. So do binary search to
246 * find the size of the partition.
248 low = 0;
249 for (high = 1024; valid_offset (fd, high); high *= 2)
250 low = high;
251 while (low < high - 1)
253 const ext2_loff_t mid = (low + high) / 2;
255 if (valid_offset (fd, mid))
256 low = mid;
257 else
258 high = mid;
260 valid_offset (fd, 0);
261 close(fd);
262 size64 = low + 1;
263 if ((sizeof(*retblocks) < sizeof(unsigned long long))
264 && ((size64 / blocksize) > 0xFFFFFFFF))
265 return EFBIG;
266 *retblocks = size64 / blocksize;
267 return 0;
270 #endif /* WIN32 */
272 #ifdef DEBUG
273 int main(int argc, char **argv)
275 blk_t blocks;
276 int retval;
278 if (argc < 2) {
279 fprintf(stderr, "Usage: %s device\n", argv[0]);
280 exit(1);
283 retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
284 if (retval) {
285 com_err(argv[0], retval,
286 "while calling ext2fs_get_device_size");
287 exit(1);
289 printf("Device %s has %d 1k blocks.\n", argv[1], blocks);
290 exit(0);
292 #endif