-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
369 lines (320 loc) · 7.46 KB
/
watcher.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package fsw
import (
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/askasoft/pango/fsu"
"github.com/askasoft/pango/log"
"github.com/fsnotify/fsnotify"
)
// Op describes a set of file operations.
type Op = fsnotify.Op
const (
// OpNone none operation
OpNone = Op(0)
// OpCreate create operation
OpCreate = Op(fsnotify.Create)
// OpWrite write operation
OpWrite = Op(fsnotify.Write)
// OpRemove remove operation
OpRemove = Op(fsnotify.Remove)
// OpRename rename operation
OpRename = Op(fsnotify.Rename)
// OpChmod chmod operation
OpChmod = Op(fsnotify.Chmod)
// OpModifies modifies operations (OpCreate | OpWrite | OpRemove | OpRename)
OpModifies = OpCreate | OpWrite | OpRemove | OpRename
// OpALL all operations
OpALL = Op(0xFFFFFFFF)
)
type fileitem struct {
OpMask Op
Recursive bool
Callback func(string, Op)
}
type fileevent struct {
Name string
Op Op
Time time.Time
Callbacks []func(string, Op)
}
// FileWatcher struct for file watching
type FileWatcher struct {
Delay time.Duration
Logger log.Logger // Error logger
fsnotify *fsnotify.Watcher
items map[string]*fileitem
events map[string]*fileevent
mutex sync.RWMutex
timer *time.Timer
}
// NewFileWatcher create a FileWatcher
func NewFileWatcher() *FileWatcher {
fw := &FileWatcher{
Delay: time.Second,
items: make(map[string]*fileitem),
events: make(map[string]*fileevent),
}
return fw
}
// Start start file watching go-routine
func (fw *FileWatcher) Start() (err error) {
if fw.fsnotify != nil {
return nil
}
log := fw.Logger
if log != nil {
log.Info("fswatch: start")
}
fsn, err := fsnotify.NewWatcher()
if err != nil {
return err
}
fw.fsnotify = fsn
for path, fi := range fw.items {
if fi.Recursive {
if err = fw.doRecursive(path, true); err != nil {
return err
}
} else {
if log != nil {
log.Debugf("fswatch: watch file '%s'", path)
}
err = fsn.Add(path)
if err != nil {
return err
}
}
}
// some editor use create->rename to save file,
// this could raise 2 OpWrite event continuously,
// delay some time to prevent duplicated event.
if fw.Delay.Milliseconds() > 0 {
fw.timer = time.AfterFunc(time.Hour, func() {
fw.delayCallbacks()
})
}
go fw.watch(fsn)
return nil
}
// Stop stop file watching go-routine
func (fw *FileWatcher) Stop() (err error) {
fsn := fw.fsnotify
if fsn == nil {
return
}
timer, log := fw.timer, fw.Logger
if log != nil {
log.Info("fswatch: stop")
}
fw.timer = nil
fw.fsnotify = nil
if timer != nil {
timer.Stop()
}
err = fsn.Close()
return
}
// Add add a file to watch on specified operation op occurred
func (fw *FileWatcher) Add(path string, op Op, callback func(string, Op)) error {
path = filepath.Clean(path)
fsn, log := fw.fsnotify, fw.Logger
if fsn != nil {
if log != nil {
log.Debugf("fswatch: watch file '%s' ", path)
}
err := fsn.Add(path)
if err != nil {
return err
}
}
fw.items[path] = &fileitem{OpMask: op, Callback: callback}
return nil
}
// Remove stop watching the file
func (fw *FileWatcher) Remove(path string) error {
path = filepath.Clean(path)
fsn, log := fw.fsnotify, fw.Logger
if fsn != nil {
if log != nil {
log.Debugf("fswatch: unwatch file '%s'", path)
}
err := fsn.Remove(path)
if err != nil {
return err
}
}
delete(fw.items, path)
return nil
}
// AddRecursive add files and all sub-directories under the path to watch
func (fw *FileWatcher) AddRecursive(path string, op Op, cb func(string, Op)) error {
path = filepath.Clean(path)
if err := fw.doRecursive(path, true); err != nil {
return err
}
fw.items[path] = &fileitem{OpMask: op, Recursive: true, Callback: cb}
return nil
}
// RemoveRecursive stops watching the directory and all sub-directories.
func (fw *FileWatcher) RemoveRecursive(path string) error {
path = filepath.Clean(path)
if err := fw.doRecursive(path, false); err != nil {
return err
}
delete(fw.items, path)
return nil
}
// doRecursive adds all directories under the given one to the watch list.
// this is probably a very racey process. What if a file is added to a folder before we get the watch added?
func (fw *FileWatcher) doRecursive(root string, watch bool) error {
fsn, log := fw.fsnotify, fw.Logger
if fsn == nil {
return nil
}
err := filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
if watch {
if log != nil {
log.Debugf("fswatch: watch dir '%s'", path)
}
if err = fsn.Add(path); err != nil {
return err
}
} else {
if log != nil {
log.Debugf("fswatch: unwatch dir '%s'", path)
}
if err = fsn.Remove(path); err != nil {
return err
}
}
}
return nil
})
return err
}
func (fw *FileWatcher) findCallbacks(fe *fileevent) (cbs []func(string, Op)) {
for k, i := range fw.items {
if fe.Op&i.OpMask != 0 && strings.HasPrefix(fe.Name, k) {
cbs = append(cbs, i.Callback)
}
}
return
}
func (fw *FileWatcher) isRecursive(name string) bool {
for k, i := range fw.items {
if strings.HasPrefix(name, k) {
if i.Recursive {
return true
}
}
}
return false
}
func (fw *FileWatcher) watch(fsn *fsnotify.Watcher) {
for {
select {
case evt, ok := <-fsn.Events:
if !ok {
return
}
if fsn == fw.fsnotify {
fw.doEvent(&evt)
}
case err, ok := <-fsn.Errors:
if !ok {
return
}
log := fw.Logger
if log != nil {
log.Errorf("fswatch: watch error: %v", err)
}
}
}
}
func (fw *FileWatcher) doEvent(evt *fsnotify.Event) {
fw.procEvent(evt)
fw.sendEvent(evt)
}
// procEvent add or remove watch file
func (fw *FileWatcher) procEvent(evt *fsnotify.Event) {
if evt.Op&OpCreate != 0 {
if err := fsu.DirExists(evt.Name); err == nil {
if fw.isRecursive(evt.Name) {
if err = fw.doRecursive(evt.Name, true); err != nil {
if log := fw.Logger; log != nil {
log.Errorf("fswatch: watch dir '%s' error: %v", evt.Name, err)
}
}
}
}
}
}
func (fw *FileWatcher) sendEvent(event *fsnotify.Event) {
timer := fw.timer
if timer == nil {
fe := &fileevent{Name: event.Name, Op: Op(event.Op), Time: time.Now()}
fe.Callbacks = fw.findCallbacks(fe)
if len(fe.Callbacks) > 0 {
fw.execCallbacks(fe)
}
return
}
fw.mutex.Lock()
defer fw.mutex.Unlock()
log := fw.Logger
fe := fw.events[event.Name]
if fe != nil {
fe.Time = time.Now()
fe.Op |= Op(event.Op)
if log != nil {
log.Debugf("fswatch: delay '%s' [%v] (%s) ", fe.Name, fe.Op, fe.Time)
}
return
}
fe = &fileevent{Name: event.Name, Op: Op(event.Op), Time: time.Now()}
fe.Callbacks = fw.findCallbacks(fe)
if len(fe.Callbacks) > 0 {
if log != nil {
log.Debugf("fswatch: queue '%s' [%v] (%s) ", fe.Name, fe.Op, fe.Time)
}
fw.events[event.Name] = fe
if len(fw.events) == 1 {
if log != nil {
log.Debugf("fswatch: reset timer (%s) ", fw.Delay)
}
timer.Reset(fw.Delay)
}
}
}
func (fw *FileWatcher) delayCallbacks() {
fw.mutex.Lock()
defer fw.mutex.Unlock()
due := time.Now().Add(-fw.Delay)
for _, fe := range fw.events {
if fe.Time.Before(due) {
fw.execCallbacks(fe)
delete(fw.events, fe.Name)
}
}
if len(fw.events) > 0 {
if log := fw.Logger; log != nil {
log.Debugf("fswatch: reset timer (%s) ", fw.Delay)
}
fw.timer.Reset(fw.Delay)
}
}
func (fw *FileWatcher) execCallbacks(fe *fileevent) {
if log := fw.Logger; log != nil {
log.Debugf("fswatch: execute callback '%s' [%v]", fe.Name, fe.Op)
}
for _, cb := range fe.Callbacks {
cb(fe.Name, Op(fe.Op))
}
}