[preservefiles] Support perm setting

- Use settings from config file
 - Refactor copy operation
 - Apply permissions inside target system
This commit is contained in:
Adriaan de Groot 2018-10-01 11:02:56 +02:00
parent 91c94c6022
commit d3d08241e2
2 changed files with 63 additions and 29 deletions

View File

@ -18,6 +18,8 @@
#include "PreserveFiles.h" #include "PreserveFiles.h"
#include "permissions.h"
#include "CalamaresVersion.h" #include "CalamaresVersion.h"
#include "JobQueue.h" #include "JobQueue.h"
#include "GlobalStorage.h" #include "GlobalStorage.h"
@ -83,6 +85,38 @@ PreserveFiles::prettyName() const
return tr( "Saving files for later ..." ); return tr( "Saving files for later ..." );
} }
static bool
copy_file( const QString& source, const QString& dest )
{
QFile sourcef( source );
if ( !sourcef.open( QFile::ReadOnly ) )
{
cWarning() << "Could not read" << source;
return false;
}
QFile destf( dest );
if ( !destf.open( QFile::WriteOnly ) )
{
sourcef.close();
cWarning() << "Could not open" << destf.fileName() << "for writing; could not copy" << source;
return false;
}
QByteArray b;
do
{
b = sourcef.read( 1_MiB );
destf.write( b );
}
while ( b.count() > 0 );
sourcef.close();
destf.close();
return true;
}
Calamares::JobResult PreserveFiles::exec() Calamares::JobResult PreserveFiles::exec()
{ {
if ( m_items.isEmpty() ) if ( m_items.isEmpty() )
@ -96,7 +130,8 @@ Calamares::JobResult PreserveFiles::exec()
for ( const auto& it : m_items ) for ( const auto& it : m_items )
{ {
QString source = it.source; QString source = it.source;
QString dest = prefix + atReplacements( it.dest ); QString bare_dest = atReplacements( it.dest );
QString dest = prefix + bare_dest;
if ( it.type == ItemType::Log ) if ( it.type == ItemType::Log )
source = Logger::logFile(); source = Logger::logFile();
@ -111,34 +146,31 @@ Calamares::JobResult PreserveFiles::exec()
cWarning() << "Skipping unnamed source file for" << dest; cWarning() << "Skipping unnamed source file for" << dest;
else else
{ {
QFile sourcef( source ); if ( copy_file( source, dest ) )
if ( !sourcef.open( QFile::ReadOnly ) )
{ {
cWarning() << "Could not read" << source; if ( it.perm.isValid() )
continue; {
auto s_p = CalamaresUtils::System::instance();
int r;
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 } );
if ( r )
cWarning() << "Could not chgrp target" << bare_dest;
r = s_p->targetEnvCall( QStringList{ "chmod", it.perm.octal(), bare_dest } );
if ( r )
cWarning() << "Could not chmod target" << bare_dest;
} }
QFile destf( dest );
if ( !destf.open( QFile::WriteOnly ) )
{
sourcef.close();
cWarning() << "Could not open" << destf.fileName() << "for writing; could not copy" << source;
continue;
}
QByteArray b;
do
{
b = sourcef.read( 1_MiB );
destf.write( b );
}
while ( b.count() > 0 );
sourcef.close();
destf.close();
++count; ++count;
} }
} }
}
return count == m_items.count() ? return count == m_items.count() ?
Calamares::JobResult::ok() : Calamares::JobResult::ok() :
@ -168,7 +200,7 @@ void PreserveFiles::setConfigurationMap(const QVariantMap& configurationMap)
{ {
QString filename = li.toString(); QString filename = li.toString();
if ( !filename.isEmpty() ) if ( !filename.isEmpty() )
m_items.append( Item{ filename, filename, ItemType::Path } ); m_items.append( Item{ filename, filename, Permissions(), ItemType::Path } );
else else
cDebug() << "Empty filename for preservefiles, item" << c; cDebug() << "Empty filename for preservefiles, item" << c;
} }
@ -181,6 +213,7 @@ void PreserveFiles::setConfigurationMap(const QVariantMap& configurationMap)
( from == "log" ) ? ItemType::Log : ( from == "log" ) ? ItemType::Log :
( from == "config" ) ? ItemType::Config : ( from == "config" ) ? ItemType::Config :
ItemType::None; ItemType::None;
QString perm = map[ "perm" ].toString();
if ( dest.isEmpty() ) if ( dest.isEmpty() )
{ {
@ -192,7 +225,7 @@ void PreserveFiles::setConfigurationMap(const QVariantMap& configurationMap)
} }
else else
{ {
m_items.append( Item{ QString(), dest, t } ); m_items.append( Item{ QString(), dest, Permissions(perm), t } );
} }
} }
else else

View File

@ -24,11 +24,11 @@
#include <QVariantMap> #include <QVariantMap>
#include "CppJob.h" #include "CppJob.h"
#include "PluginDllMacro.h"
#include "utils/PluginFactory.h" #include "utils/PluginFactory.h"
#include "PluginDllMacro.h" #include "permissions.h"
class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob
{ {
@ -46,6 +46,7 @@ class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob
{ {
QString source; QString source;
QString dest; QString dest;
Permissions perm;
ItemType type; ItemType type;
} ; } ;