github.com/april1989/origin-go-tools@v0.0.32/godoc/vfs/emptyvfs.go (about) 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. 4 5 package vfs 6 7 import ( 8 "fmt" 9 "os" 10 "time" 11 ) 12 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 { 18 ns := NameSpace{} 19 ns.Bind("/", &emptyVFS{}, "/", BindReplace) 20 return ns 21 } 22 23 // type emptyVFS emulates a FileSystem consisting of an empty directory 24 type emptyVFS struct{} 25 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) { 29 if path == "/" { 30 return nil, fmt.Errorf("open: / is a directory") 31 } 32 return nil, os.ErrNotExist 33 } 34 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) { 38 if path == "/" { 39 return e, nil 40 } 41 return nil, os.ErrNotExist 42 } 43 44 func (e *emptyVFS) Lstat(path string) (os.FileInfo, error) { 45 return e.Stat(path) 46 } 47 48 // ReadDir returns an empty os.FileInfo slice for "/", else error. 49 func (e *emptyVFS) ReadDir(path string) ([]os.FileInfo, error) { 50 if path == "/" { 51 return []os.FileInfo{}, nil 52 } 53 return nil, os.ErrNotExist 54 } 55 56 func (e *emptyVFS) String() string { 57 return "emptyVFS(/)" 58 } 59 60 func (e *emptyVFS) RootType(path string) RootType { 61 return "" 62 } 63 64 // These functions below implement os.FileInfo for the single 65 // empty emulated directory. 66 67 func (e *emptyVFS) Name() string { 68 return "/" 69 } 70 71 func (e *emptyVFS) Size() int64 { 72 return 0 73 } 74 75 func (e *emptyVFS) Mode() os.FileMode { 76 return os.ModeDir | os.ModePerm 77 } 78 79 func (e *emptyVFS) ModTime() time.Time { 80 return time.Time{} 81 } 82 83 func (e *emptyVFS) IsDir() bool { 84 return true 85 } 86 87 func (e *emptyVFS) Sys() interface{} { 88 return nil 89 }