xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/getsize.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
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 Library
11  * General Public License, version 2.
12  * %End-Header%
13  */
14 
15 #ifndef _LARGEFILE_SOURCE
16 #define _LARGEFILE_SOURCE
17 #endif
18 #ifndef _LARGEFILE64_SOURCE
19 #define _LARGEFILE64_SOURCE
20 #endif
21 
22 #include "config.h"
23 #include <stdio.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #if HAVE_ERRNO_H
28 #include <errno.h>
29 #endif
30 #include <fcntl.h>
31 #ifdef HAVE_SYS_IOCTL_H
32 #include <sys/ioctl.h>
33 #endif
34 #ifdef HAVE_LINUX_FD_H
35 #include <linux/fd.h>
36 #endif
37 #ifdef HAVE_SYS_DISKLABEL_H
38 #include <sys/disklabel.h>
39 #endif
40 #ifdef HAVE_SYS_DISK_H
41 #include <sys/disk.h>
42 #endif
43 #ifdef __linux__
44 #include <sys/utsname.h>
45 #endif
46 #if HAVE_SYS_STAT_H
47 #include <sys/stat.h>
48 #endif
49 #include <ctype.h>
50 
51 #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
52 #define BLKGETSIZE _IO(0x12,96)	/* return device size */
53 #endif
54 
55 #if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
56 #define BLKGETSIZE64 _IOR(0x12,114,size_t)	/* return device size in bytes (u64 *arg) */
57 #endif
58 
59 #ifdef APPLE_DARWIN
60 #define BLKGETSIZE DKIOCGETBLOCKCOUNT32
61 #endif /* APPLE_DARWIN */
62 
63 #include "ext2_fs.h"
64 #include "ext2fs.h"
65 
66 #if defined(__CYGWIN__) || defined (WIN32)
67 #include "windows.h"
68 #include "winioctl.h"
69 
70 #if (_WIN32_WINNT >= 0x0500)
71 #define HAVE_GET_FILE_SIZE_EX 1
72 #endif
73 
ext2fs_get_device_size2(const char * file,int blocksize,blk64_t * retblocks)74 errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
75 				  blk64_t *retblocks)
76 {
77 	int fd;
78 	HANDLE h;
79 	PARTITION_INFORMATION pi;
80 	DISK_GEOMETRY gi;
81 	DWORD retbytes;
82 #ifdef HAVE_GET_FILE_SIZE_EX
83 	LARGE_INTEGER filesize;
84 #else
85 	DWORD filesize;
86 #endif /* HAVE_GET_FILE_SIZE_EX */
87 
88 	fd = ext2fs_open_file(file, O_RDONLY, 0);
89 	if (fd < 0)
90 		return errno;
91 	h = (HANDLE)_get_osfhandle(fd);
92 	if (DeviceIoControl(h, IOCTL_DISK_GET_PARTITION_INFO,
93 			    &pi, sizeof(PARTITION_INFORMATION),
94 			    &pi, sizeof(PARTITION_INFORMATION),
95 			    &retbytes, NULL)) {
96 
97 		*retblocks = pi.PartitionLength.QuadPart / blocksize;
98 
99 	} else if (DeviceIoControl(h, IOCTL_DISK_GET_DRIVE_GEOMETRY,
100 				&gi, sizeof(DISK_GEOMETRY),
101 				&gi, sizeof(DISK_GEOMETRY),
102 				&retbytes, NULL)) {
103 
104 		*retblocks = gi.BytesPerSector *
105 			     gi.SectorsPerTrack *
106 			     gi.TracksPerCylinder *
107 			     gi.Cylinders.QuadPart / blocksize;
108 
109 #ifdef HAVE_GET_FILE_SIZE_EX
110 	} else if (GetFileSizeEx(h, &filesize)) {
111 		*retblocks = filesize.QuadPart / blocksize;
112 	}
113 #else
114 	} else {
115 		filesize = GetFileSize(h, NULL);
116 		if (INVALID_FILE_SIZE != filesize) {
117 			*retblocks = filesize / blocksize;
118 		}
119 	}
120 #endif /* HAVE_GET_FILE_SIZE_EX */
121 
122 	close(fd);
123 	return 0;
124 }
125 
126 #else
127 
valid_offset(int fd,ext2_loff_t offset)128 static int valid_offset (int fd, ext2_loff_t offset)
129 {
130 	char ch;
131 
132 	if (ext2fs_llseek (fd, offset, 0) < 0)
133 		return 0;
134 	if (read (fd, &ch, 1) < 1)
135 		return 0;
136 	return 1;
137 }
138 
139 /*
140  * Returns the number of blocks in a partition
141  */
ext2fs_get_device_size2(const char * file,int blocksize,blk64_t * retblocks)142 errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
143 				  blk64_t *retblocks)
144 {
145 	int	fd, rc = 0;
146 	unsigned long long size64;
147 	ext2_loff_t high, low;
148 
149 	fd = ext2fs_open_file(file, O_RDONLY, 0);
150 	if (fd < 0)
151 		return errno;
152 
153 #if defined DKIOCGETBLOCKCOUNT && defined DKIOCGETBLOCKSIZE	/* For Apple Darwin */
154 	unsigned int size;
155 
156 	if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0 &&
157 	    ioctl(fd, DKIOCGETBLOCKSIZE, &size) >= 0) {
158 		*retblocks = size64 * size / blocksize;
159 		goto out;
160 	}
161 #endif
162 
163 #ifdef BLKGETSIZE64
164 	{
165 		int valid_blkgetsize64 = 1;
166 #ifdef __linux__
167 		struct utsname ut;
168 
169 		if ((uname(&ut) == 0) &&
170 		    ((ut.release[0] == '2') && (ut.release[1] == '.') &&
171 		     (ut.release[2] < '6') && (ut.release[3] == '.')))
172 			valid_blkgetsize64 = 0;
173 #endif
174 		if (valid_blkgetsize64 &&
175 		    ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
176 			*retblocks = size64 / blocksize;
177 			goto out;
178 		}
179 	}
180 #endif /* BLKGETSIZE64 */
181 
182 #ifdef BLKGETSIZE
183 	{
184 		unsigned long	size;
185 
186 		if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
187 			*retblocks = size / (blocksize / 512);
188 			goto out;
189 		}
190 	}
191 #endif
192 
193 #ifdef FDGETPRM
194 	{
195 		struct floppy_struct this_floppy;
196 
197 		if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
198 			*retblocks = this_floppy.size / (blocksize / 512);
199 			goto out;
200 		}
201 	}
202 #endif
203 
204 #ifdef HAVE_SYS_DISKLABEL_H
205 	{
206 		int part;
207 		struct disklabel lab;
208 		struct partition *pp;
209 		char ch;
210 
211 #if defined(DIOCGMEDIASIZE)
212 		{
213 			off_t ms;
214 			u_int bs;
215 			if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
216 				*retblocks = ms / blocksize;
217 				goto out;
218 			}
219 		}
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;
231 		}
232 		if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
233 			pp = &lab.d_partitions[part];
234 			if (pp->p_size) {
235 				*retblocks = pp->p_size / (blocksize / 512);
236 				goto out;
237 			}
238 		}
239 #endif /* defined(DIOCG*) */
240 	}
241 #endif /* HAVE_SYS_DISKLABEL_H */
242 
243 	{
244 		ext2fs_struct_stat st;
245 
246 		if (ext2fs_fstat(fd, &st) == 0)
247 			if (S_ISREG(st.st_mode)) {
248 				*retblocks = st.st_size / blocksize;
249 				goto out;
250 			}
251 	}
252 
253 	/*
254 	 * OK, we couldn't figure it out by using a specialized ioctl,
255 	 * which is generally the best way.  So do binary search to
256 	 * find the size of the partition.
257 	 */
258 	low = 0;
259 	for (high = 1024; valid_offset(fd, high); high *= 2)
260 		low = high;
261 	while (low < high - 1) {
262 		const ext2_loff_t mid = (low + high) / 2;
263 
264 		if (valid_offset (fd, mid))
265 			low = mid;
266 		else
267 			high = mid;
268 	}
269 	valid_offset(fd, 0);
270 	size64 = low + 1;
271 	*retblocks = size64 / blocksize;
272 out:
273 	close(fd);
274 	return rc;
275 }
276 
277 #endif /* WIN32 */
278 
ext2fs_get_device_size(const char * file,int blocksize,blk_t * retblocks)279 errcode_t ext2fs_get_device_size(const char *file, int blocksize,
280 				 blk_t *retblocks)
281 {
282 	errcode_t retval;
283 	blk64_t	blocks;
284 
285 	retval = ext2fs_get_device_size2(file, blocksize, &blocks);
286 	if (retval)
287 		return retval;
288 	if (blocks >= (1ULL << 32))
289 		return EFBIG;
290 	*retblocks = (blk_t) blocks;
291 	return 0;
292 }
293 
294 #ifdef DEBUG
main(int argc,char ** argv)295 int main(int argc, char **argv)
296 {
297 	blk64_t	blocks;
298 	int	retval;
299 
300 	if (argc < 2) {
301 		fprintf(stderr, "Usage: %s device\n", argv[0]);
302 		exit(1);
303 	}
304 
305 	retval = ext2fs_get_device_size2(argv[1], 1024, &blocks);
306 	if (retval) {
307 		com_err(argv[0], retval,
308 			"while calling ext2fs_get_device_size");
309 		exit(1);
310 	}
311 	printf("Device %s has %llu 1k blocks.\n", argv[1],
312 	       (unsigned long long) locks);
313 	exit(0);
314 }
315 #endif
316