Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 11 additions & 11 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
122 changes: 122 additions & 0 deletions Sources/ContainerCommands/Container/ContainerCopy.swift
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)")
}
}
}
}
2 changes: 2 additions & 0 deletions Sources/Helpers/APIServer/APIServer+Start.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,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
routes[XPCRoute.containerExport] = harness.export

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 @@ -104,6 +104,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
39 changes: 39 additions & 0 deletions Sources/Services/ContainerAPIService/Client/ContainerClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,45 @@ public struct ContainerClient: Sendable {
return fh
}

/// Copy a file or directory from the host into the container.
public func copyIn(id: String, source: URL, destination: URL, mode: UInt32 = 0o644, createParents: Bool = true) async throws {
let request = XPCMessage(route: .containerCopyIn)
request.set(key: .id, value: id)
request.set(key: .sourcePath, value: source.path)
request.set(key: .destinationPath, value: destination.path)
request.set(key: .fileMode, value: UInt64(mode))
request.set(key: .createParents, value: createParents)

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 or directory from the container to the host.
public func copyOut(id: String, source: URL, destination: URL, createParents: Bool = true) async throws {
let request = XPCMessage(route: .containerCopyOut)
request.set(key: .id, value: id)
request.set(key: .sourcePath, value: source.path)
request.set(key: .destinationPath, value: destination.path)
request.set(key: .createParents, value: createParents)

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
8 changes: 8 additions & 0 deletions Sources/Services/ContainerAPIService/Client/XPC+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ public enum XPCKeys: String {

/// Disk usage
case diskUsageStats

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

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

case pluginLoad
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,60 @@ 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))
let createParents = message.bool(key: .createParents)

try await service.copyIn(id: id, source: sourcePath, destination: destinationPath, mode: mode, createParents: createParents)
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"
)
}

let createParents = message.bool(key: .createParents)

try await service.copyOut(id: id, source: sourcePath, destination: destinationPath, createParents: createParents)
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 @@ -776,6 +776,30 @@ public actor ContainersService {
}
}

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

let state = try self._getContainerState(id: id)
guard state.snapshot.status == .running else {
throw ContainerizationError(.invalidState, message: "container \(id) is not running")
}
let client = try state.getClient()
try await client.copyIn(source: source, destination: destination, mode: mode, createParents: createParents)
}

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

let state = try self._getContainerState(id: id)
guard state.snapshot.status == .running else {
throw ContainerizationError(.invalidState, message: "container \(id) is not running")
}
let client = try state.getClient()
try await client.copyOut(source: source, destination: destination, createParents: createParents)
}

/// Get statistics for the container.
public func stats(id: String) async throws -> ContainerStats {
log.debug(
Expand Down
Loading