gspath.go 7.7 KB

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