diff mbox series

[v2,1/4] cpio_utils: factor in copy_write_padded()

Message ID 20181214144130.18819-1-christian.storm@siemens.com
State Accepted
Headers show
Series [v2,1/4] cpio_utils: factor in copy_write_padded() | expand

Commit Message

Storm, Christian Dec. 14, 2018, 2:41 p.m. UTC
Shift copy_write_padded() from raw_handler.c to
cpio_utils.c so that other handlers may use it
as well, just like cpio_utils.c's copy_write().

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 core/cpio_utils.c      | 29 +++++++++++++++++++++++++++--
 handlers/raw_handler.c | 25 -------------------------
 include/util.h         |  3 +++
 3 files changed, 30 insertions(+), 27 deletions(-)
diff mbox series

Patch

diff --git a/core/cpio_utils.c b/core/cpio_utils.c
index 6216e6f..143474b 100644
--- a/core/cpio_utils.c
+++ b/core/cpio_utils.c
@@ -77,8 +77,8 @@  static int fill_buffer(int fd, unsigned char *buf, unsigned int nbytes, unsigned
 }
 
 /*
- * Export this to be used in other modules
- * It just copy a buffer to a file
+ * Export the copy_write{,_*} functions to be used in other modules
+ * for copying a buffer to a file.
  */
 int copy_write(void *out, const void *buf, unsigned int len)
 {
@@ -107,6 +107,31 @@  int copy_write(void *out, const void *buf, unsigned int len)
 	return 0;
 }
 
+#if defined(__FreeBSD__)
+/*
+ * FreeBSD likes to have multiples of 512 bytes written
+ * to a device node, hence slice the buffer in palatable
+ * chunks assuming that only the last written buffer's
+ * length is smaller than cpio_utils.c's CPIO_BUFFER_SIZE and
+ * doesn't satisfy length % 512 == 0.
+ */
+int copy_write_padded(void *out, const void *buf, unsigned int len)
+{
+	if (len % 512 == 0) {
+		return copy_write(out, buf, len);
+	}
+
+	uint8_t buffer[512] = { 0 };
+	int chunklen = len - (len % 512);
+	int res = copy_write(out, buf, chunklen);
+	if (res != 0) {
+		return res;
+	}
+	memcpy(&buffer, buf+chunklen, len-chunklen);
+	return copy_write(out, buffer, 512);
+}
+#endif
+
 /*
  * Pipeline description
  *
diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c
index 95bb9b1..913dce6 100644
--- a/handlers/raw_handler.c
+++ b/handlers/raw_handler.c
@@ -23,31 +23,6 @@ 
 void raw_handler(void);
 void raw_filecopy_handler(void);
 
-#if defined(__FreeBSD__)
-/*
- * FreeBSD likes to have multiples of 512 bytes written
- * to a device node, hence slice the buffer in palatable
- * chunks assuming that only the last written buffer's
- * length is smaller than cpio_utils.c's BUFF_SIZE and
- * doesn't satisfy length % 512 == 0.
- */
-static int copy_write_padded(void *out, const void *buf, unsigned int len)
-{
-	if (len % 512 == 0) {
-		return copy_write(out, buf, len);
-	}
-
-	uint8_t buffer[512] = { 0 };
-	int chunklen = len - (len % 512);
-	int res = copy_write(out, buf, chunklen);
-	if (res != 0) {
-		return res;
-	}
-	memcpy(&buffer, buf+chunklen, len-chunklen);
-	return copy_write(out, buffer, 512);
-}
-#endif
-
 static int install_raw_image(struct img_type *img,
 	void __attribute__ ((__unused__)) *data)
 {
diff --git a/include/util.h b/include/util.h
index f09795a..e48d652 100644
--- a/include/util.h
+++ b/include/util.h
@@ -156,6 +156,9 @@  typedef int (*writeimage) (void *out, const void *buf, unsigned int len);
 
 int openfile(const char *filename);
 int copy_write(void *out, const void *buf, unsigned int len);
+#if defined(__FreeBSD__)
+int copy_write_padded(void *out, const void *buf, unsigned int len);
+#endif
 int copyfile(int fdin, void *out, unsigned int nbytes, unsigned long *offs,
 	unsigned long long seek,
 	int skip_file, int compressed, uint32_t *checksum,