-
Notifications
You must be signed in to change notification settings - Fork 20
WASI MemoryFileSystem #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andrewmd5
wants to merge
6
commits into
swiftwasm:main
Choose a base branch
from
6over3:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe1b938
in-memory file system for WASI with support for directories, files, a…
andrewmd5 ff46f85
fixups
andrewmd5 1183a12
formatting
andrewmd5 efad28d
remove file
andrewmd5 7d916fe
apply formatting
andrewmd5 39b78a9
address reviewed items
andrewmd5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import SystemPackage | ||
|
|
||
| /// A WASIDir implementation backed by an in-memory directory node. | ||
| struct MemoryDirEntry: WASIDir { | ||
| let preopenPath: String? | ||
| let dirNode: MemoryDirectoryNode | ||
| let path: String | ||
| let fileSystem: MemoryFileSystem | ||
|
|
||
| func attributes() throws -> WASIAbi.Filestat { | ||
| return WASIAbi.Filestat( | ||
| dev: 0, ino: 0, filetype: .DIRECTORY, | ||
| nlink: 1, size: 0, | ||
| atim: 0, mtim: 0, ctim: 0 | ||
| ) | ||
| } | ||
|
|
||
| func fileType() throws -> WASIAbi.FileType { | ||
| return .DIRECTORY | ||
| } | ||
|
|
||
| func status() throws -> WASIAbi.Fdflags { | ||
| return [] | ||
| } | ||
|
|
||
| func setTimes( | ||
| atim: WASIAbi.Timestamp, mtim: WASIAbi.Timestamp, | ||
| fstFlags: WASIAbi.FstFlags | ||
| ) throws { | ||
| // No-op for memory filesystem - timestamps not tracked | ||
| } | ||
|
|
||
| func advise( | ||
| offset: WASIAbi.FileSize, length: WASIAbi.FileSize, advice: WASIAbi.Advice | ||
| ) throws { | ||
| // No-op for memory filesystem | ||
| } | ||
|
|
||
| func close() throws { | ||
| // No-op for memory filesystem - no resources to release | ||
| } | ||
|
|
||
| func openFile( | ||
| symlinkFollow: Bool, | ||
| path: String, | ||
| oflags: WASIAbi.Oflags, | ||
| accessMode: FileAccessMode, | ||
| fdflags: WASIAbi.Fdflags | ||
| ) throws -> FileDescriptor { | ||
| // Memory filesystem doesn't return real file descriptors for this method | ||
| // File opening is handled through the WASI bridge's path_open implementation | ||
| throw WASIAbi.Errno.ENOTSUP | ||
| } | ||
|
|
||
| func createDirectory(atPath path: String) throws { | ||
| let fullPath = self.path.hasSuffix("/") ? self.path + path : self.path + "/" + path | ||
| try fileSystem.ensureDirectory(at: fullPath) | ||
| } | ||
|
|
||
| func removeDirectory(atPath path: String) throws { | ||
| try fileSystem.removeNode(in: dirNode, at: path, mustBeDirectory: true) | ||
| } | ||
|
|
||
| func removeFile(atPath path: String) throws { | ||
| try fileSystem.removeNode(in: dirNode, at: path, mustBeDirectory: false) | ||
| } | ||
|
|
||
| func symlink(from sourcePath: String, to destPath: String) throws { | ||
| // Symlinks not supported in memory filesystem | ||
| throw WASIAbi.Errno.ENOTSUP | ||
| } | ||
|
|
||
| func rename(from sourcePath: String, toDir newDir: any WASIDir, to destPath: String) throws { | ||
| guard let newMemoryDir = newDir as? MemoryDirEntry else { | ||
| throw WASIAbi.Errno.EXDEV | ||
| } | ||
|
|
||
| try fileSystem.rename( | ||
| from: sourcePath, in: dirNode, | ||
| to: destPath, in: newMemoryDir.dirNode | ||
| ) | ||
| } | ||
|
|
||
| func readEntries(cookie: WASIAbi.DirCookie) throws -> AnyIterator<Result<ReaddirElement, any Error>> { | ||
| let children = dirNode.listChildren() | ||
|
|
||
| let iterator = children.enumerated() | ||
| .dropFirst(Int(cookie)) | ||
| .map { (index, name) -> Result<ReaddirElement, any Error> in | ||
| return Result(catching: { | ||
| let childPath = self.path.hasSuffix("/") ? self.path + name : self.path + "/" + name | ||
| guard let childNode = fileSystem.lookup(at: childPath) else { | ||
| throw WASIAbi.Errno.ENOENT | ||
| } | ||
|
|
||
| let fileType: WASIAbi.FileType | ||
| switch childNode.type { | ||
| case .directory: fileType = .DIRECTORY | ||
| case .file: fileType = .REGULAR_FILE | ||
| case .characterDevice: fileType = .CHARACTER_DEVICE | ||
| } | ||
|
|
||
| let dirent = WASIAbi.Dirent( | ||
| dNext: WASIAbi.DirCookie(index + 1), | ||
| dIno: 0, | ||
| dirNameLen: WASIAbi.DirNameLen(name.utf8.count), | ||
| dType: fileType | ||
| ) | ||
|
|
||
| return (dirent, name) | ||
| }) | ||
| } | ||
| .makeIterator() | ||
|
|
||
| return AnyIterator(iterator) | ||
| } | ||
|
|
||
| func attributes(path: String, symlinkFollow: Bool) throws -> WASIAbi.Filestat { | ||
| let fullPath = self.path.hasSuffix("/") ? self.path + path : self.path + "/" + path | ||
| guard let node = fileSystem.lookup(at: fullPath) else { | ||
| throw WASIAbi.Errno.ENOENT | ||
| } | ||
|
|
||
| let fileType: WASIAbi.FileType | ||
| var size: WASIAbi.FileSize = 0 | ||
|
|
||
| switch node.type { | ||
| case .directory: | ||
| fileType = .DIRECTORY | ||
| case .file: | ||
| fileType = .REGULAR_FILE | ||
| if let fileNode = node as? MemoryFileNode { | ||
| size = WASIAbi.FileSize(fileNode.size) | ||
| } | ||
| case .characterDevice: | ||
| fileType = .CHARACTER_DEVICE | ||
| } | ||
|
|
||
| return WASIAbi.Filestat( | ||
| dev: 0, ino: 0, filetype: fileType, | ||
| nlink: 1, size: size, | ||
| atim: 0, mtim: 0, ctim: 0 | ||
| ) | ||
| } | ||
|
|
||
| func setFilestatTimes( | ||
| path: String, | ||
| atim: WASIAbi.Timestamp, mtim: WASIAbi.Timestamp, | ||
| fstFlags: WASIAbi.FstFlags, symlinkFollow: Bool | ||
| ) throws { | ||
| // No-op for memory filesystem - timestamps not tracked | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the use of existentials is justified here? Could we use opaque types instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WASIDirseems to be defined as a protocol so I believe an existential is how its used elsewhere (i.e in Directory.swift)