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