package sysfs

import "github.com/tetratelabs/wazero/internal/sysfs"

Package sysfs includes a low-level filesystem interface and utilities needed for WebAssembly host functions (ABI) such as WASI and runtime.GOOS=js.

The name sysfs was chosen because wazero's public API has a "sys" package, which was named after https://github.com/golang/sys.

Index

Functions

func FileDatasync

func FileDatasync(f fs.File) (err syscall.Errno)

FileDatasync is like syscall.Fdatasync except that's only defined in linux.

func ReaderAtOffset

func ReaderAtOffset(f fs.File, offset int64) io.Reader

ReaderAtOffset gets an io.Reader from a fs.File that reads from an offset, yet doesn't affect the underlying position. This is used to implement syscall.Pread.

Note: The file accessed shouldn't be used concurrently, but wasm isn't safe to use concurrently anyway. Hence, we don't do any locking against parallel reads.

func StripPrefixesAndTrailingSlash

func StripPrefixesAndTrailingSlash(path string) string

func WriterAtOffset

func WriterAtOffset(f fs.File, offset int64) io.Writer

WriterAtOffset gets an io.Writer from a fs.File that writes to an offset, yet doesn't affect the underlying position. This is used to implement syscall.Pwrite.

Types

type CompositeFS

type CompositeFS struct {
	UnimplementedFS
	// contains filtered or unexported fields
}

func (*CompositeFS) Chmod

func (c *CompositeFS) Chmod(path string, perm fs.FileMode) syscall.Errno

Chmod implements FS.Chmod

func (*CompositeFS) Chown

func (c *CompositeFS) Chown(path string, uid, gid int) syscall.Errno

Chown implements FS.Chown

func (*CompositeFS) FS

func (c *CompositeFS) FS() (fs []FS)

FS returns the underlying filesystems in original order.

func (*CompositeFS) GuestPaths

func (c *CompositeFS) GuestPaths() (guestPaths []string)

GuestPaths returns the underlying pre-open paths in original order.

func (*CompositeFS) Lchown

func (c *CompositeFS) Lchown(path string, uid, gid int) syscall.Errno

Lchown implements FS.Lchown

func (c *CompositeFS) Link(oldName, newName string) syscall.Errno

Link implements FS.Link.

func (*CompositeFS) Lstat

func (c *CompositeFS) Lstat(path string) (platform.Stat_t, syscall.Errno)

Lstat implements FS.Lstat

func (*CompositeFS) Mkdir

func (c *CompositeFS) Mkdir(path string, perm fs.FileMode) syscall.Errno

Mkdir implements FS.Mkdir

func (*CompositeFS) Open

func (c *CompositeFS) Open(name string) (fs.File, error)

Open implements the same method as documented on fs.FS

func (*CompositeFS) OpenFile

func (c *CompositeFS) OpenFile(path string, flag int, perm fs.FileMode) (f fs.File, err syscall.Errno)

OpenFile implements FS.OpenFile

func (c *CompositeFS) Readlink(path string) (string, syscall.Errno)

Readlink implements FS.Readlink

func (*CompositeFS) Rename

func (c *CompositeFS) Rename(from, to string) syscall.Errno

Rename implements FS.Rename

func (*CompositeFS) Rmdir

func (c *CompositeFS) Rmdir(path string) syscall.Errno

Rmdir implements FS.Rmdir

func (*CompositeFS) Stat

func (c *CompositeFS) Stat(path string) (platform.Stat_t, syscall.Errno)

Stat implements FS.Stat

func (*CompositeFS) String

func (c *CompositeFS) String() string

String implements fmt.Stringer

func (c *CompositeFS) Symlink(oldName, link string) (err syscall.Errno)

Symlink implements FS.Symlink

func (*CompositeFS) Truncate

func (c *CompositeFS) Truncate(path string, size int64) syscall.Errno

Truncate implements FS.Truncate

func (c *CompositeFS) Unlink(path string) syscall.Errno

Unlink implements FS.Unlink

func (*CompositeFS) Utimens

func (c *CompositeFS) Utimens(path string, times *[2]syscall.Timespec, symlinkFollow bool) syscall.Errno

Utimens implements FS.Utimens

type FS

