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).
This commit is contained in:
Chris George
2026-05-12 12:35:02 -07:00
committed by GitHub
parent c56a659d33
commit 940fefb7cd
@@ -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()
}