file.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package utils
  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. // #nosec G304
  22. // ReadFile returns the raw content of a file
  23. func ReadFile(path string, fs http.FileSystem) ([]byte, error) {
  24. if fs != nil {
  25. file, err := fs.Open(path)
  26. if err != nil {
  27. return nil, err
  28. }
  29. defer file.Close()
  30. return io.ReadAll(file)
  31. }
  32. return os.ReadFile(path)
  33. }
  34. // readDirNames reads the directory named by dirname and returns
  35. // a sorted list of directory entries.
  36. func readDirNames(fs http.FileSystem, dirname string) ([]string, error) {
  37. fis, err := readDir(fs, dirname)
  38. if err != nil {
  39. return nil, err
  40. }
  41. names := make([]string, len(fis))
  42. for i := range fis {
  43. names[i] = fis[i].Name()
  44. }
  45. sort.Strings(names)
  46. return names, nil
  47. }
  48. // walk recursively descends path, calling walkFn.
  49. func walk(fs http.FileSystem, path string, info os.FileInfo, walkFn filepath.WalkFunc) error {
  50. err := walkFn(path, info, nil)
  51. if err != nil {
  52. if info.IsDir() && err == filepath.SkipDir {
  53. return nil
  54. }
  55. return err
  56. }
  57. if !info.IsDir() {
  58. return nil
  59. }
  60. names, err := readDirNames(fs, path)
  61. if err != nil {
  62. return walkFn(path, info, err)
  63. }
  64. for _, name := range names {
  65. filename := pathpkg.Join(path, name)
  66. fileInfo, err := stat(fs, filename)
  67. if err != nil {
  68. if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
  69. return err
  70. }
  71. } else {
  72. err = walk(fs, filename, fileInfo, walkFn)
  73. if err != nil {
  74. if !fileInfo.IsDir() || err != filepath.SkipDir {
  75. return err
  76. }
  77. }
  78. }
  79. }
  80. return nil
  81. }
  82. // readDir reads the contents of the directory associated with file and
  83. // returns a slice of FileInfo values in directory order.
  84. func readDir(fs http.FileSystem, name string) ([]os.FileInfo, error) {
  85. f, err := fs.Open(name)
  86. if err != nil {
  87. return nil, err
  88. }
  89. defer f.Close()
  90. return f.Readdir(0)
  91. }
  92. // stat returns the FileInfo structure describing file.
  93. func stat(fs http.FileSystem, name string) (os.FileInfo, error) {
  94. f, err := fs.Open(name)
  95. if err != nil {
  96. return nil, err
  97. }
  98. defer f.Close()
  99. return f.Stat()
  100. }