gfsnotify_filefunc.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 gfsnotify
  7. import (
  8. "fmt"
  9. "os"
  10. "path/filepath"
  11. "sort"
  12. "strings"
  13. "github.com/gogf/gf/v2/errors/gerror"
  14. )
  15. // fileDir returns all but the last element of path, typically the path's directory.
  16. // After dropping the final element, Dir calls Clean on the path and trailing
  17. // slashes are removed.
  18. // If the path is empty, Dir returns ".".
  19. // If the path consists entirely of separators, Dir returns a single separator.
  20. // The returned path does not end in a separator unless it is the root directory.
  21. func fileDir(path string) string {
  22. return filepath.Dir(path)
  23. }
  24. // fileRealPath converts the given `path` to its absolute path
  25. // and checks if the file path exists.
  26. // If the file does not exist, return an empty string.
  27. func fileRealPath(path string) string {
  28. p, err := filepath.Abs(path)
  29. if err != nil {
  30. return ""
  31. }
  32. if !fileExists(p) {
  33. return ""
  34. }
  35. return p
  36. }
  37. // fileExists checks whether given `path` exist.
  38. func fileExists(path string) bool {
  39. if stat, err := os.Stat(path); stat != nil && !os.IsNotExist(err) {
  40. return true
  41. }
  42. return false
  43. }
  44. // fileIsDir checks whether given `path` a directory.
  45. func fileIsDir(path string) bool {
  46. s, err := os.Stat(path)
  47. if err != nil {
  48. return false
  49. }
  50. return s.IsDir()
  51. }
  52. // fileAllDirs returns all sub-folders including itself of given `path` recursively.
  53. func fileAllDirs(path string) (list []string) {
  54. list = []string{path}
  55. file, err := os.Open(path)
  56. if err != nil {
  57. return list
  58. }
  59. defer file.Close()
  60. names, err := file.Readdirnames(-1)
  61. if err != nil {
  62. return list
  63. }
  64. for _, name := range names {
  65. tempPath := fmt.Sprintf("%s%s%s", path, string(filepath.Separator), name)
  66. if fileIsDir(tempPath) {
  67. if array := fileAllDirs(tempPath); len(array) > 0 {
  68. list = append(list, array...)
  69. }
  70. }
  71. }
  72. return
  73. }
  74. // fileScanDir returns all sub-files with absolute paths of given `path`,
  75. // It scans directory recursively if given parameter `recursive` is true.
  76. func fileScanDir(path string, pattern string, recursive ...bool) ([]string, error) {
  77. list, err := doFileScanDir(path, pattern, recursive...)
  78. if err != nil {
  79. return nil, err
  80. }
  81. if len(list) > 0 {
  82. sort.Strings(list)
  83. }
  84. return list, nil
  85. }
  86. // doFileScanDir is an internal method which scans directory
  87. // and returns the absolute path list of files that are not sorted.
  88. //
  89. // The pattern parameter `pattern` supports multiple file name patterns,
  90. // using the ',' symbol to separate multiple patterns.
  91. //
  92. // It scans directory recursively if given parameter `recursive` is true.
  93. func doFileScanDir(path string, pattern string, recursive ...bool) ([]string, error) {
  94. var (
  95. list []string
  96. file, err = os.Open(path)
  97. )
  98. if err != nil {
  99. err = gerror.Wrapf(err, `os.Open failed for path "%s"`, path)
  100. return nil, err
  101. }
  102. defer file.Close()
  103. names, err := file.Readdirnames(-1)
  104. if err != nil {
  105. err = gerror.Wrapf(err, `read directory files failed for path "%s"`, path)
  106. return nil, err
  107. }
  108. filePath := ""
  109. for _, name := range names {
  110. filePath = fmt.Sprintf("%s%s%s", path, string(filepath.Separator), name)
  111. if fileIsDir(filePath) && len(recursive) > 0 && recursive[0] {
  112. array, _ := doFileScanDir(filePath, pattern, true)
  113. if len(array) > 0 {
  114. list = append(list, array...)
  115. }
  116. }
  117. for _, p := range strings.Split(pattern, ",") {
  118. if match, _ := filepath.Match(strings.TrimSpace(p), name); match {
  119. list = append(list, filePath)
  120. }
  121. }
  122. }
  123. return list, nil
  124. }