This also changes the other compression options (such as -j) so that we
pass no arguments for compression and just -d for decompression, which
is what -I does to its filter and which appears sufficient. (I think I
used -dc before just out of habit, since that's what I've been typing on
the command line for decades.)
---
 tests/tar.test   |  7 +++++++
 toys/posix/tar.c | 21 +++++++++++----------
 2 files changed, 18 insertions(+), 10 deletions(-)
From f51ef656e13df26ee30e1b99fbd71458aa62304d Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Sat, 22 Aug 2020 12:41:55 -0700
Subject: [PATCH] tar: add -I (--use-compress-program) support.

This also changes the other compression options (such as -j) so that we
pass no arguments for compression and just -d for decompression, which
is what -I does to its filter and which appears sufficient. (I think I
used -dc before just out of habit, since that's what I've been typing on
the command line for decades.)
---
 tests/tar.test   |  7 +++++++
 toys/posix/tar.c | 21 +++++++++++----------
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/tests/tar.test b/tests/tar.test
index d6c505ae..0a91bac8 100644
--- a/tests/tar.test
+++ b/tests/tar.test
@@ -161,6 +161,13 @@ testing "manually specify bz2" 'LST -jf "$FILES"/tar/tar.tbz2' \
   "drwxr-x--- enh/eng 0 2017-05-13 01:05 dir/\n-rw-r----- enh/eng 12 2017-05-13 01:05 dir/file\n" \
   "" ""
 
+# -I
+testing "-I gzip c" "$TAR -Igzip file | file -" \
+  "/dev/stdin: gzip compressed data, from Unix\n" "" ""
+testing "-I gzip t" 'LST -Igzip -f "$FILES"/tar/tar.tgz' \
+  "drwxr-x--- enh/eng 0 2017-05-13 01:05 dir/\n-rw-r----- enh/eng 12 2017-05-13 01:05 dir/file\n" \
+  "" ""
+
 skipnot mknod dir/char c 12 34 2>/dev/null
 testing "character special" "tar --mtime @0 -cf test.tar dir/char && rm -f dir/char && tar xf test.tar && ls -l dir/char" \
   "crw-rw---- 1 root root 12,  34 1970-01-01 00:00 dir/char\n" "" ""
diff --git a/toys/posix/tar.c b/toys/posix/tar.c
index 9e1a4bb5..66547613 100644
--- a/toys/posix/tar.c
+++ b/toys/posix/tar.c
@@ -17,7 +17,7 @@
  * Why --exclude pattern but no --include? tar cvzf a.tgz dir --include '*.txt'
  *
 
-USE_TAR(NEWTOY(tar, "&(restrict)(full-time)(no-recursion)(numeric-owner)(no-same-permissions)(overwrite)(exclude)*(mode):(mtime):(group):(owner):(to-command):o(no-same-owner)p(same-permissions)k(keep-old)c(create)|h(dereference)x(extract)|t(list)|v(verbose)J(xz)j(bzip2)z(gzip)S(sparse)O(to-stdout)P(absolute-names)m(touch)X(exclude-from)*T(files-from)*C(directory):f(file):a[!txc][!jzJa]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_TAR(NEWTOY(tar, "&(restrict)(full-time)(no-recursion)(numeric-owner)(no-same-permissions)(overwrite)(exclude)*(mode):(mtime):(group):(owner):(to-command):o(no-same-owner)p(same-permissions)k(keep-old)c(create)|h(dereference)x(extract)|t(list)|v(verbose)I(use-compress-program):J(xz)j(bzip2)z(gzip)S(sparse)O(to-stdout)P(absolute-names)m(touch)X(exclude-from)*T(files-from)*C(directory):f(file):a[!txc][!jzJa]", TOYFLAG_USR|TOYFLAG_BIN))
 
 config TAR
   bool "tar"
@@ -41,6 +41,7 @@ config TAR
     --restrict       All archive contents must extract under one subdirectory
     --numeric-owner  Save/use/display uid and gid, not user/group name
     --no-recursion   Don't store directory contents
+    -I PROG          Filter through PROG to compress or PROG -d to decompress
 */
 
 #define FOR_tar
@@ -49,7 +50,7 @@ config TAR
 GLOBALS(
   char *f, *C;
   struct arg_list *T, *X;
-  char *to_command, *owner, *group, *mtime, *mode;
+  char *I, *to_command, *owner, *group, *mtime, *mode;
   struct arg_list *exclude;
 
   struct double_list *incl, *excl, *seen;
@@ -790,7 +791,7 @@ static void do_XT(char **pline, long len)
 void tar_main(void)
 {
   char *s, **args = toys.optargs,
-    *archiver = FLAG(z) ? "gzip" : (FLAG(J) ? "xz" : "bzip2");
+    *archiver = FLAG(I) ? TT.I : (FLAG(z) ? "gzip" : (FLAG(J) ? "xz":"bzip2"));
   int len = 0;
 
   // Needed when extracting to command
@@ -842,7 +843,7 @@ void tar_main(void)
     char *hdr = 0;
 
     // autodetect compression type when not specified
-    if (!(FLAG(j)||FLAG(z)||FLAG(J))) {
+    if (!(FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J))) {
       len = xread(TT.fd, hdr = toybuf+sizeof(toybuf)-512, 512);
       if (len!=512 || !is_tar_header(hdr)) {
         // detect gzip and bzip signatures
@@ -856,14 +857,14 @@ void tar_main(void)
       }
     }
 
-    if (FLAG(j)||FLAG(z)||FLAG(J)) {
+    if (FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J)) {
       int pipefd[2] = {hdr ? -1 : TT.fd, -1}, i, pid;
-      struct string_list *zcat = find_in_path(getenv("PATH"),
+      struct string_list *zcat = FLAG(I) ? 0 : find_in_path(getenv("PATH"),
         FLAG(j) ? "bzcat" : FLAG(J) ? "xzcat" : "zcat");
 
       // Toybox provides more decompressors than compressors, so try them first
       xpopen_both(zcat ? (char *[]){zcat->str, 0} :
-        (char *[]){archiver, "-dc", 0}, pipefd);
+        (char *[]){archiver, "-d", 0}, pipefd);
       if (CFG_TOYBOX_FREE) llist_traverse(zcat, free);
 
       if (!hdr) {
@@ -920,7 +921,7 @@ void tar_main(void)
     struct double_list *dl = TT.incl;
 
     // autodetect compression type based on -f name. (Use > to avoid.)
-    if (TT.f && !FLAG(j) && !FLAG(z)) {
+    if (TT.f && !FLAG(j) && !FLAG(z) && !FLAG(I) && !FLAG(J)) {
       char *tbz[] = {".tbz", ".tbz2", ".tar.bz", ".tar.bz2"};
       if (strend(TT.f, ".tgz") || strend(TT.f, ".tar.gz"))
         toys.optflags |= FLAG_z;
@@ -930,10 +931,10 @@ void tar_main(void)
         if (strend(TT.f, tbz[len])) toys.optflags |= FLAG_j;
     }
 
-    if (FLAG(j)||FLAG(z)||FLAG(J)) {
+    if (FLAG(j)||FLAG(z)||FLAG(I)||FLAG(J)) {
       int pipefd[2] = {-1, TT.fd};
 
-      xpopen_both((char *[]){archiver, "-f", 0}, pipefd);
+      xpopen_both((char *[]){archiver, 0}, pipefd);
       close(TT.fd);
       TT.fd = pipefd[0];
     }
-- 
2.28.0.297.g1956fa8f8d-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to