Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
3JoB committed Mar 18, 2023
1 parent 2c3e227 commit 7f92107
Show file tree
Hide file tree
Showing 34 changed files with 856 additions and 182 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cSpell.words": [
"newname",
"newpath",
"oldname",
"oldpath"
]
}
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
vfs for golang [![Build Status](https://travis-ci.org/blang/vfs.svg?branch=master)](https://travis-ci.org/blang/vfs) [![GoDoc](https://godoc.org/github.com/blang/vfs?status.png)](https://godoc.org/github.com/blang/vfs) [![Coverage Status](https://img.shields.io/coveralls/blang/vfs.svg)](https://coveralls.io/r/blang/vfs?branch=master) [![Join the chat at https://gitter.im/blang/vfs](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/blang/vfs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
vfs for golang [![Build Status](https://travis-ci.org/blang/vfs.svg?branch=master)](https://travis-ci.org/blang/vfs) [![GoDoc](https://godoc.org/github.com/3JoB/vfs?status.png)](https://godoc.org/github.com/3JoB/vfs) [![Coverage Status](https://img.shields.io/coveralls/blang/vfs.svg)](https://coveralls.io/r/blang/vfs?branch=master) [![Join the chat at https://gitter.im/blang/vfs](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/blang/vfs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
======

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
-----
```bash
$ go get github.com/blang/vfs
$ go get github.com/3JoB/vfs
```
Note: Always vendor your dependencies or fix on a specific version tag.

```go
import github.com/blang/vfs
import github.com/3JoB/vfs
```

```go
Expand Down Expand Up @@ -52,7 +52,7 @@ fs.Mkdir("/memfs/testdir", 0777)
fs.Mkdir("/tmp/testdir", 0777)
```

Check detailed examples below. Also check the [GoDocs](http://godoc.org/github.com/blang/vfs).
Check detailed examples below. Also check the [GoDocs](http://godoc.org/github.com/3JoB/vfs).

Why should I use this lib?
-----
Expand All @@ -62,16 +62,16 @@ Why should I use this lib?
- 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](http://godoc.org/github.com/blang/vfs) and examples below
- Many features, see [GoDocs](http://godoc.org/github.com/3JoB/vfs) and examples below

Features and Examples
-----

- [OS Filesystem support](http://godoc.org/github.com/blang/vfs#example-OsFS)
- [ReadOnly Wrapper](http://godoc.org/github.com/blang/vfs#example-RoFS)
- [DummyFS for quick mocking](http://godoc.org/github.com/blang/vfs#example-DummyFS)
- [MemFS - full in-memory filesystem](http://godoc.org/github.com/blang/vfs/memfs#example-MemFS)
- [MountFS - support mounts across filesystems](http://godoc.org/github.com/blang/vfs/mountfs#example-MountFS)
- [OS Filesystem support](http://godoc.org/github.com/3JoB/vfs#example-OsFS)
- [ReadOnly Wrapper](http://godoc.org/github.com/3JoB/vfs#example-RoFS)
- [DummyFS for quick mocking](http://godoc.org/github.com/3JoB/vfs#example-DummyFS)
- [MemFS - full in-memory filesystem](http://godoc.org/github.com/3JoB/vfs/memfs#example-MemFS)
- [MountFS - support mounts across filesystems](http://godoc.org/github.com/3JoB/vfs/mountfs#example-MountFS)

Current state: ALPHA
-----
Expand Down
30 changes: 20 additions & 10 deletions dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// Dummy creates a new dummy filesystem which returns the given error on every operation.
func Dummy(err error) *DummyFS {
return &DummyFS{err}
return &DummyFS{err: err}
}

// DummyFS is dummy filesystem which returns an error on every operation.
Expand All @@ -21,6 +21,11 @@ func (fs DummyFS) PathSeparator() uint8 {
return '/'
}

// Open returns dummy error
func (fs DummyFS) Open(name string) (File, error) {
return fs.OpenFile(name, os.O_RDONLY, 0)
}

// OpenFile returns dummy error
func (fs DummyFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
return nil, fs.err
Expand All @@ -41,6 +46,11 @@ func (fs DummyFS) Mkdir(name string, perm os.FileMode) error {
return fs.err
}

// Symlink returns dummy error
func (fs DummyFS) Symlink(oldname, newname string) error {
return fs.err
}

// Stat returns dummy error
func (fs DummyFS) Stat(name string) (os.FileInfo, error) {
return nil, fs.err
Expand All @@ -60,15 +70,15 @@ func (fs DummyFS) ReadDir(path string) ([]os.FileInfo, error) {
// To create a DummyFS returning a dummyFile instead of an error
// you can your own DummyFS:
//
// type writeDummyFS struct {
// Filesystem
// }
// type writeDummyFS struct {
// Filesystem
// }
//
// func (fs writeDummyFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
// return DummyFile(dummyError), nil
// }
// func (fs writeDummyFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
// return DummyFile(dummyError), nil
// }
func DummyFile(err error) *DumFile {
return &DumFile{err}
return &DumFile{err: err}
}

// DumFile represents a dummy File
Expand Down Expand Up @@ -124,7 +134,7 @@ type DumFileInfo struct {
IMode os.FileMode
IModTime time.Time
IDir bool
ISys interface{}
ISys any
}

// Name returns the field IName
Expand Down Expand Up @@ -153,6 +163,6 @@ func (fi DumFileInfo) IsDir() bool {
}

// Sys returns the field ISys
func (fi DumFileInfo) Sys() interface{} {
func (fi DumFileInfo) Sys() any {
return fi.ISys
}
3 changes: 3 additions & 0 deletions dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func TestInterface(t *testing.T) {

func TestDummyFS(t *testing.T) {
fs := Dummy(errDum)
if _, err := fs.Open("/tmp/test123"); err != errDum {
t.Errorf("Open DummyError expected: %s", err)
}
if _, err := fs.OpenFile("test", 0, 0); err != errDum {
t.Errorf("OpenFile DummyError expected: %s", err)
}
Expand Down
7 changes: 3 additions & 4 deletions example_dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"os"

"github.com/blang/vfs"
"github.com/3JoB/vfs"
)

type myFS struct {
Expand All @@ -14,7 +14,7 @@ type myFS struct {

func MyFS() *myFS {
return &myFS{
vfs.Dummy(errors.New("Not implemented yet!")),
Filesystem: vfs.Dummy(errors.New("Not implemented yet!")),
}
}

Expand All @@ -35,7 +35,6 @@ func ExampleDummyFS() {
// 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")
fmt.Print("Error will be: Not implemented yet!\n")
}

}
3 changes: 1 addition & 2 deletions example_os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package vfs_test
import (
"fmt"

"github.com/blang/vfs"
"github.com/3JoB/vfs"
)

func ExampleOsFS() {

// Create a vfs accessing the filesystem of the underlying OS
osFS := vfs.OS()
err := osFS.Mkdir("/tmp/vfs_example", 0777)
Expand Down
3 changes: 1 addition & 2 deletions example_readonly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/blang/vfs"
"github.com/3JoB/vfs"
)

// Every vfs.Filesystem could be easily wrapped
Expand All @@ -26,7 +26,6 @@ func ExampleRoFS() {
if err != nil {
fmt.Printf("Could not create file: %s\n", err)
return

}
defer f.Close()

Expand Down
11 changes: 6 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package vfs_test

import (
"fmt"
"github.com/blang/vfs"
"github.com/blang/vfs/memfs"
"github.com/blang/vfs/mountfs"
"errors"
"os"

"github.com/3JoB/vfs"
"github.com/3JoB/vfs/memfs"
"github.com/3JoB/vfs/mountfs"
)

func Example() {
Expand All @@ -23,7 +24,7 @@ func Example() {
// Return vfs.ErrReadOnly
_, err := f.Write([]byte("Write on readonly fs?"))
if err != nil {
fmt.Errorf("Filesystem is read only!\n")
panic(errors.New("filesystem is read only!\n"))
}

// Create a fully writable filesystem in memory
Expand Down
7 changes: 3 additions & 4 deletions example_wrapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"fmt"
"os"

"github.com/blang/vfs"
"github.com/3JoB/vfs"
)

type noNewDirs struct {
vfs.Filesystem
}

func NoNewDirs(fs vfs.Filesystem) *noNewDirs {
return &noNewDirs{fs}
return &noNewDirs{Filesystem: fs}
}

// Mkdir is disabled
Expand All @@ -22,12 +22,11 @@ func (fs *noNewDirs) Mkdir(name string, perm os.FileMode) error {
}

func ExampleOsFS_myWrapper() {

// 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")
fmt.Print("Mkdir disabled!\n")
}
}
19 changes: 14 additions & 5 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,45 @@ import (

var (
// ErrIsDirectory is returned if a file is a directory
ErrIsDirectory = errors.New("Is directory")
ErrIsDirectory = errors.New("is directory")

// ErrNotDirectory is returned if a file is not a directory
ErrNotDirectory = errors.New("Is not a directory")
ErrNotDirectory = errors.New("is not a directory")
)

// Filesystem represents an abstract 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

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

// 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())
//
// osfile.Stat() -> filesystem.Stat(file.Name())
type File interface {
Name() string
Sync() error

// Truncate shrinks or extends the size of the File to the specified size.
Truncate(int64) error

io.Reader
io.ReaderAt
io.Writer
Expand Down Expand Up @@ -74,7 +83,7 @@ func MkdirAll(fs Filesystem, path string, perm os.FileMode) error {
if dir.IsDir() {
return nil
}
return &os.PathError{"mkdir", path, ErrNotDirectory}
return &os.PathError{Op: "mkdir", Path: path, Err: ErrNotDirectory}
}

parts := SplitPath(path, string(fs.PathSeparator()))
Expand Down
4 changes: 1 addition & 3 deletions filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ func (fs *rmFS) ReadDir(path string) ([]os.FileInfo, error) {
if fi.IsDir() {
s := make([]os.FileInfo, len(fi.subfiles))
for i, sf := range fi.subfiles {

s[i] = sf
}
for _, sf := range s {
Expand Down Expand Up @@ -219,7 +218,7 @@ func (fs *rmFS) Remove(name string) error {
return nil
}

return &os.PathError{"remove", name, os.ErrNotExist}
return &os.PathError{Op: "remove", Path: name, Err: os.ErrNotExist}
}

func TestRemoveAll(t *testing.T) {
Expand Down Expand Up @@ -277,5 +276,4 @@ func TestRemoveAll(t *testing.T) {
if _, ok := fs.files["/"]; !ok {
t.Errorf("/ was removed")
}

}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/3JoB/vfs

go 1.20
Empty file added go.sum
Empty file.
4 changes: 2 additions & 2 deletions ioutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"
"testing"

"github.com/blang/vfs"
"github.com/blang/vfs/memfs"
"github.com/3JoB/vfs"
"github.com/3JoB/vfs/memfs"
)

var (
Expand Down
Loading

0 comments on commit 7f92107

Please sign in to comment.