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.
|
|
|
|
*
|
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the GNU Public
|
|
|
|
* License.
|
|
|
|
* %End-Header%
|
1997-04-26 17:58:21 +04:00
|
|
|
*/
|
|
|
|
|
2000-05-26 03:31:54 +04:00
|
|
|
#define _LARGEFILE_SOURCE
|
|
|
|
#define _LARGEFILE64_SOURCE
|
|
|
|
|
1997-04-26 17:58:21 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1998-02-20 08:33:14 +03:00
|
|
|
#if HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
1997-04-26 17:58:21 +04:00
|
|
|
#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 */
|
|
|
|
|
2000-05-26 03:42:22 +04:00
|
|
|
#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
|
|
|
|
#define BLKGETSIZE _IO(0x12,96) /* return device size */
|
|
|
|
#endif
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
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_size(const char *file, int blocksize,
|
|
|
|
blk_t *retblocks)
|
|
|
|
{
|
|
|
|
int fd;
|
Many files:
dosio.c: New file to do DOS/BIOS disk accesses.
namei.c (open_namei): Make pathlen be of type size_t.
llseek.c: Always #include stdlib.h since it's need to define
size_t.
io.h: Use errcode_t for magic numbers.
icount.c (get_icount_el), dupfs.c (ext2fs_dup_handle), dblist.c
(dir_block_cmp): Use size_t where appropriate.
read_bb.c (ext2fs_read_bb_inode), cmp_bitmaps.c
(ext2fs_compare_inode_bitmap): Use blk_t, ino_t and size_t
where appropriate.
closefs.c (ext2fs_flush): Use dgrp_t instead of int where
appropriate.
openfs.c (ext2fs_open), check_desc.c (ext2fs_check_desc): Use blk_t
instead of int where appropriate.
rw_bitmaps.c (read_bitmaps), irel_ma.c, inode.c (ext2fs_write_inode),
initialize.c (ext2fs_initialize): brel_ma.c: Fix to make be
16-bit safe.
link.c (ext2fs_link), unlink.c (ext2fs_unlink), lookup.c (lookup_proc),
ismounted.c (ext2fs_check_if_mounted), block.c (xlate_func):
Add #pragma argsused for Turbo C.
1997-08-12 00:29:22 +04:00
|
|
|
#ifdef BLKGETSIZE
|
2001-04-26 05:42:37 +04:00
|
|
|
unsigned long size;
|
Many files:
dosio.c: New file to do DOS/BIOS disk accesses.
namei.c (open_namei): Make pathlen be of type size_t.
llseek.c: Always #include stdlib.h since it's need to define
size_t.
io.h: Use errcode_t for magic numbers.
icount.c (get_icount_el), dupfs.c (ext2fs_dup_handle), dblist.c
(dir_block_cmp): Use size_t where appropriate.
read_bb.c (ext2fs_read_bb_inode), cmp_bitmaps.c
(ext2fs_compare_inode_bitmap): Use blk_t, ino_t and size_t
where appropriate.
closefs.c (ext2fs_flush): Use dgrp_t instead of int where
appropriate.
openfs.c (ext2fs_open), check_desc.c (ext2fs_check_desc): Use blk_t
instead of int where appropriate.
rw_bitmaps.c (read_bitmaps), irel_ma.c, inode.c (ext2fs_write_inode),
initialize.c (ext2fs_initialize): brel_ma.c: Fix to make be
16-bit safe.
link.c (ext2fs_link), unlink.c (ext2fs_unlink), lookup.c (lookup_proc),
ismounted.c (ext2fs_check_if_mounted), block.c (xlate_func):
Add #pragma argsused for Turbo C.
1997-08-12 00:29:22 +04:00
|
|
|
#endif
|
1997-04-26 17:58:21 +04:00
|
|
|
ext2_loff_t high, low;
|
|
|
|
#ifdef FDGETPRM
|
|
|
|
struct floppy_struct this_floppy;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_DISKLABEL_H
|
1997-04-29 20:17:09 +04:00
|
|
|
int part;
|
1997-04-26 17:58:21 +04:00
|
|
|
struct disklabel lab;
|
|
|
|
struct partition *pp;
|
|
|
|
char ch;
|
|
|
|
#endif /* HAVE_SYS_DISKLABEL_H */
|
|
|
|
|
2000-05-26 03:31:54 +04:00
|
|
|
#ifdef HAVE_OPEN64
|
|
|
|
fd = open64(file, O_RDONLY);
|
|
|
|
#else
|
1997-04-26 18:48:50 +04:00
|
|
|
fd = open(file, O_RDONLY);
|
2000-05-26 03:31:54 +04:00
|
|
|
#endif
|
1997-04-26 17:58:21 +04:00
|
|
|
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
|
1997-04-29 20:17:09 +04:00
|
|
|
part = strlen(file) - 1;
|
|
|
|
if (part >= 0) {
|
|
|
|
ch = file[part];
|
1997-04-26 17:58:21 +04:00
|
|
|
if (isdigit(ch))
|
1997-04-29 20:17:09 +04:00
|
|
|
part = 0;
|
1997-04-26 17:58:21 +04:00
|
|
|
else if (ch >= 'a' && ch <= 'h')
|
1997-04-29 20:17:09 +04:00
|
|
|
part = ch - 'a';
|
1997-04-26 17:58:21 +04:00
|
|
|
else
|
1997-04-29 20:17:09 +04:00
|
|
|
part = -1;
|
1997-04-26 17:58:21 +04:00
|
|
|
}
|
1997-04-29 20:17:09 +04:00
|
|
|
if (part >= 0 && (ioctl(fd, DIOCGDINFO, (char *)&lab) >= 0)) {
|
|
|
|
pp = &lab.d_partitions[part];
|
1997-04-26 17:58:21 +04:00
|
|
|
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,
|
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)
|
|
|
|
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;
|
|
|
|
}
|
2000-06-10 23:27:17 +04:00
|
|
|
|
|
|
|
#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
|