/
file_monitor.go
106 lines (95 loc) · 2.6 KB
/
file_monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package dm_1
import (
"github.com/fsnotify/fsnotify"
"github.com/kpango/glg"
"os"
"path/filepath"
)
type (
DMNotifyFile struct {
Watch *fsnotify.Watcher
Create, Write, Remove, Rename, Chmod DMWatchEventFunc
}
DMWatchEventFunc func( event fsnotify.Event )
)
func DMNewNotifyFileEmpty() *DMNotifyFile {
w := new(DMNotifyFile)
w.Watch, _ = fsnotify.NewWatcher()
w.Create = func(event fsnotify.Event) {}
w.Remove = func(event fsnotify.Event) {}
w.Rename = func(event fsnotify.Event) {}
w.Write = func(event fsnotify.Event) {}
w.Chmod = func(event fsnotify.Event) {}
return w
}
// 递归监控目录 dir
func (pR *DMNotifyFile) AddWatchDirRecruit( dir string ) ( e error ) {
defer func() {
if e := recover(); e != nil {
glg.Error( e ); glg.Warn( "watch event may not work properly")
}
}()
e = filepath.Walk( dir, func( path string, info os.FileInfo, err error ) error {
if info.IsDir() {
if path, err = filepath.Abs( path ); err != nil { return err }
if err = pR.Watch.Add( path ); err != nil { return err }
Info( "ADD MONITOR: " + path )
}
return nil
})
return e
}
func ( pR *DMNotifyFile ) RemoveWatchDirRecruit( dir string ) ( e error ) {
e = filepath.Walk( dir, func( path string, info os.FileInfo, err error ) error {
if info.IsDir() {
if path, err = filepath.Abs( path ); err != nil { return err }
if err = pR.Watch.Remove( path ); err != nil { return err }
Info( "REMOVE MONITOR: " + path )
}
return nil
})
return e
}
func (pR *DMNotifyFile) WatchEvent() {
defer pR.Watch.Close()
for {
select {
case ev := <- pR.Watch.Events: {
if ev.Op&fsnotify.Create == fsnotify.Create {
Info("CREATE: "+ ev.Name)
file, err := os.Stat(ev.Name)
if err == nil { pR.Create( ev ) }
if err == nil && file.IsDir() { pR.AddWatchDirRecruit( ev.Name ) }
}
if ev.Op&fsnotify.Write == fsnotify.Write {
Info("WRITE: "+ ev.Name)
pR.Write( ev )
}
if ev.Op&fsnotify.Remove == fsnotify.Remove {
Info("DELETE: ", ev.Name)
fi, err := os.Stat(ev.Name)
if err == nil { pR.Remove( ev ) }
if err == nil && fi.IsDir() {
pR.RemoveWatchDirRecruit( ev.Name )
Info("REMOVE MONITOR: ", ev.Name)
}
}
if ev.Op&fsnotify.Rename == fsnotify.Rename {
Info("RENAME: ", ev.Name)
pR.Rename( ev )
// 名字更换后的文件夹会被认为是CREATE的
pR.Watch.Remove( ev.Name )
}
if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
{ pR.Chmod( ev ) }
Info("CHMOD: ", ev.Name)
}
}
case err := <- pR.Watch.Errors:
{
Fatal("ERROR IN WATCH EVENT: ", err)
return
}
}
}
}