}
}
-#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)
{
// 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.
#include <fcntl.h>
+#include <filesystem>
#include <optional>
#include <string_view>
#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
// 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
const util::Duration k_staleness_limit(2);
#endif
+namespace fs = std::filesystem;
+
namespace {
class RandomNumberGenerator
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;
}
}