Skip to content
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

Expose fsConfig.WithSysFSMount for custom writable FS support #2076

Open
paralin opened this issue Feb 20, 2024 · 5 comments
Open

Expose fsConfig.WithSysFSMount for custom writable FS support #2076

paralin opened this issue Feb 20, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@paralin
Copy link

paralin commented Feb 20, 2024

I would like to implement a read/write filesystem in pure-Go and in memory, (not exposing an os fs).

The writable fs.FS proposal for Go was declined for the time being: golang/go#45757

There is a comment still mentioning this proposal here:

// https://github.com/golang/go/issues/45757

experimental/sys.FS is the writable FS interface:

type FS interface {

fsConfig has WithSysFSMount but it is not exposed in the FSConfig interface and is therefore inaccessible:

func (c *fsConfig) WithSysFSMount(fs experimentalsys.FS, guestPath string) FSConfig {

I want to write an implementation of experimental/sys.FS and provide it to wazero via WithSysFSMount.

Could WithSysFSMount be added to fsConfig or otherwise be exposed so I can call it? Thanks!

@paralin paralin added the enhancement New feature or request label Feb 20, 2024
@paralin
Copy link
Author

paralin commented Feb 20, 2024

I can expose it like this:

// FSConfigWithSysFSMount extends FSConfig to expose the existing WithSysFSMount function.
// https://github.com/tetratelabs/wazero/issues/2076
type FSConfigWithSysFSMount interface {
	WithSysFSMount(fs wazero_sys.FS, guestPath string) wazero.FSConfig
}

var writableFS wazero_sys.FS
fsConf.(FSConfigWithSysFSMount).WithSysFSMount(writableFS, "/")

But this is a runtime type assertion and can't be enforced at compile time, ideally FSConfigWithSysFSMount would be exposed by wazero and type-asserted that fsConfig conforms to that exported interface.

@evacchi
Copy link
Contributor

evacchi commented Feb 20, 2024

Admittedly, I think this may not be easy to find, but you should be able to write the following:

import (
	"github.com/tetratelabs/wazero"
	"github.com/tetratelabs/wazero/experimental/sysfs"
)

func main() {
	var sys.FS = ...
	config := wazero.NewFSConfig()
	config.(sysfs.FSConfig).WithSysFSMount(myFs, guestPath)
	...
}

e.g. see

func ExampleAdaptFS() {
m := fstest.MapFS{
"a/b.txt": &fstest.MapFile{Mode: 0o666},
".": &fstest.MapFile{Mode: 0o777 | fs.ModeDir},
}
root := &sysfs.AdaptFS{FS: m}
moduleConfig = wazero.NewModuleConfig().
WithFSConfig(wazero.NewFSConfig().(sysfs.FSConfig).WithSysFSMount(root, "/"))
}

@paralin
Copy link
Author

paralin commented Feb 20, 2024

Could that just be added to the main FSConfig or perhaps add a NewFSConfig to sysfs which returns sysfs.FSConfig and have the type assertion in wazero? That way we can trust that the type assertion won't panic (covered by wazero tests)

@evacchi
Copy link
Contributor

evacchi commented Feb 20, 2024

Eventually, they will. They are not yet because this API has not been tested extensively by our users so far. So, please do play with it and let us know what other roadblocks you encounter :)

@rockwotj
Copy link
Contributor

For those of you who just want to expose an in-memory FS to their module and was fighting wasip1 path_open calls getting the flags dropped, I have a (horrible?, glorious?) workaround here: https://github.com/rockwotj/redpanda-connect-ghostscript-plugin/blob/main/wasifs.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants