]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::get_{actual,apparent}_cwd to util
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 16 Jul 2023 12:48:36 +0000 (14:48 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 19:36:09 +0000 (21:36 +0200)
12 files changed:
src/Context.cpp
src/Util.cpp
src/Util.hpp
src/util/path.cpp
src/util/path.hpp
unittest/TestUtil.cpp
unittest/main.cpp
unittest/test_InodeCache.cpp
unittest/test_Util.cpp
unittest/test_argprocessing.cpp
unittest/test_ccache.cpp
unittest/test_util_path.cpp

index 98d175af8db0e426a86027e1bb3e951ec6fd7394..1aaf996cd881a1b7e66cf4306455294db66eecd9 100644 (file)
@@ -39,8 +39,8 @@
 #include <vector>
 
 Context::Context()
-  : actual_cwd(Util::get_actual_cwd()),
-    apparent_cwd(Util::get_apparent_cwd(actual_cwd)),
+  : actual_cwd(util::actual_cwd()),
+    apparent_cwd(util::apparent_cwd(actual_cwd)),
     storage(config)
 #ifdef INODE_CACHE_SUPPORTED
     ,
index 20b35cc57b24181d35837f514ef8dcf5b82a2f67..fb25c052514343ff441b5aa1efbcf87273738435 100644 (file)
@@ -238,39 +238,6 @@ ensure_dir_exists(std::string_view dir)
   }
 }
 
-std::string
-get_actual_cwd()
-{
-  auto cwd = fs::current_path();
-  if (!cwd) {
-    return {};
-  }
-  auto cwd_str = cwd->string();
-#ifdef _WIN32
-  std::replace(cwd_str.begin(), cwd_str.end(), '\\', '/');
-#endif
-  return cwd_str;
-}
-
-std::string
-get_apparent_cwd(const std::string& actual_cwd)
-{
-#ifdef _WIN32
-  return actual_cwd;
-#else
-  auto pwd = getenv("PWD");
-  if (!pwd || !util::is_absolute_path(pwd)) {
-    return actual_cwd;
-  }
-
-  auto pwd_stat = Stat::stat(pwd);
-  auto cwd_stat = Stat::stat(actual_cwd);
-  return !pwd_stat || !cwd_stat || !pwd_stat.same_inode_as(cwd_stat)
-           ? actual_cwd
-           : normalize_concrete_absolute_path(pwd);
-#endif
-}
-
 std::string_view
 get_extension(std::string_view path)
 {
index 1a79a1c926ef9d0088789e03d34e33130a989786..81f1c542c0bc738485e63aad8366f318424d407d 100644 (file)
@@ -61,16 +61,6 @@ void ensure_dir_exists(std::string_view dir);
 // not intended to be machine parsable. `argv` must be terminated by a nullptr.
 std::string format_argv_for_logging(const char* const* argv);
 
-// Return current working directory (CWD) as returned from getcwd(3) (i.e.,
-// normalized path without symlink parts). Returns the empty string on error.
-std::string get_actual_cwd();
-
-// Return current working directory (CWD) by reading the environment variable
-// PWD (thus keeping any symlink parts in the path and potentially ".." or "//"
-// parts). If PWD does not resolve to the same i-node as `actual_cwd` then
-// `actual_cwd` is returned instead.
-std::string get_apparent_cwd(const std::string& actual_cwd);
-
 // Return the file extension (including the dot) as a view into `path`. If
 // `path` has no file extension, an empty string_view is returned.
 std::string_view get_extension(std::string_view path);
index 3e0b8bdb2622e43ce350d6e22f82f5970b317df8..9c9b5ca4eb5d0a6033e358b93133123b10aefdd6 100644 (file)
@@ -36,6 +36,39 @@ namespace fs = util::filesystem;
 
 namespace util {
 
+std::string
+actual_cwd()
+{
+  auto cwd = fs::current_path();
+  if (!cwd) {
+    return {};
+  }
+  auto cwd_str = cwd->string();
+#ifdef _WIN32
+  std::replace(cwd_str.begin(), cwd_str.end(), '\\', '/');
+#endif
+  return cwd_str;
+}
+
+std::string
+apparent_cwd(const std::string& actual_cwd)
+{
+#ifdef _WIN32
+  return actual_cwd;
+#else
+  auto pwd = getenv("PWD");
+  if (!pwd || !util::is_absolute_path(pwd)) {
+    return actual_cwd;
+  }
+
+  auto pwd_stat = Stat::stat(pwd);
+  auto cwd_stat = Stat::stat(actual_cwd);
+  return !pwd_stat || !cwd_stat || !pwd_stat.same_inode_as(cwd_stat)
+           ? actual_cwd
+           : Util::normalize_concrete_absolute_path(pwd);
+#endif
+}
+
 const char*
 get_dev_null_path()
 {
@@ -110,7 +143,7 @@ to_absolute_path(std::string_view path)
     return std::string(path);
   } else {
     return Util::normalize_abstract_absolute_path(
-      FMT("{}/{}", Util::get_actual_cwd(), path));
+      FMT("{}/{}", actual_cwd(), path));
   }
 }
 
