Do not repeatedly open and close file when reading from it.
This commit is contained in:
parent
eddbd7a301
commit
fd7f9b87a9
|
@ -71,25 +71,25 @@ ExternalCommandHelper::ExternalCommandHelper()
|
||||||
@param size the number of bytes to read
|
@param size the number of bytes to read
|
||||||
@return true on success
|
@return true on success
|
||||||
*/
|
*/
|
||||||
bool ExternalCommandHelper::readData(const QString& sourceDevice, QByteArray& buffer, const qint64 offset, const qint64 size)
|
bool ExternalCommandHelper::readData(QFile& device, QByteArray& buffer, const qint64 offset, const qint64 size)
|
||||||
{
|
{
|
||||||
QFile device(sourceDevice);
|
if (!device.isOpen()) {
|
||||||
|
if (!device.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
|
||||||
if (!device.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
|
qCritical() << xi18n("Could not open device <filename>%1</filename> for reading.", device.fileName());
|
||||||
qCritical() << xi18n("Could not open device <filename>%1</filename> for reading.", sourceDevice);
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sequential devices such as /dev/zero or /dev/urandom return false on seek().
|
// Sequential devices such as /dev/zero or /dev/urandom return false on seek().
|
||||||
if (!device.isSequential() && !device.seek(offset)) {
|
if (!device.isSequential() && !device.seek(offset)) {
|
||||||
qCritical() << xi18n("Could not seek position %1 on device <filename>%2</filename>.", offset, sourceDevice);
|
qCritical() << xi18n("Could not seek position %1 on device <filename>%2</filename>.", offset, device.fileName());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer = device.read(size);
|
buffer = device.read(size);
|
||||||
|
|
||||||
if (size != buffer.size()) {
|
if (size != buffer.size()) {
|
||||||
qCritical() << xi18n("Could not read from device <filename>%1</filename>.", sourceDevice);
|
qCritical() << xi18n("Could not read from device <filename>%1</filename>.", device.fileName());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,12 +233,13 @@ QVariantMap ExternalCommandHelper::CopyFileData(const QString& sourceDevice, con
|
||||||
|
|
||||||
bool rval = true;
|
bool rval = true;
|
||||||
|
|
||||||
QFile device(targetDevice);
|
QFile target(targetDevice);
|
||||||
|
QFile source(sourceDevice);
|
||||||
while (blocksCopied < blocksToCopy) {
|
while (blocksCopied < blocksToCopy) {
|
||||||
if (!(rval = readData(sourceDevice, buffer, readOffset + blockSize * blocksCopied * copyDirection, blockSize)))
|
if (!(rval = readData(source, buffer, readOffset + blockSize * blocksCopied * copyDirection, blockSize)))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (!(rval = writeData(device, buffer, writeOffset + blockSize * blocksCopied * copyDirection)))
|
if (!(rval = writeData(target, buffer, writeOffset + blockSize * blocksCopied * copyDirection)))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
bytesWritten += buffer.size();
|
bytesWritten += buffer.size();
|
||||||
|
@ -264,10 +265,10 @@ QVariantMap ExternalCommandHelper::CopyFileData(const QString& sourceDevice, con
|
||||||
const qint64 lastBlockWriteOffset = copyDirection == CopyDirection::Left ? writeOffset + blockSize * blocksCopied : targetOffset;
|
const qint64 lastBlockWriteOffset = copyDirection == CopyDirection::Left ? writeOffset + blockSize * blocksCopied : targetOffset;
|
||||||
reportText = xi18nc("@info:progress", "Copying remainder of block size %1 from %2 to %3.", lastBlock, lastBlockReadOffset, lastBlockWriteOffset);
|
reportText = xi18nc("@info:progress", "Copying remainder of block size %1 from %2 to %3.", lastBlock, lastBlockReadOffset, lastBlockWriteOffset);
|
||||||
Q_EMIT report(reportText);
|
Q_EMIT report(reportText);
|
||||||
rval = readData(sourceDevice, buffer, lastBlockReadOffset, lastBlock);
|
rval = readData(source, buffer, lastBlockReadOffset, lastBlock);
|
||||||
|
|
||||||
if (rval) {
|
if (rval) {
|
||||||
rval = writeData(device, buffer, lastBlockWriteOffset);
|
rval = writeData(target, buffer, lastBlockWriteOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rval) {
|
if (rval) {
|
||||||
|
@ -298,7 +299,8 @@ QByteArray ExternalCommandHelper::ReadData(const QString& device, const qint64 o
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray buffer;
|
QByteArray buffer;
|
||||||
bool rval = readData(device, buffer, offset, length);
|
QFile sourceDevice(device);
|
||||||
|
bool rval = readData(sourceDevice, buffer, offset, length);
|
||||||
if (rval) {
|
if (rval) {
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ Q_SIGNALS:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ExternalCommandHelper();
|
ExternalCommandHelper();
|
||||||
bool readData(const QString& sourceDevice, QByteArray& buffer, const qint64 offset, const qint64 size);
|
bool readData(QFile& device, QByteArray& buffer, const qint64 offset, const qint64 size);
|
||||||
bool writeData(QFile& device, const QByteArray& buffer, const qint64 offset);
|
bool writeData(QFile& device, const QByteArray& buffer, const qint64 offset);
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
|
|
Loading…
Reference in New Issue