Use std::unique_ptr for CoreBackendDevices

This commit is contained in:
Andrius Štikonas 2018-03-31 14:44:40 +01:00
parent 8fa1814f45
commit 1500eeb625
18 changed files with 43 additions and 77 deletions

View File

@ -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<CoreBackendDevice> 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<CoreBackendDevice> 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<CoreBackendDevice> coreDevice) = 0;
/**
* Emit progress.

View File

@ -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
*/

View File

@ -19,9 +19,12 @@
#define KPMCORE_COPYSOURCEDEVICE_H
#include "backend/corebackenddevice.h"
#include "core/copysource.h"
#include "util/libpartitionmanagerexport.h"
#include <memory>
#include <QtGlobal>
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<CoreBackendDevice> m_BackendDevice;
};
#endif

View File

@ -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
*/

View File

@ -19,9 +19,12 @@
#define KPMCORE_COPYTARGETDEVICE_H
#include "backend/corebackenddevice.h"
#include "core/copytarget.h"
#include "util/libpartitionmanagerexport.h"
#include <memory>
#include <QtGlobal>
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<CoreBackendDevice> m_BackendDevice;
const qint64 m_FirstByte;
const qint64 m_LastByte;
};

View File

@ -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<CoreBackendDevice> 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 <filename>%1</filename> to set the system type for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
delete backendDevice;
} else
report->line() << xi18nc("@info:progress", "Could not open device <filename>%1</filename> to set the system type for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
} else if (device().type() == Device::LVM_Device) {

View File

@ -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<CoreBackendDevice> 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 <filename>%1</filename> to create new partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
delete backendDevice;
} else
report->line() << xi18nc("@info:progress", "Could not open device <filename>%1</filename> to create new partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
} else if (device().type() == Device::LVM_Device) {

View File

@ -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<CoreBackendDevice> 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 <filename>%1</filename>.", device().deviceNode());
} else if (device().type() == Device::LVM_Device) {

View File

@ -74,7 +74,7 @@ bool DeleteFileSystemJob::run(Report& parent)
return false;
}
CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device());
std::unique_ptr<CoreBackendDevice> 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 <filename>%1</filename> to delete file system on <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
delete backendDevice;
} else
report->line() << xi18nc("@info:progress", "Could not delete file system signature for partition <filename>%1</filename>: Failed to open device <filename>%2</filename>.", partition().deviceNode(), device().deviceNode());
}

View File

@ -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<CoreBackendDevice> 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 <filename>%1</filename> to delete partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
delete backendDevice;
} else
report->line() << xi18nc("@info:progress", "Deleting partition failed: Could not open device <filename>%1</filename>.", device().deviceNode());
} else if (device().type() == Device::LVM_Device) {

View File

@ -31,6 +31,8 @@
#include "util/report.h"
#include "util/capacity.h"
#include <memory>
#include <QDebug>
#include <KLocalizedString>
@ -112,7 +114,7 @@ bool ResizeFileSystemJob::resizeFileSystemBackend(Report& report)
{
bool rval = false;
CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device());
std::unique_ptr<CoreBackendDevice> 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 <filename>%1</filename> while trying to resize the file system.", partition().deviceNode());
delete backendDevice;
} else
report.line() << xi18nc("@info:progress", "Could not read geometry for partition <filename>%1</filename> while trying to resize the file system.", partition().deviceNode());

View File

@ -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<CoreBackendDevice> backendDevice = CoreBackendManager::self()->backend()->openDevice(targetDevice());
FileSystem::Type t = FileSystem::Unknown;

View File

@ -56,7 +56,7 @@ bool SetPartFlagsJob::run(Report& parent)
Report* report = jobStarted(parent);
CoreBackendDevice* backendDevice = CoreBackendManager::self()->backend()->openDevice(device());
std::unique_ptr<CoreBackendDevice> 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 <filename>%1</filename> to set partition flags for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
delete backendDevice;
} else
report->line() << xi18nc("@info:progress", "Could not open device <filename>%1</filename> to set partition flags for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());

View File

@ -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<CoreBackendDevice> 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 <filename>%1</filename> while trying to resize/move partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
} else if (device().type() == Device::LVM_Device) {

View File

@ -91,31 +91,27 @@ QString DummyBackend::readUUID(const QString& deviceNode) const
return QString();
}
CoreBackendDevice* DummyBackend::openDevice(const Device& d)
std::unique_ptr<CoreBackendDevice> DummyBackend::openDevice(const Device& d)
{
DummyDevice* device = new DummyDevice(d.deviceNode());
std::unique_ptr<DummyDevice> device = std::make_unique<DummyDevice>(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<CoreBackendDevice> DummyBackend::openDeviceExclusive(const Device& d)
{
DummyDevice* device = new DummyDevice(d.deviceNode());
std::unique_ptr<DummyDevice> device = std::make_unique<DummyDevice>(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<CoreBackendDevice> coreDevice)
{
return coreDevice->close();
}

View File

@ -45,9 +45,9 @@ public:
void initFSSupport() override;
QList<Device*> 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<CoreBackendDevice> openDevice(const Device& d) override;
std::unique_ptr<CoreBackendDevice> openDeviceExclusive(const Device& d) override;
bool closeDevice(std::unique_ptr<CoreBackendDevice> coreDevice) override;
Device* scanDevice(const QString& deviceNode) override;
FileSystem::Type detectFileSystem(const QString& deviceNode) override;
QString readLabel(const QString& deviceNode) const override;

View File

@ -403,31 +403,27 @@ PartitionTable::Flags SfdiskBackend::availableFlags(PartitionTable::TableType ty
return flags;
}
CoreBackendDevice* SfdiskBackend::openDevice(const Device& d)
std::unique_ptr<CoreBackendDevice> SfdiskBackend::openDevice(const Device& d)
{
SfdiskDevice* device = new SfdiskDevice(d);
std::unique_ptr<SfdiskDevice> device = std::make_unique<SfdiskDevice>(d);
if (device == nullptr || !device->open()) {
delete device;
if (!device->open())
device = nullptr;
}
return device;
}
CoreBackendDevice* SfdiskBackend::openDeviceExclusive(const Device& d)
std::unique_ptr<CoreBackendDevice> SfdiskBackend::openDeviceExclusive(const Device& d)
{
SfdiskDevice* device = new SfdiskDevice(d);
std::unique_ptr<SfdiskDevice> device = std::make_unique<SfdiskDevice>(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<CoreBackendDevice> coreDevice)
{
return coreDevice->close();
}

View File

@ -47,9 +47,9 @@ public:
void initFSSupport() override;
QList<Device*> 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<CoreBackendDevice> openDevice(const Device& d) override;
std::unique_ptr<CoreBackendDevice> openDeviceExclusive(const Device& d) override;
bool closeDevice(std::unique_ptr<CoreBackendDevice> coreDevice) override;
Device* scanDevice(const QString& deviceNode) override;
FileSystem::Type detectFileSystem(const QString& partitionPath) override;
QString readLabel(const QString& deviceNode) const override;