diff mbox series

[07/10] Factorize callback to transfer data via FIFO

Message ID 20241030101550.21014-8-stefano.babic@swupdate.org
State Changes Requested
Headers show
Series Introduce BTRFS Snapshot Handler | expand

Commit Message

Stefano Babic Oct. 30, 2024, 10:15 a.m. UTC
There is a common pattern: the copyimage() is called with a callback for
transferring the stream to a FIFO. Then factorize the function and let
that multiple handlers can use it.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 handlers/docker_handler.c  | 38 ++------------------------------------
 handlers/handler_helpers.c | 27 +++++++++++++++++++++++++++
 include/handler_helpers.h  | 10 ++++++++++
 3 files changed, 39 insertions(+), 36 deletions(-)

--
2.34.1
diff mbox series

Patch

diff --git a/handlers/docker_handler.c b/handlers/docker_handler.c
index ab4afd8d..1f690a40 100644
--- a/handlers/docker_handler.c
+++ b/handlers/docker_handler.c
@@ -27,41 +27,7 @@ 
 #include "parselib.h"
 #include "swupdate_image.h"
 #include "docker_interface.h"
-
-#define FIFO_THREAD_READ	0
-#define FIFO_HND_WRITE		1
-
-struct hnd_load_priv {
-	int fifo[2];	/* PIPE where to write */
-	size_t	totalbytes;
-	int exit_status;
-};
-
-/*
- * This is the copyimage's callback. When called,
- * there is a buffer to be passed to curl connections
- * This is part of the image load: using copyimage() the image is
- * transferred without copy to the daemon.
- */
-static int transfer_data(void *data, const void *buf, size_t len)
-{
-	struct hnd_load_priv *priv = (struct hnd_load_priv *)data;
-	ssize_t written;
-	unsigned int nbytes = len;
-	const void *tmp = buf;
-
-	while (nbytes) {
-		written = write(priv->fifo[FIFO_HND_WRITE], buf, len);
-		if (written < 0) {
-			ERROR ("Cannot write to fifo");
-			return -EFAULT;
-		}
-		nbytes -= written;
-		tmp += written;
-	}
-
-	return 0;
-}
+#include "handler_helpers.h"

 /*
  * Background threa dto transfer the image to the daemon.
@@ -125,7 +91,7 @@  static int docker_install_image(struct img_type *img,
 			goto handler_exit;
 	}

-	ret = copyimage(&priv, img, transfer_data);
+	ret = copyimage(&priv, img, handler_transfer_data);
 	if (ret) {
 		ERROR("Transferring SWU image was not successful");
 		ret = FAILURE;
diff --git a/handlers/handler_helpers.c b/handlers/handler_helpers.c
index 76f87da2..5861690a 100644
--- a/handlers/handler_helpers.c
+++ b/handlers/handler_helpers.c
@@ -19,6 +19,33 @@ 
 #include "pctl.h"
 #include "util.h"

+/*
+ * This is the copyimage's callback. When called,
+ * there is a buffer to be passed to curl connections
+ * This is part of the image load: using copyimage() the image is
+ * transferred without copy to the daemon.
+ */
+int handler_transfer_data(void *data, const void *buf, size_t len)
+{
+	struct hnd_load_priv *priv = (struct hnd_load_priv *)data;
+	ssize_t written;
+	unsigned int nbytes = len;
+	const void *tmp = buf;
+
+	while (nbytes) {
+		written = write(priv->fifo[FIFO_HND_WRITE], buf, len);
+		if (written < 0) {
+			ERROR ("Cannot write to fifo");
+			return -EFAULT;
+		}
+		nbytes -= written;
+		tmp += written;
+	}
+
+	return 0;
+}
+
+
 /*
  * Thread to start the chained handler.
  * This received from FIFO the reassembled stream with
diff --git a/include/handler_helpers.h b/include/handler_helpers.h
index fdd831dd..30d338eb 100644
--- a/include/handler_helpers.h
+++ b/include/handler_helpers.h
@@ -12,4 +12,14 @@  struct chain_handler_data {
 	struct img_type img;
 };

+#define FIFO_THREAD_READ	0
+#define FIFO_HND_WRITE		1
+
+struct hnd_load_priv {
+	int fifo[2];	/* PIPE where to write */
+	size_t	totalbytes;
+	int exit_status;
+};
+
 extern void *chain_handler_thread(void *data);
+extern int handler_transfer_data(void *data, const void *buf, size_t len);