]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Use fs::read_symlink instead of custom implementation
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 11 Jul 2023 06:07:28 +0000 (08:07 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 12 Jul 2023 09:07:58 +0000 (11:07 +0200)
src/Util.cpp
src/Util.hpp
src/ccache.cpp
src/util/LockFile.cpp

index 61992478f0f1df3ff5fcc41e328c98bf606f32cd..6e3083c46ec60185dd2dceac1505381e992ae696 100644 (file)
@@ -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<char[]> 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)
 {
index 7026cfccb4ad1b64c3bce543d79b5c523085410f..42cea7a138302ad996c8a0f9769c586346b4040c 100644 (file)
@@ -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.
index 18338d0501a34e719e3e8a7413895638194e57f6..351007708130a0123ebd195d319c4efaa105c1cc 100644 (file)
@@ -64,6 +64,7 @@
 
 #include <fcntl.h>
 
+#include <filesystem>
 #include <optional>
 #include <string_view>
 
@@ -78,6 +79,8 @@
 #include <memory>
 #include <unordered_map>
 
+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
index 43ed32cd2885391e41d81b9b8378be3386ded99f..03346986853a93b59e1babbadce19877fd70c7b5 100644 (file)
@@ -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;
       }
     }