2018-05-03 13:35:08 +08:00
|
|
|
|
// Copyright 2018 gf Author(https://gitee.com/johng/gf). 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://gitee.com/johng/gf.
|
|
|
|
|
|
2019-01-15 23:27:47 +08:00
|
|
|
|
// Package gspath implements file index and search for folders.
|
2019-01-16 13:35:16 +08:00
|
|
|
|
//
|
|
|
|
|
// 搜索目录管理,
|
2018-05-03 13:35:08 +08:00
|
|
|
|
// 可以添加搜索目录,按照添加的优先级进行文件检索,并在内部进行高效缓存处理。
|
|
|
|
|
package gspath
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
2018-10-30 23:58:10 +08:00
|
|
|
|
"fmt"
|
2018-11-16 18:20:09 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/garray"
|
2018-05-03 13:35:08 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/gmap"
|
2018-10-25 22:18:36 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gfile"
|
2018-05-31 12:07:31 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gfsnotify"
|
2018-11-16 18:20:09 +08:00
|
|
|
|
"gitee.com/johng/gf/g/util/gstr"
|
|
|
|
|
"runtime"
|
2018-11-17 02:39:23 +08:00
|
|
|
|
"sort"
|
2018-10-25 22:18:36 +08:00
|
|
|
|
"strings"
|
2018-05-03 13:35:08 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 文件目录搜索管理对象
|
|
|
|
|
type SPath struct {
|
2018-11-23 16:45:30 +08:00
|
|
|
|
paths *garray.StringArray // 搜索路径,按照优先级进行排序
|
|
|
|
|
cache *gmap.StringStringMap // 搜索结果缓存map
|
2018-05-03 13:35:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 18:20:09 +08:00
|
|
|
|
// 文件搜索缓存项
|
|
|
|
|
type SPathCacheItem struct {
|
2018-11-28 20:19:28 +08:00
|
|
|
|
path string // 文件/目录绝对路径
|
|
|
|
|
isDir bool // 是否目录
|
2018-11-16 18:20:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 09:48:57 +08:00
|
|
|
|
var (
|
|
|
|
|
// 单个目录路径对应的SPath对象指针,用于路径检索对象复用
|
|
|
|
|
pathsMap = gmap.NewStringInterfaceMap()
|
|
|
|
|
)
|
|
|
|
|
|
2018-11-16 18:20:09 +08:00
|
|
|
|
// 创建一个搜索对象
|
2018-11-30 09:48:57 +08:00
|
|
|
|
func New(path...string) *SPath {
|
|
|
|
|
sp := &SPath {
|
|
|
|
|
paths : garray.NewStringArray(0, 1),
|
2018-11-23 16:45:30 +08:00
|
|
|
|
cache : gmap.NewStringStringMap(),
|
2018-05-03 13:35:08 +08:00
|
|
|
|
}
|
2018-11-30 09:48:57 +08:00
|
|
|
|
if len(path) > 0 {
|
|
|
|
|
sp.Add(path[0])
|
|
|
|
|
}
|
|
|
|
|
return sp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建/获取一个单例的搜索对象, root必须为目录的绝对路径
|
|
|
|
|
func Get(root string) *SPath {
|
|
|
|
|
return pathsMap.GetOrSetFuncLock(root, func() interface{} {
|
|
|
|
|
return New(root)
|
|
|
|
|
}).(*SPath)
|
2018-05-03 13:35:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 09:48:57 +08:00
|
|
|
|
// 检索root目录(必须为绝对路径)下面的name文件的绝对路径,indexFiles用于指定当检索到的结果为目录时,同时检索是否存在这些indexFiles文件
|
|
|
|
|
func Search(root string, name string, indexFiles...string) (filePath string, isDir bool) {
|
|
|
|
|
return Get(root).Search(name, indexFiles...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-05-03 13:35:08 +08:00
|
|
|
|
// 设置搜索路径,只保留当前设置项,其他搜索路径被清空
|
2018-11-16 18:20:09 +08:00
|
|
|
|
func (sp *SPath) Set(path string) (realPath string, err error) {
|
|
|
|
|
realPath = gfile.RealPath(path)
|
|
|
|
|
if realPath == "" {
|
2018-11-17 02:39:23 +08:00
|
|
|
|
realPath, _ = sp.Search(path)
|
2018-11-16 18:20:09 +08:00
|
|
|
|
if realPath == "" {
|
|
|
|
|
realPath = gfile.RealPath(gfile.Pwd() + gfile.Separator + path)
|
2018-08-14 19:53:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-16 18:20:09 +08:00
|
|
|
|
if realPath == "" {
|
|
|
|
|
return realPath, errors.New(fmt.Sprintf(`path "%s" does not exist`, path))
|
|
|
|
|
}
|
|
|
|
|
// 设置的搜索路径必须为目录
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sp.paths.Clear()
|
2018-05-03 13:35:08 +08:00
|
|
|
|
sp.cache.Clear()
|
2018-11-30 09:48:57 +08:00
|
|
|
|
|
2018-11-16 18:20:09 +08:00
|
|
|
|
sp.paths.Append(realPath)
|
|
|
|
|
sp.updateCacheByPath(realPath)
|
|
|
|
|
sp.addMonitorByPath(realPath)
|
|
|
|
|
return realPath, nil
|
|
|
|
|
} else {
|
|
|
|
|
return "", errors.New(path + " should be a folder")
|
2018-05-03 13:35:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加搜索路径
|
2018-11-16 18:20:09 +08:00
|
|
|
|
func (sp *SPath) Add(path string) (realPath string, err error) {
|
|
|
|
|
realPath = gfile.RealPath(path)
|
|
|
|
|
if realPath == "" {
|
2018-11-17 02:39:23 +08:00
|
|
|
|
realPath, _ = sp.Search(path)
|
2018-11-16 18:20:09 +08:00
|
|
|
|
if realPath == "" {
|
|
|
|
|
realPath = gfile.RealPath(gfile.Pwd() + gfile.Separator + path)
|
2018-08-14 19:53:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-16 18:20:09 +08:00
|
|
|
|
if realPath == "" {
|
|
|
|
|
return realPath, errors.New(fmt.Sprintf(`path "%s" does not exist`, path))
|
|
|
|
|
}
|
|
|
|
|
// 添加的搜索路径必须为目录
|
|
|
|
|
if gfile.IsDir(realPath) {
|
|
|
|
|
// 如果已经添加则不再添加
|
|
|
|
|
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 "", errors.New(path + " should be a folder")
|
2018-05-03 13:35:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 18:20:09 +08:00
|
|
|
|
// 给定的name只是相对文件路径,找不到该文件时,返回空字符串;
|
2018-11-17 02:39:23 +08:00
|
|
|
|
// 当给定indexFiles时,如果name时一个目录,那么会进一步检索其下对应的indexFiles文件是否存在,存在则返回indexFile绝对路径;
|
2018-11-16 18:20:09 +08:00
|
|
|
|
// 否则返回name目录绝对路径。
|
2018-11-23 16:45:30 +08:00
|
|
|
|
func (sp *SPath) Search(name string, indexFiles...string) (filePath string, isDir bool) {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
name = sp.formatCacheName(name)
|
2018-11-23 16:45:30 +08:00
|
|
|
|
if v := sp.cache.Get(name); v != "" {
|
|
|
|
|
filePath, isDir = sp.parseCacheValue(v)
|
|
|
|
|
if len(indexFiles) > 0 && isDir {
|
2018-11-17 19:45:15 +08:00
|
|
|
|
if name == "/" {
|
|
|
|
|
name = ""
|
|
|
|
|
}
|
2018-11-16 18:20:09 +08:00
|
|
|
|
for _, file := range indexFiles {
|
2018-11-23 16:45:30 +08:00
|
|
|
|
if v := sp.cache.Get(name + "/" + file); v != "" {
|
|
|
|
|
return sp.parseCacheValue(v)
|
2018-11-16 18:20:09 +08:00
|
|
|
|
}
|
2018-05-03 13:35:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-23 16:45:30 +08:00
|
|
|
|
return
|
2018-11-17 02:39:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-17 11:17:02 +08:00
|
|
|
|
// 从搜索路径中移除指定的文件,这样该文件无法给搜索。
|
|
|
|
|
// path可以是绝对路径,也可以相对路径。
|
|
|
|
|
func (sp *SPath) Remove(path string) {
|
|
|
|
|
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-25 22:18:36 +08:00
|
|
|
|
// 返回当前对象搜索目录路径列表
|
|
|
|
|
func (sp *SPath) Paths() []string {
|
|
|
|
|
return sp.paths.Slice()
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-17 02:39:23 +08:00
|
|
|
|
// 返回当前对象缓存的所有路径列表
|
|
|
|
|
func (sp *SPath) AllPaths() []string {
|
|
|
|
|
paths := sp.cache.Keys()
|
|
|
|
|
if len(paths) > 0 {
|
|
|
|
|
sort.Strings(paths)
|
|
|
|
|
}
|
|
|
|
|
return paths
|
2018-05-31 12:07:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 当前的搜索路径数量
|
|
|
|
|
func (sp *SPath) Size() int {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
return sp.paths.Len()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 递归添加目录下的文件
|
|
|
|
|
func (sp *SPath) updateCacheByPath(path string) {
|
|
|
|
|
sp.addToCache(path, path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 格式化name返回符合规范的缓存名称,分隔符号统一为'/',且前缀必须以'/'开头(类似HTTP URI).
|
|
|
|
|
func (sp *SPath) formatCacheName(name string) string {
|
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
|
name = gstr.Replace(name, "\\", "/")
|
|
|
|
|
}
|
2018-11-18 19:14:17 +08:00
|
|
|
|
return "/" + strings.Trim(name, "./")
|
2018-11-16 18:20:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 09:20:45 +08:00
|
|
|
|
// 根据path计算出对应的缓存name, dirPath为检索根目录路径
|
|
|
|
|
func (sp *SPath) nameFromPath(filePath, rootPath string) string {
|
|
|
|
|
name := gstr.Replace(filePath, rootPath, "")
|
2018-11-16 18:20:09 +08:00
|
|
|
|
name = sp.formatCacheName(name)
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 16:45:30 +08:00
|
|
|
|
// 按照一定数据结构生成缓存的数据项字符串
|
|
|
|
|
func (sp *SPath) makeCacheValue(filePath string, isDir bool) string {
|
|
|
|
|
if isDir {
|
|
|
|
|
return filePath + "_D_"
|
|
|
|
|
}
|
|
|
|
|
return filePath + "_F_"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按照一定数据结构解析数据项字符串
|
|
|
|
|
func (sp *SPath) parseCacheValue(value string) (filePath string, isDir bool) {
|
|
|
|
|
if value[len(value) - 2 : len(value) - 1][0] == 'F' {
|
|
|
|
|
return value[: len(value) - 3], false
|
|
|
|
|
}
|
|
|
|
|
return value[: len(value) - 3], true
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 18:20:09 +08:00
|
|
|
|
// 添加path到缓存中(递归)
|
2018-11-23 09:20:45 +08:00
|
|
|
|
func (sp *SPath) addToCache(filePath, rootPath string) {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
// 首先添加自身
|
|
|
|
|
idDir := gfile.IsDir(filePath)
|
2018-11-23 16:45:30 +08:00
|
|
|
|
sp.cache.SetIfNotExist(sp.nameFromPath(filePath, rootPath), sp.makeCacheValue(filePath, idDir))
|
2018-11-23 09:20:45 +08:00
|
|
|
|
// 如果添加的是目录,那么需要递归添加
|
2018-11-16 18:20:09 +08:00
|
|
|
|
if idDir {
|
|
|
|
|
if files, err := gfile.ScanDir(filePath, "*", true); err == nil {
|
|
|
|
|
for _, path := range files {
|
2018-11-23 16:45:30 +08:00
|
|
|
|
sp.cache.SetIfNotExist(sp.nameFromPath(path, rootPath), sp.makeCacheValue(path, gfile.IsDir(path)))
|
2018-11-16 18:20:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-31 12:07:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 18:20:09 +08:00
|
|
|
|
// 添加文件目录监控(递归),当目录下的文件有更新时,会同时更新缓存。
|
|
|
|
|
// 这里需要注意的点是,由于添加监听是递归添加的,那么假如删除一个目录,那么该目录下的文件(包括目录)也会产生一条删除事件,总共会产生N条事件。
|
|
|
|
|
func (sp *SPath) addMonitorByPath(path string) {
|
2018-05-31 12:07:31 +08:00
|
|
|
|
gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
2018-11-17 02:39:23 +08:00
|
|
|
|
//glog.Debug(event.String())
|
2018-11-16 18:20:09 +08:00
|
|
|
|
switch {
|
|
|
|
|
case event.IsRemove():
|
|
|
|
|
sp.cache.Remove(sp.nameFromPath(event.Path, path))
|
|
|
|
|
|
|
|
|
|
case event.IsRename():
|
|
|
|
|
if !gfile.Exists(event.Path) {
|
|
|
|
|
sp.cache.Remove(sp.nameFromPath(event.Path, path))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case event.IsCreate():
|
|
|
|
|
sp.addToCache(event.Path, path)
|
2018-05-31 12:07:31 +08:00
|
|
|
|
}
|
2018-11-17 02:39:23 +08:00
|
|
|
|
}, true)
|
2018-11-16 18:20:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除监听(递归)
|
|
|
|
|
func (sp *SPath) removeMonitorByPath(path string) {
|
|
|
|
|
gfsnotify.Remove(path)
|
2018-05-03 13:35:08 +08:00
|
|
|
|
}
|