diff --git a/src/core/partition.cpp b/src/core/partition.cpp index eeb0bd0..61c59f4 100644 --- a/src/core/partition.cpp +++ b/src/core/partition.cpp @@ -368,25 +368,6 @@ 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 5bff232..e4ee12a 100644 --- a/src/core/partition.h +++ b/src/core/partition.h @@ -259,14 +259,6 @@ 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; 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 #include #include +#include const std::vector 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);