From afdf431b774a5a58abf8ef2521e126a91772fb7d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 8 Feb 2021 11:13:49 +0100 Subject: [PATCH 1/8] [libcalamares] Add partition service for managing global storage - the global storage key filesystem_use has a structured meaning, so give it a (trivial-ish) API for reading and writing. --- src/libcalamares/CMakeLists.txt | 1 + src/libcalamares/partition/Global.cpp | 78 +++++++++++++++++++++++++++ src/libcalamares/partition/Global.h | 66 +++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 src/libcalamares/partition/Global.cpp create mode 100644 src/libcalamares/partition/Global.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index a084c120d..404ac129f 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -63,6 +63,7 @@ set( libSources packages/Globals.cpp # Partition service + partition/Global.cpp partition/Mount.cpp partition/PartitionSize.cpp partition/Sync.cpp diff --git a/src/libcalamares/partition/Global.cpp b/src/libcalamares/partition/Global.cpp new file mode 100644 index 000000000..7203f6b55 --- /dev/null +++ b/src/libcalamares/partition/Global.cpp @@ -0,0 +1,78 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + * + */ +#include "Global.h" + +#include "FileSystem.h" +#include "GlobalStorage.h" +#include "JobQueue.h" + +#include + +static const QString fsUse_key = QStringLiteral( "filesystem_use" ); + +STATICTEST +bool +isFilesystemUsedGS( const Calamares::GlobalStorage& gs, const QString& filesystemType ) +{ + + const QVariantMap fsUse = gs.value( fsUse_key ).toMap(); + if ( fsUse.contains( filesystemType ) ) + { + const auto v = fsUse.value( filesystemType ); + return v.toBool(); + } + 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 ) +{ + const auto* jq = Calamares::JobQueue::instance(); + auto* gs = jq ? jq->globalStorage() : nullptr; + + if ( gs ) + { + useFilesystemGS( *gs, filesystemType, used ); + } +} + +#ifdef WITH_KPMCORE4API +bool +CalamaresUtils::Partition::isFilesystemUsedGS( FileSystem::Type filesystem ) +{ + return isFilesystemUsedGS( untranslatedFS( filesystem ) ); +} +void +CalamaresUtils::Partition::useFilesystemGS( FileSystem::Type filesystem, bool used ) +{ + useFilesystemGS( untranslatedFS( filesystem ), used ); +} +#endif diff --git a/src/libcalamares/partition/Global.h b/src/libcalamares/partition/Global.h new file mode 100644 index 000000000..3164d06fc --- /dev/null +++ b/src/libcalamares/partition/Global.h @@ -0,0 +1,66 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + * + */ + +/* + * This is the API for manipulating Global Storage keys related to + * filesystems and partitions. + */ + +#ifndef PARTITION_GLOBAL_H +#define PARTITION_GLOBAL_H + +#include "DllMacro.h" + +#ifdef WITH_KPMCORE4API +#include +#endif + +namespace CalamaresUtils +{ +namespace Partition +{ +/** @brief Mark a particular filesystem type as used (or not) + * + * Filesystems are marked used (or not) in the global storage + * key *filesystem_use*. Sub-keys are the filesystem name, + * and the values are boolean; filesystems that are used in + * the target system are marked with @c true. Unused filesystems + * may be unmarked, or may be marked @c false. + * + * 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 ); +/** @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. + */ +bool DLLEXPORT isFilesystemUsedGS( const QString& filesystemType ); + +#ifdef WITH_KPMCORE4API +/** @brief Mark a particular filesystem type as used (or not) + * + * See useFilesystemGS(const QString&, bool); this method uses the filesystem type + * enumeration to pick the name. + */ +void DLLEXPORT useFilesystemGS( FileSystem::Type filesystem, bool used ); +/* @brief Reads from global storage whether the typesystem type is used + * + * See isFilesystemUsedGS(const QString&). + */ +bool DLLEXPORT isFilesystemUsedGS( FileSystem::Type filesystem ); + +#endif +} // namespace Partition +} // namespace CalamaresUtils + +#endif From 75eb2c3cd4482543afec01a995f7249ced694c8a Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 8 Feb 2021 11:21:15 +0100 Subject: [PATCH 2/8] [libcalamares] Add tests for filesystem_use service --- src/libcalamares/CMakeLists.txt | 3 + src/libcalamares/partition/Tests.cpp | 93 ++++++++++++++++++++++++---- src/libcalamares/partition/Tests.h | 33 ---------- 3 files changed, 84 insertions(+), 45 deletions(-) delete mode 100644 src/libcalamares/partition/Tests.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index 404ac129f..69533cfff 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -242,7 +242,10 @@ calamares_add_test( calamares_add_test( libcalamarespartitiontest SOURCES + partition/Global.cpp partition/Tests.cpp + LIBRARIES + ${OPTIONAL_PRIVATE_LIBRARIES} ) if( KPMcore_FOUND ) diff --git a/src/libcalamares/partition/Tests.cpp b/src/libcalamares/partition/Tests.cpp index cd2922ee2..512a23110 100644 --- a/src/libcalamares/partition/Tests.cpp +++ b/src/libcalamares/partition/Tests.cpp @@ -8,32 +8,54 @@ * */ -#include "Tests.h" - #include "PartitionSize.h" +#include "GlobalStorage.h" +#include "utils/Logger.h" + +#include +#include + +// 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; Q_DECLARE_METATYPE( SizeUnit ) -#include "utils/Logger.h" +class PartitionServiceTests : public QObject +{ + Q_OBJECT +public: + PartitionServiceTests(); + ~PartitionServiceTests() override; -#include +private Q_SLOTS: + void initTestCase(); -QTEST_GUILESS_MAIN( PartitionSizeTests ) + void testUnitComparison_data(); + void testUnitComparison(); -PartitionSizeTests::PartitionSizeTests() {} + void testUnitNormalisation_data(); + void testUnitNormalisation(); -PartitionSizeTests::~PartitionSizeTests() {} + void testFilesystemGS(); +}; + +PartitionServiceTests::PartitionServiceTests() {} + +PartitionServiceTests::~PartitionServiceTests() {} void -PartitionSizeTests::initTestCase() +PartitionServiceTests::initTestCase() { } void -PartitionSizeTests::testUnitComparison_data() +PartitionServiceTests::testUnitComparison_data() { QTest::addColumn< SizeUnit >( "u1" ); QTest::addColumn< SizeUnit >( "u2" ); @@ -71,7 +93,7 @@ original_compare( SizeUnit m_unit, SizeUnit other_m_unit ) } void -PartitionSizeTests::testUnitComparison() +PartitionServiceTests::testUnitComparison() { QFETCH( SizeUnit, u1 ); QFETCH( SizeUnit, u2 ); @@ -98,7 +120,7 @@ constexpr qint64 operator""_qi( unsigned long long m ) } void -PartitionSizeTests::testUnitNormalisation_data() +PartitionServiceTests::testUnitNormalisation_data() { QTest::addColumn< SizeUnit >( "u1" ); QTest::addColumn< int >( "v" ); @@ -131,7 +153,7 @@ PartitionSizeTests::testUnitNormalisation_data() } void -PartitionSizeTests::testUnitNormalisation() +PartitionServiceTests::testUnitNormalisation() { QFETCH( SizeUnit, u1 ); QFETCH( int, v ); @@ -139,3 +161,50 @@ PartitionSizeTests::testUnitNormalisation() QCOMPARE( PartitionSize( v, u1 ).toBytes(), bytes ); } + +void +PartitionServiceTests::testFilesystemGS() +{ + // 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 + auto pred = []( const QString& s ) { return !s.startsWith( 'z' ); }; + + // Fill the GS + Calamares::GlobalStorage gs; + for ( const auto& s : fsNames ) + { + useFilesystemGS( gs, s, pred( s ) ); + } + + QVERIFY( gs.contains( "filesystem_use" ) ); + { + const auto map = gs.value( "filesystem_use" ).toMap(); + QCOMPARE( map.count(), fsNames.count() ); + } + + for ( const auto& s : fsNames ) + { + QCOMPARE( isFilesystemUsedGS( gs, s ), pred( s ) ); + } + 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 ); + // .. and replace it again + useFilesystemGS( gs, QString(), false ); + QCOMPARE( isFilesystemUsedGS( gs, QString() ), false ); + // Now there is one more key + { + const auto map = gs.value( "filesystem_use" ).toMap(); + QCOMPARE( map.count(), fsNames.count() + 1 ); + } +} + + +QTEST_GUILESS_MAIN( PartitionServiceTests ) + +#include "utils/moc-warnings.h" + +#include "Tests.moc" diff --git a/src/libcalamares/partition/Tests.h b/src/libcalamares/partition/Tests.h deleted file mode 100644 index 0d6f77a76..000000000 --- a/src/libcalamares/partition/Tests.h +++ /dev/null @@ -1,33 +0,0 @@ -/* === This file is part of Calamares - === - * - * SPDX-FileCopyrightText: 2019 Adriaan de Groot - * SPDX-License-Identifier: GPL-3.0-or-later - * - * Calamares is Free Software: see the License-Identifier above. - * - * - */ - -#ifndef LIBCALAMARES_PARTITION_TESTS_H -#define LIBCALAMARES_PARTITION_TESTS_H - -#include - -class PartitionSizeTests : public QObject -{ - Q_OBJECT -public: - PartitionSizeTests(); - ~PartitionSizeTests() override; - -private Q_SLOTS: - void initTestCase(); - - void testUnitComparison_data(); - void testUnitComparison(); - - void testUnitNormalisation_data(); - void testUnitNormalisation(); -}; - -#endif From 488631824dd6036ea476fba4fbb716347755945f Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 8 Feb 2021 14:01:32 +0100 Subject: [PATCH 3/8] [libcalamares] Make the KPMCore global storage filesystem handlers inline --- src/libcalamares/partition/Global.cpp | 13 ------------- src/libcalamares/partition/Global.h | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/libcalamares/partition/Global.cpp b/src/libcalamares/partition/Global.cpp index 7203f6b55..5be9036c7 100644 --- a/src/libcalamares/partition/Global.cpp +++ b/src/libcalamares/partition/Global.cpp @@ -63,16 +63,3 @@ CalamaresUtils::Partition::useFilesystemGS( const QString& filesystemType, bool useFilesystemGS( *gs, filesystemType, used ); } } - -#ifdef WITH_KPMCORE4API -bool -CalamaresUtils::Partition::isFilesystemUsedGS( FileSystem::Type filesystem ) -{ - return isFilesystemUsedGS( untranslatedFS( filesystem ) ); -} -void -CalamaresUtils::Partition::useFilesystemGS( FileSystem::Type filesystem, bool used ) -{ - useFilesystemGS( untranslatedFS( filesystem ), used ); -} -#endif diff --git a/src/libcalamares/partition/Global.h b/src/libcalamares/partition/Global.h index 3164d06fc..a013c4e2f 100644 --- a/src/libcalamares/partition/Global.h +++ b/src/libcalamares/partition/Global.h @@ -19,6 +19,8 @@ #include "DllMacro.h" #ifdef WITH_KPMCORE4API +#include "FileSystem.h" + #include #endif @@ -52,12 +54,21 @@ bool DLLEXPORT isFilesystemUsedGS( const QString& filesystemType ); * See useFilesystemGS(const QString&, bool); this method uses the filesystem type * enumeration to pick the name. */ -void DLLEXPORT useFilesystemGS( FileSystem::Type filesystem, bool used ); +void +useFilesystemGS( FileSystem::Type filesystem, bool used ) +{ + useFilesystemGS( untranslatedFS( filesystem ), used ); +} + /* @brief Reads from global storage whether the typesystem type is used * * See isFilesystemUsedGS(const QString&). */ -bool DLLEXPORT isFilesystemUsedGS( FileSystem::Type filesystem ); +bool +isFilesystemUsedGS( FileSystem::Type filesystem ) +{ + return isFilesystemUsedGS( untranslatedFS( filesystem ) ); +} #endif } // namespace Partition From f3681a533e7d414627abac0c7d763d0894f810de Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 8 Feb 2021 14:41:17 +0100 Subject: [PATCH 4/8] [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 --- src/libcalamares/partition/Global.cpp | 40 +++++++-------------------- src/libcalamares/partition/Global.h | 32 ++++++++++++++++++--- src/libcalamares/partition/Tests.cpp | 25 ++++++++--------- 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/src/libcalamares/partition/Global.cpp b/src/libcalamares/partition/Global.cpp index 5be9036c7..3071cd081 100644 --- a/src/libcalamares/partition/Global.cpp +++ b/src/libcalamares/partition/Global.cpp @@ -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 ); } } diff --git a/src/libcalamares/partition/Global.h b/src/libcalamares/partition/Global.h index a013c4e2f..960e302d0 100644 --- a/src/libcalamares/partition/Global.h +++ b/src/libcalamares/partition/Global.h @@ -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 ) ); diff --git a/src/libcalamares/partition/Tests.cpp b/src/libcalamares/partition/Tests.cpp index 512a23110..bf1c433fe 100644 --- a/src/libcalamares/partition/Tests.cpp +++ b/src/libcalamares/partition/Tests.cpp @@ -8,6 +8,7 @@ * */ +#include "Global.h" #include "PartitionSize.h" #include "GlobalStorage.h" @@ -16,11 +17,6 @@ #include #include -// 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(); From 48541629f91842340f515fe018f59c0fcf5c65a1 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 8 Feb 2021 14:46:34 +0100 Subject: [PATCH 5/8] [libcalamares] Extend tests to handle case-insensitive --- src/libcalamares/partition/Tests.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libcalamares/partition/Tests.cpp b/src/libcalamares/partition/Tests.cpp index bf1c433fe..a456d7e36 100644 --- a/src/libcalamares/partition/Tests.cpp +++ b/src/libcalamares/partition/Tests.cpp @@ -199,6 +199,16 @@ PartitionServiceTests::testFilesystemGS() const auto map = gs.value( "filesystem_use" ).toMap(); QCOMPARE( map.count(), fsNames.count() + 1 ); } + + // The API says that it it case-insensitive + QVERIFY( !isFilesystemUsedGS( &gs, "ZFS" ) ); + QVERIFY( isFilesystemUsedGS( &gs, "EXT4" ) ); + QCOMPARE( isFilesystemUsedGS( &gs, "ZFS" ), isFilesystemUsedGS( &gs, "zfs" ) ); + QCOMPARE( isFilesystemUsedGS( &gs, "EXT4" ), isFilesystemUsedGS( &gs, "ext4" ) ); + + useFilesystemGS( &gs, "EXT4", false ); + QVERIFY( !isFilesystemUsedGS( &gs, "EXT4" ) ); + QCOMPARE( isFilesystemUsedGS( &gs, "EXT4" ), isFilesystemUsedGS( &gs, "ext4" ) ); } From 9665af0e5a19fd863663d0a4ad84c64e296dba81 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 8 Feb 2021 14:47:55 +0100 Subject: [PATCH 6/8] [libcalamares] Make keys case-insensitive (as documented) --- src/libcalamares/partition/Global.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libcalamares/partition/Global.cpp b/src/libcalamares/partition/Global.cpp index 3071cd081..fbe775f11 100644 --- a/src/libcalamares/partition/Global.cpp +++ b/src/libcalamares/partition/Global.cpp @@ -25,9 +25,10 @@ CalamaresUtils::Partition::isFilesystemUsedGS( const Calamares::GlobalStorage* g return false; } const QVariantMap fsUse = gs->value( fsUse_key ).toMap(); - if ( fsUse.contains( filesystemType ) ) + QString key = filesystemType.toLower(); + if ( fsUse.contains( key ) ) { - const auto v = fsUse.value( filesystemType ); + const auto v = fsUse.value( key ); return v.toBool(); } return false; @@ -39,7 +40,8 @@ CalamaresUtils::Partition::useFilesystemGS( Calamares::GlobalStorage* gs, const if ( gs ) { QVariantMap existingMap = gs->contains( fsUse_key ) ? gs->value( fsUse_key ).toMap() : QVariantMap(); - existingMap.insert( filesystemType, used ); + QString key = filesystemType.toLower(); + existingMap.insert( key, used ); gs->insert( fsUse_key, existingMap ); } } From 10bec1d97079d5ac0ebf80d2ad534fadc2146673 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 8 Feb 2021 15:05:24 +0100 Subject: [PATCH 7/8] [libcalamares] Expand API to allow clearing out the filesystem use --- src/libcalamares/partition/Global.cpp | 9 +++++++++ src/libcalamares/partition/Global.h | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/src/libcalamares/partition/Global.cpp b/src/libcalamares/partition/Global.cpp index fbe775f11..fb7b09aaf 100644 --- a/src/libcalamares/partition/Global.cpp +++ b/src/libcalamares/partition/Global.cpp @@ -45,3 +45,12 @@ CalamaresUtils::Partition::useFilesystemGS( Calamares::GlobalStorage* gs, const gs->insert( fsUse_key, existingMap ); } } + +void +CalamaresUtils::Partition::clearFilesystemGS( Calamares::GlobalStorage* gs ) +{ + if ( gs ) + { + gs->remove( fsUse_key ); + } +} diff --git a/src/libcalamares/partition/Global.h b/src/libcalamares/partition/Global.h index 960e302d0..733e2f69c 100644 --- a/src/libcalamares/partition/Global.h +++ b/src/libcalamares/partition/Global.h @@ -52,6 +52,12 @@ void DLLEXPORT useFilesystemGS( Calamares::GlobalStorage* gs, const QString& fil */ bool DLLEXPORT isFilesystemUsedGS( const Calamares::GlobalStorage* gs, const QString& filesystemType ); +/** @brief Clears the usage data for filesystems + * + * This removes the internal key *filesystem_use*. + */ +void DLLEXPORT clearFilesystemGS( Calamares::GlobalStorage* gs ); + /** @brief Convenience function for using "the" Global Storage * * @see useFilesystemGS(const QString&, bool) From 559b79f92054b285c0dc3e5c381a95f298180c1e Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 8 Feb 2021 15:08:45 +0100 Subject: [PATCH 8/8] [partition] Use (better documented) filesystem-use API --- src/libcalamares/partition/Tests.cpp | 7 ++++ .../partition/jobs/FillGlobalStorageJob.cpp | 37 ++++++++----------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/libcalamares/partition/Tests.cpp b/src/libcalamares/partition/Tests.cpp index a456d7e36..16f7d78c2 100644 --- a/src/libcalamares/partition/Tests.cpp +++ b/src/libcalamares/partition/Tests.cpp @@ -209,6 +209,13 @@ PartitionServiceTests::testFilesystemGS() useFilesystemGS( &gs, "EXT4", false ); QVERIFY( !isFilesystemUsedGS( &gs, "EXT4" ) ); QCOMPARE( isFilesystemUsedGS( &gs, "EXT4" ), isFilesystemUsedGS( &gs, "ext4" ) ); + useFilesystemGS( &gs, "ext4", true ); + QVERIFY( isFilesystemUsedGS( &gs, "EXT4" ) ); + + CalamaresUtils::Partition::clearFilesystemGS( &gs ); + QVERIFY( !isFilesystemUsedGS( &gs, "ZFS" ) ); + QVERIFY( !isFilesystemUsedGS( &gs, "EXT4" ) ); + QVERIFY( !isFilesystemUsedGS( &gs, "ext4" ) ); } diff --git a/src/modules/partition/jobs/FillGlobalStorageJob.cpp b/src/modules/partition/jobs/FillGlobalStorageJob.cpp index 1660dbb54..f79918e64 100644 --- a/src/modules/partition/jobs/FillGlobalStorageJob.cpp +++ b/src/modules/partition/jobs/FillGlobalStorageJob.cpp @@ -18,6 +18,7 @@ #include "GlobalStorage.h" #include "JobQueue.h" #include "partition/FileSystem.h" +#include "partition/Global.h" #include "partition/PartitionIterator.h" #include "utils/Logger.h" @@ -291,35 +292,29 @@ FillGlobalStorageJob::prettyStatusMessage() const * .. mark as "1" if it's on the system, somewhere * .. mark as "2" if it's one of the claimed / in-use FSses * - * Stores a GS key called "filesystems_use" with this mapping. + * Stores a GS key called "filesystem_use" with this mapping. + * @see CalamaresUtils::Partition::useFilesystemGS() */ static void storeFSUse( Calamares::GlobalStorage* storage, const QVariantList& partitions ) { - QMap< QString, int > fsUses; - for ( const auto& p : partitions ) + if ( storage ) { - const auto pmap = p.toMap(); - - QString fs = pmap.value( "fs" ).toString(); - int thisUse = pmap.value( "claimed" ).toBool() ? 2 : 1; - - if ( fs.isEmpty() ) + CalamaresUtils::Partition::clearFilesystemGS( storage ); + for ( const auto& p : partitions ) { - continue; + const auto pmap = p.toMap(); + + QString fs = pmap.value( "fs" ).toString(); + + if ( fs.isEmpty() ) + { + continue; + } + + CalamaresUtils::Partition::useFilesystemGS( storage, fs, true ); } - - int newUse = qMax( fsUses.value( fs ), thisUse ); // value() is 0 if not present - fsUses.insert( fs, newUse ); } - - QVariantMap fsUsesVariant; - for ( auto it = fsUses.cbegin(); it != fsUses.cend(); ++it ) - { - fsUsesVariant.insert( it.key(), it.value() ); - } - - storage->insert( "filesystems_use", fsUsesVariant ); } Calamares::JobResult