Categorygithub.com/blang/vfs
modulepackage
1.0.0
Repository: https://github.com/blang/vfs.git
Documentation: pkg.go.dev

# README

vfs for golang Build Status GoDoc Coverage Status Join the chat at https://gitter.im/blang/vfs

vfs is library to support virtual filesystems. It provides basic abstractions of filesystems and implementations, like OS accessing the file system of the underlying OS and memfs a full filesystem in-memory.

Usage

$ go get github.com/blang/vfs

Note: Always vendor your dependencies or fix on a specific version tag.

import github.com/blang/vfs
// Create a vfs accessing the filesystem of the underlying OS
var osfs vfs.Filesystem = vfs.OS()
osfs.Mkdir("/tmp", 0777)

// Make the filesystem read-only:
osfs = vfs.ReadOnly(osfs) // Simply wrap filesystems to change its behaviour

// os.O_CREATE will fail and return vfs.ErrReadOnly
// os.O_RDWR is supported but Write(..) on the file is disabled
f, _ := osfs.OpenFile("/tmp/example.txt", os.O_RDWR, 0)

// Return vfs.ErrReadOnly
_, err := f.Write([]byte("Write on readonly fs?"))
if err != nil {
    fmt.Errorf("Filesystem is read only!\n")
}

// Create a fully writable filesystem in memory
mfs := memfs.Create()
mfs.Mkdir("/root", 0777)

// Create a vfs supporting mounts
// The root fs is accessing the filesystem of the underlying OS
fs := mountfs.Create(osfs)

// Mount a memfs inside /memfs
// /memfs may not exist
fs.Mount(mfs, "/memfs")

// This will create /testdir inside the memfs
fs.Mkdir("/memfs/testdir", 0777)

// This would create /tmp/testdir inside your OS fs
// But the rootfs `osfs` is read-only
fs.Mkdir("/tmp/testdir", 0777)

Check detailed examples below. Also check the GoDocs.

Why should I use this lib?

  • Only Stdlib
  • (Nearly) Fully tested (Coverage >90%)
  • Easy to create your own filesystem
  • Mock a full filesystem for testing (or use included memfs)
  • Compose/Wrap Filesystems ReadOnly(OS()) and write simple Wrappers
  • Many features, see GoDocs and examples below

Features and Examples

Current state: ALPHA

While the functionality is quite stable and heavily tested, interfaces are subject to change.

You need more/less abstraction? Let me know by creating a Issue, thank you.

Motivation

I simply couldn't find any lib supporting this wide range of variation and adaptability.

Contribution

Feel free to make a pull request. For bigger changes create a issue first to discuss about it.

License

See LICENSE file.

# Packages

Package memfs defines an in-memory filesystem.
Package mountfs defines a filesystem supporting the composition of multiple filesystems by mountpoints.

# Functions

Create creates the named file mode 0666 (before umask) on the given Filesystem, truncating it if it already exists.
Dummy creates a new dummy filesystem which returns the given error on every operation.
DummyFile mocks a File returning an error on every operation To create a DummyFS returning a dummyFile instead of an error you can your own DummyFS: type writeDummyFS struct { Filesystem } func (fs writeDummyFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) { return DummyFile(dummyError), nil }.
MkdirAll creates a directory named path on the given Filesystem, along with any necessary parents, and returns nil, or else returns an error.
Open opens the named file on the given Filesystem for reading.
OS returns a filesystem backed by the filesystem of the os.
ReadFile reads the file named by filename and returns the contents.
ReadOnly creates a readonly wrapper around the given filesystem.
ReadOnlyFile wraps the given file and disables Write(..) operation.
RemoveAll removes path and any children it contains.
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.
WriteFile writes data to a file named by filename on the given Filesystem.

# Variables

ErrIsDirectory is returned if a file is a directory.
ErrNotDirectory is returned if a file is not a directory.
ErrorReadOnly is returned on every disabled operation.

# Structs

DumFile represents a dummy File.
DumFileInfo mocks a os.FileInfo returning default values on every operation Struct fields can be set.
DummyFS is dummy filesystem which returns an error on every operation.
OsFS represents a filesystem backed by the filesystem of the underlying OS.
RoFS represents a read-only filesystem and works as a wrapper around existing filesystems.

# Interfaces

File represents a File with common operations.
Filesystem represents an abstract filesystem.