/
emptyvfs.go
91 lines (75 loc) · 2.12 KB
/
emptyvfs.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package emptyvfs
import (
"fmt"
"golang.org/x/tools/godoc/vfs"
"os"
"time"
)
// NewNameSpace returns a vfs.NameSpace initialized with an empty
// emulated directory mounted on the root mount point "/" so that
// directory traversal routines don't break if the user doesn't
// explicitly mount a FileSystem at "/". See the following issue:
// https://github.com/golang/go/issues/14190
func NewNameSpace() vfs.NameSpace {
ns := vfs.NameSpace{}
ns.Bind("/", newemptyVFS(), "/", vfs.BindReplace)
return ns
}
// type emptyVFS emulates a vfs.FileSystem consisting of an empty directory
type emptyVFS struct {
modtime time.Time
}
func newemptyVFS() *emptyVFS {
return &emptyVFS{modtime: time.Now()}
}
// Open implements Opener. Since emptyVFS is an empty directory, all
// attempt to open a file returns errors.
func (e *emptyVFS) Open(path string) (vfs.ReadSeekCloser, error) {
if path == "/" {
return nil, fmt.Errorf("open: / is a directory")
}
return nil, os.ErrNotExist
}
// Stat returns FileInfo (implemented by emptyVFS itself) if the path
// is root "/" or error for any other path.
func (e *emptyVFS) Stat(path string) (os.FileInfo, error) {
if path == "/" {
return e, nil
}
return nil, os.ErrNotExist
}
// Lstat simply calls Stat
func (e *emptyVFS) Lstat(path string) (os.FileInfo, error) {
return e.Stat(path)
}
// ReadDir returns an empty FileInfo slice if the path is root "/"
// else error for any other path.
func (e *emptyVFS) ReadDir(path string) ([]os.FileInfo, error) {
if path == "/" {
return []os.FileInfo{}, nil
}
return nil, os.ErrNotExist
}
func (e *emptyVFS) String() string {
return "emptyVFS"
}
// These functions below implement os.FileInfo for the single
// empty emulated directory.
func (e *emptyVFS) Name() string {
return "root"
}
func (e *emptyVFS) Size() int64 {
return 0
}
func (e *emptyVFS) Mode() os.FileMode {
return os.ModeDir | os.ModePerm
}
func (e *emptyVFS) ModTime() time.Time {
return e.modtime
}
func (e *emptyVFS) IsDir() bool {
return true
}
func (e *emptyVFS) Sys() interface{} {
return nil
}