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.
$ 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
fs := vfs.OS()
fs.Mkdir("/tmp", 0777)
// Make the filesystem read-only:
fs = vfs.ReadOnly(fs) // 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, _ := fs.OpenFile("/tmp/example.txt", os.O_RDWR, 0)
// Return vfs.ErrReadOnly
_, err := f.Write([]byte("Write on readonly fs?"))
// Create a fully writable filesystem in memory
fs := memfs.Create()
fs.Mkdir("/root")
Check detailed examples below. Also check the GoDocs.
- 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
Have a look at full examples in examples/
- OS Filesystem support
- ReadOnly Wrapper
- DummyFS for quick mocking
- MemFS full in-memory filesystem
- MountFS - support mounts across filesystems
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.
I simply couldn't find any lib supporting this wide range of variation and adaptability.
Feel free to make a pull request. For bigger changes create a issue first to discuss about it.
See LICENSE file.