-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
170 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package vfs_test | ||
|
||
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 ExampleDummyFS() { | ||
// 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") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package vfs_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/blang/vfs" | ||
) | ||
|
||
func ExampleOsFS() { | ||
|
||
// 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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,39 @@ | ||
package examples | ||
package vfs_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/blang/vfs" | ||
) | ||
|
||
// Every vfs.Filesystem could be easily wrapped | ||
func ExampleReadOnlyOS() { | ||
func ExampleRoFS() { | ||
// 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", 0777) | ||
err := roFS.Mkdir("/tmp/vfs_example", 0777) | ||
if err != nil { | ||
fatal("Error creating directory: %s\n", err) | ||
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.txt", os.O_RDWR, 0) | ||
f, err := roFS.OpenFile("/tmp/vfs_example/example.txt", os.O_RDWR, 0) | ||
if err != nil { | ||
fatal("Could not create file: %s\n", err) | ||
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 { | ||
fatal("Could not write file on read only filesystem: %s", err) | ||
fmt.Printf("Could not write file on read only filesystem: %s", err) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package vfs_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/blang/vfs" | ||
"github.com/blang/vfs/memfs" | ||
"github.com/blang/vfs/mountfs" | ||
"os" | ||
) | ||
|
||
func Example() { | ||
// 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters