From 9ceb6699b5a12ce92afde0a64bcaccf23ad8fa0a Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Tue, 18 Jul 2023 21:25:26 +0200 Subject: [PATCH] refactor: Move Util::get_home_directory to Config --- src/Config.cpp | 35 ++++++++++++++++++++++++++++++++++- src/Util.cpp | 31 ------------------------------- src/Util.hpp | 4 ---- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index 9290acd41..3080fa7bc 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -40,6 +40,12 @@ #include "third_party/fmt/core.h" +#include + +#ifdef HAVE_PWD_H +# include +#endif + #ifdef HAVE_UNISTD_H # include #endif @@ -481,6 +487,33 @@ default_config_dir(const std::string& home_dir) } #endif +std::string +home_directory() +{ +#ifdef _WIN32 + if (const char* p = getenv("USERPROFILE")) { + return p; + } + throw core::Fatal( + "The USERPROFILE environment variable must be set to your user profile" + " folder"); +#else + if (const char* p = getenv("HOME")) { + return p; + } +# ifdef HAVE_GETPWUID + { + struct passwd* pwd = getpwuid(getuid()); + if (pwd) { + return pwd->pw_dir; + } + } +# endif + throw core::Fatal( + "Could not determine home directory from $HOME or getpwuid(3)"); +#endif +} + std::string compiler_type_to_string(CompilerType compiler_type) { @@ -512,7 +545,7 @@ Config::read(const std::vector& cmdline_config_settings) auto cmdline_settings_map = create_cmdline_settings_map(cmdline_config_settings); - const std::string home_dir = Util::get_home_directory(); + const std::string home_dir = home_directory(); const std::string legacy_ccache_dir = Util::make_path(home_dir, ".ccache"); const bool legacy_ccache_dir_exists = Stat::stat(legacy_ccache_dir).is_directory(); diff --git a/src/Util.cpp b/src/Util.cpp index 81ef5f712..4e34bd0ac 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -46,10 +46,6 @@ #include -#ifdef HAVE_PWD_H -# include -#endif - using IncludeDelimiter = util::Tokenizer::IncludeDelimiter; namespace fs = util::filesystem; @@ -232,33 +228,6 @@ get_extension(std::string_view path) } } -std::string -get_home_directory() -{ -#ifdef _WIN32 - if (const char* p = getenv("USERPROFILE")) { - return p; - } - throw core::Fatal( - "The USERPROFILE environment variable must be set to your user profile " - "folder"); -#else - if (const char* p = getenv("HOME")) { - return p; - } -# ifdef HAVE_GETPWUID - { - struct passwd* pwd = getpwuid(getuid()); - if (pwd) { - return pwd->pw_dir; - } - } -# endif - throw core::Fatal( - "Could not determine home directory from $HOME or getpwuid(3)"); -#endif -} - const char* get_hostname() { diff --git a/src/Util.hpp b/src/Util.hpp index 70ef15cff..6c4652582 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -56,10 +56,6 @@ std::string_view dir_name(std::string_view path); // `path` has no file extension, an empty string_view is returned. std::string_view get_extension(std::string_view path); -// Return the current user's home directory, or throw `Fatal` if it can't -// be determined. -std::string get_home_directory(); - // Return a static string with the current hostname. const char* get_hostname(); -- 2.47.2