From 3264774571cc1ae5fbb4dee29d4216e041cdd73d Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 27 Aug 2022 00:48:08 -0500 Subject: [PATCH] Tar compression autodetect wasn't falling back to gzip -d without zcat because fallback compressor name selection happened before autodetect. --- tests/tar.test | 6 ++++++ toys/posix/tar.c | 12 ++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/tar.test b/tests/tar.test index 84f99083..24a43a81 100755 --- a/tests/tar.test +++ b/tests/tar.test @@ -218,6 +218,12 @@ toyonly testing "cat tbz | extract dir/file (autodetect)" \ "dir/\ndir/file\ndrwxr-x--- 1494637555 dd/dir\n-rw-r----- 1494637555 dd/dir/file\n" \ "" "" +mkdir path && ln -s "$(which gzip)" "$(which tar)" path/ +toyonly [ -x path/gzip ] +testing "autodetect falls back to gzip -d when no zcat" \ + "PATH=path; tar tf $FILES/tar/tar.tgz" "dir/\ndir/file\n" "" "" +rm -rf path + yes | head -n $((1<<18)) > bang { dd bs=$((1<<16)) count=1 status=none; dd bs=8192 seek=14 count=1 status=none; dd bs=4096 seek=64 count=5 status=none; } < bang > fweep testing "sparse without overflow" "$TAR --sparse fweep | SUM 3" \ diff --git a/toys/posix/tar.c b/toys/posix/tar.c index 4ac10e75..9d57f30e 100644 --- a/toys/posix/tar.c +++ b/toys/posix/tar.c @@ -878,10 +878,14 @@ static void do_XT(char **pline, long len) if (pline) trim2list(TT.X ? &TT.excl : &TT.incl, *pline); } +static char *get_archiver() +{ + return FLAG(I) ? TT.I : FLAG(z) ? "gzip" : FLAG(J) ? "xz" : "bzip2"; +} + void tar_main(void) { - char *s, **args = toys.optargs, - *archiver = FLAG(I) ? TT.I : (FLAG(z) ? "gzip" : (FLAG(J) ? "xz":"bzip2")); + char *s, **args = toys.optargs; int len = 0, ii; // Needed when extracting to command @@ -980,7 +984,7 @@ void tar_main(void) // Toybox provides more decompressors than compressors, so try them first TT.pid = xpopen_both(zcat ? (char *[]){zcat->str, 0} : - (char *[]){archiver, "-d", 0}, pipefd); + (char *[]){get_archiver(), "-d", 0}, pipefd); if (CFG_TOYBOX_FREE) llist_traverse(zcat, free); if (!hdr) { @@ -1052,7 +1056,7 @@ void tar_main(void) if (FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J)) { int pipefd[2] = {-1, TT.fd}; - xpopen_both((char *[]){archiver, 0}, pipefd); + xpopen_both((char *[]){get_archiver(), 0}, pipefd); close(TT.fd); TT.fd = pipefd[0]; } -- 2.39.2