From: Joel Rosdahl Date: Mon, 23 Oct 2023 18:55:09 +0000 (+0200) Subject: refactor: Use fs::weakly_canonical in prepare_debug_path X-Git-Tag: v4.9~31 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d96b24a9162834ca1ff9dbc2b35c228a76acb8fd;p=thirdparty%2Fccache.git refactor: Use fs::weakly_canonical in prepare_debug_path --- diff --git a/src/Config.cpp b/src/Config.cpp index 74aee464d..064e09822 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -788,7 +788,7 @@ Config::get_string_value(const std::string& key) const return format_bool(m_debug); case ConfigItem::debug_dir: - return m_debug_dir; + return m_debug_dir.string(); case ConfigItem::debug_level: return FMT("{}", m_debug_level); diff --git a/src/Config.hpp b/src/Config.hpp index 75bd9c2a8..253865a38 100644 --- a/src/Config.hpp +++ b/src/Config.hpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -62,7 +63,7 @@ public: int8_t compression_level() const; const std::string& cpp_extension() const; bool debug() const; - const std::string& debug_dir() const; + const std::filesystem::path& debug_dir() const; uint8_t debug_level() const; bool depend_mode() const; bool direct_mode() const; @@ -177,7 +178,7 @@ private: int8_t m_compression_level = 0; // Use default level std::string m_cpp_extension; bool m_debug = false; - std::string m_debug_dir; + std::filesystem::path m_debug_dir; uint8_t m_debug_level = 2; bool m_depend_mode = false; bool m_direct_mode = true; @@ -300,7 +301,7 @@ Config::debug() const return m_debug; } -inline const std::string& +inline const std::filesystem::path& Config::debug_dir() const { return m_debug_dir; diff --git a/src/ccache.cpp b/src/ccache.cpp index 71eefb409..7d414f316 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -183,19 +183,25 @@ add_prefix(const Context& ctx, Args& args, const std::string& prefix_command) } static std::string -prepare_debug_path(const std::string& debug_dir, +prepare_debug_path(const fs::path& cwd, + const fs::path& debug_dir, const util::TimePoint& time_of_invocation, - const std::string& output_obj, + const fs::path& output_obj, std::string_view suffix) { - auto prefix = debug_dir.empty() - ? output_obj - : debug_dir + util::to_absolute_path_no_drive(output_obj); + auto prefix = + debug_dir.empty() + ? output_obj + : (debug_dir + / (output_obj.is_absolute() + ? output_obj + : fs::weakly_canonical(cwd / output_obj).value_or(output_obj)) + .relative_path()); // Ignore any error from fs::create_directories since we can't handle an error // in another way in this context. The caller takes care of logging when // trying to open the path for writing. - fs::create_directories(Util::dir_name(prefix)); + fs::create_directories(prefix.parent_path()); char timestamp[100]; const auto tm = util::localtime(time_of_invocation); @@ -208,7 +214,7 @@ prepare_debug_path(const std::string& debug_dir, static_cast(time_of_invocation.sec())); } return FMT("{}.{}_{:06}.ccache-{}", - prefix, + prefix.string(), timestamp, time_of_invocation.nsec_decimal_part() / 1000, suffix); @@ -225,7 +231,8 @@ init_hash_debug(Context& ctx, return; } - const auto path = prepare_debug_path(ctx.config.debug_dir(), + const auto path = prepare_debug_path(ctx.apparent_cwd, + ctx.config.debug_dir(), ctx.time_of_invocation, ctx.args_info.output_obj, FMT("input-{}", type)); @@ -2304,7 +2311,8 @@ finalize_at_exit(Context& ctx) // Dump log buffer last to not lose any logs. if (ctx.config.debug() && !ctx.args_info.output_obj.empty()) { - util::logging::dump_log(prepare_debug_path(ctx.config.debug_dir(), + util::logging::dump_log(prepare_debug_path(ctx.apparent_cwd, + ctx.config.debug_dir(), ctx.time_of_invocation, ctx.args_info.output_obj, "log")); @@ -2506,7 +2514,8 @@ do_cache_compilation(Context& ctx) MTR_META_THREAD_NAME(ctx.args_info.output_obj.c_str()); if (ctx.config.debug() && ctx.config.debug_level() >= 2) { - const auto path = prepare_debug_path(ctx.config.debug_dir(), + const auto path = prepare_debug_path(ctx.apparent_cwd, + ctx.config.debug_dir(), ctx.time_of_invocation, ctx.args_info.orig_output_obj, "input-text"); diff --git a/src/util/filesystem.hpp b/src/util/filesystem.hpp index edea2c66d..a7bd25772 100644 --- a/src/util/filesystem.hpp +++ b/src/util/filesystem.hpp @@ -114,6 +114,7 @@ DEF_WRAP_1_R(read_symlink, path, const path&, p) DEF_WRAP_1_R(remove, bool, const path&, p) DEF_WRAP_1_R(remove_all, std::uintmax_t, const path&, p) DEF_WRAP_0_R(temp_directory_path, path) +DEF_WRAP_1_R(weakly_canonical, path, const path&, p) // clang-format on diff --git a/src/util/path.cpp b/src/util/path.cpp index 3473cdd01..bfa955aa0 100644 --- a/src/util/path.cpp +++ b/src/util/path.cpp @@ -139,27 +139,4 @@ real_path(std::string_view path) return real_path ? real_path->string() : std::string(path); } -std::string -to_absolute_path(std::string_view path) -{ - if (is_absolute_path(path)) { - return std::string(path); - } else { - return Util::normalize_abstract_absolute_path( - FMT("{}/{}", actual_cwd(), path)); - } -} - -std::string -to_absolute_path_no_drive(std::string_view path) -{ - std::string abs_path = to_absolute_path(path); -#ifdef _WIN32 - if (abs_path.length() >= 2 && abs_path[1] == ':') { - abs_path.erase(0, 2); - } -#endif - return abs_path; -} - } // namespace util diff --git a/src/util/path.hpp b/src/util/path.hpp index 6d43135e3..68c120708 100644 --- a/src/util/path.hpp +++ b/src/util/path.hpp @@ -61,12 +61,6 @@ bool path_starts_with(std::string_view path, std::string_view prefix); // doesn't exist) path is returned unmodified. std::string real_path(std::string_view path); -// Make `path` an absolute path. -std::string to_absolute_path(std::string_view path); - -// Make `path` an absolute path, but do not include Windows drive. -std::string to_absolute_path_no_drive(std::string_view path); - // --- Inline implementations --- inline bool diff --git a/unittest/test_util_path.cpp b/unittest/test_util_path.cpp index 1e6ff03c4..f52a50f37 100644 --- a/unittest/test_util_path.cpp +++ b/unittest/test_util_path.cpp @@ -74,45 +74,6 @@ TEST_CASE("util::is_dev_null_path") #endif } -TEST_CASE("util::to_absolute_path") -{ - CHECK(util::to_absolute_path("/foo/bar") == "/foo/bar"); - -#ifdef _WIN32 - CHECK(util::to_absolute_path("C:\\foo\\bar") == "C:\\foo\\bar"); -#endif - - const auto cwd = util::actual_cwd(); - - CHECK(util::to_absolute_path("") == cwd); - CHECK(util::to_absolute_path(".") == cwd); - CHECK(util::to_absolute_path("..") == Util::dir_name(cwd)); - CHECK(util::to_absolute_path("foo") == FMT("{}/foo", cwd)); - CHECK(util::to_absolute_path("../foo/bar") - == FMT("{}/foo/bar", Util::dir_name(cwd))); -} - -TEST_CASE("util::to_absolute_path_no_drive") -{ - CHECK(util::to_absolute_path_no_drive("/foo/bar") == "/foo/bar"); - -#ifdef _WIN32 - CHECK(util::to_absolute_path_no_drive("C:\\foo\\bar") == "\\foo\\bar"); -#endif - - auto cwd = util::actual_cwd(); -#ifdef _WIN32 - cwd = cwd.substr(2); -#endif - - CHECK(util::to_absolute_path_no_drive("") == cwd); - CHECK(util::to_absolute_path_no_drive(".") == cwd); - CHECK(util::to_absolute_path_no_drive("..") == Util::dir_name(cwd)); - CHECK(util::to_absolute_path_no_drive("foo") == FMT("{}/foo", cwd)); - CHECK(util::to_absolute_path_no_drive("../foo/bar") - == FMT("{}/foo/bar", Util::dir_name(cwd))); -} - TEST_CASE("util::path_starts_with") { CHECK(!util::path_starts_with("", ""));