Make scoped_pipe able to read multiples of 1024 bytes
We were incorrectly handling the return pattern of:
read(...) = 1024
read(...) = 1024
read(...) = -1 (EWOULDBLOCK)
This means we have data, not no data. We were catching this in
log_indexer when it would get a digets of exactly 2048 bytes long.
Change-Id: I848512357ddb8ca48e16f9d5ed005d9181ef6691
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/util/scoped_pipe.cc b/aos/util/scoped_pipe.cc
index e2e0ca5..d64fad1 100644
--- a/aos/util/scoped_pipe.cc
+++ b/aos/util/scoped_pipe.cc
@@ -55,8 +55,8 @@
read(fd(), buffer->data() + buffer->size() - kBufferSize, kBufferSize);
if (result == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
- buffer->resize(original_size);
- return 0;
+ buffer->resize(original_size + read_bytes);
+ return read_bytes;
}
PLOG(FATAL) << "Error on reading pipe.";
} else if (result < kBufferSize) {
diff --git a/aos/util/scoped_pipe_test.cc b/aos/util/scoped_pipe_test.cc
index c71e272..0e024c2 100644
--- a/aos/util/scoped_pipe_test.cc
+++ b/aos/util/scoped_pipe_test.cc
@@ -44,6 +44,21 @@
}
}
+// Tests using string read/write methods on the ScopedPipe objects.
+TEST(ScopedPipeTest, StringPipe2048) {
+ ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();
+ std::string buffer;
+ ASSERT_EQ(0u, pipe.read->Read(&buffer))
+ << "Shouldn't get anything on empty read.";
+ ASSERT_TRUE(buffer.empty());
+
+ std::string a(2048, 'a');
+ pipe.write->Write(absl::Span<const uint8_t>(
+ reinterpret_cast<const uint8_t *>(a.data()), a.size()));
+ ASSERT_EQ(2048u, pipe.read->Read(&buffer));
+ ASSERT_EQ(a, buffer);
+}
+
// Tests that calling SetCloexec succeeds and does indeed set FD_CLOEXEC.
TEST(ScopedPipeTest, SetCloexec) {
ScopedPipe::PipePair pipe = ScopedPipe::MakePipe();