Skip to content

core: add File.extract to extract archives into directories - #14846

Merged
sipsma merged 4 commits into
mainfrom
sipsma/file-extract
Oct 26, 2026
Merged

sipsma merged 4 commits into
mainfrom
sipsma/file-extract

Conversation

@sipsma

@sipsma sipsma commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Adds File.extract, which extracts an archive file into a Directory without running a container. Fixes #13353, following the direction agreed there (and the review of #13354, the earlier attempt).

extract(stripComponents: Int = 0): Directory!
src := dag.HTTP("https://example.com/pkg-1.0.tar.gz").Extract(dagger.FileExtractOpts{StripComponents: 1})

Behaviour

  • Formats, detected from the file's first bytes: zip, or tar, either uncompressed or compressed with gzip, bzip2, xz or zstd. A file that is neither is an error ("not a zip or tar archive"). An empty zip or tar gives an empty directory.
  • stripComponents removes the first elements of each entry's path as stored, . included, as tar --strip-components does. Entries with no more elements than that are skipped. Hard link targets in a tar are stripped too.
  • Paths: a leading / is dropped, as tar does. An entry or hard link whose path would leave the directory is an error.
  • Zip specifics:
    • Directories are made first, then regular files, then symlinks, so no entry is ever written through a symlink the archive itself makes.
    • Permissions and modification times are kept where the zip records them (Unix and macOS creators). Files with none recorded get 0644, directories 0755.
    • A later entry with the same path replaces an earlier one.
    • Device and FIFO entries are an error.
  • Tars are unpacked with chrootarchive.Untar, the unpacker Directory.withDirectory's Dockerfile ADD compatibility already uses. Its ownership, xattr and special-file handling applies, after the same path checks and stripping as zips.
  • Cancellation stops an extraction partway through an entry. A failed extraction releases the snapshot it was writing.
  • Caching: the field is persistable and returns an ordinary snapshot-backed Directory. Extracting archives with the same content, from different recipes, gives the same result. Another engine can pull the result through the remote cache without extracting again.

Choosing the zip implementation

Inputs: the 254 module zips go mod download fetches for dagger's CLI (./cmd/dagger), 300 MB in total. The median is 55 KB; the largest four are 39–49 MB (cloudflare/circl, google.golang.org/api, modernc.org/sqlite, klauspost/compress).

Setup:

  • Each implementation extracted every zip into an empty directory on a Dagger cache volume, the same kind of bind-mounted snapshot directory the engine writes into, in a Wolfi container on a 16-core host.
  • 5 rounds, with the implementations' order rotated each round; medians reported.
  • "One at a time" extracts the zips sequentially; "16 at a time" runs 16 extractions at once.
  • CPU is user plus system time of the process and its children.
  • All implementations produced identical file contents.
implementation one at a time CPU 16 at a time CPU largest zip median small zip
klauspost/compress/zip, 8 entries at once (this PR) 1.89 s 8.66 s 0.84 s 7.54 s 154 ms 1.1 ms
archive/zip, 8 entries at once 2.22 s 10.31 s 0.86 s 8.75 s 195 ms 1.2 ms
klauspost/compress/zip, 4 entries at once 2.65 s 8.65 s 0.85 s 7.69 s 277 ms 1.4 ms
bsdtar (libarchive) 5.92 s 5.88 s 1.06 s 5.88 s 739 ms 5.0 ms
7zz (7-Zip) 6.38 s 6.42 s 1.16 s 6.51 s 936 ms 4.9 ms
klauspost/compress/zip, sequential 7.52 s 9.14 s 1.44 s 7.25 s 1045 ms 3.2 ms
unzip (Info-ZIP) 8.19 s 8.24 s 1.75 s 8.17 s 1530 ms 4.7 ms
archive/zip, sequential 8.31 s 9.89 s 1.72 s 8.32 s 1183 ms 3.2 ms

Writing entries in parallel is what cuts wall time. klauspost's inflate cuts CPU against the standard library's, and klauspost/compress is already an engine dependency.

bsdtar uses the least CPU, but it starts a process per archive and decompresses one entry at a time: a median small zip takes 5.0 ms against 1.1 ms, and the largest 739 ms against 154 ms. It would also be a new binary in the engine image.

Reading zips through mmap instead of pread made no difference.

In a real build

The first user is the per-package Go build in the dagger.io/go module, in a follow-up to dagger/go#74 (which made it build one cached exec per package) that also builds the standard library and dependencies package by package. That follow-up extracts every dependency module's zip, taken from the build's own verified download, into its own cached directory. Today it does that with one container exec per module, running a small unzip helper. The comparison below replaces only that exec with File.extract.

Setup:

  • First build of dagger's CLI (./cmd/dagger), 254 module zips.
  • Each build on its own fresh dev engine, on an arm64 Linux VM, in the order without, with, with, without, without, with.
  • Both arms use the same engine: v1.0.0-beta.20 plus this change, plus some unrelated in-progress snapshot performance changes.