type FS interface {
	// String should return a human-readable format of the filesystem
	//
	// For example, if this filesystem is backed by the real directory
	// "/tmp/wasm", the expected value is "/tmp/wasm".
	//
	// When the host filesystem isn't a real filesystem, substitute a symbolic,
	// human-readable name. e.g. "virtual"
	String() string

	// OpenFile is similar to os.OpenFile, except the path is relative to this
	// file system, and syscall.Errno are returned instead of an os.PathError.
	// A zero syscall.Errno is success.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `path` or `flag` is invalid.
	//   - syscall.ENOENT: `path` doesn't exist and `flag` doesn't contain
	//     os.O_CREATE.
	//
	// # Constraints on the returned file
	//
	// Implementations that can read flags should enforce them regardless of
	// the type returned. For example, while os.File implements io.Writer,
	// attempts to write to a directory or a file opened with os.O_RDONLY fail
	// with a syscall.EBADF.
	//
	// Some implementations choose whether to enforce read-only opens, namely
	// fs.FS. While fs.FS is supported (Adapt), wazero cannot runtime enforce
	// open flags. Instead, we encourage good behavior and test our built-in
	// implementations.
	//
	// # Notes
	//
	//   - flag are the same as OpenFile, for example, os.O_CREATE.
	//   - Implications of permissions when os.O_CREATE are described in Chmod
	//     notes.
	OpenFile(path string, flag int, perm fs.FileMode) (fs.File, syscall.Errno)

	// Lstat is similar to syscall.Lstat, except the path is relative to this
	// file system.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.ENOENT: `path` doesn't exist.
	//
	// # Notes
	//
	//   - An fs.FileInfo backed implementation sets atim, mtim and ctim to the
	//     same value.
	//   - When the path is a symbolic link, the stat returned is for the link,
	//     not the file it refers to.
	Lstat(path string) (platform.Stat_t, syscall.Errno)

	// Stat is similar to syscall.Stat, except the path is relative to this
	// file system.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.ENOENT: `path` doesn't exist.
	//
	// # Notes
	//
	//   - An fs.FileInfo backed implementation sets atim, mtim and ctim to the
	//     same value.
	//   - When the path is a symbolic link, the stat returned is for the file
	//     it refers to.
	Stat(path string) (platform.Stat_t, syscall.Errno)

	// Mkdir is similar to os.Mkdir, except the path is relative to this file
	// system, and syscall.Errno are returned instead of a os.PathError. A zero
	// syscall.Errno is success.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `path` is invalid.
	//   - syscall.EEXIST: `path` exists and is a directory.
	//   - syscall.ENOTDIR: `path` exists and is a file.
	//
	// # Notes
	//
	//   - Implications of permissions are described in Chmod notes.
	Mkdir(path string, perm fs.FileMode) syscall.Errno

	// Chmod is similar to os.Chmod, except the path is relative to this file
	// system, and syscall.Errno are returned instead of a os.PathError. A zero
	// syscall.Errno is success.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `path` is invalid.
	//   - syscall.ENOENT: `path` does not exist.
	//
	// # Notes
	//
	//   - Windows ignores the execute bit, and any permissions come back as
	//     group and world. For example, chmod of 0400 reads back as 0444, and
	//     0700 0666. Also, permissions on directories aren't supported at all.
	Chmod(path string, perm fs.FileMode) syscall.Errno

	// Chown is like os.Chown except the path is relative to this file
	// system, and syscall.Errno are returned instead of an os.PathError.
	// A zero syscall.Errno is success.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `path` is invalid.
	//   - syscall.ENOENT: `path` does not exist.
	//
	// # Notes
	//
	//   - Windows will always return syscall.ENOSYS
	//   - This is similar to https://linux.die.net/man/3/chown
	Chown(path string, uid, gid int) syscall.Errno

	// Lchown is like os.Lchown except the path is relative to this file
	// system, and syscall.Errno are returned instead of an os.PathError. A
	// zero syscall.Errno is success.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `path` is invalid.
	//   - syscall.ENOENT: `path` does not exist.
	//
	// # Notes
	//
	//   - Windows will always return syscall.ENOSYS
	//   - This is similar to https://linux.die.net/man/3/lchown
	Lchown(path string, uid, gid int) syscall.Errno

	// Rename is similar to syscall.Rename, except the path is relative to this
	// file system.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `from` or `to` is invalid.
	//   - syscall.ENOENT: `from` or `to` don't exist.
	//   - syscall.ENOTDIR: `from` is a directory and `to` exists as a file.
	//   - syscall.EISDIR: `from` is a file and `to` exists as a directory.
	//   - syscall.ENOTEMPTY: `both from` and `to` are existing directory, but
	//    `to` is not empty.
	//
	// # Notes
	//
	//   -  Windows doesn't let you overwrite an existing directory.
	Rename(from, to string) syscall.Errno

	// Rmdir is similar to syscall.Rmdir, except the path is relative to this
	// file system.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `path` is invalid.
	//   - syscall.ENOENT: `path` doesn't exist.
	//   - syscall.ENOTDIR: `path` exists, but isn't a directory.
	//   - syscall.ENOTEMPTY: `path` exists, but isn't empty.
	//
	// # Notes
	//
	//   - As of Go 1.19, Windows maps syscall.ENOTDIR to syscall.ENOENT.
	Rmdir(path string) syscall.Errno

	// Unlink is similar to syscall.Unlink, except the path is relative to this
	// file system.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `path` is invalid.
	//   - syscall.ENOENT: `path` doesn't exist.
	//   - syscall.EISDIR: `path` exists, but is a directory.
	//
	// # Notes
	//
	//   - On Windows, syscall.Unlink doesn't delete symlink to directory unlike other platforms. Implementations might
	//     want to combine syscall.RemoveDirectory with syscall.Unlink in order to delete such links on Windows.
	//     See https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-removedirectorya
	Unlink(path string) syscall.Errno

	// Link is similar to syscall.Link, except the path is relative to this
	// file system. This creates "hard" link from oldPath to newPath, in
	// contrast to soft link as in Symlink.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EPERM: `oldPath` is invalid.
	//   - syscall.ENOENT: `oldPath` doesn't exist.
	//   - syscall.EISDIR: `newPath` exists, but is a directory.
	Link(oldPath, newPath string) syscall.Errno

	// Symlink is similar to syscall.Symlink, except the `oldPath` is relative
	// to this file system. This creates "soft" link from oldPath to newPath,
	// in contrast to hard link as in Link.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EPERM: `oldPath` or `newPath` is invalid.
	//   - syscall.EEXIST: `newPath` exists.
	//
	// # Notes
	//
	//   - Only `newPath` is relative to this file system and `oldPath` is kept
	//     as-is. That is because the link is only resolved relative to the
	//     directory when dereferencing it (e.g. ReadLink).
	//     See https://github.com/bytecodealliance/cap-std/blob/v1.0.4/cap-std/src/fs/dir.rs#L404-L409
	//     for how others implement this.
	//   - Symlinks in Windows requires `SeCreateSymbolicLinkPrivilege`.
	//     Otherwise, syscall.EPERM results.
	//     See https://learn.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links
	Symlink(oldPath, linkName string) syscall.Errno

	// Readlink is similar to syscall.Readlink, except the path is relative to
	// this file system.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `path` is invalid.
	//
	// # Notes
	//   - On Windows, the path separator is different from other platforms,
	//     but to provide consistent results to Wasm, this normalizes to a "/"
	//     separator.
	Readlink(path string) (string, syscall.Errno)

	// Truncate is similar to syscall.Truncate, except the path is relative to
	// this file system.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `path` is invalid or size is negative.
	//   - syscall.ENOENT: `path` doesn't exist
	//   - syscall.EACCES: `path` doesn't have write access.
	Truncate(path string, size int64) syscall.Errno

	// Utimens set file access and modification times on a path relative to
	// this file system, at nanosecond precision.
	//
	// # Parameters
	//
	// The `times` parameter includes the access and modification timestamps to
	// assign. Special syscall.Timespec NSec values UTIME_NOW and UTIME_OMIT may be
	// specified instead of real timestamps. A nil `times` parameter behaves the
	// same as if both were set to UTIME_NOW.
	//
	// When the `symlinkFollow` parameter is true and the path is a symbolic link,
	// the target of expanding that link is updated.
	//
	// # Errors
	//
	// The following errors are expected:
	//   - syscall.EINVAL: `path` is invalid.
	//   - syscall.EEXIST: `path` exists and is a directory.
	//   - syscall.ENOTDIR: `path` exists and is a file.
	//
	// # Notes
	//
	//   - This is similar to syscall.Utimens, except that doesn't have flags to
	//     control expansion of symbolic links. It also doesn't support special
	//     values UTIME_NOW or UTIME_NOW.
	//   - This is like `utimensat` with `AT_FDCWD` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/futimens.html
	Utimens(path string, times *[2]syscall.Timespec, symlinkFollow bool) syscall.Errno
}

