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 --- include/tar.h | 8 ++++++++ include/util.h | 9 --------- lib/tar/Makemodule.am | 1 + lib/tar/padd_file.c | 4 ++-- lib/tar/write_header.c | 6 +++--- lib/tar/write_retry.c | 37 +++++++++++++++++++++++++++++++++++++ lib/util/Makemodule.am | 2 +- lib/util/write_data.c | 37 ------------------------------------- tar/sqfs2tar.c | 4 ++-- 9 files changed, 54 insertions(+), 54 deletions(-) create mode 100644 lib/tar/write_retry.c delete mode 100644 lib/util/write_data.c diff --git a/include/tar.h b/include/tar.h index 319f035..f71757e 100644 --- a/include/tar.h +++ b/include/tar.h @@ -147,4 +147,12 @@ int padd_file(int outfd, sqfs_u64 size); */ int read_retry(const char *errstr, int fd, void *buffer, size_t size); +/* + A wrapper around the write() system call. It retries the write if it is + interrupted by a signal or only part of the data was written. Returns 0 + on success. Writes to stderr on failure using 'errstr' as a perror style + error prefix. +*/ +int write_retry(const char *errstr, int fd, const void *data, size_t size); + #endif /* TAR_H */ diff --git a/include/util.h b/include/util.h index 323bedd..881b59c 100644 --- a/include/util.h +++ b/include/util.h @@ -40,15 +40,6 @@ #error Cannot determine maximum value of size_t #endif -/* - A wrapper around the write() system call. It retries the write if it is - interrupted by a signal or only part of the data was written. Returns 0 - on success. Writes to stderr on failure using 'errstr' as a perror style - error prefix. -*/ -SQFS_INTERNAL -int write_data(const char *errstr, int fd, const void *data, size_t size); - /* Helper for allocating data structures with flexible array members. 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; +} diff --git a/lib/util/Makemodule.am b/lib/util/Makemodule.am index 8dce70c..77f6c05 100644 --- a/lib/util/Makemodule.am +++ b/lib/util/Makemodule.am @@ -1,4 +1,4 @@ -libutil_la_SOURCES = lib/util/write_data.c include/util.h include/compat.h +libutil_la_SOURCES = include/util.h include/compat.h libutil_la_SOURCES += lib/util/str_table.c include/str_table.h libutil_la_SOURCES += lib/util/alloc.c lib/util/canonicalize_name.c libutil_la_CFLAGS = $(AM_CFLAGS) diff --git a/lib/util/write_data.c b/lib/util/write_data.c deleted file mode 100644 index 8b4c00d..0000000 --- a/lib/util/write_data.c +++ /dev/null @@ -1,37 +0,0 @@ -/* SPDX-License-Identifier: LGPL-3.0-or-later */ -/* - * write_data.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" - -#include -#include -#include - -#include "util.h" - -int write_data(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; -} diff --git a/tar/sqfs2tar.c b/tar/sqfs2tar.c index aaef0f7..ec1deac 100644 --- a/tar/sqfs2tar.c +++ b/tar/sqfs2tar.c @@ -175,8 +175,8 @@ static int terminate_archive(void) memset(buffer, '\0', sizeof(buffer)); - return write_data("adding archive terminator", STDOUT_FILENO, - buffer, sizeof(buffer)); + return write_retry("adding archive terminator", STDOUT_FILENO, + buffer, sizeof(buffer)); } static int get_xattrs(const char *name, const sqfs_inode_generic_t *inode, -- cgit v1.2.3