gfsnotify_filefunc.go 3.4 KB

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