FS is a writeable fs.FS bridge backed by syscall functions needed for ABI including WASI and runtime.GOOS=js.

Implementations should embed UnimplementedFS for forward compatability. Any unsupported method or parameter should return syscall.ENOSYS.

Errors

All methods that can return an error return a syscall.Errno, which is zero on success.

Restricting to syscall.Errno matches current WebAssembly host functions, which are constrained to well-known error codes. For example, `GOOS=js` maps hard coded values and panics otherwise. More commonly, WASI maps syscall errors to u32 numeric values.

Notes

A writable filesystem abstraction is not yet implemented as of Go 1.20. See https://github.com/golang/go/issues/45757

func Adapt

func Adapt(fs fs.FS) FS

Adapt adapts the input to FS unless it is already one. Use NewDirFS instead of os.DirFS as it handles interop issues such as windows support.

Note: This performs no flag verification on FS.OpenFile. fs.FS cannot read flags as there is no parameter to pass them through with. Moreover, fs.FS documentation does not require the file to be present. In summary, we can't enforce flag behavior.

func NewDirFS

func NewDirFS(dir string) FS

func NewReadFS

func NewReadFS(fs FS) FS

NewReadFS is used to mask an existing FS for reads. Notably, this allows the CLI to do read-only mounts of directories the host user can write, but doesn't want the guest wasm to. For example, Python libraries shouldn't be written to at runtime by the python wasm file.

