Browse Source

Manager: Put machine specific values in a separate class

master
Riyyi 3 years ago
parent
commit
bc3fc6c345
  1. 14
      src/dotfile.cpp
  2. 49
      src/machine.cpp
  3. 36
      src/machine.h
  4. 13
      src/main.cpp
  5. 21
      src/package.cpp
  6. 6
      src/package.h

14
src/dotfile.cpp

@ -16,6 +16,7 @@
#include <vector> #include <vector>
#include "dotfile.h" #include "dotfile.h"
#include "machine.h"
std::vector<Dotfile::ExcludePath> Dotfile::s_excludePaths; std::vector<Dotfile::ExcludePath> Dotfile::s_excludePaths;
std::vector<std::filesystem::path> Dotfile::s_systemDirectories; 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; 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 { auto printError = [](const std::filesystem::path& path, const std::error_code& error) -> void {
// std::filesystem::copy doesnt respect 'overwrite_existing' for symlinks // std::filesystem::copy doesnt respect 'overwrite_existing' for symlinks
if (error.value() && error.message() != "File exists") { 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::recursive
| std::filesystem::copy_options::copy_symlinks; | std::filesystem::copy_options::copy_symlinks;
auto copy = [&root, &user, &printError](const std::filesystem::path& from, auto copy = [&root, &printError](const std::filesystem::path& from,
const std::filesystem::path& to, bool homePath) -> void { const std::filesystem::path& to, bool homePath) -> void {
if (homePath && root) { if (homePath && root) {
seteuid(user->pw_uid); seteuid(Machine::the().uid());
setegid(user->pw_gid); setegid(Machine::the().gid());
} }
// Create directory for the file // Create directory for the file
@ -228,7 +224,7 @@ void Dotfile::sync(const std::vector<std::string>& paths, const std::vector<size
}; };
// /home/<user>/ // /home/<user>/
std::string homeDirectory = "/home/" + std::string(user->pw_name); std::string homeDirectory = "/home/" + Machine::the().username();
for (size_t i : homeIndices) { for (size_t i : homeIndices) {
std::string homePaths[2]; std::string homePaths[2];
generateHomePaths(homePaths, paths.at(i), homeDirectory); generateHomePaths(homePaths, paths.at(i), homeDirectory);

49
src/machine.cpp

@ -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()
{
}

36
src/machine.h

@ -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

13
src/main.cpp

@ -1,3 +1,9 @@
/*
* Copyright (C) 2021-2022 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include <cstdio> // fprintf, perror, stderr #include <cstdio> // fprintf, perror, stderr
#include <filesystem> #include <filesystem>
#include <string> #include <string>
@ -54,11 +60,6 @@ int main(int argc, const char* argv[])
return 1; return 1;
} }
char hostname[64] { 0 };
if (gethostname(hostname, 64) < 0) {
perror("\033[31;1mError:\033[0m gethostname");
}
Util::Timer t; Util::Timer t;
if (fileOperation) { if (fileOperation) {
@ -96,8 +97,6 @@ int main(int argc, const char* argv[])
else if (packageOperation) { else if (packageOperation) {
Package package; Package package;
Package::setHostname(hostname);
if (addOrAur) { if (addOrAur) {
// TODO install tracked AUR packages // TODO install tracked AUR packages
} }

21
src/package.cpp

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2021 Riyyi * Copyright (C) 2021-2022 Riyyi
* *
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
@ -10,12 +10,11 @@
#include <string> // getline #include <string> // getline
#include <vector> #include <vector>
#include "machine.h"
#include "package.h" #include "package.h"
#include "util/file.h" #include "util/file.h"
#include "util/system.h" #include "util/system.h"
std::string Package::m_hostname;
Package::Package() Package::Package()
{ {
} }
@ -74,20 +73,8 @@ void Package::store()
bool Package::distroDetect() bool Package::distroDetect()
{ {
std::string id; std::string id = Machine::the().distroId();
std::string idLike; std::string idLike = Machine::the().distroIdLike();
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);
}
}
if (id == "arch") { if (id == "arch") {
m_distro = Distro::Arch; m_distro = Distro::Arch;

6
src/package.h

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2021 Riyyi * Copyright (C) 2021-2022 Riyyi
* *
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
@ -26,15 +26,11 @@ public:
void list(const std::vector<std::string>& targets = {}); void list(const std::vector<std::string>& targets = {});
void store(); void store();
static void setHostname(const std::string& hostname) { m_hostname = hostname; }
private: private:
bool distroDetect(); bool distroDetect();
bool distroDependencies(); bool distroDependencies();
std::string getPackageList(); std::string getPackageList();
static std::string m_hostname;
Distro m_distro { Distro::Unsupported }; Distro m_distro { Distro::Unsupported };
}; };

Loading…
Cancel
Save