Skip to content

Commit

Permalink
Exposes writeable filesystem as experimentalsys.FS (#1605)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
codefromthecrypt authored Jul 30, 2023
1 parent d88286b commit 1f8c908
Show file tree
Hide file tree
Showing 67 changed files with 1,058 additions and 1,000 deletions.
10 changes: 6 additions & 4 deletions cmd/wazero/wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/tetratelabs/wazero/experimental/gojs"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/experimental/sock"
"github.com/tetratelabs/wazero/experimental/sysfs"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/internal/platform"
internalsys "github.com/tetratelabs/wazero/internal/sys"
Expand Down Expand Up @@ -394,7 +395,7 @@ func validateMounts(mounts sliceFlag, stdErr logging.Writer) (rc int, rootPath s
readOnly = true
}

// TODO(anuraaga): Support wasm paths with colon in them.
// TODO: Support wasm paths with colon in them.
var dir, guestPath string
if clnIdx := strings.LastIndexByte(mount, ':'); clnIdx != -1 {
dir, guestPath = mount[:clnIdx], mount[clnIdx+1:]
Expand All @@ -418,12 +419,13 @@ func validateMounts(mounts sliceFlag, stdErr logging.Writer) (rc int, rootPath s
fmt.Fprintf(stdErr, "invalid mount: path %q is not a directory\n", dir)
}

root := sysfs.NewDirFS(dir)
if readOnly {
config = config.WithReadOnlyDirMount(dir, guestPath)
} else {
config = config.WithDirMount(dir, guestPath)
root = sysfs.NewReadFS(root)
}

config = config.(sysfs.FSConfig).WithSysFSMount(root, guestPath)

if internalsys.StripPrefixesAndTrailingSlash(guestPath) == "" {
rootPath = dir
}
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"time"

"github.com/tetratelabs/wazero/api"
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/internal/engine/compiler"
"github.com/tetratelabs/wazero/internal/engine/interpreter"
"github.com/tetratelabs/wazero/internal/filecache"
"github.com/tetratelabs/wazero/internal/fsapi"
"github.com/tetratelabs/wazero/internal/internalapi"
"github.com/tetratelabs/wazero/internal/platform"
internalsock "github.com/tetratelabs/wazero/internal/sock"
Expand Down Expand Up @@ -846,7 +846,7 @@ func (c *moduleConfig) toSysContext() (sysCtx *internalsys.Context, err error) {
environ = append(environ, result)
}

var fs []fsapi.FS
var fs []experimentalsys.FS
var guestPaths []string
if f, ok := c.fsConfig.(*fsConfig); ok {
fs, guestPaths = f.preopens()
Expand Down
39 changes: 19 additions & 20 deletions internal/fsapi/dir.go → experimental/sys/dir.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package fsapi
package sys

import (
"fmt"
"io/fs"

experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
"github.com/tetratelabs/wazero/sys"
)

Expand All @@ -21,7 +20,7 @@ type FileType = fs.FileMode
// - This extends `dirent` defined in POSIX with some fields defined by
// Linux. See https://man7.org/linux/man-pages/man3/readdir.3.html and
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/dirent.h.html
// - This has a subset of fields defined in Stat_t. Notably, there is no
// - This has a subset of fields defined in sys.Stat_t. Notably, there is no
// field corresponding to Stat_t.Dev because that value will be constant
// for all files in a directory. To get the Dev value, call File.Stat on
// the directory File.Readdir was called on.
Expand Down Expand Up @@ -58,8 +57,8 @@ func (DirFile) IsAppend() bool {
}

// SetAppend implements File.SetAppend
func (DirFile) SetAppend(bool) experimentalsys.Errno {
return experimentalsys.EISDIR
func (DirFile) SetAppend(bool) Errno {
return EISDIR
}

// IsNonblock implements File.IsNonblock
Expand All @@ -68,41 +67,41 @@ func (DirFile) IsNonblock() bool {
}

// SetNonblock implements File.SetNonblock
func (DirFile) SetNonblock(bool) experimentalsys.Errno {
return experimentalsys.EISDIR
func (DirFile) SetNonblock(bool) Errno {
return EISDIR
}

// IsDir implements File.IsDir
func (DirFile) IsDir() (bool, experimentalsys.Errno) {
func (DirFile) IsDir() (bool, Errno) {
return true, 0
}

// Read implements File.Read
func (DirFile) Read([]byte) (int, experimentalsys.Errno) {
return 0, experimentalsys.EISDIR
func (DirFile) Read([]byte) (int, Errno) {
return 0, EISDIR
}

// Pread implements File.Pread
func (DirFile) Pread([]byte, int64) (int, experimentalsys.Errno) {
return 0, experimentalsys.EISDIR
func (DirFile) Pread([]byte, int64) (int, Errno) {
return 0, EISDIR
}

// Poll implements File.Poll
func (DirFile) Poll(Pflag, int32) (ready bool, errno experimentalsys.Errno) {
return false, experimentalsys.ENOSYS
func (DirFile) Poll(Pflag, int32) (ready bool, errno Errno) {
return false, ENOSYS
}

// Write implements File.Write
func (DirFile) Write([]byte) (int, experimentalsys.Errno) {
return 0, experimentalsys.EISDIR
func (DirFile) Write([]byte) (int, Errno) {
return 0, EISDIR
}

// Pwrite implements File.Pwrite
func (DirFile) Pwrite([]byte, int64) (int, experimentalsys.Errno) {
return 0, experimentalsys.EISDIR
func (DirFile) Pwrite([]byte, int64) (int, Errno) {
return 0, EISDIR
}

// Truncate implements File.Truncate
func (DirFile) Truncate(int64) experimentalsys.Errno {
return experimentalsys.EISDIR
func (DirFile) Truncate(int64) Errno {
return EISDIR
}
Loading

0 comments on commit 1f8c908

Please sign in to comment.