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.
|
|
|
|
|
|
|
|
|
|
// 搜索目录管理.
|
|
|
|
|
// 可以添加搜索目录,按照添加的优先级进行文件检索,并在内部进行高效缓存处理。
|
|
|
|
|
package gspath
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sync"
|
|
|
|
|
"errors"
|
|
|
|
|
"strings"
|
|
|
|
|
"gitee.com/johng/gf/g/os/gfile"
|
|
|
|
|
"gitee.com/johng/gf/g/container/gmap"
|
2018-05-31 12:07:31 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gfsnotify"
|
2018-09-25 17:25:38 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/glog"
|
2018-05-03 13:35:08 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 文件目录搜索管理对象
|
|
|
|
|
type SPath struct {
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
paths []string // 搜索路径,按照优先级进行排序
|
|
|
|
|
cache *gmap.StringStringMap // 搜索结果缓存map
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New () *SPath {
|
|
|
|
|
return &SPath{
|
|
|
|
|
paths : make([]string, 0),
|
|
|
|
|
cache : gmap.NewStringStringMap(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置搜索路径,只保留当前设置项,其他搜索路径被清空
|
|
|
|
|
func (sp *SPath) Set(path string) error {
|
|
|
|
|
r := gfile.RealPath(path)
|
2018-08-14 19:53:31 +08:00
|
|
|
|
if r == "" {
|
|
|
|
|
r = sp.Search(path)
|
|
|
|
|
if r == "" {
|
|
|
|
|
r = gfile.RealPath(gfile.Pwd() + gfile.Separator + path)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-03 13:35:08 +08:00
|
|
|
|
if r != "" && gfile.IsDir(r) {
|
2018-09-25 17:25:38 +08:00
|
|
|
|
r = strings.TrimRight(r, gfile.Separator)
|
2018-05-03 13:35:08 +08:00
|
|
|
|
sp.mu.Lock()
|
2018-09-25 17:25:38 +08:00
|
|
|
|
sp.paths = []string{r}
|
2018-05-03 13:35:08 +08:00
|
|
|
|
sp.mu.Unlock()
|
|
|
|
|
sp.cache.Clear()
|
2018-10-08 13:38:36 +08:00
|
|
|
|
glog.Debug("gspath.SetPath:", r)
|
2018-05-03 13:35:08 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-10-18 13:43:00 +08:00
|
|
|
|
glog.Warning("gspath.SetPath failed:", path)
|
2018-05-03 13:35:08 +08:00
|
|
|
|
return errors.New("invalid path:" + path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加搜索路径
|
|
|
|
|
func (sp *SPath) Add(path string) error {
|
|
|
|
|
r := gfile.RealPath(path)
|
2018-08-14 19:53:31 +08:00
|
|
|
|
if r == "" {
|
|
|
|
|
r = sp.Search(path)
|
|
|
|
|
if r == "" {
|
|
|
|
|
r = gfile.RealPath(gfile.Pwd() + gfile.Separator + path)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-03 13:35:08 +08:00
|
|
|
|
if r != "" && gfile.IsDir(r) {
|
2018-09-25 17:25:38 +08:00
|
|
|
|
r = strings.TrimRight(r, gfile.Separator)
|
2018-05-03 13:35:08 +08:00
|
|
|
|
sp.mu.Lock()
|
|
|
|
|
sp.paths = append(sp.paths, r)
|
|
|
|
|
sp.mu.Unlock()
|
2018-10-08 13:38:36 +08:00
|
|
|
|
glog.Debug("gspath.Add:", r)
|
2018-05-03 13:35:08 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-10-18 13:43:00 +08:00
|
|
|
|
glog.Warning("gspath.Add failed:", path)
|
2018-05-03 13:35:08 +08:00
|
|
|
|
return errors.New("invalid path:" + path)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 20:29:28 +08:00
|
|
|
|
// 按照优先级搜索文件,返回搜索到的文件绝对路径,找不到该文件时,返回空字符串
|
2018-08-26 21:36:39 +08:00
|
|
|
|
// 给定的name只是相对文件路径,或者只是一个文件名
|
2018-05-03 13:35:08 +08:00
|
|
|
|
func (sp *SPath) Search(name string) string {
|
|
|
|
|
path := sp.cache.Get(name)
|
|
|
|
|
if path == "" {
|
2018-08-26 21:36:39 +08:00
|
|
|
|
sp.mu.RLock()
|
|
|
|
|
for _, v := range sp.paths {
|
|
|
|
|
path = gfile.RealPath(v + gfile.Separator + name)
|
|
|
|
|
if path != "" && gfile.Exists(path) {
|
|
|
|
|
break
|
2018-05-03 13:35:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-26 21:36:39 +08:00
|
|
|
|
sp.mu.RUnlock()
|
2018-05-03 13:35:08 +08:00
|
|
|
|
if path != "" {
|
|
|
|
|
sp.cache.Set(name, path)
|
2018-05-31 12:07:31 +08:00
|
|
|
|
sp.addMonitor(name, path)
|
2018-05-03 13:35:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return path
|
2018-05-31 12:07:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 当前的搜索路径数量
|
|
|
|
|
func (sp *SPath) Size() int {
|
|
|
|
|
sp.mu.RLock()
|
|
|
|
|
length := len(sp.paths)
|
|
|
|
|
sp.mu.RUnlock()
|
|
|
|
|
return length
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加文件监控,当文件删除时,同时也删除搜索结果缓存
|
|
|
|
|
func (sp *SPath) addMonitor(name, path string) {
|
2018-10-11 09:56:01 +08:00
|
|
|
|
glog.Debug("gspath.addMonitor:", name, path)
|
2018-05-31 12:07:31 +08:00
|
|
|
|
gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
2018-09-25 17:25:38 +08:00
|
|
|
|
glog.Debug("gspath.monitor:", event)
|
2018-05-31 12:07:31 +08:00
|
|
|
|
if event.IsRemove() {
|
|
|
|
|
sp.cache.Remove(name)
|
|
|
|
|
}
|
2018-09-28 22:39:15 +08:00
|
|
|
|
}, false)
|
2018-05-03 13:35:08 +08:00
|
|
|
|
}
|