memfs

package
v0.0.0-...-0f47780 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2014 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package memfs provides an interface for an in-memory filesystem.

Index

Constants

This section is empty.

Variables

View Source
var ErrTreeBuilder = errors.New("invalid name and/or depth values")

ErrTreeBuilder represents a failure in handling returned values from a TreeBuilder.DecodeLine call.

View Source
var Umask os.FileMode = 0002

Umask defines a default file mode creation mask. When not specified explicitely, new files has default permission bits equal to 0666 & ~Umask, and 0777 & ~Umask for directories.

Functions

func Equal

func Equal(lhs, rhs FS) bool

Equal returns true when the structure of the lhs and rhs is the same. It does not compare the value of the Files between the trees. If both trees are empty it returns true.

func Fsck

func Fsck(fs FS) bool

Fsck checks the fs Tree whether each node has proper type: either a File or a Directory. Moreover it fails if an empty key of a directory is defined and is not of Property type. Fsking empty tree gives true.

func IsDir

func IsDir(v interface{}) bool

IsDir reports whether v is of Directory type.

func MarshalTab

func MarshalTab(fs FS) ([]byte, error)

MarshalTab serializes FS.Tree to a text representation, which is a \t-separated file tree.

Example

var fs = memfs.FS{
           Tree: memfs.Directory{
             "dir": memfs.Directory{
               "file.txt": memfs.File{},
             },
           },
         }

var tree, _ = memfs.MarshalTab(fs)

The tree slice is equal to:

var tree = []byte(`.\ndir\n\tfile1.txt\n\tfile2.txt`)

MarshalTab is a conveniance function which wraps Tab.Encode.

func MarshalUnix

func MarshalUnix(fs FS) ([]byte, error)

MarshalUnix serializes FS.Tree to a text representation, which is the same as the Unix tree command's output.

Example

var fs = memfs.FS{
           Tree: memfs.Directory{
             "dir": memfs.Directory{
               "file.txt": memfs.File{},
             },
           },
         }

var tree, _ = memfs.MarshalUnix(fs)

The tree slice is equal to:

var tree = []byte(`.
└── dir
    └── file.txt`)

MarshalUnix is a conveniance function which wraps Unix.Decode.

Types

type Directory

type Directory map[string]interface{}

Directory represents an in-memory directory. Valid directory has each value of a File or Directory type, where a key of such value must be non-empty and contain no backward nor forward slashes. An empty key has a special treatment - when defined, its value must be of a Property type.

func (Directory) Lsnames

func (d Directory) Lsnames(order Order) []string

Lsnames lists all the files or directories names of the directory in the given order. It returns nil if the directory is empty.

type EncodingState

type EncodingState uint8

EncodingState denotes a part of a tree, which is currently being printed by the TreeBuilder.Encode method.

const (
	EncodingLevel     EncodingState = iota // returns []byte("│   ")
	EncodingLevelLast                      // returns []byte("    ")
	EncodingItem                           // returns []byte("├── ")
	EncodingItemLast                       // returns []byte("└── ")
)

Example: Each Endcoding* variable makes the Unix.EncodeState function member return the corresponding bytes.

type FS

type FS struct {
	Tree Directory
}

FS provides an implementation for Filesystem interface, operating on an in-memory file tree. TODO(rjeczalik): sync.RWMutex

func Must

func Must(fs FS, err error) FS

Must is a helper that wraps a call to a function returning (FS, error) and panics if the error is non-nil. It is intended for use in variable initializations such as:

var fs = memfs.Must(memfs.TabTree([]byte(".\ndir\n\tfile.txt")))

func New

func New() FS

New returns an empty filesystem.

func UnmarshalTab

func UnmarshalTab(p []byte) (FS, error)

UnmarshalTab builds FS.Tree from a buffer that contains \t-separated file tree.

Example

var tree = []byte(`.\ndir\n\tfile1.txt\n\tfile2.txt`)
var fs = memfs.Must(memfs.UnmarshalTab(tree))

The above is an equivalent to:

var fs = memfs.FS{
           Tree: memfs.Directory{
             "dir": memfs.Directory{
               "file1.txt": memfs.File{},
               "file2.txt": memfs.File{},
             },
           },
         }

UnmarshalTab is a conveniance function which wraps Tab.Decode.

func UnmarshalUnix

func UnmarshalUnix(p []byte) (FS, error)

UnmarshalUnix builds FS.Tree from a buffer that contains tree-like (Unix command) output.

Example

var tree = []byte(`.
└── dir
    └── file.txt`)

var fs = memfs.Must(memfs.UnmarshalUnix(tree))

The above is an equivalent to:

var fs = memfs.FS{
           Tree: memfs.Directory{
             "dir": memfs.Directory{
               "file.txt": memfs.File{},
             },
           },
         }

