-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.go
194 lines (180 loc) · 3.52 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
package main
import (
"log"
"os"
"path/filepath"
"sort"
"sync"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
)
type Watcher interface {
Add(id, path string) error
Remove(path string) error
SetOnChange(func(ids []string))
Close()
}
func NewFSWatcher(w *fsnotify.Watcher, wait time.Duration, debug bool) Watcher {
f := &fsWatcher{
w: w,
wait: wait,
mm: new(sync.RWMutex),
debug: debug,
exitC: make(chan struct{}),
doneC: make(chan struct{}),
}
go f.mon()
return f
}
type fsWatcher struct {
w *fsnotify.Watcher
wait time.Duration
mm *sync.RWMutex
m map[string]map[string]struct{}
debug bool
exitC chan struct{}
doneC chan struct{}
onChange func(ids []string)
}
func (f *fsWatcher) SetOnChange(fn func(ids []string)) {
f.onChange = fn
}
func (f *fsWatcher) Add(id, path string) error {
info, err := os.Stat(path)
if err != nil {
return err
} else if !info.IsDir() {
switch filepath.Ext(path) {
case ".go", ".c", ".h":
default:
return nil
}
path = filepath.Dir(path)
}
f.mm.Lock()
if f.m == nil {
f.m = make(map[string]map[string]struct{})
}
if f.m[path] == nil {
f.m[path] = make(map[string]struct{})
if err := f.w.Add(path); err != nil {
f.mm.Unlock()
return err
}
}
f.m[path][id] = struct{}{}
f.mm.Unlock()
return nil
}
func (f *fsWatcher) mon() {
bufMux := new(sync.Mutex)
buf := make(map[string]time.Time)
notify := func(path string) {
bufMux.Lock()
buf[path] = time.Now()
bufMux.Unlock()
}
go func() {
for {
select {
case <-f.exitC:
return
default:
bufMux.Lock()
queue := make([]string, 0, len(buf))
for path, ts := range buf {
if time.Since(ts) > f.wait {
queue = append(queue, path)
delete(buf, path)
}
}
bufMux.Unlock()
go func() {
for _, path := range queue {
f.mm.RLock()
ids, ok := f.m[path]
f.mm.RUnlock()
if ok && f.onChange != nil {
f.onChange(idList(ids))
} else if ids, ok := f.findParentIDs(path); ok {
f.onChange(idList(ids))
}
}
}()
time.Sleep(200 * time.Millisecond)
}
}
}()
for {
select {
case <-f.exitC:
close(f.doneC)
return
case ev, ok := <-f.w.Events:
if !ok {
close(f.doneC)
return
}
if f.debug {
log.Println(ev.String())
}
switch {
case opIs(ev.Op, fsnotify.Remove):
notify(ev.Name)
case opIs(ev.Op, fsnotify.Write):
notify(ev.Name)
case opIs(ev.Op, fsnotify.Create):
if ids, ok := f.findParentIDs(ev.Name); ok {
for id := range ids {
if err := f.Add(id, ev.Name); err != nil {
log.Println("watcher:", err)
}
}
} else {
log.Println("watcher: no parent for", ev.Name)
}
notify(ev.Name)
}
case err := <-f.w.Errors:
if v, ok := err.(*os.SyscallError); ok {
if v.Err == syscall.EINTR {
continue
}
}
log.Println("watcher:", err)
}
}
}
func (f *fsWatcher) Remove(path string) error {
f.mm.Lock()
delete(f.m, path)
f.mm.Unlock()
return f.w.Remove(path)
}
func (f *fsWatcher) Close() {
close(f.exitC)
<-f.doneC
}
func (f *fsWatcher) findParentIDs(path string) (map[string]struct{}, bool) {
f.mm.RLock()
defer f.mm.RUnlock()
parent := filepath.Dir(path)
for path, ids := range f.m {
if path == parent {
return ids, true
}
}
return nil, false
}
func opIs(op, mask fsnotify.Op) bool {
return op&mask == mask
}
func idList(set map[string]struct{}) []string {
ids := make([]string, 0, len(set))
for id := range set {
ids = append(ids, id)
}
sort.Strings(ids)
return ids
}