Documentation
¶
Overview ¶
Package vfs defines an abstract file system and various implementations.
Example ¶
package main import ( "fmt" "github.com/blang/vfs" "github.com/blang/vfs/memfs" "github.com/blang/vfs/mountfs" "os" ) func main() { // 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) }
Output:
Index ¶
- Variables
- func MkdirAll(fs Filesystem, path string, perm os.FileMode) error
- func RemoveAll(fs Filesystem, path string) error
- func SplitPath(path string, sep string) []string
- type DumFile
- type DumFileInfo
- type DummyFS
- func (fs DummyFS) Lstat(name string) (os.FileInfo, error)
- func (fs DummyFS) Mkdir(name string, perm os.FileMode) error
- func (fs DummyFS) OpenFile(name string, flag int, perm os.FileMode) (File, error)
- func (fs DummyFS) PathSeparator() uint8
- func (fs DummyFS) ReadDir(path string) ([]os.FileInfo, error)
- func (fs DummyFS) Remove(name string) error
- func (fs DummyFS) Rename(oldpath, newpath string) error
- func (fs DummyFS) Stat(name string) (os.FileInfo, error)
- type File
- type Filesystem
- type OsFS
- func (fs OsFS) Lstat(name string) (os.FileInfo, error)
- func (fs OsFS) Mkdir(name string, perm os.FileMode) error
- func (fs OsFS) OpenFile(name string, flag int, perm os.FileMode) (File, error)
- func (fs OsFS) PathSeparator() uint8
- func (fs OsFS) ReadDir(path string) ([]os.FileInfo, error)
- func (fs OsFS) Remove(name string) error
- func (fs OsFS) Rename(oldpath, newpath string) error
- func (fs OsFS) Stat(name string) (os.FileInfo, error)
- type RoFS
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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") )
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 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.
Types ¶
type DumFile ¶
type DumFile struct {
// contains filtered or unexported fields
}
DumFile represents a dummy File
func DummyFile ¶
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 }
type DumFileInfo ¶
type DumFileInfo struct { IName string ISize int64 IMode os.FileMode IModTime time.Time IDir bool ISys interface{} }
DumFileInfo mocks a os.FileInfo returning default values on every operation Struct fields can be set.
func (DumFileInfo) ModTime ¶
func (fi DumFileInfo) ModTime() time.Time
ModTime returns the field IModTime
type DummyFS ¶
type DummyFS struct {
// contains filtered or unexported fields
}
DummyFS is dummy filesystem which returns an error on every operation. It can be used to mock a full filesystem for testing or fs creation.
Example ¶
package main import ( "errors" "fmt" "os" "github.com/blang/vfs" ) type myFS struct { vfs.Filesystem // Embed the Filesystem interface and fill it with vfs.Dummy on creation } func MyFS() *myFS { return &myFS{ vfs.Dummy(errors.New("Not implemented yet!")), } } func (fs myFS) Mkdir(name string, perm os.FileMode) error { // Create a directory // ... return nil } func main() { // Simply bootstrap your filesystem var fs vfs.Filesystem = MyFS() // Your mkdir implementation fs.Mkdir("/tmp", 0777) // All necessary methods like OpenFile (therefor Create) are stubbed // and return the dummys error _, err := vfs.Create(fs, "/tmp/vfs/example.txt") if err != nil { fmt.Printf("Error will be: Not implemented yet!\n") } }
Output:
func (DummyFS) PathSeparator ¶
PathSeparator returns the path separator
type File ¶
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 ¶
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.
Example ¶
package main import ( "fmt" "github.com/blang/vfs" ) func main() { // Create a vfs accessing the filesystem of the underlying OS osFS := vfs.OS() err := osFS.Mkdir("/tmp/vfs_example", 0777) if err != nil { fmt.Printf("Error creating directory: %s\n", err) } // Convenience method f, err := vfs.Create(osFS, "/tmp/vfs_example/example.txt") // f, err := osFS.OpenFile("/tmp/vfs/example.txt", os.O_CREATE|os.O_RDWR, 0666) if err != nil { fmt.Printf("Could not create file: %s\n", err) } defer f.Close() if _, err := f.Write([]byte("VFS working on your filesystem")); err != nil { fmt.Printf("Error writing to file: %s\n", err) } }
Output:
Example (MyWrapper) ¶
package main import ( "errors" "fmt" "os" "github.com/blang/vfs" ) type noNewDirs struct { vfs.Filesystem } func NoNewDirs(fs vfs.Filesystem) *noNewDirs { return &noNewDirs{fs} } // Mkdir is disabled func (fs *noNewDirs) Mkdir(name string, perm os.FileMode) error { return errors.New("Mkdir disabled!") } func main() { // Disable Mkdirs on the OS Filesystem var fs vfs.Filesystem = NoNewDirs(vfs.OS()) err := fs.Mkdir("/tmp", 0777) if err != nil { fmt.Printf("Mkdir disabled!\n") } }
Output:
func OS ¶
func OS() *OsFS
OS returns a filesystem backed by the filesystem of the os. It wraps os.* stdlib operations.
func (OsFS) PathSeparator ¶
PathSeparator returns the path separator
type RoFS ¶
type RoFS struct {
Filesystem
}
RoFS represents a read-only filesystem and works as a wrapper around existing filesystems.
Example ¶
Every vfs.Filesystem could be easily wrapped
package main import ( "fmt" "os" "github.com/blang/vfs" ) func main() { // Create a readonly vfs accessing the filesystem of the underlying OS roFS := vfs.ReadOnly(vfs.OS()) // Mkdir is disabled on ReadOnly vfs, will return vfs.ErrReadOnly // See vfs.ReadOnly for all disabled operations err := roFS.Mkdir("/tmp/vfs_example", 0777) if err != nil { fmt.Printf("Error creating directory: %s\n", err) return } // OpenFile is controlled to support read-only functionality. os.O_CREATE or os.O_APPEND will fail. // Flags like os.O_RDWR are supported but the returned file is protected e.g. from Write(..). f, err := roFS.OpenFile("/tmp/vfs_example/example.txt", os.O_RDWR, 0) if err != nil { fmt.Printf("Could not create file: %s\n", err) return } defer f.Close() // Will fail and return vfs.ErrReadOnly _, err = f.Write([]byte("VFS working on your filesystem")) if err != nil { fmt.Printf("Could not write file on read only filesystem: %s", err) return } }
Output:
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) OpenFile ¶
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.
Directories
¶
Path | Synopsis |
---|---|
Package memfs defines an in-memory filesystem
|
Package memfs defines an in-memory filesystem |
Package mountfs defines a filesystem supporting the composition of multiple filesystems by mountpoints.
|
Package mountfs defines a filesystem supporting the composition of multiple filesystems by mountpoints. |