Documentation
¶
Overview ¶
Package memfs defines an in-memory filesystem
Index ¶
- Constants
- Variables
- type Buf
- type Buffer
- type MemFS
- func (fs *MemFS) Lstat(name string) (os.FileInfo, error)
- func (fs *MemFS) Mkdir(name string, perm os.FileMode) error
- func (fs *MemFS) Open(name string) (vfs.File, error)
- func (fs *MemFS) OpenFile(name string, flag int, perm os.FileMode) (vfs.File, error)
- func (fs *MemFS) PathSeparator() uint8
- func (fs *MemFS) ReadDir(path string) ([]os.FileInfo, error)
- func (fs *MemFS) Remove(name string) error
- func (fs *MemFS) Rename(oldpath, newpath string) error
- func (fs *MemFS) Stat(name string) (os.FileInfo, error)
- func (fs *MemFS) Symlink(oldname, newname string) error
- type MemFile
- func (b MemFile) Name() string
- func (b *MemFile) Read(p []byte) (n int, err error)
- func (b *MemFile) ReadAt(p []byte, off int64) (n int, err error)
- func (b *MemFile) Seek(offset int64, whence int) (n int64, err error)
- func (b MemFile) Sync() error
- func (b MemFile) Truncate(size int64) (err error)
- func (b *MemFile) Write(p []byte) (n int, err error)
Examples ¶
Constants ¶
const MinBufferSize = 512
MinBufferSize is the minimal initial allocated buffer size
const PathSeparator = "/"
PathSeparator used to separate path segments
Variables ¶
var ( // ErrReadOnly is returned if the file is read-only and write operations are disabled. ErrReadOnly = errors.New("File is read-only") // ErrWriteOnly is returned if the file is write-only and read operations are disabled. ErrWriteOnly = errors.New("File is write-only") // ErrIsDirectory is returned if the file under operation is not a regular file but a directory. ErrIsDirectory = errors.New("Is directory") )
var ErrTooLarge = errors.New("volume too large")
ErrTooLarge is thrown if it was not possible to enough memory
Functions ¶
This section is empty.
Types ¶
type Buf ¶
type Buf struct {
// contains filtered or unexported fields
}
Buf is a Buffer working on a slice of bytes.
func (*Buf) Read ¶
Read reads len(p) byte from the Buffer starting at the current offset. It returns the number of bytes read and an error if any. Returns io.EOF error if pointer is at the end of the Buffer.
func (*Buf) ReadAt ¶
ReadAt reads len(b) bytes from the Buffer starting at byte offset off. It returns the number of bytes read and the error, if any. ReadAt always returns a non-nil error when n < len(b). At end of file, that error is io.EOF.
func (*Buf) Seek ¶
Seek sets the offset for the next Read or Write on the buffer to offset, interpreted according to whence:
0 (os.SEEK_SET) means relative to the origin of the file 1 (os.SEEK_CUR) means relative to the current offset 2 (os.SEEK_END) means relative to the end of the file
It returns the new offset and an error, if any.
type Buffer ¶
type Buffer interface { io.Reader io.ReaderAt io.Writer io.Seeker io.Closer // Truncate shrinks or extends the size of the Buffer to the specified size. Truncate(int64) error }
Buffer is a usable block of data similar to a file
type MemFS ¶
type MemFS struct {
// contains filtered or unexported fields
}
MemFS is a in-memory filesystem
Example ¶
package main import ( "github.com/3JoB/vfs/memfs" ) func main() { // Create a fully writable filesystem in memory fs := memfs.Create() // Like every other vfs.Filesytem, it could be wrapped, e.g. read-only: // fs = vfs.ReadOnly(fs) // The memory fs is completely empty, permissions are supported (e.g. Stat()) but have no effect. fs.Mkdir("/tmp", 0777) }
Output:
func (*MemFS) Lstat ¶
Lstat returns a FileInfo describing the named file. MemFS does not support symbolic links. Alias for fs.Stat(name)
func (*MemFS) Open ¶
Open opens the named file on the given Filesystem for reading. If successful, methods on the returned file can be used for reading. The associated file descriptor has mode os.O_RDONLY. If there is an error, it will be of type *PathError.
func (*MemFS) OpenFile ¶
OpenFile opens a file handle with a specified flag (os.O_RDONLY etc.) and perm (e.g. 0666). If success the returned File can be used for I/O. Otherwise an error is returned, which is a *os.PathError and can be extracted for further information.
func (*MemFS) PathSeparator ¶
PathSeparator returns the path separator
func (*MemFS) ReadDir ¶
ReadDir reads the directory named by path and returns a list of sorted directory entries.
func (*MemFS) Remove ¶
Remove removes the named file or directory. If there is an error, it will be of type *PathError.
func (*MemFS) Rename ¶
Rename renames (moves) a file. Handles to the oldpath persist but might return oldpath if Name() is called.
type MemFile ¶
type MemFile struct { Buffer // contains filtered or unexported fields }
MemFile represents a file backed by a Buffer which is secured from concurrent access.
func NewMemFile ¶
NewMemFile creates a Buffer which byte slice is safe from concurrent access, the file itself is not thread-safe.
This means multiple files can work safely on the same byte slice, but multiple go routines working on the same file may corrupt the internal pointer structure.
func (*MemFile) Read ¶
Read reads len(p) byte from the underlying buffer starting at the current offset. It returns the number of bytes read and an error if any. Returns io.EOF error if pointer is at the end of the Buffer. See Buf.Read()
func (*MemFile) ReadAt ¶
ReadAt reads len(b) bytes from the Buffer starting at byte offset off. It returns the number of bytes read and the error, if any. ReadAt always returns a non-nil error when n < len(b). At end of file, that error is io.EOF. See Buf.ReadAt()
func (*MemFile) Seek ¶
Seek sets the offset for the next Read or Write on the buffer to offset, interpreted according to whence:
0 (os.SEEK_SET) means relative to the origin of the file 1 (os.SEEK_CUR) means relative to the current offset 2 (os.SEEK_END) means relative to the end of the file
It returns the new offset and an error, if any.