2018-10-01 10:45:39 +02:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
*
|
2020-06-22 22:32:47 +02:00
|
|
|
* SPDX-FileCopyrightText: 2018 Scott Harvey <scott@spharvey.me>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
* License-Filename: LICENSE
|
2018-10-01 10:40:43 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-06-22 22:32:47 +02:00
|
|
|
#ifndef LIBCALAMARES_PERMISSIONS_H
|
|
|
|
#define LIBCALAMARES_PERMISSIONS_H
|
|
|
|
|
|
|
|
#include "DllMacro.h"
|
2018-10-01 10:40:43 +02:00
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
2020-07-27 10:57:15 +02:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
|
|
|
|
2018-10-01 10:40:43 +02:00
|
|
|
/**
|
2020-06-22 22:32:47 +02:00
|
|
|
* @brief The Permissions class takes a QString @p in the form of
|
2018-10-01 10:40:43 +02:00
|
|
|
* <user>:<group>:<permissions>, checks it for validity, and makes the three
|
|
|
|
* components available indivdually.
|
|
|
|
*/
|
2020-06-22 22:32:47 +02:00
|
|
|
class DLLEXPORT Permissions
|
2018-10-01 10:40:43 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2018-10-01 10:51:21 +02:00
|
|
|
/** @brief Constructor
|
2020-06-22 22:32:47 +02:00
|
|
|
*
|
|
|
|
* Splits the string @p at the colon (":") into separate elements for
|
2020-07-24 14:24:03 +02:00
|
|
|
* <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.
|
2018-10-01 10:51:21 +02:00
|
|
|
*/
|
2020-07-24 14:24:03 +02:00
|
|
|
Permissions( QString const& p );
|
2020-06-22 22:32:47 +02:00
|
|
|
|
2020-07-24 14:24:03 +02:00
|
|
|
/// @brief Default constructor of an invalid Permissions.
|
2018-10-01 10:51:21 +02:00
|
|
|
Permissions();
|
2018-10-01 10:40:43 +02:00
|
|
|
|
2020-07-24 14:24:03 +02:00
|
|
|
/// @brief Was the Permissions object constructed from valid data?
|
2018-10-01 10:40:43 +02:00
|
|
|
bool isValid() const { return m_valid; }
|
2020-07-24 14:24:03 +02:00
|
|
|
/// @brief The user (first component, e.g. "root" in "root:wheel:755")
|
2018-10-01 10:40:43 +02:00
|
|
|
QString username() const { return m_username; }
|
2020-07-24 14:24:03 +02:00
|
|
|
/// @brief The group (second component, e.g. "wheel" in "root:wheel:755")
|
2018-10-01 10:40:43 +02:00
|
|
|
QString group() const { return m_group; }
|
2020-07-24 14:24:03 +02:00
|
|
|
/** @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").
|
|
|
|
*/
|
2018-10-01 10:40:43 +02:00
|
|
|
int value() const { return m_value; }
|
2020-07-24 14:24:03 +02:00
|
|
|
/** @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 ); }
|
2018-10-01 10:40:43 +02:00
|
|
|
|
2020-07-27 10:57:15 +02:00
|
|
|
/// chmod(path, mode), returns true on success
|
|
|
|
static bool apply( const QString& path, int mode );
|
|
|
|
|
2018-10-01 10:40:43 +02:00
|
|
|
private:
|
2020-06-22 22:32:47 +02:00
|
|
|
void parsePermissions( QString const& p );
|
2018-10-01 10:40:43 +02:00
|
|
|
|
|
|
|
QString m_username;
|
|
|
|
QString m_group;
|
|
|
|
int m_value;
|
2020-07-24 14:24:03 +02:00
|
|
|
bool m_valid;
|
2018-10-01 10:40:43 +02:00
|
|
|
};
|
|
|
|
|
2020-07-27 10:57:15 +02:00
|
|
|
} // namespace CalamaresUtils
|
|
|
|
|
2020-06-22 22:32:47 +02:00
|
|
|
#endif // LIBCALAMARES_PERMISSIONS_H
|