]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify get_home_directory
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 31 Jul 2020 17:12:14 +0000 (19:12 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 31 Jul 2020 17:12:14 +0000 (19:12 +0200)
src/AtomicFile.cpp
src/Config.cpp
src/Config.hpp
src/Util.cpp
src/Util.hpp
src/legacy_util.cpp
src/legacy_util.hpp
unittest/test_Config.cpp

index 795c45fb42f2c6099c45700f928130af27f5a458..10920fc229aad82ab5343d0abd2105e3c395376d 100644 (file)
@@ -21,6 +21,7 @@
 #include "TemporaryFile.hpp"
 #include "Util.hpp"
 #include "exceptions.hpp"
+#include "legacy_util.hpp"
 
 #include "third_party/fmt/core.h"
 
index 9a708e7dce83e0e2c1bdb1ce39aecae0884619af..61bd6a03d417b938128619a7ef52a74e044e3a23 100644 (file)
@@ -23,6 +23,8 @@
 #include "ccache.hpp"
 #include "exceptions.hpp"
 
+#include "third_party/fmt/core.h"
+
 #include <algorithm>
 #include <cassert>
 #include <fstream>
index 76e10dc33c77d74f2a56b9088cc1e3aadb0109e1..0cfbac8794a8a8fb1a956e523744376d060b5497 100644 (file)
@@ -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 <functional>
@@ -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";
index c2cb37b43b1eac04e72e74478479314c6b91f911..d63565ecfa473c6d31aea0ffc79d5d214c7aedc1 100644 (file)
 #include <algorithm>
 #include <fstream>
 
+#ifdef HAVE_PWD_H
+#  include <pwd.h>
+#endif
+
 #ifdef HAVE_LINUX_FS_H
 #  include <linux/magic.h>
 #  include <sys/statfs.h>
@@ -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()
 {
index 000f4b855eac0cb04549e5b960ec6df6ad5ca3cc..62b9e49fc5da9eac9c8479545a6fc0a0094c736d 100644 (file)
@@ -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<std::shared_ptr<CacheFile>>& 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();
 
index ac5e0339c3bd7bea33d4b0f63e3792821507356b..ca79f26b394cde35810e27d3d559f5d1606f82ab 100644 (file)
 #  include "Win32Util.hpp"
 #endif
 
-#ifdef HAVE_PWD_H
-#  include <pwd.h>
-#endif
 #ifdef HAVE_SYS_TIME_H
 #  include <sys/time.h>
 #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)
index 1c37959c59d2171bfa7e7bfe99e04e3dcf88f914..44b018fe588dbe4a71ba91278285b0ecf6cee9f8 100644 (file)
@@ -22,7 +22,6 @@
 
 #include <string>
 
-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;
index 454fcc24404417071624ebefe19227828a005652..7653dc2679e3fc150c00afb9741ad47e9b5d62c6 100644 (file)
@@ -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);