Do not repeatedly open and close file when writing to it.
This commit is contained in:
parent
fb11a02d24
commit
eddbd7a301
|
@ -97,28 +97,28 @@ bool ExternalCommandHelper::readData(const QString& sourceDevice, QByteArray& bu
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Writes the data from buffer to a given device.
|
/** Writes the data from buffer to a given device.
|
||||||
@param targetDevice device or file to write to
|
@param device device or file to write to
|
||||||
@param buffer the data that we write
|
@param buffer the data that we write
|
||||||
@param offset offset where to begin writing
|
@param offset offset where to begin writing
|
||||||
@return true on success
|
@return true on success
|
||||||
*/
|
*/
|
||||||
bool ExternalCommandHelper::writeData(const QString &targetDevice, const QByteArray& buffer, const qint64 offset)
|
bool ExternalCommandHelper::writeData(QFile& device, const QByteArray& buffer, const qint64 offset)
|
||||||
{
|
{
|
||||||
QFile device(targetDevice);
|
|
||||||
|
|
||||||
auto flags = QIODevice::WriteOnly | QIODevice::Unbuffered;
|
auto flags = QIODevice::WriteOnly | QIODevice::Unbuffered;
|
||||||
if (!device.open(flags)) {
|
if (!device.isOpen()) {
|
||||||
qCritical() << xi18n("Could not open device <filename>%1</filename> for writing.", targetDevice);
|
if (!device.open(flags)) {
|
||||||
return false;
|
qCritical() << xi18n("Could not open device <filename>%1</filename> for writing.", device.fileName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!device.seek(offset)) {
|
if (!device.seek(offset)) {
|
||||||
qCritical() << xi18n("Could not seek position %1 on device <filename>%2</filename>.", offset, targetDevice);
|
qCritical() << xi18n("Could not seek position %1 on device <filename>%2</filename>.", offset, device.fileName());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (device.write(buffer) != buffer.size()) {
|
if (device.write(buffer) != buffer.size()) {
|
||||||
qCritical() << xi18n("Could not write to device <filename>%1</filename>.", targetDevice);
|
qCritical() << xi18n("Could not write to device <filename>%1</filename>.", device.fileName());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,11 +233,12 @@ QVariantMap ExternalCommandHelper::CopyFileData(const QString& sourceDevice, con
|
||||||
|
|
||||||
bool rval = true;
|
bool rval = true;
|
||||||
|
|
||||||
|
QFile device(targetDevice);
|
||||||
while (blocksCopied < blocksToCopy) {
|
while (blocksCopied < blocksToCopy) {
|
||||||
if (!(rval = readData(sourceDevice, buffer, readOffset + blockSize * blocksCopied * copyDirection, blockSize)))
|
if (!(rval = readData(sourceDevice, buffer, readOffset + blockSize * blocksCopied * copyDirection, blockSize)))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (!(rval = writeData(targetDevice, buffer, writeOffset + blockSize * blocksCopied * copyDirection)))
|
if (!(rval = writeData(device, buffer, writeOffset + blockSize * blocksCopied * copyDirection)))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
bytesWritten += buffer.size();
|
bytesWritten += buffer.size();
|
||||||
|
@ -266,7 +267,7 @@ QVariantMap ExternalCommandHelper::CopyFileData(const QString& sourceDevice, con
|
||||||
rval = readData(sourceDevice, buffer, lastBlockReadOffset, lastBlock);
|
rval = readData(sourceDevice, buffer, lastBlockReadOffset, lastBlock);
|
||||||
|
|
||||||
if (rval) {
|
if (rval) {
|
||||||
rval = writeData(targetDevice, buffer, lastBlockWriteOffset);
|
rval = writeData(device, buffer, lastBlockWriteOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rval) {
|
if (rval) {
|
||||||
|
@ -320,8 +321,8 @@ bool ExternalCommandHelper::WriteData(const QByteArray& buffer, const QString& t
|
||||||
}
|
}
|
||||||
|
|
||||||
auto canonicalTargetPath = std::filesystem::canonical(targetPath);
|
auto canonicalTargetPath = std::filesystem::canonical(targetPath);
|
||||||
|
QFile device(QLatin1String(canonicalTargetPath.c_str()));
|
||||||
return writeData(QLatin1String(canonicalTargetPath.c_str()), buffer, targetOffset);
|
return writeData(device, buffer, targetOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantMap ExternalCommandHelper::RunCommand(const QString& command, const QStringList& arguments, const QByteArray& input, const int processChannelMode)
|
QVariantMap ExternalCommandHelper::RunCommand(const QString& command, const QStringList& arguments, const QByteArray& input, const int processChannelMode)
|
||||||
|
|
|
@ -14,10 +14,11 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
|
||||||
#include <QEventLoop>
|
|
||||||
#include <QString>
|
|
||||||
#include <QProcess>
|
|
||||||
#include <QDBusContext>
|
#include <QDBusContext>
|
||||||
|
#include <QEventLoop>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QProcess>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
class QDBusServiceWatcher;
|
class QDBusServiceWatcher;
|
||||||
constexpr qint64 MiB = 1 << 30;
|
constexpr qint64 MiB = 1 << 30;
|
||||||
|
@ -34,7 +35,7 @@ Q_SIGNALS:
|
||||||
public:
|
public:
|
||||||
ExternalCommandHelper();
|
ExternalCommandHelper();
|
||||||
bool readData(const QString& sourceDevice, QByteArray& buffer, const qint64 offset, const qint64 size);
|
bool readData(const QString& sourceDevice, QByteArray& buffer, const qint64 offset, const qint64 size);
|
||||||
bool writeData(const QString& targetDevice, const QByteArray& buffer, const qint64 offset);
|
bool writeData(QFile& device, const QByteArray& buffer, const qint64 offset);
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
Q_SCRIPTABLE QVariantMap RunCommand(const QString& command, const QStringList& arguments, const QByteArray& input, const int processChannelMode);
|
Q_SCRIPTABLE QVariantMap RunCommand(const QString& command, const QStringList& arguments, const QByteArray& input, const int processChannelMode);
|
||||||
|
|
Loading…
Reference in New Issue