[libcalamares] Rearrange filesystem-use API

- make the functies that take a GS* first-class
- use the convenience functions from JobQueue for the others
- inline so only the explicit-GS* functions are in the library
This commit is contained in:
Adriaan de Groot 2021-02-08 14:41:17 +01:00
parent 488631824d
commit f3681a533e
3 changed files with 50 additions and 47 deletions

View File

@ -17,12 +17,14 @@
static const QString fsUse_key = QStringLiteral( "filesystem_use" );
STATICTEST
bool
isFilesystemUsedGS( const Calamares::GlobalStorage& gs, const QString& filesystemType )
CalamaresUtils::Partition::isFilesystemUsedGS( const Calamares::GlobalStorage* gs, const QString& filesystemType )
{
const QVariantMap fsUse = gs.value( fsUse_key ).toMap();
if ( !gs )
{
return false;
}
const QVariantMap fsUse = gs->value( fsUse_key ).toMap();
if ( fsUse.contains( filesystemType ) )
{
const auto v = fsUse.value( filesystemType );
@ -31,35 +33,13 @@ isFilesystemUsedGS( const Calamares::GlobalStorage& gs, const QString& filesyste
return false;
}
STATICTEST void
useFilesystemGS( Calamares::GlobalStorage& gs, const QString& filesystemType, bool used )
{
QVariantMap existingMap = gs.contains( fsUse_key ) ? gs.value( fsUse_key ).toMap() : QVariantMap();
existingMap.insert( filesystemType, used );
gs.insert( fsUse_key, existingMap );
}
bool
CalamaresUtils::Partition::isFilesystemUsedGS( const QString& filesystemType )
{
const auto* jq = Calamares::JobQueue::instance();
const auto* gs = jq ? jq->globalStorage() : nullptr;
if ( !gs )
{
return false;
}
return isFilesystemUsedGS( *gs, filesystemType );
}
void
CalamaresUtils::Partition::useFilesystemGS( const QString& filesystemType, bool used )
CalamaresUtils::Partition::useFilesystemGS( Calamares::GlobalStorage* gs, const QString& filesystemType, bool used )
{
const auto* jq = Calamares::JobQueue::instance();
auto* gs = jq ? jq->globalStorage() : nullptr;
if ( gs )
{
useFilesystemGS( *gs, filesystemType, used );
QVariantMap existingMap = gs->contains( fsUse_key ) ? gs->value( fsUse_key ).toMap() : QVariantMap();
existingMap.insert( filesystemType, used );
gs->insert( fsUse_key, existingMap );
}
}

View File

@ -17,6 +17,7 @@
#define PARTITION_GLOBAL_H
#include "DllMacro.h"
#include "JobQueue.h"
#ifdef WITH_KPMCORE4API
#include "FileSystem.h"
@ -39,14 +40,37 @@ namespace Partition
* The filesystem name should be the untranslated name. Filesystem
* names are **lower**cased when used as keys.
*/
void DLLEXPORT useFilesystemGS( const QString& filesystemType, bool used );
void DLLEXPORT useFilesystemGS( Calamares::GlobalStorage* gs, const QString& filesystemType, bool used );
/** @brief Reads from global storage whether the filesystem type is used
*
* Reads from the global storage key *filesystem_use* and returns
* the boolean value stored in subkey @p filesystemType. Returns
* @c false if the subkey is not set at all.
*
* The filesystem name should be the untranslated name. Filesystem
* names are **lower**cased when used as keys.
*/
bool DLLEXPORT isFilesystemUsedGS( const QString& filesystemType );
bool DLLEXPORT isFilesystemUsedGS( const Calamares::GlobalStorage* gs, const QString& filesystemType );
/** @brief Convenience function for using "the" Global Storage
*
* @see useFilesystemGS(const QString&, bool)
*/
inline void
useFilesystemGS( const QString& filesystemType, bool used )
{
useFilesystemGS( Calamares::JobQueue::instanceGlobalStorage(), filesystemType, used );
}
/** @brief Convenience function for using "the" Global Storage
*
* @see isFilesystemUsedGS(const QString&);
*/
inline bool
isFilesystemUsedGS( const QString& filesystemType )
{
return isFilesystemUsedGS( Calamares::JobQueue::instanceGlobalStorage(), filesystemType );
}
#ifdef WITH_KPMCORE4API
/** @brief Mark a particular filesystem type as used (or not)
@ -54,7 +78,7 @@ bool DLLEXPORT isFilesystemUsedGS( const QString& filesystemType );
* See useFilesystemGS(const QString&, bool); this method uses the filesystem type
* enumeration to pick the name.
*/
void
inline void
useFilesystemGS( FileSystem::Type filesystem, bool used )
{
useFilesystemGS( untranslatedFS( filesystem ), used );
@ -64,7 +88,7 @@ useFilesystemGS( FileSystem::Type filesystem, bool used )
*
* See isFilesystemUsedGS(const QString&).
*/
bool
inline bool
isFilesystemUsedGS( FileSystem::Type filesystem )
{
return isFilesystemUsedGS( untranslatedFS( filesystem ) );

View File

@ -8,6 +8,7 @@
*
*/
#include "Global.h"
#include "PartitionSize.h"
#include "GlobalStorage.h"
@ -16,11 +17,6 @@
#include <QObject>
#include <QtTest/QtTest>
// Implementation details being tested
extern bool isFilesystemUsedGS( const Calamares::GlobalStorage& gs, const QString& filesystemType );
extern void useFilesystemGS( Calamares::GlobalStorage& gs, const QString& filesystemType, bool used );
using SizeUnit = CalamaresUtils::Partition::SizeUnit;
using PartitionSize = CalamaresUtils::Partition::PartitionSize;
@ -165,6 +161,9 @@ PartitionServiceTests::testUnitNormalisation()
void
PartitionServiceTests::testFilesystemGS()
{
using CalamaresUtils::Partition::isFilesystemUsedGS;
using CalamaresUtils::Partition::useFilesystemGS;
// Some filesystems names, they don't have to be real
const QStringList fsNames { "ext4", "zfs", "berries", "carrot" };
// Predicate to return whether we consider this FS in use
@ -174,7 +173,7 @@ PartitionServiceTests::testFilesystemGS()
Calamares::GlobalStorage gs;
for ( const auto& s : fsNames )
{
useFilesystemGS( gs, s, pred( s ) );
useFilesystemGS( &gs, s, pred( s ) );
}
QVERIFY( gs.contains( "filesystem_use" ) );
@ -185,16 +184,16 @@ PartitionServiceTests::testFilesystemGS()
for ( const auto& s : fsNames )
{
QCOMPARE( isFilesystemUsedGS( gs, s ), pred( s ) );
QCOMPARE( isFilesystemUsedGS( &gs, s ), pred( s ) );
}
QCOMPARE( isFilesystemUsedGS( gs, QStringLiteral( "derp" ) ), false );
QCOMPARE( isFilesystemUsedGS( gs, QString() ), false );
QCOMPARE( isFilesystemUsedGS( &gs, QStringLiteral( "derp" ) ), false );
QCOMPARE( isFilesystemUsedGS( &gs, QString() ), false );
// But I can set a value for QString!
useFilesystemGS( gs, QString(), true );
QCOMPARE( isFilesystemUsedGS( gs, QString() ), true );
useFilesystemGS( &gs, QString(), true );
QCOMPARE( isFilesystemUsedGS( &gs, QString() ), true );
// .. and replace it again
useFilesystemGS( gs, QString(), false );
QCOMPARE( isFilesystemUsedGS( gs, QString() ), false );
useFilesystemGS( &gs, QString(), false );
QCOMPARE( isFilesystemUsedGS( &gs, QString() ), false );
// Now there is one more key
{
const auto map = gs.value( "filesystem_use" ).toMap();