From 66fabe6c3101bf3cc882cedbb1101027eefc914a Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 18 Apr 2025 11:08:03 -0500 Subject: [PATCH] Allow tar -T /dev/null to create an empty archive. --- tests/tar.test | 2 ++ toys/posix/tar.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/tar.test b/tests/tar.test index 871a38d7..60ec9808 100755 --- a/tests/tar.test +++ b/tests/tar.test @@ -43,6 +43,8 @@ testing "-T newline" "$TAR -T input | LST" \ "-rw-rw-r-- root/sys 0 2009-02-13 23:31 file1\n-rw-rw-r-- root/sys 0 2009-02-13 23:31 file2\n" "file1\nfile2\n" "" testing "-T null" "$TAR --null -T input | LST" \ "-rw-rw-r-- root/sys 0 2009-02-13 23:31 file1\n-rw-rw-r-- root/sys 0 2009-02-13 23:31 file2\n" "file1\0file2\0" "" +testing "empty -T creates empty tarball" "tar c | SUM 2" \ + "da39a3ee5e6b4b0d3255bfef95601890afd80709\n" "" "" # User "root" is UID 0 and group "sys" is GID 3 (on Linux, BSD, and Mac), # inherited from Bell Labs Unix v7 diff --git a/toys/posix/tar.c b/toys/posix/tar.c index 06482c16..bcfb8ae9 100644 --- a/toys/posix/tar.c +++ b/toys/posix/tar.c @@ -1045,7 +1045,7 @@ void tar_main(void) // If include file list empty, don't create empty archive if (FLAG(c)) { - if (!TT.incl) error_exit("empty archive"); + if (!TT.incl && !FLAG(T)) error_exit("empty archive"); TT.fd = 1; } @@ -1194,7 +1194,7 @@ void tar_main(void) close(TT.fd); TT.fd = pipefd[0]; } - do { + if (dl) do { TT.warn = 1; dirtree_flagread(dl->data, DIRTREE_SYMFOLLOW*FLAG(h)|DIRTREE_BREADTH*(FLAG(sort)|FLAG(s)), -- 2.39.5