From: Joel Rosdahl Date: Sun, 31 Dec 2023 14:47:06 +0000 (+0100) Subject: refactor: Convert usage of util::real_path to std::filesystem X-Git-Tag: v4.10~140 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d3d8310e766c2e5239e7418dcf698956c58bf091;p=thirdparty%2Fccache.git refactor: Convert usage of util::real_path to std::filesystem --- diff --git a/src/Config.cpp b/src/Config.cpp index 0e9e641fe..55c44b292 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -34,7 +34,6 @@ #include #include #include -#include #include #include @@ -913,9 +912,9 @@ Config::set_value_in_file(const std::string& path, Config dummy_config; dummy_config.set_item(key, value, std::nullopt, false, ""); - const auto resolved_path = util::real_path(path); - if (!DirEntry(resolved_path).is_regular_file()) { - core::ensure_dir_exists(Util::dir_name(resolved_path)); + const fs::path resolved_path = fs::canonical(path).value_or(path); + if (!fs::is_regular_file(resolved_path)) { + core::ensure_dir_exists(resolved_path.parent_path()); util::throw_on_error( util::write_file(resolved_path, ""), FMT("failed to write to {}: ", resolved_path)); diff --git a/src/Util.cpp b/src/Util.cpp index c93e760cb..12e16a8b8 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2023 Joel Rosdahl and other contributors +// Copyright (C) 2019-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -234,7 +234,7 @@ make_relative_path(const std::string& base_dir, dir_entry = DirEntry(path); } const auto path_suffix = std::string(original_path.substr(path.length())); - const auto real_path = util::real_path(path); + const std::string real_path = fs::canonical(path).value_or(path).string(); const auto add_relpath_candidates = [&](auto p) { const std::string normalized_path = diff --git a/src/ccache.cpp b/src/ccache.cpp index 6cb38b98a..37c4122f1 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1545,7 +1545,8 @@ hash_common_info(const Context& ctx, if (!ctx.args_info.profile_path.empty()) { dir = ctx.args_info.profile_path; } else { - dir = util::real_path(Util::dir_name(ctx.args_info.output_obj)); + const auto output_dir = fs::path(ctx.args_info.output_obj).parent_path(); + dir = fs::canonical(output_dir).value_or(output_dir).string(); } std::string_view stem = Util::remove_extension( fs::path(ctx.args_info.output_obj).filename().string()); diff --git a/src/core/common.cpp b/src/core/common.cpp index 63a214346..1a69f815b 100644 --- a/src/core/common.cpp +++ b/src/core/common.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2023 Joel Rosdahl and other contributors +// Copyright (C) 2023-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -103,14 +103,10 @@ rewrite_stderr_to_absolute_paths(std::string_view text) if (path_end == std::string_view::npos) { result.append(line.data(), line.length()); } else { - std::string path(line.substr(0, path_end)); - if (util::DirEntry(path)) { - result += util::real_path(path); - auto tail = line.substr(path_end); - result.append(tail.data(), tail.length()); - } else { - result.append(line.data(), line.length()); - } + fs::path path(line.substr(0, path_end)); + result += fs::canonical(path).value_or(path).string(); + auto tail = line.substr(path_end); + result.append(tail.data(), tail.length()); } } return result; diff --git a/src/util/path.cpp b/src/util/path.cpp index 412c82c06..bf2845c9d 100644 --- a/src/util/path.cpp +++ b/src/util/path.cpp @@ -120,11 +120,4 @@ path_starts_with(std::string_view path, std::string_view prefix) return true; } -std::string -real_path(std::string_view path) -{ - auto real_path = fs::canonical(path); - return real_path ? real_path->string() : std::string(path); -} - } // namespace util diff --git a/src/util/path.hpp b/src/util/path.hpp index 5a29a2d6f..a6fc7ab9e 100644 --- a/src/util/path.hpp +++ b/src/util/path.hpp @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -54,10 +55,6 @@ bool is_full_path(std::string_view path); // Windows bool path_starts_with(std::string_view path, std::string_view prefix); -// Return a normalized absolute path of `path`. On error (e.g. if the `path` -// doesn't exist) path is returned unmodified. -std::string real_path(std::string_view path); - // --- Inline implementations --- inline bool