gfile_scan.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gfile
  7. import (
  8. "github.com/gogf/gf/errors/gcode"
  9. "github.com/gogf/gf/errors/gerror"
  10. "github.com/gogf/gf/text/gstr"
  11. "os"
  12. "path/filepath"
  13. "sort"
  14. )
  15. const (
  16. // Max recursive depth for directory scanning.
  17. maxScanDepth = 100000
  18. )
  19. // ScanDir returns all sub-files with absolute paths of given <path>,
  20. // It scans directory recursively if given parameter <recursive> is true.
  21. //
  22. // The pattern parameter <pattern> supports multiple file name patterns,
  23. // using the ',' symbol to separate multiple patterns.
  24. func ScanDir(path string, pattern string, recursive ...bool) ([]string, error) {
  25. isRecursive := false
  26. if len(recursive) > 0 {
  27. isRecursive = recursive[0]
  28. }
  29. list, err := doScanDir(0, path, pattern, isRecursive, nil)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if len(list) > 0 {
  34. sort.Strings(list)
  35. }
  36. return list, nil
  37. }
  38. // ScanDirFunc returns all sub-files with absolute paths of given <path>,
  39. // It scans directory recursively if given parameter <recursive> is true.
  40. //
  41. // The pattern parameter <pattern> supports multiple file name patterns, using the ','
  42. // symbol to separate multiple patterns.
  43. //
  44. // The parameter <recursive> specifies whether scanning the <path> recursively, which
  45. // means it scans its sub-files and appends the files path to result array if the sub-file
  46. // is also a folder. It is false in default.
  47. //
  48. // The parameter <handler> specifies the callback function handling each sub-file path of
  49. // the <path> and its sub-folders. It ignores the sub-file path if <handler> returns an empty
  50. // string, or else it appends the sub-file path to result slice.
  51. func ScanDirFunc(path string, pattern string, recursive bool, handler func(path string) string) ([]string, error) {
  52. list, err := doScanDir(0, path, pattern, recursive, handler)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if len(list) > 0 {
  57. sort.Strings(list)
  58. }
  59. return list, nil
  60. }
  61. // ScanDirFile returns all sub-files with absolute paths of given <path>,
  62. // It scans directory recursively if given parameter <recursive> is true.
  63. //
  64. // The pattern parameter <pattern> supports multiple file name patterns,
  65. // using the ',' symbol to separate multiple patterns.
  66. //
  67. // Note that it returns only files, exclusive of directories.
  68. func ScanDirFile(path string, pattern string, recursive ...bool) ([]string, error) {
  69. isRecursive := false
  70. if len(recursive) > 0 {
  71. isRecursive = recursive[0]
  72. }
  73. list, err := doScanDir(0, path, pattern, isRecursive, func(path string) string {
  74. if IsDir(path) {
  75. return ""
  76. }
  77. return path
  78. })
  79. if err != nil {
  80. return nil, err
  81. }
  82. if len(list) > 0 {
  83. sort.Strings(list)
  84. }
  85. return list, nil
  86. }
  87. // ScanDirFileFunc returns all sub-files with absolute paths of given <path>,
  88. // It scans directory recursively if given parameter <recursive> is true.
  89. //
  90. // The pattern parameter <pattern> supports multiple file name patterns, using the ','
  91. // symbol to separate multiple patterns.
  92. //
  93. // The parameter <recursive> specifies whether scanning the <path> recursively, which
  94. // means it scans its sub-files and appends the files path to result array if the sub-file
  95. // is also a folder. It is false in default.
  96. //
  97. // The parameter <handler> specifies the callback function handling each sub-file path of
  98. // the <path> and its sub-folders. It ignores the sub-file path if <handler> returns an empty
  99. // string, or else it appends the sub-file path to result slice.
  100. //
  101. // Note that the parameter <path> for <handler> is not a directory but a file.
  102. // It returns only files, exclusive of directories.
  103. func ScanDirFileFunc(path string, pattern string, recursive bool, handler func(path string) string) ([]string, error) {
  104. list, err := doScanDir(0, path, pattern, recursive, func(path string) string {
  105. if IsDir(path) {
  106. return ""
  107. }
  108. return handler(path)
  109. })
  110. if err != nil {
  111. return nil, err
  112. }
  113. if len(list) > 0 {
  114. sort.Strings(list)
  115. }
  116. return list, nil
  117. }
  118. // doScanDir is an internal method which scans directory and returns the absolute path
  119. // list of files that are not sorted.
  120. //
  121. // The pattern parameter <pattern> supports multiple file name patterns, using the ','
  122. // symbol to separate multiple patterns.
  123. //
  124. // The parameter <recursive> specifies whether scanning the <path> recursively, which
  125. // means it scans its sub-files and appends the files path to result array if the sub-file
  126. // is also a folder. It is false in default.
  127. //
  128. // The parameter <handler> specifies the callback function handling each sub-file path of
  129. // the <path> and its sub-folders. It ignores the sub-file path if <handler> returns an empty
  130. // string, or else it appends the sub-file path to result slice.
  131. func doScanDir(depth int, path string, pattern string, recursive bool, handler func(path string) string) ([]string, error) {
  132. if depth >= maxScanDepth {
  133. return nil, gerror.NewCodef(gcode.CodeOperationFailed, "directory scanning exceeds max recursive depth: %d", maxScanDepth)
  134. }
  135. list := ([]string)(nil)
  136. file, err := os.Open(path)
  137. if err != nil {
  138. return nil, err
  139. }
  140. defer file.Close()
  141. names, err := file.Readdirnames(-1)
  142. if err != nil {
  143. return nil, err
  144. }
  145. var (
  146. filePath = ""
  147. patterns = gstr.SplitAndTrim(pattern, ",")
  148. )
  149. for _, name := range names {
  150. filePath = path + Separator + name
  151. if IsDir(filePath) && recursive {
  152. array, _ := doScanDir(depth+1, filePath, pattern, true, handler)
  153. if len(array) > 0 {
  154. list = append(list, array...)
  155. }
  156. }
  157. // Handler filtering.
  158. if handler != nil {
  159. filePath = handler(filePath)
  160. if filePath == "" {
  161. continue
  162. }
  163. }
  164. // If it meets pattern, then add it to the result list.
  165. for _, p := range patterns {
  166. if match, err := filepath.Match(p, name); err == nil && match {
  167. filePath = Abs(filePath)
  168. if filePath != "" {
  169. list = append(list, filePath)
  170. }
  171. }
  172. }
  173. }
  174. return list, nil
  175. }