1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*
* getsize.c --- get the size of a partition.
*
* Copyright (C) 1995, 1995 Theodore Ts'o.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#include <fcntl.h>
#ifdef HAVE_LINUX_FD_H
#include <sys/ioctl.h>
#include <linux/fd.h>
#endif
#ifdef HAVE_SYS_DISKLABEL_H
#include <sys/ioctl.h>
#include <sys/disklabel.h>
#endif /* HAVE_SYS_DISKLABEL_H */
#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
#define BLKGETSIZE _IO(0x12,96) /* return device size */
#endif
#if EXT2_FLAT_INCLUDES
#include "ext2_fs.h"
#else
#include <linux/ext2_fs.h>
#endif
#include "ext2fs.h"
static int valid_offset (int fd, ext2_loff_t offset)
{
char ch;
if (ext2fs_llseek (fd, offset, 0) < 0)
return 0;
if (read (fd, &ch, 1) < 1)
return 0;
return 1;
}
/*
* Returns the number of blocks in a partition
*/
errcode_t ext2fs_get_device_size(const char *file, int blocksize,
blk_t *retblocks)
{
int fd;
#ifdef BLKGETSIZE
long size;
#endif
ext2_loff_t high, low;
#ifdef FDGETPRM
struct floppy_struct this_floppy;
#endif
#ifdef HAVE_SYS_DISKLABEL_H
int part;
struct disklabel lab;
struct partition *pp;
char ch;
#endif /* HAVE_SYS_DISKLABEL_H */
#ifdef HAVE_OPEN64
fd = open64(file, O_RDONLY);
#else
fd = open(file, O_RDONLY);
#endif
if (fd < 0)
return errno;
#ifdef BLKGETSIZE
if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
close(fd);
*retblocks = size / (blocksize / 512);
return 0;
}
#endif
#ifdef FDGETPRM
if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
close(fd);
*retblocks = this_floppy.size / (blocksize / 512);
return 0;
}
#endif
#ifdef HAVE_SYS_DISKLABEL_H
part = strlen(file) - 1;
if (part >= 0) {
ch = file[part];
if (isdigit(ch))
part = 0;
else if (ch >= 'a' && ch <= 'h')
part = ch - 'a';
else
part = -1;
}
if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
pp = &lab.d_partitions[part];
if (pp->p_size) {
close(fd);
*retblocks = pp->p_size / (blocksize / 512);
return 0;
}
}
#endif /* HAVE_SYS_DISKLABEL_H */
/*
* OK, we couldn't figure it out by using a specialized ioctl,
* which is generally the best way. So do binary search to
* find the size of the partition.
*/
low = 0;
for (high = 1024; valid_offset (fd, high); high *= 2)
low = high;
while (low < high - 1)
{
const ext2_loff_t mid = (low + high) / 2;
if (valid_offset (fd, mid))
low = mid;
else
high = mid;
}
valid_offset (fd, 0);
close(fd);
*retblocks = (low + 1) / blocksize;
return 0;
}
#ifdef DEBUG
int main(int argc, char **argv)
{
blk_t blocks;
int retval;
if (argc < 2) {
fprintf(stderr, "Usage: %s device\n", argv[0]);
exit(1);
}
retval = ext2fs_get_device_size(argv[1], 1024, &blocks);
if (retval) {
com_err(argv[0], retval,
"while calling ext2fs_get_device_size");
exit(1);
}
printf("Device %s has %d 1k blocks.\n", argv[1], blocks);
exit(0);
}
#endif
|