Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6f13061
Add support to Copy Files between Container and Host
simone-panico Feb 10, 2026
e8973a2
Merge branch 'main' of https://github.com/simone-panico/container
simone-panico Feb 10, 2026
5f81b12
Add CLICopy Tests
simone-panico Feb 10, 2026
dd5e9d3
Merge branch 'apple:main' into main
simone-panico Feb 10, 2026
821d2b6
Update Docs
simone-panico Feb 10, 2026
3170d79
Merge branch 'apple:main' into main
simone-panico Feb 14, 2026
993f396
Refactor file copy methods to use URL types and add container state v…
simone-panico Feb 17, 2026
bd2c055
Correct destination path handling in ContainerCopy command
simone-panico Feb 17, 2026
8d91bcc
Add correct destination path handling in ContainerCopy command
simone-panico Feb 18, 2026
4b43d1c
Merge branch 'main' into main
simone-panico Feb 18, 2026
fd7899e
Merge branch 'main' into main
simone-panico Feb 19, 2026
5ef7882
Merge branch 'main' into main
JaewonHur Mar 10, 2026
e50aa86
Update Package.resolved and fix XPCKeys enum case for statistics
simone-panico Mar 11, 2026
e030032
Add Copy Directory Support + Add Copy Directory Tests
simone-panico Mar 11, 2026
0564e14
Add destinationIsDirectory bool
simone-panico Mar 16, 2026
48d9686
Revert Package.swift
simone-panico Mar 16, 2026
4b1a7d6
Refactor copyIn and copyOut methods to use createParents instead of d…
simone-panico Mar 18, 2026
3b8540c
Rebuild
simone-panico Apr 29, 2026
3df4643
Merge remote-tracking branch 'upstream/main'
simone-panico Apr 29, 2026
1b3380b
Package.resolved
simone-panico Apr 29, 2026
13e6ee6
Remove DNS dependency from Package.swift
simone-panico Apr 29, 2026
edd8f9e
Merge branch 'main' into main
simone-panico Apr 30, 2026
be139d6
Implement CopyIn with Stat Logic
simone-panico May 6, 2026
6b2252f
Merge branch 'main' into main
simone-panico May 6, 2026
0d91dcc
Merge branch 'main' into main
simone-panico May 6, 2026
275d1da
Merge branch 'main' into main
simone-panico May 9, 2026
09116cc
Update Package.swift
simone-panico May 12, 2026
61b7f63
Merge branch 'main' of https://github.com/simone-panico/container
simone-panico May 12, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ integration: init-block
$(SWIFT) test -c $(BUILD_CONFIGURATION) $(SWIFT_CONFIGURATION) --filter TestCLIRunLifecycle || exit_code=1 ; \
$(SWIFT) test -c $(BUILD_CONFIGURATION) $(SWIFT_CONFIGURATION) --filter TestCLIExecCommand || exit_code=1 ; \
$(SWIFT) test -c $(BUILD_CONFIGURATION) $(SWIFT_CONFIGURATION) --filter TestCLICreateCommand || exit_code=1 ; \
$(SWIFT) test -c $(BUILD_CONFIGURATION) $(SWIFT_CONFIGURATION) --filter TestCLICopyCommand || exit_code=1 ; \
$(SWIFT) test -c $(BUILD_CONFIGURATION) $(SWIFT_CONFIGURATION) --filter TestCLIRunCommand1 || exit_code=1 ; \
$(SWIFT) test -c $(BUILD_CONFIGURATION) $(SWIFT_CONFIGURATION) --filter TestCLIRunCommand2 || exit_code=1 ; \
$(SWIFT) test -c $(BUILD_CONFIGURATION) $(SWIFT_CONFIGURATION) --filter TestCLIRunCommand3 || exit_code=1 ; \
Expand Down
1 change: 1 addition & 0 deletions Sources/ContainerCommands/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public struct Application: AsyncLoggableCommand {
CommandGroup(
name: "Container",
subcommands: [
ContainerCopy.self,
ContainerCreate.self,
ContainerDelete.self,
ContainerExec.self,
Expand Down
81 changes: 81 additions & 0 deletions Sources/ContainerCommands/Container/ContainerCopy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//===----------------------------------------------------------------------===//
// 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) -> PathRef {
if let colonIdx = ref.firstIndex(of: ":") {
Comment thread
simone-panico marked this conversation as resolved.
Outdated
let id = String(ref[ref.startIndex..<colonIdx])
let path = String(ref[ref.index(after: colonIdx)...])
if !id.isEmpty && !path.isEmpty {
return .container(id: id, path: path)
}
}
return .local(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 = Self.parsePathRef(source)
let dstRef = Self.parsePathRef(destination)

switch (srcRef, dstRef) {
Comment thread
simone-panico marked this conversation as resolved.
case (.container(let id, let path), .local(let localPath)):
let resolvedLocal = URL(fileURLWithPath: localPath).standardizedFileURL.path
try await client.copyOut(id: id, source: path, destination: resolvedLocal)
case (.local(let localPath), .container(let id, let path)):
let resolvedLocal = URL(fileURLWithPath: localPath).standardizedFileURL.path
let filename = URL(fileURLWithPath: resolvedLocal).lastPathComponent
let containerDest = path.hasSuffix("/") ? path + filename : path + "/" + filename
try await client.copyIn(id: id, source: resolvedLocal, destination: containerDest)
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)")
}
}
}
}
2 changes: 2 additions & 0 deletions Sources/Helpers/APIServer/APIServer+Start.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ extension APIServer {
routes[XPCRoute.containerKill] = harness.kill
routes[XPCRoute.containerStats] = harness.stats
routes[XPCRoute.containerDiskUsage] = harness.diskUsage
routes[XPCRoute.containerCopyIn] = harness.copyIn
routes[XPCRoute.containerCopyOut] = harness.copyOut

return service
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Helpers/RuntimeLinux/RuntimeLinuxHelper+Start.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ extension RuntimeLinuxHelper {
SandboxRoutes.dial.rawValue: server.dial,
SandboxRoutes.shutdown.rawValue: server.shutdown,
SandboxRoutes.statistics.rawValue: server.statistics,
SandboxRoutes.copyIn.rawValue: server.copyIn,
SandboxRoutes.copyOut.rawValue: server.copyOut,
],
log: log
)
Expand Down
37 changes: 37 additions & 0 deletions Sources/Services/ContainerAPIService/Client/ContainerClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,43 @@ public struct ContainerClient: Sendable {
return fh
}

