From 111b3ac7ef616664890b733f7a0205adf40d3268 Mon Sep 17 00:00:00 2001 From: laszlocsomor Date: Thu, 30 Mar 2017 12:38:05 +0000 Subject: Bazel client: simplify ReadDirectorySymlink This is a bugfixed version of https://cr.bazel.build/9520 which broke some Google-internal tests. This change allows removing duplicate code: ReadDirectorySymlink was implemented on Windows as a junction resolver, which is also implemented in file_windows.cc. Now it uses the JunctionResolver. RELNOTES: none PiperOrigin-RevId: 151691895 --- src/main/cpp/util/file_platform.h | 5 +++++ src/main/cpp/util/file_posix.cc | 12 ++++++++++++ src/main/cpp/util/file_windows.cc | 15 +++++++++++++++ 3 files changed, 32 insertions(+) (limited to 'src/main/cpp/util') diff --git a/src/main/cpp/util/file_platform.h b/src/main/cpp/util/file_platform.h index 50b75de3bb..31a0d1c24c 100644 --- a/src/main/cpp/util/file_platform.h +++ b/src/main/cpp/util/file_platform.h @@ -130,6 +130,11 @@ enum RenameDirectoryResult { // Returns one of the RenameDirectoryResult enum values. int RenameDirectory(const std::string &old_name, const std::string &new_name); +// Reads which directory a symlink points to. Puts the target of the symlink +// in ``result`` and returns if the operation was successful. Will not work on +// symlinks that don't point to directories on Windows. +bool ReadDirectorySymlink(const std::string &symlink, std::string *result); + // Unlinks the file given by 'file_path'. // Returns true on success. In case of failure sets errno. bool UnlinkPath(const std::string &file_path); diff --git a/src/main/cpp/util/file_posix.cc b/src/main/cpp/util/file_posix.cc index 076bff2bfd..daa465af94 100644 --- a/src/main/cpp/util/file_posix.cc +++ b/src/main/cpp/util/file_posix.cc @@ -251,6 +251,18 @@ int RenameDirectory(const std::string &old_name, const std::string &new_name) { } } +bool ReadDirectorySymlink(const string &name, string *result) { + char buf[PATH_MAX + 1]; + int len = readlink(name.c_str(), buf, PATH_MAX); + if (len < 0) { + return false; + } + + buf[len] = 0; + *result = buf; + return true; +} + bool UnlinkPath(const string &file_path) { return unlink(file_path.c_str()) == 0; } diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc index c2e0fd79ee..af6ed4f325 100644 --- a/src/main/cpp/util/file_windows.cc +++ b/src/main/cpp/util/file_windows.cc @@ -863,6 +863,21 @@ bool JunctionResolver::Resolve(const WCHAR* path, unique_ptr* result) { return Resolve(path, result, kMaximumJunctionDepth); } +bool ReadDirectorySymlink(const string& name, string* result) { + wstring wname; + if (!AsWindowsPathWithUncPrefix(name, &wname)) { + PrintError("ReadDirectorySymlink: AsWindowsPathWithUncPrefix(%s)", + name.c_str()); + return false; + } + unique_ptr result_ptr; + if (!JunctionResolver().Resolve(wname.c_str(), &result_ptr)) { + return false; + } + *result = WstringToCstring(RemoveUncPrefixMaybe(result_ptr.get())).get(); + return true; +} + bool PathExists(const string& path) { if (path.empty()) { return false; -- cgit v1.2.3