|
|
|
@ -1,39 +1,28 @@
|
|
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
|
*
|
|
|
|
|
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
|
|
|
|
* SPDX-FileCopyrightText: 2018 Adriaan de Groot <groot@kde.org>
|
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
* License-Filename: LICENSE
|
|
|
|
|
*
|
|
|
|
|
* 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 "PreserveFiles.h"
|
|
|
|
|
|
|
|
|
|
#include "permissions.h"
|
|
|
|
|
|
|
|
|
|
#include "CalamaresVersion.h"
|
|
|
|
|
#include "JobQueue.h"
|
|
|
|
|
#include "GlobalStorage.h"
|
|
|
|
|
|
|
|
|
|
#include "JobQueue.h"
|
|
|
|
|
#include "utils/CalamaresUtilsSystem.h"
|
|
|
|
|
#include "utils/CommandList.h"
|
|
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
#include "utils/Permissions.h"
|
|
|
|
|
#include "utils/Units.h"
|
|
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
|
|
using CalamaresUtils::operator""_MiB;
|
|
|
|
|
|
|
|
|
|
QString targetPrefix()
|
|
|
|
|
QString
|
|
|
|
|
targetPrefix()
|
|
|
|
|
{
|
|
|
|
|
if ( CalamaresUtils::System::instance()->doChroot() )
|
|
|
|
|
{
|
|
|
|
@ -42,9 +31,13 @@ QString targetPrefix()
|
|
|
|
|
{
|
|
|
|
|
QString r = gs->value( "rootMountPoint" ).toString();
|
|
|
|
|
if ( !r.isEmpty() )
|
|
|
|
|
{
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cDebug() << "RootMountPoint is empty";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
@ -55,16 +48,21 @@ QString targetPrefix()
|
|
|
|
|
return QLatin1String( "/" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString atReplacements( QString s )
|
|
|
|
|
QString
|
|
|
|
|
atReplacements( QString s )
|
|
|
|
|
{
|
|
|
|
|
Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
|
QString root( "/" );
|
|
|
|
|
QString user;
|
|
|
|
|
|
|
|
|
|
if ( gs && gs->contains( "rootMountPoint" ) )
|
|
|
|
|
{
|
|
|
|
|
root = gs->value( "rootMountPoint" ).toString();
|
|
|
|
|
}
|
|
|
|
|
if ( gs && gs->contains( "username" ) )
|
|
|
|
|
{
|
|
|
|
|
user = gs->value( "username" ).toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s.replace( "@@ROOT@@", root ).replace( "@@USER@@", user );
|
|
|
|
|
}
|
|
|
|
@ -74,9 +72,7 @@ PreserveFiles::PreserveFiles( QObject* parent )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PreserveFiles::~PreserveFiles()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
PreserveFiles::~PreserveFiles() {}
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
|
PreserveFiles::prettyName() const
|
|
|
|
@ -107,8 +103,7 @@ copy_file( const QString& source, const QString& dest )
|
|
|
|
|
{
|
|
|
|
|
b = sourcef.read( 1_MiB );
|
|
|
|
|
destf.write( b );
|
|
|
|
|
}
|
|
|
|
|
while ( b.count() > 0 );
|
|
|
|
|
} while ( b.count() > 0 );
|
|
|
|
|
|
|
|
|
|
sourcef.close();
|
|
|
|
|
destf.close();
|
|
|
|
@ -116,14 +111,19 @@ copy_file( const QString& source, const QString& dest )
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Calamares::JobResult PreserveFiles::exec()
|
|
|
|
|
Calamares::JobResult
|
|
|
|
|
PreserveFiles::exec()
|
|
|
|
|
{
|
|
|
|
|
if ( m_items.isEmpty() )
|
|
|
|
|
{
|
|
|
|
|
return Calamares::JobResult::error( tr( "No files configured to save for later." ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString prefix = targetPrefix();
|
|
|
|
|
if ( !prefix.endsWith( '/' ) )
|
|
|
|
|
{
|
|
|
|
|
prefix.append( '/' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
for ( const auto& it : m_items )
|
|
|
|
@ -133,16 +133,24 @@ Calamares::JobResult PreserveFiles::exec()
|
|
|
|
|
QString dest = prefix + bare_dest;
|
|
|
|
|
|
|
|
|
|
if ( it.type == ItemType::Log )
|
|
|
|
|
{
|
|
|
|
|
source = Logger::logFile();
|
|
|
|
|
}
|
|
|
|
|
if ( it.type == ItemType::Config )
|
|
|
|
|
{
|
|
|
|
|
if ( Calamares::JobQueue::instance()->globalStorage()->save( dest ) )
|
|
|
|
|
{
|
|
|
|
|
cWarning() << "Could not write config for" << dest;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if ( source.isEmpty() )
|
|
|
|
|
{
|
|
|
|
|
cWarning() << "Skipping unnamed source file for" << dest;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ( copy_file( source, dest ) )
|
|
|
|
@ -153,17 +161,23 @@ Calamares::JobResult PreserveFiles::exec()
|
|
|
|
|
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
|
|
r = s_p->targetEnvCall( QStringList{ "chown", it.perm.username(), bare_dest } );
|
|
|
|
|
r = s_p->targetEnvCall( QStringList { "chown", it.perm.username(), bare_dest } );
|
|
|
|
|
if ( r )
|
|
|
|
|
{
|
|
|
|
|
cWarning() << "Could not chown target" << bare_dest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = s_p->targetEnvCall( QStringList{ "chgrp", it.perm.group(), bare_dest } );
|
|
|
|
|
r = s_p->targetEnvCall( QStringList { "chgrp", it.perm.group(), bare_dest } );
|
|
|
|
|
if ( r )
|
|
|
|
|
{
|
|
|
|
|
cWarning() << "Could not chgrp target" << bare_dest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = s_p->targetEnvCall( QStringList{ "chmod", it.perm.octal(), bare_dest } );
|
|
|
|
|
r = s_p->targetEnvCall( QStringList { "chmod", it.perm.octal(), bare_dest } );
|
|
|
|
|
if ( r )
|
|
|
|
|
{
|
|
|
|
|
cWarning() << "Could not chmod target" << bare_dest;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++count;
|
|
|
|
@ -171,12 +185,13 @@ Calamares::JobResult PreserveFiles::exec()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count == m_items.count() ?
|
|
|
|
|
Calamares::JobResult::ok() :
|
|
|
|
|
Calamares::JobResult::error( tr( "Not all of the configured files could be preserved." ) );
|
|
|
|
|
return count == m_items.count()
|
|
|
|
|
? Calamares::JobResult::ok()
|
|
|
|
|
: Calamares::JobResult::error( tr( "Not all of the configured files could be preserved." ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PreserveFiles::setConfigurationMap(const QVariantMap& configurationMap)
|
|
|
|
|
void
|
|
|
|
|
PreserveFiles::setConfigurationMap( const QVariantMap& configurationMap )
|
|
|
|
|
{
|
|
|
|
|
auto files = configurationMap[ "files" ];
|
|
|
|
|
if ( !files.isValid() )
|
|
|
|
@ -193,7 +208,9 @@ void PreserveFiles::setConfigurationMap(const QVariantMap& configurationMap)
|
|
|
|
|
|
|
|
|
|
QString defaultPermissions = configurationMap[ "perm" ].toString();
|
|
|
|
|
if ( defaultPermissions.isEmpty() )
|
|
|
|
|
{
|
|
|
|
|
defaultPermissions = QStringLiteral( "root:root:0400" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariantList l = files.toList();
|
|
|
|
|
unsigned int c = 0;
|
|
|
|
@ -203,22 +220,23 @@ void PreserveFiles::setConfigurationMap(const QVariantMap& configurationMap)
|
|
|
|
|
{
|
|
|
|
|
QString filename = li.toString();
|
|
|
|
|
if ( !filename.isEmpty() )
|
|
|
|
|
m_items.append( Item{ filename, filename, Permissions( defaultPermissions ), ItemType::Path } );
|
|
|
|
|
m_items.append( Item { filename, filename, Permissions( defaultPermissions ), ItemType::Path } );
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cDebug() << "Empty filename for preservefiles, item" << c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if ( li.type() == QVariant::Map )
|
|
|
|
|
{
|
|
|
|
|
const auto map = li.toMap();
|
|
|
|
|
QString dest = map[ "dest" ].toString();
|
|
|
|
|
QString from = map[ "from" ].toString();
|
|
|
|
|
ItemType t =
|
|
|
|
|
( from == "log" ) ? ItemType::Log :
|
|
|
|
|
( from == "config" ) ? ItemType::Config :
|
|
|
|
|
ItemType::None;
|
|
|
|
|
ItemType t = ( from == "log" ) ? ItemType::Log : ( from == "config" ) ? ItemType::Config : ItemType::None;
|
|
|
|
|
QString perm = map[ "perm" ].toString();
|
|
|
|
|
if ( perm.isEmpty() )
|
|
|
|
|
{
|
|
|
|
|
perm = defaultPermissions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( dest.isEmpty() )
|
|
|
|
|
{
|
|
|
|
@ -230,15 +248,16 @@ void PreserveFiles::setConfigurationMap(const QVariantMap& configurationMap)
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_items.append( Item{ QString(), dest, Permissions( perm ), t } );
|
|
|
|
|
m_items.append( Item { QString(), dest, Permissions( perm ), t } );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cDebug() << "Invalid type for preservefiles, item" << c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CALAMARES_PLUGIN_FACTORY_DEFINITION( PreserveFilesFactory, registerPlugin<PreserveFiles>(); )
|
|
|
|
|
|
|
|
|
|
CALAMARES_PLUGIN_FACTORY_DEFINITION( PreserveFilesFactory, registerPlugin< PreserveFiles >(); )
|
|
|
|
|