vfs

package module
v0.0.0-...-0b2bc0d Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2019 License: Unlicense Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrIsDirectory is returned if a file is a directory
	ErrIsDirectory = errors.New("Is directory")
	// ErrNotDirectory is returned if a file is not a directory
	ErrNotDirectory = errors.New("Is not a directory")
)
View Source
var ErrReadOnly = errors.New("Filesystem is read-only")

ErrorReadOnly is returned on every disabled operation.

Functions

func MkdirAll

func MkdirAll(fs Filesystem, path string, perm os.FileMode) error

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

func ReadFile

func ReadFile(fs Filesystem, filename string) ([]byte, error)

ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.

This is a port of the stdlib ioutil.ReadFile function.

func RemoveAll

func RemoveAll(fs Filesystem, path string) error

RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil.

func SplitPath

func SplitPath(path string, sep string) []string

SplitPath splits the given path in segments:

"/" 				-> []string{""}
"./file" 			-> []string{".", "file"}
"file" 				-> []string{".", "file"}
"/usr/src/linux/" 	-> []string{"", "usr", "src", "linux"}

The returned slice of path segments consists of one more more segments.

func WriteFile

func WriteFile(fs Filesystem, filename string, data []byte, perm os.FileMode) error

WriteFile writes data to a file named by filename on the given Filesystem. If the file does not exist, WriteFile creates it with permissions perm; otherwise WriteFile truncates it before writing.

This is a port of the stdlib ioutil.WriteFile function.

Types

type File

type File interface {
	Name() string
	Sync() error
	Stat() (os.FileInfo, error)
	// Truncate shrinks or extends the size of the File to the specified size.
	Truncate(int64) error
	io.Reader
	io.ReaderAt
	io.Writer
	io.Seeker
	io.Closer
}

File represents a File with common operations. It differs from os.File so e.g. Stat() needs to be called from the Filesystem instead.

osfile.Stat() -> filesystem.Stat(file.Name())

func Create

func Create(fs Filesystem, name string) (File, error)

Create creates the named file mode 0666 (before umask) on the given Filesystem, truncating it if it already exists. The associated file descriptor has mode os.O_RDWR. If there is an error, it will be of type *os.PathError.

func Open

func Open(fs Filesystem, name string) (File, error)

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 ReadOnlyFile

func ReadOnlyFile(f File) File

ReadOnlyFile wraps the given file and disables Write(..) operation.

type Filesystem

type Filesystem interface {
	PathSeparator() uint8
	OpenFile(name string, flag int, perm os.FileMode) (File, error)
	Remove(name string) error
	// RemoveAll(path string) error
	Rename(oldpath, newpath string) error
	Mkdir(name string, perm os.FileMode) error
	// Symlink(oldname, newname string) error
	// TempDir() string
	// Chmod(name string, mode FileMode) error
	// Chown(name string, uid, gid int) error
	Stat(name string) (os.FileInfo, error)
	Lstat(name string) (os.FileInfo, error)
	ReadDir(path string) ([]os.FileInfo, error)
}

Filesystem represents an abstract filesystem

type OsFS

type OsFS struct{}

OsFS represents a filesystem backed by the filesystem of the underlying OS.

func OS

func OS() *OsFS

OS returns a filesystem backed by the filesystem of the os. It wraps os.* stdlib operations.

func (OsFS) Lstat

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

Lstat wraps os.Lstat

func (OsFS) Mkdir

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

Mkdir wraps os.Mkdir

func (OsFS) OpenFile

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

OpenFile wraps os.OpenFile

func (OsFS) PathSeparator

func (fs OsFS) PathSeparator() uint8

PathSeparator returns the path separator

func (OsFS) ReadDir

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

ReadDir wraps ioutil.ReadDir

func (OsFS) Remove

func (fs OsFS) Remove(name string) error

Remove wraps os.Remove

func (OsFS) Rename

func (fs OsFS) Rename(oldpath, newpath string) error

Rename wraps os.Rename

func (OsFS) Stat

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

Stat wraps os.Stat

type RoFS

type RoFS struct {
	Filesystem
}

RoFS represents a read-only filesystem and works as a wrapper around existing filesystems.

func ReadOnly

func ReadOnly(fs Filesystem) *RoFS

ReadOnly creates a readonly wrapper around the given filesystem. It disables the following operations:

  • Create
  • Remove
  • Rename
  • Mkdir

And disables OpenFile flags: os.O_CREATE, os.O_APPEND, os.O_WRONLY

OpenFile returns a File with disabled Write() method otherwise.

func (RoFS) Mkdir

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

Mkdir is disabled and returns ErrorReadOnly

func (RoFS) OpenFile

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

OpenFile returns ErrorReadOnly if flag contains os.O_CREATE, os.O_APPEND, os.O_WRONLY. Otherwise it returns a read-only File with disabled Write(..) operation.

func (RoFS) Remove

func (fs RoFS) Remove(name string) error

Remove is disabled and returns ErrorReadOnly

func (RoFS) Rename

func (fs RoFS) Rename(oldpath, newpath string) error

Rename is disabled and returns ErrorReadOnly

Directories

Path Synopsis
examles

Jump to

Keyboard shortcuts

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