diff --git a/src/backend/corebackend.h b/src/backend/corebackend.h index 92c1bb8..c2e1153 100644 --- a/src/backend/corebackend.h +++ b/src/backend/corebackend.h @@ -136,25 +136,21 @@ public: * an instance is returned, it's the caller's responsibility to delete the * object. */ - virtual CoreBackendDevice* openDevice(const Device& d) = 0; + virtual std::unique_ptr openDevice(const Device& d) = 0; /** * Open a device in exclusive mode for writing. * @param deviceNode The path of the device that is to be opened (e.g. /dev/sda) - * @return a pointer to a CoreBackendDevice or nullptr if the open failed. If a pointer to - * an instance is returned, it's the caller's responsibility to delete the - * object. + * @return a pointer to a CoreBackendDevice or nullptr if the open failed. */ - virtual CoreBackendDevice* openDeviceExclusive(const Device& d) = 0; + virtual std::unique_ptr openDeviceExclusive(const Device& d) = 0; /** * Close a CoreBackendDevice that has previously been opened. * @param core_device Pointer to the CoreBackendDevice to be closed. Must not be nullptr. * @return true if closing the CoreBackendDevice succeeded, otherwise false. - * - * This method does not delete the object. */ - virtual bool closeDevice(CoreBackendDevice* core_device) = 0; + virtual bool closeDevice(std::unique_ptr coreDevice) = 0; /** * Emit progress. diff --git a/src/core/copysourcedevice.cpp b/src/core/copysourcedevice.cpp index 9754a0d..18fcb85 100644 --- a/src/core/copysourcedevice.cpp +++ b/src/core/copysourcedevice.cpp @@ -20,7 +20,6 @@ #include "backend/corebackend.h" #include "backend/corebackendmanager.h" -#include "backend/corebackenddevice.h" #include "core/copytarget.h" #include "core/copytargetdevice.h" @@ -40,12 +39,6 @@ CopySourceDevice::CopySourceDevice(Device& d, qint64 firstbyte, qint64 lastbyte) { } -/** Destructs a CopySourceDevice */ -CopySourceDevice::~CopySourceDevice() -{ - delete m_BackendDevice; -} - /** Opens the Device @return true if the Device could be successfully opened */ diff --git a/src/core/copysourcedevice.h b/src/core/copysourcedevice.h index bfdbd45..07aee9d 100644 --- a/src/core/copysourcedevice.h +++ b/src/core/copysourcedevice.h @@ -19,9 +19,12 @@ #define KPMCORE_COPYSOURCEDEVICE_H +#include "backend/corebackenddevice.h" #include "core/copysource.h" #include "util/libpartitionmanagerexport.h" +#include + #include class Device; @@ -41,7 +44,6 @@ class CopySourceDevice : public CopySource public: CopySourceDevice(Device& d, qint64 firstbyte, qint64 lastbyte); - ~CopySourceDevice(); public: bool open() override; @@ -68,7 +70,7 @@ protected: Device& m_Device; const qint64 m_FirstByte; const qint64 m_LastByte; - CoreBackendDevice* m_BackendDevice; + std::unique_ptr m_BackendDevice; }; #endif diff --git a/src/core/copytargetdevice.cpp b/src/core/copytargetdevice.cpp index 2ad9a02..b5dd671 100644 --- a/src/core/copytargetdevice.cpp +++ b/src/core/copytargetdevice.cpp @@ -19,7 +19,6 @@ #include "backend/corebackend.h" #include "backend/corebackendmanager.h" -#include "backend/corebackenddevice.h" #include "core/device.h" @@ -37,12 +36,6 @@ CopyTargetDevice::CopyTargetDevice(Device& d, qint64 firstbyte, qint64 lastbyte) { } -/** Destructs a CopyTargetDevice */ -CopyTargetDevice::~CopyTargetDevice() -{ - delete m_BackendDevice; -} - /** Opens a CopyTargetDevice for writing to. @return true on success */ diff --git a/src/core/copytargetdevice.h b/src/core/copytargetdevice.h index f7021d2..3306b47 100644 --- a/src/core/copytargetdevice.h +++ b/src/core/copytargetdevice.h @@ -19,9 +19,12 @@ #define KPMCORE_COPYTARGETDEVICE_H +#include "backend/corebackenddevice.h" #include "core/copytarget.h" #include "util/libpartitionmanagerexport.h" +#include + #include class Device; @@ -42,7 +45,6 @@ class CopyTargetDevice : public CopyTarget public: CopyTargetDevice(Device& d, qint64 firstbyte, qint64 lastbyte); - ~CopyTargetDevice(); public: bool open() override; @@ -63,7 +65,7 @@ public: protected: Device& m_Device; - CoreBackendDevice* m_BackendDevice; + std::unique_ptr m_BackendDevice; const qint64 m_FirstByte; const qint64 m_LastByte; }; diff --git a/src/jobs/createfilesystemjob.cpp b/src/jobs/createfilesystemjob.cpp index 8db2b47..ad20db7 100644 --- a/src/jobs/createfilesystemjob.cpp +++ b/src/jobs/createfilesystemjob.cpp @@ -60,7 +60,7 @@ bool CreateFileSystemJob::run(Report& parent) createResult = partition().fileSystem().create(*report, partition().deviceNode()); if (createResult) { if (device().type() == Device::Disk_Device) { - CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); + std::unique_ptr backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); if (backendDevice) { CoreBackendPartitionTable* backendPartitionTable = backendDevice->openPartitionTable(); @@ -75,8 +75,6 @@ bool CreateFileSystemJob::run(Report& parent) delete backendPartitionTable; } else report->line() << xi18nc("@info:progress", "Could not open partition table on device %1 to set the system type for partition %2.", device().deviceNode(), partition().deviceNode()); - - delete backendDevice; } else report->line() << xi18nc("@info:progress", "Could not open device %1 to set the system type for partition %2.", device().deviceNode(), partition().deviceNode()); } else if (device().type() == Device::LVM_Device) { diff --git a/src/jobs/createpartitionjob.cpp b/src/jobs/createpartitionjob.cpp index bb2b1b2..dae4bf7 100644 --- a/src/jobs/createpartitionjob.cpp +++ b/src/jobs/createpartitionjob.cpp @@ -51,7 +51,7 @@ bool CreatePartitionJob::run(Report& parent) Report* report = jobStarted(parent); if (device().type() == Device::Disk_Device) { - CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); + std::unique_ptr backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); if (backendDevice) { CoreBackendPartitionTable* backendPartitionTable = backendDevice->openPartitionTable(); @@ -70,8 +70,6 @@ bool CreatePartitionJob::run(Report& parent) delete backendPartitionTable; } else report->line() << xi18nc("@info:progress", "Could not open partition table on device %1 to create new partition %2.", device().deviceNode(), partition().deviceNode()); - - delete backendDevice; } else report->line() << xi18nc("@info:progress", "Could not open device %1 to create new partition %2.", device().deviceNode(), partition().deviceNode()); } else if (device().type() == Device::LVM_Device) { diff --git a/src/jobs/createpartitiontablejob.cpp b/src/jobs/createpartitiontablejob.cpp index 3d2fe0b..8ceb8b9 100644 --- a/src/jobs/createpartitiontablejob.cpp +++ b/src/jobs/createpartitiontablejob.cpp @@ -45,14 +45,12 @@ bool CreatePartitionTableJob::run(Report& parent) Report* report = jobStarted(parent); if (device().type() == Device::Disk_Device) { - CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); + std::unique_ptr backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); if (backendDevice != nullptr) { Q_ASSERT(device().partitionTable()); rval = backendDevice->createPartitionTable(*report, *device().partitionTable()); - - delete backendDevice; } else report->line() << xi18nc("@info:progress", "Creating partition table failed: Could not open device %1.", device().deviceNode()); } else if (device().type() == Device::LVM_Device) { diff --git a/src/jobs/deletefilesystemjob.cpp b/src/jobs/deletefilesystemjob.cpp index da95287..f01a3e3 100644 --- a/src/jobs/deletefilesystemjob.cpp +++ b/src/jobs/deletefilesystemjob.cpp @@ -74,7 +74,7 @@ bool DeleteFileSystemJob::run(Report& parent) return false; } - CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); + std::unique_ptr backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); if (backendDevice) { CoreBackendPartitionTable* backendPartitionTable = backendDevice->openPartitionTable(); @@ -92,7 +92,6 @@ bool DeleteFileSystemJob::run(Report& parent) } else report->line() << xi18nc("@info:progress", "Could not open partition table on device %1 to delete file system on %2.", device().deviceNode(), partition().deviceNode()); - delete backendDevice; } else report->line() << xi18nc("@info:progress", "Could not delete file system signature for partition %1: Failed to open device %2.", partition().deviceNode(), device().deviceNode()); } diff --git a/src/jobs/deletepartitionjob.cpp b/src/jobs/deletepartitionjob.cpp index 5ba7401..293496c 100644 --- a/src/jobs/deletepartitionjob.cpp +++ b/src/jobs/deletepartitionjob.cpp @@ -58,7 +58,7 @@ bool DeletePartitionJob::run(Report& parent) Report* report = jobStarted(parent); if (device().type() == Device::Disk_Device) { - CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); + std::unique_ptr backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); if (backendDevice) { CoreBackendPartitionTable* backendPartitionTable = backendDevice->openPartitionTable(); @@ -75,8 +75,6 @@ bool DeletePartitionJob::run(Report& parent) } else report->line() << xi18nc("@info:progress", "Could not open partition table on device %1 to delete partition %2.", device().deviceNode(), partition().deviceNode()); - - delete backendDevice; } else report->line() << xi18nc("@info:progress", "Deleting partition failed: Could not open device %1.", device().deviceNode()); } else if (device().type() == Device::LVM_Device) { diff --git a/src/jobs/resizefilesystemjob.cpp b/src/jobs/resizefilesystemjob.cpp index a3af897..d86108a 100644 --- a/src/jobs/resizefilesystemjob.cpp +++ b/src/jobs/resizefilesystemjob.cpp @@ -31,6 +31,8 @@ #include "util/report.h" #include "util/capacity.h" +#include + #include #include @@ -112,7 +114,7 @@ bool ResizeFileSystemJob::resizeFileSystemBackend(Report& report) { bool rval = false; - CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); + std::unique_ptr backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); if (backendDevice) { CoreBackendPartitionTable* backendPartitionTable = backendDevice->openPartitionTable(); @@ -131,7 +133,6 @@ bool ResizeFileSystemJob::resizeFileSystemBackend(Report& report) } else report.line() << xi18nc("@info:progress", "Could not open partition %1 while trying to resize the file system.", partition().deviceNode()); - delete backendDevice; } else report.line() << xi18nc("@info:progress", "Could not read geometry for partition %1 while trying to resize the file system.", partition().deviceNode()); diff --git a/src/jobs/restorefilesystemjob.cpp b/src/jobs/restorefilesystemjob.cpp index aff8d70..a9265da 100644 --- a/src/jobs/restorefilesystemjob.cpp +++ b/src/jobs/restorefilesystemjob.cpp @@ -80,7 +80,7 @@ bool RestoreFileSystemJob::run(Report& parent) // create a new file system for what was restored with the length of the image file const qint64 newLastSector = targetPartition().firstSector() + copySource.length() - 1; - CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(targetDevice()); + std::unique_ptr backendDevice = CoreBackendManager::self()->backend()->openDevice(targetDevice()); FileSystem::Type t = FileSystem::Unknown; diff --git a/src/jobs/setpartflagsjob.cpp b/src/jobs/setpartflagsjob.cpp index 2aad649..33a3070 100644 --- a/src/jobs/setpartflagsjob.cpp +++ b/src/jobs/setpartflagsjob.cpp @@ -56,7 +56,7 @@ bool SetPartFlagsJob::run(Report& parent) Report* report = jobStarted(parent); - CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); + std::unique_ptr backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); if (backendDevice) { CoreBackendPartitionTable* backendPartitionTable = backendDevice->openPartitionTable(); @@ -85,8 +85,6 @@ bool SetPartFlagsJob::run(Report& parent) delete backendPartitionTable; } else report->line() << xi18nc("@info:progress", "Could not open partition table on device %1 to set partition flags for partition %2.", device().deviceNode(), partition().deviceNode()); - - delete backendDevice; } else report->line() << xi18nc("@info:progress", "Could not open device %1 to set partition flags for partition %2.", device().deviceNode(), partition().deviceNode()); diff --git a/src/jobs/setpartgeometryjob.cpp b/src/jobs/setpartgeometryjob.cpp index 7a4fbd7..9c3a488 100644 --- a/src/jobs/setpartgeometryjob.cpp +++ b/src/jobs/setpartgeometryjob.cpp @@ -56,7 +56,7 @@ bool SetPartGeometryJob::run(Report& parent) Report* report = jobStarted(parent); if(device().type() == Device::Disk_Device) { - CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); + std::unique_ptr backendDevice = CoreBackendManager::self()->backend()->openDevice(device()); if (backendDevice) { CoreBackendPartitionTable* backendPartitionTable = backendDevice->openPartitionTable(); @@ -72,8 +72,6 @@ bool SetPartGeometryJob::run(Report& parent) delete backendPartitionTable; } - - delete backendDevice; } else report->line() << xi18nc("@info:progress", "Could not open device %1 while trying to resize/move partition %2.", device().deviceNode(), partition().deviceNode()); } else if (device().type() == Device::LVM_Device) { diff --git a/src/plugins/dummy/dummybackend.cpp b/src/plugins/dummy/dummybackend.cpp index 5abc6f2..30fe534 100644 --- a/src/plugins/dummy/dummybackend.cpp +++ b/src/plugins/dummy/dummybackend.cpp @@ -91,31 +91,27 @@ QString DummyBackend::readUUID(const QString& deviceNode) const return QString(); } -CoreBackendDevice* DummyBackend::openDevice(const Device& d) +std::unique_ptr DummyBackend::openDevice(const Device& d) { - DummyDevice* device = new DummyDevice(d.deviceNode()); + std::unique_ptr device = std::make_unique(d.deviceNode()); - if (device == nullptr || !device->open()) { - delete device; + if (!device->open()) device = nullptr; - } return device; } -CoreBackendDevice* DummyBackend::openDeviceExclusive(const Device& d) +std::unique_ptr DummyBackend::openDeviceExclusive(const Device& d) { - DummyDevice* device = new DummyDevice(d.deviceNode()); + std::unique_ptr device = std::make_unique(d.deviceNode()); - if (device == nullptr || !device->openExclusive()) { - delete device; + if (!device->openExclusive()) device = nullptr; - } return device; } -bool DummyBackend::closeDevice(CoreBackendDevice* coreDevice) +bool DummyBackend::closeDevice(std::unique_ptr coreDevice) { return coreDevice->close(); } diff --git a/src/plugins/dummy/dummybackend.h b/src/plugins/dummy/dummybackend.h index 12aad6b..a9f8fb3 100644 --- a/src/plugins/dummy/dummybackend.h +++ b/src/plugins/dummy/dummybackend.h @@ -45,9 +45,9 @@ public: void initFSSupport() override; QList scanDevices(bool excludeReadOnly = false) override; - CoreBackendDevice* openDevice(const Device& d) override; - CoreBackendDevice* openDeviceExclusive(const Device& d) override; - bool closeDevice(CoreBackendDevice* coreDevice) override; + std::unique_ptr openDevice(const Device& d) override; + std::unique_ptr openDeviceExclusive(const Device& d) override; + bool closeDevice(std::unique_ptr coreDevice) override; Device* scanDevice(const QString& deviceNode) override; FileSystem::Type detectFileSystem(const QString& deviceNode) override; QString readLabel(const QString& deviceNode) const override; diff --git a/src/plugins/sfdisk/sfdiskbackend.cpp b/src/plugins/sfdisk/sfdiskbackend.cpp index 57a510d..aa103b7 100644 --- a/src/plugins/sfdisk/sfdiskbackend.cpp +++ b/src/plugins/sfdisk/sfdiskbackend.cpp @@ -403,31 +403,27 @@ PartitionTable::Flags SfdiskBackend::availableFlags(PartitionTable::TableType ty return flags; } -CoreBackendDevice* SfdiskBackend::openDevice(const Device& d) +std::unique_ptr SfdiskBackend::openDevice(const Device& d) { - SfdiskDevice* device = new SfdiskDevice(d); + std::unique_ptr device = std::make_unique(d); - if (device == nullptr || !device->open()) { - delete device; + if (!device->open()) device = nullptr; - } return device; } -CoreBackendDevice* SfdiskBackend::openDeviceExclusive(const Device& d) +std::unique_ptr SfdiskBackend::openDeviceExclusive(const Device& d) { - SfdiskDevice* device = new SfdiskDevice(d); + std::unique_ptr device = std::make_unique(d); - if (device == nullptr || !device->openExclusive()) { - delete device; + if (!device->openExclusive()) device = nullptr; - } return device; } -bool SfdiskBackend::closeDevice(CoreBackendDevice* coreDevice) +bool SfdiskBackend::closeDevice(std::unique_ptr coreDevice) { return coreDevice->close(); } diff --git a/src/plugins/sfdisk/sfdiskbackend.h b/src/plugins/sfdisk/sfdiskbackend.h index 5cb3291..d60deb1 100644 --- a/src/plugins/sfdisk/sfdiskbackend.h +++ b/src/plugins/sfdisk/sfdiskbackend.h @@ -47,9 +47,9 @@ public: void initFSSupport() override; QList scanDevices(bool excludeReadOnly = false) override; - CoreBackendDevice* openDevice(const Device& d) override; - CoreBackendDevice* openDeviceExclusive(const Device& d) override; - bool closeDevice(CoreBackendDevice* coreDevice) override; + std::unique_ptr openDevice(const Device& d) override; + std::unique_ptr openDeviceExclusive(const Device& d) override; + bool closeDevice(std::unique_ptr coreDevice) override; Device* scanDevice(const QString& deviceNode) override; FileSystem::Type detectFileSystem(const QString& partitionPath) override; QString readLabel(const QString& deviceNode) const override;