From: Joel Rosdahl Date: Sun, 16 Jul 2023 12:48:36 +0000 (+0200) Subject: refactor: Move Util::get_{actual,apparent}_cwd to util X-Git-Tag: v4.9~117 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=644736871a7e9373e30c46b56741b8064b8eb980;p=thirdparty%2Fccache.git refactor: Move Util::get_{actual,apparent}_cwd to util --- diff --git a/src/Context.cpp b/src/Context.cpp index 98d175af8..1aaf996cd 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -39,8 +39,8 @@ #include 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 , diff --git a/src/Util.cpp b/src/Util.cpp index 20b35cc57..fb25c0525 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -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) { diff --git a/src/Util.hpp b/src/Util.hpp index 1a79a1c92..81f1c542c 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -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); diff --git a/src/util/path.cpp b/src/util/path.cpp index 3e0b8bdb2..9c9b5ca4e 100644 --- a/src/util/path.cpp +++ b/src/util/path.cpp @@ -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)); } } diff --git a/src/util/path.hpp b/src/util/path.hpp index 476fbcc1d..1dc242980 100644 --- a/src/util/path.hpp +++ b/src/util/path.hpp @@ -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. diff --git a/unittest/TestUtil.cpp b/unittest/TestUtil.cpp index 0f07d0dc6..362624ba9 100644 --- a/unittest/TestUtil.cpp +++ b/unittest/TestUtil.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #ifdef HAVE_UNISTD_H # include @@ -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"); diff --git a/unittest/main.cpp b/unittest/main.cpp index f2aab3ad4..c03a4f524 100644 --- a/unittest/main.cpp +++ b/unittest/main.cpp @@ -22,6 +22,7 @@ #include #include +#include #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); diff --git a/unittest/test_InodeCache.cpp b/unittest/test_InodeCache.cpp index 523a79304..5c2fc3ecc 100644 --- a/unittest/test_InodeCache.cpp +++ b/unittest/test_InodeCache.cpp @@ -20,11 +20,11 @@ #include "../src/Context.hpp" #include "../src/Hash.hpp" #include "../src/InodeCache.hpp" -#include "../src/Util.hpp" #include "TestUtil.hpp" #include #include +#include #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 diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 195e1caf9..f4ee3ecb3 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #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)); diff --git a/unittest/test_argprocessing.cpp b/unittest/test_argprocessing.cpp index dc7e935ea..19a485cca 100644 --- a/unittest/test_argprocessing.cpp +++ b/unittest/test_argprocessing.cpp @@ -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 #include #include +#include #include #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; diff --git a/unittest/test_ccache.cpp b/unittest/test_ccache.cpp index 8e1f510e1..bb4bf8860 100644 --- a/unittest/test_ccache.cpp +++ b/unittest/test_ccache.cpp @@ -21,9 +21,9 @@ #include "../src/fmtmacros.hpp" #include "TestUtil.hpp" -#include #include #include +#include #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); diff --git a/unittest/test_util_path.cpp b/unittest/test_util_path.cpp index 8bb703dff..307a03db3 100644 --- a/unittest/test_util_path.cpp +++ b/unittest/test_util_path.cpp @@ -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