From afdf431b774a5a58abf8ef2521e126a91772fb7d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 8 Feb 2021 11:13:49 +0100 Subject: [PATCH] [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