diff mbox series

[v2] cpio_utils: Ensure all bytes are extracted from stream

Message ID SN6PR06MB4541AF839A007ECCF3C29E7B92C69@SN6PR06MB4541.namprd06.prod.outlook.com
State Accepted
Delegated to: Stefano Babic
Headers show
Series [v2] cpio_utils: Ensure all bytes are extracted from stream | expand

Commit Message

Jayasinghe, Dushara Jan. 17, 2023, 10:33 p.m. UTC
From 1108d73fb6264c1956ad550ca29c3a0e00e5e8fd Mon Sep 17 00:00:00 2001
From: Dushara Jayasinghe <dushara.jayasinghe@carrier.com>
Date: Mon, 16 Jan 2023 16:30:53 +1100
Subject: [PATCH v2] cpio_utils: Ensure all bytes are extracted from stream

In scenarios where the image is streamed over an interface that
provides data in chunks smaller than 512 bytes, more than one
read operation will be required to extract all the padding data.

As it stands, the reader closes the interface after a single read
causing a broken pipe error at the writers end. Consequently the
writer (e.g. swupdate_async_thread) registers an update failure
while swupdate itself considers the update to be successful.

Signed-off-by: Dushara Jayasinghe <dushara.jayasinghe@carrier.com>
---
 core/cpio_utils.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/core/cpio_utils.c b/core/cpio_utils.c
index a31f254..c282671 100644
--- a/core/cpio_utils.c
+++ b/core/cpio_utils.c
@@ -104,14 +104,19 @@  void extract_padding(int fd, unsigned long *offset)
         return;
 
     padding = (512 - (*offset % 512)) % 512;
-    if (padding) {
-        TRACE("Expecting %d padding bytes at end-of-file", padding);
-        len = read(fd, buf, padding);
-        if (len < 0) {
-            DEBUG("Failure while reading padding %d: %s", fd, strerror(errno));
-            return;
-        }
-    }
+	len = 0;
+	do {
+		if (padding) {
+			TRACE("Expecting %d padding bytes at end-of-file", padding);
+			len = read(fd, buf, padding);
+			if (len < 0) {
+				DEBUG("Failure while reading padding %d: %s", fd, strerror(errno));
+				return;
+			}
+			TRACE("Extracted %d bytes", len);
+			padding -= len;
+		}
+	} while(len > 0);
 
     return;
 }