File 0001-Write-into-crontab-instead-of-replacing-the-file.patch of Package kcron

From db2185dc9d9a423b0375880522ec85acd6decd1c Mon Sep 17 00:00:00 2001
From: Albert Astals Cid <aacid@kde.org>
Date: Wed, 23 Feb 2022 23:42:06 +0100
Subject: [PATCH] Write into crontab instead of replacing the file

Keeps permissions, owners, etc.
---
 src/helper/kcronhelper.cpp | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/src/helper/kcronhelper.cpp b/src/helper/kcronhelper.cpp
index 96fe8a0..eafb4ac 100644
--- a/src/helper/kcronhelper.cpp
+++ b/src/helper/kcronhelper.cpp
@@ -31,29 +31,38 @@
 ActionReply KcronHelper::save(const QVariantMap &args)
 {
     qCDebug(KCM_CRON_HELPER_LOG) << "running actions";
-    const QString source = args[QLatin1String("source")].toString();
-    const QString destination = QStringLiteral("/etc/crontab");
+
+    QByteArray newCronData;
     {
-        QFile destinationFile(destination);
-        if (destinationFile.exists() && !destinationFile.remove()) {
+        const QString source = args[QLatin1String("source")].toString();
+        QFile sourceFile(source);
+        if (!sourceFile.open(QIODevice::ReadOnly)) {
+            qCWarning(KCM_CRON_HELPER_LOG) << "can't open source file for reading" << source << sourceFile.errorString();
             ActionReply reply = ActionReply::HelperErrorReply();
-            qCWarning(KCM_CRON_HELPER_LOG) << "can't remove file" << destinationFile.errorString();
-            reply.setErrorDescription(destinationFile.errorString());
+            reply.setErrorDescription(sourceFile.errorString());
             return reply;
         }
+
+        newCronData = sourceFile.readAll();
     }
+
     {
-        QFile sourceFile(source);
-        if (!sourceFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ReadGroup | QFileDevice::ReadOther)) {
-            qCWarning(KCM_CRON_HELPER_LOG) << "can't change permissions to 644";
-        }
-        if (!sourceFile.copy(destination)) {
-            qCWarning(KCM_CRON_HELPER_LOG) << "can't write into the system file" << sourceFile.errorString();
+        const QString destination = QStringLiteral("/etc/crontab");
+        QFile destinationFile(destination);
+        if (!destinationFile.open(QIODevice::WriteOnly)) {
             ActionReply reply = ActionReply::HelperErrorReply();
-            reply.setErrorDescription(sourceFile.errorString());
+            qCWarning(KCM_CRON_HELPER_LOG) << "can't open destination file for writing" << destinationFile.errorString();
+            reply.setErrorDescription(destinationFile.errorString());
             return reply;
         }
+
+        if (destinationFile.write(newCronData) < 0) {
+            ActionReply reply = ActionReply::HelperErrorReply();
+            qCWarning(KCM_CRON_HELPER_LOG) << "writing to destination file failed" << destinationFile.errorString();
+            reply.setErrorDescription(destinationFile.errorString());
+        }
     }
+
     return ActionReply::SuccessReply();
 }
 
-- 
2.35.1

openSUSE Build Service is sponsored by