[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.
This commit is contained in:
Adriaan de Groot 2021-02-08 11:13:49 +01:00
parent 1e1b7b7ece
commit afdf431b77
3 changed files with 145 additions and 0 deletions

View File

@ -63,6 +63,7 @@ set( libSources
packages/Globals.cpp
# Partition service
partition/Global.cpp
partition/Mount.cpp
partition/PartitionSize.cpp
partition/Sync.cpp

View File

@ -0,0 +1,78 @@
/* === This file is part of Calamares - <https://calamares.io> ===
*
* SPDX-FileCopyrightText: 2021 Adriaan de Groot <groot@kde.org>
* 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 <QVariantMap>
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

View File

@ -0,0 +1,66 @@
/* === This file is part of Calamares - <https://calamares.io> ===
*
* SPDX-FileCopyrightText: 2021 Adriaan de Groot <groot@kde.org>
* 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 <kpmcore/fs/filesystem.h>
#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