Stout: Set `_fmode` to binary in `protobuf.hpp`.

In Windows, the I/O subsystem will automatically convert linefeed
endings if the I/O is done on "text" files. For binary files, this
effect is almost never desirable.

This commit will turn of these conversions for protobuf I/O operations.

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


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

Branch: refs/heads/master
Commit: e42db6b93c00c3b757db28f567c1c99c23b6cd81
Parents: 78e00e9
Author: Alex Clemmer <clemmer.alexan...@gmail.com>
Authored: Mon May 23 16:02:38 2016 -0700
Committer: Joris Van Remoortere <joris.van.remoort...@gmail.com>
Committed: Mon May 23 16:48:48 2016 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/stout/protobuf.hpp | 51 ++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/e42db6b9/3rdparty/stout/include/stout/protobuf.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/protobuf.hpp 
b/3rdparty/stout/include/stout/protobuf.hpp
index eb4ac8c..7bf704d 100644
--- a/3rdparty/stout/include/stout/protobuf.hpp
+++ b/3rdparty/stout/include/stout/protobuf.hpp
@@ -84,6 +84,16 @@ inline Try<Nothing> write(int fd, const 
google::protobuf::Message& message)
 }
 
 
+#ifdef __WINDOWS__
+// NOTE: Ordinarily this would go in a Windows-specific header; we put it here
+// to avoid complex forward declarations.
+inline Try<Nothing> write(HANDLE handle, const google::protobuf::Message& 
message)
+{
+  return write(_open_osfhandle(reinterpret_cast<intptr_t>(handle), O_WRONLY), 
message);
+}
+#endif // __WINDOWS__
+
+
 // Write out the given sequence of protobuf messages to the
 // specified file descriptor by repeatedly invoking write
 // on each of the messages.
@@ -107,9 +117,16 @@ Try<Nothing> write(
 template <typename T>
 Try<Nothing> write(const std::string& path, const T& t)
 {
+  int operation_flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC;
+#ifdef __WINDOWS__
+  // NOTE: Windows does automatic linefeed conversions in I/O on text files.
+  // We include the `_O_BINARY` flag here to avoid this.
+  operation_flags |= _O_BINARY;
+#endif // __WINDOWS__
+
   Try<int> fd = os::open(
       path,
-      O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
+      operation_flags,
       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
   if (fd.isError()) {
@@ -131,9 +148,16 @@ inline Try<Nothing> append(
     const std::string& path,
     const google::protobuf::Message& message)
 {
+  int operation_flags = O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC;
+#ifdef __WINDOWS__
+  // NOTE: Windows does automatic linefeed conversions in I/O on text files.
+  // We include the `_O_BINARY` flag here to avoid this.
+  operation_flags |= _O_BINARY;
+#endif // __WINDOWS__
+
   Try<int> fd = os::open(
       path,
-      O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC,
+      operation_flags,
       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
   if (fd.isError()) {
@@ -289,14 +313,35 @@ Result<T> read(int fd, bool ignorePartial = false, bool 
undoFailed = false)
 }
 
 
+#ifdef __WINDOWS__
+// NOTE: Ordinarily this would go in a Windows-specific header; we put it here
+// to avoid complex forward declarations.
+template <typename T>
+Result<T> read(HANDLE handle, bool ignorePartial = false, bool undoFailed = 
false)
+{
+  return read<T>(
+      _open_osfhandle(reinterpret_cast<intptr_t>(handle), O_RDONLY),
+      ignorePartial,
+      undoFailed);
+}
+#endif // __WINDOWS__
+
+
 // A wrapper function that wraps the above read() with open and
 // closing the file.
 template <typename T>
 Result<T> read(const std::string& path)
 {
+  int operation_flags = O_RDONLY | O_CLOEXEC;
+#ifdef __WINDOWS__
+  // NOTE: Windows does automatic linefeed conversions in I/O on text files.
+  // We include the `_O_BINARY` flag here to avoid this.
+  operation_flags |= _O_BINARY;
+#endif // __WINDOWS__
+
   Try<int> fd = os::open(
       path,
-      O_RDONLY | O_CLOEXEC,
+      operation_flags,
       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
   if (fd.isError()) {

Reply via email to