basicfs_walk.go 2.7 KB

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