You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.0 KiB

  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. package vfs
  5. import (
  6. "fmt"
  7. "os"
  8. "time"
  9. )
  10. // NewNameSpace returns a NameSpace pre-initialized with an empty
  11. // emulated directory mounted on the root mount point "/". This
  12. // allows directory traversal routines to work properly even if
  13. // a folder is not explicitly mounted at root by the user.
  14. func NewNameSpace() NameSpace {
  15. ns := NameSpace{}
  16. ns.Bind("/", &emptyVFS{}, "/", BindReplace)
  17. return ns
  18. }
  19. // type emptyVFS emulates a FileSystem consisting of an empty directory
  20. type emptyVFS struct{}
  21. // Open implements Opener. Since emptyVFS is an empty directory, all
  22. // attempts to open a file should returns errors.
  23. func (e *emptyVFS) Open(path string) (ReadSeekCloser, error) {
  24. if path == "/" {
  25. return nil, fmt.Errorf("open: / is a directory")
  26. }
  27. return nil, os.ErrNotExist
  28. }
  29. // Stat returns os.FileInfo for an empty directory if the path is
  30. // is root "/" or error. os.FileInfo is implemented by emptyVFS
  31. func (e *emptyVFS) Stat(path string) (os.FileInfo, error) {
  32. if path == "/" {
  33. return e, nil
  34. }
  35. return nil, os.ErrNotExist
  36. }
  37. func (e *emptyVFS) Lstat(path string) (os.FileInfo, error) {
  38. return e.Stat(path)
  39. }
  40. // ReadDir returns an empty os.FileInfo slice for "/", else error.
  41. func (e *emptyVFS) ReadDir(path string) ([]os.FileInfo, error) {
  42. if path == "/" {
  43. return []os.FileInfo{}, nil
  44. }
  45. return nil, os.ErrNotExist
  46. }
  47. func (e *emptyVFS) String() string {
  48. return "emptyVFS(/)"
  49. }
  50. // These functions below implement os.FileInfo for the single
  51. // empty emulated directory.
  52. func (e *emptyVFS) Name() string {
  53. return "/"
  54. }
  55. func (e *emptyVFS) Size() int64 {
  56. return 0
  57. }
  58. func (e *emptyVFS) Mode() os.FileMode {
  59. return os.ModeDir | os.ModePerm
  60. }
  61. func (e *emptyVFS) ModTime() time.Time {
  62. return time.Time{}
  63. }
  64. func (e *emptyVFS) IsDir() bool {
  65. return true
  66. }
  67. func (e *emptyVFS) Sys() interface{} {
  68. return nil
  69. }