]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Implement Util::read_link, replacing legacy x_readlink
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 11 Feb 2020 20:56:02 +0000 (21:56 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 15 Feb 2020 15:39:48 +0000 (16:39 +0100)
src/Util.cpp
src/Util.hpp
src/legacy_util.cpp
src/legacy_util.hpp
src/lockfile.cpp
unittest/test_lockfile.cpp

index 59024626c526f8f3a964242e7a0e17877b260c98..216559d769c8bf35bfd564bc29d0bf247d6db563 100644 (file)
@@ -281,6 +281,21 @@ read_file(const std::string& path)
                      std::istreambuf_iterator<char>());
 }
 
+#ifndef _WIN32
+std::string
+read_link(const std::string& path)
+{
+  size_t buffer_size = path_max(path.c_str());
+  std::unique_ptr<char[]> buffer(new char[buffer_size]);
+  ssize_t 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 1d70aadcffc0d4c70b49203f7bf61a2f8e117f4f..f4576bc4194123bbb0d617d991439565fac8a217 100644 (file)
@@ -189,6 +189,11 @@ int parse_int(const std::string& value);
 // Throws Error on error.
 std::string read_file(const std::string& path);
 
+#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 canonicalized 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 80832c20f7d8054a53028989f7acad4de5c85f17..48840a0734640770a18239a9677db2133c0d98c2 100644 (file)
 #  endif
 #endif
 
-static long
-path_max(const char* path)
-{
-#ifdef PATH_MAX
-  (void)path;
-  return PATH_MAX;
-#elif defined(MAXPATHLEN)
-  (void)path;
-  return MAXPATHLEN;
-#elif defined(_PC_PATH_MAX)
-  long maxlen = pathconf(path, _PC_PATH_MAX);
-  return maxlen >= 4096 ? maxlen : 4096;
-#endif
-}
-
 // Something went badly wrong!
 void
 fatal(const char* format, ...)
@@ -1008,23 +993,6 @@ x_try_unlink(const char* path)
   return do_x_unlink(path, false);
 }
 
-#ifndef _WIN32
-// Like readlink() but returns the string or NULL on failure. Caller frees.
-char*
-x_readlink(const char* path)
-{
-  long maxlen = path_max(path);
-  char* buf = static_cast<char*>(x_malloc(maxlen));
-  ssize_t len = readlink(path, buf, maxlen - 1);
-  if (len == -1) {
-    free(buf);
-    return NULL;
-  }
-  buf[len] = 0;
-  return buf;
-}
-#endif
-
 // Reads the content of a file. Size hint 0 means no hint. Returns true on
 // success, otherwise false.
 bool
index b21a257653ac8459065d75e77097185ac18efdf1..7a995390169959f9f2f5ca58d88a38ffbebf624a 100644 (file)
@@ -66,9 +66,6 @@ int x_rename(const char* oldpath, const char* newpath);
 int tmp_unlink(const char* path);
 int x_unlink(const char* path);
 int x_try_unlink(const char* path);
-#ifndef _WIN32
-char* x_readlink(const char* path);
-#endif
 bool read_file(const char* path, size_t size_hint, char** data, size_t* size);
 char* read_text_file(const char* path, size_t size_hint);
 char* subst_env_in_string(const char* str, char** errmsg);
index 390bb587ffacd87f60bb6cebeba37be145619410..6092e6ad4c235cb1a4448c470abe8184b5eb26f0 100644 (file)
@@ -123,8 +123,8 @@ lockfile_acquire(const char* path, unsigned staleness_limit)
       goto out;
     }
     free(content);
-    content = x_readlink(lockfile);
-    if (!content) {
+    content = x_strdup(Util::read_link(lockfile).c_str());
+    if (str_eq(content, "")) {
       if (errno == ENOENT) {
         // The symlink was removed after the symlink() call above, so retry
         // acquiring it.
index e389c954a414d9fbd9feb9a97635721ddb94298e..7ad4a2b7d379722608bf2fb9e23547e4cf620f0c 100644 (file)
@@ -19,6 +19,7 @@
 // This file contains tests for functions in lockfile.c.
 
 #include "../src/Stat.hpp"
+#include "../src/Util.hpp"
 #include "../src/legacy_util.hpp"
 #include "../src/lockfile.hpp"
 #include "framework.hpp"
@@ -61,7 +62,7 @@ TEST(lock_breaking)
 #if defined(_WIN32) || defined(__CYGWIN__)
   p = read_text_file("test.lock", 0);
 #else
-  p = x_readlink("test.lock");
+  p = x_strdup(Util::read_link("test.lock").c_str());
 #endif
   CHECK(p);
   CHECK(!str_eq(p, "foo"));