UnmarshalUnix(p) is a conveniance function which wraps Unix.Decode.

func (FS) Cd

func (fs FS) Cd(path string) (FS, error)

Cd gives new filesystem with a root starting at the path of the old filesystem.

func (FS) Create

func (fs FS) Create(name string) (fs.File, error)

Create creates an in-memory file under the given path.

func (FS) MarshalText

func (fs FS) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (FS) Mkdir

func (fs FS) Mkdir(name string, perm os.FileMode) error

Mkdir creates an in-memory directory under the given path.

func (FS) MkdirAll

func (fs FS) MkdirAll(name string, perm os.FileMode) error

MkdirAll creates new in-memory directory and all its parents, if needed.

func (FS) Open

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

Open opens a file or directory given by the path.

func (FS) Remove

func (fs FS) Remove(name string) error

Remove removes a file from the tree given by the path.

func (FS) RemoveAll

func (fs FS) RemoveAll(name string) error

RemoveAll removes a file or a directory with all its descendants from the tree rooted at the given path.

func (FS) Stat

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

Stat gives the details of a file or a directory given by the path.

func (FS) String

func (fs FS) String() string

Encode produces Unix-tree-like filesystem representation as a string.

Example

String can be use to convert between Tab and Unix tree representations, like in the following example:

var fs = memfs.Must(memfs.UnmarshalTab([]byte(".\ndir\n\tfile1.txt\n\tfile2.txt")))
fmt.Println(fs)

Which prints:

.
└── dir
    ├── file1.txt
    └── file2.txt

func (FS) UnmarshalText

func (fs FS) UnmarshalText(text []byte) (err error)

UnmarshalText implements encoding.TextUnmarshaler.

func (FS) Walk

func (fs FS) Walk(root string, fn filepath.WalkFunc) (err error)

Walk walks the file tree in a depth-first lexical order, calling fn for each file or directory.

type File

type File struct {
	Property        // file properties
	Content  []byte // file content
}

File represents an in-memory file.

type Order

type Order uint8

Order denotes the access order pattern used by an external operation.

const (
	OrderLexicalAsc  Order = iota // access items in ascending order
	OrderLexicalDesc              // access items in descending order
)

type Property

type Property struct {
	Mode    os.FileMode // permission bits
	ModTime time.Time   // last modification time
}

Property defines file mode and modification time values for a File and a Directory.

Example

To change propety of a File, change its embedded Property value:

var authorized = File{Property{0600, time.Now()}}

To change property of a Directory, define it for a special empty key:

var dotssh = Directory{"": Property{0700}, "authorized_keys": authorized}

type TreeBuilder

type TreeBuilder struct {
	DecodeLine  func([]byte) (int, []byte, error)
	EncodeState func(EncodingState) []byte
}

TreeBuilder provides an implementation of encoding.TextMarshaler and encoding.TextUnmarshaler for the memfs.FS struct. It may be configured to support custom formats by providing DecodeLine member function.

Decoding

DecodeLine instructs the TreeBuilder.Decode how to parse single line of given buffer, where 'name' is the name of a tree node, 'depth' is its depth in the tree and 'err' eventual parsing failure. The 'line' is guaranteed to be non-nil and non-empty.

The function is expected to return non-nil and non-empty name and non-negative depth when err is nil. If the err is io.EOF, it will be translated to ErrTreeBuilder, because it will.

If DecodeLine is nil, Unix.DecodeLine is used.

Encoding

EncodeState instructs the TreeBuilder.Encode how to print each part of the tree, denoted by a EncodingState argument passed to it. The output is not sanitized, which means e.g. newlines or other characters may break the tree layout.

If EncodeState is nil, Unix.EncodeState is used.

var Tab TreeBuilder

Tab is a tree builder for simplified tree representation, where each level is idented with one tabulation character (\t) only. It's guaranteed calls to Tab.Decode do not return ErrTreeBuilder.

var Unix TreeBuilder

Unix is a tree builder for the 'tree' Unix command. It's guaranteed calls to Unix.Decode do not return ErrTreeBuilder.

func (TreeBuilder) Decode

func (tb TreeBuilder) Decode(r io.Reader) (fs FS, err error)

Decode builds fs.Tree from given reader using bt.DecodeLine callback for parsing node's name and its depth in the tree. Tree returns ErrTreeBuilder error when a call to ct gives invalid values.

If tb.DecodeLine is nil, Unix.DecodeLine is used.

func (TreeBuilder) Encode

func (tb TreeBuilder) Encode(fs FS, w io.Writer) (err error)

Encode serializes the fs.Tree by writing its text representation to the w writer. The text is formatted according to the bt.EncodeState function.

If tb.EncodeState is nil, Unix.EncodeState is used.

Jump to

Keyboard shortcuts

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