memoryfs

package module
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2022 License: MIT Imports: 10 Imported by: 4

README

memoryfs

An in-memory filesystem implementation of io/fs.FS.

memoryfs implements all of the currently defined io/fs interfaces:

It also allows the creation of files and directories.

Example

package main

import (
    "fmt"
    "io/fs"

    "github.com/aquasecurity/memoryfs"
)

func main() {

    memfs := memoryfs.New()

    if err := memfs.MkdirAll("my/dir", 0o700); err != nil {
        panic(err)
    }

    if err := memfs.WriteFile("my/dir/file.txt", []byte("hello world"), 0o600); err != nil {
        panic(err)
    }

    data, err := fs.ReadFile(memfs, "my/dir/file.txt")
    if err != nil {
        panic(err)
    }

    fmt.Println(string(data))
}

Lazy Loading

If you are mirroring a disk file-system in memory, it can become very inefficient when large files are in use. For this scenario, the WriteLazyFile method is recommended. It allows you to add a file whose content will be provided on-demand by calling the LazyOpener function.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FS

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

FS is an in-memory filesystem

func CloneFS

func CloneFS(base fs.FS) *FS

CloneFS allows you to take on fs.FS and wrap it in an fs that is writable

func New

func New() *FS

New creates a new filesystem

func (*FS) Glob

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

Glob returns the names of all files matching pattern or nil if there is no matching file. The syntax of patterns is the same as in Match. The pattern may describe hierarchical names such as /usr/*/bin/ed (assuming the Separator is '/').

Glob ignores file system errors such as I/O errors reading directories. The only possible returned error is ErrBadPattern, when pattern is malformed.

func (*FS) MkdirAll

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

MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm (before umask) are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil.

func (*FS) Open

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

Open opens the named file for reading.

func (*FS) ReadDir

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

ReadDir reads the named directory and returns a list of directory entries sorted by filename.

func (*FS) ReadFile

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

ReadFile reads the named file and returns its contents. A successful call returns a nil error, not io.EOF. (Because ReadFile reads the whole file, the expected EOF from the final Read is not treated as an error to be reported.)

The caller is permitted to modify the returned byte slice. This method should return a copy of the underlying data.

func (*FS) Remove

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

Remove deletes a file or directory from the filesystem

func (*FS) RemoveAll

func (m *FS) RemoveAll(path string) error

RemoveAll deletes a file or directory and any children if present from the filesystem

func (*FS) Stat

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

Stat returns a FileInfo describing the file.

func (*FS) Sub

func (m *FS) Sub(dir string) (fs.FS, error)

Sub returns an FS corresponding to the subtree rooted at dir.

func (*FS) WriteFile

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

WriteFile writes the specified bytes to the named file. If the file exists, it will be overwritten.

func (*FS) WriteLazyFile

func (m *FS) WriteLazyFile(path string, opener LazyOpener, perm fs.FileMode) error

WriteLazyFile creates (or overwrites) the named file. The contents of the file are not set at this time, but are read on-demand later using the provided LazyOpener.

type LazyOpener

type LazyOpener func() (io.Reader, error)

LazyOpener provides an io.Reader that can be used to access the content of a file, whatever the actual storage medium. If the LazyOpener returns an io.ReadCloser, it will be closed after each read.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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