-
Notifications
You must be signed in to change notification settings - Fork 90
/
watcher.go
316 lines (297 loc) · 7.89 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
// Copyright 2014 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package watch
import (
"errors"
"os"
"path/filepath"
"sync"
"github.com/limetext/backend/log"
"github.com/limetext/util"
"github.com/rjeczalik/notify"
)
type (
// Wrapper around fsnotify watcher to suit lime needs
// - Watching directories, we will have less individual watchers
// - Have multiple subscribers on single file or directory
// - Watching a path which doesn't exist yet
// - Watching and applying action on certain events
Watcher struct {
sync.Mutex
fsEvent chan notify.EventInfo
watched map[string][]interface{}
watchers []string // paths we created watcher on
dirs []string // dirs we are watching
}
// Called on file change directories won't recieve this callback
FileChangedCallback interface {
FileChanged(string)
}
// Called on a file or directory is created
FileCreatedCallback interface {
FileCreated(string)
}
// Called on a file or directory is removed
FileRemovedCallback interface {
FileRemoved(string)
}
// Called when a directory or file is renamed
// TODO: fsnotify behavior after rename is obscure
// if we have foo dir with a bar file inside and we are watching foo dir,
// on renaming foo to boo we will get rename event for foo dir but if we
// delete boo/bar we will get remove event for foo/bar not boo/bar
FileRenamedCallback interface {
FileRenamed(string)
}
)
func NewWatcher() (*Watcher, error) {
w := &Watcher{fsEvent: make(chan notify.EventInfo, 5)}
w.watched = make(map[string][]interface{})
w.watchers = make([]string, 0)
w.dirs = make([]string, 0)
go w.observe()
return w, nil
}
func (w *Watcher) Close() {
notify.Stop(w.fsEvent)
}
func (w *Watcher) Watch(name string, cb interface{}) error {
if !filepath.IsAbs(name) {
var err error
name, err = filepath.Abs(name)
if err != nil {
return err
}
}
log.Fine("Watch(%s)", name)
fi, err := os.Stat(name)
isDir := err == nil && fi.IsDir()
// If the file doesn't exist currently we will add watcher for file
// directory and look for create event inside the directory
if os.IsNotExist(err) {
log.Fine("%s doesn't exist, Watching parent directory", name)
if err := w.Watch(filepath.Dir(name), nil); err != nil {
return err
}
}
w.Lock()
defer w.Unlock()
if err := w.add(name, cb); err != nil {
if !isDir {
return err
}
if util.Exists(w.dirs, name) {
log.Fine("%s is watched already", name)
return nil
}
}
// If exists in watchers we are already watching the path
// Or
// If the file is under one of watched dirs
//
// no need to create watcher
if util.Exists(w.watchers, name) || (!isDir && util.Exists(w.dirs, filepath.Dir(name))) {
return nil
}
if err := w.watch(name, isDir); err != nil {
return err
}
if isDir {
w.flushDir(name)
}
return nil
}
func (w *Watcher) add(name string, cb interface{}) error {
log.Fine("Adding %s callback", name)
numok := 0
if _, ok := cb.(FileChangedCallback); ok {
numok++
}
if _, ok := cb.(FileCreatedCallback); ok {
numok++
}
if _, ok := cb.(FileRemovedCallback); ok {
numok++
}
if _, ok := cb.(FileRenamedCallback); ok {
numok++
}
if numok == 0 {
return errors.New("The callback argument does satisfy any File*Callback interfaces")
}
w.watched[name] = append(w.watched[name], cb)
return nil
}
func (w *Watcher) watch(name string, isDir bool) error {
watchPath := name
if isDir {
watchPath = filepath.Join(watchPath, "...")
}
log.Fine("Creating watcher on %s", name)
if err := notify.Watch(watchPath, w.fsEvent, notify.All); err != nil {
return err
}
w.watchers = append(w.watchers, name)
return nil
}
// Remove watchers created on files under this directory because
// one watcher on the parent directory is enough for all of them
func (w *Watcher) flushDir(name string) {
log.Finest("Flusing watched directory %s", name)
w.dirs = append(w.dirs, name)
for _, p := range w.watchers {
if filepath.Dir(p) == name && !util.Exists(w.dirs, p) {
if err := w.removeWatch(p); err != nil {
log.Error("Couldn't unwatch file %s: %s", p, err)
}
}
}
}
func (w *Watcher) UnWatch(name string, cb interface{}) error {
if !filepath.IsAbs(name) {
var err error
name, err = filepath.Abs(name)
if err != nil {
return err
}
}
log.Fine("UnWatch(%s)", name)
w.Lock()
defer w.Unlock()
if cb == nil {
return w.unWatch(name)
}
for i, c := range w.watched[name] {
if c == cb {
w.watched[name][i] = w.watched[name][len(w.watched[name])-1]
w.watched[name][len(w.watched[name])-1] = nil
w.watched[name] = w.watched[name][:len(w.watched[name])-1]
break
}
}
if len(w.watched[name]) == 0 {
w.unWatch(name)
}
return nil
}
func (w *Watcher) unWatch(name string) error {
delete(w.watched, name)
if err := w.removeWatch(name); err != nil {
return err
}
return nil
}
func (w *Watcher) removeWatch(name string) error {
log.Finest("removing watcher from %s", name)
// TODO
// notify.Stop(w.fsEvent) would stop ALL watchers, and the only way for
// unwatching is notify.Stop(). So we won't unwatch from the notify for
// now, just we will remove it from Watcher struct untill there is a
// better solution
w.watchers = util.Remove(w.watchers, name)
if util.Exists(w.dirs, name) {
w.removeDir(name)
}
return nil
}
// Put back watchers on watching files under the directory
func (w *Watcher) removeDir(name string) {
for p, _ := range w.watched {
if filepath.Dir(p) == name {
stat, err := os.Stat(p)
if err != nil {
log.Error("Stat error: %s", err)
}
if err := w.watch(p, stat.IsDir()); err != nil {
log.Error("Could not watch: %s", err)
continue
}
}
}
w.dirs = util.Remove(w.dirs, name)
}
// Observe dispatches notifications received by the watcher. This function will
// return when the watcher is closed.
func (w *Watcher) observe() {
for {
select {
case ev, ok := <-w.fsEvent:
if !ok {
// We get here only when w.fsEvent is stopped when closing the watcher
w.watched = nil
w.watchers = nil
w.dirs = nil
close(w.fsEvent)
w.fsEvent = nil
return
}
w.parseEv(ev)
}
}
}
func (w *Watcher) parseEv(ev notify.EventInfo) {
log.Finest("watcher event %s", ev)
w.Lock()
defer w.Unlock()
path := ev.Path()
w.apply(path, ev.Event())
// currently fsnotify pushs remove event for files
// inside directory when a directory is removed but
// when the directory is renamed there is no event for
// files inside directory
if ev.Event()¬ify.Rename != 0 && util.Exists(w.dirs, path) {
for p, _ := range w.watched {
if filepath.Dir(p) == path {
w.apply(p, ev.Event())
}
}
}
dir := filepath.Dir(path)
// The watcher will be removed if the file is deleted
// so we need to watch the parent directory for when the
// file is created again
if ev.Event()¬ify.Remove != 0 {
w.watchers = util.Remove(w.watchers, path)
w.Unlock()
w.Watch(dir, nil)
w.Lock()
}
// If the event is create we will apply FileCreated callback
// for the parent directory to because when new file is created
// inside directory we won't get any event for the watched directory.
// we need this feature to detect new packages(plugins, settings, etc)
if cbs, exist := w.watched[dir]; ev.Event()¬ify.Create != 0 && exist {
for _, cb := range cbs {
if c, ok := cb.(FileCreatedCallback); ok {
w.Unlock()
c.FileCreated(path)
w.Lock()
}
}
}
}
func (w *Watcher) apply(path string, flags notify.Event) {
for _, cb := range w.watched[path] {
if flags¬ify.Create != 0 {
if c, ok := cb.(FileCreatedCallback); ok {
c.FileCreated(path)
}
}
if flags¬ify.Write != 0 {
if c, ok := cb.(FileChangedCallback); ok {
c.FileChanged(path)
}
}
if flags¬ify.Remove != 0 {
if c, ok := cb.(FileRemovedCallback); ok {
c.FileRemoved(path)
}
}
if flags¬ify.Rename != 0 {
if c, ok := cb.(FileRenamedCallback); ok {
c.FileRenamed(path)
}
}
}
}