fs: adds FSConfig to replace experimental writefs (#1061)
This adds a new top-level type FSConfig, which is configured via
`ModuleConfig.WithFSConfig(fcfg)`. This implements read-only and
read-write directory mounts, something not formally supported before. It
also implements `WithFS` which adapts a normal `fs.FS`. For convenience,
we retain the old `ModuleConfig.WithFS` signature so as to not affect
existing users much. A new configuration for our emerging raw
filesystem, `FSConfig.WithSysfs()` will happen later without breaking
this API.
Here's an example:
```
moduleConfig = wazero.NewModuleConfig().
// Make the current directory read-only accessible to the guest.
WithReadOnlyDirMount(".", "/")
// Make "/tmp/wasm" accessible to the guest as "/tmp".
WithDirMount("/tmp/wasm", "/tmp")
```
Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
27
fsconfig_example_test.go
Normal file
27
fsconfig_example_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package wazero_test
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"log"
|
||||
|
||||
"github.com/tetratelabs/wazero"
|
||||
)
|
||||
|
||||
//go:embed testdata/index.html
|
||||
var testdataIndex embed.FS
|
||||
|
||||
var moduleConfig wazero.ModuleConfig
|
||||
|
||||
// This example shows how to configure an embed.FS.
|
||||
func Example_withFSConfig_embedFS() {
|
||||
// Strip the embedded path testdata/
|
||||
rooted, err := fs.Sub(testdataIndex, "testdata")
|
||||
if err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
moduleConfig = wazero.NewModuleConfig().
|
||||
// Make "index.html" accessible to the guest as "/index.html".
|
||||
WithFSConfig(wazero.NewFSConfig().WithFSMount(rooted, "/"))
|
||||
}
|
||||
Reference in New Issue
Block a user