diff --git a/src/core/device.cpp b/src/core/device.cpp index b52177c..f6acae1 100644 --- a/src/core/device.cpp +++ b/src/core/device.cpp @@ -1,155 +1,179 @@ /************************************************************************* * Copyright (C) 2008 by Volker Lanz * * Copyright (C) 2016-2018 by Andrius Štikonas * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 3 of * * the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #include "core/device.h" #include "core/device_p.h" #include "core/partitiontable.h" #include "core/smartstatus.h" #include "util/capacity.h" #include /** 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" */ Device::Device(const QString& name, const QString& deviceNode, const qint64 logicalSectorSize, const qint64 totalLogicalSectors, const QString& iconName, Device::Type type) : QObject() , d(std::make_shared()) { d->m_Name = name.length() > 0 ? name : i18n("Unknown Device"); d->m_DeviceNode = deviceNode; d->m_LogicalSectorSize = logicalSectorSize; d->m_TotalLogical = totalLogicalSectors; d->m_PartitionTable = nullptr; d->m_IconName = iconName.isEmpty() ? QStringLiteral("drive-harddisk") : iconName; d->m_SmartStatus = type == Device::Disk_Device ? std::make_shared(deviceNode) : nullptr; d->m_Type = type; } +/** 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" +*/ +Device::Device(std::shared_ptr d_ptr, + const QString& name, + const QString& deviceNode, + const qint64 logicalSectorSize, + const qint64 totalLogicalSectors, + const QString& iconName, + Device::Type type) + : QObject() + , d(d_ptr) +{ + d->m_Name = name.length() > 0 ? name : i18n("Unknown Device"); + d->m_DeviceNode = deviceNode; + d->m_LogicalSectorSize = logicalSectorSize; + d->m_TotalLogical = totalLogicalSectors; + d->m_PartitionTable = nullptr; + d->m_IconName = iconName.isEmpty() ? QStringLiteral("drive-harddisk") : iconName; + d->m_SmartStatus = type == Device::Disk_Device ? std::make_shared(deviceNode) : nullptr; + d->m_Type = type; +} + /** Copy constructor for Device. * @param other the other Device. */ Device::Device(const Device& other) : QObject() { d->m_Name = other.d->m_Name; d->m_DeviceNode = other.d->m_DeviceNode; d->m_LogicalSectorSize = other.d->m_LogicalSectorSize; d->m_TotalLogical = other.d->m_TotalLogical; d->m_PartitionTable = nullptr; d->m_IconName = other.d->m_IconName; d->m_SmartStatus = nullptr; d->m_Type = other.d->m_Type; d->m_SmartStatus = other.d->m_SmartStatus; if (other.d->m_PartitionTable) d->m_PartitionTable = new PartitionTable(*other.d->m_PartitionTable); } /** Destructs a Device. */ Device::~Device() { delete d->m_PartitionTable; } bool Device::operator==(const Device& other) const { return d->m_DeviceNode == other.d->m_DeviceNode; } bool Device::operator!=(const Device& other) const { return !(other == *this); } QString Device::prettyName() const { return xi18nc("@item:inlistbox Device name – Capacity (device node)", "%1 – %2 (%3)", name(), Capacity::formatByteSize(capacity()), deviceNode()); } QString& Device::name() { return d->m_Name; } const QString& Device::name() const { return d->m_Name; } const QString& Device::deviceNode() const { return d->m_DeviceNode; } qint64 Device::logicalSize() const { return d->m_LogicalSectorSize; } qint64 Device::totalLogical() const { return d->m_TotalLogical; } PartitionTable* Device::partitionTable() { return d->m_PartitionTable; } const PartitionTable* Device::partitionTable() const { return d->m_PartitionTable; } void Device::setPartitionTable(PartitionTable* ptable) { d->m_PartitionTable = ptable; } const QString& Device::iconName() const { return d->m_IconName; } void Device::setIconName(const QString& name) { d->m_IconName = name; } SmartStatus& Device::smartStatus() { return *(d->m_SmartStatus); } const SmartStatus& Device::smartStatus() const { return *(d->m_SmartStatus); } Device::Type Device::type() const { return d->m_Type; } diff --git a/src/core/device.h b/src/core/device.h index a628f34..393723a 100644 --- a/src/core/device.h +++ b/src/core/device.h @@ -1,116 +1,117 @@ /************************************************************************* * Copyright (C) 2008 by Volker Lanz * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 3 of * * the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #ifndef KPMCORE_DEVICE_H #define KPMCORE_DEVICE_H #include "util/libpartitionmanagerexport.h" #include #include #include class PartitionTable; class CreatePartitionTableOperation; class CoreBackend; class SmartStatus; -struct DevicePrivate; +class DevicePrivate; /** A device description. Represents a device like /dev/sda. Contains information about the device (name, status, size ..) but does not operate on the device itself. @see CoreBackendDevice Devices are the outermost entity; they contain a PartitionTable that itself contains Partitions. @see PartitionTable, Partition @author Volker Lanz */ class LIBKPMCORE_EXPORT Device : public QObject { Device &operator=(const Device &) = delete; friend class CreatePartitionTableOperation; friend class CoreBackend; public: enum Type { Disk_Device = 0, LVM_Device = 1, /* VG */ RAID_Device = 2, /* software RAID device */ Unknown_Device = 4 }; protected: explicit Device(const QString& name, const QString& deviceNode, const qint64 logicalSectorSize, const qint64 totalLogicalSectors, const QString& iconName = QString(), Device::Type type = Device::Disk_Device); + explicit Device(std::shared_ptr d_ptr, const QString& name, const QString& deviceNode, const qint64 logicalSectorSize, const qint64 totalLogicalSectors, const QString& iconName = QString(), Device::Type type = Device::Disk_Device); + public: explicit Device(const Device& other); virtual ~Device(); -public: virtual bool operator==(const Device& other) const; virtual bool operator!=(const Device& other) const; /**< @return the Device's name, usually some manufacturer string */ virtual QString& name(); virtual const QString& name() const; /**< @return the Device's node, for example "/dev/sda" */ virtual const QString& deviceNode() const; /**< @return the logical sector size the Device uses (would be extent size for e.g. LVM devices) */ virtual qint64 logicalSize() const; /**< @return the total number of logical sectors on the device */ virtual qint64 totalLogical() const; /**< @return the Device's PartitionTable */ virtual PartitionTable* partitionTable(); virtual const PartitionTable* partitionTable() const; /** * Change the description of the partition table for different one. * The device itself is not changed; use CreatePartitionTableOperation * for that. The Device instance becomes the owner of @p ptable . */ virtual void setPartitionTable(PartitionTable* ptable); virtual qint64 capacity() const { /**< @return the Device's capacity in bytes */ return logicalSize() * totalLogical(); } /**< @return suggested icon name for this Device */ virtual const QString& iconName() const; /**< @param name set the new Icon for this Device */ virtual void setIconName(const QString& name); virtual SmartStatus& smartStatus(); virtual const SmartStatus& smartStatus() const; virtual Device::Type type() const; virtual QString prettyName() const; protected: std::shared_ptr d; }; #endif diff --git a/src/core/device_p.h b/src/core/device_p.h index 0577fe3..688f3c5 100644 --- a/src/core/device_p.h +++ b/src/core/device_p.h @@ -1,37 +1,38 @@ /************************************************************************* * Copyright (C) 2018 by Andrius Štikonas * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 3 of * * the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #include "core/device.h" #include #include class PartitionTable; class SmartStatus; -struct DevicePrivate +class DevicePrivate { +public: QString m_Name; QString m_DeviceNode; qint64 m_LogicalSectorSize; qint64 m_TotalLogical; PartitionTable* m_PartitionTable; QString m_IconName; std::shared_ptr m_SmartStatus; Device::Type m_Type; }; diff --git a/src/core/diskdevice.cpp b/src/core/diskdevice.cpp index 6a1cdee..c23cb58 100644 --- a/src/core/diskdevice.cpp +++ b/src/core/diskdevice.cpp @@ -1,140 +1,144 @@ /************************************************************************* * Copyright (C) 2008 by Volker Lanz * * Copyright (C) 2016-2018 by Andrius Štikonas * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 3 of * * the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #include "core/diskdevice.h" +#include "core/device_p.h" #include "core/partitiontable.h" #include "core/smartstatus.h" #include #include #include #include #include #include #include #include #ifdef __gnu_linux__ #include #endif #if !defined(BLKPBSZGET) #define BLKPBSZGET _IO(0x12,123)/* get block physical sector size */ #endif -struct DiskDevicePrivate { +#define d_ptr std::static_pointer_cast(d) + +class DiskDevicePrivate : public DevicePrivate +{ +public: qint32 m_Heads; qint32 m_SectorsPerTrack; qint32 m_Cylinders; qint64 m_LogicalSectorSize; qint64 m_PhysicalSectorSize; }; static qint64 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 2.17 or later (not implemented) */ #if defined(BLKPBSZGET) int phSectorSize = -1; int fd = open(device_node.toLocal8Bit().constData(), O_RDONLY); if (fd != -1) { if (ioctl(fd, BLKPBSZGET, &phSectorSize) >= 0) { close(fd); return phSectorSize; } close(fd); } #endif QFile f(QStringLiteral("/sys/block/%1/queue/physical_block_size").arg(QString(device_node).remove(QStringLiteral("/dev/")))); if (f.open(QIODevice::ReadOnly)) { QByteArray a = f.readLine(); return a.trimmed().toInt(); } return -1; } /** Constructs a Disk 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" @param heads the number of heads in CHS notation @param numSectors the number of sectors in CHS notation @param cylinders the number of cylinders in CHS notation @param sectorSize the size of a sector in bytes */ DiskDevice::DiskDevice(const QString& name, const QString& deviceNode, qint32 heads, qint32 numSectors, qint32 cylinders, qint64 sectorSize, const QString& iconName) - : Device(name, deviceNode, sectorSize, (static_cast(heads) * cylinders * numSectors), iconName, Device::Disk_Device) - , d(std::make_unique()) + : Device(std::make_shared(), name, deviceNode, sectorSize, (static_cast(heads) * cylinders * numSectors), iconName, Device::Disk_Device) { - d->m_Heads = heads; - d->m_SectorsPerTrack = numSectors; - d->m_Cylinders = cylinders; - d->m_LogicalSectorSize = sectorSize; - d->m_PhysicalSectorSize = getPhysicalSectorSize(deviceNode); + d_ptr->m_Heads = heads; + d_ptr->m_SectorsPerTrack = numSectors; + d_ptr->m_Cylinders = cylinders; + d_ptr->m_LogicalSectorSize = sectorSize; + d_ptr->m_PhysicalSectorSize = getPhysicalSectorSize(deviceNode); } qint32 DiskDevice::heads() const { - return d->m_Heads; + return d_ptr->m_Heads; } qint32 DiskDevice::cylinders() const { - return d->m_Cylinders; + return d_ptr->m_Cylinders; } qint32 DiskDevice::sectorsPerTrack() const { - return d->m_SectorsPerTrack; + return d_ptr->m_SectorsPerTrack; } qint64 DiskDevice::physicalSectorSize() const { - return d->m_PhysicalSectorSize; + return d_ptr->m_PhysicalSectorSize; } qint64 DiskDevice::logicalSectorSize() const { - return d->m_LogicalSectorSize; + return d_ptr->m_LogicalSectorSize; } qint64 DiskDevice::totalSectors() const { - return static_cast(d->m_Heads) * d->m_Cylinders * d->m_SectorsPerTrack; + return static_cast(d_ptr->m_Heads) * d_ptr->m_Cylinders * d_ptr->m_SectorsPerTrack; } qint64 DiskDevice::cylinderSize() const { - return static_cast(d->m_Heads) * d->m_SectorsPerTrack; + return static_cast(d_ptr->m_Heads) * d_ptr->m_SectorsPerTrack; } diff --git a/src/core/diskdevice.h b/src/core/diskdevice.h index 5b5013c..1205b88 100644 --- a/src/core/diskdevice.h +++ b/src/core/diskdevice.h @@ -1,99 +1,96 @@ /************************************************************************* * Copyright (C) 2008 by Volker Lanz * * Copyright (C) 2018 by Andrius Štikonas * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 3 of * * the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #ifndef KPMCORE_DISKDEVICE_H #define KPMCORE_DISKDEVICE_H #include "util/libpartitionmanagerexport.h" #include "core/device.h" #include #include #include #include class PartitionTable; class CreatePartitionTableOperation; class CoreBackend; class SmartStatus; -struct DiskDevicePrivate; +class DiskDevicePrivate; /** A disk device. Represents a device like /dev/sda. Devices are the outermost entity; they contain a PartitionTable that itself contains Partitions. @see PartitionTable, Partition @author Volker Lanz */ class LIBKPMCORE_EXPORT DiskDevice : public Device { Q_DISABLE_COPY(DiskDevice) friend class CreatePartitionTableOperation; friend class CoreBackend; public: DiskDevice(const QString& name, const QString& deviceNode, qint32 heads, qint32 numSectors, qint32 cylinders, qint64 sectorSize, const QString& iconName = QString()); public: /** * @return the number of heads on the Device in CHS notation */ [[deprecated]] qint32 heads() const; /** * @return the number of cylinders on the Device in CHS notation */ [[deprecated]] qint32 cylinders() const; /** * @return the number of sectors on the Device in CHS notation */ qint32 sectorsPerTrack() const; /** * @return the physical sector size the Device uses or -1 if unknown */ qint64 physicalSectorSize() const; /** * @return the logical sector size the Device uses */ qint64 logicalSectorSize() const; /** * @return the total number of sectors on the device */ qint64 totalSectors() const; /** * @return the size of a cylinder on this Device in sectors */ qint64 cylinderSize() const; - -private: - std::unique_ptr d; }; #endif