aboutsummaryrefslogtreecommitdiff
path: root/lib/util
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-15 14:48:29 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-15 14:49:58 +0200
commit868843fbf76dd67e804bc8a02121f5b56c560621 (patch)
tree0ccdd3a107c0e6f35175712d88c37de32be107e1 /lib/util
parent083fc146266a9eeb2dd407546c86c3bf725330c1 (diff)
Move function to padd a file with 0 bytes to libutil
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/util')
-rw-r--r--lib/util/padd_file.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/util/padd_file.c b/lib/util/padd_file.c
new file mode 100644
index 0000000..8598b8b
--- /dev/null
+++ b/lib/util/padd_file.c
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "util.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+
+int padd_file(int outfd, uint64_t size, size_t blocksize)
+{
+ size_t padd_sz = size % blocksize;
+ int status = -1;
+ uint8_t *buffer;
+ ssize_t ret;
+
+ if (padd_sz == 0)
+ return 0;
+
+ padd_sz = blocksize - padd_sz;
+
+ buffer = calloc(1, padd_sz);
+ if (buffer == NULL)
+ goto fail_errno;
+
+ ret = write_retry(outfd, buffer, padd_sz);
+
+ if (ret < 0)
+ goto fail_errno;
+
+ if ((size_t)ret < padd_sz)
+ goto fail_trunc;
+
+ status = 0;
+out:
+ free(buffer);
+ return status;
+fail_trunc:
+ fputs("padding output to block size: truncated write\n", stderr);
+ goto out;
+fail_errno:
+ perror("padding output file to block size");
+ goto out;
+}