Windows: Fixed `os::write()` to use `WriteFile()`.

This can eventually support overlapped I/O.

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


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

Branch: refs/heads/master
Commit: 5d1bbdd1b3ad92317226add8881ae63da010cd3b
Parents: bf585fa
Author: Andrew Schwartzmeyer <[email protected]>
Authored: Mon Mar 19 20:42:42 2018 -0700
Committer: Andrew Schwartzmeyer <[email protected]>
Committed: Tue May 1 18:36:04 2018 -0700

----------------------------------------------------------------------
 .../stout/include/stout/os/windows/write.hpp     | 19 ++++++++++++++-----
 3rdparty/stout/include/stout/os/write.hpp        | 12 ++++++++++--
 2 files changed, 24 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/5d1bbdd1/3rdparty/stout/include/stout/os/windows/write.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/windows/write.hpp 
b/3rdparty/stout/include/stout/os/windows/write.hpp
index 57660a3..982d084 100644
--- a/3rdparty/stout/include/stout/os/windows/write.hpp
+++ b/3rdparty/stout/include/stout/os/windows/write.hpp
@@ -13,12 +13,10 @@
 #ifndef __STOUT_OS_WINDOWS_WRITE_HPP__
 #define __STOUT_OS_WINDOWS_WRITE_HPP__
 
-#include <io.h>
-
 #include <stout/nothing.hpp>
 #include <stout/try.hpp>
 #include <stout/unreachable.hpp>
-#include <stout/windows.hpp> // For order-dependent networking headers.
+#include <stout/windows.hpp>
 
 #include <stout/os/int_fd.hpp>
 #include <stout/os/socket.hpp>
@@ -30,10 +28,21 @@ inline ssize_t write(const int_fd& fd, const void* data, 
size_t size)
   CHECK_LE(size, INT_MAX);
 
   switch (fd.type()) {
-    case WindowsFD::FD_CRT:
-    case WindowsFD::FD_HANDLE: {
+    // TODO(andschwa): Remove this when `FD_CRT` is removed, MESOS-8675.
+    case WindowsFD::FD_CRT: {
       return ::_write(fd.crt(), data, static_cast<unsigned int>(size));
     }
+    case WindowsFD::FD_HANDLE: {
+      DWORD bytes;
+      // TODO(andschwa): Handle overlapped I/O.
+      const BOOL result =
+        ::WriteFile(fd, data, static_cast<DWORD>(size), &bytes, nullptr);
+      if (result == FALSE) {
+        return -1; // Indicates an error, but we can't return a `WindowsError`.
+      }
+
+      return static_cast<ssize_t>(bytes);
+    }
     case WindowsFD::FD_SOCKET: {
       return ::send(fd, (const char*)data, static_cast<int>(size), 0);
     }

http://git-wip-us.apache.org/repos/asf/mesos/blob/5d1bbdd1/3rdparty/stout/include/stout/os/write.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/write.hpp 
b/3rdparty/stout/include/stout/os/write.hpp
index 9ff749f..cd35285 100644
--- a/3rdparty/stout/include/stout/os/write.hpp
+++ b/3rdparty/stout/include/stout/os/write.hpp
@@ -44,7 +44,11 @@ inline ssize_t write_impl(int_fd fd, const char* buffer, 
size_t count)
 
     if (length < 0) {
 #ifdef __WINDOWS__
-      int error = WSAGetLastError();
+      // NOTE: There is no actual difference between `WSAGetLastError` and
+      // `GetLastError`, the former is an alias for the latter. So we can
+      // simply use the former here for both `HANDLE` and `SOCKET` types of
+      // `int_fd`. See MESOS-8764.
+      int error = ::GetLastError();
 #else
       int error = errno;
 #endif // __WINDOWS__
@@ -95,7 +99,11 @@ inline Try<Nothing> write(int_fd fd, const std::string& 
message)
 {
   ssize_t result = signal_safe::write(fd, message);
   if (result < 0) {
+#ifdef __WINDOWS__
+    return WindowsError();
+#else
     return ErrnoError();
+#endif // __WINDOWS__
   }
 
   return Nothing();
@@ -112,7 +120,7 @@ inline Try<Nothing> write(const std::string& path, const 
std::string& message)
       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
   if (fd.isError()) {
-    return ErrnoError("Failed to open file '" + path + "'");
+    return Error(fd.error());
   }
 
   Try<Nothing> result = write(fd.get(), message);

Reply via email to