From 559786d6c98cdfa4ec20323a56b0448205fe2450 Mon Sep 17 00:00:00 2001 From: "cantab.net!aia21" Date: Wed, 24 Mar 2004 10:36:31 +0000 Subject: [PATCH] - Add new API ntfs_device_heads_get() and ntfs_device_sectors_per_track_get(). (Logical change 1.345) --- libntfs/device.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/libntfs/device.c b/libntfs/device.c index d0450c58..3b4d8fe4 100644 --- a/libntfs/device.c +++ b/libntfs/device.c @@ -458,10 +458,17 @@ static inline int ntfs_device_offset_valid(struct ntfs_device *dev, s64 ofs) * open device @dev. * * Adapted from e2fsutils-1.19, Copyright (C) 1995 Theodore Ts'o. + * + * On error return -1 with errno set to the error code. */ s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size) { s64 high, low; + + if (!dev || block_size <= 0 || (block_size - 1) & block_size) { + errno = EINVAL; + return -1; + } #ifdef BLKGETSIZE64 { u64 size; @@ -520,11 +527,16 @@ s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size) * block device of @dev. On error return -1 with errno set to the error code. * * The following error codes are defined: + * EINVAL Input parameter error * ENOTSUP System does not support HDIO_GETGEO ioctl * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO */ s64 ntfs_device_partition_start_sector_get(struct ntfs_device *dev) { + if (!dev) { + errno = EINVAL; + return -1; + } #ifdef HDIO_GETGEO { struct hd_geometry geo; @@ -539,3 +551,71 @@ s64 ntfs_device_partition_start_sector_get(struct ntfs_device *dev) #endif return -1; } + +/** + * ntfs_device_heads_get - get number of heads of device + * @dev: open device + * + * On success, return the number of heads on the device @dev. On error return + * -1 with errno set to the error code. + * + * The following error codes are defined: + * EINVAL Input parameter error + * ENOTSUP System does not support HDIO_GETGEO ioctl + * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO + */ +int ntfs_device_heads_get(struct ntfs_device *dev) +{ + if (!dev) { + errno = EINVAL; + return -1; + } +#ifdef HDIO_GETGEO + { struct hd_geometry geo; + + if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) { + Dprintf("HDIO_GETGEO heads = %u (0x%x)\n", + (unsigned)geo.heads, + (unsigned)geo.heads); + return geo.heads; + } + } +#else + errno = ENOTSUP; +#endif + return -1; +} + +/** + * ntfs_device_sectors_per_track_get - get number of sectors per track of device + * @dev: open device + * + * On success, return the number of sectors per track on the device @dev. On + * error return -1 with errno set to the error code. + * + * The following error codes are defined: + * EINVAL Input parameter error + * ENOTSUP System does not support HDIO_GETGEO ioctl + * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO + */ +int ntfs_device_sectors_per_track_get(struct ntfs_device *dev) +{ + if (!dev) { + errno = EINVAL; + return -1; + } +#ifdef HDIO_GETGEO + { struct hd_geometry geo; + + if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) { + Dprintf("HDIO_GETGEO sectors_per_track = %u (0x%x)\n", + (unsigned)geo.sectors, + (unsigned)geo.sectors); + return geo.sectors; + } + } +#else + errno = ENOTSUP; +#endif + return -1; +}