From 6c14ddb043ce22706740701b1c7413198b608c21 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Thu, 9 Dec 2021 16:54:41 +0100 Subject: [PATCH] Add new job to change permission of the newly created partition --- src/core/partition.cpp | 19 ++++++++++++ src/core/partition.h | 9 ++++++ src/jobs/CMakeLists.txt | 1 + src/jobs/changepermissionsjob.cpp | 42 ++++++++++++++++++++++++++ src/jobs/changepermissionsjob.h | 43 +++++++++++++++++++++++++++ src/ops/createfilesystemoperation.cpp | 5 ++++ src/ops/newoperation.cpp | 5 ++++ 7 files changed, 124 insertions(+) create mode 100644 src/jobs/changepermissionsjob.cpp create mode 100644 src/jobs/changepermissionsjob.h diff --git a/src/core/partition.cpp b/src/core/partition.cpp index 61c59f4..eeb0bd0 100644 --- a/src/core/partition.cpp +++ b/src/core/partition.cpp @@ -368,6 +368,25 @@ void Partition::setPartitionPath(const QString& s) setNumber(-1); } + +bool Partition::execChangePermission(Report& report) +{ + if (m_permission.isEmpty()) { + return true; + } + + ExternalCommand chmodCmd(report, + QStringLiteral("chmod"), + { + m_permission, + deviceNode() + }); + + if ( chmodCmd.run() && chmodCmd.exitCode() == 0 ) + return true; + return false; +} + void Partition::setFileSystem(FileSystem* fs) { m_FileSystem = fs; diff --git a/src/core/partition.h b/src/core/partition.h index e4bbff7..5bff232 100644 --- a/src/core/partition.h +++ b/src/core/partition.h @@ -259,6 +259,14 @@ public: } void deleteFileSystem(); + void setPermissions(const QString& permission) { + m_permission = permission; + } + + // sets the disk to have write and read permissions of 777. + // userful for pendrives and such. + bool execChangePermission(Report& report); + private: void setNumber(qint32 n) { m_Number = n; @@ -275,6 +283,7 @@ private: QString m_Label; QString m_Type; QString m_UUID; + QString m_permission; quint64 m_Attributes = 0; QString m_PartitionPath; QString m_MountPoint; diff --git a/src/jobs/CMakeLists.txt b/src/jobs/CMakeLists.txt index 6aec254..a6968de 100644 --- a/src/jobs/CMakeLists.txt +++ b/src/jobs/CMakeLists.txt @@ -31,6 +31,7 @@ set(JOBS_SRC jobs/setpartflagsjob.cpp jobs/copyfilesystemjob.cpp jobs/movefilesystemjob.cpp + jobs/changepermissionsjob.cpp ) set(JOBS_LIB_HDRS diff --git a/src/jobs/changepermissionsjob.cpp b/src/jobs/changepermissionsjob.cpp new file mode 100644 index 0000000..eabca29 --- /dev/null +++ b/src/jobs/changepermissionsjob.cpp @@ -0,0 +1,42 @@ +/* + SPDX-FileCopyrightText: Tomaz Canabrava + + SPDX-License-Identifier: GPL-3.0-or-later +*/ + +#include "jobs/changepermissionsjob.h" + +#include "core/lvmdevice.h" +#include "core/partition.h" + +#include "util/report.h" + +#include + +/** Creates a new CreateVolumeGroupJob + * @param permission the new permission for the partition, in chmod style. + * @param partition the partition to change the permission. + */ +ChangePermissionJob::ChangePermissionJob(Partition& partition) : + Job(), + m_Partition(partition) +{ +} + +bool ChangePermissionJob::run(Report& parent) +{ + bool rval = false; + + Report* report = jobStarted(parent); + + rval = m_Partition.execChangePermission(*report); + + jobFinished(*report, rval); + + return rval; +} + +QString ChangePermissionJob::description() const +{ + return xi18nc("@info/plain", "Change the permissions of: %1 to %2", m_Partition.deviceNode(), m_permissions); +} diff --git a/src/jobs/changepermissionsjob.h b/src/jobs/changepermissionsjob.h new file mode 100644 index 0000000..805f180 --- /dev/null +++ b/src/jobs/changepermissionsjob.h @@ -0,0 +1,43 @@ +/* + SPDX-FileCopyrightText: 2021 Tomaz Canabrava + + SPDX-License-Identifier: GPL-3.0-or-later +*/ + +#ifndef KPMCORE_CHANGEPERMISSIONJOB_H +#define KPMCORE_CHANGEPERMISSIONJOB_H + +#include "jobs/job.h" + +class Partition; +class Report; + +class QString; + +/** Check a FileSystem. + @author Volker Lanz +*/ +class ChangePermissionJob : public Job +{ +public: + /* Permission should be set in the partition. */ + explicit ChangePermissionJob(Partition& p); + +public: + bool run(Report& parent) override; + QString description() const override; + +protected: + Partition& partition() { + return m_Partition; + } + const Partition& partition() const { + return m_Partition; + } + +private: + Partition& m_Partition; + QString m_permissions; +}; + +#endif diff --git a/src/ops/createfilesystemoperation.cpp b/src/ops/createfilesystemoperation.cpp index 4e6f40e..1f9890b 100644 --- a/src/ops/createfilesystemoperation.cpp +++ b/src/ops/createfilesystemoperation.cpp @@ -13,6 +13,7 @@ #include "jobs/deletefilesystemjob.h" #include "jobs/createfilesystemjob.h" #include "jobs/checkfilesystemjob.h" +#include "jobs/changepermissionsjob.h" #include "fs/filesystem.h" #include "fs/filesystemfactory.h" @@ -42,6 +43,10 @@ CreateFileSystemOperation::CreateFileSystemOperation(Device& d, Partition& p, Fi addJob(deleteJob()); addJob(createJob()); addJob(checkJob()); + + // if the user never configured a new permission, nothing will run, if he did, + // then we change the permissions on the newly created partition. + addJob(new ChangePermissionJob(p)); } CreateFileSystemOperation::~CreateFileSystemOperation() diff --git a/src/ops/newoperation.cpp b/src/ops/newoperation.cpp index 2c4fe51..7242af2 100644 --- a/src/ops/newoperation.cpp +++ b/src/ops/newoperation.cpp @@ -21,6 +21,7 @@ #include "jobs/setfilesystemlabeljob.h" #include "jobs/setpartflagsjob.h" #include "jobs/checkfilesystemjob.h" +#include "jobs/changepermissionsjob.h" #include "fs/filesystem.h" #include "fs/filesystemfactory.h" @@ -106,6 +107,10 @@ NewOperation::NewOperation(Device& d, Partition* p) : d_ptr->m_CheckFileSystemJob = new CheckFileSystemJob(newPartition()); addJob(checkJob()); + + // if the user never configured a new permission, nothing will run, if he did, + // then we change the permissions on the newly created partition. + addJob(new ChangePermissionJob(newPartition())); } }