/// Copy a file from the host into the container.
public func copyIn(id: String, source: String, destination: String, mode: UInt32 = 0o644) async throws {
Comment thread
simone-panico marked this conversation as resolved.
Outdated
let request = XPCMessage(route: .containerCopyIn)
request.set(key: .id, value: id)
request.set(key: .sourcePath, value: source)
request.set(key: .destinationPath, value: destination)
request.set(key: .fileMode, value: UInt64(mode))

do {
try await xpcSend(message: request, timeout: .seconds(300))
} catch {
throw ContainerizationError(
.internalError,
message: "failed to copy into container \(id)",
cause: error
)
}
}

/// Copy a file from the container to the host.
public func copyOut(id: String, source: String, destination: String) async throws {
Comment thread
simone-panico marked this conversation as resolved.
Outdated
let request = XPCMessage(route: .containerCopyOut)
request.set(key: .id, value: id)
request.set(key: .sourcePath, value: source)
request.set(key: .destinationPath, value: destination)

do {
try await xpcSend(message: request, timeout: .seconds(300))
} catch {
throw ContainerizationError(
.internalError,
message: "failed to copy from container \(id)",
cause: error
)
}
}

/// Get resource usage statistics for a container.
public func stats(id: String) async throws -> ContainerStats {
let request = XPCMessage(route: .containerStats)
Expand Down
7 changes: 7 additions & 0 deletions Sources/Services/ContainerAPIService/Client/XPC+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ public enum XPCKeys: String {

/// Disk usage
case diskUsageStats

/// Copy parameters
case sourcePath
case destinationPath
case fileMode
}

public enum XPCRoute: String {
Expand All @@ -148,6 +153,8 @@ public enum XPCRoute: String {
case containerEvent
case containerStats
case containerDiskUsage
case containerCopyIn
case containerCopyOut

case pluginLoad
case pluginGet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,57 @@ public struct ContainersHarness: Sendable {
return reply
}

@Sendable
public func copyIn(_ message: XPCMessage) async throws -> XPCMessage {
guard let id = message.string(key: .id) else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
guard let sourcePath = message.string(key: .sourcePath) else {
throw ContainerizationError(
.invalidArgument,
message: "source path cannot be empty"
)
}
guard let destinationPath = message.string(key: .destinationPath) else {
throw ContainerizationError(
.invalidArgument,
message: "destination path cannot be empty"
)
}
let mode = UInt32(message.uint64(key: .fileMode))

try await service.copyIn(id: id, source: sourcePath, destination: destinationPath, mode: mode)
return message.reply()
}

@Sendable
public func copyOut(_ message: XPCMessage) async throws -> XPCMessage {
guard let id = message.string(key: .id) else {
throw ContainerizationError(
.invalidArgument,
message: "id cannot be empty"
)
}
guard let sourcePath = message.string(key: .sourcePath) else {
throw ContainerizationError(
.invalidArgument,
message: "source path cannot be empty"
)
}
guard let destinationPath = message.string(key: .destinationPath) else {
throw ContainerizationError(
.invalidArgument,
message: "destination path cannot be empty"
)
}

try await service.copyOut(id: id, source: sourcePath, destination: destinationPath)
return message.reply()
}

@Sendable
public func stats(_ message: XPCMessage) async throws -> XPCMessage {
let id = message.string(key: .id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,24 @@ public actor ContainersService {
}
}

/// Copy a file from the host into the container.
public func copyIn(id: String, source: String, destination: String, mode: UInt32) async throws {
self.log.debug("\(#function)")

let state = try self._getContainerState(id: id)
let client = try state.getClient()
Comment thread
simone-panico marked this conversation as resolved.
try await client.copyIn(source: source, destination: destination, mode: mode)
}

/// Copy a file from the container to the host.
public func copyOut(id: String, source: String, destination: String) async throws {
self.log.debug("\(#function)")

let state = try self._getContainerState(id: id)
Comment thread
simone-panico marked this conversation as resolved.
let client = try state.getClient()
try await client.copyOut(source: source, destination: destination)
}

/// Get statistics for the container.
public func stats(id: String) async throws -> ContainerStats {
self.log.debug("\(#function)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,39 @@ extension SandboxClient {
}
}

public func copyIn(source: String, destination: String, mode: UInt32) async throws {
let request = XPCMessage(route: SandboxRoutes.copyIn.rawValue)
request.set(key: SandboxKeys.sourcePath.rawValue, value: source)
request.set(key: SandboxKeys.destinationPath.rawValue, value: destination)
request.set(key: SandboxKeys.fileMode.rawValue, value: UInt64(mode))
Comment thread
simone-panico marked this conversation as resolved.

do {
try await self.client.send(request, responseTimeout: .seconds(300))
} catch {
throw ContainerizationError(
.internalError,
message: "failed to copy into container \(self.id)",
cause: error
)
}
}

public func copyOut(source: String, destination: String) async throws {
let request = XPCMessage(route: SandboxRoutes.copyOut.rawValue)
request.set(key: SandboxKeys.sourcePath.rawValue, value: source)
request.set(key: SandboxKeys.destinationPath.rawValue, value: destination)

do {
try await self.client.send(request, responseTimeout: .seconds(300))
} catch {
throw ContainerizationError(
.internalError,
message: "failed to copy from container \(self.id)",
cause: error
)
}
}

public func statistics() async throws -> ContainerStats {
let request = XPCMessage(route: SandboxRoutes.statistics.rawValue)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public enum SandboxKeys: String {

/// Container statistics
case statistics

/// Copy parameters
case sourcePath
case destinationPath
case fileMode
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public enum SandboxRoutes: String {
case shutdown = "com.apple.container.sandbox/shutdown"
/// Get statistics for the sandbox.
case statistics = "com.apple.container.sandbox/statistics"
/// Copy a file into the container.
case copyIn = "com.apple.container.sandbox/copyIn"
/// Copy a file out of the container.
case copyOut = "com.apple.container.sandbox/copyOut"
}
Loading