walkfs.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2009 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. // This part copied directly from golang.org/src/path/filepath/path.go (Go
  5. // 1.6) and lightly modified to be methods on BasicFilesystem.
  6. // In our Walk() all paths given to a WalkFunc() are relative to the
  7. // filesystem root.
  8. package fs
  9. import "path/filepath"
  10. // WalkFunc is the type of the function called for each file or directory
  11. // visited by Walk. The path argument contains the argument to Walk as a
  12. // prefix; that is, if Walk is called with "dir", which is a directory
  13. // containing the file "a", the walk function will be called with argument
  14. // "dir/a". The info argument is the FileInfo for the named path.
  15. //
  16. // If there was a problem walking to the file or directory named by path, the
  17. // incoming error will describe the problem and the function can decide how
  18. // to handle that error (and Walk will not descend into that directory). If
  19. // an error is returned, processing stops. The sole exception is when the function
  20. // returns the special value SkipDir. If the function returns SkipDir when invoked
  21. // on a directory, Walk skips the directory's contents entirely.
  22. // If the function returns SkipDir when invoked on a non-directory file,
  23. // Walk skips the remaining files in the containing directory.
  24. type WalkFunc func(path string, info FileInfo, err error) error
  25. type walkFilesystem struct {
  26. Filesystem
  27. }
  28. func NewWalkFilesystem(next Filesystem) Filesystem {
  29. return &walkFilesystem{next}
  30. }
  31. // walk recursively descends path, calling walkFn.
  32. func (f *walkFilesystem) walk(path string, info FileInfo, walkFn WalkFunc) error {
  33. err := walkFn(path, info, nil)
  34. if err != nil {
  35. if info.IsDir() && err == SkipDir {
  36. return nil
  37. }
  38. return err
  39. }
  40. if !info.IsDir() {
  41. return nil
  42. }
  43. names, err := f.DirNames(path)
  44. if err != nil {
  45. return walkFn(path, info, err)
  46. }
  47. for _, name := range names {
  48. filename := filepath.Join(path, name)
  49. fileInfo, err := f.Lstat(filename)
  50. if err != nil {
  51. if err := walkFn(filename, fileInfo, err); err != nil && err != SkipDir {
  52. return err
  53. }
  54. } else {
  55. err = f.walk(filename, fileInfo, walkFn)
  56. if err != nil {
  57. if !fileInfo.IsDir() || err != SkipDir {
  58. return err
  59. }
  60. }
  61. }
  62. }
  63. return nil
  64. }
  65. // Walk walks the file tree rooted at root, calling walkFn for each file or
  66. // directory in the tree, including root. All errors that arise visiting files
  67. // and directories are filtered by walkFn. The files are walked in lexical
  68. // order, which makes the output deterministic but means that for very
  69. // large directories Walk can be inefficient.
  70. // Walk does not follow symbolic links.
  71. func (f *walkFilesystem) Walk(root string, walkFn WalkFunc) error {
  72. info, err := f.Lstat(root)
  73. if err != nil {
  74. return walkFn(root, nil, err)
  75. }
  76. return f.walk(root, info, walkFn)
  77. }