Skip to main content
coreydaley.dev

libarchive pads pipe output with NULs, and zstd chokes on them

If you write a compressed tar with libarchive to anything other than a regular file, such as a pipe, stdout, or your own write callbacks, the result can fail to decompress:

Unknown frame descriptor

That’s from the zstd decoder. The zstd command-line tool calls the same file unsupported format, and gzip reads it but complains:

gzip: out.tar.gz: trailing garbage ignored

The data inside is fine. The problem is what comes after it: up to 10 KiB of zero bytes that libarchive appended on purpose.

Reproducing it with the tar on your Mac

The tar that ships with macOS is bsdtar, which is libarchive, so you can see this without writing any code (you’ll need the zstd command-line tool installed):

$ tar --zstd -cf - notes.txt | cat > piped.tar.zst
$ ls -l piped.tar.zst
-rw-r--r--  1 corey  wheel  10240  piped.tar.zst
$ zstd -dc piped.tar.zst > /dev/null
zstd: piped.tar.zst: unsupported format

A 5 KB text file doesn’t compress to exactly 10,240 bytes. Write the same archive with -f piped.tar.zst and it’s about 2 KB. In my testing even tar --zstd -cf - notes.txt > out.tar.zst, redirected straight to a regular file, came out padded to 10,240. The built-in tar couldn’t list its own padded output either: it hands zstd to an external program, and that program failed.

Why libarchive does this

libarchive’s writer comes from the tape-drive era. It writes output in blocks, 10,240 bytes by default (twenty 512-byte tar records), and by default it pads the final block to full size with zeros. Tape drives and some devices need that.

The padding happens at the very end of the pipeline, after compression. For an uncompressed tar it’s harmless, because tar readers expect zero blocks at the end. For a compressed tar, the zeros land after the end of the compressed stream, where the decoder expects either end-of-file or the start of another frame. zstd treats them as a malformed frame and fails, and gzip warns and exits with status 2. xz happens to survive in my test: its format allows zero padding after a stream in multiples of four bytes, and xz output is always four-byte aligned.

Whether you get padding depends on how you opened the writer. Each open function sets the policy only if you haven’t set it yourself:

Open callPads the last block when the output is
archive_write_open_filenamea character device, block device, or FIFO
archive_write_open_fdany of those, or file descriptor 1 (stdout)
archive_write_open (your callbacks)always: it can’t inspect the output, so the default applies

Writing to a path on disk, the most common case, never pads. That’s why this goes unnoticed until you move from paths to pipes or callbacks.

Where I ran into it, twice

The first time was splitting archives into volumes. To write a .tar.zst across several files, Purrchiver’s engine opens libarchive with custom write callbacks that cut the stream into pieces. With callbacks, padding is the default. The last volume ended in zeros, and reading the set back failed with Unknown frame descriptor.

The second time came while building the pieces for streaming encryption. The engine now has a writer that sends a tar straight into a file descriptor, a pipe whose other end is an encryptor, so the unencrypted archive never sits on disk. A pipe is a FIFO, so archive_write_open_fd turns padding on, and every compressed tar written that way would have had zeros at the end.

The fix

Tell libarchive not to pad, before you open:

struct archive *a = archive_write_new();
archive_write_add_filter_zstd(a);
archive_write_set_format_pax_restricted(a);

archive_write_set_bytes_in_last_block(a, 1);   // no padding: round to 1 byte
archive_write_open_fd(a, fd);

bytes_in_last_block is the size the final block is rounded up to. The default of -1 means “let the open call decide,” and 0 means pad the last block to the full block size. 1 rounds to a single byte, which is no padding at all, and it’s what archive_write_open_filename already chooses for a regular file.

I make the call before opening. Each open function checks whether the value is still unset and only applies its own default if it is, so setting it first means the open call leaves your choice alone.

From Swift it’s the same call, made on the handle before opening:

archive_write_set_bytes_in_last_block(handle, 1)
let rc = archive_write_open_fd(handle, fd)

Takeaways

  • If libarchive’s output works from a path and fails through a pipe, look at the last 10,240 bytes. A file size that’s an exact multiple of 10,240 is the giveaway.
  • Set bytes_in_last_block yourself whenever you use callbacks or a file descriptor. Unless you’re writing to a tape, there’s no reason to pad.
  • Test compressed output with the standalone tool, not only with libarchive. A reader that tolerates trailing zeros can hide the problem from you, and zstd -t won’t.

Related posts