[PATCH RISU v2] risu: Handle short reads when replaying a trace from stdin

Philippe Mathieu-Daudé posted 1 patch 3 days, 22 hours ago
risu.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
[PATCH RISU v2] risu: Handle short reads when replaying a trace from stdin
Posted by Philippe Mathieu-Daudé 3 days, 22 hours ago
With "-t -" the trace is read from a pipe, where read() may return
fewer bytes than requested, which was reported as an I/O error.
Loop until the whole record is read, retrying on EINTR.

Without zlib this is how a compressed trace has to be replayed:

  $ gunzip -c trace.gz | risu -t - image.bin

Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
---
v2: Declare variable in function prologue.
---
 risu.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/risu.c b/risu.c
index c28b4a5..db268f4 100644
--- a/risu.c
+++ b/risu.c
@@ -60,6 +60,7 @@ static sigjmp_buf jmpbuf;
 static RisuResult read_buffer(void *ptr, size_t bytes)
 {
     size_t res;
+    char *p;
 
     if (!trace) {
         return recv_data_pkt(comm_fd, ptr, bytes);
@@ -68,7 +69,19 @@ static RisuResult read_buffer(void *ptr, size_t bytes)
 #ifdef HAVE_ZLIB
     if (comm_fd == STDIN_FILENO) {
 #endif
-        res = read(comm_fd, ptr, bytes);
+        p = ptr;
+        res = 0;
+        while (res < bytes) {
+            ssize_t r = read(comm_fd, p + res, bytes - res);
+
+            if (r < 0 && errno == EINTR) {
+                continue;
+            }
+            if (r <= 0) {
+                break;
+            }
+            res += r;
+        }
 #ifdef HAVE_ZLIB
     } else {
         res = gzread(gz_trace_file, ptr, bytes);
-- 
2.55.0