[libcalamares] Chase Permissions move

- Fix include names in *preservefiles*
- Tidy up include guards
- Fix CMakeLists in *perservefiles* and *libcalamares*
- Use SPDX license headers
main
Adriaan de Groot 5 years ago
parent 4473d7f5dd
commit e24f812b2d

@ -76,6 +76,7 @@ set( libSources
utils/Dirs.cpp
utils/Entropy.cpp
utils/Logger.cpp
utils/Permissions.cpp
utils/PluginFactory.cpp
utils/Retranslator.cpp
utils/String.cpp

@ -1,60 +1,58 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright (C) 2018 Scott Harvey <scott@spharvey.me>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* SPDX-FileCopyrightText: 2018 Scott Harvey <scott@spharvey.me>
* SPDX-License-Identifier: GPL-3.0-or-later
* License-Filename: LICENSE
*
*/
#include "Permissions.h"
#include <QString>
#include <QStringList>
#include "permissions.h"
Permissions::Permissions() :
m_username(),
m_group(),
m_valid(false),
m_value(0)
Permissions::Permissions()
: m_username()
, m_group()
, m_valid( false )
, m_value( 0 )
{
}
Permissions::Permissions(QString p) : Permissions()
Permissions::Permissions( QString p )
: Permissions()
{
parsePermissions( p );
}
void Permissions::parsePermissions(const QString& p) {
void
Permissions::parsePermissions( const QString& p )
{
QStringList segments = p.split( ":" );
if (segments.length() != 3) {
if ( segments.length() != 3 )
{
m_valid = false;
return;
}
if (segments[0].isEmpty() || segments[1].isEmpty()) {
if ( segments[ 0 ].isEmpty() || segments[ 1 ].isEmpty() )
{
m_valid = false;
return;
}
bool ok;
int octal = segments[ 2 ].toInt( &ok, 8 );
if (!ok || octal == 0) {
if ( !ok || octal == 0 )
{
m_valid = false;
return;
} else {
}
else
{
m_value = octal;
}
@ -65,11 +63,4 @@ void Permissions::parsePermissions(const QString& p) {
m_group = segments[ 1 ];
return;
}

@ -1,24 +1,15 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright (C) 2018 Scott Harvey <scott@spharvey.me>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* SPDX-FileCopyrightText: 2018 Scott Harvey <scott@spharvey.me>
* SPDX-License-Identifier: GPL-3.0-or-later
* License-Filename: LICENSE
*
*/
#ifndef PERMISSIONS_H
#define PERMISSIONS_H
#ifndef LIBCALAMARES_PERMISSIONS_H
#define LIBCALAMARES_PERMISSIONS_H
#include "DllMacro.h"
#include <QString>
@ -27,11 +18,10 @@
* <user>:<group>:<permissions>, checks it for validity, and makes the three
* components available indivdually.
*/
class Permissions
class DLLEXPORT Permissions
{
public:
/** @brief Constructor
*
* Splits the string @p at the colon (":") into separate elements for
@ -56,7 +46,6 @@ private:
QString m_group;
bool m_valid;
int m_value;
};
#endif // PERMISSIONS_H
#endif // LIBCALAMARES_PERMISSIONS_H

@ -4,7 +4,6 @@ calamares_add_plugin( preservefiles
TYPE job
EXPORT_MACRO PLUGINDLLEXPORT_PRO
SOURCES
permissions.cpp
PreserveFiles.cpp
LINK_PRIVATE_LIBRARIES
calamares

@ -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,10 +31,14 @@ QString targetPrefix()
{
QString r = gs->value( "rootMountPoint" ).toString();
if ( !r.isEmpty() )
{
return r;
}
else
{
cDebug() << "RootMountPoint is empty";
}
}
else
{
cDebug() << "No rootMountPoint defined, preserving files to '/'";
@ -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 ) )
@ -155,28 +163,35 @@ Calamares::JobResult PreserveFiles::exec()
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;
}
}
++count;
}
}
}
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;
@ -205,20 +222,21 @@ void PreserveFiles::setConfigurationMap(const QVariantMap& configurationMap)
if ( !filename.isEmpty() )
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() )
{
@ -234,11 +252,12 @@ void PreserveFiles::setConfigurationMap(const QVariantMap& configurationMap)
}
}
else
{
cDebug() << "Invalid type for preservefiles, item" << c;
}
++c;
}
}
CALAMARES_PLUGIN_FACTORY_DEFINITION( PreserveFilesFactory, registerPlugin< PreserveFiles >(); )

@ -1,34 +1,22 @@
/* === 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/>.
*/
#ifndef PRESERVEFILES_H
#define PRESERVEFILES_H
#include <QList>
#include <QObject>
#include <QVariantMap>
#include "CppJob.h"
#include "DllMacro.h"
#include "utils/Permissions.h"
#include "utils/PluginFactory.h"
#include "permissions.h"
#include <QList>
#include <QObject>
#include <QVariantMap>
class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob
{

Loading…
Cancel
Save