aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/util
diff options
context:
space:
mode:
authorGravatar laszlocsomor <laszlocsomor@google.com>2017-03-30 12:38:05 +0000
committerGravatar Philipp Wollermann <philwo@google.com>2017-03-31 17:07:37 +0200
commit111b3ac7ef616664890b733f7a0205adf40d3268 (patch)
tree2ae692178531f681c0a47ca0fc6705ec74959e5f /src/main/cpp/util
parent6127358c1799d8d83cebbd499edac89f869df41b (diff)
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
Diffstat (limited to 'src/main/cpp/util')
-rw-r--r--src/main/cpp/util/file_platform.h5
-rw-r--r--src/main/cpp/util/file_posix.cc12
-rw-r--r--src/main/cpp/util/file_windows.cc15
3 files changed, 32 insertions, 0 deletions
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<WCHAR[]>* 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<WCHAR[]> 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;