memfs

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

Package memfs is an in-memory fs.FS that can be written to while it is being read.

It exists for hosts with no filesystem, such as a WebAssembly build in a browser, where bytes are pushed in (a dropped folder, a fetched bundle) before the code that reads them through fs.FS runs. Every method is safe for concurrent use, so a host can add files between requests while handler goroutines are still reading.

Directories are implied by file paths ("a/b.txt" makes "a" a directory) and can also be created empty with MkdirAll. A path is either a file or a directory, never both.

The write methods (WriteFile, MkdirAll, Remove, Rename) follow the os package's shapes, so an FS satisfies read-write interfaces such as templar's WritableFS.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotEmpty = errors.New("directory not empty")

ErrNotEmpty is the error Remove returns, wrapped in an *fs.PathError, for a directory that still has entries.

Functions

This section is empty.

Types

type FS

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

FS is an in-memory file tree. The zero value is an empty FS ready to use.

A file opened from an FS keeps the contents it had when it was opened. A later Put, WriteFile or Remove changes what the next Open sees, never a file already open.

func New

func New(files map[string][]byte) (*FS, error)

New returns an FS holding files, keyed by slash-separated path. It takes ownership of each byte slice, so the caller must not modify one afterwards. It returns an error for an invalid path or for a path that is also another path's parent directory.

func (*FS) Glob

func (m *FS) Glob(pattern string) ([]string, error)

Glob implements fs.GlobFS.

func (*FS) MkdirAll

func (m *FS) MkdirAll(p string, perm fs.FileMode) error

MkdirAll creates the directory p, which then exists even with nothing in it. Like os.MkdirAll it succeeds if p is already a directory, and fails if p or one of its parents is a file.

func (*FS) Open

func (m *FS) Open(name string) (fs.File, error)

Open implements fs.FS.

func (*FS) Put

func (m *FS) Put(name string, data []byte) error

Put adds or replaces the file name. Like New it takes ownership of data instead of copying it, which matters when a host pushes in tens of megabytes it has no other use for. Parent directories are implied. It fails if name is invalid, is a directory, or has a file as one of its parents.

func (*FS) ReadDir

func (m *FS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir implements fs.ReadDirFS, listing files and subdirectories sorted by name.

func (*FS) ReadFile

func (m *FS) ReadFile(name string) ([]byte, error)

ReadFile implements fs.ReadFileFS. The returned slice is a copy the caller may modify.

func (*FS) Remove

func (m *FS) Remove(name string) error

Remove deletes the file or empty directory name, as os.Remove does. A directory with entries is an *fs.PathError wrapping ErrNotEmpty, and a missing name one wrapping fs.ErrNotExist.

func (*FS) Rename

func (m *FS) Rename(oldname, newname string) error

Rename moves the file or directory oldname to newname, taking a directory's whole subtree with it. As with os.Rename, a file replaces an existing file at newname. Anything else already at newname, or a file among newname's parents, is an error wrapping fs.ErrExist.

func (*FS) Replace

func (m *FS) Replace(files map[string][]byte) error

Replace swaps the whole tree for files in one step, so a concurrent reader sees either the old tree or the new one and never a mix. It takes ownership of the byte slices as New does. On error the FS is unchanged.

func (*FS) Stat

func (m *FS) Stat(name string) (fs.FileInfo, error)

Stat implements fs.StatFS.

func (*FS) WriteFile

func (m *FS) WriteFile(name string, data []byte, perm fs.FileMode) error

WriteFile writes the file name with a copy of data, as os.WriteFile does, so the caller may reuse data. perm is recorded as the file's mode. Unlike os.WriteFile, missing parent directories are implied rather than an error.

Jump to

Keyboard shortcuts

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