From 484494b915bdaa612897bd8b4449c29be76f6137 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Tue, 28 Apr 2020 16:27:31 +0200 Subject: [PATCH] enhanced read to restart when interrupted, and to read until really done --- fdiobuf.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/fdiobuf.cpp b/fdiobuf.cpp index 1529b99..6a8e9e2 100644 --- a/fdiobuf.cpp +++ b/fdiobuf.cpp @@ -24,7 +24,24 @@ std::streambuf::int_type sc::io::fdiobuf::underflow() { } std::streamsize sc::io::fdiobuf::xsgetn(char* s, std::streamsize n) { - return ::read(_fd, s, n); + std::streamsize tot_read = 0; + std::streamsize next_amount = n; + while (next_amount) { + ssize_t num_read = ::read(_fd, s, next_amount); + if (num_read == -1) { + if (errno == EINTR) { // interrupted => restart + continue; + } else { + return -1; // real error + } + } else if (num_read == 0) { // EOF + break; + } else { + tot_read += num_read; + next_amount -= num_read; + } + } + return tot_read; } std::streambuf::int_type sc::io::fdiobuf::overflow(std::streambuf::int_type c) {