diff --git a/src/util/externalcommandhelper.cpp b/src/util/externalcommandhelper.cpp index c2f0382..72ab7af 100644 --- a/src/util/externalcommandhelper.cpp +++ b/src/util/externalcommandhelper.cpp @@ -97,28 +97,28 @@ bool ExternalCommandHelper::readData(const QString& sourceDevice, QByteArray& bu } /** 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 offset offset where to begin writing @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; - if (!device.open(flags)) { - qCritical() << xi18n("Could not open device %1 for writing.", targetDevice); - return false; + if (!device.isOpen()) { + if (!device.open(flags)) { + qCritical() << xi18n("Could not open device %1 for writing.", device.fileName()); + return false; + } } if (!device.seek(offset)) { - qCritical() << xi18n("Could not seek position %1 on device %2.", offset, targetDevice); + qCritical() << xi18n("Could not seek position %1 on device %2.", offset, device.fileName()); return false; } if (device.write(buffer) != buffer.size()) { - qCritical() << xi18n("Could not write to device %1.", targetDevice); + qCritical() << xi18n("Could not write to device %1.", device.fileName()); return false; } @@ -233,11 +233,12 @@ QVariantMap ExternalCommandHelper::CopyFileData(const QString& sourceDevice, con bool rval = true; + QFile device(targetDevice); while (blocksCopied < blocksToCopy) { if (!(rval = readData(sourceDevice, buffer, readOffset + blockSize * blocksCopied * copyDirection, blockSize))) break; - if (!(rval = writeData(targetDevice, buffer, writeOffset + blockSize * blocksCopied * copyDirection))) + if (!(rval = writeData(device, buffer, writeOffset + blockSize * blocksCopied * copyDirection))) break; bytesWritten += buffer.size(); @@ -266,7 +267,7 @@ QVariantMap ExternalCommandHelper::CopyFileData(const QString& sourceDevice, con rval = readData(sourceDevice, buffer, lastBlockReadOffset, lastBlock); if (rval) { - rval = writeData(targetDevice, buffer, lastBlockWriteOffset); + rval = writeData(device, buffer, lastBlockWriteOffset); } if (rval) { @@ -320,8 +321,8 @@ bool ExternalCommandHelper::WriteData(const QByteArray& buffer, const QString& t } auto canonicalTargetPath = std::filesystem::canonical(targetPath); - - return writeData(QLatin1String(canonicalTargetPath.c_str()), buffer, targetOffset); + QFile device(QLatin1String(canonicalTargetPath.c_str())); + return writeData(device, buffer, targetOffset); } QVariantMap ExternalCommandHelper::RunCommand(const QString& command, const QStringList& arguments, const QByteArray& input, const int processChannelMode) diff --git a/src/util/externalcommandhelper.h b/src/util/externalcommandhelper.h index a98ac97..967d886 100644 --- a/src/util/externalcommandhelper.h +++ b/src/util/externalcommandhelper.h @@ -14,10 +14,11 @@ #include #include -#include -#include -#include #include +#include +#include +#include +#include class QDBusServiceWatcher; constexpr qint64 MiB = 1 << 30; @@ -34,7 +35,7 @@ Q_SIGNALS: public: ExternalCommandHelper(); 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: Q_SCRIPTABLE QVariantMap RunCommand(const QString& command, const QStringList& arguments, const QByteArray& input, const int processChannelMode);