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
20 changes: 13 additions & 7 deletions Sources/ContainerCommands/Container/ContainerCopy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,27 @@ import SystemPackage

extension Application {
public struct ContainerCopy: AsyncLoggableCommand {
enum PathRef {
enum PathRef: Equatable {
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:
// A container reference is "<id>:<absolute-path>". The id never
// contains a path separator, but the path may itself contain ':'
// (e.g. an ISO-8601 timestamp in a filename), so split on the FIRST
// ':' only rather than on every colon. If the text before the first
// ':' contains '/', the whole ref is a local path that happens to
// contain a colon (e.g. "/home/2026-01-01T00:00:00.log").
guard let colon = ref.firstIndex(of: ":"), !ref[..<colon].contains("/") else {
return .local(ref)
case 2 where !parts[0].isEmpty && parts[1].starts(with: "/"):
return .container(id: parts[0], path: parts[1])
default:
}
let id = String(ref[..<colon])
let path = String(ref[ref.index(after: colon)...])
guard !id.isEmpty, path.hasPrefix("/") else {
throw ContainerizationError(.invalidArgument, message: "invalid path given: \(ref)")
}
return .container(id: id, path: path)
}

public init() {}
Expand Down
65 changes: 65 additions & 0 deletions Tests/ContainerCommandsTests/ContainerCopyPathRefTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//===----------------------------------------------------------------------===//
// 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 Foundation
import Testing

@testable import ContainerCommands

struct ContainerCopyPathRefTests {
private func parse(_ ref: String) throws -> Application.ContainerCopy.PathRef {
try Application.ContainerCopy.parsePathRef(ref)
}

@Test("a path without a colon is a local path")
func localNoColon() throws {
#expect(try parse("/tmp/file.txt") == .local("/tmp/file.txt"))
}

@Test("an id followed by an absolute path is a container reference")
func containerRef() throws {
#expect(try parse("box:/tmp/file.txt") == .container(id: "box", path: "/tmp/file.txt"))
}

@Test("a container path containing colons is preserved")
func containerPathWithColons() throws {
// Regression: an ISO-8601 timestamp filename was rejected as an
// "invalid path" because the reference was split on every colon
// instead of only the first.
#expect(
try parse("box:/var/log/app-2026-07-20T10:30:00.log")
== .container(id: "box", path: "/var/log/app-2026-07-20T10:30:00.log")
)
}

@Test("a local path containing colons is a local path")
func localPathWithColons() throws {
#expect(
try parse("/home/user/2026-07-20T10:30:00.log")
== .local("/home/user/2026-07-20T10:30:00.log")
)
}

@Test("an empty container id is rejected")
func emptyId() {
#expect(throws: (any Error).self) { try parse(":/tmp/x") }
}

@Test("a non-absolute container path is rejected")
func relativeContainerPath() {
#expect(throws: (any Error).self) { try parse("box:relative/path") }
}
}