[preservefiles] Start cleanup of structure, polymorphism

This commit is contained in:
Adriaan de Groot 2021-12-12 00:33:55 +01:00
parent 795b2c88e8
commit b1ecbb4151
2 changed files with 43 additions and 33 deletions

View File

@ -18,6 +18,30 @@
#include <QFile>
enum class ItemType
{
None,
Path,
Log,
Config
};
struct Item
{
QString source;
QString dest;
CalamaresUtils::Permissions perm;
ItemType type;
Item( const QString& src, const QString& d, CalamaresUtils::Permissions p, ItemType t )
: source( src )
, dest( d )
, perm( std::move( p ) )
, type( t )
{
}
};
using namespace CalamaresUtils::Units;
QString
@ -113,7 +137,7 @@ copy_file( const QString& source, const QString& dest )
Calamares::JobResult
PreserveFiles::exec()
{
if ( m_items.isEmpty() )
if ( m_items.empty() )
{
return Calamares::JobResult::error( tr( "No files configured to save for later." ) );
}
@ -124,18 +148,18 @@ PreserveFiles::exec()
prefix.append( '/' );
}
int count = 0;
for ( const auto& it : m_items )
size_t count = 0;
for ( const auto& it : qAsConst( m_items ) )
{
QString source = it.source;
QString bare_dest = atReplacements( it.dest );
QString source = it->source;
QString bare_dest = atReplacements( it->dest );
QString dest = prefix + bare_dest;
if ( it.type == ItemType::Log )
if ( it->type == ItemType::Log )
{
source = Logger::logFile();
}
if ( it.type == ItemType::Config )
if ( it->type == ItemType::Config )
{
if ( !Calamares::JobQueue::instance()->globalStorage()->saveJson( dest ) )
{
@ -154,9 +178,9 @@ PreserveFiles::exec()
{
if ( copy_file( source, dest ) )
{
if ( it.perm.isValid() )
if ( it->perm.isValid() )
{
if ( !it.perm.apply( CalamaresUtils::System::instance()->targetPath( bare_dest ) ) )
if ( !it->perm.apply( CalamaresUtils::System::instance()->targetPath( bare_dest ) ) )
{
cWarning() << "Could not set attributes of" << bare_dest;
}
@ -167,7 +191,7 @@ PreserveFiles::exec()
}
}
return count == m_items.count()
return count == m_items.size()
? Calamares::JobResult::ok()
: Calamares::JobResult::error( tr( "Not all of the configured files could be preserved." ) );
}
@ -202,8 +226,8 @@ PreserveFiles::setConfigurationMap( const QVariantMap& configurationMap )
{
QString filename = li.toString();
if ( !filename.isEmpty() )
m_items.append(
Item { filename, filename, CalamaresUtils::Permissions( defaultPermissions ), ItemType::Path } );
m_items.push_back( std::make_unique< Item >(
filename, filename, CalamaresUtils::Permissions( defaultPermissions ), ItemType::Path ) );
else
{
cDebug() << "Empty filename for preservefiles, item" << c;
@ -231,7 +255,8 @@ PreserveFiles::setConfigurationMap( const QVariantMap& configurationMap )
}
else
{
m_items.append( Item { QString(), dest, CalamaresUtils::Permissions( perm ), t } );
m_items.push_back(
std::make_unique< Item >( QString(), dest, CalamaresUtils::Permissions( perm ), t ) );
}
}
else

View File

@ -13,31 +13,16 @@
#include "utils/Permissions.h"
#include "utils/PluginFactory.h"
#include <QList>
#include <QObject>
#include <QVariantMap>
#include <memory>
#include <vector>
struct Item;
class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob
{
Q_OBJECT
enum class ItemType
{
None,
Path,
Log,
Config
};
struct Item
{
QString source;
QString dest;
CalamaresUtils::Permissions perm;
ItemType type;
};
using ItemList = QList< Item >;
using ItemList = std::vector< std::unique_ptr< Item > >;
public:
explicit PreserveFiles( QObject* parent = nullptr );