Skip to content

Commit 940fefb

Browse files
authored
Use SystemPath for ContainerResource.Filesystem. (apple#1523)
- Closes apple#1521. - Using URL for filesystem paths is bad practice. FilePath is safer and more ergonomic. - Same pattern as apple#1480 (HostDNSResolver) and apple#1518 (PacketFilter).
1 parent c56a659 commit 940fefb

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

Sources/ContainerResource/Container/Filesystem.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
import Foundation
18+
import SystemPackage
1819

1920
/// Options to pass to a mount call.
2021
public typealias MountOptions = [String]
@@ -95,7 +96,7 @@ public struct Filesystem: Sendable, Codable {
9596
) -> Filesystem {
9697
.init(
9798
type: .block(format: format, cache: cache, sync: sync),
98-
source: URL(fileURLWithPath: source).absolutePath(),
99+
source: absoluteFilePath(for: source).string,
99100
destination: destination,
100101
options: options
101102
)
@@ -108,7 +109,7 @@ public struct Filesystem: Sendable, Codable {
108109
) -> Filesystem {
109110
.init(
110111
type: .volume(name: name, format: format, cache: cache, sync: sync),
111-
source: URL(fileURLWithPath: source).absolutePath(),
112+
source: absoluteFilePath(for: source).string,
112113
destination: destination,
113114
options: options
114115
)
@@ -118,7 +119,7 @@ public struct Filesystem: Sendable, Codable {
118119
public static func virtiofs(source: String, destination: String, options: MountOptions) -> Filesystem {
119120
.init(
120121
type: .virtiofs,
121-
source: URL(fileURLWithPath: source).absolutePath(),
122+
source: absoluteFilePath(for: source).string,
122123
destination: destination,
123124
options: options
124125
)
@@ -184,3 +185,11 @@ public struct Filesystem: Sendable, Codable {
184185
return .init(type: self.type, source: to, destination: self.destination, options: self.options)
185186
}
186187
}
188+
189+
private func absoluteFilePath(for source: String) -> FilePath {
190+
let path = FilePath(source)
191+
guard path.isRelative else { return path.lexicallyNormalized() }
192+
return FilePath(FileManager.default.currentDirectoryPath)
193+
.pushing(path)
194+
.lexicallyNormalized()
195+
}

0 commit comments

Comments
 (0)