memfs

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2018 License: MIT Imports: 9 Imported by: 11

Documentation

Overview

Package memfs defines an in-memory filesystem

Index

Examples

Constants

View Source
const MinBufferSize = 512

MinBufferSize is the minimal initial allocated buffer size

View Source
const PathSeparator = "/"

PathSeparator used to separate path segments

Variables

View Source
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")
)
View Source
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 NewBuffer

func NewBuffer(buf *[]byte) *Buf

NewBuffer creates a new data volume based on a buffer

func (*Buf) Close

func (v *Buf) Close() error

Close the buffer. Currently no effect.

func (*Buf) Read

func (v *Buf) Read(p []byte) (n int, err error)

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

func (v *Buf) ReadAt(p []byte, off int64) (n int, err error)

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

func (v *Buf) Seek(offset int64, whence int) (int64, error)

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.

func (*Buf) Truncate

func (v *Buf) Truncate(size int64) (err error)

Truncate truncates the Buffer to a given size. It returns an error if the given size is negative. If the Buffer is larger than the specified size, the extra data is lost. If the Buffer is smaller, it is extended and the extended part (hole) reads as zero bytes.

func (*Buf) Write

func (v *Buf) Write(p []byte) (int, error)

Write writes len(p) byte to the Buffer. It returns the number of bytes written and an error if any. Write returns non-nil error when n!=len(p).

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/blang/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 Create

func Create() *MemFS

Create a new MemFS filesystem which entirely resides in memory

func (*MemFS) Lstat

func (fs *MemFS) Lstat(name string) (os.FileInfo, error)

Lstat returns a FileInfo describing the named file. MemFS does not support symbolic links. Alias for fs.Stat(name)

func (*MemFS) Mkdir

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

Mkdir creates a new directory with given permissions

func (*MemFS) OpenFile

func (fs *MemFS) OpenFile(name string, flag int, perm os.FileMode) (vfs.File, error)

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

func (fs *MemFS) PathSeparator() uint8

PathSeparator returns the path separator

func (*MemFS) ReadDir

func (fs *MemFS) ReadDir(path string) ([]os.FileInfo, error)

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

func (*MemFS) Remove

func (fs *MemFS) Remove(name string) error

Remove removes the named file or directory. If there is an error, it will be of type *PathError.

func (*MemFS) Rename

func (fs *MemFS) Rename(oldpath, newpath string) error

Rename renames (moves) a file. Handles to the oldpath persist but might return oldpath if Name() is called.

func (*MemFS) Stat

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

Stat returns the FileInfo structure describing the named file. If there is an error, it will be of type *PathError.

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

func NewMemFile(name string, rwMutex *sync.RWMutex, buf *[]byte) *MemFile

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) Name

func (b MemFile) Name() string

Name of the file

func (*MemFile) Read

func (b *MemFile) Read(p []byte) (n int, err error)

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

func (b *MemFile) ReadAt(p []byte, off int64) (n int, err error)

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

func (b *MemFile) Seek(offset int64, whence int) (n int64, err error)

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.

func (MemFile) Sync

func (b MemFile) Sync() error

Sync has no effect

func (MemFile) Truncate

func (b MemFile) Truncate(size int64) (err error)

Truncate changes the size of the file

func (*MemFile) Write

func (b *MemFile) Write(p []byte) (n int, err error)

Write writes len(p) byte to the Buffer. It returns the number of bytes written and an error if any. Write returns non-nil error when n!=len(p).

Jump to

Keyboard shortcuts

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