From 5b1a81160a6d0e63ab1360e8777009b913464d89 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 8 Jun 2023 13:22:13 +0200 Subject: Move tar compressor auto wrapping code from tar2sqfs into libtar Signed-off-by: David Oberhollenzer --- bin/tar2sqfs/src/process_tarball.c | 18 +++------- bin/tar2sqfs/src/tar2sqfs.c | 71 ++++++-------------------------------- bin/tar2sqfs/src/tar2sqfs.h | 3 +- 3 files changed, 15 insertions(+), 77 deletions(-) (limited to 'bin') diff --git a/bin/tar2sqfs/src/process_tarball.c b/bin/tar2sqfs/src/process_tarball.c index 9ad7d9a..91572ec 100644 --- a/bin/tar2sqfs/src/process_tarball.c +++ b/bin/tar2sqfs/src/process_tarball.c @@ -148,16 +148,10 @@ static int set_root_attribs(sqfs_writer_t *sqfs, dir_iterator_t *it, return 0; } -int process_tarball(istream_t *input_file, sqfs_writer_t *sqfs) +int process_tarball(dir_iterator_t *it, sqfs_writer_t *sqfs) { - dir_iterator_t *it = tar_open_stream(input_file); size_t rootlen = root_becomes == NULL ? 0 : strlen(root_becomes); - if (it == NULL) { - fputs("Creating tar stream: out-of-memory\n", stderr); - return -1; - } - for (;;) { bool skip = false, is_root = false, is_prefixed = true; dir_entry_t *ent = NULL; @@ -168,7 +162,7 @@ int process_tarball(istream_t *input_file, sqfs_writer_t *sqfs) if (ret > 0) break; if (ret < 0) - goto fail; + return -1; if (ent->mtime < 0) ent->mtime = 0; @@ -181,7 +175,7 @@ int process_tarball(istream_t *input_file, sqfs_writer_t *sqfs) if (ret != 0) { sqfs_perror(ent->name, "read link", ret); free(ent); - goto fail; + return -1; } } @@ -235,12 +229,8 @@ int process_tarball(istream_t *input_file, sqfs_writer_t *sqfs) free(ent); free(link); if (ret) - goto fail; + return -1; } - sqfs_drop(it); return 0; -fail: - sqfs_drop(it); - return -1; } diff --git a/bin/tar2sqfs/src/tar2sqfs.c b/bin/tar2sqfs/src/tar2sqfs.c index 1145675..b09f920 100644 --- a/bin/tar2sqfs/src/tar2sqfs.c +++ b/bin/tar2sqfs/src/tar2sqfs.c @@ -6,65 +6,11 @@ */ #include "tar2sqfs.h" -static int tar_probe(const sqfs_u8 *data, size_t size) -{ - size_t i, offset; - - if (size >= TAR_RECORD_SIZE) { - for (i = 0; i < TAR_RECORD_SIZE; ++i) { - if (data[i] != 0x00) - break; - } - - if (i == TAR_RECORD_SIZE) { - data += TAR_RECORD_SIZE; - size -= TAR_RECORD_SIZE; - } - } - - offset = offsetof(tar_header_t, magic); - - if (offset + 5 <= size) { - if (memcmp(data + offset, "ustar", 5) == 0) - return 1; - } - - return 0; -} - -static istream_t *magic_autowrap(istream_t *strm) -{ - xfrm_stream_t *xfrm = NULL; - istream_t *wrapper = NULL; - int ret; - - ret = istream_precache(strm); - if (ret != 0) - goto out; - - ret = tar_probe(strm->buffer, strm->buffer_used); - if (ret > 0) - return strm; - - ret = xfrm_compressor_id_from_magic(strm->buffer, strm->buffer_used); - if (ret <= 0) - return strm; - - xfrm = decompressor_stream_create(ret); - if (xfrm == NULL) - goto out; - - wrapper = istream_xfrm_create(strm, xfrm); -out: - sqfs_drop(strm); - sqfs_drop(xfrm); - return wrapper; -} - int main(int argc, char **argv) { int status = EXIT_FAILURE; istream_t *input_file = NULL; + dir_iterator_t *tar = NULL; sqfs_writer_t sqfs; process_args(argc, argv); @@ -73,15 +19,18 @@ int main(int argc, char **argv) if (input_file == NULL) return EXIT_FAILURE; - input_file = magic_autowrap(input_file); - if (input_file == NULL) + tar = tar_open_stream(input_file); + sqfs_drop(input_file); + if (tar == NULL) { + fputs("Creating tar stream: out-of-memory\n", stderr); return EXIT_FAILURE; + } memset(&sqfs, 0, sizeof(sqfs)); if (sqfs_writer_init(&sqfs, &cfg)) - goto out_if; + goto out_it; - if (process_tarball(input_file, &sqfs)) + if (process_tarball(tar, &sqfs)) goto out; if (fstree_post_process(&sqfs.fs)) @@ -93,7 +42,7 @@ int main(int argc, char **argv) status = EXIT_SUCCESS; out: sqfs_writer_cleanup(&sqfs, status); -out_if: - sqfs_drop(input_file); +out_it: + sqfs_drop(tar); return status; } diff --git a/bin/tar2sqfs/src/tar2sqfs.h b/bin/tar2sqfs/src/tar2sqfs.h index a21774b..e853ae9 100644 --- a/bin/tar2sqfs/src/tar2sqfs.h +++ b/bin/tar2sqfs/src/tar2sqfs.h @@ -15,7 +15,6 @@ #include "tar/tar.h" #include "tar/format.h" #include "xfrm/compress.h" -#include "io/xfrm.h" #include #include @@ -34,6 +33,6 @@ extern char *root_becomes; void process_args(int argc, char **argv); /* process_tarball.c */ -int process_tarball(istream_t *input_file, sqfs_writer_t *sqfs); +int process_tarball(dir_iterator_t *it, sqfs_writer_t *sqfs); #endif /* TAR2SQFS_H */ -- cgit v1.2.3