Skip to content

Commit 39b78a9

Browse files
committed
address reviewed items
1 parent 7d916fe commit 39b78a9

File tree

8 files changed

+43
-46
lines changed

8 files changed

+43
-46
lines changed

Sources/WASI/FileSystem.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public enum FileContent {
137137
/// Public protocol for file system providers that users interact with.
138138
///
139139
/// This protocol exposes only user-facing methods for managing files and directories.
140-
public protocol FileSystemProvider {
140+
public protocol FileSystemProvider: ~Copyable {
141141
/// Adds a file to the file system with the given byte content.
142-
func addFile(at path: String, content: [UInt8]) throws
142+
func addFile(at path: String, content: some Sequence<UInt8>) throws
143143

144144
/// Adds a file to the file system with the given string content.
145145
func addFile(at path: String, content: String) throws
@@ -158,9 +158,9 @@ public protocol FileSystemProvider {
158158
///
159159
/// This protocol contains WASI-specific implementation details that should not
160160
/// be exposed to library users.
161-
internal protocol FileSystem {
161+
protocol FileSystemImplementation: ~Copyable {
162162
/// Returns the list of pre-opened directory paths.
163-
func getPreopenPaths() -> [String]
163+
var preopenPaths: [String] { get }
164164

165165
/// Opens a directory and returns a WASIDir implementation.
166166
func openDirectory(at path: String) throws -> any WASIDir

Sources/WASI/MemoryFileSystem/MemoryDirEntry.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SystemPackage
22

33
/// A WASIDir implementation backed by an in-memory directory node.
4-
internal struct MemoryDirEntry: WASIDir {
4+
struct MemoryDirEntry: WASIDir {
55
let preopenPath: String?
66
let dirNode: MemoryDirectoryNode
77
let path: String

Sources/WASI/MemoryFileSystem/MemoryFSNodes.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import SystemPackage
22

33
/// Base protocol for all file system nodes in memory.
4-
internal protocol MemFSNode: AnyObject {
4+
protocol MemFSNode: AnyObject {
55
var type: MemFSNodeType { get }
66
}
77

88
/// Types of file system nodes.
9-
internal enum MemFSNodeType {
9+
enum MemFSNodeType {
1010
case directory
1111
case file
1212
case characterDevice
1313
}
1414

1515
/// A directory node in the memory file system.
16-
internal final class MemoryDirectoryNode: MemFSNode {
16+
final class MemoryDirectoryNode: MemFSNode {
1717
let type: MemFSNodeType = .directory
1818
private var children: [String: MemFSNode] = [:]
1919

@@ -42,16 +42,16 @@ internal final class MemoryDirectoryNode: MemFSNode {
4242
}
4343

4444
/// A regular file node in the memory file system.
45-
internal final class MemoryFileNode: MemFSNode {
45+
final class MemoryFileNode: MemFSNode {
4646
let type: MemFSNodeType = .file
4747
var content: FileContent
4848

4949
init(content: FileContent) {
5050
self.content = content
5151
}
5252

53-
convenience init(bytes: [UInt8]) {
54-
self.init(content: .bytes(bytes))
53+
convenience init(bytes: some Sequence<UInt8>) {
54+
self.init(content: .bytes(Array(bytes)))
5555
}
5656

5757
convenience init(handle: FileDescriptor) {
@@ -74,7 +74,7 @@ internal final class MemoryFileNode: MemFSNode {
7474
}
7575

7676
/// A character device node in the memory file system.
77-
internal final class MemoryCharacterDeviceNode: MemFSNode {
77+
final class MemoryCharacterDeviceNode: MemFSNode {
7878
let type: MemFSNodeType = .characterDevice
7979

8080
enum Kind {
@@ -89,7 +89,7 @@ internal final class MemoryCharacterDeviceNode: MemFSNode {
8989
}
9090

9191
/// A WASIFile implementation for character devices like /dev/null
92-
internal final class MemoryCharacterDeviceEntry: WASIFile {
92+
final class MemoryCharacterDeviceEntry: WASIFile {
9393
let deviceNode: MemoryCharacterDeviceNode
9494
let accessMode: FileAccessMode
9595

Sources/WASI/MemoryFileSystem/MemoryFileEntry.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SystemPackage
22

33
/// A WASIFile implementation for regular files in the memory file system.
4-
internal final class MemoryFileEntry: WASIFile {
4+
final class MemoryFileEntry: WASIFile {
55
let fileNode: MemoryFileNode
66
let accessMode: FileAccessMode
77
var position: Int

Sources/WASI/MemoryFileSystem/MemoryFileSystem.swift

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import SystemPackage
1616
/// let fd = try FileDescriptor.open("/path/to/file", .readOnly)
1717
/// try fs.addFile(at: "/mounted.txt", handle: fd)
1818
/// ```
19-
public final class MemoryFileSystem: FileSystemProvider, FileSystem {
19+
public final class MemoryFileSystem: FileSystemProvider, FileSystemImplementation {
2020
private static let rootPath = "/"
2121

2222
private var root: MemoryDirectoryNode
23-
private let preopenPaths: [String]
23+
let preopenPaths: [String]
2424

2525
/// Creates a new in-memory file system.
2626
///
@@ -50,8 +50,8 @@ public final class MemoryFileSystem: FileSystemProvider, FileSystem {
5050
///
5151
/// - Parameters:
5252
/// - path: The path where the file should be created
53-
/// - content: The file content as byte array
54-
public func addFile(at path: String, content: [UInt8]) throws {
53+
/// - content: The file content as a sequence of bytes
54+
public func addFile(at path: String, content: some Sequence<UInt8>) throws {
5555
let normalized = normalizePath(path)
5656
let (parentPath, fileName) = try splitPath(normalized)
5757

@@ -65,7 +65,7 @@ public final class MemoryFileSystem: FileSystemProvider, FileSystem {
6565
/// - path: The path where the file should be created
6666
/// - content: The file content as string (converted to UTF-8)
6767
public func addFile(at path: String, content: String) throws {
68-
try addFile(at: path, content: Array(content.utf8))
68+
try addFile(at: path, content: content.utf8)
6969
}
7070

7171
/// Adds a file to the file system backed by a file descriptor.
@@ -113,13 +113,9 @@ public final class MemoryFileSystem: FileSystemProvider, FileSystem {
113113
}
114114
}
115115

116-
// MARK: - FileSystem (Internal WASI API)
116+
// MARK: - FileSystemImplementation (WASI API)
117117

118-
internal func getPreopenPaths() -> [String] {
119-
return preopenPaths
120-
}
121-
122-
internal func openDirectory(at path: String) throws -> any WASIDir {
118+
func openDirectory(at path: String) throws -> any WASIDir {
123119
guard let node = lookup(at: path) else {
124120
throw WASIAbi.Errno.ENOENT
125121
}
@@ -136,7 +132,7 @@ public final class MemoryFileSystem: FileSystemProvider, FileSystem {
136132
)
137133
}
138134

139-
internal func openAt(
135+
func openAt(
140136
dirFd: any WASIDir,
141137
path: String,
142138
oflags: WASIAbi.Oflags,
@@ -227,13 +223,13 @@ public final class MemoryFileSystem: FileSystemProvider, FileSystem {
227223
throw WASIAbi.Errno.ENOTSUP
228224
}
229225

230-
internal func createStdioFile(fd: FileDescriptor, accessMode: FileAccessMode) -> any WASIFile {
226+
func createStdioFile(fd: FileDescriptor, accessMode: FileAccessMode) -> any WASIFile {
231227
return MemoryStdioFile(fd: fd, accessMode: accessMode)
232228
}
233229

234-
// MARK: - Internal File Operations
230+
// MARK: - File Operations
235231

236-
internal func lookup(at path: String) -> MemFSNode? {
232+
func lookup(at path: String) -> MemFSNode? {
237233
let normalized = normalizePath(path)
238234

239235
if normalized == Self.rootPath {
@@ -256,7 +252,7 @@ public final class MemoryFileSystem: FileSystemProvider, FileSystem {
256252
return current
257253
}
258254

259-
internal func resolve(from directory: MemoryDirectoryNode, at directoryPath: String, path relativePath: String) -> MemFSNode? {
255+
func resolve(from directory: MemoryDirectoryNode, at directoryPath: String, path relativePath: String) -> MemFSNode? {
260256
if relativePath.isEmpty {
261257
return directory
262258
}
@@ -292,7 +288,7 @@ public final class MemoryFileSystem: FileSystemProvider, FileSystem {
292288
}
293289

294290
@discardableResult
295-
internal func ensureDirectory(at path: String) throws -> MemoryDirectoryNode {
291+
func ensureDirectory(at path: String) throws -> MemoryDirectoryNode {
296292
let normalized = normalizePath(path)
297293

298294
if normalized == Self.rootPath {
@@ -342,7 +338,7 @@ public final class MemoryFileSystem: FileSystemProvider, FileSystem {
342338
}
343339

344340
@discardableResult
345-
internal func createFile(in directory: MemoryDirectoryNode, at relativePath: String, oflags: WASIAbi.Oflags) throws -> MemoryFileNode {
341+
func createFile(in directory: MemoryDirectoryNode, at relativePath: String, oflags: WASIAbi.Oflags) throws -> MemoryFileNode {
346342
try validateRelativePath(relativePath)
347343

348344
let components = relativePath.split(separator: "/").map(String.init)
@@ -367,7 +363,7 @@ public final class MemoryFileSystem: FileSystemProvider, FileSystem {
367363
}
368364
}
369365

370-
internal func removeNode(in directory: MemoryDirectoryNode, at relativePath: String, mustBeDirectory: Bool) throws {
366+
func removeNode(in directory: MemoryDirectoryNode, at relativePath: String, mustBeDirectory: Bool) throws {
371367
try validateRelativePath(relativePath)
372368

373369
let components = relativePath.split(separator: "/").map(String.init)
@@ -403,7 +399,7 @@ public final class MemoryFileSystem: FileSystemProvider, FileSystem {
403399
current.removeChild(name: fileName)
404400
}
405401

406-
internal func rename(from sourcePath: String, in sourceDir: MemoryDirectoryNode, to destPath: String, in destDir: MemoryDirectoryNode) throws {
402+
func rename(from sourcePath: String, in sourceDir: MemoryDirectoryNode, to destPath: String, in destDir: MemoryDirectoryNode) throws {
407403
guard let sourceNode = resolve(from: sourceDir, at: "", path: sourcePath) else {
408404
throw WASIAbi.Errno.ENOENT
409405
}

Sources/WASI/Platform/HostFileSystem.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import SystemPackage
2020
///
2121
/// This implementation provides access to actual files and directories on the host system,
2222
/// with appropriate sandboxing through pre-opened directories.
23-
public final class HostFileSystem: FileSystemProvider, FileSystem {
23+
public final class HostFileSystem: FileSystemProvider, FileSystemImplementation {
24+
2425
private let preopens: [String: String]
2526

2627
/// Creates a new host file system with the specified pre-opened directories.
@@ -32,7 +33,7 @@ public final class HostFileSystem: FileSystemProvider, FileSystem {
3233

3334
// MARK: - FileSystemProvider (Public API)
3435

35-
public func addFile(at path: String, content: [UInt8]) throws {
36+
public func addFile(at path: String, content: some Sequence<UInt8>) throws {
3637
throw WASIAbi.Errno.ENOTSUP
3738
}
3839

@@ -52,13 +53,13 @@ public final class HostFileSystem: FileSystemProvider, FileSystem {
5253
throw WASIAbi.Errno.ENOTSUP
5354
}
5455

55-
// MARK: - FileSystem (Internal WASI API)
56+
// MARK: - FileSystemImplementation (WASI API)
5657

57-
internal func getPreopenPaths() -> [String] {
58+
var preopenPaths: [String] {
5859
return Array(preopens.keys).sorted()
5960
}
6061

61-
internal func openDirectory(at path: String) throws -> any WASIDir {
62+
func openDirectory(at path: String) throws -> any WASIDir {
6263
guard let hostPath = preopens[path] else {
6364
throw WASIAbi.Errno.ENOENT
6465
}
@@ -83,7 +84,7 @@ public final class HostFileSystem: FileSystemProvider, FileSystem {
8384
return DirEntry(preopenPath: path, fd: fd)
8485
}
8586

86-
internal func openAt(
87+
func openAt(
8788
dirFd: any WASIDir,
8889
path: String,
8990
oflags: WASIAbi.Oflags,
@@ -124,7 +125,7 @@ public final class HostFileSystem: FileSystemProvider, FileSystem {
124125
#endif
125126
}
126127

127-
internal func createStdioFile(fd: FileDescriptor, accessMode: FileAccessMode) -> any WASIFile {
128+
func createStdioFile(fd: FileDescriptor, accessMode: FileAccessMode) -> any WASIFile {
128129
return StdioFileEntry(fd: fd, accessMode: accessMode)
129130
}
130131
}

Sources/WASI/WASI.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ public class WASIBridgeToHost: WASI {
13731373
private let wallClock: WallClock
13741374
private let monotonicClock: MonotonicClock
13751375
private var randomGenerator: RandomBufferGenerator
1376-
private let fileSystem: FileSystem
1376+
private let fileSystem: FileSystemImplementation
13771377

13781378
public init(
13791379
args: [String] = [],
@@ -1390,7 +1390,7 @@ public class WASIBridgeToHost: WASI {
13901390
self.args = args
13911391
self.environment = environment
13921392
if let provider = fileSystemProvider {
1393-
guard let fs = provider as? FileSystem else {
1393+
guard let fs = provider as? FileSystemImplementation else {
13941394
throw WASIError(description: "Invalid file system provider")
13951395
}
13961396
self.fileSystem = fs
@@ -1403,7 +1403,7 @@ public class WASIBridgeToHost: WASI {
14031403
fdTable[1] = .file(self.fileSystem.createStdioFile(fd: stdout, accessMode: .write))
14041404
fdTable[2] = .file(self.fileSystem.createStdioFile(fd: stderr, accessMode: .write))
14051405

1406-
for preopenPath in self.fileSystem.getPreopenPaths() {
1406+
for preopenPath in self.fileSystem.preopenPaths {
14071407
let dirEntry = try self.fileSystem.openDirectory(at: preopenPath)
14081408
_ = try fdTable.push(.directory(dirEntry))
14091409
}

Tests/WASITests/WASITests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ struct WASITests {
139139
func memoryFileSystem() throws {
140140
let fs = try MemoryFileSystem(preopens: ["/": "/"])
141141

142-
#expect(fs.getPreopenPaths() == ["/"])
142+
#expect(fs.preopenPaths == ["/"])
143143
#expect(fs.lookup(at: "/") != nil)
144144
#expect(fs.lookup(at: "/dev/null") != nil)
145145

@@ -329,7 +329,7 @@ struct WASITests {
329329
"/data": "/data",
330330
])
331331

332-
let preopens = fs.getPreopenPaths()
332+
let preopens = fs.preopenPaths
333333
#expect(preopens.count == 3)
334334
#expect(preopens.contains("/"))
335335
#expect(preopens.contains("/tmp"))

0 commit comments

Comments
 (0)