sys

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 8 Imported by: 3 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sys includes constants and types used by both public and internal APIs.

Index

Constants

View Source
const (
	// ExitCodeContextCanceled corresponds to context.Canceled and returned by ExitError.ExitCode in that case.
	ExitCodeContextCanceled uint32 = 0xffffffff
	// ExitCodeDeadlineExceeded corresponds to context.DeadlineExceeded and returned by ExitError.ExitCode in that case.
	ExitCodeDeadlineExceeded uint32 = 0xefffffff
)

These two special exit codes are reserved by wazy for context Cancel and Timeout integrations. The assumption here is that well-behaving Wasm programs won't use these two exit codes.

View Source
const UTIME_OMIT int64 = math.MinInt64

UTIME_OMIT is a special constant for use in updating times via FS.Utimens or File.Utimens. When used for atim or mtim, the value is retained.

Note: This may be implemented via a stat when the underlying filesystem does not support this value.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClockResolution

type ClockResolution uint32

ClockResolution is a positive granularity of clock precision in nanoseconds. For example, if the resolution is 1us, this returns 1000.

Note: Some implementations return arbitrary resolution because there's no perfect alternative. For example, according to the source in time.go, windows monotonic resolution can be 15ms. See /RATIONALE.md.

type DirFile

type DirFile struct{}

DirFile is embeddable to reduce the amount of functions to implement a file.

func (DirFile) IsAppend

func (DirFile) IsAppend() bool

IsAppend implements File.IsAppend

func (DirFile) IsDir

func (DirFile) IsDir() (bool, Errno)

IsDir implements File.IsDir

func (DirFile) Pread

func (DirFile) Pread([]byte, int64) (int, Errno)

Pread implements File.Pread

func (DirFile) Pwrite

func (DirFile) Pwrite([]byte, int64) (int, Errno)

Pwrite implements File.Pwrite

func (DirFile) Read

func (DirFile) Read([]byte) (int, Errno)

Read implements File.Read

func (DirFile) SetAppend

func (DirFile) SetAppend(bool) Errno

SetAppend implements File.SetAppend

func (DirFile) Truncate

func (DirFile) Truncate(int64) Errno

Truncate implements File.Truncate

func (DirFile) Write

func (DirFile) Write([]byte) (int, Errno)

Write implements File.Write

type Dirent

type Dirent struct {
	// Ino is the file serial number, or zero if not available. See Ino for
	// more details including impact returning a zero value.
	Ino Inode

	// Name is the base name of the directory entry. Empty is invalid.
	Name string

	// Type is fs.FileMode masked on fs.ModeType. For example, zero is a
	// regular file, fs.ModeDir is a directory and fs.ModeIrregular is unknown.
	//
	// Note: This is defined by Linux, not POSIX.
	Type fs.FileMode
}

Dirent is an entry read from a directory via File.Readdir.

Notes

func (*Dirent) IsDir

func (d *Dirent) IsDir() bool

IsDir returns true if the Type is fs.ModeDir.

func (*Dirent) String

func (d *Dirent) String() string

type EpochNanos

type EpochNanos = int64

EpochNanos is a timestamp in epoch nanoseconds, or zero if unknown.

This defines epoch time the same way as Walltime, except this value is packed into an int64. Common conversions are detailed in the examples.

type Errno

type Errno uint16

Errno is a subset of POSIX errno used by wazy interfaces. Zero is not an error. Other values should not be interpreted numerically, rather by constants prefixed with 'E'.

See https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html

const (
	EACCES Errno = iota + 1
	EAGAIN
	EBADF
	EEXIST
	EFAULT
	EINTR
	EINVAL
	EIO
	EISDIR
	ELOOP
	ENAMETOOLONG
	ENOENT
	ENOSYS
	ENOTDIR
	ERANGE
	ENOTEMPTY
	ENOTSOCK
	ENOTSUP
	EPERM
	EROFS
)

This is a subset of errors to reduce implementation burden. `wasip1` defines almost all POSIX error numbers, but not all are used in practice. wazy will add ones needed in POSIX order, as needed by functions that explicitly document returning them.

https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-errno-enumu16

func UnwrapOSError

