File 0018-Do-not-repeatedly-open-and-close-file-when-writing-to-it.patch of Package kpmcore

From eddbd7a3016b62a0f0a2e5cc3610182b424e3fb5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= <andrius@stikonas.eu>
Date: Sun, 6 Feb 2022 21:01:05 +0000
Subject: [PATCH] Do not repeatedly open and close file when writing to it.

---
 src/util/externalcommandhelper.cpp | 27 ++++++++++++++-------------
 src/util/externalcommandhelper.h   |  7 ++++---
 2 files changed, 18 insertions(+), 16 deletions(-)

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 <filename>%1</filename> for writing.", targetDevice);
-        return false;
+    if (!device.isOpen()) {
+        if (!device.open(flags)) {
+            qCritical() << xi18n("Could not open device <filename>%1</filename> for writing.", device.fileName());
+            return false;
+        }
     }
 
     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;
     }
 
     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;
     }
 
@@ -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 <memory>
 #include <unordered_set>
 
+#include <QDBusContext>
 #include <QEventLoop>
-#include <QString>
+#include <QFile>
 #include <QProcess>
-#include <QDBusContext>
+#include <QString>
 
 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);
-- 
GitLab

openSUSE Build Service is sponsored by