From 868843fbf76dd67e804bc8a02121f5b56c560621 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 15 Jun 2019 14:48:29 +0200 Subject: Move function to padd a file with 0 bytes to libutil Signed-off-by: David Oberhollenzer --- lib/Makemodule.am | 2 +- lib/util/padd_file.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/util/padd_file.c (limited to 'lib') diff --git a/lib/Makemodule.am b/lib/Makemodule.am index ddc78f6..a676291 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -30,7 +30,7 @@ libutil_a_SOURCES = lib/util/canonicalize_name.c lib/util/write_retry.c libutil_a_SOURCES += lib/util/read_retry.c include/util.h libutil_a_SOURCES += lib/util/print_version.c lib/util/mkdir_p.c libutil_a_SOURCES += lib/util/str_table.c include/str_table.h -libutil_a_SOURCES += lib/util/dirstack.c +libutil_a_SOURCES += lib/util/dirstack.c lib/util/padd_file.c if WITH_GZIP libcompress_a_SOURCES += lib/comp/gzip.c 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 +#include + +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; +} -- cgit v1.2.3