calamares/src/libcalamares/GlobalStorage.cpp
Adriaan de Groot 3c618a9a19 [libcalamares] Fix GS load behavior
- the loadJson behavior did too many notifications, and was likely to
  deadlock; write directly to the map instead and emit only once.
- the loadYaml method did something very different from its
  documentation or intent.
2020-08-07 00:08:47 +02:00

203 lines
4.4 KiB
C++

/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
* SPDX-FileCopyrightText: 2017-2018 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Calamares is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "GlobalStorage.h"
#include "utils/Logger.h"
#include "utils/Units.h"
#include "utils/Yaml.h"
#include <QFile>
#include <QJsonDocument>
#include <QMutexLocker>
using CalamaresUtils::operator""_MiB;
namespace Calamares
{
class GlobalStorage::ReadLock : public QMutexLocker
{
public:
ReadLock( const GlobalStorage* gs )
: QMutexLocker( &gs->m_mutex )
{
}
};
class GlobalStorage::WriteLock : public QMutexLocker
{
public:
WriteLock( GlobalStorage* gs )
: QMutexLocker( &gs->m_mutex )
, m_gs( gs )
{
}
~WriteLock() { m_gs->changed(); }
GlobalStorage* m_gs;
};
GlobalStorage::GlobalStorage( QObject* parent )
: QObject( parent )
{
}
bool
GlobalStorage::contains( const QString& key ) const
{
ReadLock l( this );
return m.contains( key );
}
int
GlobalStorage::count() const
{
ReadLock l( this );
return m.count();
}
void
GlobalStorage::insert( const QString& key, const QVariant& value )
{
WriteLock l( this );
m.insert( key, value );
}
QStringList
GlobalStorage::keys() const
{
ReadLock l( this );
return m.keys();
}
int
GlobalStorage::remove( const QString& key )
{
WriteLock l( this );
int nItems = m.remove( key );
return nItems;
}
QVariant
GlobalStorage::value( const QString& key ) const
{
ReadLock l( this );
return m.value( key );
}
void
GlobalStorage::debugDump() const
{
ReadLock l( this );
cDebug() << "GlobalStorage" << Logger::Pointer( this ) << m.count() << "items";
for ( auto it = m.cbegin(); it != m.cend(); ++it )
{
cDebug() << Logger::SubEntry << it.key() << '\t' << it.value();
}
}
bool
GlobalStorage::saveJson( const QString& filename ) const
{
ReadLock l( this );
QFile f( filename );
if ( !f.open( QFile::WriteOnly ) )
{
return false;
}
f.write( QJsonDocument::fromVariant( m ).toJson() );
f.close();
return true;
}
bool
GlobalStorage::loadJson( const QString& filename )
{
QFile f( filename );
if ( !f.open( QFile::ReadOnly ) )
{
return false;
}
QJsonParseError e;
QJsonDocument d = QJsonDocument::fromJson( f.read( 1_MiB ), &e );
if ( d.isNull() )
{
cWarning() << filename << e.errorString();
}
else if ( !d.isObject() )
{
cWarning() << filename << "Not suitable JSON.";
}
else
{
WriteLock l( this );
// Do **not** use method insert() here, because it would
// recursively lock the mutex, leading to deadlock. Also,
// that would emit changed() for each key.
auto map = d.toVariant().toMap();
for ( auto i = map.constBegin(); i != map.constEnd(); ++i )
{
m.insert( i.key(), *i );
}
return true;
}
return false;
}
bool
GlobalStorage::saveYaml( const QString& filename ) const
{
ReadLock l( this );
return CalamaresUtils::saveYaml( filename, m );
}
bool
GlobalStorage::loadYaml( const QString& filename )
{
bool ok = false;
auto map = CalamaresUtils::loadYaml( filename, &ok );
if ( ok )
{
WriteLock l( this );
// Do **not** use method insert() here, because it would
// recursively lock the mutex, leading to deadlock. Also,
// that would emit changed() for each key.
for ( auto i = map.constBegin(); i != map.constEnd(); ++i )
{
m.insert( i.key(), *i );
}
return true;
}
return false;
}
} // namespace Calamares