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 ¶
- Variables
- type FS
- func (m *FS) Glob(pattern string) ([]string, error)
- func (m *FS) MkdirAll(p string, perm fs.FileMode) error
- func (m *FS) Open(name string) (fs.File, error)
- func (m *FS) Put(name string, data []byte) error
- func (m *FS) ReadDir(name string) ([]fs.DirEntry, error)
- func (m *FS) ReadFile(name string) ([]byte, error)
- func (m *FS) Remove(name string) error
- func (m *FS) Rename(oldname, newname string) error
- func (m *FS) Replace(files map[string][]byte) error
- func (m *FS) Stat(name string) (fs.FileInfo, error)
- func (m *FS) WriteFile(name string, data []byte, perm fs.FileMode) error
Constants ¶
This section is empty.
Variables ¶
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 ¶
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) MkdirAll ¶
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) Put ¶
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 ¶
ReadDir implements fs.ReadDirFS, listing files and subdirectories sorted by name.
func (*FS) ReadFile ¶
ReadFile implements fs.ReadFileFS. The returned slice is a copy the caller may modify.
func (*FS) Remove ¶
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 ¶
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 ¶
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.