From 4d46b361ff1371a6f3f4f89ed8ca81ee23e86de8 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 6 Nov 2019 11:54:56 +0100 Subject: Remove raw file descriptors from tar read path Signed-off-by: David Oberhollenzer --- lib/tar/internal.h | 2 +- lib/tar/read_header.c | 22 +++++++++++----------- lib/tar/read_retry.c | 16 ++++++++-------- lib/tar/read_sparse_map_old.c | 4 ++-- lib/tar/skip.c | 12 ++++++------ 5 files changed, 28 insertions(+), 28 deletions(-) (limited to 'lib/tar') diff --git a/lib/tar/internal.h b/lib/tar/internal.h index 77393ed..e67e7fa 100644 --- a/lib/tar/internal.h +++ b/lib/tar/internal.h @@ -56,7 +56,7 @@ bool is_checksum_valid(const tar_header_t *hdr); sparse_map_t *read_sparse_map(const char *line); -sparse_map_t *read_gnu_old_sparse(int fd, tar_header_t *hdr); +sparse_map_t *read_gnu_old_sparse(FILE *fp, tar_header_t *hdr); void free_sparse_list(sparse_map_t *sparse); diff --git a/lib/tar/read_header.c b/lib/tar/read_header.c index 760d36a..7ec1333 100644 --- a/lib/tar/read_header.c +++ b/lib/tar/read_header.c @@ -35,17 +35,17 @@ static int check_version(const tar_header_t *hdr) return ETV_UNKNOWN; } -static char *record_to_memory(int fd, sqfs_u64 size) +static char *record_to_memory(FILE *fp, sqfs_u64 size) { char *buffer = malloc(size + 1); if (buffer == NULL) goto fail_errno; - if (read_retry("reading tar record", fd, buffer, size)) + if (read_retry("reading tar record", fp, buffer, size)) goto fail; - if (skip_padding(fd, size)) + if (skip_padding(fp, size)) goto fail; buffer[size] = '\0'; @@ -74,7 +74,7 @@ static tar_xattr_t *mkxattr(const char *key, size_t keylen, return xattr; } -static int read_pax_header(int fd, sqfs_u64 entsize, unsigned int *set_by_pax, +static int read_pax_header(FILE *fp, sqfs_u64 entsize, unsigned int *set_by_pax, tar_header_decoded_t *out) { sparse_map_t *sparse_last = NULL, *sparse; @@ -83,7 +83,7 @@ static int read_pax_header(int fd, sqfs_u64 entsize, unsigned int *set_by_pax, tar_xattr_t *xattr; sqfs_u64 i; - buffer = record_to_memory(fd, entsize); + buffer = record_to_memory(fp, entsize); if (buffer == NULL) return -1; @@ -357,7 +357,7 @@ static int decode_header(const tar_header_t *hdr, unsigned int set_by_pax, return 0; } -int read_header(int fd, tar_header_decoded_t *out) +int read_header(FILE *fp, tar_header_decoded_t *out) { unsigned int set_by_pax = 0; bool prev_was_zero = false; @@ -368,7 +368,7 @@ int read_header(int fd, tar_header_decoded_t *out) memset(out, 0, sizeof(*out)); for (;;) { - if (read_retry("reading tar header", fd, &hdr, sizeof(hdr))) + if (read_retry("reading tar header", fp, &hdr, sizeof(hdr))) goto fail; if (is_zero_block(&hdr)) { @@ -394,7 +394,7 @@ int read_header(int fd, tar_header_decoded_t *out) if (pax_size < 1 || pax_size > TAR_MAX_SYMLINK_LEN) goto fail_slink_len; free(out->link_target); - out->link_target = record_to_memory(fd, pax_size); + out->link_target = record_to_memory(fp, pax_size); if (out->link_target == NULL) goto fail; set_by_pax |= PAX_SLINK_TARGET; @@ -405,7 +405,7 @@ int read_header(int fd, tar_header_decoded_t *out) if (pax_size < 1 || pax_size > TAR_MAX_PATH_LEN) goto fail_path_len; free(out->name); - out->name = record_to_memory(fd, pax_size); + out->name = record_to_memory(fp, pax_size); if (out->name == NULL) goto fail; set_by_pax |= PAX_NAME; @@ -417,12 +417,12 @@ int read_header(int fd, tar_header_decoded_t *out) if (pax_size < 1 || pax_size > TAR_MAX_PAX_LEN) goto fail_pax_len; set_by_pax = 0; - if (read_pax_header(fd, pax_size, &set_by_pax, out)) + if (read_pax_header(fp, pax_size, &set_by_pax, out)) goto fail; continue; case TAR_TYPE_GNU_SPARSE: free_sparse_list(out->sparse); - out->sparse = read_gnu_old_sparse(fd, &hdr); + out->sparse = read_gnu_old_sparse(fp, &hdr); if (out->sparse == NULL) goto fail; if (read_number(hdr.tail.gnu.realsize, diff --git a/lib/tar/read_retry.c b/lib/tar/read_retry.c index 5d06595..f81c52a 100644 --- a/lib/tar/read_retry.c +++ b/lib/tar/read_retry.c @@ -12,23 +12,23 @@ #include "tar.h" -int read_retry(const char *errstr, int fd, void *buffer, size_t size) +int read_retry(const char *errstr, FILE *fp, void *buffer, size_t size) { - ssize_t ret; + size_t ret; while (size > 0) { - ret = read(fd, buffer, size); - if (ret < 0) { - if (errno == EINTR) - continue; - perror(errstr); + if (ferror(fp)) { + fprintf(stderr, "%s: error reading from file\n", + errstr); return -1; } - if (ret == 0) { + + if (feof(fp)) { fprintf(stderr, "%s: short read\n", errstr); return -1; } + ret = fread(buffer, 1, size, fp); size -= ret; buffer = (char *)buffer + ret; } diff --git a/lib/tar/read_sparse_map_old.c b/lib/tar/read_sparse_map_old.c index 959c9a8..84e1f9e 100644 --- a/lib/tar/read_sparse_map_old.c +++ b/lib/tar/read_sparse_map_old.c @@ -8,7 +8,7 @@ #include "internal.h" -sparse_map_t *read_gnu_old_sparse(int fd, tar_header_t *hdr) +sparse_map_t *read_gnu_old_sparse(FILE *fp, tar_header_t *hdr) { sparse_map_t *list = NULL, *end = NULL, *node; gnu_sparse_t sph; @@ -48,7 +48,7 @@ sparse_map_t *read_gnu_old_sparse(int fd, tar_header_t *hdr) do { if (read_retry("reading GNU sparse header", - fd, &sph, sizeof(sph))) { + fp, &sph, sizeof(sph))) { goto fail; } diff --git a/lib/tar/skip.c b/lib/tar/skip.c index e5c9772..94e4a96 100644 --- a/lib/tar/skip.c +++ b/lib/tar/skip.c @@ -11,7 +11,7 @@ #include -static int skip_bytes(int fd, sqfs_u64 size) +static int skip_bytes(FILE *fp, sqfs_u64 size) { unsigned char buffer[1024]; size_t diff; @@ -21,7 +21,7 @@ static int skip_bytes(int fd, sqfs_u64 size) if (diff > size) diff = size; - if (read_retry("reading tar record padding", fd, buffer, diff)) + if (read_retry("reading tar record padding", fp, buffer, diff)) return -1; size -= diff; @@ -30,16 +30,16 @@ static int skip_bytes(int fd, sqfs_u64 size) return 0; } -int skip_padding(int fd, sqfs_u64 size) +int skip_padding(FILE *fp, sqfs_u64 size) { size_t tail = size % 512; - return tail ? skip_bytes(fd, 512 - tail) : 0; + return tail ? skip_bytes(fp, 512 - tail) : 0; } -int skip_entry(int fd, sqfs_u64 size) +int skip_entry(FILE *fp, sqfs_u64 size) { size_t tail = size % 512; - return skip_bytes(fd, tail ? (size + 512 - tail) : size); + return skip_bytes(fp, tail ? (size + 512 - tail) : size); } -- cgit v1.2.3