[Toybox] tar creating empty archive?
Rob Landley
rob at landley.net
Thu Apr 17 18:28:22 PDT 2025
On 4/17/25 14:28, Hong, Yifan wrote:
> Hello Rob,
>
> Is there a way to create an empty tar archive with toybox? I tried:
>
> $ ./toybox tar czf foo.tar.gz -T /dev/null
> tar: empty archive
So... two terminating 512 byte blocks of zeroes with no other content?
You could do it with something like:
dd if=/dev/zero bs=1k | gzip > foo.tar.gz
I've just never encountered this use case before?
> It seems that this comes from from
> https://github.com/gfto/toybox/blob/75ebbd1571c85a06c0f4767beb7c20a19068f0b6/toys/pending/tar.c#L763
Tar moved out of pending in something like 2018, but the test and error
message still exist on line 1048.
> Background The reason I ask is that I want to do this:
>
> tar czf out.tar.gz -T filelist.txt
>
> But filelist.txt could be empty and I expect it to create an empty archive.
> If toybox can't create empty archive when -T is empty, I have to use the
> following workaround instead (untested pseudocode):
>
> if [ -s filelist.txt ]; then
> tar czf out.tar.gz -T filelist.txt
> else
> gzip < /dev/null > out.tar.gz
> fi
Technically each tar stream is ended with 2 entirely zeroed 512 byte
header blocks, hence the 1k of /dev/zero with dd above. (The gnu/dammit
version tends to add lots of extra zero padding blocks at the end for no
obvious reason, but back when posix had a standard for this it only
REQUIRED two.)
> ... which works, but it is not quite gracefully handled IMO.
>
> As a reference, GNU tar 1.35 does the following:
>
> # This fails
> $ tar czf out.tar.gz
> tar: Cowardly refusing to create an empty archive
>
> # This succeeds
> $ tar czf out.tar.gz -T /dev/null
I didn't know there was a distinction between them.
So you want something like:
--- 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;
}
Rob
More information about the Toybox
mailing list