[libcalamares] Add a convenience for reading mtab

- used by umount and cleartempmounts (in future)
This commit is contained in:
Adriaan de Groot 2021-12-08 17:10:25 +01:00
parent 4c5ee3b53a
commit 0070dd2c01
2 changed files with 67 additions and 1 deletions

View File

@ -14,6 +14,7 @@
#include "partition/Sync.h" #include "partition/Sync.h"
#include "utils/CalamaresUtilsSystem.h" #include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "utils/String.h"
#include <QDir> #include <QDir>
#include <QTemporaryDir> #include <QTemporaryDir>
@ -123,5 +124,32 @@ TemporaryMount::path() const
return m_d ? m_d->m_mountDir.path() : QString(); 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: <device> <mountpoint> <options>, 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 Partition
} // namespace CalamaresUtils } // namespace CalamaresUtils

View File

@ -14,6 +14,7 @@
#include "DllMacro.h" #include "DllMacro.h"
#include <QList>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
@ -50,6 +51,13 @@ DLLEXPORT int mount( const QString& devicePath,
*/ */
DLLEXPORT int unmount( const QString& path, const QStringList& options = QStringList() ); 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 class DLLEXPORT TemporaryMount
{ {
public: public:
@ -68,6 +76,36 @@ private:
std::unique_ptr< Private > m_d; std::unique_ptr< Private > m_d;
}; };
/** @brief Information about a mount point from /etc/mtab
*
* Entries in /etc/mtab are of the form: <device> <mountpoint> <other>
* 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 Partition
} // namespace CalamaresUtils } // namespace CalamaresUtils