]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::get_home_directory to Config
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 19:25:26 +0000 (21:25 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 19:55:03 +0000 (21:55 +0200)
src/Config.cpp
src/Util.cpp
src/Util.hpp

index 9290acd41ca3f99e7a6edb82665aab5331820a48..3080fa7bc759bdeaf400ff13bd089da07fbe78de 100644 (file)
 
 #include "third_party/fmt/core.h"
 
+#include <sys/types.h>
+
+#ifdef HAVE_PWD_H
+#  include <pwd.h>
+#endif
+
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
 #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<std::string>& 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();
index 81ef5f712669d2a7caafc2bf6ad1c348b896e86a..4e34bd0ac4c6c3d47dc8af6f6c3b9d7b48f00234 100644 (file)
 
 #include <fcntl.h>
 
-#ifdef HAVE_PWD_H
-#  include <pwd.h>
-#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()
 {
index 70ef15cffbf05a9312ce88548ad73dfee86c4220..6c4652582b2ae9f4d7c1e5c1a2dc5ca1cb7e26bb 100644 (file)
@@ -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();