本站源代码
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.

147 lines
3.8KB

  1. package vfsutil
  2. import (
  3. "io"
  4. "net/http"
  5. "os"
  6. pathpkg "path"
  7. "path/filepath"
  8. "sort"
  9. )
  10. // Walk walks the filesystem rooted at root, calling walkFn for each file or
  11. // directory in the filesystem, including root. All errors that arise visiting files
  12. // and directories are filtered by walkFn. The files are walked in lexical
  13. // order.
  14. func Walk(fs http.FileSystem, root string, walkFn filepath.WalkFunc) error {
  15. info, err := Stat(fs, root)
  16. if err != nil {
  17. return walkFn(root, nil, err)
  18. }
  19. return walk(fs, root, info, walkFn)
  20. }
  21. // readDirNames reads the directory named by dirname and returns
  22. // a sorted list of directory entries.
  23. func readDirNames(fs http.FileSystem, dirname string) ([]string, error) {
  24. fis, err := ReadDir(fs, dirname)
  25. if err != nil {
  26. return nil, err
  27. }
  28. names := make([]string, len(fis))
  29. for i := range fis {
  30. names[i] = fis[i].Name()
  31. }
  32. sort.Strings(names)
  33. return names, nil
  34. }
  35. // walk recursively descends path, calling walkFn.
  36. func walk(fs http.FileSystem, path string, info os.FileInfo, walkFn filepath.WalkFunc) error {
  37. err := walkFn(path, info, nil)
  38. if err != nil {
  39. if info.IsDir() && err == filepath.SkipDir {
  40. return nil
  41. }
  42. return err
  43. }
  44. if !info.IsDir() {
  45. return nil
  46. }
  47. names, err := readDirNames(fs, path)
  48. if err != nil {
  49. return walkFn(path, info, err)
  50. }
  51. for _, name := range names {
  52. filename := pathpkg.Join(path, name)
  53. fileInfo, err := Stat(fs, filename)
  54. if err != nil {
  55. if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
  56. return err
  57. }
  58. } else {
  59. err = walk(fs, filename, fileInfo, walkFn)
  60. if err != nil {
  61. if !fileInfo.IsDir() || err != filepath.SkipDir {
  62. return err
  63. }
  64. }
  65. }
  66. }
  67. return nil
  68. }
  69. // WalkFilesFunc is the type of the function called for each file or directory visited by WalkFiles.
  70. // It's like filepath.WalkFunc, except it provides an additional ReadSeeker parameter for file being visited.
  71. type WalkFilesFunc func(path string, info os.FileInfo, rs io.ReadSeeker, err error) error
  72. // WalkFiles walks the filesystem rooted at root, calling walkFn for each file or
  73. // directory in the filesystem, including root. In addition to FileInfo, it passes an
  74. // ReadSeeker to walkFn for each file it visits.
  75. func WalkFiles(fs http.FileSystem, root string, walkFn WalkFilesFunc) error {
  76. file, info, err := openStat(fs, root)
  77. if err != nil {
  78. return walkFn(root, nil, nil, err)
  79. }
  80. return walkFiles(fs, root, info, file, walkFn)
  81. }
  82. // walkFiles recursively descends path, calling walkFn.
  83. // It closes the input file after it's done with it, so the caller shouldn't.
  84. func walkFiles(fs http.FileSystem, path string, info os.FileInfo, file http.File, walkFn WalkFilesFunc) error {
  85. err := walkFn(path, info, file, nil)
  86. file.Close()
  87. if err != nil {
  88. if info.IsDir() && err == filepath.SkipDir {
  89. return nil
  90. }
  91. return err
  92. }
  93. if !info.IsDir() {
  94. return nil
  95. }
  96. names, err := readDirNames(fs, path)
  97. if err != nil {
  98. return walkFn(path, info, nil, err)
  99. }
  100. for _, name := range names {
  101. filename := pathpkg.Join(path, name)
  102. file, fileInfo, err := openStat(fs, filename)
  103. if err != nil {
  104. if err := walkFn(filename, nil, nil, err); err != nil && err != filepath.SkipDir {
  105. return err
  106. }
  107. } else {
  108. err = walkFiles(fs, filename, fileInfo, file, walkFn)
  109. // file is closed by walkFiles, so we don't need to close it here.
  110. if err != nil {
  111. if !fileInfo.IsDir() || err != filepath.SkipDir {
  112. return err
  113. }
  114. }
  115. }
  116. }
  117. return nil
  118. }
  119. // openStat performs Open and Stat and returns results, or first error encountered.
  120. // The caller is responsible for closing the returned file when done.
  121. func openStat(fs http.FileSystem, name string) (http.File, os.FileInfo, error) {
  122. f, err := fs.Open(name)
  123. if err != nil {
  124. return nil, nil, err
  125. }
  126. fi, err := f.Stat()
  127. if err != nil {
  128. f.Close()
  129. return nil, nil, err
  130. }
  131. return f, fi, nil
  132. }
上海开阖软件有限公司 沪ICP备12045867号-1