diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-11-06 11:54:56 +0100 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-11-06 11:56:02 +0100 |
commit | 4d46b361ff1371a6f3f4f89ed8ca81ee23e86de8 (patch) | |
tree | 0705d7c216ea345665cd38345bc21f8931195849 | |
parent | 3afffc2a59cfc3888a84b2b2305b5312393ff4e8 (diff) |
Remove raw file descriptors from tar read path
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r-- | include/tar.h | 8 | ||||
-rw-r--r-- | lib/common/io_stdin.c | 21 | ||||
-rw-r--r-- | lib/tar/internal.h | 2 | ||||
-rw-r--r-- | lib/tar/read_header.c | 22 | ||||
-rw-r--r-- | lib/tar/read_retry.c | 16 | ||||
-rw-r--r-- | lib/tar/read_sparse_map_old.c | 4 | ||||
-rw-r--r-- | lib/tar/skip.c | 12 | ||||
-rw-r--r-- | tar/tar2sqfs.c | 6 | ||||
-rw-r--r-- | tests/tar_fuzz.c | 15 | ||||
-rw-r--r-- | tests/tar_gnu.c | 64 | ||||
-rw-r--r-- | tests/tar_pax.c | 56 | ||||
-rw-r--r-- | tests/tar_sparse_gnu.c | 22 | ||||
-rw-r--r-- | tests/tar_sparse_gnu1.c | 16 | ||||
-rw-r--r-- | tests/tar_sparse_gnu2.c | 22 | ||||
-rw-r--r-- | tests/tar_ustar.c | 64 | ||||
-rw-r--r-- | tests/tar_xattr_bsd.c | 18 | ||||
-rw-r--r-- | tests/tar_xattr_schily.c | 18 |
17 files changed, 192 insertions, 194 deletions
diff --git a/include/tar.h b/include/tar.h index 8f9f55c..dacea42 100644 --- a/include/tar.h +++ b/include/tar.h @@ -124,12 +124,12 @@ int write_tar_header(FILE *fp, const struct stat *sb, const char *name, unsigned int counter); /* calcuate and skip the zero padding */ -int skip_padding(int fd, sqfs_u64 size); +int skip_padding(FILE *fp, sqfs_u64 size); /* round up to block size and skip the entire entry */ -int skip_entry(int fd, sqfs_u64 size); +int skip_entry(FILE *fp, sqfs_u64 size); -int read_header(int fd, tar_header_decoded_t *out); +int read_header(FILE *fp, tar_header_decoded_t *out); void clear_header(tar_header_decoded_t *hdr); @@ -146,7 +146,7 @@ int padd_file(FILE *fp, sqfs_u64 size); on success. Writes to stderr on failure using 'errstr' as a perror style error prefix. */ -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); /* A wrapper around the write() system call. It retries the write if it is 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); } diff --git a/tar/tar2sqfs.c b/tar/tar2sqfs.c index 30f1abb..4634364 100644 --- a/tar/tar2sqfs.c +++ b/tar/tar2sqfs.c @@ -251,7 +251,7 @@ static int write_file(tar_header_decoded_t *hdr, file_info_t *fi, if (ret) return -1; - return skip_padding(STDIN_FILENO, hdr->sparse == NULL ? + return skip_padding(stdin, hdr->sparse == NULL ? filesize : hdr->record_size); } @@ -337,7 +337,7 @@ static int process_tar_ball(void) int ret; for (;;) { - ret = read_header(STDIN_FILENO, &hdr); + ret = read_header(stdin, &hdr); if (ret > 0) break; if (ret < 0) @@ -395,7 +395,7 @@ static int process_tar_ball(void) if (skip) { if (dont_skip) goto fail; - if (skip_entry(STDIN_FILENO, hdr.sb.st_size)) + if (skip_entry(stdin, hdr.sb.st_size)) goto fail; clear_header(&hdr); diff --git a/tests/tar_fuzz.c b/tests/tar_fuzz.c index dad3d5a..1a51da4 100644 --- a/tests/tar_fuzz.c +++ b/tests/tar_fuzz.c @@ -17,36 +17,37 @@ int main(int argc, char **argv) { tar_header_decoded_t hdr; - int fd, ret; + FILE *fp; + int ret; if (argc != 2) { fputs("usage: tar_fuzz <tarball>\n", stderr); return EXIT_FAILURE; } - fd = open(argv[1], O_RDONLY); - if (fd < 0) { + fp = fopen(argv[1], "rb"); + if (fp == NULL) { perror(argv[1]); return EXIT_FAILURE; } for (;;) { - ret = read_header(fd, &hdr); + ret = read_header(fp, &hdr); if (ret > 0) break; if (ret < 0) goto fail; - ret = lseek(fd, hdr.sb.st_size, SEEK_CUR); + ret = fseek(fp, hdr.sb.st_size, SEEK_CUR); clear_header(&hdr); if (ret < 0) goto fail; } - close(fd); + fclose(fp); return EXIT_SUCCESS; fail: - close(fd); + fclose(fp); return EXIT_FAILURE; } diff --git a/tests/tar_gnu.c b/tests/tar_gnu.c index b58c9f7..43ebccf 100644 --- a/tests/tar_gnu.c +++ b/tests/tar_gnu.c @@ -21,16 +21,16 @@ #define TEST_PATH STRVALUE(TESTPATH) -static int open_read(const char *path) +static FILE *open_read(const char *path) { - int fd = open(path, O_RDONLY); + FILE *fp = fopen(path, "rb"); - if (fd < 0) { + if (fp == NULL) { perror(path); exit(EXIT_FAILURE); } - return fd; + return fp; } static const char *filename = @@ -42,12 +42,12 @@ int main(void) { tar_header_decoded_t hdr; char buffer[6]; - int fd; + FILE *fp; assert(chdir(TEST_PATH) == 0); - fd = open_read("format-acceptance/gnu.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("format-acceptance/gnu.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -56,14 +56,14 @@ int main(void) assert(hdr.mtime == 1542905892); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data0", fd, buffer, 5) == 0); + assert(read_retry("data0", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("format-acceptance/gnu-g.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("format-acceptance/gnu-g.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -72,14 +72,14 @@ int main(void) assert(hdr.mtime == 013375560044); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data1", fd, buffer, 5) == 0); + assert(read_retry("data1", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("file-size/gnu.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("file-size/gnu.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -89,10 +89,10 @@ int main(void) assert(strcmp(hdr.name, "big-file.bin") == 0); assert(!hdr.unknown_record); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("user-group-largenum/gnu.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("user-group-largenum/gnu.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 0x80000000); assert(hdr.sb.st_gid == 0x80000000); @@ -101,14 +101,14 @@ int main(void) assert(hdr.mtime == 013376036700); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data2", fd, buffer, 5) == 0); + assert(read_retry("data2", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("large-mtime/gnu.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("large-mtime/gnu.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -121,14 +121,14 @@ int main(void) assert(hdr.mtime == 8589934592L); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data3", fd, buffer, 5) == 0); + assert(read_retry("data3", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("negative-mtime/gnu.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("negative-mtime/gnu.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -137,14 +137,14 @@ int main(void) assert(hdr.mtime == -315622800); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data4", fd, buffer, 5) == 0); + assert(read_retry("data4", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("long-paths/gnu.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("long-paths/gnu.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -153,11 +153,11 @@ int main(void) assert(hdr.mtime == 1542909670); assert(strcmp(hdr.name, filename) == 0); assert(!hdr.unknown_record); - assert(read_retry("data5", fd, buffer, 5) == 0); + assert(read_retry("data5", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); return EXIT_SUCCESS; } diff --git a/tests/tar_pax.c b/tests/tar_pax.c index f0fdef0..475a7f1 100644 --- a/tests/tar_pax.c +++ b/tests/tar_pax.c @@ -21,16 +21,16 @@ #define TEST_PATH STRVALUE(TESTPATH) -static int open_read(const char *path) +static FILE *open_read(const char *path) { - int fd = open(path, O_RDONLY); + FILE *fp = fopen(path, "rb"); - if (fd < 0) { + if (fp == NULL) { perror(path); exit(EXIT_FAILURE); } - return fd; + return fp; } static const char *filename = @@ -42,12 +42,12 @@ int main(void) { tar_header_decoded_t hdr; char buffer[6]; - int fd; + FILE *fp; assert(chdir(TEST_PATH) == 0); - fd = open_read("format-acceptance/pax.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("format-acceptance/pax.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -56,14 +56,14 @@ int main(void) assert(hdr.mtime == 1542905892); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data0", fd, buffer, 5) == 0); + assert(read_retry("data0", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("file-size/pax.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("file-size/pax.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -73,10 +73,10 @@ int main(void) assert(strcmp(hdr.name, "big-file.bin") == 0); assert(!hdr.unknown_record); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("user-group-largenum/pax.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("user-group-largenum/pax.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 2147483648); assert(hdr.sb.st_gid == 2147483648); @@ -85,14 +85,14 @@ int main(void) assert(hdr.mtime == 013376036700); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data1", fd, buffer, 5) == 0); + assert(read_retry("data1", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("large-mtime/pax.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("large-mtime/pax.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -105,14 +105,14 @@ int main(void) assert(hdr.mtime == 8589934592L); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data2", fd, buffer, 5) == 0); + assert(read_retry("data2", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("negative-mtime/pax.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("negative-mtime/pax.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -121,14 +121,14 @@ int main(void) assert(hdr.mtime == -315622800); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data3", fd, buffer, 5) == 0); + assert(read_retry("data3", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("long-paths/pax.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("long-paths/pax.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -137,11 +137,11 @@ int main(void) assert(hdr.mtime == 1542909670); assert(strcmp(hdr.name, filename) == 0); assert(!hdr.unknown_record); - assert(read_retry("data4", fd, buffer, 5) == 0); + assert(read_retry("data4", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); return EXIT_SUCCESS; } diff --git a/tests/tar_sparse_gnu.c b/tests/tar_sparse_gnu.c index 3546571..b67171d 100644 --- a/tests/tar_sparse_gnu.c +++ b/tests/tar_sparse_gnu.c @@ -21,28 +21,28 @@ #define TEST_PATH STRVALUE(TESTPATH) -static int open_read(const char *path) +static FILE *open_read(const char *path) { - int fd = open(path, O_RDONLY); + FILE *fp = fopen(path, "rb"); - if (fd < 0) { + if (fp == NULL) { perror(path); exit(EXIT_FAILURE); } - return fd; + return fp; } int main(void) { tar_header_decoded_t hdr; sparse_map_t *sparse; - int fd; + FILE *fp; assert(chdir(TEST_PATH) == 0); - fd = open_read("sparse-files/gnu-small.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("sparse-files/gnu-small.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -70,10 +70,10 @@ int main(void) assert(sparse->next == NULL); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("sparse-files/gnu.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("sparse-files/gnu.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -132,7 +132,7 @@ int main(void) assert(sparse == NULL); clear_header(&hdr); - close(fd); + fclose(fp); return EXIT_SUCCESS; } diff --git a/tests/tar_sparse_gnu1.c b/tests/tar_sparse_gnu1.c index 19106cd..a9d90d4 100644 --- a/tests/tar_sparse_gnu1.c +++ b/tests/tar_sparse_gnu1.c @@ -21,28 +21,28 @@ #define TEST_PATH STRVALUE(TESTPATH) -static int open_read(const char *path) +static FILE *open_read(const char *path) { - int fd = open(path, O_RDONLY); + FILE *fp = fopen(path, "rb"); - if (fd < 0) { + if (fp == NULL) { perror(path); exit(EXIT_FAILURE); } - return fd; + return fp; } int main(void) { tar_header_decoded_t hdr; sparse_map_t *sparse; - int fd; + FILE *fp; assert(chdir(TEST_PATH) == 0); - fd = open_read("sparse-files/pax-gnu0-0.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("sparse-files/pax-gnu0-0.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -101,7 +101,7 @@ int main(void) assert(sparse == NULL); clear_header(&hdr); - close(fd); + fclose(fp); return EXIT_SUCCESS; } diff --git a/tests/tar_sparse_gnu2.c b/tests/tar_sparse_gnu2.c index 2171850..6402c87 100644 --- a/tests/tar_sparse_gnu2.c +++ b/tests/tar_sparse_gnu2.c @@ -21,28 +21,28 @@ #define TEST_PATH STRVALUE(TESTPATH) -static int open_read(const char *path) +static FILE *open_read(const char *path) { - int fd = open(path, O_RDONLY); + FILE *fp = open(path, O_RDONLY); - if (fd < 0) { + if (fp == NULL) { perror(path); exit(EXIT_FAILURE); } - return fd; + return fp; } int main(void) { tar_header_decoded_t hdr; sparse_map_t *sparse; - int fd; + FILE *fp; assert(chdir(TEST_PATH) == 0); - fd = open_read("sparse-files/pax-gnu0-1.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("sparse-files/pax-gnu0-1.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -101,10 +101,10 @@ int main(void) assert(sparse == NULL); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("sparse-files/pax-gnu1-0.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("sparse-files/pax-gnu1-0.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -163,7 +163,7 @@ int main(void) assert(sparse == NULL); clear_header(&hdr); - close(fd); + fclose(fp); return EXIT_SUCCESS; } diff --git a/tests/tar_ustar.c b/tests/tar_ustar.c index 20a0bd0..8b9b453 100644 --- a/tests/tar_ustar.c +++ b/tests/tar_ustar.c @@ -21,16 +21,16 @@ #define TEST_PATH STRVALUE(TESTPATH) -static int open_read(const char *path) +static FILE *open_read(const char *path) { - int fd = open(path, O_RDONLY); + FILE *fp = fopen(path, "rb"); - if (fd < 0) { + if (fp == NULL) { perror(path); exit(EXIT_FAILURE); } - return fd; + return fp; } static const char *filename = @@ -42,12 +42,12 @@ int main(void) { tar_header_decoded_t hdr; char buffer[6]; - int fd; + FILE *fp; assert(chdir(TEST_PATH) == 0); - fd = open_read("format-acceptance/ustar.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("format-acceptance/ustar.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -56,14 +56,14 @@ int main(void) assert(hdr.mtime == 1542905892); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data0", fd, buffer, 5) == 0); + assert(read_retry("data0", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("format-acceptance/ustar-pre-posix.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("format-acceptance/ustar-pre-posix.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -72,14 +72,14 @@ int main(void) assert(hdr.mtime == 1542905892); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data1", fd, buffer, 5) == 0); + assert(read_retry("data1", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("format-acceptance/v7.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("format-acceptance/v7.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -88,14 +88,14 @@ int main(void) assert(hdr.mtime == 1542905892); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data2", fd, buffer, 5) == 0); + assert(read_retry("data2", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("file-size/12-digit.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("file-size/12-digit.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -105,10 +105,10 @@ int main(void) assert(strcmp(hdr.name, "big-file.bin") == 0); assert(!hdr.unknown_record); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("user-group-largenum/8-digit.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("user-group-largenum/8-digit.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 8388608); assert(hdr.sb.st_gid == 8388608); @@ -117,14 +117,14 @@ int main(void) assert(hdr.mtime == 013376036700); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data3", fd, buffer, 5) == 0); + assert(read_retry("data3", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("large-mtime/12-digit.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("large-mtime/12-digit.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -137,14 +137,14 @@ int main(void) assert(hdr.mtime == 8589934592L); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data4", fd, buffer, 5) == 0); + assert(read_retry("data4", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); - fd = open_read("long-paths/ustar.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("long-paths/ustar.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -153,11 +153,11 @@ int main(void) assert(hdr.mtime == 1542909670); assert(strcmp(hdr.name, filename) == 0); assert(!hdr.unknown_record); - assert(read_retry("data5", fd, buffer, 5) == 0); + assert(read_retry("data5", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); clear_header(&hdr); - close(fd); + fclose(fp); return EXIT_SUCCESS; } diff --git a/tests/tar_xattr_bsd.c b/tests/tar_xattr_bsd.c index 36d8ac1..9a7eab2 100644 --- a/tests/tar_xattr_bsd.c +++ b/tests/tar_xattr_bsd.c @@ -21,28 +21,28 @@ #define TEST_PATH STRVALUE(TESTPATH) -static int open_read(const char *path) +static FILE *open_read(const char *path) { - int fd = open(path, O_RDONLY); + FILE *fp = fopen(path, "rb"); - if (fd < 0) { + if (fp == NULL) { perror(path); exit(EXIT_FAILURE); } - return fd; + return fp; } int main(void) { tar_header_decoded_t hdr; char buffer[6]; - int fd; + FILE *fp; assert(chdir(TEST_PATH) == 0); - fd = open_read("xattr/xattr-libarchive.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("xattr/xattr-libarchive.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -51,7 +51,7 @@ int main(void) assert(hdr.mtime == 1543094477); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data0", fd, buffer, 5) == 0); + assert(read_retry("data0", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); @@ -61,6 +61,6 @@ int main(void) assert(hdr.xattr->next == NULL); clear_header(&hdr); - close(fd); + fclose(fp); return EXIT_SUCCESS; } diff --git a/tests/tar_xattr_schily.c b/tests/tar_xattr_schily.c index e543351..7434a41 100644 --- a/tests/tar_xattr_schily.c +++ b/tests/tar_xattr_schily.c @@ -21,28 +21,28 @@ #define TEST_PATH STRVALUE(TESTPATH) -static int open_read(const char *path) +static FILE *open_read(const char *path) { - int fd = open(path, O_RDONLY); + FILE *fp = fopen(path, "rb"); - if (fd < 0) { + if (fp == NULL) { perror(path); exit(EXIT_FAILURE); } - return fd; + return fp; } int main(void) { tar_header_decoded_t hdr; char buffer[6]; - int fd; + FILE *fp; assert(chdir(TEST_PATH) == 0); - fd = open_read("xattr/xattr-schily.tar"); - assert(read_header(fd, &hdr) == 0); + fp = open_read("xattr/xattr-schily.tar"); + assert(read_header(fp, &hdr) == 0); assert(hdr.sb.st_mode == (S_IFREG | 0644)); assert(hdr.sb.st_uid == 01750); assert(hdr.sb.st_gid == 01750); @@ -51,7 +51,7 @@ int main(void) assert(hdr.mtime == 1543094477); assert(strcmp(hdr.name, "input.txt") == 0); assert(!hdr.unknown_record); - assert(read_retry("data0", fd, buffer, 5) == 0); + assert(read_retry("data0", fp, buffer, 5) == 0); buffer[5] = '\0'; assert(strcmp(buffer, "test\n") == 0); @@ -61,6 +61,6 @@ int main(void) assert(hdr.xattr->next == NULL); clear_header(&hdr); - close(fd); + fclose(fp); return EXIT_SUCCESS; } |