From 8c73c618d777a07c017ec0deefe88db83652827e Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Mon, 17 Jun 2024 15:11:36 +0200 Subject: Move functionality from StorageConfig to related classes. --- src/buildtool/file_system/file_system_manager.hpp | 26 ++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/buildtool/file_system/file_system_manager.hpp') diff --git a/src/buildtool/file_system/file_system_manager.hpp b/src/buildtool/file_system/file_system_manager.hpp index 9e8826aa..93a911d8 100644 --- a/src/buildtool/file_system/file_system_manager.hpp +++ b/src/buildtool/file_system/file_system_manager.hpp @@ -18,7 +18,8 @@ #include #include #include -#include // for std::fopen +#include // for std::fopen +#include // std::exit, std::getenv #include #include #include @@ -28,10 +29,13 @@ #ifdef __unix__ #include +#include #include #include #include #include +#else +#error "Non-unix is not supported yet" #endif #include "gsl/gsl" @@ -139,6 +143,26 @@ class FileSystemManager { return CreateFileImpl(file) == CreationStatus::Created; } + /// \brief Determine user home directory + [[nodiscard]] static auto GetUserHome() noexcept -> std::filesystem::path { + char const* root{nullptr}; + +#ifdef __unix__ + root = std::getenv("HOME"); + if (root == nullptr) { + root = getpwuid(getuid())->pw_dir; + } +#endif + + if (root == nullptr) { + Logger::Log(LogLevel::Error, + "Cannot determine user home directory."); + std::exit(EXIT_FAILURE); + } + + return root; + } + /// \brief We are POSIX-compliant, therefore we only care about the string /// value the symlinks points to, whether it exists or not, not the target /// type. As such, we don't distinguish directory or file targets. However, -- cgit v1.2.3