#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
,
}
}
-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)
{
// 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);
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()
{
return std::string(path);
} else {
return Util::normalize_abstract_absolute_path(
- FMT("{}/{}", Util::get_actual_cwd(), path));
+ FMT("{}/{}", actual_cwd(), path));
}
}
// --- 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.
#include <core/wincompat.hpp>
#include <fmtmacros.hpp>
#include <util/filesystem.hpp>
+#include <util/path.hpp>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
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");
#include <util/environment.hpp>
#include <util/filesystem.hpp>
+#include <util/path.hpp>
#include "third_party/fmt/core.h"
#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);
#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"
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);
}
{
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
#include <util/environment.hpp>
#include <util/file.hpp>
#include <util/filesystem.hpp>
+#include <util/path.hpp>
#include "third_party/doctest.h"
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;
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));
#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"
#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"
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
}
}
-// 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;
#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"
#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);
-// 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.
//
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);
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