You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by an...@apache.org on 2017/11/30 03:28:27 UTC

[08/13] mesos git commit: Windows: Fixed `os::realpath` to behave like POSIX version.

Windows: Fixed `os::realpath` to behave like POSIX version.

The existing implementation (1) did not resolve symlinks and (2) did not
check for existence of the path. This resulted in unexpected behaviors
by users of the function.

Review: https://reviews.apache.org/r/63812


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/4f015587
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/4f015587
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/4f015587

Branch: refs/heads/master
Commit: 4f01558778e27ec1687fa8d336c0d8bef2372109
Parents: f122989
Author: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Authored: Wed Nov 1 12:46:03 2017 -0700
Committer: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Committed: Wed Nov 29 19:24:11 2017 -0800

----------------------------------------------------------------------
 .../stout/include/stout/os/windows/realpath.hpp | 21 +++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/4f015587/3rdparty/stout/include/stout/os/windows/realpath.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/windows/realpath.hpp b/3rdparty/stout/include/stout/os/windows/realpath.hpp
index 81a33bd..c6bad50 100644
--- a/3rdparty/stout/include/stout/os/windows/realpath.hpp
+++ b/3rdparty/stout/include/stout/os/windows/realpath.hpp
@@ -18,21 +18,28 @@
 #include <stout/result.hpp>
 #include <stout/stringify.hpp>
 #include <stout/strings.hpp>
+#include <stout/windows.hpp>
 
 #include <stout/internal/windows/longpath.hpp>
+#include <stout/internal/windows/reparsepoint.hpp>
 
 
 namespace os {
 
+// This should behave like the POSIX `realpath` API: specifically it should
+// resolve symlinks in the path, and succeed only if the target file exists.
+// This requires that the user has permissions to resolve each component of the
+// path.
 inline Result<std::string> realpath(const std::string& path)
 {
-  // TODO(andschwa): Test the existence of `path` to be consistent with POSIX
-  // `::realpath`.
-
-  std::wstring longpath = ::internal::windows::longpath(path);
+  const Try<SharedHandle> handle = ::internal::windows::get_handle_follow(path);
+  if (handle.isError()) {
+    return Error(handle.error());
+  }
 
   // First query for the buffer size required.
-  DWORD length = GetFullPathNameW(longpath.data(), 0, nullptr, nullptr);
+  DWORD length = ::GetFinalPathNameByHandleW(
+      handle.get().get_handle(), nullptr, 0, FILE_NAME_NORMALIZED);
   if (length == 0) {
     return WindowsError("Failed to retrieve realpath buffer size");
   }
@@ -40,8 +47,8 @@ inline Result<std::string> realpath(const std::string& path)
   std::vector<wchar_t> buffer;
   buffer.reserve(static_cast<size_t>(length));
 
-  DWORD result =
-    GetFullPathNameW(longpath.data(), length, buffer.data(), nullptr);
+  DWORD result = ::GetFinalPathNameByHandleW(
+      handle.get().get_handle(), buffer.data(), length, FILE_NAME_NORMALIZED);
 
   if (result == 0) {
     return WindowsError("Failed to determine realpath");