From: Joel Rosdahl Date: Tue, 11 Jul 2023 06:07:28 +0000 (+0200) Subject: refactor: Use fs::read_symlink instead of custom implementation X-Git-Tag: v4.9~137 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fe3aa934f42d07c68110b5fa573ecfbd25234b83;p=thirdparty%2Fccache.git refactor: Use fs::read_symlink instead of custom implementation --- diff --git a/src/Util.cpp b/src/Util.cpp index 61992478f..6e3083c46 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -737,21 +737,6 @@ parse_duration(std::string_view duration) } } -#ifndef _WIN32 -std::string -read_link(const std::string& path) -{ - size_t buffer_size = path_max(path); - std::unique_ptr buffer(new char[buffer_size]); - const auto len = readlink(path.c_str(), buffer.get(), buffer_size - 1); - if (len == -1) { - return ""; - } - buffer[len] = 0; - return buffer.get(); -} -#endif - std::string real_path(const std::string& path, bool return_empty_on_error) { diff --git a/src/Util.hpp b/src/Util.hpp index 7026cfccb..42cea7a13 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -179,11 +179,6 @@ std::string normalize_concrete_absolute_path(const std::string& path); // into seconds. Throws `core::Error` on error. uint64_t parse_duration(std::string_view duration); -#ifndef _WIN32 -// Like readlink(2) but returns the string (or the empty string on failure). -std::string read_link(const std::string& path); -#endif - // Return a normalized absolute path of `path`. On error (e.g. if the `path` // doesn't exist) the empty string is returned if return_empty_on_error is true, // otherwise `path` unmodified. diff --git a/src/ccache.cpp b/src/ccache.cpp index 18338d050..351007708 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -64,6 +64,7 @@ #include +#include #include #include @@ -78,6 +79,8 @@ #include #include +namespace fs = std::filesystem; + using core::Statistic; // This is a string that identifies the current "version" of the hash sum @@ -241,15 +244,17 @@ guess_compiler(std::string_view path) // Follow symlinks to the real compiler to learn its name. We're not using // Util::real_path in order to save some unnecessary stat calls. while (true) { - std::string symlink_value = Util::read_link(compiler_path); - if (symlink_value.empty()) { + std::error_code ec; + auto symlink_target = fs::read_symlink(compiler_path, ec); + if (ec) { + // Not a symlink. break; } - if (util::is_absolute_path(symlink_value)) { - compiler_path = symlink_value; + if (symlink_target.is_absolute()) { + compiler_path = symlink_target; } else { compiler_path = - FMT("{}/{}", Util::dir_name(compiler_path), symlink_value); + FMT("{}/{}", Util::dir_name(compiler_path), symlink_target.string()); } } #endif diff --git a/src/util/LockFile.cpp b/src/util/LockFile.cpp index 43ed32cd2..033469868 100644 --- a/src/util/LockFile.cpp +++ b/src/util/LockFile.cpp @@ -44,6 +44,8 @@ const double k_max_sleep_time = 0.050; const util::Duration k_staleness_limit(2); #endif +namespace fs = std::filesystem; + namespace { class RandomNumberGenerator @@ -260,14 +262,15 @@ LockFile::do_acquire(const bool blocking) return false; } - std::string content = Util::read_link(m_lock_file); - if (content.empty()) { - if (errno == ENOENT) { + std::error_code ec; + std::string content = fs::read_symlink(m_lock_file, ec); + if (ec) { + if (ec == std::errc::no_such_file_or_directory) { // The symlink was removed after the symlink() call above, so retry // acquiring it. continue; } else { - LOG("Could not read symlink {}: {}", m_lock_file, strerror(errno)); + LOG("Could not read symlink {}: {}", m_lock_file, ec.message()); return false; } }