Documentation ¶
Overview ¶
Package memfs provides an interface for an in-memory filesystem.
Index ¶
- Variables
- func Equal(lhs, rhs FS) bool
- func Fsck(fs FS) bool
- func IsDir(v interface{}) bool
- func MarshalTab(fs FS) ([]byte, error)
- func MarshalUnix(fs FS) ([]byte, error)
- type Directory
- type EncodingState
- type FS
- func (fs FS) Cd(path string) (FS, error)
- func (fs FS) Create(name string) (fs.File, error)
- func (fs FS) MarshalText() ([]byte, error)
- func (fs FS) Mkdir(name string, perm os.FileMode) error
- func (fs FS) MkdirAll(name string, perm os.FileMode) error
- func (fs FS) Open(name string) (fs.File, error)
- func (fs FS) Remove(name string) error
- func (fs FS) RemoveAll(name string) error
- func (fs FS) Stat(name string) (os.FileInfo, error)
- func (fs FS) String() string
- func (fs FS) UnmarshalText(text []byte) (err error)
- func (fs FS) Walk(root string, fn filepath.WalkFunc) (err error)
- type File
- type Order
- type Property
- type TreeBuilder
Constants ¶
This section is empty.
Variables ¶
var ErrTreeBuilder = errors.New("invalid name and/or depth values")
ErrTreeBuilder represents a failure in handling returned values from a TreeBuilder.DecodeLine call.
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 ¶
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 ¶
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 MarshalTab ¶
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 ¶
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.
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 ¶
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 UnmarshalTab ¶
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 ¶
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) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (FS) RemoveAll ¶
RemoveAll removes a file or a directory with all its descendants from the tree rooted at the given path.
func (FS) 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 ¶
UnmarshalText implements encoding.TextUnmarshaler.
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.