gf/os/gspath/gspath.go

256 lines
7.9 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2019-01-15 23:27:47 +08:00
// Package gspath implements file index and search for folders.
2019-06-19 09:06:52 +08:00
//
// It searches file internally with high performance in order by the directory adding sequence.
// Note that:
// If caching feature enabled, there would be a searching delay after adding/deleting files.
package gspath
import (
"context"
2019-06-19 09:06:52 +08:00
"os"
"sort"
"strings"
2019-07-29 21:01:19 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/gmap"
2021-11-15 20:49:02 +08:00
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
)
// SPath manages the path searching feature.
type SPath struct {
paths *garray.StrArray // The searching directories array.
cache *gmap.StrStrMap // Searching cache map, it is not enabled if it's nil.
}
// SPathCacheItem is a cache item for searching.
2018-11-16 18:20:09 +08:00
type SPathCacheItem struct {
path string // Absolute path for file/dir.
isDir bool // Is directory or not.
2018-11-16 18:20:09 +08:00
}
var (
// Path to searching object mapping, used for instance management.
pathsMap = gmap.NewStrAnyMap(true)
)
// New creates and returns a new path searching manager.
func New(path string, cache bool) *SPath {
2019-06-19 09:06:52 +08:00
sp := &SPath{
2019-09-05 11:38:36 +08:00
paths: garray.NewStrArray(true),
2019-06-19 09:06:52 +08:00
}
if cache {
sp.cache = gmap.NewStrStrMap(true)
2019-06-19 09:06:52 +08:00
}
if len(path) > 0 {
if _, err := sp.Add(path); err != nil {
2021-11-15 20:49:02 +08:00
// intlog.Print(err)
2019-06-19 09:06:52 +08:00
}
}
return sp
}
// Get creates and returns a instance of searching manager for given path.
2021-02-06 11:42:58 +08:00
// The parameter `cache` specifies whether using cache feature for this manager.
// If cache feature is enabled, it asynchronously and recursively scans the path
// and updates all sub files/folders to the cache using package gfsnotify.
func Get(root string, cache bool) *SPath {
2019-08-16 00:29:14 +08:00
if root == "" {
root = "/"
}
2019-06-19 09:06:52 +08:00
return pathsMap.GetOrSetFuncLock(root, func() interface{} {
return New(root, cache)
}).(*SPath)
}
2021-02-06 11:42:58 +08:00
// Search searches file `name` under path `root`.
// The parameter `root` should be a absolute path. It will not automatically
// convert `root` to absolute path for performance reason.
// The optional parameter `indexFiles` specifies the searching index files when the result is a directory.
// For example, if the result `filePath` is a directory, and `indexFiles` is [index.html, main.html], it will also
// search [index.html, main.html] under `filePath`. It returns the absolute file path if any of them found,
// or else it returns `filePath`.
2019-06-19 09:06:52 +08:00
func Search(root string, name string, indexFiles ...string) (filePath string, isDir bool) {
return Get(root, false).Search(name, indexFiles...)
}
2021-02-06 11:42:58 +08:00
// SearchWithCache searches file `name` under path `root` with cache feature enabled.
// The parameter `root` should be a absolute path. It will not automatically
// convert `root` to absolute path for performance reason.
// The optional parameter `indexFiles` specifies the searching index files when the result is a directory.
// For example, if the result `filePath` is a directory, and `indexFiles` is [index.html, main.html], it will also
// search [index.html, main.html] under `filePath`. It returns the absolute file path if any of them found,
// or else it returns `filePath`.
2019-06-19 09:06:52 +08:00
func SearchWithCache(root string, name string, indexFiles ...string) (filePath string, isDir bool) {
return Get(root, true).Search(name, indexFiles...)
}
// Set deletes all other searching directories and sets the searching directory for this manager.
2018-11-16 18:20:09 +08:00
func (sp *SPath) Set(path string) (realPath string, err error) {
2019-06-19 09:06:52 +08:00
realPath = gfile.RealPath(path)
if realPath == "" {
realPath, _ = sp.Search(path)
if realPath == "" {
realPath = gfile.RealPath(gfile.Pwd() + gfile.Separator + path)
}
}
if realPath == "" {
return realPath, gerror.NewCodef(gcode.CodeInvalidParameter, `path "%s" does not exist`, path)
2019-06-19 09:06:52 +08:00
}
// The set path must be a directory.
2019-06-19 09:06:52 +08:00
if gfile.IsDir(realPath) {
realPath = strings.TrimRight(realPath, gfile.Separator)
if sp.paths.Search(realPath) != -1 {
for _, v := range sp.paths.Slice() {
sp.removeMonitorByPath(v)
}
}
intlog.Print(context.TODO(), "paths clear:", sp.paths)
2019-06-19 09:06:52 +08:00
sp.paths.Clear()
if sp.cache != nil {
sp.cache.Clear()
}
sp.paths.Append(realPath)
sp.updateCacheByPath(realPath)
sp.addMonitorByPath(realPath)
return realPath, nil
} else {
return "", gerror.NewCode(gcode.CodeInvalidParameter, path+" should be a folder")
2019-06-19 09:06:52 +08:00
}
}
// Add adds more searching directory to the manager.
// The manager will search file in added order.
2018-11-16 18:20:09 +08:00
func (sp *SPath) Add(path string) (realPath string, err error) {
2019-06-19 09:06:52 +08:00
realPath = gfile.RealPath(path)
if realPath == "" {
realPath, _ = sp.Search(path)
if realPath == "" {
realPath = gfile.RealPath(gfile.Pwd() + gfile.Separator + path)
}
}
if realPath == "" {
return realPath, gerror.NewCodef(gcode.CodeInvalidParameter, `path "%s" does not exist`, path)
2019-06-19 09:06:52 +08:00
}
// The added path must be a directory.
2019-06-19 09:06:52 +08:00
if gfile.IsDir(realPath) {
2021-11-15 20:49:02 +08:00
// fmt.Println("gspath:", realPath, sp.paths.Search(realPath))
// It will not add twice for the same directory.
2019-06-19 09:06:52 +08:00
if sp.paths.Search(realPath) < 0 {
realPath = strings.TrimRight(realPath, gfile.Separator)
sp.paths.Append(realPath)
sp.updateCacheByPath(realPath)
sp.addMonitorByPath(realPath)
}
return realPath, nil
} else {
return "", gerror.NewCode(gcode.CodeInvalidParameter, path+" should be a folder")
2019-06-19 09:06:52 +08:00
}
}
2021-02-06 11:42:58 +08:00
// Search searches file `name` in the manager.
// The optional parameter `indexFiles` specifies the searching index files when the result is a directory.
// For example, if the result `filePath` is a directory, and `indexFiles` is [index.html, main.html], it will also
// search [index.html, main.html] under `filePath`. It returns the absolute file path if any of them found,
// or else it returns `filePath`.
2019-06-19 09:06:52 +08:00
func (sp *SPath) Search(name string, indexFiles ...string) (filePath string, isDir bool) {
// No cache enabled.
2019-06-19 09:06:52 +08:00
if sp.cache == nil {
sp.paths.LockFunc(func(array []string) {
path := ""
for _, v := range array {
path = gfile.Join(v, name)
2019-12-25 21:22:06 +08:00
if stat, err := os.Stat(path); stat != nil && !os.IsNotExist(err) {
path = gfile.Abs(path)
// Security check: the result file path must be under the searching directory.
if len(path) >= len(v) && path[:len(v)] == v {
filePath = path
isDir = stat.IsDir()
break
}
2019-06-19 09:06:52 +08:00
}
}
})
if len(indexFiles) > 0 && isDir {
if name == "/" {
name = ""
}
path := ""
for _, file := range indexFiles {
path = filePath + gfile.Separator + file
if gfile.Exists(path) {
filePath = path
isDir = false
break
}
}
}
return
}
// Using cache feature.
2019-06-19 09:06:52 +08:00
name = sp.formatCacheName(name)
if v := sp.cache.Get(name); v != "" {
filePath, isDir = sp.parseCacheValue(v)
if len(indexFiles) > 0 && isDir {
if name == "/" {
name = ""
}
for _, file := range indexFiles {
2021-12-02 20:55:41 +08:00
if v = sp.cache.Get(name + "/" + file); v != "" {
2019-06-19 09:06:52 +08:00
return sp.parseCacheValue(v)
}
}
}
}
return
}
2021-02-06 11:42:58 +08:00
// Remove deletes the `path` from cache files of the manager.
// The parameter `path` can be either a absolute path or just a relative file name.
2018-11-17 11:17:02 +08:00
func (sp *SPath) Remove(path string) {
2019-06-19 09:06:52 +08:00
if sp.cache == nil {
return
}
if gfile.Exists(path) {
for _, v := range sp.paths.Slice() {
name := gstr.Replace(path, v, "")
name = sp.formatCacheName(name)
sp.cache.Remove(name)
}
} else {
name := sp.formatCacheName(path)
sp.cache.Remove(name)
}
2018-11-17 11:17:02 +08:00
}
// Paths returns all searching directories.
func (sp *SPath) Paths() []string {
2019-06-19 09:06:52 +08:00
return sp.paths.Slice()
}
// AllPaths returns all paths cached in the manager.
func (sp *SPath) AllPaths() []string {
2019-06-19 09:06:52 +08:00
if sp.cache == nil {
return nil
}
paths := sp.cache.Keys()
if len(paths) > 0 {
sort.Strings(paths)
}
return paths
}
// Size returns the count of the searching directories.
func (sp *SPath) Size() int {
2019-06-19 09:06:52 +08:00
return sp.paths.Len()
2018-11-16 18:20:09 +08:00
}