Manager: Put machine specific values in a separate class
This commit is contained in:
+6
-10
@@ -16,6 +16,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "dotfile.h"
|
||||
#include "machine.h"
|
||||
|
||||
std::vector<Dotfile::ExcludePath> Dotfile::s_excludePaths;
|
||||
std::vector<std::filesystem::path> Dotfile::s_systemDirectories;
|
||||
@@ -179,11 +180,6 @@ void Dotfile::sync(const std::vector<std::string>& paths, const std::vector<size
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the password database record (/etc/passwd) of the user logged in on
|
||||
// the controlling terminal of the process
|
||||
// FIXME: Do error handling on these calls
|
||||
passwd* user = getpwnam(getlogin());
|
||||
|
||||
auto printError = [](const std::filesystem::path& path, const std::error_code& error) -> void {
|
||||
// std::filesystem::copy doesnt respect 'overwrite_existing' for symlinks
|
||||
if (error.value() && error.message() != "File exists") {
|
||||
@@ -198,11 +194,11 @@ void Dotfile::sync(const std::vector<std::string>& paths, const std::vector<size
|
||||
| std::filesystem::copy_options::recursive
|
||||
| std::filesystem::copy_options::copy_symlinks;
|
||||
|
||||
auto copy = [&root, &user, &printError](const std::filesystem::path& from,
|
||||
const std::filesystem::path& to, bool homePath) -> void {
|
||||
auto copy = [&root, &printError](const std::filesystem::path& from,
|
||||
const std::filesystem::path& to, bool homePath) -> void {
|
||||
if (homePath && root) {
|
||||
seteuid(user->pw_uid);
|
||||
setegid(user->pw_gid);
|
||||
seteuid(Machine::the().uid());
|
||||
setegid(Machine::the().gid());
|
||||
}
|
||||
|
||||
// Create directory for the file
|
||||
@@ -228,7 +224,7 @@ void Dotfile::sync(const std::vector<std::string>& paths, const std::vector<size
|
||||
};
|
||||
|
||||
// /home/<user>/
|
||||
std::string homeDirectory = "/home/" + std::string(user->pw_name);
|
||||
std::string homeDirectory = "/home/" + Machine::the().username();
|
||||
for (size_t i : homeIndices) {
|
||||
std::string homePaths[2];
|
||||
generateHomePaths(homePaths, paths.at(i), homeDirectory);
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <pwd.h> // getpwnam
|
||||
#include <sstream> // istringstream
|
||||
#include <unistd.h> // gethostname, getlogin
|
||||
|
||||
#include "machine.h"
|
||||
#include "util/file.h"
|
||||
|
||||
Machine::Machine(s)
|
||||
{
|
||||
auto osRelease = Util::File("/etc/os-release");
|
||||
auto stream = std::istringstream(osRelease.data());
|
||||
std::string line;
|
||||
while (std::getline(stream, line)) {
|
||||
if (line.find("ID=") == 0) {
|
||||
m_distroId = line.substr(3);
|
||||
}
|
||||
if (line.find("ID_LIKE=") == 0) {
|
||||
m_distroIdLike = line.substr(8);
|
||||
}
|
||||
}
|
||||
|
||||
char hostname[64] { 0 };
|
||||
if (gethostname(hostname, 64) < 0) {
|
||||
perror("\033[31;1mError:\033[0m gethostname");
|
||||
}
|
||||
m_hostname = hostname;
|
||||
|
||||
// Get the username logged in on the controlling terminal of the process
|
||||
char username[32] { 0 };
|
||||
if (getlogin_r(username, 32) != 0) {
|
||||
perror("\033[31;1mError:\033[0m getlogin_r");
|
||||
}
|
||||
|
||||
// Get the password database record (/etc/passwd) of the user
|
||||
m_passwd = getpwnam(username);
|
||||
if (m_passwd == nullptr) {
|
||||
perror("\033[31;1mError:\033[0m getpwnam");
|
||||
}
|
||||
}
|
||||
|
||||
Machine::~Machine()
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#ifndef MACHINE_H
|
||||
#define MACHINE_H
|
||||
|
||||
#include <cstdint> // uint32_t
|
||||
#include <pwd.h> // passwd
|
||||
#include <string>
|
||||
|
||||
#include "util/singleton.h"
|
||||
|
||||
class Machine : public Util::Singleton<Machine> {
|
||||
public:
|
||||
Machine(s);
|
||||
virtual ~Machine();
|
||||
|
||||
const std::string& distroId() { return m_distroId; }
|
||||
const std::string& distroIdLike() { return m_distroIdLike; }
|
||||
const std::string& hostname() { return m_hostname; }
|
||||
|
||||
std::string username() { return m_passwd->pw_name; }
|
||||
uint32_t uid() { return m_passwd->pw_uid; }
|
||||
uint32_t gid() { return m_passwd->pw_gid; }
|
||||
|
||||
private:
|
||||
std::string m_distroId;
|
||||
std::string m_distroIdLike;
|
||||
std::string m_hostname;
|
||||
passwd* m_passwd { nullptr };
|
||||
};
|
||||
|
||||
#endif // MACHINE_H
|
||||
+6
-7
@@ -1,3 +1,9 @@
|
||||
/*
|
||||
* Copyright (C) 2021-2022 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <cstdio> // fprintf, perror, stderr
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
@@ -54,11 +60,6 @@ int main(int argc, const char* argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
char hostname[64] { 0 };
|
||||
if (gethostname(hostname, 64) < 0) {
|
||||
perror("\033[31;1mError:\033[0m gethostname");
|
||||
}
|
||||
|
||||
Util::Timer t;
|
||||
|
||||
if (fileOperation) {
|
||||
@@ -96,8 +97,6 @@ int main(int argc, const char* argv[])
|
||||
else if (packageOperation) {
|
||||
Package package;
|
||||
|
||||
Package::setHostname(hostname);
|
||||
|
||||
if (addOrAur) {
|
||||
// TODO install tracked AUR packages
|
||||
}
|
||||
|
||||
+4
-17
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Riyyi
|
||||
* Copyright (C) 2021-2022 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
@@ -10,12 +10,11 @@
|
||||
#include <string> // getline
|
||||
#include <vector>
|
||||
|
||||
#include "machine.h"
|
||||
#include "package.h"
|
||||
#include "util/file.h"
|
||||
#include "util/system.h"
|
||||
|
||||
std::string Package::m_hostname;
|
||||
|
||||
Package::Package()
|
||||
{
|
||||
}
|
||||
@@ -74,20 +73,8 @@ void Package::store()
|
||||
|
||||
bool Package::distroDetect()
|
||||
{
|
||||
std::string id;
|
||||
std::string idLike;
|
||||
|
||||
auto osRelease = Util::File("/etc/os-release");
|
||||
auto stream = std::istringstream(osRelease.data());
|
||||
std::string line;
|
||||
while (std::getline(stream, line)) {
|
||||
if (line.find("ID=") == 0) {
|
||||
id = line.substr(3);
|
||||
}
|
||||
if (line.find("ID_LIKE=") == 0) {
|
||||
idLike = line.substr(8);
|
||||
}
|
||||
}
|
||||
std::string id = Machine::the().distroId();
|
||||
std::string idLike = Machine::the().distroIdLike();
|
||||
|
||||
if (id == "arch") {
|
||||
m_distro = Distro::Arch;
|
||||
|
||||
+1
-5
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Riyyi
|
||||
* Copyright (C) 2021-2022 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
@@ -26,15 +26,11 @@ public:
|
||||
void list(const std::vector<std::string>& targets = {});
|
||||
void store();
|
||||
|
||||
static void setHostname(const std::string& hostname) { m_hostname = hostname; }
|
||||
|
||||
private:
|
||||
bool distroDetect();
|
||||
bool distroDependencies();
|
||||
std::string getPackageList();
|
||||
|
||||
static std::string m_hostname;
|
||||
|
||||
Distro m_distro { Distro::Unsupported };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user