sys

package
v1.0.0-pre.7 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FdStdin uint32 = iota
	FdStdout
	FdStderr
	// FdRoot is the file descriptor of the root ("/") filesystem.
	//
	// # Why file descriptor 3?
	//
	// While not specified, the most common WASI implementation, wasi-libc, expects
	// POSIX style file descriptor allocation, where the lowest available number is
	// used to open the next file. Since 1 and 2 are taken by stdout and stderr,
	// `root` is assigned 3.
	//   - https://github.com/WebAssembly/WASI/issues/122
	//   - https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_14
	//   - https://github.com/WebAssembly/wasi-libc/blob/wasi-sdk-16/libc-bottom-half/sources/preopens.c#L215
	FdRoot
)

Variables

View Source
var EmptyFS = &emptyFS{}

EmptyFS is exported to special-case an empty file system.

Functions

This section is empty.

Types

type Context

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

Context holds module-scoped system resources currently only supported by built-in host functions.

func DefaultContext

func DefaultContext(fs fs.FS) *Context

DefaultContext returns Context with no values set except a possibly nil fs.FS

func NewContext

func NewContext(
	max uint32,
	args, environ [][]byte,
	stdin io.Reader,
	stdout, stderr io.Writer,
	randSource io.Reader,
	walltime *sys.Walltime,
	walltimeResolution sys.ClockResolution,
	nanotime *sys.Nanotime,
	nanotimeResolution sys.ClockResolution,
	nanosleep *sys.Nanosleep,
	fs fs.FS,
) (sysCtx *Context, err error)

NewContext is a factory function which helps avoid needing to know defaults or exporting all fields. Note: max is exposed for testing. max is only used for env/args validation.

func (*Context) Args

func (c *Context) Args() [][]byte

Args is like os.Args and defaults to nil.

Note: The count will never be more than math.MaxUint32. See wazero.ModuleConfig WithArgs

func (*Context) ArgsSize

func (c *Context) ArgsSize() uint32

ArgsSize is the size to encode Args as Null-terminated strings.

Note: To get the size without null-terminators, subtract the length of Args from this value. See wazero.ModuleConfig WithArgs See https://en.wikipedia.org/wiki/Null-terminated_string

func (*Context) Environ

func (c *Context) Environ() [][]byte

Environ are "key=value" entries like os.Environ and default to nil.

Note: The count will never be more than math.MaxUint32. See wazero.ModuleConfig WithEnv

func (*Context) EnvironSize

func (c *Context) EnvironSize() uint32

EnvironSize is the size to encode Environ as Null-terminated strings.

Note: To get the size without null-terminators, subtract the length of Environ from this value. See wazero.ModuleConfig WithEnv See https://en.wikipedia.org/wiki/Null-terminated_string

func (*Context) FS

func (c *Context) FS() *FSContext

FS returns the possibly empty (EmptyFS) file system context.

func (*Context) Nanosleep

func (c *Context) Nanosleep(ns int64)

Nanosleep implements sys.Nanosleep.

func (*Context) Nanotime

func (c *Context) Nanotime() int64

Nanotime implements sys.Nanotime.

func (*Context) NanotimeResolution

func (c *Context) NanotimeResolution() sys.ClockResolution

NanotimeResolution returns resolution of Nanotime.

func (*Context) RandSource

func (c *Context) RandSource() io.Reader

RandSource is a source of random bytes and defaults to a deterministic source. see wazero.ModuleConfig WithRandSource

func (*Context) Walltime

func (c *Context) Walltime() (sec int64, nsec int32)

Walltime implements sys.Walltime.

func (*Context) WalltimeResolution

func (c *Context) WalltimeResolution() sys.ClockResolution

WalltimeResolution returns resolution of Walltime.

type FSContext

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

func NewFSContext

func NewFSContext(stdin io.Reader, stdout, stderr io.Writer, root fs.FS) (fsc *FSContext, err error)

NewFSContext creates a FSContext, using the `root` parameter for any paths beginning at "/". If the input is EmptyFS, there is no root filesystem. Otherwise, `root` is assigned file descriptor FdRoot and the returned context can open files in that file system. Any error on opening "." is returned.

func (*FSContext) Close

func (c *FSContext) Close(context.Context) (err error)

Close implements api.Closer

func (*FSContext) CloseFile

func (c *FSContext) CloseFile(fd uint32) bool

CloseFile returns true if a file was opened and closed without error, or false if syscall.EBADF.

