walkfs.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. path, err := Canonicalize(path)
  34. if err != nil {
  35. return err
  36. }
  37. err = walkFn(path, info, nil)
  38. if err != nil {
  39. if info.IsDir() && err == SkipDir {
  40. return nil
  41. }
  42. return err
  43. }
  44. if !info.IsDir() && path != "." {
  45. return nil
  46. }
  47. names, err := f.DirNames(path)
  48. if err != nil {
  49. return walkFn(path, info, err)
  50. }
  51. for _, name := range names {
  52. filename := filepath.Join(path, name)
  53. fileInfo, err := f.Lstat(filename)
  54. if err != nil {
  55. if err := walkFn(filename, fileInfo, err); err != nil && err != SkipDir {
  56. return err
  57. }
  58. } else {
  59. err = f.walk(filename, fileInfo, walkFn)
  60. if err != nil {
  61. if !fileInfo.IsDir() || err != SkipDir {
  62. return err
  63. }
  64. }
  65. }
  66. }
  67. return nil
  68. }
  69. // Walk walks the file tree rooted at root, calling walkFn for each file or
  70. // directory in the tree, including root. All errors that arise visiting files
  71. // and directories are filtered by walkFn. The files are walked in lexical
  72. // order, which makes the output deterministic but means that for very
  73. // large directories Walk can be inefficient.
  74. // Walk does not follow symbolic links.
  75. func (f *walkFilesystem) Walk(root string, walkFn WalkFunc) error {
  76. info, err := f.Lstat(root)
  77. if err != nil {
  78. return walkFn(root, nil, err)
  79. }
  80. return f.walk(root, info, walkFn)
  81. }