From 882c69cb08ea1c355aa4e5f15b0c9bcfd3c74387 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 11 Nov 2018 11:26:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bgfsnotify=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=BC=96=E8=BE=91=E5=99=A8=E5=AF=B9=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E9=9D=9E=E6=A0=87=E5=87=86=E7=BC=96=E8=BE=91=E6=97=B6(RENAME+C?= =?UTF-8?q?HMOD)=E7=9A=84=E7=83=AD=E6=9B=B4=E6=96=B0=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_watcher.go | 36 ++++++++++++------- geg/net/ghttp/server/template/tpl1/index.html | 2 +- geg/os/gfsnotify/gfsnotify_limit.go | 19 ++++++++++ 3 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 geg/os/gfsnotify/gfsnotify_limit.go diff --git a/g/os/gfsnotify/gfsnotify_watcher.go b/g/os/gfsnotify/gfsnotify_watcher.go index ee6a32fcc..44bbfc9dc 100644 --- a/g/os/gfsnotify/gfsnotify_watcher.go +++ b/g/os/gfsnotify/gfsnotify_watcher.go @@ -207,19 +207,31 @@ func (w *Watcher) startEventLoop() { for { if v := w.events.Pop(); v != nil { event := v.(*Event) - // 如果是删除操作,那么需要判断是否文件真正不存在了 - if event.IsRemove() { - if fileExists(event.Path) { - // 如果是文件删除事件,判断该文件是否存在,如果存在,那么将此事件认为“假删除”, - // 并重新添加监控(底层fsnotify会自动删除掉监控,这里重新添加回去) - w.watcher.Add(event.Path) - // 修改事件操作为重命名(相当于重命名为自身名称,最终名称没变) - event.Op = RENAME - } else { - // 如果是真实删除,那么递归删除监控信息 - w.Remove(event.Path) - } + fmt.Println(event) + fmt.Println(fileExists(event.Path)) + switch { + // 如果是删除操作,那么需要判断是否文件真正不存在了,如果存在,那么将此事件认为“假删除” + case event.IsRemove(): + if fileExists(event.Path) { + // 重新添加监控(底层fsnotify会自动删除掉监控,这里重新添加回去) + // 注意这里调用的是底层fsnotify添加监控,只会产生回调事件,并不会使回调函数重复注册 + w.watcher.Add(event.Path) + // 修改事件操作为重命名(相当于重命名为自身名称,最终名称没变) + event.Op = RENAME + } else { + // 如果是真实删除,那么递归删除监控信息 + w.Remove(event.Path) + } + + // 如果是删除操作,那么需要判断是否文件真正不存在了,如果存在,那么将此事件认为“假命名” + // (特别是某些编辑器在编辑文件时会先对文件RENAME再CHMOD) + case event.IsRename(): + if fileExists(event.Path) { + // 重新添加监控 + w.watcher.Add(event.Path) + } } + callbacks := w.getCallbacks(event.Path) // 如果创建了新的目录,那么将这个目录递归添加到监控中 if event.IsCreate() && fileIsDir(event.Path) { diff --git a/geg/net/ghttp/server/template/tpl1/index.html b/geg/net/ghttp/server/template/tpl1/index.html index 8a50061ce..ec5f48cfc 100644 --- a/geg/net/ghttp/server/template/tpl1/index.html +++ b/geg/net/ghttp/server/template/tpl1/index.html @@ -6,6 +6,6 @@

姓名 : {{.name}}

-12311 +12345678910 \ No newline at end of file diff --git a/geg/os/gfsnotify/gfsnotify_limit.go b/geg/os/gfsnotify/gfsnotify_limit.go new file mode 100644 index 000000000..f42a29824 --- /dev/null +++ b/geg/os/gfsnotify/gfsnotify_limit.go @@ -0,0 +1,19 @@ +package main + +import ( + "gitee.com/johng/gf/g/os/gfsnotify" + "gitee.com/johng/gf/g/os/glog" +) + +// 对同一个文件多次Add是否超过系统inotify限制 +func main() { + path := "/Users/john/temp/log" + for i := 0; i < 9999999; i++ { + _, err := gfsnotify.Add(path, func(event *gfsnotify.Event) { + glog.Println(event) + }) + if err != nil { + glog.Fatalln(err) + } + } +} \ No newline at end of file