func NewRootFS

func NewRootFS(fs []FS, guestPaths []string) (FS, error)

type UnimplementedFS

type UnimplementedFS struct{}

UnimplementedFS is an FS that returns syscall.ENOSYS for all functions, This should be embedded to have forward compatible implementations.

func (UnimplementedFS) Chmod

func (UnimplementedFS) Chmod(path string, perm fs.FileMode) syscall.Errno

Chmod implements FS.Chmod

func (UnimplementedFS) Chown

func (UnimplementedFS) Chown(path string, uid, gid int) syscall.Errno

Chown implements FS.Chown

func (UnimplementedFS) Lchown

func (UnimplementedFS) Lchown(path string, uid, gid int) syscall.Errno

Lchown implements FS.Lchown

func (UnimplementedFS) Link(_, _ string) syscall.Errno

Link implements FS.Link

func (UnimplementedFS) Lstat

Lstat implements FS.Lstat

func (UnimplementedFS) Mkdir

func (UnimplementedFS) Mkdir(path string, perm fs.FileMode) syscall.Errno

Mkdir implements FS.Mkdir

func (UnimplementedFS) Open

func (UnimplementedFS) Open(name string) (fs.File, error)

Open implements the same method as documented on fs.FS

func (UnimplementedFS) OpenFile

func (UnimplementedFS) OpenFile(path string, flag int, perm fs.FileMode) (fs.File, syscall.Errno)

OpenFile implements FS.OpenFile

func (UnimplementedFS) Readlink(path string) (string, syscall.Errno)

Readlink implements FS.Readlink

func (UnimplementedFS) Rename

func (UnimplementedFS) Rename(from, to string) syscall.Errno

Rename implements FS.Rename

func (UnimplementedFS) Rmdir

func (UnimplementedFS) Rmdir(path string) syscall.Errno

Rmdir implements FS.Rmdir

func (UnimplementedFS) Stat

Stat implements FS.Stat

func (UnimplementedFS) String

func (UnimplementedFS) String() string

String implements fmt.Stringer

func (UnimplementedFS) Symlink(_, _ string) syscall.Errno

Symlink implements FS.Symlink

func (UnimplementedFS) Truncate

Truncate implements FS.Truncate

func (UnimplementedFS) Unlink(path string) syscall.Errno

Unlink implements FS.Unlink

func (UnimplementedFS) Utimens

func (UnimplementedFS) Utimens(path string, times *[2]syscall.Timespec, symlinkFollow bool) syscall.Errno

Utimens implements FS.Utimens

Source Files

adapter.go dirfs.go readfs.go rootfs.go sysfs.go unsupported.go

Version
v1.0.2
Published
Apr 18, 2023
Platform
linux/amd64
Imports
10 packages
Last checked
2 minutes ago

Tools for package owners.