-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwatcher.go
173 lines (150 loc) · 3.29 KB
/
rwatcher.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
package rfsnotify
import (
"errors"
"os"
"path/filepath"
"github.com/fsnotify/fsnotify"
)
type Watcher struct {
Events chan fsnotify.Event
// Errors sends any errors.
Errors chan error
w_fsnotify *fsnotify.Watcher // Underlying fsnotify watcher
done chan struct{} // Channel for sending a "quit message" to the reader goroutine
doneResp chan struct{} // Channel to respond to Close
}
func NewWatcher() (*Watcher, error) {
w_fsnotify, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
w := &Watcher{
Events: make(chan fsnotify.Event),
Errors: make(chan error),
w_fsnotify: w_fsnotify,
done: make(chan struct{}),
doneResp: make(chan struct{}),
}
go w.readEvents()
return w, nil
}
func (w *Watcher) isClosed() bool {
select {
case <-w.done:
return true
default:
return false
}
}
// Close removes all watches and closes the events channel.
func (w *Watcher) Close() error {
if w.isClosed() {
return nil
}
// Send 'close' signal to goroutine, and set the Watcher to closed.
close(w.done)
// Wait for the reader to finish so we can close the events channel
<-w.doneResp
return w.w_fsnotify.Close()
}
func (w *Watcher) Add(name string) error {
if w.isClosed() {
return errors.New("already closed")
}
if err := w.recursive(name, true); err != nil {
return err
}
return nil
}
func (w *Watcher) Remove(name string) error {
if w.isClosed() {
return errors.New("already closed")
}
if err := w.recursive(name, false); err != nil {
return err
}
return nil
}
func (w *Watcher) WatchList() []string {
return w.w_fsnotify.WatchList()
}
func (w *Watcher) recursive(path string, isAdd bool) error {
err := filepath.Walk(path, func(walkPath string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
if isAdd {
if err = w.w_fsnotify.Add(walkPath); err != nil {
return err
}
} else {
if err = w.w_fsnotify.Remove(walkPath); err != nil {
return err
}
}
}
return nil
})
return err
}
func (w *Watcher) eventRecursive(path string, isAdd bool, eventQueue *eventQueue) error {
err := filepath.Walk(path, func(walkPath string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if walkPath == path {
return nil
}
if fi.IsDir() {
if isAdd {
if err = w.w_fsnotify.Add(walkPath); err != nil {
return err
}
} else {
if err = w.w_fsnotify.Remove(walkPath); err != nil {
return err
}
}
}
// Assume the path is created
eventQueue.push(fsnotify.Event{
Name: walkPath,
Op: fsnotify.Create,
})
return nil
})
return err
}
func (w *Watcher) readEvents() {
defer func() {
close(w.doneResp)
close(w.Errors)
close(w.Events)
}()
for {
// See if we have been closed.
if w.isClosed() {
return
}
select {
case event := <-w.w_fsnotify.Events:
w.Events <- event
// Recursively add or remove directories
if event.Has(fsnotify.Create) || event.Has(fsnotify.Chmod) {
eventQueue := newEventQueue()
if err := w.eventRecursive(event.Name, true, eventQueue); err != nil {
w.Errors <- err
}
for eventQueue.size() > 0 {
w.Events <- eventQueue.pop()
}
}
case err := <-w.w_fsnotify.Errors:
w.Errors <- err
case <-w.done:
w.doneResp <- struct{}{}
return
}
}
}