-
Notifications
You must be signed in to change notification settings - Fork 18
/
fsnwatcher.go
95 lines (79 loc) · 1.41 KB
/
fsnwatcher.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
package fswatcher
import (
fsnotify "github.com/fsnotify/fsnotify"
)
type FsnWatcher struct {
w *fsnotify.Watcher
events chan any
opMask Op
}
func NewFsnWatcher() (*FsnWatcher, error) {
w0, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
w := &FsnWatcher{
w: w0,
events: make(chan any),
}
w.opMask = AllOps
go w.eventLoop()
return w, nil
}
//----------
func (w *FsnWatcher) Close() error {
return w.w.Close()
}
func (w *FsnWatcher) OpMask() *Op {
return &w.opMask
}
//----------
func (w *FsnWatcher) Add(name string) error {
return w.w.Add(name)
}
func (w *FsnWatcher) Remove(name string) error {
return w.w.Remove(name)
}
//----------
func (w *FsnWatcher) Events() <-chan any {
return w.events
}
//----------
func (w *FsnWatcher) eventLoop() {
for {
select {
case err, ok := <-w.w.Errors:
if !ok {
return
}
w.events <- err
case ev, ok := <-w.w.Events:
if !ok {
return
}
name := ev.Name
var op Op
if ev.Op&fsnotify.Create > 0 {
op.Add(Create)
}
if ev.Op&fsnotify.Write > 0 {
op.Add(Modify)
}
if ev.Op&fsnotify.Remove > 0 {
op.Add(Remove)
}
if ev.Op&fsnotify.Rename > 0 {
op.Add(Rename)
}
if ev.Op&fsnotify.Chmod > 0 {
op.Add(Attrib)
}
op2 := op & w.opMask
if op2 > 0 {
w.events <- &Event{Op: op, Name: name}
} else {
//log.Printf("not sending event: %v", ev)
}
}
}
}