build without File.extract with File.extract
1 34.07 s 32.73 s
2 37.21 s 33.00 s
3 33.98 s 31.98 s
median 34.07 s 32.73 s
execs 1,670 1,416

Every build with File.extract was faster than every build without it. The build runs 254 fewer execs, one per module zip.

Tests

  • core unit tests (core/file_extract_test.go), each of them observed failing with its fix removed:

    • zip modes, times and symlinks;
    • stripComponents, including . elements and leading /;
    • the tar stream handed to the unpacker: stripped names and hard link targets, and dropped entries;
    • duplicate entries;
    • path escapes in zip and tar, including hard links;
    • zip-slip through the archive's own symlink;
    • a bounded read of symlink targets;
    • unsupported entry types;
    • empty archives;
    • non-archives;
    • cancellation in zip and tar;
    • release of the snapshot on failure.

    TestLazyOperationCodecs covers the new lazy operation's persistence.

  • Integration (core/integration/file_extract_test.go, in TestFile):

    • zip with modes, times and symlinks;
    • tar.gz with and without stripping, including a hard link and a symlink;
    • stripComponents and its validation;
    • path escapes;
    • empty archives and non-archives;
    • a large archive (64 MiB plus 5,000 files);
    • caching of the same archive from different recipes.
  • Remote cache (core/integration/remote_cache_extract_test.go, in TestRemoteCacheTransferSuite): a directory extracted on one engine is transferred to a second engine. The second engine installs the downloaded snapshot and never runs the extraction.

The new tests run in the existing CI shards: TestFile in the base shard, TestRemoteCacheTransferSuite in the base-extra shard, and the core unit tests in the packages shard.

@sipsma
sipsma marked this pull request as ready for review October 23, 2026 21:28
@sipsma
sipsma requested review from a team as code owners October 23, 2026 21:28
File.extract(stripComponents) extracts an archive file into a new
directory without running a container, as proposed in #13353. The
format is detected from the file's first bytes: zip, or tar, either
uncompressed or compressed with gzip, bzip2, xz or zstd.

Zips are extracted in the engine with klauspost/compress, already an
engine dependency, writing up to 8 entries at once. On the 254 module
zips dagger's CLI build downloads (300 MB), that took 1.9 s one zip at a
time, against 8.3 s for archive/zip, 5.9 s for bsdtar, 6.4 s for 7zz
and 8.2 s for unzip.

Directories are made first, then files, then symlinks, so no entry is
written through a symlink the archive itself makes. Permissions and
modification times are kept where the zip records them; files with
none recorded get 0644 and directories 0755. Tars go through the
unpacker Directory.withDirectory's Dockerfile ADD compatibility already
uses, after the same path checks and stripping. For both, a leading
"/" is dropped from entry paths, and an entry or hard link whose path
leaves the directory is an error.

The result is an ordinary persistable Directory: an extraction of the
same archive is cached, and another engine can pull it through the
remote cache without extracting again.

Signed-off-by: Erik Sipsma <[email protected]>
Four fixes to File.extract, each with a test:

- stripComponents removes the first elements of each entry's name as
  stored, "." included, before cleaning it, as tar --strip-components
  does: "./top/f" with stripComponents 1 extracts to "top/f", where it
  extracted to "f".
- Canceling stops an extraction in the middle of an entry: zip entries
  and tar streams are read through a reader that checks the context.
- A failed extraction releases the snapshot it was writing, as
  File's other snapshot-creating operations do.
- A zip entry that records Unix permissions 000 keeps them; only an
  entry with no Unix mode recorded gets the 0644 or 0755 default.

Signed-off-by: Erik Sipsma <[email protected]>
CI runs the core package's tests unprivileged. There the unpacker
File.extract uses for tars can't create the mount namespace it chroots
in, so the unit tests that unpacked a tar failed with "unshare:
operation not permitted".

The unit tests now check what File.extract does to a tar before it is
unpacked: the stream stripTarComponents writes, with names and hard link
targets stripped and emptied entries dropped. The integration test,
which unpacks in the engine, now also covers a hard link and a symlink
in a stripped tar.

Signed-off-by: Erik Sipsma <[email protected]>
gosec's G110 flags the copy of each tar entry as a possible
decompression bomb. Extraction writes every entry in full, as it does
for zips, so a cap would only truncate large entries. Suppress the
finding the way the tar converter in internal/buildkit already does for
the same copy.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/file-extract branch from 224b097 to 4300d43 Compare October 26, 2026 18:19
@sipsma
sipsma merged commit 7bd17c3 into main Oct 26, 2026
92 checks passed
@sipsma
sipsma deleted the sipsma/file-extract branch October 26, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add API to extract archive File objects into directories

2 participants