diff --git a/src/dotfile.cpp b/src/dotfile.cpp index 23c878e..b0860ec 100644 --- a/src/dotfile.cpp +++ b/src/dotfile.cpp @@ -16,6 +16,7 @@ #include #include "dotfile.h" +#include "machine.h" std::vector Dotfile::s_excludePaths; std::vector Dotfile::s_systemDirectories; @@ -179,11 +180,6 @@ void Dotfile::sync(const std::vector& paths, const std::vector 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& paths, const std::vector 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& paths, const std::vector/ - 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); diff --git a/src/machine.cpp b/src/machine.cpp new file mode 100644 index 0000000..70ef712 --- /dev/null +++ b/src/machine.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2022 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#include // getpwnam +#include // istringstream +#include // 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() +{ +} diff --git a/src/machine.h b/src/machine.h new file mode 100644 index 0000000..547ed1e --- /dev/null +++ b/src/machine.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2022 Riyyi + * + * SPDX-License-Identifier: MIT + */ + +#ifndef MACHINE_H +#define MACHINE_H + +#include // uint32_t +#include // passwd +#include + +#include "util/singleton.h" + +class Machine : public Util::Singleton { +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 diff --git a/src/main.cpp b/src/main.cpp index a12ac81..e807e5d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,9 @@ +/* + * Copyright (C) 2021-2022 Riyyi + * + * SPDX-License-Identifier: MIT + */ + #include // fprintf, perror, stderr #include #include @@ -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 } diff --git a/src/package.cpp b/src/package.cpp index 6319253..53a3cc4 100644 --- a/src/package.cpp +++ b/src/package.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Riyyi + * Copyright (C) 2021-2022 Riyyi * * SPDX-License-Identifier: MIT */ @@ -10,12 +10,11 @@ #include // getline #include +#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; diff --git a/src/package.h b/src/package.h index 411aef7..4f2c6ce 100644 --- a/src/package.h +++ b/src/package.h @@ -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& 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 }; };