basepath.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package afero
  2. import (
  3. "io/fs"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "strings"
  8. "time"
  9. )
  10. var (
  11. _ Lstater = (*BasePathFs)(nil)
  12. _ fs.ReadDirFile = (*BasePathFile)(nil)
  13. )
  14. // The BasePathFs restricts all operations to a given path within an Fs.
  15. // The given file name to the operations on this Fs will be prepended with
  16. // the base path before calling the base Fs.
  17. // Any file name (after filepath.Clean()) outside this base path will be
  18. // treated as non existing file.
  19. //
  20. // Note that it does not clean the error messages on return, so you may
  21. // reveal the real path on errors.
  22. type BasePathFs struct {
  23. source Fs
  24. path string
  25. }
  26. type BasePathFile struct {
  27. File
  28. path string
  29. }
  30. func (f *BasePathFile) Name() string {
  31. sourcename := f.File.Name()
  32. return strings.TrimPrefix(sourcename, filepath.Clean(f.path))
  33. }
  34. func (f *BasePathFile) ReadDir(n int) ([]fs.DirEntry, error) {
  35. if rdf, ok := f.File.(fs.ReadDirFile); ok {
  36. return rdf.ReadDir(n)
  37. }
  38. return readDirFile{f.File}.ReadDir(n)
  39. }
  40. func NewBasePathFs(source Fs, path string) Fs {
  41. return &BasePathFs{source: source, path: path}
  42. }
  43. // on a file outside the base path it returns the given file name and an error,
  44. // else the given file with the base path prepended
  45. func (b *BasePathFs) RealPath(name string) (path string, err error) {
  46. if err := validateBasePathName(name); err != nil {
  47. return name, err
  48. }
  49. bpath := filepath.Clean(b.path)
  50. path = filepath.Clean(filepath.Join(bpath, name))
  51. if !strings.HasPrefix(path, bpath) {
  52. return name, os.ErrNotExist
  53. }
  54. return path, nil
  55. }
  56. func validateBasePathName(name string) error {
  57. if runtime.GOOS != "windows" {
  58. // Not much to do here;
  59. // the virtual file paths all look absolute on *nix.
  60. return nil
  61. }
  62. // On Windows a common mistake would be to provide an absolute OS path
  63. // We could strip out the base part, but that would not be very portable.
  64. if filepath.IsAbs(name) {
  65. return os.ErrNotExist
  66. }
  67. return nil
  68. }
  69. func (b *BasePathFs) Chtimes(name string, atime, mtime time.Time) (err error) {
  70. if name, err = b.RealPath(name); err != nil {
  71. return &os.PathError{Op: "chtimes", Path: name, Err: err}
  72. }
  73. return b.source.Chtimes(name, atime, mtime)
  74. }
  75. func (b *BasePathFs) Chmod(name string, mode os.FileMode) (err error) {
  76. if name, err = b.RealPath(name); err != nil {
  77. return &os.PathError{Op: "chmod", Path: name, Err: err}
  78. }
  79. return b.source.Chmod(name, mode)
  80. }
  81. func (b *BasePathFs) Chown(name string, uid, gid int) (err error) {
  82. if name, err = b.RealPath(name); err != nil {
  83. return &os.PathError{Op: "chown", Path: name, Err: err}
  84. }
  85. return b.source.Chown(name, uid, gid)
  86. }
  87. func (b *BasePathFs) Name() string {
  88. return "BasePathFs"
  89. }
  90. func (b *BasePathFs) Stat(name string) (fi os.FileInfo, err error) {
  91. if name, err = b.RealPath(name); err != nil {
  92. return nil, &os.PathError{Op: "stat", Path: name, Err: err}
  93. }
  94. return b.source.Stat(name)
  95. }
  96. func (b *BasePathFs) Rename(oldname, newname string) (err error) {
  97. if oldname, err = b.RealPath(oldname); err != nil {
  98. return &os.PathError{Op: "rename", Path: oldname, Err: err}
  99. }
  100. if newname, err = b.RealPath(newname); err != nil {
  101. return &os.PathError{Op: "rename", Path: newname, Err: err}
  102. }
  103. return b.source.Rename(oldname, newname)
  104. }
  105. func (b *BasePathFs) RemoveAll(name string) (err error) {
  106. if name, err = b.RealPath(name); err != nil {
  107. return &os.PathError{Op: "remove_all", Path: name, Err: err}
  108. }
  109. return b.source.RemoveAll(name)
  110. }
  111. func (b *BasePathFs) Remove(name string) (err error) {
  112. if name, err = b.RealPath(name); err != nil {
  113. return &os.PathError{Op: "remove", Path: name, Err: err}
  114. }
  115. return b.source.Remove(name)
  116. }
  117. func (b *BasePathFs) OpenFile(name string, flag int, mode os.FileMode) (f File, err error) {
  118. if name, err = b.RealPath(name); err != nil {
  119. return nil, &os.PathError{Op: "openfile", Path: name, Err: err}
  120. }
  121. sourcef, err := b.source.OpenFile(name, flag, mode)
  122. if err != nil {
  123. return nil, err
  124. }
  125. return &BasePathFile{sourcef, b.path}, nil
  126. }
  127. func (b *BasePathFs) Open(name string) (f File, err error) {
  128. if name, err = b.RealPath(name); err != nil {
  129. return nil, &os.PathError{Op: "open", Path: name, Err: err}
  130. }
  131. sourcef, err := b.source.Open(name)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &BasePathFile{File: sourcef, path: b.path}, nil
  136. }
  137. func (b *BasePathFs) Mkdir(name string, mode os.FileMode) (err error) {
  138. if name, err = b.RealPath(name); err != nil {
  139. return &os.PathError{Op: "mkdir", Path: name, Err: err}
  140. }
  141. return b.source.Mkdir(name, mode)
  142. }
  143. func (b *BasePathFs) MkdirAll(name string, mode os.FileMode) (err error) {
  144. if name, err = b.RealPath(name); err != nil {
  145. return &os.PathError{Op: "mkdir", Path: name, Err: err}
  146. }
  147. return b.source.MkdirAll(name, mode)
  148. }
  149. func (b *BasePathFs) Create(name string) (f File, err error) {
  150. if name, err = b.RealPath(name); err != nil {
  151. return nil, &os.PathError{Op: "create", Path: name, Err: err}
  152. }
  153. sourcef, err := b.source.Create(name)
  154. if err != nil {
  155. return nil, err
  156. }
  157. return &BasePathFile{File: sourcef, path: b.path}, nil
  158. }
  159. func (b *BasePathFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
  160. name, err := b.RealPath(name)
  161. if err != nil {
  162. return nil, false, &os.PathError{Op: "lstat", Path: name, Err: err}
  163. }
  164. if lstater, ok := b.source.(Lstater); ok {
  165. return lstater.LstatIfPossible(name)
  166. }
  167. fi, err := b.source.Stat(name)
  168. return fi, false, err
  169. }
  170. func (b *BasePathFs) SymlinkIfPossible(oldname, newname string) error {
  171. oldname, err := b.RealPath(oldname)
  172. if err != nil {
  173. return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: err}
  174. }
  175. newname, err = b.RealPath(newname)
  176. if err != nil {
  177. return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: err}
  178. }
  179. if linker, ok := b.source.(Linker); ok {
  180. return linker.SymlinkIfPossible(oldname, newname)
  181. }
  182. return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: ErrNoSymlink}
  183. }
  184. func (b *BasePathFs) ReadlinkIfPossible(name string) (string, error) {
  185. name, err := b.RealPath(name)
  186. if err != nil {
  187. return "", &os.PathError{Op: "readlink", Path: name, Err: err}
  188. }
  189. if reader, ok := b.source.(LinkReader); ok {
  190. return reader.ReadlinkIfPossible(name)
  191. }
  192. return "", &os.PathError{Op: "readlink", Path: name, Err: ErrNoReadlink}
  193. }