Remove backend readData/writeData functions.
This commit is contained in:
parent
06139c85ac
commit
ac6dc0eb87
|
@ -91,23 +91,6 @@ public:
|
|||
*/
|
||||
virtual bool createPartitionTable(Report& report, const PartitionTable& ptable) = 0;
|
||||
|
||||
/**
|
||||
* Read data from an opened device into a buffer.
|
||||
* @param buffer the buffer to write the read data to
|
||||
* @param offset offset byte where to start reading on the device
|
||||
* @param size the number of bytes to read
|
||||
* @return true on success
|
||||
*/
|
||||
virtual bool readData(QByteArray& buffer, qint64 offset, qint64 size) = 0;
|
||||
|
||||
/**
|
||||
* Write data from a buffer to an exclusively opened device.
|
||||
* @param buffer the buffer with the data
|
||||
* @param offset offset byte where to start writing to the device
|
||||
* @return true on success
|
||||
*/
|
||||
virtual bool writeData(QByteArray& buffer, qint64 offset) = 0;
|
||||
|
||||
protected:
|
||||
void setExclusive(bool b) {
|
||||
m_Exclusive = b;
|
||||
|
|
|
@ -43,7 +43,6 @@ protected:
|
|||
public:
|
||||
virtual bool open() = 0;
|
||||
virtual QString path() const = 0;
|
||||
virtual bool readData(QByteArray& buffer, qint64 readOffset, qint64 size) = 0;
|
||||
virtual qint64 length() const = 0;
|
||||
virtual bool overlaps(const CopyTarget& target) const = 0;
|
||||
|
||||
|
|
|
@ -63,22 +63,6 @@ qint64 CopySourceDevice::length() const
|
|||
return lastByte() - firstByte() + 1;
|
||||
}
|
||||
|
||||
/** Reads a given number of bytes from the Device into the given buffer.
|
||||
|
||||
Note that @p readOffset must be greater or equal than zero.
|
||||
|
||||
@param buffer the buffer to store the read bytes in
|
||||
@param readOffset the offset to begin reading
|
||||
@param size the number of bytes to read
|
||||
|
||||
@return true if successful
|
||||
*/
|
||||
bool CopySourceDevice::readData(QByteArray& buffer, qint64 readOffset, qint64 size)
|
||||
{
|
||||
Q_ASSERT(readOffset >= 0);
|
||||
return m_BackendDevice->readData(buffer, readOffset, size);
|
||||
}
|
||||
|
||||
/** Checks if this CopySourceDevice overlaps with the given CopyTarget
|
||||
@param target the CopyTarget to check overlapping with
|
||||
@return true if overlaps
|
||||
|
|
|
@ -45,7 +45,6 @@ public:
|
|||
|
||||
public:
|
||||
bool open() override;
|
||||
bool readData(QByteArray& buffer, qint64 readOffset, qint64 size) override;
|
||||
qint64 length() const override;
|
||||
bool overlaps(const CopyTarget& target) const override;
|
||||
|
||||
|
|
|
@ -45,18 +45,3 @@ qint64 CopySourceFile::length() const
|
|||
{
|
||||
return QFileInfo(file()).size();
|
||||
}
|
||||
|
||||
/** Reads the given number of bytes from the file into the given buffer.
|
||||
@param buffer buffer to store the bytes read in
|
||||
@param readOffset offset where to begin reading
|
||||
@param size the number of bytes to read
|
||||
@return true on success
|
||||
*/
|
||||
bool CopySourceFile::readData(QByteArray& buffer, qint64 readOffset, qint64 size)
|
||||
{
|
||||
if (!file().seek(readOffset))
|
||||
return false;
|
||||
|
||||
buffer = file().read(size);
|
||||
return !buffer.isEmpty();
|
||||
}
|
||||
|
|
|
@ -40,7 +40,6 @@ public:
|
|||
|
||||
public:
|
||||
bool open() override;
|
||||
bool readData(QByteArray& buffer, qint64 readOffset, qint64 size) override;
|
||||
qint64 length() const override;
|
||||
|
||||
bool overlaps(const CopyTarget&) const override {
|
||||
|
|
|
@ -43,17 +43,3 @@ qint64 CopySourceShred::length() const
|
|||
{
|
||||
return size();
|
||||
}
|
||||
|
||||
/** Reads the given number of bytes from the source into the given buffer.
|
||||
@param buffer buffer to store the data read in
|
||||
@param readOffset offset where to begin reading (unused)
|
||||
@param size the number of bytes to read
|
||||
@return true on success
|
||||
*/
|
||||
bool CopySourceShred::readData(QByteArray& buffer, qint64 readOffset, qint64 size)
|
||||
{
|
||||
Q_UNUSED(readOffset);
|
||||
|
||||
buffer = sourceFile().read(size);
|
||||
return !buffer.isEmpty();
|
||||
}
|
||||
|
|
|
@ -39,7 +39,6 @@ public:
|
|||
|
||||
public:
|
||||
bool open() override;
|
||||
bool readData(QByteArray& buffer, qint64 readOffset, qint64 size) override;
|
||||
qint64 length() const override;
|
||||
|
||||
bool overlaps(const CopyTarget&) const override {
|
||||
|
|
|
@ -41,7 +41,6 @@ protected:
|
|||
|
||||
public:
|
||||
virtual bool open() = 0;
|
||||
virtual bool writeData(QByteArray& buffer, qint64 writeOffset) = 0;
|
||||
virtual qint64 firstByte() const = 0;
|
||||
virtual qint64 lastByte() const = 0;
|
||||
virtual QString path() const = 0;
|
||||
|
|
|
@ -52,25 +52,6 @@ bool CopyTargetDevice::open()
|
|||
return m_BackendDevice != nullptr;
|
||||
}
|
||||
|
||||
/** Writes the given number of bytes to the Device.
|
||||
|
||||
Note that @p writeOffset must be greater or equal than zero.
|
||||
|
||||
@param buffer the data to write
|
||||
@param writeOffset where to start writing on the Device
|
||||
@return true on success
|
||||
*/
|
||||
bool CopyTargetDevice::writeData(QByteArray& buffer, qint64 writeOffset)
|
||||
{
|
||||
Q_ASSERT(writeOffset >= 0);
|
||||
bool rval = m_BackendDevice->writeData(buffer, writeOffset);
|
||||
|
||||
if (rval)
|
||||
setBytesWritten(bytesWritten() + buffer.size());
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
QString CopyTargetDevice::path() const
|
||||
{
|
||||
return m_Device.deviceNode();
|
||||
|
|
|
@ -46,7 +46,6 @@ public:
|
|||
|
||||
public:
|
||||
bool open() override;
|
||||
bool writeData(QByteArray& buffer, qint64 writeOffset) override;
|
||||
qint64 firstByte() const override {
|
||||
return m_FirstByte; /**< @return the first byte to write to */
|
||||
}
|
||||
|
|
|
@ -33,21 +33,3 @@ bool CopyTargetFile::open()
|
|||
{
|
||||
return file().open(QIODevice::WriteOnly | QIODevice::Truncate);
|
||||
}
|
||||
|
||||
/** Writes the given number of bytes from the given buffer to the file.
|
||||
@param buffer the data to write
|
||||
@param writeOffset where in the file to start writing
|
||||
@return true on success
|
||||
*/
|
||||
bool CopyTargetFile::writeData(QByteArray& buffer, qint64 writeOffset)
|
||||
{
|
||||
if (!file().seek(writeOffset))
|
||||
return false;
|
||||
|
||||
bool rval = file().write(buffer) == buffer.size();
|
||||
|
||||
if (rval)
|
||||
setBytesWritten(bytesWritten() + buffer.size());
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
|
|
@ -40,7 +40,6 @@ public:
|
|||
|
||||
public:
|
||||
bool open() override;
|
||||
bool writeData(QByteArray& buffer, qint64 writeOffset) override;
|
||||
|
||||
qint64 firstByte() const override {
|
||||
return 0; /**< @return always 0 for a file */
|
||||
|
|
|
@ -66,26 +66,3 @@ bool DummyDevice::createPartitionTable(Report& report, const PartitionTable& pta
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DummyDevice::readData(QByteArray& buffer, qint64 offset, qint64 size)
|
||||
{
|
||||
Q_UNUSED(buffer);
|
||||
Q_UNUSED(offset);
|
||||
Q_UNUSED(size);
|
||||
|
||||
if (!isExclusive())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DummyDevice::writeData(QByteArray& buffer, qint64 offset)
|
||||
{
|
||||
Q_UNUSED(buffer);
|
||||
Q_UNUSED(offset);
|
||||
|
||||
if (!isExclusive())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -44,9 +44,6 @@ public:
|
|||
CoreBackendPartitionTable* openPartitionTable() override;
|
||||
|
||||
bool createPartitionTable(Report& report, const PartitionTable& ptable) override;
|
||||
|
||||
bool readData(QByteArray& buffer, qint64 offset, qint64 size) override;
|
||||
bool writeData(QByteArray& buffer, qint64 offset) override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -110,23 +110,3 @@ bool LibPartedDevice::createPartitionTable(Report& report, const PartitionTable&
|
|||
|
||||
return LibPartedPartitionTable::commit(disk);
|
||||
}
|
||||
|
||||
bool LibPartedDevice::readData(QByteArray& buffer, qint64 offset, qint64 size)
|
||||
{
|
||||
if (!isExclusive())
|
||||
return false;
|
||||
|
||||
void *data = malloc(size);
|
||||
bool rval = ped_device_read(pedDevice(), data, offset / pedDevice()->sector_size, size / pedDevice()->sector_size);
|
||||
buffer = QByteArray(static_cast<char*>(data), size);
|
||||
free(data);
|
||||
return rval;
|
||||
}
|
||||
|
||||
bool LibPartedDevice::writeData(QByteArray& buffer, qint64 offset)
|
||||
{
|
||||
if (!isExclusive())
|
||||
return false;
|
||||
|
||||
return ped_device_write(pedDevice(), static_cast<const void *>(buffer.constData()), offset / pedDevice()->sector_size, buffer.size() / pedDevice()->sector_size);
|
||||
}
|
||||
|
|
|
@ -47,9 +47,6 @@ public:
|
|||
|
||||
bool createPartitionTable(Report& report, const PartitionTable& ptable) override;
|
||||
|
||||
bool readData(QByteArray& buffer, qint64 offset, qint64 size) override;
|
||||
bool writeData(QByteArray& buffer, qint64 offset) override;
|
||||
|
||||
protected:
|
||||
PedDevice* pedDevice() {
|
||||
return m_PedDevice;
|
||||
|
|
|
@ -79,40 +79,3 @@ bool SfdiskDevice::createPartitionTable(Report& report, const PartitionTable& pt
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SfdiskDevice::readData(QByteArray& buffer, qint64 offset, qint64 size)
|
||||
{
|
||||
if (!isExclusive())
|
||||
return false;
|
||||
|
||||
ExternalCommand ddCommand(QStringLiteral("dd"), {
|
||||
QStringLiteral("skip=") + QString::number(offset),
|
||||
QStringLiteral("bs=") + QString::number(size),
|
||||
QStringLiteral("count=1"),
|
||||
QStringLiteral("iflag=skip_bytes"),
|
||||
QStringLiteral("if=") + m_device->deviceNode() }, QProcess::SeparateChannels);
|
||||
if (ddCommand.run(-1) && ddCommand.exitCode() == 0) {
|
||||
buffer = ddCommand.rawOutput();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SfdiskDevice::writeData(QByteArray& buffer, qint64 offset)
|
||||
{
|
||||
if (!isExclusive())
|
||||
return false;
|
||||
|
||||
ExternalCommand ddCommand(QStringLiteral("dd"), {
|
||||
QStringLiteral("of=") + m_device->deviceNode(),
|
||||
QStringLiteral("seek=") + QString::number(offset),
|
||||
QStringLiteral("bs=1M"),
|
||||
QStringLiteral("oflag=seek_bytes"),
|
||||
QStringLiteral("conv=fsync") }, QProcess::SeparateChannels);
|
||||
if ( ddCommand.write(buffer) && ddCommand.start(-1) && ddCommand.waitFor() && ddCommand.exitCode() == 0 ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -46,9 +46,6 @@ public:
|
|||
|
||||
bool createPartitionTable(Report& report, const PartitionTable& ptable) override;
|
||||
|
||||
bool readData(QByteArray& buffer, qint64 offset, qint64 size) override;
|
||||
bool writeData(QByteArray& buffer, qint64 offset) override;
|
||||
|
||||
private:
|
||||
const Device *m_device;
|
||||
};
|
||||
|
|
|
@ -25,7 +25,13 @@
|
|||
|
||||
#include <KLocalizedString>
|
||||
|
||||
|
||||
/** Reads the given number of bytes from the sourceDevice into the given buffer.
|
||||
@param sourceDevice device or file to read from
|
||||
@param buffer buffer to store the bytes read in
|
||||
@param offset offset where to begin reading
|
||||
@param size the number of bytes to read
|
||||
@return true on success
|
||||
*/
|
||||
bool ExternalCommandHelper::readData(QString& sourceDevice, QByteArray& buffer, qint64 offset, qint64 size)
|
||||
{
|
||||
QFile device(sourceDevice);
|
||||
|
@ -50,6 +56,12 @@ bool ExternalCommandHelper::readData(QString& sourceDevice, QByteArray& buffer,
|
|||
return true;
|
||||
}
|
||||
|
||||
/** Writes the data from buffer to a given device or file.
|
||||
@param targetDevice device or file to write to
|
||||
@param buffer the data that we write
|
||||
@param offset offset where to begin writing
|
||||
@return true on success
|
||||
*/
|
||||
bool ExternalCommandHelper::writeData(QString &targetDevice, QByteArray& buffer, qint64 offset)
|
||||
{
|
||||
QFile device(targetDevice);
|
||||
|
|
Loading…
Reference in New Issue