From 0070dd2c01bcb06f9dcd5072a7e0b186b0a2d807 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 8 Dec 2021 17:10:25 +0100 Subject: [PATCH] [libcalamares] Add a convenience for reading mtab - used by umount and cleartempmounts (in future) --- src/libcalamares/partition/Mount.cpp | 30 +++++++++++++++++++++- src/libcalamares/partition/Mount.h | 38 ++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/libcalamares/partition/Mount.cpp b/src/libcalamares/partition/Mount.cpp index 89e17a885..6bc3a7cd2 100644 --- a/src/libcalamares/partition/Mount.cpp +++ b/src/libcalamares/partition/Mount.cpp @@ -14,6 +14,7 @@ #include "partition/Sync.h" #include "utils/CalamaresUtilsSystem.h" #include "utils/Logger.h" +#include "utils/String.h" #include #include @@ -92,7 +93,7 @@ struct TemporaryMount::Private TemporaryMount::TemporaryMount( const QString& devicePath, const QString& filesystemName, const QString& options ) - : m_d( std::make_unique() ) + : m_d( std::make_unique< Private >() ) { m_d->m_devicePath = devicePath; m_d->m_mountDir.setAutoRemove( false ); @@ -123,5 +124,32 @@ TemporaryMount::path() const return m_d ? m_d->m_mountDir.path() : QString(); } +QList< MtabInfo > +MtabInfo::fromMtabFilteredByPrefix( const QString& mountPrefix, const QString& mtabPath ) +{ + QFile f( mtabPath.isEmpty() ? "/etc/mtab" : mtabPath ); + if ( !f.open( QIODevice::ReadOnly ) ) + { + return {}; + } + + QTextStream in( &f ); + QList< MtabInfo > l; + while ( !f.atEnd() ) + { + QStringList line = in.readLine().split( ' ', SplitSkipEmptyParts ); + if ( line.length() == 3 && !line[ 0 ].startsWith( '#' ) ) + { + // Lines have format: , so check + // the mountpoint field. Everything starts with an empty string. + if ( line[ 1 ].startsWith( mountPrefix ) ) + { + l.append( { line[ 0 ], line[ 1 ] } ); + } + } + } + return l; +} + } // namespace Partition } // namespace CalamaresUtils diff --git a/src/libcalamares/partition/Mount.h b/src/libcalamares/partition/Mount.h index d088b108f..f772c33a4 100644 --- a/src/libcalamares/partition/Mount.h +++ b/src/libcalamares/partition/Mount.h @@ -14,6 +14,7 @@ #include "DllMacro.h" +#include #include #include @@ -50,6 +51,13 @@ DLLEXPORT int mount( const QString& devicePath, */ DLLEXPORT int unmount( const QString& path, const QStringList& options = QStringList() ); + +/** @brief Mount and automatically unmount a device + * + * The TemporaryMount object mounts a filesystem, and is like calling + * the mount() function, above. When the object is destroyed, unmount() + * is called with suitable options to undo the original mount. + */ class DLLEXPORT TemporaryMount { public: @@ -68,6 +76,36 @@ private: std::unique_ptr< Private > m_d; }; + +/** @brief Information about a mount point from /etc/mtab + * + * Entries in /etc/mtab are of the form: + * This struct only stores device and mountpoint. + * + * The main way of getting these structs is to call fromMtab() to read + * an /etc/mtab-like file and storing all of the entries from it. + */ +struct DLLEXPORT MtabInfo +{ + QString device; + QString mountPoint; + + /** @brief Reads an mtab-like file and returns the entries from it + * + * When @p mtabPath is given, that file is read. If the given name is + * empty (e.g. the default) then /etc/mtab is read, instead. + * + * If @p mountPrefix is given, then only entries that have a mount point + * that starts with that prefix are returned. + */ + static QList< MtabInfo > fromMtabFilteredByPrefix( const QString& mountPrefix = QString(), + const QString& mtabPath = QString() ); + /// @brief Predicate to sort MtabInfo objects by device-name + static bool deviceOrder( const MtabInfo& a, const MtabInfo& b ) { return a.device > b.device; } + /// @brief Predicate to sort MtabInfo objects by mount-point + static bool mountPointOrder( const MtabInfo& a, const MtabInfo& b ) { return a.mountPoint > b.mountPoint; } +}; + } // namespace Partition } // namespace CalamaresUtils