func (*FSContext) FdReader

func (c *FSContext) FdReader(fd uint32) io.Reader

FdReader returns a valid reader for the given file descriptor or nil if syscall.EBADF.

func (*FSContext) FdWriter

func (c *FSContext) FdWriter(fd uint32) io.Writer

FdWriter returns a valid writer for the given file descriptor or nil if syscall.EBADF.

func (*FSContext) Mkdir

func (c *FSContext) Mkdir(name string, perm fs.FileMode) (newFD uint32, err error)

Mkdir is like syscall.Mkdir and returns the file descriptor of the new directory or an error.

func (*FSContext) OpenFile

func (c *FSContext) OpenFile(name string, flags int, perm fs.FileMode) (newFD uint32, err error)

OpenFile is like syscall.Open and returns the file descriptor of the new file or an error.

func (*FSContext) OpenedFile

func (c *FSContext) OpenedFile(fd uint32) (*FileEntry, bool)

OpenedFile returns a file and true if it was opened or nil and false, if syscall.EBADF.

func (*FSContext) Rename

func (c *FSContext) Rename(from, to string) (err error)

Rename is like syscall.Rename.

func (*FSContext) Rmdir

func (c *FSContext) Rmdir(name string) (err error)

Rmdir is like syscall.Rmdir.

func (*FSContext) StatFile

func (c *FSContext) StatFile(fd uint32) (fs.FileInfo, error)

func (*FSContext) StatPath

func (c *FSContext) StatPath(name string) (fs.FileInfo, error)
func (c *FSContext) Unlink(name string) (err error)

Unlink is like syscall.Unlink.

func (*FSContext) Utimes

func (c *FSContext) Utimes(name string, atimeNsec, mtimeNsec int64) (err error)

Utimes is like syscall.Utimes.

type FileEntry

type FileEntry struct {
	// Name is the basename of the file, at the time it was opened. When the
	// file is root "/" (fd = FdRoot), this is "/".
	//
	// Note: This must match fs.FileInfo.
	Name string

	// File is always non-nil, even when root "/" (fd = FdRoot).
	File fs.File

	// ReadDir is present when this File is a fs.ReadDirFile and `ReadDir`
	// was called.
	ReadDir *ReadDir
}

FileEntry maps a path to an open file in a file system.

type FileTable

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

FileTable is a data structure mapping 32 bit file descriptor to file objects.

The data structure optimizes for memory density and lookup performance, trading off compute at insertion time. This is a useful compromise for the use cases we employ it with: files are usually read or written a lot more often than they are opened, each operation requires a table lookup so we are better off spending extra compute to insert files in the table in order to get cheaper lookups. Memory efficiency is also crucial to support scaling with programs that open thousands of files: having a high or non-linear memory-to-item ratio could otherwise be used as an attack vector by malicous applications attempting to damage performance of the host.

func (*FileTable) Delete

func (t *FileTable) Delete(fd uint32)

Delete deletes the file stored at the given fd from the table.

func (*FileTable) Grow

func (t *FileTable) Grow(n int)

Grow ensures that t has enough room for n files, potentially reallocating the internal buffers if their capacity was too small to hold this many files.

func (*FileTable) Insert

func (t *FileTable) Insert(file *FileEntry) (fd uint32)

Insert inserts the given file to the table, returning the fd that it is mapped to.

The method does not perform deduplication, it is possible for the same file to be inserted multiple times, each insertion will return a different fd.

func (*FileTable) Len

func (t *FileTable) Len() (n int)

Len returns the number of files stored in the table.

func (*FileTable) Lookup

func (t *FileTable) Lookup(fd uint32) (file *FileEntry, found bool)

Lookup returns the file associated with the given fd (may be nil).

func (*FileTable) Range

func (t *FileTable) Range(f func(uint32, *FileEntry) bool)

Range calls f for each file and its associated fd in the table. The function f might return false to interupt the iteration.

func (*FileTable) Reset

func (t *FileTable) Reset()

Reset clears the content of the table.

type ReadDir

type ReadDir struct {
	// CountRead is the total count of files read including Entries.
	CountRead uint64

	// Entries is the contents of the last fs.ReadDirFile call. Notably,
	// directory listing are not rewindable, so we keep entries around in case
	// the caller mis-estimated their buffer and needs a few still cached.
	Entries []fs.DirEntry
}

ReadDir is the status of a prior fs.ReadDirFile call.

Jump to

Keyboard shortcuts

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