Repository navigation
core: add File.extract to extract archives into directories - #14846
Merged
Merged
Conversation
sipsma
marked this pull request as ready for review
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
force-pushed
the
sipsma/file-extract
branch
from
October 26, 2026 18:19
224b097 to
4300d43
Compare
tiborvass
approved these changes
Oct 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
File.extract, which extracts an archive file into aDirectorywithout running a container. Fixes #13353, following the direction agreed there (and the review of #13354, the earlier attempt).Behaviour
stripComponentsremoves the first elements of each entry's path as stored,.included, astar --strip-componentsdoes. Entries with no more elements than that are skipped. Hard link targets in a tar are stripped too./is dropped, as tar does. An entry or hard link whose path would leave the directory is an error.chrootarchive.Untar, the unpackerDirectory.withDirectory's Dockerfile ADD compatibility already uses. Its ownership, xattr and special-file handling applies, after the same path checks and stripping as zips.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 downloadfetches 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:
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:
./cmd/dagger), 254 module zips.File.extractFile.extractEvery build with
File.extractwas 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:stripComponents, including.elements and leading/;TestLazyOperationCodecscovers the new lazy operation's persistence.Integration (
core/integration/file_extract_test.go, inTestFile):stripComponentsand its validation;Remote cache (
core/integration/remote_cache_extract_test.go, inTestRemoteCacheTransferSuite): 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:
TestFilein the base shard,TestRemoteCacheTransferSuitein the base-extra shard, and the core unit tests in the packages shard.