File 0001-Move-the-changePosixPermission-to-the-Filesystem.patch of Package kpmcore

From 9093b27bd819f6418dd654497dff2bc7cada539a Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <tcanabrava@kde.org>
Date: Sun, 12 Dec 2021 19:36:14 +0000
Subject: [PATCH] Move the changePosixPermission to the Filesystem

And implement it on ext2, 3, 4. I don't know all the filesystems
that can have posix permissions, those three will do. if we need
more in the future we implement them, it's two lines of code.
---
 src/core/partition.cpp            | 19 ----------
 src/core/partition.h              |  8 ----
 src/fs/ext2.h                     |  3 ++
 src/fs/ext3.h                     |  3 ++
 src/fs/ext4.h                     |  3 ++
 src/fs/filesystem.cpp             | 63 +++++++++++++++++++++++++++++++
 src/fs/filesystem.h               | 11 ++++++
 src/jobs/changepermissionsjob.cpp |  4 +-
 8 files changed, 86 insertions(+), 28 deletions(-)

diff --git a/src/fs/ext2.h b/src/fs/ext2.h
index 07c6abf..58e51b8 100644
--- a/src/fs/ext2.h
+++ b/src/fs/ext2.h
@@ -91,6 +91,9 @@ public:
     SupportTool supportToolName() const override;
     bool supportToolFound() const override;
 
+    QString posixPermissions() const override { return implPosixPermissions();  };
+    void setPosixPermissions(const QString& permissions) override { implSetPosixPermissions(permissions); };
+
 public:
     static CommandSupportType m_GetUsed;
     static CommandSupportType m_GetLabel;
diff --git a/src/fs/ext3.h b/src/fs/ext3.h
index af4b1d3..99b6f6f 100644
--- a/src/fs/ext3.h
+++ b/src/fs/ext3.h
@@ -42,6 +42,9 @@ public:
     CommandSupportType supportGrowOnline() const override {
         return m_Grow;
     }
+
+    QString posixPermissions() const override { return implPosixPermissions();  };
+    void setPosixPermissions(const QString& permissions) override { implSetPosixPermissions(permissions); };
 };
 }
 
diff --git a/src/fs/ext4.h b/src/fs/ext4.h
index 821b6a7..b631a8c 100644
--- a/src/fs/ext4.h
+++ b/src/fs/ext4.h
@@ -42,6 +42,9 @@ public:
     CommandSupportType supportGrowOnline() const override {
         return m_Grow;
     }
+
+    QString posixPermissions() const override { return implPosixPermissions();  };
+    void setPosixPermissions(const QString& permissions) override { implSetPosixPermissions(permissions); };
 };
 }
 
diff --git a/src/fs/filesystem.cpp b/src/fs/filesystem.cpp
index e046fa5..ec57a1f 100644
--- a/src/fs/filesystem.cpp
+++ b/src/fs/filesystem.cpp
@@ -31,6 +31,7 @@
 #include <QFileInfo>
 #include <QStandardPaths>
 #include <QStorageInfo>
+#include <QTemporaryDir>
 
 const std::vector<QColor> FileSystem::defaultColorCode =
 {
@@ -80,6 +81,7 @@ struct FileSystemPrivate {
     qint64 m_SectorsUsed;
     QString m_Label;
     QString m_UUID;
+    QString m_posixPermissions;
     QStringList m_AvailableFeatures;
     QVariantMap m_Features;
 };
@@ -126,6 +128,67 @@ FileSystem::~FileSystem()
 {
 }
 
+QString FileSystem::implPosixPermissions() const
+{
+    return d->m_posixPermissions;
+}
+
+void FileSystem::implSetPosixPermissions(const QString& permissions)
+{
+    d->m_posixPermissions = permissions;
+}
+
+
+bool FileSystem::execChangePosixPermission(Report& report, const QString& deviceNode)
+{
+    // do nothing if the posix permissions is not used here.
+    if (d->m_posixPermissions.isEmpty()) {
+        return true;
+    }
+
+    QTemporaryDir tmpDir;
+
+    ExternalCommand mountCmd(report, QStringLiteral("mount"),
+                             { deviceNode, tmpDir.path() });
+
+    bool step =  mountCmd.run() && mountCmd.exitCode() == 0;
+    if (!step) {
+        return false;
+    }
+
+    ExternalCommand chmodCmd(report, QStringLiteral("chmod"),
+                             // forcing recursive, should be empty but
+                             // programming is weird.
+                            {
+                                d->m_posixPermissions,
+                                tmpDir.path(),
+                                QStringLiteral("-R")
+                            });
+
+    const bool chmodStep =  chmodCmd.run() && chmodCmd.exitCode() == 0;
+
+    ExternalCommand umountCmd(report, QStringLiteral("umount"),
+                            // forcing recursive, should be empty but
+                             // programming is weird.
+                            {
+                                deviceNode,
+                            });
+
+    const bool umountStep =  umountCmd.run() && umountCmd.exitCode() == 0;
+
+    // we can't return false if chmodStep fails because we still need to umount
+    // the drive.
+    if (!chmodStep) {
+        return false;
+    }
+
+    if (!umountStep) {
+        return false;
+    }
+
+    return true;
+}
+
 /** Reads the capacity in use on this FileSystem
     @param deviceNode the device node for the Partition the FileSystem is on
     @return the used capacity in bytes or -1 in case of an error
diff --git a/src/fs/filesystem.h b/src/fs/filesystem.h
index c7b8093..724d7b1 100644
--- a/src/fs/filesystem.h
+++ b/src/fs/filesystem.h
@@ -113,6 +113,9 @@ protected:
     FileSystem(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, FileSystem::Type type);
     FileSystem(qint64 firstsector, qint64 lastsector, qint64 sectorsused, const QString& label, const QVariantMap& features, FileSystem::Type type);
 
+    QString implPosixPermissions() const;
+    void implSetPosixPermissions(const QString& permissions);
+
 public:
     virtual ~FileSystem();
 
@@ -199,6 +202,14 @@ public:
     virtual SupportTool supportToolName() const;
     virtual bool supportToolFound() const;
 
+    virtual QString posixPermissions() const { return QString{}; };
+    virtual void setPosixPermissions(const QString& permissions) { Q_UNUSED(permissions); };
+
+    // Tries to change the posix permission on the filesystem, if the
+    // filesystem supports it. by supports I mean reimplements `posixPermissions()`
+    // and setPosixPermissions.
+    bool execChangePosixPermission(Report& report, const QString& deviceNode);
+
     /**
      * Returns the (possibly translated) name of the type of this filesystem.
      * @see nameForType()
diff --git a/src/jobs/changepermissionsjob.cpp b/src/jobs/changepermissionsjob.cpp
index eabca29..98320b6 100644
--- a/src/jobs/changepermissionsjob.cpp
+++ b/src/jobs/changepermissionsjob.cpp
@@ -27,9 +27,11 @@ bool ChangePermissionJob::run(Report& parent)
 {
     bool rval = false;
 
+    auto &fs = m_Partition.fileSystem();
+
     Report* report = jobStarted(parent);
 
-    rval = m_Partition.execChangePermission(*report);
+    rval = fs.execChangePosixPermission(*report, m_Partition.deviceNode());
 
     jobFinished(*report, rval);
 
-- 
2.35.1

openSUSE Build Service is sponsored by