1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // NewNameSpace returns a NameSpace pre-initialized with an empty
14 // emulated directory mounted on the root mount point "/". This
15 // allows directory traversal routines to work properly even if
16 // a folder is not explicitly mounted at root by the user.
17 func NewNameSpace() NameSpace {
19 ns.Bind("/", &emptyVFS{}, "/", BindReplace)
23 // type emptyVFS emulates a FileSystem consisting of an empty directory
24 type emptyVFS struct{}
26 // Open implements Opener. Since emptyVFS is an empty directory, all
27 // attempts to open a file should returns errors.
28 func (e *emptyVFS) Open(path string) (ReadSeekCloser, error) {
30 return nil, fmt.Errorf("open: / is a directory")
32 return nil, os.ErrNotExist
35 // Stat returns os.FileInfo for an empty directory if the path is
36 // is root "/" or error. os.FileInfo is implemented by emptyVFS
37 func (e *emptyVFS) Stat(path string) (os.FileInfo, error) {
41 return nil, os.ErrNotExist
44 func (e *emptyVFS) Lstat(path string) (os.FileInfo, error) {
48 // ReadDir returns an empty os.FileInfo slice for "/", else error.
49 func (e *emptyVFS) ReadDir(path string) ([]os.FileInfo, error) {
51 return []os.FileInfo{}, nil
53 return nil, os.ErrNotExist
56 func (e *emptyVFS) String() string {
60 func (e *emptyVFS) RootType(path string) RootType {
64 // These functions below implement os.FileInfo for the single
65 // empty emulated directory.
67 func (e *emptyVFS) Name() string {
71 func (e *emptyVFS) Size() int64 {
75 func (e *emptyVFS) Mode() os.FileMode {
76 return os.ModeDir | os.ModePerm
79 func (e *emptyVFS) ModTime() time.Time {
83 func (e *emptyVFS) IsDir() bool {
87 func (e *emptyVFS) Sys() interface{} {