56 Commits

Author SHA1 Message Date
Kameron Kilchrist 450d44ecb6 perf: index EXT4 FileTree children by name to avoid O(n^2) unpack (#793)
- `FileTree.lookup` resolved each path component by linearly scanning the
  node's `children` array. This changes the node's child storage to an
  `OrderedDictionary<String, Ptr<FileTreeNode>>` (from swift-collections,
  which is already a package dependency) keyed by name, so `lookup`
  resolves each component in O(1) while iteration keeps the existing
  insertion order.
- Little or no difference in unpack time for images with ~10k files, significant
  improvement for images with ~100k files or more.
2026-07-23 14:34:28 -07:00
Malte Poll d992a1996d fix(EXT4): create missing parent dirs for hardlinks during unpack (#777)
When unpacking an OCI/tar layer, create() already creates missing parent
directories recursively, so regular files and symlinks with absent
parent entries unpack correctly. link() did not, so a hardlink whose
parent directory had no explicit archive entry failed with "<path> not
found" (e.g. images produced by Bazel rules_img). Mirror create()'s
implicit parent creation in link() so such layers unpack, matching
Docker/containerd.

Adds a direct link() unit test and an end-to-end unpack regression test
covering a hardlink, regular file, and symlink with no explicit parent.

Fixes https://github.com/apple/container/issues/1797
2026-06-24 10:31:07 -07:00
Sidhartha Mani df125a2300 Prevent recursion attacks in EXT4Formatter.unlink (#415)
This issue doesn't affect any of our existing products. This is a
preemptive fix for downstream consumers of EXT4.format where, in some
platforms, leading `//` in the path could get resolved into a FileTree
that looks like this

```sh
/ 
 └──  / 
 └── usr
```
2026-04-30 12:01:32 -07:00
J Logan f1ee6f8b73 Adds journal modes to EXT4.Formatter. (#689)
Release containerization / deployDocs (push) Has been cancelled
Release containerization / Publish release (push) Has been cancelled
Release containerization / containerization (push) Successful in 1s
- Closes #671.
- Adds optional journal parameter to `EXT4.Formatter.init()`, with nil
default specifying the current no-journal filesystem configuration.
Otherwise the parameter value contains the journal size and mode.
- `minDiskSize` parameter for formatter init specifies the minimum
usable capacity of the resulting filesystem. The resulting disk image
grows past this value to accommodate the filesystem on-disk structures,
including the journal if specified.
2026-04-21 12:25:09 -07:00
Danny Canter 8908ffc5d1 EXT4: Rework Ptr (#687) 2026-04-14 20:17:46 -07:00
Dmitry Kovba 8c53f4d526 Restore a configurable block size in the EXT4 formatter (#679)
Restores a configurable block size in the EXT4 formatter. 

https://github.com/apple/containerization/pull/662 resolved a conflict
between the hardcoded log block size and a user-provided block size, but
removed the later. This PR restores the configurable block size without
creating the conflict.
2026-04-10 14:04:20 -07:00
Dmitry Kovba 250546f185 Fix the hardlink count decrement threshold and unlink cleanup (#610)
- Fixes a hardlink count decrement threshold
- Fixes unlinking not freeing the first inode

Resolves the failing added tests:

```
✘ Test hardlinkLinksCount() recorded an issue at TestEXT4Format+Link.swift:43:9: Expectation failed: try EXT4.EXT4Reader(blockDevice: afterUnlink).stat("/original").inode.linksCount == 1
✘ Test hardlinkLinksCount() failed after 0.016 seconds with 1 issue.
```

```
✘ Test unlinkFirstInodeFreesInode() recorded an issue at TestEXT4Format+Link.swift:58:9: Expectation failed: try EXT4.EXT4Reader(blockDevice: path).superBlock.freeInodesCount == EXT4.EXT4Reader(blockDevice: emptyPath).superBlock.freeInodesCount
✘ Test unlinkFirstInodeFreesInode() failed after 0.014 seconds with 1 issue.
```
2026-04-09 15:28:35 -04:00
Dmitry Kovba 2c4012e4e8 Derive the block size from the log block size (#662)
Derives the block size from the log block size.
2026-04-09 15:26:23 -04:00
Dmitry Kovba 3a84f98821 Fix the xattr read loop bounds (#658)
Fixes the `xattr` read loop bounds. Resolves the failing added tests:

> ✘ Test lastXattrNotDroppedAtBufferBoundary() recorded an issue at
TestEXT4ExtendedAttributes.swift:72:13: Expectation failed: (attrs.count
→ 0) == 1
> ✘ Test lastXattrNotDroppedAtBufferBoundary() failed after 0.001
seconds with 1 issue.

> Swift/Array.swift:430: Fatal error: Array index is out of range
2026-04-09 14:09:55 -04:00
Dmitry Kovba b2be8dec67 Remove the incorrect check for visited inodes (#659)
Removes the incorrect check for visited inodes. Fixes the added failing
tests:

> ✘ Test sameAbsoluteSymlinkFollowedTwice() recorded an issue at
TestEXT4Reader+IO.swift:502:6: Caught error: symlink loop while
resolving: target/../symlink/file.txt
> ✘ Test sameAbsoluteSymlinkFollowedTwice() failed after 0.009 seconds
with 1 issue.

> ✘ Test sameRelativeSymlinkFollowedTwice() recorded an issue at
TestEXT4Reader+IO.swift:516:6: Caught error: symlink loop while
resolving: ../target/../symlink/file.txt
> ✘ Test sameRelativeSymlinkFollowedTwice() failed after 0.010 seconds
with 1 issue.
2026-04-09 14:09:07 -04:00
Dmitry Kovba d8ec470b39 Throw an error for non-ASCII xattr names (#657)
Throws an error for non-ASCII xattr names on read instead of crashing at
force-unwrapping.
2026-04-09 13:59:24 -04:00
Dmitry Kovba 757885052b Crash instead of silently truncate on a type overflow (#663)
Crash instead of silently truncate on a type overflow. This change has
no real effect and only improves the code quality.
2026-04-09 13:56:19 -04:00
Dmitry Kovba 74ac944790 Match the byte order in the EXT4 reader to how the EXT4 formatter writes data (#664)
Matches the byte order in the EXT4 reader to how the EXT4 formatter
writes data.
2026-04-09 13:55:27 -04:00
Dmitry Kovba adce496396 Trim null bytes from the symlink target string (#618)
Trims null bytes from the symlink target string.
2026-04-09 13:19:51 -04:00
Dmitry Kovba 340c31d1d9 EXT4: Fix the construction of the path (#666)
Fixes the construction of the path and removed unnecessary code. Fixes
the added failing test:

> ✘ Test fileTreeNodePathWithAbsoluteRoot() recorded an issue at
TestEXT4Reader+IO.swift:600:9: Expectation failed: (dirPtr.pointee.path
→ /) == (FilePath("/dir") → /dir)
> ✘ Test fileTreeNodePathWithAbsoluteRoot() recorded an issue at
TestEXT4Reader+IO.swift:601:9: Expectation failed: (filePtr.pointee.path
→ /) == (FilePath("/dir/file") → /dir/file)
> ✘ Test fileTreeNodePathWithAbsoluteRoot() failed after 0.001 seconds
with 2 issues.
2026-04-08 22:32:38 -07:00
Dmitry Kovba f6e7099cf1 Fix the minimum disk size check comparing bytes to blocks (#670)
Fixes the minimum disk size check comparing bytes to blocks and reduces
the code duplication.
2026-04-08 20:43:59 -07:00
Dmitry Kovba bc287a0f1c Fix the SuperBlock reserved block count field names (#668)
Fixes the `SuperBlock` reserved block count field names.
2026-04-08 20:11:50 -07:00
Dmitry Kovba 4c5c731a99 Fix typos in the code and comments (#673)
Fixes typos in the code and comments.
2026-04-08 20:11:41 -07:00
Dmitry Kovba 50560a33cb Remove unused, duplicated, and outdated private methods (#667)
Removes unused, duplicated, and outdated private methods.
2026-04-08 14:45:18 -07:00
Dmitry Kovba 744a7174c6 Remove not used code in the EXT4 formatter (#656)
Removes not used code in the EXT4 formatter.
2026-04-08 14:45:02 -07:00
Dmitry Kovba 56916452e9 Fix the uid/gid truncation to 16 bits (#636)
Release containerization / deployDocs (push) Has been cancelled
Release containerization / Publish release (push) Has been cancelled
Release containerization / containerization (push) Successful in 1s
Fixes the uid/gid truncation to 16 bits.
2026-04-03 19:07:30 -07:00
Dmitry Kovba 33afe9974f Remove the unreachable partial last block group handling in the EXT4 formatter (#640)
Fixes the last block group handling in the EXT4 formatter.
2026-04-03 19:07:17 -07:00
Dmitry Kovba 0273d0dcff Fix a force-unwrap crash on the last leaf of an extent tree (#639)
Fixes a force-unwrap crash on the last leaf of an extent tree:

> ContainerizationEXT4/EXT4+Formatter.swift:1148: Fatal error:
Unexpectedly found nil while unwrapping an Optional value
2026-04-03 19:06:50 -07:00
Dmitry Kovba 587339ae63 Add a guard against an empty range in the EXT4 formatter (#641)
Adds a guard against an empty range in the EXT4 formatter to prevent a
possible crash:

> Swift/arm64e-apple-macos.swiftinterface:6314: Fatal error: Range
requires lowerBound <= upperBound
2026-04-03 19:06:22 -07:00
Dmitry Kovba 586385fb15 Remove not used code which had a buffer overread (#629)
Removes not used `FilePath.init?(Data)` which had a buffer overread. It
used `String(cString:)` which reads until `\0`, but `Data` is not
null-terminated.
2026-04-03 17:07:43 -04:00
Dmitry Kovba b36c63c0b1 Skip deleted directory entries instead of breaking (#637)
Skips deleted directory entries instead of breaking.
2026-04-03 17:06:47 -04:00
Dmitry Kovba 79427211dc Fix a retain cycle in the FileTreeNode class (#638)
Fixes a retain cycle in the `FileTreeNode` class.
2026-04-03 17:01:24 -04:00
Anthony 35f97f225d Add progress handler for rootfs unpacking (#515)
I've added an optional progress handler for rootfs unpacking so that
consumers can show progress. The Ubuntu image takes about 8 seconds to
unpack on my machine, and I'm developing an app where it would be useful
to show progress in the UI.

Total size is determined in an optional first pass that scans archive
headers. Bytes written are then reported during unpacking. The optional
first pass adds 15 (Alpine) to 115 (Ubuntu) ms to unpacking duration on
my machine, depending on image size.
2026-04-02 17:33:55 -07:00
Danny Canter aa6f2c5d6c ContainerizationExt4: Remove unneeded os guards (#626)
There was some platform (darwin) specific guards in this package that
didn't make sense.
2026-04-01 15:07:26 -04:00
Dmitry Kovba d46f6ec92f Fix a range subscript when reading an xattr header (#612)
Fixes a range subscript when reading an `xattr` header.
2026-04-01 09:06:39 -07:00
Dmitry Kovba c28d3f1ba9 Fix a hardlink cycle detection (#611)
Fixes a hardlink cycle detection.
2026-03-30 19:02:54 -07:00
Danny Canter c5c1500eac Remove some unneeded Foundation imports (#613) 2026-03-30 17:00:58 -07:00
Dmitry Kovba 15bbddcaeb Fix the xattr sort comparator (#605)
- Closes #603.
- Fixes the `xattr` sort comparator. For example, when `$0.index >
$1.index` and `$0.name.count < $1.name.count`.
2026-03-30 13:03:37 -07:00
Dmitry Kovba bed3705dbb Remove a double-counted offset in large files (#597)
* Removes a double-counted offset in large files. 
* This is the same fix as was suggested previously in 
https://github.com/apple/containerization/pull/462.

Co-authored-by: Ronit Sabhaya <ronitsabhaya75@gmail.com>
2026-03-30 09:50:43 -07:00
Dmitry Kovba 59f0a046f2 Fix date and time bugs in EXT4 (#592)
- Fixes EXT4 timestamp encoding for pre-1970 dates
- Fixes EXT4 timestamp decoding for pre-1970 dates
- Fixes the creation date
- Fixes a `UInt32` overflow

Resolves the following failures in the added tests:
```
Swift/arm64e-apple-macos.swiftinterface:38198: Fatal error: Double value cannot be converted to UInt64 because the result would be less than UInt64.min
error: Process '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/libexec/swift/pm/swiftpm-testing-helper --test-bundle-path /Users/Dmitry/Apple/containerization/.build/arm64-apple-macosx/debug/containerizationPackageTests.xctest/Contents/MacOS/containerizationPackageTests --filter encodeNegativeTimestamp /Users/Dmitry/Apple/containerization/.build/arm64-apple-macosx/debug/containerizationPackageTests.xctest/Contents/MacOS/containerizationPackageTests --testing-library swift-testing' exited with unexpected signal code 5
```

```
✘ Test decodeNegativeTimestamp() recorded an issue at TestEXT4Format+Create.swift:100:6: Caught error: not a valid EXT4 superblock
✘ Test decodeNegativeTimestamp() failed after 0.003 seconds with 1 issue.
✘ Suite NegativeTimestampRoundtripTests failed after 0.004 seconds with 1 issue.
✘ Test run with 1 test in 1 suite failed after 0.004 seconds with 1 issue.
```
2026-03-25 02:25:19 -07:00
Dmitry Kovba 8df7ce78d7 Enforce progress event value types (#579)
Enforces progress event value types.
2026-03-17 09:23:45 -07:00
Stuart Lang c8ddc8a826 Make EXT4.Inode properties public (#500)
## Summary

The `EXT4.Inode` struct is public but its properties were internal,
preventing external consumers from reading inode metadata. This change
makes the properties public to enable use cases like reading file
metadata (mode, uid, gid, timestamps) from ext4 filesystems.

Motivating use case: https://github.com/socktainer/socktainer/pull/169

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-28 13:53:20 -08:00
Kathryn Baldauf ec2ee3e94d Update license header on all files to include the current year (#470)
Related to https://github.com/apple/container/pull/1024

Signed-off-by: Kathryn Baldauf <k_baldauf@apple.com>
2026-01-05 13:08:48 -08:00
Dmitry Kovba bb0cd39177 Lowercase error messages (#440)
For consistency, all error messages are lowercased.
2025-12-09 05:38:48 -08:00
Danny Canter d0b43df9b5 EXT4: Remove advertising inline data (#438) 2025-12-06 13:45:36 -08:00
Danny Canter 8b39713a00 EXT4: Optimize unpack (#378)
Optimize unpack a little by trying to reduce allocations in the hot
path. Today for every file we read the entire file into memory and then
pass the data blob to the ext4 writer to eventually be written to the
sparse file. Before being written to the sparse file the data is copied
*again* to a temp buffer before finally hitting write(2) in FileHandle.

This change moves things around such that we can pass an optional buffer
to the ext4 create() (so we can reuse a buffer for file writes), as well
as stops reading entire files into memory by passing the archive entry
itself (wrapped in a ReaderStream object albeit) down to the writer.

Testing with unpacking every platform for
`docker.io/jenkins/jenkins:lts` on an M1 Max:

Old Avg (5 runs): 7.43s
New Avg (5 runs): 5.31s
2025-11-03 12:22:38 -08:00
J Logan 995a231348 Removes "all rights reserved" from license header. (#309) 2025-10-03 13:27:59 -07:00
Sidhartha Mani fe227c805e Add IO utilities for traversing dirs and reading files from EXT4 devices (#285) 2025-09-18 09:03:26 -07:00
Danny Canter 1992cfe779 Containerization: Reduce allocations for image subsystems (#152)
Continue the allocations journey for anything that is in the codepaths
for pulling images. This time there's a couple spots in archive and ext4
we can get rid of some copies.
2025-06-18 16:11:46 -07:00
Dmitry Kovba b14395515c Fix warnings in make docs (#139)
This PR also has additional small improvements.
2025-06-16 17:22:22 -07:00
Josh Soref c00ede68c1 fix comment misspellings (#131)
This PR corrects misspellings identified by the [check-spelling
action](https://github.com/marketplace/actions/check-spelling)

The misspellings have been reported at
https://github.com/jsoref/containerization/actions/runs/15662940240/attempts/1#summary-44123291170
The action reports that the changes in this PR would make it happy:
https://github.com/jsoref/containerization/actions/runs/15662940315/attempts/1#summary-44123291367

---------

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2025-06-16 16:06:37 -04:00
Danny Canter 90dfc9d90e EXT4: Adjustments to file names and visibilities (#112)
- FileTimestamps constructor that actually let you provide values was
not public, so it wasn't possible to pass in values other than nil. This
change makes the other constructor and the underlying fields public.
- Rename EXT4+Format to EXT4+Formatter
- Rename EXT4+Export to EXT4Reader+Export
- Make the superblock publicly accessible in the reader like the docs
for the product states.
2025-06-12 23:32:45 -07:00
Michael Crosby 5d2d7a1bc3 update license header removing new line (#99)
Fixes #63

Signed-off-by: crosbymichael <michael_crosby@apple.com>
2025-06-12 09:57:35 -04:00
Seyed Mojtaba Hosseini Zeidabadi 6bb3d1ad1f Fix: extended in throughout the code (#92)
- Corrected the typo in the method name (`Extened` → `Extended`)
 - Updated all references to reflect the corrected spelling
- Introduced a `@available(*, deprecated)` alias for the original method
to maintain backward compatibility, as it was part of the public API
2025-06-11 12:49:23 -07:00
Noritaka Kobayashi 4181e50775 refactor: fix typos (#64)
fix typos
2025-06-10 08:19:50 -07:00