-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
47 lines (37 loc) · 1.2 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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)
}