aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-11-06 11:54:56 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-11-06 11:56:02 +0100
commit4d46b361ff1371a6f3f4f89ed8ca81ee23e86de8 (patch)
tree0705d7c216ea345665cd38345bc21f8931195849 /lib
parent3afffc2a59cfc3888a84b2b2305b5312393ff4e8 (diff)
Remove raw file descriptors from tar read path
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/common/io_stdin.c21
-rw-r--r--lib/tar/internal.h2
-rw-r--r--lib/tar/read_header.c22
-rw-r--r--lib/tar/read_retry.c16
-rw-r--r--lib/tar/read_sparse_map_old.c4
-rw-r--r--lib/tar/skip.c12
6 files changed, 37 insertions, 40 deletions
diff --git a/lib/common/io_stdin.c b/lib/common/io_stdin.c
index 9e96e45..ea73771 100644
--- a/lib/common/io_stdin.c
+++ b/lib/common/io_stdin.c
@@ -46,7 +46,7 @@ static int stdin_read_at(sqfs_file_t *base, sqfs_u64 offset,
size_t temp_size = 0;
sqfs_u8 *temp = NULL;
sqfs_u64 diff;
- ssize_t ret;
+ size_t ret;
if (offset < file->offset)
return SQFS_ERROR_IO;
@@ -60,24 +60,21 @@ static int stdin_read_at(sqfs_file_t *base, sqfs_u64 offset,
return SQFS_ERROR_OUT_OF_BOUNDS;
while (size > 0) {
+ if (ferror(stdin))
+ return SQFS_ERROR_IO;
+
+ if (feof(stdin))
+ return SQFS_ERROR_OUT_OF_BOUNDS;
+
if (offset > file->offset) {
diff = file->offset - offset;
diff = diff > (sqfs_u64)temp_size ? temp_size : diff;
- ret = read(STDIN_FILENO, temp, diff);
+ ret = fread(temp, 1, diff, stdin);
} else {
- ret = read(STDIN_FILENO, buffer, size);
- }
-
- if (ret < 0) {
- if (errno == EINTR)
- continue;
- return SQFS_ERROR_IO;
+ ret = fread(buffer, 1, size, stdin);
}
- if (ret == 0)
- return SQFS_ERROR_OUT_OF_BOUNDS;
-
if (offset <= file->offset) {
buffer = (char *)buffer + ret;
size -= ret;
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 <stdio.h>
-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);
}