From 3afffc2a59cfc3888a84b2b2305b5312393ff4e8 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 6 Nov 2019 10:44:26 +0100 Subject: Remove raw file descriptors from unpack write paths Instead, use stdio FILE pointers. On POSIX systems, use fileno to get the file descriptor and hopefully create sparase files. Signed-off-by: David Oberhollenzer --- lib/tar/padd_file.c | 4 ++-- lib/tar/write_header.c | 30 +++++++++++++++--------------- lib/tar/write_retry.c | 15 +++++++-------- 3 files changed, 24 insertions(+), 25 deletions(-) (limited to 'lib/tar') diff --git a/lib/tar/padd_file.c b/lib/tar/padd_file.c index f58cfbf..dd945a3 100644 --- a/lib/tar/padd_file.c +++ b/lib/tar/padd_file.c @@ -10,7 +10,7 @@ #include #include -int padd_file(int outfd, sqfs_u64 size) +int padd_file(FILE *fp, sqfs_u64 size) { size_t padd_sz = size % TAR_RECORD_SIZE; int status = -1; @@ -26,7 +26,7 @@ int padd_file(int outfd, sqfs_u64 size) goto fail_errno; if (write_retry("padding output file to block size", - outfd, buffer, padd_sz)) { + fp, buffer, padd_sz)) { goto out; } diff --git a/lib/tar/write_header.c b/lib/tar/write_header.c index 84aa2d3..6626d00 100644 --- a/lib/tar/write_header.c +++ b/lib/tar/write_header.c @@ -53,7 +53,7 @@ static void write_number_signed(char *dst, sqfs_s64 value, int digits) } } -static int write_header(int fd, const struct stat *sb, const char *name, +static int write_header(FILE *fp, const struct stat *sb, const char *name, const char *slink_target, int type) { int maj = 0, min = 0; @@ -88,10 +88,10 @@ static int write_header(int fd, const struct stat *sb, const char *name, update_checksum(&hdr); - return write_retry("writing tar header record", fd, &hdr, sizeof(hdr)); + return write_retry("writing tar header record", fp, &hdr, sizeof(hdr)); } -static int write_gnu_header(int fd, const struct stat *orig, +static int write_gnu_header(FILE *fp, const struct stat *orig, const char *payload, size_t payload_len, int type, const char *name) { @@ -101,15 +101,15 @@ static int write_gnu_header(int fd, const struct stat *orig, sb.st_mode = S_IFREG | 0644; sb.st_size = payload_len; - if (write_header(fd, &sb, name, NULL, type)) + if (write_header(fp, &sb, name, NULL, type)) return -1; if (write_retry("writing GNU extension header", - fd, payload, payload_len)) { + fp, payload, payload_len)) { return -1; } - return padd_file(fd, payload_len); + return padd_file(fp, payload_len); } static size_t num_digits(size_t num) @@ -124,7 +124,7 @@ static size_t num_digits(size_t num) return i; } -static int write_schily_xattr(int fd, const struct stat *orig, +static int write_schily_xattr(FILE *fp, const struct stat *orig, const char *name, const tar_xattr_t *xattr) { static const char *prefix = "SCHILY.xattr."; @@ -142,20 +142,20 @@ static int write_schily_xattr(int fd, const struct stat *orig, sb.st_mode = S_IFREG | 0644; sb.st_size = total_size; - if (write_header(fd, &sb, name, NULL, TAR_TYPE_PAX)) + if (write_header(fp, &sb, name, NULL, TAR_TYPE_PAX)) return -1; for (it = xattr; it != NULL; it = it->next) { len = strlen(prefix) + strlen(it->key) + strlen(it->value) + 2; len += num_digits(len) + 1; - dprintf(fd, "%zu %s%s=%s\n", len, prefix, it->key, it->value); + fprintf(fp, "%zu %s%s=%s\n", len, prefix, it->key, it->value); } - return padd_file(fd, total_size); + return padd_file(fp, total_size); } -int write_tar_header(int fd, const struct stat *sb, const char *name, +int write_tar_header(FILE *fp, const struct stat *sb, const char *name, const char *slink_target, const tar_xattr_t *xattr, unsigned int counter) { @@ -166,7 +166,7 @@ int write_tar_header(int fd, const struct stat *sb, const char *name, if (xattr != NULL) { sprintf(buffer, "pax/xattr%u", counter); - if (write_schily_xattr(fd, sb, buffer, xattr)) + if (write_schily_xattr(fp, sb, buffer, xattr)) return -1; } @@ -175,7 +175,7 @@ int write_tar_header(int fd, const struct stat *sb, const char *name, if (S_ISLNK(sb->st_mode) && sb->st_size >= 100) { sprintf(buffer, "gnu/target%u", counter); - if (write_gnu_header(fd, sb, slink_target, sb->st_size, + if (write_gnu_header(fp, sb, slink_target, sb->st_size, TAR_TYPE_GNU_SLINK, buffer)) return -1; slink_target = NULL; @@ -184,7 +184,7 @@ int write_tar_header(int fd, const struct stat *sb, const char *name, if (strlen(name) >= 100) { sprintf(buffer, "gnu/name%u", counter); - if (write_gnu_header(fd, sb, name, strlen(name), + if (write_gnu_header(fp, sb, name, strlen(name), TAR_TYPE_GNU_PATH, buffer)) { return -1; } @@ -208,7 +208,7 @@ int write_tar_header(int fd, const struct stat *sb, const char *name, goto out_skip; } - return write_header(fd, sb, name, slink_target, type); + return write_header(fp, sb, name, slink_target, type); out_skip: fprintf(stderr, "WARNING: %s: %s\n", name, reason); return 1; diff --git a/lib/tar/write_retry.c b/lib/tar/write_retry.c index 1ff1a7e..3977db1 100644 --- a/lib/tar/write_retry.c +++ b/lib/tar/write_retry.c @@ -12,23 +12,22 @@ #include "tar.h" -int write_retry(const char *errstr, int fd, const void *data, size_t size) +int write_retry(const char *errstr, FILE *fp, const void *data, size_t size) { - ssize_t ret; + size_t ret; while (size > 0) { - ret = write(fd, data, size); - if (ret == 0) { + if (feof(fp)) { fprintf(stderr, "%s: write truncated\n", errstr); return -1; } - if (ret < 0) { - if (errno == EINTR) - continue; - perror(errstr); + + if (ferror(fp)) { + fprintf(stderr, "%s: error writing to file\n", errstr); return -1; } + ret = fwrite(data, 1, size, fp); data = (const char *)data + ret; size -= ret; } -- cgit v1.2.3