From 940fefb7cd79c0d658e6e9f98ea777aa4738faee Mon Sep 17 00:00:00 2001 From: Chris George Date: Tue, 12 May 2026 12:35:02 -0700 Subject: [PATCH] Use SystemPath for ContainerResource.Filesystem. (#1523) - Closes #1521. - Using URL for filesystem paths is bad practice. FilePath is safer and more ergonomic. - Same pattern as #1480 (HostDNSResolver) and #1518 (PacketFilter). --- .../ContainerResource/Container/Filesystem.swift | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Sources/ContainerResource/Container/Filesystem.swift b/Sources/ContainerResource/Container/Filesystem.swift index 7522a515..445a8762 100644 --- a/Sources/ContainerResource/Container/Filesystem.swift +++ b/Sources/ContainerResource/Container/Filesystem.swift @@ -15,6 +15,7 @@ //===----------------------------------------------------------------------===// import Foundation +import SystemPackage /// Options to pass to a mount call. public typealias MountOptions = [String] @@ -95,7 +96,7 @@ public struct Filesystem: Sendable, Codable { ) -> Filesystem { .init( type: .block(format: format, cache: cache, sync: sync), - source: URL(fileURLWithPath: source).absolutePath(), + source: absoluteFilePath(for: source).string, destination: destination, options: options ) @@ -108,7 +109,7 @@ public struct Filesystem: Sendable, Codable { ) -> Filesystem { .init( type: .volume(name: name, format: format, cache: cache, sync: sync), - source: URL(fileURLWithPath: source).absolutePath(), + source: absoluteFilePath(for: source).string, destination: destination, options: options ) @@ -118,7 +119,7 @@ public struct Filesystem: Sendable, Codable { public static func virtiofs(source: String, destination: String, options: MountOptions) -> Filesystem { .init( type: .virtiofs, - source: URL(fileURLWithPath: source).absolutePath(), + source: absoluteFilePath(for: source).string, destination: destination, options: options ) @@ -184,3 +185,11 @@ public struct Filesystem: Sendable, Codable { return .init(type: self.type, source: to, destination: self.destination, options: self.options) } } + +private func absoluteFilePath(for source: String) -> FilePath { + let path = FilePath(source) + guard path.isRelative else { return path.lexicallyNormalized() } + return FilePath(FileManager.default.currentDirectoryPath) + .pushing(path) + .lexicallyNormalized() +}