-
Notifications
You must be signed in to change notification settings - Fork 699
Add container copy/cp command for host-container file transfer #1190
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
simone-panico
wants to merge
17
commits into
apple:main
Choose a base branch
from
simone-panico:main
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.
+768
−11
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6f13061
Add support to Copy Files between Container and Host
simone-panico e8973a2
Merge branch 'main' of https://github.com/simone-panico/container
simone-panico 5f81b12
Add CLICopy Tests
simone-panico dd5e9d3
Merge branch 'apple:main' into main
simone-panico 821d2b6
Update Docs
simone-panico 3170d79
Merge branch 'apple:main' into main
simone-panico 993f396
Refactor file copy methods to use URL types and add container state v…
simone-panico bd2c055
Correct destination path handling in ContainerCopy command
simone-panico 8d91bcc
Add correct destination path handling in ContainerCopy command
simone-panico 4b43d1c
Merge branch 'main' into main
simone-panico fd7899e
Merge branch 'main' into main
simone-panico 5ef7882
Merge branch 'main' into main
JaewonHur e50aa86
Update Package.resolved and fix XPCKeys enum case for statistics
simone-panico e030032
Add Copy Directory Support + Add Copy Directory Tests
simone-panico 0564e14
Add destinationIsDirectory bool
simone-panico 48d9686
Revert Package.swift
simone-panico 4b1a7d6
Refactor copyIn and copyOut methods to use createParents instead of d…
simone-panico 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
122 changes: 122 additions & 0 deletions
122
Sources/ContainerCommands/Container/ContainerCopy.swift
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 |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // Copyright © 2026 Apple Inc. and the container project authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import ArgumentParser | ||
| import ContainerAPIClient | ||
| import ContainerResource | ||
| import Containerization | ||
| import ContainerizationError | ||
| import Foundation | ||
|
|
||
| extension Application { | ||
| public struct ContainerCopy: AsyncLoggableCommand { | ||
| enum PathRef { | ||
| case local(String) | ||
| case container(id: String, path: String) | ||
| } | ||
|
|
||
| static func parsePathRef(_ ref: String) throws -> PathRef { | ||
| let parts = ref.components(separatedBy: ":") | ||
| switch parts.count { | ||
| case 1: | ||
| return .local(ref) | ||
| case 2 where !parts[0].isEmpty && parts[1].starts(with: "/"): | ||
| return .container(id: parts[0], path: parts[1]) | ||
| default: | ||
| throw ContainerizationError(.invalidArgument, message: "invalid path given: \(ref)") | ||
| } | ||
| } | ||
|
|
||
| public init() {} | ||
|
|
||
| public static let configuration = CommandConfiguration( | ||
| commandName: "copy", | ||
| abstract: "Copy files/folders between a container and the local filesystem", | ||
| aliases: ["cp"]) | ||
|
|
||
| @OptionGroup() | ||
| public var logOptions: Flags.Logging | ||
|
|
||
| @Argument(help: "Source path (container:path or local path)") | ||
| var source: String | ||
|
|
||
| @Argument(help: "Destination path (container:path or local path)") | ||
| var destination: String | ||
|
|
||
| public func run() async throws { | ||
| let client = ContainerClient() | ||
| let srcRef = try Self.parsePathRef(source) | ||
| let dstRef = try Self.parsePathRef(destination) | ||
|
|
||
| switch (srcRef, dstRef) { | ||
| case (.container(let id, let path), .local(let localPath)): | ||
| let srcURL = URL(fileURLWithPath: path) | ||
| let destURL = URL(fileURLWithPath: localPath).standardizedFileURL | ||
| var isDirectory: ObjCBool = false | ||
| let exists = FileManager.default.fileExists(atPath: destURL.path, isDirectory: &isDirectory) | ||
|
|
||
| if exists && isDirectory.boolValue { | ||
| let finalDest = destURL.appendingPathComponent(srcURL.lastPathComponent) | ||
| try await client.copyOut(id: id, source: srcURL, destination: finalDest) | ||
| } else if localPath.hasSuffix("/") { | ||
| try await client.copyOut(id: id, source: srcURL, destination: destURL) | ||
| var resultIsDir: ObjCBool = false | ||
| if FileManager.default.fileExists(atPath: destURL.path, isDirectory: &resultIsDir), | ||
| !resultIsDir.boolValue | ||
| { | ||
| try? FileManager.default.removeItem(at: destURL) | ||
| throw ContainerizationError( | ||
| .invalidArgument, | ||
| message: "destination is not a directory: \(localPath)") | ||
| } | ||
| } else { | ||
| try await client.copyOut(id: id, source: srcURL, destination: destURL) | ||
| } | ||
| case (.local(let localPath), .container(let id, let path)): | ||
| let srcURL = URL(fileURLWithPath: localPath).standardizedFileURL | ||
| var isDirectory: ObjCBool = false | ||
| guard FileManager.default.fileExists(atPath: srcURL.path, isDirectory: &isDirectory) else { | ||
| throw ContainerizationError(.notFound, message: "source path does not exist: \(localPath)") | ||
| } | ||
| if localPath.hasSuffix("/") && !isDirectory.boolValue { | ||
| throw ContainerizationError(.invalidArgument, message: "source path is not a directory: \(localPath)") | ||
| } | ||
|
|
||
| let destURL = URL(fileURLWithPath: path) | ||
| let dstWithBasename = destURL.appendingPathComponent(srcURL.lastPathComponent) | ||
| do { | ||
| try await client.copyIn(id: id, source: srcURL, destination: dstWithBasename, createParents: false) | ||
| } catch { | ||
| if isDirectory.boolValue { | ||
| try await client.copyIn(id: id, source: srcURL, destination: destURL) | ||
| } else if path.hasSuffix("/") { | ||
| throw ContainerizationError( | ||
| .invalidArgument, | ||
| message: "destination is not a directory: \(path)") | ||
| } else { | ||
| try await client.copyIn(id: id, source: srcURL, destination: destURL) | ||
| } | ||
| } | ||
| case (.container, .container): | ||
| throw ContainerizationError(.invalidArgument, message: "copying between containers is not supported") | ||
| case (.local, .local): | ||
| throw ContainerizationError( | ||
| .invalidArgument, | ||
| message: "one of source or destination must be a container reference (container_id:path)") | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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
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.