-
Notifications
You must be signed in to change notification settings - Fork 289
WASI: fs.FS
as Preopen parameter with implementation of fd_prestat_get
#362
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
Changes from all commits
ca9e8d4
8c397e7
300fd69
d01825c
abc270a
d9a9eea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ package examples | |
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"embed" | ||
"io" | ||
"io/fs" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
@@ -16,42 +18,49 @@ import ( | |
//go:embed testdata/file_system.wasm | ||
var filesystemWasm []byte | ||
|
||
func writeFile(fs wasi.FS, path string, data []byte) error { | ||
f, err := fs.OpenWASI(0, path, wasi.O_CREATE|wasi.O_TRUNC, wasi.R_FD_WRITE, 0, 0) | ||
if err != nil { | ||
return err | ||
} | ||
// Embed a directory as FS. | ||
//go:embed testdata/file_system_input/input.txt | ||
var embeddedFS embed.FS | ||
|
||
if _, err := io.Copy(f, bytes.NewBuffer(data)); err != nil { | ||
return err | ||
} | ||
|
||
return f.Close() | ||
} | ||
|
||
func readFile(fs wasi.FS, path string) ([]byte, error) { | ||
f, err := fs.OpenWASI(0, path, 0, 0, 0, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
func writeFile(t *testing.T, fs wasi.OpenFileFS, path string, data []byte) { | ||
f, err := fs.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0644)) | ||
require.NoError(t, err) | ||
|
||
buf := bytes.NewBuffer(nil) | ||
// fs.File can be casted to io.Writer when it's writable. | ||
writer, ok := f.(io.Writer) | ||
require.True(t, ok) | ||
|
||
if _, err := io.Copy(buf, f); err != nil { | ||
return buf.Bytes(), nil | ||
} | ||
_, err = io.Copy(writer, bytes.NewBuffer(data)) | ||
require.NoError(t, err) | ||
|
||
return buf.Bytes(), f.Close() | ||
err = f.Close() | ||
require.NoError(t, err) | ||
} | ||
|
||
func Test_file_system(t *testing.T) { | ||
r := wazero.NewRuntime() | ||
|
||
memFS := wazero.WASIMemFS() | ||
err := writeFile(memFS, "input.txt", []byte("Hello, file system!")) | ||
// embeddedFS is a read-only embed.FS file system. Get the sub FS to shorten the nested directories. | ||
embeddedFS, err := fs.Sub(embeddedFS, "testdata/file_system_input") | ||
require.NoError(t, err) | ||
|
||
wasiConfig := wazero.NewWASIConfig().WithPreopens(map[string]wasi.FS{".": memFS}) | ||
// WASIMemFS returns a in-memory file system that supports both read and write. | ||
memFS := wazero.WASIMemFS() | ||
// memFS is writable. Create another input file. | ||
writeFile(t, memFS, "input.txt", []byte("Hello, ")) | ||
|
||
// Configure what resources you share with the WASI program. | ||
wasiConfig := wazero.NewWASIConfig() | ||
// Share fs.FS as pre-opened directories | ||
wasiConfig = wasiConfig.WithPreopens( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feels like WithXXXMount will be easier for people to understand that "preopens" WDYT? Also, we can make it chained instead of a map. the map can be an internal detail Ex.
|
||
map[string]fs.FS{ | ||
".": memFS, // pre-open the writable in-memory directory at the working directory | ||
"/input": embeddedFS, // pre-open the embeddedFS at "/input" | ||
}, | ||
) | ||
// the wasm module will concatenate the contents of the two input.txt and write to output.txt | ||
wasiConfig = wasiConfig.WithArgs("./input.txt", "/input/input.txt", "./output.txt") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just overwrite a file instead? that's a lot simpler and less code. |
||
|
||
wasi, err := r.InstantiateModule(wazero.WASISnapshotPreview1WithConfig(wasiConfig)) | ||
require.NoError(t, err) | ||
defer wasi.Close() | ||
|
@@ -61,7 +70,9 @@ func Test_file_system(t *testing.T) { | |
require.NoError(t, err) | ||
defer mod.Close() | ||
|
||
out, err := readFile(memFS, "output.txt") | ||
// "input.txt" in the MemFS has "Hello, ", and "/testdata/file_system_input/input.txt" has "file system!\n". | ||
// So, the result "output.txt" should contain "Hello, file system!\n". | ||
out, err := fs.ReadFile(memFS, "output.txt") | ||
require.NoError(t, err) | ||
require.Equal(t, "Hello, file system!", string(out)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
file system! |
Uh oh!
There was an error while loading. Please reload this page.