sfdisk: store a pointer to the device in SfdiskPartitionTable class.

This commit is contained in:
Andrius Štikonas 2017-12-07 20:50:54 +00:00
parent 41473119b1
commit 55b8cd3e6b
9 changed files with 27 additions and 26 deletions

View File

@ -17,8 +17,8 @@
#include "backend/corebackenddevice.h" #include "backend/corebackenddevice.h"
CoreBackendDevice::CoreBackendDevice(const QString& device_node) : CoreBackendDevice::CoreBackendDevice(const QString& deviceNode) :
m_DeviceNode(device_node), m_DeviceNode(deviceNode),
m_Exclusive(false) m_Exclusive(false)
{ {
} }

View File

@ -40,7 +40,7 @@ class Report;
class LIBKPMCORE_EXPORT CoreBackendDevice class LIBKPMCORE_EXPORT CoreBackendDevice
{ {
public: public:
CoreBackendDevice(const QString& device_node); CoreBackendDevice(const QString& deviceNode);
virtual ~CoreBackendDevice() {} virtual ~CoreBackendDevice() {}
public: public:

View File

@ -23,8 +23,8 @@
#include "util/globallog.h" #include "util/globallog.h"
#include "util/report.h" #include "util/report.h"
DummyDevice::DummyDevice(const QString& device_node) : DummyDevice::DummyDevice(const QString& deviceNode) :
CoreBackendDevice(device_node) CoreBackendDevice(deviceNode)
{ {
} }

View File

@ -33,7 +33,7 @@ class DummyDevice : public CoreBackendDevice
Q_DISABLE_COPY(DummyDevice) Q_DISABLE_COPY(DummyDevice)
public: public:
DummyDevice(const QString& device_node); DummyDevice(const QString& deviceNode);
~DummyDevice(); ~DummyDevice();
public: public:

View File

@ -365,7 +365,7 @@ PartitionTable::Flags SfdiskBackend::availableFlags(PartitionTable::TableType ty
CoreBackendDevice* SfdiskBackend::openDevice(const Device& d) CoreBackendDevice* SfdiskBackend::openDevice(const Device& d)
{ {
SfdiskDevice* device = new SfdiskDevice(d.deviceNode()); SfdiskDevice* device = new SfdiskDevice(d);
if (device == nullptr || !device->open()) { if (device == nullptr || !device->open()) {
delete device; delete device;
@ -377,7 +377,7 @@ CoreBackendDevice* SfdiskBackend::openDevice(const Device& d)
CoreBackendDevice* SfdiskBackend::openDeviceExclusive(const Device& d) CoreBackendDevice* SfdiskBackend::openDeviceExclusive(const Device& d)
{ {
SfdiskDevice* device = new SfdiskDevice(d.deviceNode()); SfdiskDevice* device = new SfdiskDevice(d);
if (device == nullptr || !device->openExclusive()) { if (device == nullptr || !device->openExclusive()) {
delete device; delete device;

View File

@ -23,9 +23,9 @@
#include "util/externalcommand.h" #include "util/externalcommand.h"
#include "util/report.h" #include "util/report.h"
SfdiskDevice::SfdiskDevice(const QString& deviceNode) : SfdiskDevice::SfdiskDevice(const Device& d) :
CoreBackendDevice(deviceNode), CoreBackendDevice(d.deviceNode()),
m_deviceNode(deviceNode) m_device(&d)
{ {
} }
@ -55,7 +55,7 @@ bool SfdiskDevice::close()
CoreBackendPartitionTable* SfdiskDevice::openPartitionTable() CoreBackendPartitionTable* SfdiskDevice::openPartitionTable()
{ {
CoreBackendPartitionTable* ptable = new SfdiskPartitionTable(m_deviceNode); CoreBackendPartitionTable* ptable = new SfdiskPartitionTable(m_device);
if (ptable == nullptr || !ptable->open()) { if (ptable == nullptr || !ptable->open()) {
delete ptable; delete ptable;
@ -73,7 +73,7 @@ bool SfdiskDevice::createPartitionTable(Report& report, const PartitionTable& pt
else else
tableType = ptable.typeName().toLocal8Bit(); tableType = ptable.typeName().toLocal8Bit();
ExternalCommand createCommand(report, QStringLiteral("sfdisk"), { m_deviceNode } ); ExternalCommand createCommand(report, QStringLiteral("sfdisk"), { m_device->deviceNode() } );
if ( createCommand.start(-1) && createCommand.write(QByteArrayLiteral("label: ") + tableType + if ( createCommand.start(-1) && createCommand.write(QByteArrayLiteral("label: ") + tableType +
QByteArrayLiteral("\nwrite\n")) && createCommand.waitFor() ) { QByteArrayLiteral("\nwrite\n")) && createCommand.waitFor() ) {
return createCommand.output().contains(QStringLiteral("Script header accepted.")); return createCommand.output().contains(QStringLiteral("Script header accepted."));
@ -92,7 +92,7 @@ bool SfdiskDevice::readData(QByteArray& buffer, qint64 offset, qint64 size)
QStringLiteral("bs=") + QString::number(size), QStringLiteral("bs=") + QString::number(size),
QStringLiteral("count=1"), QStringLiteral("count=1"),
QStringLiteral("iflag=skip_bytes"), QStringLiteral("iflag=skip_bytes"),
QStringLiteral("if=") + m_deviceNode }, QProcess::SeparateChannels); QStringLiteral("if=") + m_device->deviceNode() }, QProcess::SeparateChannels);
if (ddCommand.run(-1) && ddCommand.exitCode() == 0) { if (ddCommand.run(-1) && ddCommand.exitCode() == 0) {
buffer = ddCommand.rawOutput(); buffer = ddCommand.rawOutput();
return true; return true;
@ -107,7 +107,7 @@ bool SfdiskDevice::writeData(QByteArray& buffer, qint64 offset)
return false; return false;
ExternalCommand ddCommand(QStringLiteral("dd"), { ExternalCommand ddCommand(QStringLiteral("dd"), {
QStringLiteral("of=") + m_deviceNode, QStringLiteral("of=") + m_device->deviceNode(),
QStringLiteral("seek=") + QString::number(offset), QStringLiteral("seek=") + QString::number(offset),
QStringLiteral("bs=1M"), QStringLiteral("bs=1M"),
QStringLiteral("oflag=seek_bytes"), QStringLiteral("oflag=seek_bytes"),

View File

@ -20,6 +20,7 @@
#define SFDISKDEVICE__H #define SFDISKDEVICE__H
#include "backend/corebackenddevice.h" #include "backend/corebackenddevice.h"
#include "core/device.h"
#include <QtGlobal> #include <QtGlobal>
@ -33,7 +34,7 @@ class SfdiskDevice : public CoreBackendDevice
Q_DISABLE_COPY(SfdiskDevice); Q_DISABLE_COPY(SfdiskDevice);
public: public:
SfdiskDevice(const QString& deviceNode); SfdiskDevice(const Device& d);
~SfdiskDevice(); ~SfdiskDevice();
public: public:
@ -49,7 +50,7 @@ public:
bool writeData(QByteArray& buffer, qint64 offset) override; bool writeData(QByteArray& buffer, qint64 offset) override;
private: private:
QString m_deviceNode; const Device *m_device;
}; };
#endif #endif

View File

@ -37,9 +37,9 @@
#include <KLocalizedString> #include <KLocalizedString>
SfdiskPartitionTable::SfdiskPartitionTable(const QString& deviceNode) : SfdiskPartitionTable::SfdiskPartitionTable(const Device* d) :
CoreBackendPartitionTable(), CoreBackendPartitionTable(),
m_deviceNode(deviceNode) m_device(d)
{ {
} }
@ -83,7 +83,7 @@ QString SfdiskPartitionTable::createPartition(Report& report, const Partition& p
return partition.devicePath() + rem.captured(1); return partition.devicePath() + rem.captured(1);
} }
report.line() << xi18nc("@info:progress", "Failed to add partition <filename>%1</filename> to device <filename>%2</filename>.", partition.deviceNode(), m_deviceNode); report.line() << xi18nc("@info:progress", "Failed to add partition <filename>%1</filename> to device <filename>%2</filename>.", partition.deviceNode(), m_device->deviceNode());
return QString(); return QString();
} }
@ -167,20 +167,20 @@ bool SfdiskPartitionTable::setPartitionSystemType(Report& report, const Partitio
bool SfdiskPartitionTable::setFlag(Report& report, const Partition& partition, PartitionTable::Flag flag, bool state) bool SfdiskPartitionTable::setFlag(Report& report, const Partition& partition, PartitionTable::Flag flag, bool state)
{ {
if (flag == PartitionTable::FlagBoot && state == true) { if (flag == PartitionTable::FlagBoot && state == true) {
ExternalCommand sfdiskCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--activate"), m_deviceNode, QString::number(partition.number()) } ); ExternalCommand sfdiskCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--activate"), m_device->deviceNode(), QString::number(partition.number()) } );
if (sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0) if (sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0)
return true; return true;
else else
return false; return false;
} else if (flag == PartitionTable::FlagBoot && state == false) { } else if (flag == PartitionTable::FlagBoot && state == false) {
ExternalCommand sfdiskCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--activate"), m_deviceNode, QStringLiteral("-") } ); ExternalCommand sfdiskCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--activate"), m_device->deviceNode(), QStringLiteral("-") } );
if (sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0) if (sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0)
return true; return true;
// FIXME: Do not return false since we have no way of checking if partition table is MBR // FIXME: Do not return false since we have no way of checking if partition table is MBR
} }
if (flag == PartitionTable::FlagEsp && state == true) { if (flag == PartitionTable::FlagEsp && state == true) {
ExternalCommand sfdiskCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--part-type"), m_deviceNode, QString::number(partition.number()), ExternalCommand sfdiskCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--part-type"), m_device->deviceNode(), QString::number(partition.number()),
QStringLiteral("C12A7328-F81F-11D2-BA4B-00A0C93EC93B") } ); QStringLiteral("C12A7328-F81F-11D2-BA4B-00A0C93EC93B") } );
if (sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0) if (sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0)
return true; return true;
@ -191,7 +191,7 @@ bool SfdiskPartitionTable::setFlag(Report& report, const Partition& partition, P
setPartitionSystemType(report, partition); setPartitionSystemType(report, partition);
if (flag == PartitionTable::FlagBiosGrub && state == true) { if (flag == PartitionTable::FlagBiosGrub && state == true) {
ExternalCommand sfdiskCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--part-type"), m_deviceNode, QString::number(partition.number()), ExternalCommand sfdiskCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--part-type"), m_device->deviceNode(), QString::number(partition.number()),
QStringLiteral("21686148-6449-6E6F-744E-656564454649") } ); QStringLiteral("21686148-6449-6E6F-744E-656564454649") } );
if (sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0) if (sfdiskCommand.run(-1) && sfdiskCommand.exitCode() == 0)
return true; return true;

View File

@ -32,7 +32,7 @@ class Partition;
class SfdiskPartitionTable : public CoreBackendPartitionTable class SfdiskPartitionTable : public CoreBackendPartitionTable
{ {
public: public:
SfdiskPartitionTable(const QString& deviceNode); SfdiskPartitionTable(const Device *d);
~SfdiskPartitionTable(); ~SfdiskPartitionTable();
public: public:
@ -50,7 +50,7 @@ public:
bool setFlag(Report& report, const Partition& partition, PartitionTable::Flag flag, bool state) override; bool setFlag(Report& report, const Partition& partition, PartitionTable::Flag flag, bool state) override;
private: private:
QString m_deviceNode; const Device *m_device;
}; };
#endif #endif