From 923baec62ea2e984c958126c3abcc4f4166511a8 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 28 Sep 2018 22:39:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dgfsnotify=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E9=80=92=E5=BD=92=E7=9B=91=E6=8E=A7=E6=95=B4=E4=B8=AAgspath.Ad?= =?UTF-8?q?d=E6=B7=BB=E5=8A=A0=E7=9A=84=E7=9B=AE=E5=BD=95=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gfsnotify/gfsnotify.go | 13 +++++++------ g/os/gspath/gspath.go | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/g/os/gfsnotify/gfsnotify.go b/g/os/gfsnotify/gfsnotify.go index 81c6e743f..91fbe48ab 100644 --- a/g/os/gfsnotify/gfsnotify.go +++ b/g/os/gfsnotify/gfsnotify.go @@ -16,6 +16,7 @@ import ( "gitee.com/johng/gf/g/container/gmap" "gitee.com/johng/gf/g/container/glist" "gitee.com/johng/gf/g/container/gqueue" + "fmt" ) // 监听管理对象 @@ -64,11 +65,11 @@ func New() (*Watcher, error) { } // 添加对指定文件/目录的监听,并给定回调函数;如果给定的是一个目录,默认递归监控。 -func Add(path string, callback func(event *Event)) error { +func Add(path string, callback func(event *Event), recursive...bool) error { if watcher == nil { return errors.New("global watcher creating failed") } - return watcher.Add(path, callback) + return watcher.Add(path, callback, recursive...) } // 移除监听,默认递归删除。 @@ -91,7 +92,7 @@ func (w *Watcher) addWatch(path string, callback func(event *Event)) error { // 这里统一转换为当前系统的绝对路径,便于统一监控文件名称 t := gfile.RealPath(path) if t == "" { - return errors.New(path + " does not exist") + return errors.New(fmt.Sprintf(`"%s" does not exist`, path)) } path = t // 注册回调函数 @@ -110,9 +111,9 @@ func (w *Watcher) addWatch(path string, callback func(event *Event)) error { return nil } -// 递归添加监控 -func (w *Watcher) Add(path string, callback func(event *Event)) error { - if gfile.IsDir(path) { +// 添加监控,path参数支持文件或者目录路径,recursive为非必需参数,默认为递归添加监控(当path为目录时) +func (w *Watcher) Add(path string, callback func(event *Event), recursive...bool) error { + if gfile.IsDir(path) && (len(recursive) == 0 || recursive[0]) { paths, _ := gfile.ScanDir(path, "*", true) list := []string{path} list = append(list, paths...) diff --git a/g/os/gspath/gspath.go b/g/os/gspath/gspath.go index 67939130d..b2a77ecf9 100644 --- a/g/os/gspath/gspath.go +++ b/g/os/gspath/gspath.go @@ -111,5 +111,5 @@ func (sp *SPath) addMonitor(name, path string) { if event.IsRemove() { sp.cache.Remove(name) } - }) + }, false) } \ No newline at end of file