-
Notifications
You must be signed in to change notification settings - Fork 258
Add progress handler for rootfs unpacking #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DePasqualeOrg
wants to merge
13
commits into
apple:main
Choose a base branch
from
DePasqualeOrg:unpacking-progress
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+591
−71
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a1be1ce
Add progress handler for rootfs unpacking
DePasqualeOrg 60d26a0
Add accurate progress calculation
DePasqualeOrg 359437a
Add tests
DePasqualeOrg fbdc035
Fix unpack progress reporting
DePasqualeOrg b416533
Avoid double zstd decompression when progress is enabled
DePasqualeOrg b1cfc86
Simplify overflow handling in totalRegularFileBytes
DePasqualeOrg c8f6875
Add timing benchmark for header scan overhead
DePasqualeOrg 4b1140b
Make unpack methods async for deterministic progress delivery
DePasqualeOrg 1072ce0
Use Int64 consistently for all progress event values
DePasqualeOrg af5682b
Consolidate unpack overloads and add total item count
DePasqualeOrg 9729f46
Format
DePasqualeOrg 33176cb
Various fixes
DePasqualeOrg 0c8bb72
Merge branch 'main' into unpacking-progress
DePasqualeOrg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -25,7 +25,83 @@ private typealias Hardlinks = [FilePath: FilePath] | |||||
|
|
||||||
| extension EXT4.Formatter { | ||||||
| /// Unpack the provided archive on to the ext4 filesystem. | ||||||
| public func unpack(reader: ArchiveReader, progress: ProgressHandler? = nil) throws { | ||||||
| public func unpack(reader: ArchiveReader, progress: ProgressHandler? = nil) async throws { | ||||||
| try await self.unpackEntries(reader: reader, progress: progress) | ||||||
| } | ||||||
|
|
||||||
| /// Unpack an archive at the source URL on to the ext4 filesystem. | ||||||
| public func unpack( | ||||||
dkovba marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| source: URL, | ||||||
| format: ContainerizationArchive.Format = .paxRestricted, | ||||||
| compression: ContainerizationArchive.Filter = .gzip, | ||||||
| progress: ProgressHandler? = nil | ||||||
| ) async throws { | ||||||
| // For zstd, decompress once and reuse for both passes to avoid double decompression. | ||||||
| let fileToRead: URL | ||||||
| let readerFilter: ContainerizationArchive.Filter | ||||||
| var decompressedFile: URL? | ||||||
| if progress != nil && compression == .zstd { | ||||||
| let decompressed = try ArchiveReader.decompressZstd(source) | ||||||
| fileToRead = decompressed | ||||||
| readerFilter = .none | ||||||
| decompressedFile = decompressed | ||||||
| } else { | ||||||
| fileToRead = source | ||||||
| readerFilter = compression | ||||||
| } | ||||||
| defer { | ||||||
| if let decompressedFile { | ||||||
| ArchiveReader.cleanUpDecompressedZstd(decompressedFile) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if let progress { | ||||||
| // First pass: scan headers to get totals (fast, metadata only) | ||||||
| let totals = try Self.scanArchiveHeaders(format: format, filter: readerFilter, file: fileToRead) | ||||||
| var totalEvents: [ProgressEvent] = [] | ||||||
| if totals.size > 0 { | ||||||
| totalEvents.append(ProgressEvent(event: "add-total-size", value: totals.size)) | ||||||
| } | ||||||
| if totals.items > 0 { | ||||||
| totalEvents.append(ProgressEvent(event: "add-total-items", value: totals.items)) | ||||||
| } | ||||||
| if !totalEvents.isEmpty { | ||||||
| await progress(totalEvents) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Unpack pass | ||||||
| let reader = try ArchiveReader( | ||||||
| format: format, | ||||||
| filter: readerFilter, | ||||||
| file: fileToRead | ||||||
| ) | ||||||
| try await self.unpackEntries(reader: reader, progress: progress) | ||||||
| } | ||||||
|
|
||||||
| /// Scan archive headers to count the total number of bytes in regular files | ||||||
| /// and the total number of entries. | ||||||
| public static func scanArchiveHeaders( | ||||||
| format: ContainerizationArchive.Format, | ||||||
| filter: ContainerizationArchive.Filter, | ||||||
| file: URL | ||||||
| ) throws -> (size: Int64, items: Int) { | ||||||
| let reader = try ArchiveReader(format: format, filter: filter, file: file) | ||||||
| var totalSize: Int64 = 0 | ||||||
| var totalItems: Int = 0 | ||||||
| for (entry, _) in reader.makeStreamingIterator() { | ||||||
| try Task.checkCancellation() | ||||||
| guard entry.path != nil else { continue } | ||||||
| totalItems += 1 | ||||||
| if entry.fileType == .regular, let size = entry.size { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems we don't emit |
||||||
| totalSize += Int64(size) | ||||||
| } | ||||||
| } | ||||||
| return (size: totalSize, items: totalItems) | ||||||
| } | ||||||
|
|
||||||
| /// Core unpack logic. When `progress` is nil the handler calls are skipped. | ||||||
| private func unpackEntries(reader: ArchiveReader, progress: ProgressHandler?) async throws { | ||||||
| var hardlinks: Hardlinks = [:] | ||||||
| // Allocate a single 128KiB reusable buffer for all files to minimize allocations | ||||||
| // and reduce the number of read calls to libarchive. | ||||||
|
|
@@ -39,35 +115,33 @@ extension EXT4.Formatter { | |||||
| continue | ||||||
| } | ||||||
|
|
||||||
| defer { | ||||||
| // Count the number of entries | ||||||
| if let progress { | ||||||
| Task { | ||||||
| await progress([ | ||||||
| ProgressEvent(event: "add-items", value: 1) | ||||||
| ]) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| pathEntry = preProcessPath(s: pathEntry) | ||||||
| let path = FilePath(pathEntry) | ||||||
|
|
||||||
| if path.base.hasPrefix(".wh.") { | ||||||
| if path.base == ".wh..wh..opq" { // whiteout directory | ||||||
| try self.unlink(path: path.dir, directoryWhiteout: true) | ||||||
| if let progress { | ||||||
| await progress([ProgressEvent(event: "add-items", value: 1)]) | ||||||
| } | ||||||
| continue | ||||||
| } | ||||||
| let startIndex = path.base.index(path.base.startIndex, offsetBy: ".wh.".count) | ||||||
| let filePath = String(path.base[startIndex...]) | ||||||
| let dir: FilePath = path.dir | ||||||
| try self.unlink(path: dir.join(filePath)) | ||||||
| if let progress { | ||||||
| await progress([ProgressEvent(event: "add-items", value: 1)]) | ||||||
| } | ||||||
| continue | ||||||
| } | ||||||
|
|
||||||
| if let hardlink = entry.hardlink { | ||||||
| let hl = preProcessPath(s: hardlink) | ||||||
| hardlinks[path] = FilePath(hl) | ||||||
| if let progress { | ||||||
| await progress([ProgressEvent(event: "add-items", value: 1)]) | ||||||
| } | ||||||
| continue | ||||||
| } | ||||||
| let ts = FileTimestamps( | ||||||
|
|
@@ -84,13 +158,8 @@ extension EXT4.Formatter { | |||||
| uid: entry.owner, | ||||||
| gid: entry.group, xattrs: entry.xattrs, fileBuffer: reusableBuffer) | ||||||
|
|
||||||
| // Count the size of files | ||||||
| if let progress, let size = entry.size { | ||||||
| Task { | ||||||
| await progress([ | ||||||
| ProgressEvent(event: "add-size", value: Int64(size)) | ||||||
| ]) | ||||||
| } | ||||||
| await progress([ProgressEvent(event: "add-size", value: Int64(size))]) | ||||||
| } | ||||||
| case .symbolicLink: | ||||||
| var symlinkTarget: FilePath? | ||||||
|
|
@@ -102,8 +171,15 @@ extension EXT4.Formatter { | |||||
| uid: entry.owner, | ||||||
| gid: entry.group, xattrs: entry.xattrs) | ||||||
| default: | ||||||
| if let progress { | ||||||
| await progress([ProgressEvent(event: "add-items", value: 1)]) | ||||||
| } | ||||||
| continue | ||||||
| } | ||||||
|
|
||||||
| if let progress { | ||||||
| await progress([ProgressEvent(event: "add-items", value: 1)]) | ||||||
| } | ||||||
| } | ||||||
| guard hardlinks.acyclic else { | ||||||
| throw UnpackError.circularLinks | ||||||
|
|
@@ -115,21 +191,6 @@ extension EXT4.Formatter { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Unpack an archive at the source URL on to the ext4 filesystem. | ||||||
| public func unpack( | ||||||
| source: URL, | ||||||
| format: ContainerizationArchive.Format = .paxRestricted, | ||||||
| compression: ContainerizationArchive.Filter = .gzip, | ||||||
| progress: ProgressHandler? = nil | ||||||
| ) throws { | ||||||
| let reader = try ArchiveReader( | ||||||
| format: format, | ||||||
| filter: compression, | ||||||
| file: source | ||||||
| ) | ||||||
| try self.unpack(reader: reader, progress: progress) | ||||||
| } | ||||||
|
|
||||||
| private func preProcessPath(s: String) -> String { | ||||||
| var p = s | ||||||
| if p.hasPrefix("./") { | ||||||
|
|
||||||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.