2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2018-10-01 10:45:39 +02:00
|
|
|
*
|
2020-06-22 22:32:47 +02:00
|
|
|
* SPDX-FileCopyrightText: 2018 Scott Harvey <scott@spharvey.me>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2018-10-01 10:40:43 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-06-22 22:32:47 +02:00
|
|
|
#include "Permissions.h"
|
|
|
|
|
2020-07-27 11:13:35 +02:00
|
|
|
#include "Logger.h"
|
2020-07-27 10:57:15 +02:00
|
|
|
|
2020-07-27 11:13:35 +02:00
|
|
|
#include <QProcess>
|
2018-10-01 10:40:43 +02:00
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
|
2020-07-27 11:13:35 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2020-07-27 10:57:15 +02:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
|
|
|
|
2020-06-22 22:32:47 +02:00
|
|
|
Permissions::Permissions()
|
|
|
|
: m_username()
|
|
|
|
, m_group()
|
|
|
|
, m_value( 0 )
|
2020-07-24 14:24:03 +02:00
|
|
|
, m_valid( false )
|
2018-10-01 10:51:21 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-10-01 10:40:43 +02:00
|
|
|
|
2020-07-24 14:24:03 +02:00
|
|
|
Permissions::Permissions( QString const& p )
|
2020-06-22 22:32:47 +02:00
|
|
|
: Permissions()
|
2018-10-01 10:40:43 +02:00
|
|
|
{
|
2020-06-22 22:32:47 +02:00
|
|
|
parsePermissions( p );
|
2018-10-01 10:40:43 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 22:32:47 +02:00
|
|
|
void
|
2020-07-24 14:24:03 +02:00
|
|
|
Permissions::parsePermissions( QString const& p )
|
2020-06-22 22:32:47 +02:00
|
|
|
{
|
2018-10-01 10:40:43 +02:00
|
|
|
|
2020-06-22 22:32:47 +02:00
|
|
|
QStringList segments = p.split( ":" );
|
2018-10-01 10:40:43 +02:00
|
|
|
|
2020-06-22 22:32:47 +02:00
|
|
|
if ( segments.length() != 3 )
|
|
|
|
{
|
2018-10-01 10:40:43 +02:00
|
|
|
m_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-22 22:32:47 +02:00
|
|
|
if ( segments[ 0 ].isEmpty() || segments[ 1 ].isEmpty() )
|
|
|
|
{
|
2018-10-01 10:40:43 +02:00
|
|
|
m_valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ok;
|
2020-06-22 22:32:47 +02:00
|
|
|
int octal = segments[ 2 ].toInt( &ok, 8 );
|
|
|
|
if ( !ok || octal == 0 )
|
|
|
|
{
|
2018-10-01 10:40:43 +02:00
|
|
|
m_valid = false;
|
|
|
|
return;
|
2020-06-22 22:32:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-01 10:40:43 +02:00
|
|
|
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;
|
2020-06-22 22:32:47 +02:00
|
|
|
m_username = segments[ 0 ];
|
|
|
|
m_group = segments[ 1 ];
|
2018-10-01 10:40:43 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-07-27 10:57:15 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
Permissions::apply( const QString& path, int mode )
|
|
|
|
{
|
2020-07-27 11:13:35 +02:00
|
|
|
// 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 );
|
|
|
|
}
|
2020-07-27 10:57:15 +02:00
|
|
|
return r == 0;
|
|
|
|
}
|
|
|
|
|
2020-07-27 11:13:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-27 10:57:15 +02:00
|
|
|
} // namespace CalamaresUtils
|