mirror of https://github.com/cutefishos/calamares
Merge branch 'move-permissions' into calamares
commit
92938f63f8
@ -0,0 +1,124 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* SPDX-FileCopyrightText: 2018 Scott Harvey <scott@spharvey.me>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* License-Filename: LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
#include "Permissions.h"
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
#include <QProcess>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace CalamaresUtils
|
||||
{
|
||||
|
||||
Permissions::Permissions()
|
||||
: m_username()
|
||||
, m_group()
|
||||
, m_value( 0 )
|
||||
, m_valid( false )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Permissions::Permissions( QString const& p )
|
||||
: Permissions()
|
||||
{
|
||||
parsePermissions( p );
|
||||
}
|
||||
|
||||
void
|
||||
Permissions::parsePermissions( QString const& p )
|
||||
{
|
||||
|
||||
QStringList segments = p.split( ":" );
|
||||
|
||||
if ( segments.length() != 3 )
|
||||
{
|
||||
m_valid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( segments[ 0 ].isEmpty() || segments[ 1 ].isEmpty() )
|
||||
{
|
||||
m_valid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
bool ok;
|
||||
int octal = segments[ 2 ].toInt( &ok, 8 );
|
||||
if ( !ok || octal == 0 )
|
||||
{
|
||||
m_valid = false;
|
||||
return;
|
||||
}
|
||||
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 ];
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool
|
||||
Permissions::apply( const QString& path, int mode )
|
||||
{
|
||||
// We **don't** use QFile::setPermissions() here because it takes
|
||||
// a Qt flags object that subtlely does not align with POSIX bits.
|
||||
// The Qt flags are **hex** based, so 0x755 for rwxr-xr-x, while
|
||||
// our integer (mode_t) stores **octal** based flags.
|
||||
//
|
||||
// Call chmod(2) directly, that's what Qt would be doing underneath
|
||||
// anyway.
|
||||
int r = chmod( path.toUtf8().constData(), mode_t( mode ) );
|
||||
if ( r )
|
||||
{
|
||||
cDebug() << Logger::SubEntry << "Could not set permissions of" << path << "to" << QString::number( mode, 8 );
|
||||
}
|
||||
return r == 0;
|
||||
}
|
||||
|
||||
bool
|
||||
Permissions::apply( const QString& path, const CalamaresUtils::Permissions& p )
|
||||
{
|
||||
if ( !p.isValid() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool r = apply( path, p.value() );
|
||||
if ( r )
|
||||
{
|
||||
// We don't use chgrp(2) or chown(2) here because then we need to
|
||||
// go through the users list (which one, target or source?) to get
|
||||
// uid_t and gid_t values to pass to that system call.
|
||||
//
|
||||
// Do a lame cop-out and let the chown(8) utility do the heavy lifting.
|
||||
if ( QProcess::execute( "chown", { p.username() + ':' + p.group(), path } ) )
|
||||
{
|
||||
r = false;
|
||||
cDebug() << Logger::SubEntry << "Could not set owner of" << path << "to"
|
||||
<< ( p.username() + ':' + p.group() );
|
||||
}
|
||||
}
|
||||
if ( r )
|
||||
{
|
||||
/* NOTUSED */ apply( path, p.value() );
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
} // namespace CalamaresUtils
|
@ -0,0 +1,92 @@
|
||||
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||
*
|
||||
* SPDX-FileCopyrightText: 2018 Scott Harvey <scott@spharvey.me>
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* License-Filename: LICENSE
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LIBCALAMARES_PERMISSIONS_H
|
||||
#define LIBCALAMARES_PERMISSIONS_H
|
||||
|
||||
#include "DllMacro.h"
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace CalamaresUtils
|
||||
{
|
||||
|
||||
/**
|
||||
* @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 DLLEXPORT Permissions
|
||||
{
|
||||
|
||||
public:
|
||||
/** @brief Constructor
|
||||
*
|
||||
* Splits the string @p at the colon (":") into separate elements for
|
||||
* <user>, <group>, and <value> (permissions), where <value> is interpreted
|
||||
* as an **octal** integer. That is, "root:wheel:755" will give
|
||||
* you an integer value of four-hundred-ninety-three (493),
|
||||
* corresponding to the UNIX file permissions rwxr-xr-x,
|
||||
* as one would expect from chmod and other command-line utilities.
|
||||
*/
|
||||
Permissions( QString const& p );
|
||||
|
||||
/// @brief Default constructor of an invalid Permissions.
|
||||
Permissions();
|
||||
|
||||
/// @brief Was the Permissions object constructed from valid data?
|
||||
bool isValid() const { return m_valid; }
|
||||
/// @brief The user (first component, e.g. "root" in "root:wheel:755")
|
||||
QString username() const { return m_username; }
|
||||
/// @brief The group (second component, e.g. "wheel" in "root:wheel:755")
|
||||
QString group() const { return m_group; }
|
||||
/** @brief The value (file permission) as an integer.
|
||||
*
|
||||
* Bear in mind that input is in octal, but integers are just integers;
|
||||
* naively printing them will get decimal results (e.g. 493 from the
|
||||
* input of "root:wheel:755"). This is suitable to pass to apply().
|
||||
*/
|
||||
int value() const { return m_value; }
|
||||
/** @brief The value (file permission) as octal string
|
||||
*
|
||||
* This is suitable for passing to chmod-the-program, or for
|
||||
* recreating the original Permissions string.
|
||||
*/
|
||||
QString octal() const { return QString::number( value(), 8 ); }
|
||||
|
||||
/** @brief Sets the file-access @p mode of @p path
|
||||
*
|
||||
* Pass a path that is relative (or absolute) in the **host** system.
|
||||
*/
|
||||
static bool apply( const QString& path, int mode );
|
||||
/** @brief Do both chmod and chown on @p path
|
||||
*
|
||||
* Note that interpreting user- and group- names for applying the
|
||||
* permissions can be different between the host system and the target
|
||||
* system; the target might not have a "live" user, for instance, and
|
||||
* the host won't have the user-entered username for the installation.
|
||||
*
|
||||
* For this call, the names are interpreted in the **host** system.
|
||||
* Pass a path that is relative (or absolute) in the **host** system.
|
||||
*/
|
||||
static bool apply( const QString& path, const Permissions& p );
|
||||
/// Convenience method for apply(const QString&, const Permissions& )
|
||||
bool apply( const QString& path ) const { return apply( path, *this ); }
|
||||
|
||||
private:
|
||||
void parsePermissions( QString const& p );
|
||||
|
||||
QString m_username;
|
||||
QString m_group;
|
||||
int m_value;
|
||||
bool m_valid;
|
||||
};
|
||||
|
||||
} // namespace CalamaresUtils
|
||||
|
||||
#endif // LIBCALAMARES_PERMISSIONS_H
|
@ -1,75 +0,0 @@
|
||||
/* === 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include "permissions.h"
|
||||
|
||||
Permissions::Permissions() :
|
||||
m_username(),
|
||||
m_group(),
|
||||
m_valid(false),
|
||||
m_value(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Permissions::Permissions(QString p) : Permissions()
|
||||
{
|
||||
parsePermissions(p);
|
||||
}
|
||||
|
||||
void Permissions::parsePermissions(const QString& p) {
|
||||
|
||||
QStringList segments = p.split(":");
|
||||
|
||||
if (segments.length() != 3) {
|
||||
m_valid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (segments[0].isEmpty() || segments[1].isEmpty()) {
|
||||
m_valid = false;
|
||||
return;
|
||||
}
|
||||
|
||||
bool ok;
|
||||
int octal = segments[2].toInt(&ok, 8);
|
||||
if (!ok || octal == 0) {
|
||||
m_valid = false;
|
||||
return;
|
||||
} 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];
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,62 +0,0 @@
|
||||
/* === 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PERMISSIONS_H
|
||||
#define PERMISSIONS_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
/**
|
||||
* @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
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
/** @brief Constructor
|
||||
*
|
||||
* 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);
|
||||
|
||||
/** @brief Default constructor of an invalid Permissions. */
|
||||
Permissions();
|
||||
|
||||
bool isValid() const { return m_valid; }
|
||||
QString username() const { return m_username; }
|
||||
QString group() const { return m_group; }
|
||||
int value() const { return m_value; }
|
||||
QString octal() const { return QString::number( m_value, 8 ); }
|
||||
|
||||
private:
|
||||
void parsePermissions(QString const &p);
|
||||
|
||||
QString m_username;
|
||||
QString m_group;
|
||||
bool m_valid;
|
||||
int m_value;
|
||||
|
||||
};
|
||||
|
||||
#endif // PERMISSIONS_H
|
Loading…
Reference in New Issue