Whamcloud - gitweb
Separate public and private interfaces into separate header files.
[tools/e2fsprogs.git] / lib / blkid / getsize.c
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 #define _LARGEFILE_SOURCE
13 #define _LARGEFILE64_SOURCE
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_LINUX_FD_H
24 #include <sys/ioctl.h>
25 #include <linux/fd.h>
26 #endif /* HAVE_LINUX_FD_H */
27 #ifdef HAVE_SYS_DISKLABEL_H
28 #include <sys/ioctl.h>
29 #include <sys/disklabel.h>
30 #endif /* HAVE_SYS_DISKLABEL_H */
31
32 #include "blkidP.h"
33
34 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
35 #define BLKGETSIZE _IO(0x12,96) /* return device size */
36 #endif
37
38 static int valid_offset(int fd, blkid_loff_t offset)
39 {
40         char ch;
41
42         if (blkid_llseek(fd, offset, 0) < 0)
43                 return 0;
44         if (read(fd, &ch, 1) < 1)
45                 return 0;
46         return 1;
47 }
48
49 /*
50  * Returns the number of blocks in a partition
51  */
52 blkid_loff_t blkid_get_dev_size(int fd)
53 {
54 #ifdef BLKGETSIZE
55         unsigned long size;
56 #endif
57         blkid_loff_t high, low;
58 #ifdef FDGETPRM
59         struct floppy_struct this_floppy;
60 #endif
61 #ifdef HAVE_SYS_DISKLABEL_H
62         int part;
63         struct disklabel lab;
64         struct partition *pp;
65         char ch;
66 #endif /* HAVE_SYS_DISKLABEL_H */
67
68 #ifdef BLKGETSIZE
69         if (ioctl(fd, BLKGETSIZE, &size) >= 0)
70                 return (blkid_loff_t)size << 9;
71 #endif
72 #ifdef FDGETPRM
73         if (ioctl(fd, FDGETPRM, &this_floppy) >= 0)
74                 return (blkid_loff_t)this_floppy.size << 9;
75 #endif
76 #ifdef HAVE_SYS_DISKLABEL_H
77         part = strlen(file) - 1;
78         if (part >= 0) {
79                 ch = file[part];
80                 if (isdigit(ch))
81                         part = 0;
82                 else if (ch >= 'a' && ch <= 'h')
83                         part = ch - 'a';
84                 else
85                         part = -1;
86         }
87         if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
88                 pp = &lab.d_partitions[part];
89                 if (pp->p_size)
90                         return pp->p_size << 9;
91         }
92 #endif /* HAVE_SYS_DISKLABEL_H */
93
94         /*
95          * OK, we couldn't figure it out by using a specialized ioctl,
96          * which is generally the best way.  So do binary search to
97          * find the size of the partition.
98          */
99         low = 0;
100         for (high = 1024; valid_offset(fd, high); high *= 2)
101                 low = high;
102         while (low < high - 1)
103         {
104                 const blkid_loff_t mid = (low + high) / 2;
105
106                 if (valid_offset(fd, mid))
107                         low = mid;
108                 else
109                         high = mid;
110         }
111         return low + 1;
112 }
113
114 #ifdef TEST_PROGRAM
115 int main(int argc, char **argv)
116 {
117         blkid_loff_t bytes;
118         int     fd;
119
120         if (argc < 2) {
121                 fprintf(stderr, "Usage: %s device\n"
122                         "Determine the size of a device\n", argv[0]);
123                 return 1;
124         }
125
126         if ((fd = open(argv[1], O_RDONLY)) < 0)
127                 perror(argv[0]);
128
129         bytes = blkid_get_dev_size(fd);
130         printf("Device %s has %Ld 1k blocks.\n", argv[1], bytes >> 10);
131
132         return 0;
133 }
134 #endif