From 1bdafec73b9424dfca5ab195851f2c3c626637c7 Mon Sep 17 00:00:00 2001 From: Chantara Tith Date: Fri, 13 May 2016 23:58:05 +0700 Subject: [PATCH] move mount logic from Partition to FileSystem --- src/core/partition.cpp | 19 +++++++------------ src/fs/filesystem.cpp | 19 +++++++++++++++---- src/fs/filesystem.h | 7 +++---- src/fs/linuxswap.cpp | 11 +++++++++-- src/fs/linuxswap.h | 10 ++-------- src/fs/luks.cpp | 11 ++++++----- src/fs/luks.h | 4 ++-- 7 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/core/partition.cpp b/src/core/partition.cpp index 0b4e12e..7a48476 100644 --- a/src/core/partition.cpp +++ b/src/core/partition.cpp @@ -267,16 +267,15 @@ void Partition::checkChildrenMounted() bool Partition::canMount() const { // cannot mount if already mounted - if (isMounted()) + if (isMounted()) { return false; + } - // if the file system says we can mount without mount points, that's fine - // (this is the case for swap only, actually) - if (fileSystem().canMount(deviceNode())) + if (fileSystem().canMount(deviceNode(), mountPoint())) { return true; + } - // cannot mount if we have no mount points - return !mountPoint().isEmpty(); + return false; } /** @return true if this Partition can be unmounted */ @@ -295,12 +294,8 @@ bool Partition::mount(Report& report) bool success = false; - if (fileSystem().canMount(deviceNode())) - success = fileSystem().mount(deviceNode(), mountPoint()); - else { - ExternalCommand mountCmd(report, QStringLiteral("mount"), QStringList() << QStringLiteral("-v") << deviceNode() << mountPoint()); - if (mountCmd.run() && mountCmd.exitCode() == 0) - success = true; + if (fileSystem().canMount(deviceNode(), mountPoint())) { + success = fileSystem().mount(report, deviceNode(), mountPoint()); } setMounted(success); diff --git a/src/fs/filesystem.cpp b/src/fs/filesystem.cpp index 6c8eb4b..15b83bc 100644 --- a/src/fs/filesystem.cpp +++ b/src/fs/filesystem.cpp @@ -393,16 +393,27 @@ void FileSystem::move(qint64 newStartSector) setFirstSector(newStartSector); setLastSector(newStartSector + savedLength - 1); } +bool FileSystem::canMount(const QString& deviceNode, const QString& mountPoint) const +{ + Q_UNUSED(deviceNode); + // cannot mount if we have no mount points + return !mountPoint.isEmpty(); +} /** Attempt to mount this FileSystem on a given mount point @param mountPoint the mount point to mount the FileSystem on @return true on success */ -bool FileSystem::mount(const QString &deviceNode, const QString &mountPoint) +bool FileSystem::mount(Report& report, const QString &deviceNode, const QString &mountPoint) { - Q_UNUSED(deviceNode); - Q_UNUSED(mountPoint); - + ExternalCommand mountCmd( report, + QStringLiteral("mount"), + { QStringLiteral("--verbose"), + deviceNode, + mountPoint }); + if (mountCmd.run() && mountCmd.exitCode() == 0) { + return true; + } return false; } diff --git a/src/fs/filesystem.h b/src/fs/filesystem.h index 48c394a..c46ac8e 100644 --- a/src/fs/filesystem.h +++ b/src/fs/filesystem.h @@ -175,9 +175,8 @@ public: static FileSystem::Type typeForName(const QString& s); static FileSystem::Type detectFileSystem(const QString& partitionPath); - virtual bool canMount(const QString&) const { - return false; /**< @return true if this FileSystem can be mounted */ - } + /**< @return true if this FileSystem can be mounted */ + virtual bool canMount(const QString& deviceNode, const QString& mountPoint) const; virtual bool canUnmount(const QString&) const { return true; /**< @return true if this FileSystem can be unmounted */ } @@ -185,7 +184,7 @@ public: virtual QString mountTitle() const; virtual QString unmountTitle() const; - virtual bool mount(const QString& deviceNode, const QString& mountPoint); + virtual bool mount(Report& report, const QString& deviceNode, const QString& mountPoint); virtual bool unmount(Report& report, const QString& deviceNode); qint64 firstSector() const { diff --git a/src/fs/linuxswap.cpp b/src/fs/linuxswap.cpp index 18db682..4664a3c 100644 --- a/src/fs/linuxswap.cpp +++ b/src/fs/linuxswap.cpp @@ -132,10 +132,17 @@ QString linuxswap::unmountTitle() const return i18nc("@title:menu", "Deactivate swap"); } -bool linuxswap::mount(const QString& deviceNode, const QString& mountPoint) +bool linuxswap::canMount(const QString& deviceNode, const QString& mountPoint) const { + Q_UNUSED(deviceNode); + Q_UNUSED(mountPoint); + // linux swap doesn't require mount point to activate + return true; +} + +bool linuxswap::mount(Report& report, const QString& deviceNode, const QString& mountPoint) { Q_UNUSED(mountPoint); - ExternalCommand cmd(QStringLiteral("swapon"), { deviceNode }); + ExternalCommand cmd(report, QStringLiteral("swapon"), { deviceNode }); return cmd.run(-1) && cmd.exitCode() == 0; } diff --git a/src/fs/linuxswap.h b/src/fs/linuxswap.h index 5a0c8e2..d5512dc 100644 --- a/src/fs/linuxswap.h +++ b/src/fs/linuxswap.h @@ -48,14 +48,8 @@ public: virtual bool copy(Report& report, const QString& targetDeviceNode, const QString& sourceDeviceNode) const override; virtual bool updateUUID(Report& report, const QString& deviceNode) const override; - virtual bool canMount(const QString&) const override { - return true; - } - virtual bool canUnmount(const QString&) const override { - return true; - } - - virtual bool mount(const QString& deviceNode, const QString& mountPoint) override; + virtual bool canMount(const QString& deviceNode, const QString& mountPoint) const override; + virtual bool mount(Report& report, const QString& deviceNode, const QString& mountPoint) override; virtual bool unmount(Report& report, const QString& deviceNode) override; virtual QString mountTitle() const override; diff --git a/src/fs/luks.cpp b/src/fs/luks.cpp index ece1529..8ce28b1 100644 --- a/src/fs/luks.cpp +++ b/src/fs/luks.cpp @@ -176,12 +176,12 @@ QString luks::passphrase() const return m_passphrase; } -bool luks::canMount(const QString& deviceNode) const +bool luks::canMount(const QString& deviceNode, const QString& mountPoint) const { return m_isCryptOpen && !m_isMounted && m_innerFs && - m_innerFs->canMount(mapperName(deviceNode)); + m_innerFs->canMount(mapperName(deviceNode), mountPoint); } bool luks::canUnmount(const QString& deviceNode) const @@ -360,7 +360,7 @@ qint64 luks::readUsedCapacity(const QString& deviceNode) const return -1; } -bool luks::mount(const QString& deviceNode, const QString& mountPoint) +bool luks::mount(Report& report, const QString& deviceNode, const QString& mountPoint) { if (!m_isCryptOpen) { @@ -382,9 +382,9 @@ bool luks::mount(const QString& deviceNode, const QString& mountPoint) if (mapperNode.isEmpty()) return false; - if (m_innerFs->canMount(mapperNode)) + if (m_innerFs->canMount(mapperNode, mountPoint)) { - if (m_innerFs->mount(mapperNode, mountPoint)) + if (m_innerFs->mount(report, mapperNode, mountPoint)) { m_isMounted = true; return true; @@ -392,6 +392,7 @@ bool luks::mount(const QString& deviceNode, const QString& mountPoint) } else { ExternalCommand mountCmd( + report, QStringLiteral("mount"), { QStringLiteral("-v"), mapperNode, mountPoint }); if (mountCmd.run() && mountCmd.exitCode() == 0) diff --git a/src/fs/luks.h b/src/fs/luks.h index a2e4edd..b98f397 100644 --- a/src/fs/luks.h +++ b/src/fs/luks.h @@ -118,7 +118,7 @@ public: void setPassphrase(const QString&); QString passphrase() const; - virtual bool canMount(const QString&) const override; + virtual bool canMount(const QString&, const QString&) const override; virtual bool canUnmount(const QString&) const override; bool isMounted() const; void setMounted(bool mounted); @@ -134,7 +134,7 @@ public: void loadInnerFileSystem(const QString& mapperNode); void createInnerFileSystem(Type type); - virtual bool mount(const QString& deviceNode, const QString& mountPoint) override; + virtual bool mount(Report& report, const QString& deviceNode, const QString& mountPoint) override; virtual bool unmount(Report& report, const QString& deviceNode) override; virtual FileSystem::Type type() const override; -- 2.43.2