-
Notifications
You must be signed in to change notification settings - Fork 7
/
fsnotify.go
78 lines (67 loc) · 1.42 KB
/
fsnotify.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
// This is free and unencumbered software released into the public
// domain. For more information, see <http://unlicense.org> or the
// accompanying UNLICENSE file.
// +build !darwin,!windows
package fsw
import (
"io"
"log"
"sync"
"github.com/fsnotify/fsnotify"
)
type watcher struct {
*fsnotify.Watcher
tracking map[string]struct{}
mu sync.Mutex
}
func New() (Watcher, error) {
fsw, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
return &watcher{Watcher: fsw, tracking: make(map[string]struct{})}, nil
}
func (w *watcher) Add(path string) error {
if err := w.Watcher.Add(path); err != nil {
return err
}
w.mu.Lock()
defer w.mu.Unlock()
w.tracking[path] = struct{}{}
return nil
}
func (w *watcher) Remove(path string) error {
if err := w.Watcher.Remove(path); err != nil {
return err
}
w.mu.Lock()
defer w.mu.Unlock()
delete(w.tracking, path)
return nil
}
func (w *watcher) Next() (Event, error) {
select {
case e, ok := <-w.Events:
if !ok {
return Event{}, io.EOF
}
return Event{Path: e.Name, Op: Op(e.Op)}, nil
case err, ok := <-w.Errors:
if !ok {
return Event{}, io.EOF
}
return Event{}, err
}
}
func (w *watcher) RemoveAll() error {
w.mu.Lock()
defer w.mu.Unlock()
for p := range w.tracking {
if err := w.Watcher.Remove(p); err != nil {
log.Printf("WARNING: error removing tracking path %s: %s", p, err)
continue
}
delete(w.tracking, p)
}
return nil
}