From: Joel Rosdahl Date: Fri, 31 Jul 2020 17:12:14 +0000 (+0200) Subject: C++-ify get_home_directory X-Git-Tag: v4.0~246 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b1503325baacc51c4e08cdb5589100efa3c5ff99;p=thirdparty%2Fccache.git C++-ify get_home_directory --- diff --git a/src/AtomicFile.cpp b/src/AtomicFile.cpp index 795c45fb4..10920fc22 100644 --- a/src/AtomicFile.cpp +++ b/src/AtomicFile.cpp @@ -21,6 +21,7 @@ #include "TemporaryFile.hpp" #include "Util.hpp" #include "exceptions.hpp" +#include "legacy_util.hpp" #include "third_party/fmt/core.h" diff --git a/src/Config.cpp b/src/Config.cpp index 9a708e7dc..61bd6a03d 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -23,6 +23,8 @@ #include "ccache.hpp" #include "exceptions.hpp" +#include "third_party/fmt/core.h" + #include #include #include diff --git a/src/Config.hpp b/src/Config.hpp index 76e10dc33..0cfbac879 100644 --- a/src/Config.hpp +++ b/src/Config.hpp @@ -21,9 +21,9 @@ #include "system.hpp" #include "NonCopyable.hpp" +#include "Util.hpp" #include "legacy_util.hpp" -#include "third_party/fmt/core.h" #include "third_party/nonstd/optional.hpp" #include @@ -128,7 +128,7 @@ private: std::string m_secondary_config_path; std::string m_base_dir = ""; - std::string m_cache_dir = fmt::format("{}/.ccache", get_home_directory()); + std::string m_cache_dir = Util::get_home_directory() + "/.ccache"; uint32_t m_cache_dir_levels = 2; std::string m_compiler = ""; std::string m_compiler_check = "mtime"; diff --git a/src/Util.cpp b/src/Util.cpp index c2cb37b43..d63565ecf 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -29,6 +29,10 @@ #include #include +#ifdef HAVE_PWD_H +# include +#endif + #ifdef HAVE_LINUX_FS_H # include # include @@ -586,6 +590,30 @@ get_level_1_files(const std::string& dir, progress_receiver(1.0); } +std::string +get_home_directory() +{ + const char* p = getenv("HOME"); + if (p) { + return p; + } +#ifdef _WIN32 + p = getenv("APPDATA"); + if (p) { + return p; + } +#endif +#ifdef HAVE_GETPWUID + { + struct passwd* pwd = getpwuid(getuid()); + if (pwd) { + return pwd->pw_dir; + } + } +#endif + FATAL("Could not determine home directory from $HOME or getpwuid(3)"); +} + const char* get_hostname() { diff --git a/src/Util.hpp b/src/Util.hpp index 000f4b855..62b9e49fc 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -21,7 +21,6 @@ #include "system.hpp" #include "CacheFile.hpp" -#include "Config.hpp" #include "third_party/nonstd/optional.hpp" #include "third_party/nonstd/string_view.hpp" @@ -195,6 +194,10 @@ void get_level_1_files(const std::string& dir, const ProgressReceiver& progress_receiver, std::vector>& files); +// Return the current user's home directory, or throw `FatalError` if it can't +// be determined. +std::string get_home_directory(); + // Return a static string with the current hostname. const char* get_hostname(); diff --git a/src/legacy_util.cpp b/src/legacy_util.cpp index ac5e0339c..ca79f26b3 100644 --- a/src/legacy_util.cpp +++ b/src/legacy_util.cpp @@ -27,39 +27,10 @@ # include "Win32Util.hpp" #endif -#ifdef HAVE_PWD_H -# include -#endif #ifdef HAVE_SYS_TIME_H # include #endif -// Return current user's home directory, or throw FatalError if it can't be -// determined. -const char* -get_home_directory() -{ - const char* p = getenv("HOME"); - if (p) { - return p; - } -#ifdef _WIN32 - p = getenv("APPDATA"); - if (p) { - return p; - } -#endif -#ifdef HAVE_GETPWUID - { - struct passwd* pwd = getpwuid(getuid()); - if (pwd) { - return pwd->pw_dir; - } - } -#endif - FATAL("Could not determine home directory from $HOME or getpwuid(3)"); -} - // Return whether the argument is a full path. bool is_full_path(const char* path) diff --git a/src/legacy_util.hpp b/src/legacy_util.hpp index 1c37959c5..44b018fe5 100644 --- a/src/legacy_util.hpp +++ b/src/legacy_util.hpp @@ -22,7 +22,6 @@ #include -const char* get_home_directory(); bool is_full_path(const char* path); void update_mtime(const char* path); void x_exit(int status) ATTR_NORETURN; diff --git a/unittest/test_Config.cpp b/unittest/test_Config.cpp index 454fcc244..7653dc267 100644 --- a/unittest/test_Config.cpp +++ b/unittest/test_Config.cpp @@ -38,8 +38,7 @@ TEST_CASE("Config: default values") { Config config; - std::string expected_cache_dir = - fmt::format("{}/.ccache", get_home_directory()); + std::string expected_cache_dir = Util::get_home_directory() + "/.ccache"; CHECK(config.base_dir().empty()); CHECK(config.cache_dir() == expected_cache_dir);