diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-07-16 19:29:27 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-07-16 22:47:59 +0200 |
commit | bfd876dbf151df164b4d87de20aec39b24f205f9 (patch) | |
tree | 53ac348da6c430b1ecf5f546ee4758ac73cb16e0 /lib/util | |
parent | 74a064d9f15fa2d534e8b2d1ade68d10d5b65dfd (diff) |
cleanup: move error handling into write_retry
If write_retry fails to write everything, it is *always* an error.
This commit renames write_retry to write_data and moves error handling
into the function, making a lot of error handling code redundant.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/util')
-rw-r--r-- | lib/util/padd_file.c | 15 | ||||
-rw-r--r-- | lib/util/write_data.c (renamed from lib/util/write_retry.c) | 15 |
2 files changed, 13 insertions, 17 deletions
diff --git a/lib/util/padd_file.c b/lib/util/padd_file.c index 8598b8b..2f1ea9a 100644 --- a/lib/util/padd_file.c +++ b/lib/util/padd_file.c @@ -9,7 +9,6 @@ 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; @@ -20,21 +19,15 @@ int padd_file(int outfd, uint64_t size, size_t blocksize) 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; + if (write_data("padding output file to block size", + outfd, buffer, padd_sz)) { + goto out; + } 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; diff --git a/lib/util/write_retry.c b/lib/util/write_data.c index 0ef856c..82f3aca 100644 --- a/lib/util/write_retry.c +++ b/lib/util/write_data.c @@ -1,27 +1,30 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ #include <unistd.h> #include <errno.h> +#include <stdio.h> #include "util.h" -ssize_t write_retry(int fd, const void *data, size_t size) +int write_data(const char *errstr, int fd, const void *data, size_t size) { - ssize_t ret, total = 0; + ssize_t ret; while (size > 0) { ret = write(fd, data, size); - if (ret == 0) - break; + if (ret == 0) { + fprintf(stderr, "%s: write truncated\n", errstr); + return -1; + } if (ret < 0) { if (errno == EINTR) continue; + perror(errstr); return -1; } data = (const char *)data + ret; size -= ret; - total += ret; } - return total; + return 0; } |