You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2014/04/03 23:38:37 UTC

git commit: Updated os::read(fd, size) to not seek.

Repository: mesos
Updated Branches:
  refs/heads/master ddf202dd9 -> 26e4df5ce


Updated os::read(fd, size) to not seek.

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


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

Branch: refs/heads/master
Commit: 26e4df5ce432a44712317b3b79fecc770ab1e6fe
Parents: ddf202d
Author: Benjamin Hindman <be...@gmail.com>
Authored: Wed Apr 2 22:27:06 2014 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Thu Apr 3 11:12:50 2014 -0700

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/os/read.hpp    | 26 ++++++++---------
 .../3rdparty/stout/include/stout/protobuf.hpp   | 30 +++++++++++++-------
 2 files changed, 31 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/26e4df5c/3rdparty/libprocess/3rdparty/stout/include/stout/os/read.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/read.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/read.hpp
index d6a124e..53c0c32 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/os/read.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/read.hpp
@@ -23,16 +23,10 @@
 namespace os {
 
 // Reads 'size' bytes from a file from its current offset.
-// If EOF is encountered before reading size bytes, then the offset
-// is restored and none is returned.
+// If EOF is encountered before reading 'size' bytes then the result
+// will contain the bytes read and a subsequent read will return None.
 inline Result<std::string> read(int fd, size_t size)
 {
-  // Save the current offset.
-  off_t current = lseek(fd, 0, SEEK_CUR);
-  if (current == -1) {
-    return ErrnoError("Failed to lseek to SEEK_CUR");
-  }
-
   char* buffer = new char[size];
   size_t offset = 0;
 
@@ -44,13 +38,17 @@ inline Result<std::string> read(int fd, size_t size)
       if (errno == EINTR) {
         continue;
       }
-      // Attempt to restore the original offset.
-      lseek(fd, current, SEEK_SET);
+      ErrnoError error; // Constructed before 'delete' to capture errno.
       delete[] buffer;
-      return ErrnoError();
+      return error;
     } else if (length == 0) {
-      // Reached EOF before expected! Restore the offset.
-      lseek(fd, current, SEEK_SET);
+      // Reached EOF before expected! Only return as much data as
+      // available or None if we haven't read anything yet.
+      if (offset > 0) {
+        std::string result(buffer, offset);
+        delete[] buffer;
+        return result;
+      }
       delete[] buffer;
       return None();
     }
@@ -58,7 +56,7 @@ inline Result<std::string> read(int fd, size_t size)
     offset += length;
   }
 
-  std::string result = std::string(buffer, size);
+  std::string result(buffer, size);
   delete[] buffer;
   return result;
 }

http://git-wip-us.apache.org/repos/asf/mesos/blob/26e4df5c/3rdparty/libprocess/3rdparty/stout/include/stout/protobuf.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/protobuf.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/protobuf.hpp
index 939cb72..36b7f1a 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/protobuf.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/protobuf.hpp
@@ -52,7 +52,7 @@ inline Try<Nothing> write(int fd, const google::protobuf::Message& message)
 
   // First write the size of the protobuf.
   uint32_t size = message.ByteSize();
-  std::string bytes = std::string((char*) &size, sizeof(size));
+  std::string bytes((char*) &size, sizeof(size));
 
   Try<Nothing> result = os::write(fd, bytes);
   if (result.isError()) {
@@ -108,10 +108,19 @@ inline Result<T> read(int fd, bool ignorePartial = false)
   uint32_t size;
   Result<std::string> result = os::read(fd, sizeof(size));
 
-  if (result.isNone()) {
-    return None(); // No more protobufs to read.
-  } else if (result.isError()) {
+  if (result.isError()) {
+    lseek(fd, offset, SEEK_SET);
     return Error("Failed to read size: " + result.error());
+  } else if (result.isNone()) {
+    return None(); // No more protobufs to read.
+  } else if (result.get().size() < sizeof(size)) {
+    // Hit EOF unexpectedly. Restore the offset to before the size read.
+    lseek(fd, offset, SEEK_SET);
+    if (ignorePartial) {
+      return None();
+    }
+    return Error(
+        "Failed to read size: hit EOF unexpectedly, possible corruption");
   }
 
   // Parse the size from the bytes.
@@ -122,7 +131,11 @@ inline Result<T> read(int fd, bool ignorePartial = false)
   // corruption.
   result = os::read(fd, size);
 
-  if (result.isNone()) {
+  if (result.isError()) {
+    // Restore the offset to before the size read.
+    lseek(fd, offset, SEEK_SET);
+    return Error("Failed to read message: " + result.error());
+  } else if (result.isNone() || result.get().size() < size) {
     // Hit EOF unexpectedly. Restore the offset to before the size read.
     lseek(fd, offset, SEEK_SET);
     if (ignorePartial) {
@@ -130,10 +143,6 @@ inline Result<T> read(int fd, bool ignorePartial = false)
     }
     return Error("Failed to read message of size " + stringify(size) +
                  " bytes: hit EOF unexpectedly, possible corruption");
-  } else if (result.isError()) {
-    // Restore the offset to before the size read.
-    lseek(fd, offset, SEEK_SET);
-    return Error("Failed to read message: " + result.error());
   }
 
   // Parse the protobuf from the string.
@@ -142,8 +151,7 @@ inline Result<T> read(int fd, bool ignorePartial = false)
   const std::string& data = result.get();
 
   T message;
-  google::protobuf::io::ArrayInputStream stream(
-      data.data(), data.size());
+  google::protobuf::io::ArrayInputStream stream(data.data(), data.size());
 
   if (!message.ParseFromZeroCopyStream(&stream)) {
     // Restore the offset to before the size read.