gspath.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 gspath implements file index and search for folders.
  7. //
  8. // It searches file internally with high performance in order by the directory adding sequence.
  9. // Note that:
  10. // If caching feature enabled, there would be a searching delay after adding/deleting files.
  11. package gspath
  12. import (
  13. "context"
  14. "github.com/gogf/gf/errors/gcode"
  15. "github.com/gogf/gf/errors/gerror"
  16. "github.com/gogf/gf/internal/intlog"
  17. "os"
  18. "sort"
  19. "strings"
  20. "github.com/gogf/gf/container/garray"
  21. "github.com/gogf/gf/container/gmap"
  22. "github.com/gogf/gf/os/gfile"
  23. "github.com/gogf/gf/text/gstr"
  24. )
  25. // SPath manages the path searching feature.
  26. type SPath struct {
  27. paths *garray.StrArray // The searching directories array.
  28. cache *gmap.StrStrMap // Searching cache map, it is not enabled if it's nil.
  29. }
  30. // SPathCacheItem is a cache item for searching.
  31. type SPathCacheItem struct {
  32. path string // Absolute path for file/dir.
  33. isDir bool // Is directory or not.
  34. }
  35. var (
  36. // Path to searching object mapping, used for instance management.
  37. pathsMap = gmap.NewStrAnyMap(true)
  38. )
  39. // New creates and returns a new path searching manager.
  40. func New(path string, cache bool) *SPath {
  41. sp := &SPath{
  42. paths: garray.NewStrArray(true),
  43. }
  44. if cache {
  45. sp.cache = gmap.NewStrStrMap(true)
  46. }
  47. if len(path) > 0 {
  48. if _, err := sp.Add(path); err != nil {
  49. //intlog.Print(err)
  50. }
  51. }
  52. return sp
  53. }
  54. // Get creates and returns a instance of searching manager for given path.
  55. // The parameter `cache` specifies whether using cache feature for this manager.
  56. // If cache feature is enabled, it asynchronously and recursively scans the path
  57. // and updates all sub files/folders to the cache using package gfsnotify.
  58. func Get(root string, cache bool) *SPath {
  59. if root == "" {
  60. root = "/"
  61. }
  62. return pathsMap.GetOrSetFuncLock(root, func() interface{} {
  63. return New(root, cache)
  64. }).(*SPath)
  65. }
  66. // Search searches file `name` under path `root`.
  67. // The parameter `root` should be a absolute path. It will not automatically
  68. // convert `root` to absolute path for performance reason.
  69. // The optional parameter `indexFiles` specifies the searching index files when the result is a directory.
  70. // For example, if the result `filePath` is a directory, and `indexFiles` is [index.html, main.html], it will also
  71. // search [index.html, main.html] under `filePath`. It returns the absolute file path if any of them found,
  72. // or else it returns `filePath`.
  73. func Search(root string, name string, indexFiles ...string) (filePath string, isDir bool) {
  74. return Get(root, false).Search(name, indexFiles...)
  75. }
  76. // SearchWithCache searches file `name` under path `root` with cache feature enabled.
  77. // The parameter `root` should be a absolute path. It will not automatically
  78. // convert `root` to absolute path for performance reason.
  79. // The optional parameter `indexFiles` specifies the searching index files when the result is a directory.
  80. // For example, if the result `filePath` is a directory, and `indexFiles` is [index.html, main.html], it will also
  81. // search [index.html, main.html] under `filePath`. It returns the absolute file path if any of them found,
  82. // or else it returns `filePath`.
  83. func SearchWithCache(root string, name string, indexFiles ...string) (filePath string, isDir bool) {
  84. return Get(root, true).Search(name, indexFiles...)
  85. }
  86. // Set deletes all other searching directories and sets the searching directory for this manager.
  87. func (sp *SPath) Set(path string) (realPath string, err error) {
  88. realPath = gfile.RealPath(path)
  89. if realPath == "" {
  90. realPath, _ = sp.Search(path)
  91. if realPath == "" {
  92. realPath = gfile.RealPath(gfile.Pwd() + gfile.Separator + path)
  93. }
  94. }
  95. if realPath == "" {
  96. return realPath, gerror.NewCodef(gcode.CodeInvalidParameter, `path "%s" does not exist`, path)
  97. }
  98. // The set path must be a directory.
  99. if gfile.IsDir(realPath) {
  100. realPath = strings.TrimRight(realPath, gfile.Separator)
  101. if sp.paths.Search(realPath) != -1 {
  102. for _, v := range sp.paths.Slice() {
  103. sp.removeMonitorByPath(v)
  104. }
  105. }
  106. intlog.Print(context.TODO(), "paths clear:", sp.paths)
  107. sp.paths.Clear()
  108. if sp.cache != nil {
  109. sp.cache.Clear()
  110. }
  111. sp.paths.Append(realPath)
  112. sp.updateCacheByPath(realPath)
  113. sp.addMonitorByPath(realPath)
  114. return realPath, nil
  115. } else {
  116. return "", gerror.NewCode(gcode.CodeInvalidParameter, path+" should be a folder")
  117. }
  118. }
  119. // Add adds more searching directory to the manager.
  120. // The manager will search file in added order.
  121. func (sp *SPath) Add(path string) (realPath string, err error) {
  122. realPath = gfile.RealPath(path)
  123. if realPath == "" {
  124. realPath, _ = sp.Search(path)
  125. if realPath == "" {
  126. realPath = gfile.RealPath(gfile.Pwd() + gfile.Separator + path)
  127. }
  128. }
  129. if realPath == "" {
  130. return realPath, gerror.NewCodef(gcode.CodeInvalidParameter, `path "%s" does not exist`, path)
  131. }
  132. // The added path must be a directory.
  133. if gfile.IsDir(realPath) {
  134. //fmt.Println("gspath:", realPath, sp.paths.Search(realPath))
  135. // It will not add twice for the same directory.
  136. if sp.paths.Search(realPath) < 0 {
  137. realPath = strings.TrimRight(realPath, gfile.Separator)
  138. sp.paths.Append(realPath)
  139. sp.updateCacheByPath(realPath)
  140. sp.addMonitorByPath(realPath)
  141. }
  142. return realPath, nil
  143. } else {
  144. return "", gerror.NewCode(gcode.CodeInvalidParameter, path+" should be a folder")
  145. }
  146. }
  147. // Search searches file `name` in the manager.
  148. // The optional parameter `indexFiles` specifies the searching index files when the result is a directory.
  149. // For example, if the result `filePath` is a directory, and `indexFiles` is [index.html, main.html], it will also
  150. // search [index.html, main.html] under `filePath`. It returns the absolute file path if any of them found,
  151. // or else it returns `filePath`.
  152. func (sp *SPath) Search(name string, indexFiles ...string) (filePath string, isDir bool) {
  153. // No cache enabled.
  154. if sp.cache == nil {
  155. sp.paths.LockFunc(func(array []string) {
  156. path := ""
  157. for _, v := range array {
  158. path = gfile.Join(v, name)
  159. if stat, err := os.Stat(path); stat != nil && !os.IsNotExist(err) {
  160. path = gfile.Abs(path)
  161. // Security check: the result file path must be under the searching directory.
  162. if len(path) >= len(v) && path[:len(v)] == v {
  163. filePath = path
  164. isDir = stat.IsDir()
  165. break
  166. }
  167. }
  168. }
  169. })
  170. if len(indexFiles) > 0 && isDir {
  171. if name == "/" {
  172. name = ""
  173. }
  174. path := ""
  175. for _, file := range indexFiles {
  176. path = filePath + gfile.Separator + file
  177. if gfile.Exists(path) {
  178. filePath = path
  179. isDir = false
  180. break
  181. }
  182. }
  183. }
  184. return
  185. }
  186. // Using cache feature.
  187. name = sp.formatCacheName(name)
  188. if v := sp.cache.Get(name); v != "" {
  189. filePath, isDir = sp.parseCacheValue(v)
  190. if len(indexFiles) > 0 && isDir {
  191. if name == "/" {
  192. name = ""
  193. }
  194. for _, file := range indexFiles {
  195. if v := sp.cache.Get(name + "/" + file); v != "" {
  196. return sp.parseCacheValue(v)
  197. }
  198. }
  199. }
  200. }
  201. return
  202. }
  203. // Remove deletes the `path` from cache files of the manager.
  204. // The parameter `path` can be either a absolute path or just a relative file name.
  205. func (sp *SPath) Remove(path string) {
  206. if sp.cache == nil {
  207. return
  208. }
  209. if gfile.Exists(path) {
  210. for _, v := range sp.paths.Slice() {
  211. name := gstr.Replace(path, v, "")
  212. name = sp.formatCacheName(name)
  213. sp.cache.Remove(name)
  214. }
  215. } else {
  216. name := sp.formatCacheName(path)
  217. sp.cache.Remove(name)
  218. }
  219. }
  220. // Paths returns all searching directories.
  221. func (sp *SPath) Paths() []string {
  222. return sp.paths.Slice()
  223. }
  224. // AllPaths returns all paths cached in the manager.
  225. func (sp *SPath) AllPaths() []string {
  226. if sp.cache == nil {
  227. return nil
  228. }
  229. paths := sp.cache.Keys()
  230. if len(paths) > 0 {
  231. sort.Strings(paths)
  232. }
  233. return paths
  234. }
  235. // Size returns the count of the searching directories.
  236. func (sp *SPath) Size() int {
  237. return sp.paths.Len()
  238. }