You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
3.2 KiB
152 lines
3.2 KiB
#include "libparted.h"
|
|
|
|
#include <QStringList>
|
|
#include <QtDebug>
|
|
|
|
LibParted::LibParted(QObject *parent)
|
|
: QObject(parent)
|
|
{
|
|
Init();
|
|
}
|
|
|
|
LibParted::~LibParted()
|
|
{
|
|
}
|
|
|
|
QString LibParted::getVersion()
|
|
{
|
|
return QString( ped_get_version() );
|
|
}
|
|
|
|
void LibParted::Init()
|
|
{
|
|
ped_device_probe_all();
|
|
|
|
PedDevice* dsk = NULL;
|
|
while ( (dsk = ped_device_get_next(dsk)) != NULL )
|
|
m_DeviceList[ QString(dsk->path) ] = dsk;
|
|
}
|
|
|
|
QStringList LibParted::getDevices()
|
|
{
|
|
QStringList res;
|
|
|
|
Devices::const_iterator i = m_DeviceList.begin();
|
|
while ( i != m_DeviceList.constEnd() )
|
|
{
|
|
res << i.key();
|
|
++i;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
DeviceInfo LibParted::getDeviceInfo(const QString& path) throw(LibPartedError)
|
|
{
|
|
if ( m_DeviceList.find(path) == m_DeviceList.end() )
|
|
throw LibPartedError();
|
|
|
|
return convertPedDevice(m_DeviceList[path]);
|
|
}
|
|
|
|
QList<PartitionInfo> LibParted::getPartitionList(const QString& path) throw(LibPartedError)
|
|
{
|
|
QList<PartitionInfo> PartInfo;
|
|
|
|
if ( m_DeviceList.find(path) == m_DeviceList.end() )
|
|
throw LibPartedError();
|
|
|
|
PedDevice* dev = m_DeviceList[path];
|
|
|
|
PedDisk* disk = NULL;
|
|
disk = ped_disk_new( dev );
|
|
if (!disk)
|
|
throw LibPartedError();
|
|
|
|
bool has_extended = ped_disk_type_check_feature (disk->type, PED_DISK_TYPE_EXTENDED);
|
|
bool has_name = ped_disk_type_check_feature (disk->type, PED_DISK_TYPE_PARTITION_NAME);
|
|
|
|
PedPartition* part = NULL;
|
|
|
|
while ( part = ped_disk_next_partition (disk, part) )
|
|
{
|
|
PartitionInfo part_inf;
|
|
|
|
if (part->type & PED_PARTITION_METADATA)
|
|
continue;
|
|
|
|
if (part->type & PED_PARTITION_EXTENDED)
|
|
continue;
|
|
|
|
if ( part->num >= 0)
|
|
part_inf.num = part->num;
|
|
|
|
part_inf.begin = part->geom.start;
|
|
part_inf.end = (part->geom.end + 1) * dev->sector_size - 1;
|
|
part_inf.size = sizeHuman( part->geom.length * dev->sector_size );
|
|
|
|
if ( !(part->type & PED_PARTITION_FREESPACE) )
|
|
{
|
|
if (has_extended)
|
|
{
|
|
char* type = const_cast<char*>( ped_partition_type_get_name(part->type) );
|
|
part_inf.type = QString(type);
|
|
}
|
|
|
|
part_inf.fs = part->fs_type ? QString(part->fs_type->name) : "";
|
|
|
|
if (has_name)
|
|
{
|
|
char* name = const_cast<char*>( ped_partition_get_name(part) );
|
|
part_inf.name = QString(name);
|
|
}
|
|
|
|
} else {
|
|
part_inf.type = "Free space";
|
|
}
|
|
|
|
PartInfo << part_inf;
|
|
}
|
|
|
|
return PartInfo;
|
|
}
|
|
|
|
DeviceInfo LibParted::convertPedDevice(const PedDevice* dev)
|
|
{
|
|
DeviceInfo part_dev;
|
|
|
|
const char *const transport[] = {"unknown", "scsi", "ide", "dac960",
|
|
"cpqarray", "file", "ataraid", "i2o",
|
|
"ubd", "dasd", "viodasd", "sx8", "dm",
|
|
"xvd", "sd/mmc", "virtblk"};
|
|
|
|
part_dev.model = QString(dev->model);
|
|
part_dev.transport = QString("%1").arg(transport[dev->type]);
|
|
|
|
part_dev.size = sizeHuman(dev->length * dev->sector_size);
|
|
part_dev.sec_ph = dev->phys_sector_size;
|
|
part_dev.sec_log = dev->sector_size;
|
|
|
|
return part_dev;
|
|
}
|
|
|
|
QString LibParted::sizeHuman(const long long size)
|
|
{
|
|
const QString unit[] = {tr("B"), tr("KB"), tr("MB"), tr("GB")};
|
|
float conv_size;
|
|
float coef = 1;
|
|
|
|
for (quint32 i = 0; i < sizeof(unit); ++i)
|
|
{
|
|
conv_size = size/coef;
|
|
QString res = QString::number(conv_size, 'f', 2);
|
|
if ( res.split(".").first().size() <= 3 )
|
|
return res + " " + unit[i];
|
|
|
|
coef *= 1000;
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|