[libcalamares] Chase Permissions move
- Fix include names in *preservefiles* - Tidy up include guards - Fix CMakeLists in *perservefiles* and *libcalamares* - Use SPDX license headers
This commit is contained in:
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,75 +1,66 @@
|
||||
/* === 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);
|
||||
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;
|
||||
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) {
|
||||
int octal = segments[ 2 ].toInt( &ok, 8 );
|
||||
if ( !ok || octal == 0 )
|
||||
{
|
||||
m_valid = false;
|
||||
return;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
m_value = 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
|
||||
m_valid = true;
|
||||
m_username = segments[0];
|
||||
m_group = segments[1];
|
||||
m_username = segments[ 0 ];
|
||||
m_group = segments[ 1 ];
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,45 +1,35 @@
|
||||
/* === 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>
|
||||
|
||||
/**
|
||||
* @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
|
||||
* components available indivdually.
|
||||
*/
|
||||
class Permissions
|
||||
class DLLEXPORT Permissions
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
/** @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
|
||||
* an **octal** integer.
|
||||
*/
|
||||
Permissions(QString p);
|
||||
|
||||
Permissions( QString p );
|
||||
|
||||
/** @brief Default constructor of an invalid Permissions. */
|
||||
Permissions();
|
||||
|
||||
@ -50,13 +40,12 @@ public:
|
||||
QString octal() const { return QString::number( m_value, 8 ); }
|
||||
|
||||
private:
|
||||
void parsePermissions(QString const &p);
|
||||
void parsePermissions( QString const& p );
|
||||
|
||||
QString m_username;
|
||||
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,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 >(); )
|
||||
|
@ -1,35 +1,23 @@
|
||||
/* === 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 "CppJob.h"
|
||||
#include "DllMacro.h"
|
||||
#include "utils/Permissions.h"
|
||||
#include "utils/PluginFactory.h"
|
||||
|
||||
#include <QList>
|
||||
#include <QObject>
|
||||
#include <QVariantMap>
|
||||
|
||||
#include "CppJob.h"
|
||||
#include "DllMacro.h"
|
||||
|
||||
#include "utils/PluginFactory.h"
|
||||
|
||||
#include "permissions.h"
|
||||
|
||||
class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -40,7 +28,7 @@ class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob
|
||||
Path,
|
||||
Log,
|
||||
Config
|
||||
} ;
|
||||
};
|
||||
|
||||
struct Item
|
||||
{
|
||||
@ -48,7 +36,7 @@ class PLUGINDLLEXPORT PreserveFiles : public Calamares::CppJob
|
||||
QString dest;
|
||||
Permissions perm;
|
||||
ItemType type;
|
||||
} ;
|
||||
};
|
||||
|
||||
using ItemList = QList< Item >;
|
||||
|
||||
@ -68,4 +56,4 @@ private:
|
||||
|
||||
CALAMARES_PLUGIN_FACTORY_DECLARATION( PreserveFilesFactory )
|
||||
|
||||
#endif // PRESERVEFILES_H
|
||||
#endif // PRESERVEFILES_H
|
||||
|
Loading…
Reference in New Issue
Block a user