func UnwrapOSError(err error) Errno

UnwrapOSError returns an Errno or zero if the input is nil.

func (Errno) Error

func (e Errno) Error() string

Error implements error

func (Errno) Unwrap

func (e Errno) Unwrap() error

Unwrap is a convenience for runtime.GOOS which define syscall.Errno.

type ExitError

type ExitError struct {
	// contains filtered or unexported fields
}

ExitError is returned to a caller of api.Function when api.Module CloseWithExitCode was invoked, or context.Context passed to api.Function Call was canceled or reached the Timeout.

ExitCode zero value means success while any other value is an error.

Here's an example of how to get the exit code:

main := module.ExportedFunction("main")
if err := main(ctx); err != nil {
	if exitErr, ok := err.(*sys.ExitError); ok {
		// This means your module exited with non-zero code!
	}
--snip--

Note: While possible the reason of this was "proc_exit" from "wasi_snapshot_preview1", it could be from other host functions, for example an AssemblyScript's abort handler, or any arbitrary caller of CloseWithExitCode.

See https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#proc_exit and https://www.assemblyscript.org/concepts.html#special-imports

Note: In the case of context cancellation or timeout, the api.Module from which the api.Function created is closed.

func NewExitError

func NewExitError(exitCode uint32) *ExitError

func (*ExitError) Error

func (e *ExitError) Error() string

Error implements the error interface.

func (*ExitError) ExitCode

func (e *ExitError) ExitCode() uint32

ExitCode returns zero on success, and an arbitrary value otherwise.

func (*ExitError) Is

func (e *ExitError) Is(err error) bool

Is allows use via errors.Is

type FS

type FS interface {
	// OpenFile opens a file. It should be closed via Close on File.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EINVAL: `path` or `flag` is invalid.
	//   - EISDIR: the path was a directory, but flag included O_RDWR or
	//     O_WRONLY
	//   - ENOENT: `path` doesn't exist and `flag` doesn't contain O_CREAT.
	//
	// # 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 O_RDONLY fail
	// with a EBADF.
	//
	// Some implementations choose whether to enforce read-only opens, namely
	// fs.FS. While fs.FS is supported (Adapt), wazy cannot runtime enforce
	// open flags. Instead, we encourage good behavior and test our built-in
	// implementations.
	//
	// # Notes
	//
	//   - This is like os.OpenFile, except the path is relative to this file
	//     system, and Errno is returned instead of os.PathError.
	//   - Implications of permissions when O_CREAT are described in Chmod notes.
	//   - This is like `open` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html
	OpenFile(path string, flag Oflag, perm fs.FileMode) (File, Errno)

	// Lstat gets file status without following symbolic links.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - ENOENT: `path` doesn't exist.
	//
	// # Notes
	//
	//   - This is like syscall.Lstat, except the `path` is relative to this
	//     file system.
	//   - This is like `lstat` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/lstat.html
	//   - 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) (Stat_t, Errno)

	// Stat gets file status.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - ENOENT: `path` doesn't exist.
	//
	// # Notes
	//
	//   - This is like syscall.Stat, except the `path` is relative to this
	//     file system.
	//   - This is like `stat` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/stat.html
	//   - 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) (Stat_t, Errno)

	// Mkdir makes a directory.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EINVAL: `path` is invalid.
	//   - EEXIST: `path` exists and is a directory.
	//   - ENOTDIR: `path` exists and is a file.
	//
	// # Notes
	//
	//   - This is like syscall.Mkdir, except the `path` is relative to this
	//     file system.
	//   - This is like `mkdir` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html
	//   - Implications of permissions are described in Chmod notes.
	Mkdir(path string, perm fs.FileMode) Errno

	// Chmod changes the mode of the file.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EINVAL: `path` is invalid.
	//   - ENOENT: `path` does not exist.
	//
	// # Notes
	//
	//   - This is like syscall.Chmod, except the `path` is relative to this
	//     file system.
	//   - This is like `chmod` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html
	//   - 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) Errno

	// Rename renames file or directory.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EINVAL: `from` or `to` is invalid.
	//   - ENOENT: `from` or `to` don't exist.
	//   - ENOTDIR: `from` is a directory and `to` exists as a file.
	//   - EISDIR: `from` is a file and `to` exists as a directory.
	//   - ENOTEMPTY: `both from` and `to` are existing directory, but
	//    `to` is not empty.
	//
	// # Notes
	//
	//   - This is like syscall.Rename, except the paths are relative to this
	//     file system.
	//   - This is like `rename` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
	//   -  Windows doesn't let you overwrite an existing directory.
	Rename(from, to string) Errno

	// Rmdir removes a directory.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EINVAL: `path` is invalid.
	//   - ENOENT: `path` doesn't exist.
	//   - ENOTDIR: `path` exists, but isn't a directory.
	//   - ENOTEMPTY: `path` exists, but isn't empty.
	//
	// # Notes
	//
	//   - This is like syscall.Rmdir, except the `path` is relative to this
	//     file system.
	//   - This is like `rmdir` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/rmdir.html
	//   - As of Go 1.19, Windows maps ENOTDIR to ENOENT.
	Rmdir(path string) Errno

	// Unlink removes a directory entry.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EINVAL: `path` is invalid.
	//   - ENOENT: `path` doesn't exist.
	//   - EISDIR: `path` exists, but is a directory.
	//
	// # Notes
	//
	//   - This is like syscall.Unlink, except the `path` is relative to this
	//     file system.
	//   - This is like `unlink` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlink.html
	//   - 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) Errno

	// Link creates a "hard" link from oldPath to newPath, in contrast to a
	// soft link (via Symlink).
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EPERM: `oldPath` is invalid.
	//   - ENOENT: `oldPath` doesn't exist.
	//   - EISDIR: `newPath` exists, but is a directory.
	//
	// # Notes
	//
	//   - This is like syscall.Link, except the `oldPath` is relative to this
	//     file system.
	//   - This is like `link` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/link.html
	Link(oldPath, newPath string) Errno

	// Symlink creates a "soft" link from oldPath to newPath, in contrast to a
	// hard link (via Link).
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EPERM: `oldPath` or `newPath` is invalid.
	//   - EEXIST: `newPath` exists.
	//
	// # Notes
	//
	//   - This is like syscall.Symlink, except the `oldPath` is relative to
	//     this file system.
	//   - This is like `symlink` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlink.html
	//   - 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, EPERM results.
	//     See https://learn.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links
	Symlink(oldPath, linkName string) Errno

	// Readlink reads the contents of a symbolic link.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EINVAL: `path` is invalid.
	//
	// # Notes
	//
	//   - This is like syscall.Readlink, except the path is relative to this
	//     filesystem.
	//   - This is like `readlink` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html
	//   - 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, Errno)

	// Utimens set file access and modification times on a path relative to
	// this file system, at nanosecond precision.
	//
	// # Parameters
	//
	// If the path is a symbolic link, the target of expanding that link is
	// updated.
	//
	// The `atim` and `mtim` parameters refer to access and modification time
	// stamps as defined in Stat_t. To retain one or the other, substitute
	// it with the pseudo-timestamp UTIME_OMIT.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EINVAL: `path` is invalid.
	//   - EEXIST: `path` exists and is a directory.
	//   - ENOTDIR: `path` exists and is a file.
	//
	// # Notes
	//
	//   - This is like syscall.UtimesNano and `utimensat` with `AT_FDCWD` in
	//     POSIX. See https://pubs.opengroup.org/onlinepubs/9699919799/functions/futimens.html
	Utimens(path string, atim, mtim int64) Errno
}

FS is a writeable fs.FS bridge backed by syscall functions needed for ABI including WASI.

Implementations should embed UnimplementedFS for forward compatibility. Any unsupported method or parameter should return ENO

Errors

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

Restricting to Errno matches current WebAssembly host functions, which are constrained to well-known error codes. For example, 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

type File

type File interface {
	// Dev returns the device ID (Stat_t.Dev) of this file, zero if unknown or
	// an error retrieving it.
	//
	// # Errors
	//
	// Possible errors are those from Stat, except ENOSYS should not
	// be returned. Zero should be returned if there is no implementation.
	//
	// # Notes
	//
	//   - Implementations should cache this result.
	//   - This combined with Ino can implement os.SameFile.
	Dev() (uint64, Errno)

	// Ino returns the serial number (Stat_t.Ino) of this file, zero if unknown
	// or an error retrieving it.
	//
	// # Errors
	//
	// Possible errors are those from Stat, except ENOSYS should not
	// be returned. Zero should be returned if there is no implementation.
	//
	// # Notes
	//
	//   - Implementations should cache this result.
	//   - This combined with Dev can implement os.SameFile.
	Ino() (Inode, Errno)

	// IsDir returns true if this file is a directory or an error there was an
	// error retrieving this information.
	//
	// # Errors
	//
	// Possible errors are those from Stat, except ENOSYS should not
	// be returned. false should be returned if there is no implementation.
	//
	// # Notes
	//
	//   - Implementations should cache this result.
	IsDir() (bool, Errno)

	// IsAppend returns true if the file was opened with O_APPEND, or
	// SetAppend was successfully enabled on this file.
	//
	// # Notes
	//
	//   - This might not match the underlying state of the file descriptor if
	//     the file was not opened via OpenFile.
	IsAppend() bool

	// SetAppend toggles the append mode (O_APPEND) of this file.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file or directory was closed.
	//
	// # Notes
	//
	//   - There is no `O_APPEND` for `fcntl` in POSIX, so implementations may
	//     have to re-open the underlying file to apply this. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html
	SetAppend(enable bool) Errno

	// Stat is similar to syscall.Fstat.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file or directory was closed.
	//
	// # Notes
	//
	//   - This is like syscall.Fstat and `fstatat` with `AT_FDCWD` in POSIX.
	//     See https://pubs.opengroup.org/onlinepubs/9699919799/functions/stat.html
	//   - A fs.FileInfo backed implementation sets atim, mtim and ctim to the
	//     same value.
	//   - Windows allows you to stat a closed directory.
	Stat() (Stat_t, Errno)

	// Read attempts to read all bytes in the file into `buf`, and returns the
	// count read even on error.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file or directory was closed or not readable.
	//   - EISDIR: the file was a directory.
	//
	// # Notes
	//
	//   - This is like io.Reader and `read` in POSIX, preferring semantics of
	//     io.Reader. See https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html
	//   - Unlike io.Reader, there is no io.EOF returned on end-of-file. To
	//     read the file completely, the caller must repeat until `n` is zero.
	Read(buf []byte) (n int, errno Errno)

	// Pread attempts to read all bytes in the file into `p`, starting at the
	// offset `off`, and returns the count read even on error.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file or directory was closed or not readable.
	//   - EINVAL: the offset was negative.
	//   - EISDIR: the file was a directory.
	//
	// # Notes
	//
	//   - This is like io.ReaderAt and `pread` in POSIX, preferring semantics
	//     of io.ReaderAt. See https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html
	//   - Unlike io.ReaderAt, there is no io.EOF returned on end-of-file. To
	//     read the file completely, the caller must repeat until `n` is zero.
	Pread(buf []byte, off int64) (n int, errno Errno)

	// Seek attempts to set the next offset for Read or Write and returns the
	// resulting absolute offset or an error.
	//
	// # Parameters
	//
	// The `offset` parameters is interpreted in terms of `whence`:
	//   - io.SeekStart: relative to the start of the file, e.g. offset=0 sets
	//     the next Read or Write to the beginning of the file.
	//   - io.SeekCurrent: relative to the current offset, e.g. offset=16 sets
	//     the next Read or Write 16 bytes past the prior.
	//   - io.SeekEnd: relative to the end of the file, e.g. offset=-1 sets the
	//     next Read or Write to the last byte in the file.
	//
	// # Behavior when a directory
	//
	// The only supported use case for a directory is seeking to `offset` zero
	// (`whence` = io.SeekStart). This should have the same behavior as
	// os.File, which resets any internal state used by Readdir.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file or directory was closed or not readable.
	//   - EINVAL: the offset was negative.
	//
	// # Notes
	//
	//   - This is like io.Seeker and `fseek` in POSIX, preferring semantics
	//     of io.Seeker. See https://pubs.opengroup.org/onlinepubs/9699919799/functions/fseek.html
	Seek(offset int64, whence int) (newOffset int64, errno Errno)

	// Readdir reads the contents of the directory associated with file and
	// returns a slice of up to n Dirent values in an arbitrary order. This is
	// a stateful function, so subsequent calls return any next values.
	//
	// If n > 0, Readdir returns at most n entries or an error.
	// If n <= 0, Readdir returns all remaining entries or an error.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file was closed or not a directory.
	//   - ENOENT: the directory could not be read (e.g. deleted).
	//
	// # Notes
	//
	//   - This is like `Readdir` on os.File, but unlike `readdir` in POSIX.
	//     See https://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html
	//   - Unlike os.File, there is no io.EOF returned on end-of-directory. To
	//     read the directory completely, the caller must repeat until the
	//     count read (`len(dirents)`) is less than `n`.
	//   - See /RATIONALE.md for design notes.
	Readdir(n int) (dirents []Dirent, errno Errno)

	// Write attempts to write all bytes in `p` to the file, and returns the
	// count written even on error.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file was closed, not writeable, or a directory.
	//
	// # Notes
	//
	//   - This is like io.Writer and `write` in POSIX, preferring semantics of
	//     io.Writer. See https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html
	Write(buf []byte) (n int, errno Errno)

	// Pwrite attempts to write all bytes in `p` to the file at the given
	// offset `off`, and returns the count written even on error.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file or directory was closed or not writeable.
	//   - EINVAL: the offset was negative.
	//   - EISDIR: the file was a directory.
	//
	// # Notes
	//
	//   - This is like io.WriterAt and `pwrite` in POSIX, preferring semantics
	//     of io.WriterAt. See https://pubs.opengroup.org/onlinepubs/9699919799/functions/pwrite.html
	Pwrite(buf []byte, off int64) (n int, errno Errno)

	// Truncate truncates a file to a specified length.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file or directory was closed.
	//   - EINVAL: the `size` is negative.
	//   - EISDIR: the file was a directory.
	//
	// # Notes
	//
	//   - This is like syscall.Ftruncate and `ftruncate` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
	//   - Windows does not error when calling Truncate on a closed file.
	Truncate(size int64) Errno

	// Sync synchronizes changes to the file.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - EBADF: the file or directory was closed.
	//
	// # Notes
	//
	//   - This is like syscall.Fsync and `fsync` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/fsync.html
	//   - This returns with no error instead of ENOSYS when
	//     unimplemented. This prevents fake filesystems from erring.
	//   - Windows does not error when calling Sync on a closed file.
	Sync() Errno

	// Datasync synchronizes the data of a file.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - EBADF: the file or directory was closed.
	//
	// # Notes
	//
	//   - This is like syscall.Fdatasync and `fdatasync` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/fdatasync.html
	//   - This returns with no error instead of ENOSYS when
	//     unimplemented. This prevents fake filesystems from erring.
	//   - As this is commonly missing, some implementations dispatch to Sync.
	Datasync() Errno

	// Utimens set file access and modification times of this file, at
	// nanosecond precision.
	//
	// # Parameters
	//
	// The `atim` and `mtim` parameters refer to access and modification time
	// stamps as defined in Stat_t. To retain one or the other, substitute
	// it with the pseudo-timestamp UTIME_OMIT.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file or directory was closed.
	//
	// # Notes
	//
	//   - This is like syscall.UtimesNano and `futimens` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/futimens.html
	//   - Windows requires files to be open with O_RDWR, which means you
	//     cannot use this to update timestamps on a directory (EPERM).
	Utimens(atim, mtim int64) Errno

	// Close closes the underlying file.
	//
	// A zero Errno is returned if unimplemented or success.
	//
	// # Notes
	//
	//   - This is like syscall.Close and `close` in POSIX. See
	//     https://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html
	Close() Errno
}

File is a writeable fs.File bridge backed by syscall functions needed for ABI including WASI.

Implementations should embed UnimplementedFile for forward compatibility. Any unsupported method or parameter should return ENOSYS.

Errors

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

Restricting to Errno matches current WebAssembly host functions, which are constrained to well-known error codes. For example, WASI maps syscall errors to u32 numeric values.

Notes

  • You must call Close to avoid file resource conflicts. For example, Windows cannot delete the underlying directory while a handle to it remains open.
  • A writable filesystem abstraction is not yet implemented as of Go 1.20. See https://github.com/golang/go/issues/45757

type FileType

type FileType = fs.FileMode

FileType is fs.FileMode masked on fs.ModeType. For example, zero is a regular file, fs.ModeDir is a directory and fs.ModeIrregular is unknown.

Note: This is defined by Linux, not POSIX.

type Inode

type Inode = uint64

Inode is the file serial number, or zero if unknown.

Any constant value will invalidate functions that use this for equivalence, such as os.SameFile (Stat_t.Ino).

When zero is returned by a `readdir`, some compilers will attempt to get a non-zero value with `lstat`. Those using this for darwin's definition of `getdirentries` conflate zero `d_fileno` with a deleted file, so skip the entry. See /RATIONALE.md for more on this.

type Nanosleep

type Nanosleep func(ns int64)

Nanosleep puts the current goroutine to sleep for at least ns nanoseconds.

type Nanotime

type Nanotime func() int64

Nanotime returns nanoseconds since an arbitrary start point, used to measure elapsed time. This is sometimes referred to as a tick or monotonic time.

Note: There are no constraints on the value return except that it increments. For example, -1 is a valid if the next value is >= 0.

type Oflag

type Oflag uint32

Oflag are flags used for FS.OpenFile. Values, including zero, should not be interpreted numerically. Instead, use by constants prefixed with 'O_' with special casing noted below.

Notes

const (
	// O_RDONLY is like os.O_RDONLY
	O_RDONLY Oflag = iota

	// O_RDWR is like os.O_RDWR
	O_RDWR

	// O_WRONLY is like os.O_WRONLY
	O_WRONLY

	// O_APPEND is like os.O_APPEND
	O_APPEND Oflag = 1 << iota

	// O_CREAT is link os.O_CREATE
	O_CREAT

	// O_DIRECTORY is defined on some platforms as syscall.O_DIRECTORY.
	//
	// Note: This ensures that the opened file is a directory. Those emulating
	// on platforms that don't support the O_DIRECTORY, can double-check the
	// result with File.IsDir (or stat) and err if not a directory.
	O_DIRECTORY

	// O_DSYNC is defined on some platforms as syscall.O_DSYNC.
	O_DSYNC

	// O_EXCL is defined on some platforms as syscall.O_EXCL.
	O_EXCL

	// O_NOFOLLOW is defined on some platforms as syscall.O_NOFOLLOW.
	//
	// Note: This allows programs to ensure that if the opened file is a
	// symbolic link, the link itself is opened instead of its target.
	O_NOFOLLOW

	// O_NONBLOCK is defined on some platforms as syscall.O_NONBLOCK.
	O_NONBLOCK

	// O_RSYNC is defined on some platforms as syscall.O_RSYNC.
	O_RSYNC

	// O_SYNC is defined on some platforms as syscall.O_SYNC.
	O_SYNC

	// O_TRUNC is defined on some platforms as syscall.O_TRUNC.
	O_TRUNC
)

This is a subset of oflags to reduce implementation burden. `wasip1` splits these across `oflags` and `fdflags`. We can't rely on the Go `os` package, as it is missing some values. Any flags added will be defined in POSIX order, as needed by functions that explicitly document accepting them.

https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-oflags-flagsu16 https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fdflags-flagsu16

type Osyield

type Osyield func()

Osyield yields the processor, typically to implement spin-wait loops.

type Pflag

type Pflag uint32

Pflag are bit flags used for polling. Values, including zero, should not be interpreted numerically. Instead, use by constants prefixed with 'POLL'.

Notes

const (
	// POLLIN is a read event.
	POLLIN Pflag = 1 << iota

	// POLLOUT is a write event.
	POLLOUT
)

Only define bitflags we support and are needed by `poll_oneoff` in wasip1 See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#eventrwflags

type Pollable

type Pollable interface {
	Poll(flag Pflag, timeoutMillis int32) (ready bool, errno Errno)
}

Pollable is implemented by custom readers that support polling for readiness. If a custom io.Reader passed to WithStdin implements this interface, poll_oneoff will use it for asynchronous I/O instead of returning "always ready".

Parameters

The `flag` parameter determines which event to await, such as POLLIN, POLLOUT, or a combination like `POLLIN|POLLOUT`.

The `timeoutMillis` parameter is how long to block for an event, or interrupted, in milliseconds. There are two special values:

  • zero returns immediately
  • any negative value blocks any amount of time

Results

`ready` means there was data ready to read or written. False can mean no event was ready or `errno` is not zero.

A zero `errno` is success. The below are expected otherwise:

  • ENOSYS: the implementation does not support this function.
  • ENOTSUP: the implementation does not support the flag combination.
  • EINTR: the call was interrupted prior to an event.

type PollableFile

type PollableFile interface {
	File
	Pollable

	// IsNonblock returns true if the file was opened with O_NONBLOCK, or
	// SetNonblock was successfully enabled on this file.
	//
	// # Notes
	//
	//   - This might not match the underlying state of the file descriptor if
	//     the file was not opened via OpenFile.
	IsNonblock() bool

	// SetNonblock toggles the non-blocking mode (O_NONBLOCK) of this file.
	//
	// # Errors
	//
	// A zero Errno is success. The below are expected otherwise:
	//   - ENOSYS: the implementation does not support this function.
	//   - EBADF: the file or directory was closed.
	//
	// # Notes
	//
	//   - This is like syscall.SetNonblock and `fcntl` with O_NONBLOCK in
	//     POSIX. See https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
	SetNonblock(enable bool) Errno
}

PollableFile is a File that additionally supports polling for readiness and non-blocking mode. This is separated from File to avoid breaking downstream implementations that do not need polling.

Implementations should embed UnimplementedFile for forward compatibility of the base File methods, and add Poll, IsNonblock, and SetNonblock.

Notes

  • This is the public equivalent of the internal fsapi.File interface.
  • See Pollable for a standalone poll interface usable with io.Reader or fs.File implementations that are not full File implementations.

type Stat_t

type Stat_t struct {
	// Dev is the device ID of device containing the file.
	Dev uint64

	// Ino is the file serial number, or zero if not available. See Inode for
	// more details including impact returning a zero value.
	Ino Inode

	// Mode is the same as Mode on fs.FileInfo containing bits to identify the
	// type of the file (fs.ModeType) and its permissions (fs.ModePerm).
	Mode fs.FileMode

	// Nlink is the number of hard links to the file.
	//
	// Note: This value is platform-specific and often at least one. Linux will
	// return 1+N for a directory, where BSD (like Darwin) return 2+N, which
	// includes the dot entry.
	Nlink uint64

	// Size is the length in bytes for regular files. For symbolic links, this
	// is length in bytes of the pathname contained in the symbolic link.
	Size int64

	// Atim is the last data access timestamp in epoch nanoseconds.
	Atim EpochNanos

	// Mtim is the last data modification timestamp in epoch nanoseconds.
	Mtim EpochNanos

	// Ctim is the last file status change timestamp in epoch nanoseconds.
	Ctim EpochNanos
}

Stat_t is similar to syscall.Stat_t, except available on all operating systems, including Windows.

Notes

  • This is used for WebAssembly ABI emulating the POSIX `stat` system call. See https://pubs.opengroup.org/onlinepubs/9699919799/functions/stat.html
  • Fields here are required for WebAssembly ABI including wasip1 (a.k.a. wasix) and wasi-filesystem (a.k.a. wasip2).
  • This isn't the same as syscall.Stat_t because wazy supports Windows, which doesn't have that type. runtime.GOOS that has this already also have inconsistent field lengths, which complicates wasm binding.
  • Use NewStat_t to create this from an existing fs.FileInfo.
  • For portability, numeric fields are 64-bit when at least one platform defines it that large.

func NewStat_t

func NewStat_t(info fs.FileInfo) Stat_t

NewStat_t fills a new Stat_t from `info`, including any runtime.GOOS-specific details from fs.FileInfo `Sys`. When `Sys` is already a *Stat_t, it is returned as-is.

Notes

  • When already in fs.FileInfo `Sys`, Stat_t must be a pointer.
  • When runtime.GOOS is "windows" Stat_t.Ino will be zero.
  • When fs.FileInfo `Sys` is nil or unknown, some fields not in fs.FileInfo are defaulted: Stat_t.Atim and Stat_t.Ctim are set to `ModTime`, and are set to ModTime and Stat_t.Nlink is set to 1.

type UnimplementedFS

type UnimplementedFS struct{}

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

func (UnimplementedFS) Chmod

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

Chmod implements FS.Chmod

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

Link implements FS.Link

func (UnimplementedFS) Lstat

func (UnimplementedFS) Lstat(path string) (Stat_t, Errno)

Lstat implements FS.Lstat

func (UnimplementedFS) Mkdir

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

Mkdir implements FS.Mkdir

func (UnimplementedFS) OpenFile

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

OpenFile implements FS.OpenFile

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

Readlink implements FS.Readlink

func (UnimplementedFS) Rename

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

Rename implements FS.Rename

func (UnimplementedFS) Rmdir

func (UnimplementedFS) Rmdir(path string) Errno

Rmdir implements FS.Rmdir

func (UnimplementedFS) Stat

func (UnimplementedFS) Stat(path string) (Stat_t, Errno)

Stat implements FS.Stat

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

Symlink implements FS.Symlink

func (UnimplementedFS) Unlink(path string) Errno

Unlink implements FS.Unlink

func (UnimplementedFS) Utimens

func (UnimplementedFS) Utimens(path string, atim, mtim int64) Errno

Utimens implements FS.Utimens

type UnimplementedFile

type UnimplementedFile struct{}

UnimplementedFile is a File that returns ENOSYS for all functions, except where no-op are otherwise documented.

This should be embedded to have forward compatible implementations.

func (UnimplementedFile) Close

func (UnimplementedFile) Close() (errno Errno)

Close implements File.Close

func (UnimplementedFile) Datasync

func (UnimplementedFile) Datasync() Errno

Datasync implements File.Datasync

func (UnimplementedFile) Dev

func (UnimplementedFile) Dev() (uint64, Errno)

Dev implements File.Dev

func (UnimplementedFile) Ino

func (UnimplementedFile) Ino() (Inode, Errno)

Ino implements File.Ino

func (UnimplementedFile) IsAppend

func (UnimplementedFile) IsAppend() bool

IsAppend implements File.IsAppend

func (UnimplementedFile) IsDir

func (UnimplementedFile) IsDir() (bool, Errno)

IsDir implements File.IsDir

func (UnimplementedFile) Pread

func (UnimplementedFile) Pread([]byte, int64) (int, Errno)

Pread implements File.Pread

func (UnimplementedFile) Pwrite

func (UnimplementedFile) Pwrite([]byte, int64) (int, Errno)

Pwrite implements File.Pwrite

func (UnimplementedFile) Read

func (UnimplementedFile) Read([]byte) (int, Errno)

Read implements File.Read

func (UnimplementedFile) Readdir

func (UnimplementedFile) Readdir(int) (dirents []Dirent, errno Errno)

Readdir implements File.Readdir

func (UnimplementedFile) Seek

Seek implements File.Seek

func (UnimplementedFile) SetAppend

func (UnimplementedFile) SetAppend(bool) Errno

SetAppend implements File.SetAppend

func (UnimplementedFile) Stat

func (UnimplementedFile) Stat() (Stat_t, Errno)

Stat implements File.Stat

func (UnimplementedFile) Sync

func (UnimplementedFile) Sync() Errno

Sync implements File.Sync

func (UnimplementedFile) Truncate

func (UnimplementedFile) Truncate(int64) Errno

Truncate implements File.Truncate

func (UnimplementedFile) Utimens

func (UnimplementedFile) Utimens(int64, int64) Errno

Utimens implements File.Utimens

func (UnimplementedFile) Write

func (UnimplementedFile) Write([]byte) (int, Errno)

Write implements File.Write

type Walltime

type Walltime func() (sec int64, nsec int32)

Walltime returns the current unix/epoch time, seconds since midnight UTC 1 January 1970, with a nanosecond fraction.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL