[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/Dirs.cpp
utils/Entropy.cpp utils/Entropy.cpp
utils/Logger.cpp utils/Logger.cpp
utils/Permissions.cpp
utils/PluginFactory.cpp utils/PluginFactory.cpp
utils/Retranslator.cpp utils/Retranslator.cpp
utils/String.cpp utils/String.cpp

@ -1,75 +1,66 @@
/* === This file is part of Calamares - <https://github.com/calamares> === /* === This file is part of Calamares - <https://github.com/calamares> ===
* *
* Copyright (C) 2018 Scott Harvey <scott@spharvey.me> * SPDX-FileCopyrightText: 2018 Scott Harvey <scott@spharvey.me>
* * SPDX-License-Identifier: GPL-3.0-or-later
* This program is free software: you can redistribute it and/or modify * License-Filename: LICENSE
* 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/>.
* *
*/ */
#include "Permissions.h"
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include "permissions.h"
Permissions::Permissions() : Permissions::Permissions()
m_username(), : m_username()
m_group(), , m_group()
m_valid(false), , m_valid( false )
m_value(0) , m_value( 0 )
{ {
} }
Permissions::Permissions(QString p) : Permissions() Permissions::Permissions( QString p )
: Permissions()
{ {
parsePermissions(p); parsePermissions( p );
} }
void Permissions::parsePermissions(const QString& p) { void
Permissions::parsePermissions( const QString& p )
{
QStringList segments = p.split(":"); QStringList segments = p.split( ":" );
if (segments.length() != 3) { if ( segments.length() != 3 )
{
m_valid = false; m_valid = false;
return; return;
} }
if (segments[0].isEmpty() || segments[1].isEmpty()) { if ( segments[ 0 ].isEmpty() || segments[ 1 ].isEmpty() )
{
m_valid = false; m_valid = false;
return; return;
} }
bool ok; bool ok;
int octal = segments[2].toInt(&ok, 8); int octal = segments[ 2 ].toInt( &ok, 8 );
if (!ok || octal == 0) { if ( !ok || octal == 0 )
{
m_valid = false; m_valid = false;
return; return;
} else { }
else
{
m_value = octal; m_value = octal;
} }
// We have exactly three segments and the third is valid octal, // We have exactly three segments and the third is valid octal,
// so we can declare the string valid and set the user and group names // so we can declare the string valid and set the user and group names
m_valid = true; m_valid = true;
m_username = segments[0]; m_username = segments[ 0 ];
m_group = segments[1]; m_group = segments[ 1 ];
return; return;
} }

@ -1,45 +1,35 @@
/* === This file is part of Calamares - <https://github.com/calamares> === /* === This file is part of Calamares - <https://github.com/calamares> ===
* *
* Copyright (C) 2018 Scott Harvey <scott@spharvey.me> * SPDX-FileCopyrightText: 2018 Scott Harvey <scott@spharvey.me>
* * SPDX-License-Identifier: GPL-3.0-or-later
* This program is free software: you can redistribute it and/or modify * License-Filename: LICENSE
* 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/>.
* *
*/ */
#ifndef PERMISSIONS_H #ifndef LIBCALAMARES_PERMISSIONS_H
#define PERMISSIONS_H #define LIBCALAMARES_PERMISSIONS_H
#include "DllMacro.h"
#include <QString> #include <QString>
/** /**
* @brief The Permissions class takes a QString @p in the form of * @brief The Permissions class takes a QString @p in the form of
* <user>:<group>:<permissions>, checks it for validity, and makes the three * <user>:<group>:<permissions>, checks it for validity, and makes the three
* components available indivdually. * components available indivdually.
*/ */
class Permissions class DLLEXPORT Permissions
{ {
public: public:
/** @brief Constructor /** @brief Constructor
* *
* Splits the string @p at the colon (":") into separate elements for * Splits the string @p at the colon (":") into separate elements for
* <user>, <group>, and <value> (permissions), where <value> is returned as * <user>, <group>, and <value> (permissions), where <value> is returned as
* an **octal** integer. * an **octal** integer.
*/ */
Permissions(QString p); Permissions( QString p );
/** @brief Default constructor of an invalid Permissions. */ /** @brief Default constructor of an invalid Permissions. */
Permissions(); Permissions();
@ -50,13 +40,12 @@ public:
QString octal() const { return QString::number( m_value, 8 ); } QString octal() const { return QString::number( m_value, 8 ); }
private: private:
void parsePermissions(QString const &p); void parsePermissions( QString const& p );
QString m_username; QString m_username;
QString m_group; QString m_group;
bool m_valid; bool m_valid;
int m_value; int m_value;
}; };
#endif // PERMISSIONS_H #endif // LIBCALAMARES_PERMISSIONS_H

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

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

@ -1,34 +1,22 @@
/* === This file is part of Calamares - <https://github.com/calamares> === /* === 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 #ifndef PRESERVEFILES_H
#define PRESERVEFILES_H #define PRESERVEFILES_H
#include <QList>
#include <QObject>
#include <QVariantMap>
#include "CppJob.h" #include "CppJob.h"
#include "DllMacro.h" #include "DllMacro.h"
#include "utils/Permissions.h"
#include "utils/PluginFactory.h" #include "utils/PluginFactory.h"
#include "permissions.h" #include <QList>
#include <QObject>
#include <QVariantMap>
class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob
{ {
@ -40,7 +28,7 @@ class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob
Path, Path,
Log, Log,
Config Config
} ; };
struct Item struct Item
{ {
@ -48,7 +36,7 @@ class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob
QString dest; QString dest;
Permissions perm; Permissions perm;
ItemType type; ItemType type;
} ; };
using ItemList = QList< Item >; using ItemList = QList< Item >;
@ -68,4 +56,4 @@ private:
CALAMARES_PLUGIN_FACTORY_DECLARATION( PreserveFilesFactory ) CALAMARES_PLUGIN_FACTORY_DECLARATION( PreserveFilesFactory )
#endif // PRESERVEFILES_H #endif // PRESERVEFILES_H

Loading…
Cancel
Save