-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfwatcher.go
214 lines (198 loc) · 7.69 KB
/
fwatcher.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/tywkeene/go-fsevents"
"golang.org/x/sys/unix"
)
var dirptr, commandptr, eventsptr *string
var rootDir string
var inotifyFlags int
var options *fsevents.WatcherOptions
var eventsArr []string
var types = map[string]int{
//基础flag
"IN_ACCESS": unix.IN_ACCESS, //文件被访问
"IN_ATTRIB": unix.IN_ATTRIB, //文件属性发生变化, e.g., permissions, timestamps, extended attributes, link count (since Linux 2.6.25), UID, GID, etc. (*).
"IN_CLOSE_NOWRITE": unix.IN_CLOSE_NOWRITE, //以非write方式打开文件并关闭
"IN_CLOSE_WRITE": unix.IN_CLOSE_WRITE, //以write方式打开文件并关闭
"IN_CREATE": unix.IN_CREATE, //文件或目录被创建
"IN_DELETE": unix.IN_DELETE, //文件或目录被删除
"IN_DELETE_SELF": unix.IN_DELETE_SELF, //监控的根目录或文件本身被删除
"IN_MODIFY": unix.IN_MODIFY, //文件内容被修改
"IN_MOVED_FROM": unix.IN_MOVED_FROM, //文件移出被监测的目录
"IN_MOVED_TO": unix.IN_MOVED_TO, //文件移入被监测的目录
"IN_MOVE_SELF": unix.IN_MOVE_SELF, //监测的根目录或文件本身移动
"IN_OPEN": unix.IN_OPEN, //文件被打开
//集合flag
"IN_ALL_EVENTS": unix.IN_ALL_EVENTS, // 以上所有flag的集合"
"IN_CLOSE": unix.IN_CLOSE, //IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
"IN_MOVE": unix.IN_MOVE, //IN_MOVED_FROM | IN_MOVED_TO
//不常用的flag
"IN_DONT_FOLLOW": unix.IN_DONT_FOLLOW, //不follow符号链接 (since 2.6.15)
"IN_EXCL_UNLINK": unix.IN_EXCL_UNLINK, //当文件从监测目中unlink后,则不再报告该文件的相关event,比如监控/tmp使用 (since 2.6.36)
"IN_MASK_ADD": unix.IN_MASK_ADD, //追加MASK到被监测的pathname
"IN_ONESHOT": unix.IN_ONESHOT, //只监测一次
"IN_ONLYDIR": unix.IN_ONLYDIR, //只监测目录
//仅由read返回
"IN_IGNORED": unix.IN_IGNORED, //inotify_rm_watch,文件被删除或者文件系统被umount
"IN_ISDIR": unix.IN_ISDIR, //发生事件的是一个目录
"IN_Q_OVERFLOW": unix.IN_Q_OVERFLOW, //Event队列溢出
"IN_UNMOUNT": unix.IN_UNMOUNT, //文件系统unmount
}
const eventsUsageText = `//基础flag
"IN_ACCESS": unix.IN_ACCESS, //文件被访问
"IN_ATTRIB": unix.IN_ATTRIB, //权限,时间戳,UID,GID,其他属性等等,link链接的数量 (since Linux 2.6.25)
"IN_CLOSE_NOWRITE": unix.IN_CLOSE_NOWRITE, //以非write方式打开文件并关闭
"IN_CLOSE_WRITE": unix.IN_CLOSE_WRITE, //以write方式打开文件并关闭
"IN_CREATE": unix.IN_CREATE, //文件或目录被创建
"IN_DELETE": unix.IN_DELETE, //文件或目录被删除
"IN_DELETE_SELF": unix.IN_DELETE_SELF, //监控的根目录或文件本身被删除
"IN_MODIFY": unix.IN_MODIFY, //文件内容被修改
"IN_MOVED_FROM": unix.IN_MOVED_FROM, //文件移出被监测的目录
"IN_MOVED_TO": unix.IN_MOVED_TO, //文件移入被监测的目录
"IN_MOVE_SELF": unix.IN_MOVE_SELF, //监测的根目录或文件本身移动
"IN_OPEN": unix.IN_OPEN, //文件被打开
//集合flag
"IN_ALL_EVENTS": unix.IN_ALL_EVENTS, // 以上所有flag的集合"
"IN_CLOSE": unix.IN_CLOSE, //IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
"IN_MOVE": unix.IN_MOVE, //IN_MOVED_FROM | IN_MOVED_TO
//不常用的flag
"IN_DONT_FOLLOW": unix.IN_DONT_FOLLOW, //不follow符号链接 (since 2.6.15)
"IN_EXCL_UNLINK": unix.IN_EXCL_UNLINK, //当文件从监测目中unlink后,则不再报告该文件的相关event,比如监控/tmp使用 (since 2.6.36)
"IN_MASK_ADD": unix.IN_MASK_ADD, //追加MASK到被监测的pathname
"IN_ONESHOT": unix.IN_ONESHOT, //只监测一次
"IN_ONLYDIR": unix.IN_ONLYDIR, //只监测目录
//仅由read返回
"IN_IGNORED": unix.IN_IGNORED, //inotify_rm_watch,文件被删除或者文件系统被umount
"IN_ISDIR": unix.IN_ISDIR, //发生事件的是一个目录
"IN_Q_OVERFLOW": unix.IN_Q_OVERFLOW, //Event队列溢出
"IN_UNMOUNT": unix.IN_UNMOUNT, //文件系统unmount`
var watcherMap = map[string]*fsevents.Watcher{}
func main() {
dirptr = flag.String("dir", "/tmp", "directory to watch")
eventsptr = flag.String("events", `IN_ALL_EVENTS,IN_ISDIR,IN_CLOSE,IN_MOVE,IN_EXCL_UNLINK`,
`设置想要监听的事件flag;
默认是:"IN_ALL_EVENTS,IN_ISDIR,IN_CLOSE,IN_MOVE,IN_EXCL_UNLINK"
全部可用的事件flag如下:
`+eventsUsageText)
commandptr = flag.String("cmd", "echo %f %t", "command to execute on change")
flag.Parse()
rootDir, _ = filepath.Abs(*dirptr)
eventsArr = strings.FieldsFunc(*eventsptr, func(c rune) bool { return c == ',' })
options = &fsevents.WatcherOptions{
Recursive: true,
UseWatcherFlags: true,
}
inotifyFlags := (unix.IN_ALL_EVENTS | unix.IN_ISDIR |
unix.IN_CLOSE | unix.IN_MOVE | unix.IN_EXCL_UNLINK)
w, err := fsevents.NewWatcher(rootDir, inotifyFlags, options)
if err != nil {
panic(err)
}
log.Printf("Watched Dir : %v", *dirptr)
log.Printf("Event Flags : %v", *eventsptr)
log.Printf("Event Command : %v", *commandptr)
log.Println("Waiting for events...")
handleEvents(w)
}
func writeOutput(buf *bytes.Buffer) {
if buf == nil {
return
}
if len(buf.Bytes()) > 0 {
fmt.Printf("%s\n", string(buf.Bytes()))
}
}
func handleEvents(watcher *fsevents.Watcher) {
watcher.StartAll()
go watcher.Watch()
for {
select {
case event := <-watcher.Events:
if event.RawEvent.Len == 0 {
continue
}
var eventTypesStr = getEventType(*event)
eventTypesArray := strings.FieldsFunc(eventTypesStr, func(c rune) bool { return c == ',' })
var inarray = true
hasInAllEvents, _ := inArray("IN_ALL_EVENTS", eventsArr)
for _, v := range eventTypesArray {
b, _ := inArray(v, eventsArr)
if !b && hasInAllEvents {
b = ((types[v] & int(types["IN_ALL_EVENTS"])) == types[v])
}
//log.Println(b, v, eventsArr)
inarray = inarray && b
}
if event.IsDirCreated() {
w, _ := fsevents.NewWatcher(event.Path, inotifyFlags, options)
watcherMap[event.Path] = w
log.Println("new dir watcher added : " + event.Path)
go handleEvents(w)
} else if event.IsDirRemoved() {
_, ok := watcherMap[event.Path]
if ok {
log.Println("new dir watcher removed : " + event.Path)
watcherMap[event.Path].StopAll()
delete(watcherMap, event.Path)
}
}
log.Printf("%s [%s]", event.Path, eventTypesStr)
if inarray {
commandStr := strings.Replace(*commandptr, "%f", event.Path, 1)
commandStr = strings.Replace(commandStr, "%t", eventTypesStr, 1)
log.Println("Exec:" + commandStr)
parts := strings.Fields(commandStr)
cmd := exec.Command(parts[0], parts[1:]...)
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
// write stdout to buffer
cmd.Stdout = stdout
cmd.Stderr = stderr
err := cmd.Run()
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("%s\n", err.Error()))
}
// write output
writeOutput(stdout)
writeOutput(stderr)
}
break
case err := <-watcher.Errors:
log.Println(err)
break
}
}
}
func getEventType(e fsevents.FsEvent) string {
var events []string
var mask = e.RawEvent.Mask
for key, value := range types {
if (value & int(mask)) == value {
events = append(events, key)
}
}
if len(events) == 0 {
events[0] = fmt.Sprintf("%d", e.RawEvent.Mask)
}
return strings.Join(events, ",")
}
func inArray(val string, array []string) (exists bool, index int) {
exists = false
index = -1
for i, v := range array {
if val == v {
index = i
exists = true
return
}
}
return
}