From 81b8461306e684371e4de1d6491b24a1f7e1d5d3 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 7 Oct 2019 15:18:03 +0200 Subject: Cleanup: move write_data to libtar Signed-off-by: David Oberhollenzer --- lib/tar/Makemodule.am | 1 + lib/tar/padd_file.c | 4 ++-- lib/tar/write_header.c | 6 +++--- lib/tar/write_retry.c | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 lib/tar/write_retry.c (limited to 'lib/tar') diff --git a/lib/tar/Makemodule.am b/lib/tar/Makemodule.am index f0945ff..abc0209 100644 --- a/lib/tar/Makemodule.am +++ b/lib/tar/Makemodule.am @@ -3,6 +3,7 @@ libtar_a_SOURCES += lib/tar/number.c lib/tar/checksum.c lib/tar/cleanup.c libtar_a_SOURCES += lib/tar/read_sparse_map.c lib/tar/read_sparse_map_old.c libtar_a_SOURCES += lib/tar/base64.c lib/tar/urldecode.c lib/tar/internal.h libtar_a_SOURCES += lib/tar/padd_file.c lib/tar/read_retry.c include/tar.h +libtar_a_SOURCES += lib/tar/write_retry.c libtar_a_CFLAGS = $(AM_CFLAGS) libtar_a_CPPFLAGS = $(AM_CPPFLAGS) diff --git a/lib/tar/padd_file.c b/lib/tar/padd_file.c index 471370d..f58cfbf 100644 --- a/lib/tar/padd_file.c +++ b/lib/tar/padd_file.c @@ -25,8 +25,8 @@ int padd_file(int outfd, sqfs_u64 size) if (buffer == NULL) goto fail_errno; - if (write_data("padding output file to block size", - outfd, buffer, padd_sz)) { + if (write_retry("padding output file to block size", + outfd, buffer, padd_sz)) { goto out; } diff --git a/lib/tar/write_header.c b/lib/tar/write_header.c index 14802c0..84aa2d3 100644 --- a/lib/tar/write_header.c +++ b/lib/tar/write_header.c @@ -88,7 +88,7 @@ static int write_header(int fd, const struct stat *sb, const char *name, update_checksum(&hdr); - return write_data("writing tar header record", fd, &hdr, sizeof(hdr)); + return write_retry("writing tar header record", fd, &hdr, sizeof(hdr)); } static int write_gnu_header(int fd, const struct stat *orig, @@ -104,8 +104,8 @@ static int write_gnu_header(int fd, const struct stat *orig, if (write_header(fd, &sb, name, NULL, type)) return -1; - if (write_data("writing GNU extension header", - fd, payload, payload_len)) { + if (write_retry("writing GNU extension header", + fd, payload, payload_len)) { return -1; } diff --git a/lib/tar/write_retry.c b/lib/tar/write_retry.c new file mode 100644 index 0000000..1ff1a7e --- /dev/null +++ b/lib/tar/write_retry.c @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * write_retry.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include +#include +#include + +#include "tar.h" + +int write_retry(const char *errstr, int fd, const void *data, size_t size) +{ + ssize_t ret; + + while (size > 0) { + ret = write(fd, data, size); + 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; + } + + return 0; +} -- cgit v1.2.3