introduce physical sector size to Device objects

svn path=/trunk/extragear/sysadmin/partitionmanager/; revision=1135557
This commit is contained in:
Volker Lanz 2010-06-07 15:37:05 +00:00
parent 97fbfb8f06
commit fbe5cbe667
2 changed files with 56 additions and 3 deletions

View File

@ -27,6 +27,56 @@
#include <kdebug.h>
#include <klocale.h>
#include <QFile>
#include <QByteArray>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/fs.h>
#if !defined(BLKPBSZGET)
#define BLKPBSZGET _IO(0x12,123)/* get block physical sector size */
#endif
static qint32 getPhysicalSectorSize(const QString& device_node)
{
/*
* possible ways of getting the physical sector size for a drive:
* - ioctl(BLKPBSZGET) -- supported with Linux 2.6.32 and later
* - /sys/block/sda/queue/physical_block_size
* - libblkid from util-linux-ng 2.17 or later
* TODO: implement the blkid method
*/
#if defined(BLKPBSZGET)
int phSectorSize = -1;
int fd = open(device_node.toLocal8Bit(), O_RDONLY);
if (fd != -1)
{
if (ioctl(fd, BLKPBSZGET, &phSectorSize) >= 0)
{
close(fd);
return phSectorSize;
}
close (fd);
}
#endif
QFile f(QString("/sys/block/%1/queue/physical_block_size").arg(QString(device_node).remove("/dev/")));
if (f.open(QIODevice::ReadOnly))
{
QByteArray a = f.readLine();
return a.simplified().toInt();
}
return -1;
}
/** Constructs a Device with an empty PartitionTable.
@param name the Device's name, usually some string defined by the manufacturer
@param devicenode the Device's node, for example "/dev/sda"
@ -43,7 +93,8 @@ Device::Device(const QString& name, const QString& devicenode, qint32 heads, qin
m_Heads(heads),
m_SectorsPerTrack(numSectors),
m_Cylinders(cylinders),
m_SectorSize(sectorSize),
m_LogicalSectorSize(sectorSize),
m_PhysicalSectorSize(getPhysicalSectorSize(devicenode)),
m_IconName(iconname.isEmpty() ? "drive-harddisk" : iconname),
m_SmartStatus(new SmartStatus(devicenode))
{

View File

@ -64,7 +64,8 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT Device : public QObject
qint32 heads() const { return m_Heads; } /**< @return the number of heads on the Device in CHS notation */
qint32 cylinders() const { return m_Cylinders; } /**< @return the number of cylinders on the Device in CHS notation */
qint32 sectorsPerTrack() const { return m_SectorsPerTrack; } /**< @return the number of sectors on the Device in CHS notation */
qint32 logicalSectorSize() const { return m_SectorSize; } /**< @return the logical sector size the Device uses */
qint32 physicalSectorSize() const { return m_PhysicalSectorSize; } /**< @return the phyiscal sector size the Device uses or -1 if unknown */
qint32 logicalSectorSize() const { return m_LogicalSectorSize; } /**< @return the logical sector size the Device uses */
qint64 totalSectors() const { return static_cast<qint64>(heads()) * cylinders() * sectorsPerTrack(); } /**< @return the total number of sectors on the device */
qint64 capacity() const { return totalSectors() * logicalSectorSize(); } /**< @return the Device's capacity in bytes */
qint64 cylinderSize() const { return static_cast<qint64>(heads()) * sectorsPerTrack(); } /**< @return the size of a cylinder on this Device in sectors */
@ -87,7 +88,8 @@ class LIBPARTITIONMANAGERPRIVATE_EXPORT Device : public QObject
qint32 m_Heads;
qint32 m_SectorsPerTrack;
qint32 m_Cylinders;
qint32 m_SectorSize;
qint32 m_LogicalSectorSize;
qint32 m_PhysicalSectorSize;
QString m_IconName;
SmartStatus* m_SmartStatus;
};