index 476fbcc1d528364702e53aef19c62a9fd6b10018..1dc2429805aa8138fa5aa8c69d0e6743c1e8b5d5 100644 (file)
@@ -26,6 +26,16 @@ namespace util {
 
 // --- Interface ---
 
+// Return current working directory (CWD) as returned from getcwd(3) (i.e.,
+// normalized path without symlink parts). Returns the empty string on error.
+std::string actual_cwd();
+
+// Return current working directory (CWD) by reading the environment variable
+// PWD (thus keeping any symlink parts in the path and potentially ".." or "//"
+// parts). If PWD does not resolve to the same inode as `actual_cwd` then
+// `actual_cwd` is returned instead.
+std::string apparent_cwd(const std::string& actual_cwd);
+
 const char* get_dev_null_path();
 
 // Return whether `path` is absolute.
index 0f07d0dc61b7b296278b14f6c799c66156f673d2..362624ba9652a23955836fdfa8e40062d0fe9679 100644 (file)
@@ -24,6 +24,7 @@
 #include <core/wincompat.hpp>
 #include <fmtmacros.hpp>
 #include <util/filesystem.hpp>
+#include <util/path.hpp>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
@@ -35,7 +36,7 @@ namespace TestUtil {
 
 size_t TestContext::m_subdir_counter = 0;
 
-TestContext::TestContext() : m_test_dir(Util::get_actual_cwd())
+TestContext::TestContext() : m_test_dir(util::actual_cwd())
 {
   if (Util::base_name(Util::dir_name(m_test_dir)) != "testdir") {
     throw core::Error("TestContext instantiated outside test directory");
index f2aab3ad4069d9df0cd7986ae8554fa0959a6f5d..c03a4f524e817e0552e4f5ca4216c2a928f195de 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <util/environment.hpp>
 #include <util/filesystem.hpp>
+#include <util/path.hpp>
 
 #include "third_party/fmt/core.h"
 
@@ -39,7 +40,7 @@ main(int argc, char** argv)
 #endif
   util::unsetenv("GCC_COLORS"); // Don't confuse argument processing tests.
 
-  std::string dir_before = Util::get_actual_cwd();
+  std::string dir_before = util::actual_cwd();
   std::string testdir = FMT("testdir/{}", getpid());
   Util::wipe_path(testdir);
   fs::create_directories(testdir);
index 523a7930471f727d2373350e91a93c7ce3a9071f..5c2fc3ecc56ad080714b06304cdc3a631e50877e 100644 (file)
 #include "../src/Context.hpp"
 #include "../src/Hash.hpp"
 #include "../src/InodeCache.hpp"
-#include "../src/Util.hpp"
 #include "TestUtil.hpp"
 
 #include <Fd.hpp>
 #include <util/file.hpp>
+#include <util/path.hpp>
 
 #include "third_party/doctest.h"
 
@@ -39,7 +39,7 @@ namespace {
 bool
 inode_cache_available()
 {
-  Fd fd(open(Util::get_actual_cwd().c_str(), O_RDONLY));
+  Fd fd(open(util::actual_cwd().c_str(), O_RDONLY));
   return fd && InodeCache::available(*fd);
 }
 
@@ -48,7 +48,7 @@ init(Config& config)
 {
   config.set_debug(true);
   config.set_inode_cache(true);
-  config.set_temporary_dir(Util::get_actual_cwd());
+  config.set_temporary_dir(util::actual_cwd());
 }
 
 bool
index 195e1caf9fa2da27a4818fa6c244631d95c17001..f4ee3ecb39fa8c4ea84282a105c2497001ed8fe1 100644 (file)
@@ -28,6 +28,7 @@
 #include <util/environment.hpp>
 #include <util/file.hpp>
 #include <util/filesystem.hpp>
+#include <util/path.hpp>
 
 #include "third_party/doctest.h"
 
@@ -247,7 +248,7 @@ TEST_CASE("Util::make_relative_path")
 
   const TestContext test_context;
 
-  const std::string cwd = Util::get_actual_cwd();
+  const std::string cwd = util::actual_cwd();
   const std::string actual_cwd = FMT("{}/d", cwd);
 #ifdef _WIN32
   const std::string apparent_cwd = actual_cwd;
@@ -380,7 +381,7 @@ TEST_CASE("Util::normalize_concrete_absolute_path")
   util::write_file("file", "");
   REQUIRE(fs::create_directories("dir1/dir2"));
   REQUIRE(symlink("dir1/dir2", "symlink") == 0);
-  const auto cwd = Util::get_actual_cwd();
+  const auto cwd = util::actual_cwd();
 
   CHECK(Util::normalize_concrete_absolute_path(FMT("{}/file", cwd))
         == FMT("{}/file", cwd));
index dc7e935ea427210691d03fec8bf8c1e9f39d7220..19a485cca799eb279736a62ef4692c9674977589 100644 (file)
@@ -19,7 +19,6 @@
 #include "../src/Args.hpp"
 #include "../src/Config.hpp"
 #include "../src/Context.hpp"
-#include "../src/Util.hpp"
 #include "../src/fmtmacros.hpp"
 #include "TestUtil.hpp"
 #include "argprocessing.hpp"
@@ -28,6 +27,7 @@
 #include <core/Statistic.hpp>
 #include <core/wincompat.hpp>
 #include <util/file.hpp>
+#include <util/path.hpp>
 #include <util/string.hpp>
 
 #include "third_party/doctest.h"
@@ -46,7 +46,7 @@ get_root()
   return "/";
 #else
   char volume[4]; // "C:\"
-  GetVolumePathName(Util::get_actual_cwd().c_str(), volume, sizeof(volume));
+  GetVolumePathName(util::actual_cwd().c_str(), volume, sizeof(volume));
   volume[2] = '/'; // Since base directory is normalized to forward slashes
   return volume;
 #endif
@@ -715,11 +715,11 @@ TEST_CASE("-x")
   }
 }
 
-// On macOS ctx.actual_cwd() typically starts with /Users which clashes with
+// On macOS ctx.actual_cwd typically starts with /Users which clashes with
 // MSVC's /U option, so disable the test case there. This will be possible to
 // improve when/if a compiler abstraction is introduced (issue #956).
 TEST_CASE("MSVC options"
-          * doctest::skip(util::starts_with(Util::get_actual_cwd(), "/U")))
+          * doctest::skip(util::starts_with(util::actual_cwd(), "/U")))
 {
   TestContext test_context;
   Context ctx;
index 8e1f510e1280de3b2cf619208db5959acba7de10..bb4bf8860504f6364f3be72017c9cb0f44468bbf 100644 (file)
@@ -21,9 +21,9 @@
 #include "../src/fmtmacros.hpp"
 #include "TestUtil.hpp"
 
-#include <Util.hpp>
 #include <core/wincompat.hpp>
 #include <util/file.hpp>
+#include <util/path.hpp>
 
 #include "third_party/doctest.h"
 
@@ -193,7 +193,7 @@ TEST_CASE("guess_compiler")
 #ifndef _WIN32
   SUBCASE("Follow symlink to actual compiler")
   {
-    const auto cwd = Util::get_actual_cwd();
+    const auto cwd = util::actual_cwd();
     util::write_file(FMT("{}/gcc", cwd), "");
     CHECK(symlink("gcc", FMT("{}/intermediate", cwd).c_str()) == 0);
     const auto cc = FMT("{}/cc", cwd);
index 8bb703dffcfac9078bca3cc5eae1d61e7d4867f2..307a03db328c53f31696b91957bf7a688a9ab85d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -87,7 +87,7 @@ TEST_CASE("util::to_absolute_path")
   CHECK(util::to_absolute_path("C:\\foo\\bar") == "C:\\foo\\bar");
 #endif
 
-  const auto cwd = Util::get_actual_cwd();
+  const auto cwd = util::actual_cwd();
 
   CHECK(util::to_absolute_path("") == cwd);
   CHECK(util::to_absolute_path(".") == cwd);
@@ -105,7 +105,7 @@ TEST_CASE("util::to_absolute_path_no_drive")
   CHECK(util::to_absolute_path_no_drive("C:\\foo\\bar") == "\\foo\\bar");
 #endif
 
-  auto cwd = Util::get_actual_cwd();
+  auto cwd = util::actual_cwd();
 #ifdef _WIN32
   cwd = cwd.substr(2);
 #endif