e2fsprogs/lib/ext2fs/getsize.c

309 lines
6.2 KiB
C
Raw Normal View History

1997-04-26 17:58:21 +04:00
/*
* getsize.c --- get the size of a partition.
*
1997-04-29 20:17:09 +04:00
* Copyright (C) 1995, 1995 Theodore Ts'o.
* Copyright (C) 2003 VMware, Inc.
1997-04-29 20:17:09 +04:00
*
* Windows version of ext2fs_get_device_size by Chris Li, VMware.
*
1997-04-29 20:17:09 +04:00
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Library
* General Public License, version 2.
1997-04-29 20:17:09 +04:00
* %End-Header%
1997-04-26 17:58:21 +04:00
*/
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#include "config.h"
1997-04-26 17:58:21 +04:00
#include <stdio.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
1997-04-26 17:58:21 +04:00
#include <fcntl.h>
#ifdef HAVE_SYS_IOCTL_H
1997-04-26 17:58:21 +04:00
#include <sys/ioctl.h>
#endif
#ifdef HAVE_LINUX_FD_H
1997-04-26 17:58:21 +04:00
#include <linux/fd.h>
#endif
#ifdef HAVE_SYS_DISKLABEL_H
#include <sys/disklabel.h>
#endif
#ifdef HAVE_SYS_DISK_H
#include <sys/disk.h>
#endif
#ifdef __linux__
#include <sys/utsname.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <ctype.h>
1997-04-26 17:58:21 +04:00
#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
#define BLKGETSIZE _IO(0x12,96) /* return device size */
#endif
#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */
#endif
2003-03-14 10:13:48 +03:00
#ifdef APPLE_DARWIN
#define BLKGETSIZE DKIOCGETBLOCKCOUNT32
#endif /* APPLE_DARWIN */
Many files: inode.c (ext2fs_open_inode_scan): Initialize the group variables so that we don't need to call get_next_blockgroup() the first time around. Saves a bit of time, and prevents us from needing to assign -1 to current_group (which is an unsigned value). icount.c (insert_icount_el): Cast the estimated number of inodes from a float to an ino_t. alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c, bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c, check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c, ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c, icount.c, initialize.c, inline.c, inode.c, irel_ma.c, ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c, newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c, rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c, tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c, version.c: If EXT2_FLAT_INCLUDES is defined, then assume all of the ext2-specific header files are in a flat directory. block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast all assignments from void * to be compatible with C++. closefs.c (ext2fs_flush): Add a call to io_channel_flush() to make sure the contents of the disk are flushed to disk. dblist.c (ext2fs_add_dir_block): Change new to be new_entry to avoid C++ namespace clash. bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to avoid C++ namespace clash. ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c, irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c, ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c, unlink.c: Change private to be priv_data (to avoid C++ namespace clash)
1998-01-19 17:47:53 +03:00
#include "ext2_fs.h"
1997-04-26 17:58:21 +04:00
#include "ext2fs.h"
#if defined(__CYGWIN__) || defined (WIN32)
#include "windows.h"
#include "winioctl.h"
#if (_WIN32_WINNT >= 0x0500)
#define HAVE_GET_FILE_SIZE_EX 1
#endif
errcode_t ext2fs_get_device_size(const char *file, int blocksize,
blk_t *retblocks)
{
HANDLE dev;
PARTITION_INFORMATION pi;
DISK_GEOMETRY gi;
DWORD retbytes;
#ifdef HAVE_GET_FILE_SIZE_EX
LARGE_INTEGER filesize;
#else
DWORD filesize;
#endif /* HAVE_GET_FILE_SIZE_EX */
dev = CreateFile(file, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (dev == INVALID_HANDLE_VALUE)
return EBADF;
if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
&pi, sizeof(PARTITION_INFORMATION),
&pi, sizeof(PARTITION_INFORMATION),
&retbytes, NULL)) {
*retblocks = pi.PartitionLength.QuadPart / blocksize;
} else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
&gi, sizeof(DISK_GEOMETRY),
&gi, sizeof(DISK_GEOMETRY),
&retbytes, NULL)) {
*retblocks = gi.BytesPerSector *
gi.SectorsPerTrack *
gi.TracksPerCylinder *
gi.Cylinders.QuadPart / blocksize;
#ifdef HAVE_GET_FILE_SIZE_EX
} else if (GetFileSizeEx(dev, &filesize)) {
*retblocks = filesize.QuadPart / blocksize;
}
#else
} else {
filesize = GetFileSize(dev, NULL);
if (INVALID_FILE_SIZE != filesize) {
*retblocks = filesize / blocksize;
}
}
#endif /* HAVE_GET_FILE_SIZE_EX */
CloseHandle(dev);
return 0;
}
#else
1997-04-26 17:58:21 +04:00
static int valid_offset (int fd, ext2_loff_t offset)
{
char ch;
1997-04-29 20:17:09 +04:00
if (ext2fs_llseek (fd, offset, 0) < 0)
1997-04-26 17:58:21 +04:00
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_size2(const char *file, int blocksize,
blk64_t *retblocks)
1997-04-26 17:58:21 +04:00
{
int fd, rc = 0;
unsigned long long size64;
1997-04-26 17:58:21 +04:00
ext2_loff_t high, low;
fd = ext2fs_open_file(file, O_RDONLY, 0);
1997-04-26 17:58:21 +04:00
if (fd < 0)
return errno;
#ifdef DKIOCGETBLOCKCOUNT /* For Apple Darwin */
if (ioctl(fd, DKIOCGETBLOCKCOUNT, &size64) >= 0) {
*retblocks = size64 / (blocksize / 512);
goto out;
}
#endif
#ifdef BLKGETSIZE64
{
int valid_blkgetsize64 = 1;
#ifdef __linux__
struct utsname ut;
if ((uname(&ut) == 0) &&
((ut.release[0] == '2') && (ut.release[1] == '.') &&
(ut.release[2] < '6') && (ut.release[3] == '.')))
valid_blkgetsize64 = 0;
#endif
if (valid_blkgetsize64 &&
ioctl(fd, BLKGETSIZE64, &size64) >= 0) {
*retblocks = size64 / blocksize;
goto out;
}
}
#endif /* BLKGETSIZE64 */
1997-04-26 17:58:21 +04:00
#ifdef BLKGETSIZE
{
unsigned long size;
if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
*retblocks = size / (blocksize / 512);
goto out;
}
1997-04-26 17:58:21 +04:00
}
#endif
1997-04-26 17:58:21 +04:00
#ifdef FDGETPRM
{
struct floppy_struct this_floppy;
if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) {
*retblocks = this_floppy.size / (blocksize / 512);
goto out;
}
1997-04-26 17:58:21 +04:00
}
#endif
1997-04-26 17:58:21 +04:00
#ifdef HAVE_SYS_DISKLABEL_H
{
int part;
struct disklabel lab;
struct partition *pp;
char ch;
#if defined(DIOCGMEDIASIZE)
{
off_t ms;
u_int bs;
if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
*retblocks = ms / blocksize;
goto out;
}
}
#elif defined(DIOCGDINFO)
/* old disklabel interface */
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) {
*retblocks = pp->p_size / (blocksize / 512);
goto out;
}
1997-04-26 17:58:21 +04:00
}
#endif /* defined(DIOCG*) */
}
1997-04-26 17:58:21 +04:00
#endif /* HAVE_SYS_DISKLABEL_H */
{
ext2fs_struct_stat st;
if (ext2fs_fstat(fd, &st) == 0)
if (S_ISREG(st.st_mode)) {
*retblocks = st.st_size / blocksize;
goto out;
}
}
1997-04-26 17:58:21 +04:00
/*
* OK, we couldn't figure it out by using a specialized ioctl,
1997-04-29 20:17:09 +04:00
* which is generally the best way. So do binary search to
1997-04-26 17:58:21 +04:00
* find the size of the partition.
*/
low = 0;
for (high = 1024; valid_offset(fd, high); high *= 2)
1997-04-26 17:58:21 +04:00
low = high;
while (low < high - 1) {
1997-04-26 17:58:21 +04:00
const ext2_loff_t mid = (low + high) / 2;
if (valid_offset (fd, mid))
low = mid;
else
high = mid;
}
valid_offset(fd, 0);
size64 = low + 1;
*retblocks = size64 / blocksize;
out:
close(fd);
return rc;
1997-04-26 17:58:21 +04:00
}
errcode_t ext2fs_get_device_size(const char *file, int blocksize,
blk_t *retblocks)
{
errcode_t retval;
blk64_t blocks;
retval = ext2fs_get_device_size2(file, blocksize, &blocks);
if (retval)
return retval;
if (blocks >= (1ULL << 32))
return EFBIG;
*retblocks = (blk_t) blocks;
return 0;
}
#endif /* WIN32 */
#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 %u 1k blocks.\n", argv[1], blocks);
exit(0);
}
#endif