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)
{
// 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.
# 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, ...)
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
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);
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.
// 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"
#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"));