-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsnotify.go
289 lines (256 loc) · 6.73 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
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
package watcher
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"github.com/fsnotify/fsnotify"
)
// FSNotifyEventSource is a file event source, based on inotify for changes
// notifications.
type FSNotifyEventSource struct {
*GenericEventSource
// AbsFilePath absolute path to the file.
AbsFilePath string
// ParentDir the parent directory of the file.
ParentDir string
// cuurentPos the last (current) position in the file.
currentPos int64
}
// AttachToWatcher attaches the parent dir to the inotify watcher.
func (s *FSNotifyEventSource) AttachToWatcher(watcher *fsnotify.Watcher) error {
return watcher.Add(s.ParentDir)
}
// DetachFromWatcher deatches the parent dir from the inotify watcher.
func (s *FSNotifyEventSource) DetachFromWatcher(watcher *fsnotify.Watcher) error {
return watcher.Remove(s.ParentDir)
}
// fileAvailable checks if the file is (still) available and sets the position
// if it is.
func (s *FSNotifyEventSource) fileAvailable() error {
f, err := os.Open(s.AbsFilePath)
if err != nil {
return err
}
defer f.Close()
endPos, err := f.Seek(0, os.SEEK_END)
if err != nil {
return err
}
s.currentPos = endPos
return nil
}
// calculateDiff calculates the diff from the previous position.
// The diff contains the bytes from the last position of the file to the end of
// the file.
func (s *FSNotifyEventSource) calculateDiff() ([]byte, error) {
f, err := os.Open(s.AbsFilePath)
if err != nil {
return nil, err
}
defer f.Close()
curr := s.currentPos
_, err = f.Seek(curr, os.SEEK_SET)
if err != nil {
return nil, err
}
diff, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
curr, err = f.Seek(0, os.SEEK_CUR)
if err != nil {
return nil, err
}
s.currentPos = curr
return diff, err
}
// handleFSNotifyEvent called on inotify change event.
// Checks for file changes and calculates the diff.
func (s *FSNotifyEventSource) handleFSNotifyEvent(ev fsnotify.Event) error {
if ev.Op&fsnotify.Write == fsnotify.Write {
diff, err := s.calculateDiff()
if err != nil {
return err
}
s.Trigger(diff)
} else if ev.Op&fsnotify.Create == fsnotify.Create {
if err := s.fileAvailable(); err != nil {
return err
}
diff, err := s.calculateDiff()
if err != nil {
return err
}
s.Trigger(diff)
}
return nil
}
// NewFileSource creates new file event source for the given file path.
func NewFileSource(filePath string) EventSource {
parentDir, absPath, err := GetFileParts(filePath)
if err != nil {
panic(err) // FIXME
}
fileSource := &FSNotifyEventSource{
GenericEventSource: NewEventSource(filePath),
AbsFilePath: absPath,
ParentDir: parentDir,
}
fileSource.fileAvailable()
return fileSource
}
// FSNotifyWatcher implements WatcherDaemon based on inotify events.
type FSNotifyWatcher struct {
watcher *fsnotify.Watcher
watchedDirs map[string][]EventSource
sources map[string]EventSource
started bool
mux sync.Mutex
done chan bool
}
// Start the watcher. When called, the watcher starts to listen for changes in
// the registered file sources.
func (f *FSNotifyWatcher) Start() error {
f.mux.Lock()
defer f.mux.Unlock()
if f.started {
return fmt.Errorf("already started")
}
f.started = true
f.listenForChanges()
return nil
}
// Stop stop the watcher. No more event are going to be handled after this call.
// The inotify watcher is closed and resources are released.
func (f *FSNotifyWatcher) Stop() error {
f.mux.Lock()
defer f.mux.Unlock()
if !f.started {
return fmt.Errorf("stopped")
}
f.watcher.Close()
<-f.done
return nil
}
// AddSource adds an event source to be managed by this watcher dameon.
// If the event source is an FSNotifyEventSource, it is attached to the
// underlying fsnotify watcher.
func (f *FSNotifyWatcher) AddSource(source string, eventSource EventSource) (EventSource, error) {
f.mux.Lock()
src, ok := f.sources[source]
if ok {
f.mux.Unlock()
return src, nil
}
if fsnSource, ok := eventSource.(*FSNotifyEventSource); ok {
sourcesInDir, ok := f.watchedDirs[fsnSource.ParentDir]
if !ok {
sourcesInDir = []EventSource{}
if err := fsnSource.AttachToWatcher(f.watcher); err != nil {
f.mux.Unlock()
return nil, err
}
}
sourcesInDir = append(sourcesInDir, fsnSource)
f.watchedDirs[fsnSource.ParentDir] = sourcesInDir
}
f.sources[source] = eventSource
f.mux.Unlock()
return eventSource, nil
}
// RemoveSource removes the event source and it is no longer managed by this
// daemon.
// If the event source is also FSNotifyEventSource it is detached from the
// underlying fsnotify watcher as well.
func (f *FSNotifyWatcher) RemoveSource(source string) error {
f.mux.Lock()
src, ok := f.sources[source]
if !ok {
f.mux.Unlock()
return fmt.Errorf("source not managed")
}
delete(f.sources, source)
f.mux.Unlock()
if fsnSource, ok := src.(*FSNotifyEventSource); ok {
// try to remove from watcher
if err := fsnSource.DetachFromWatcher(f.watcher); err != nil {
return err
}
}
// TODO: generic dispose call
return nil
}
// listenForChanges detaches a go routing that consumes the events from the
// fsnotify watcher.
func (f *FSNotifyWatcher) listenForChanges() {
go func() {
defer func() {
f.done <- true
}()
for {
select {
case event, ok := <-f.watcher.Events:
if !ok {
return
}
f.handleWatcherEvent(event)
case err, ok := <-f.watcher.Errors:
if !ok {
return
}
log.Println("[ERR]: Watcher event error: ", err.Error())
}
}
}()
}
// handleWatcherEvent handles single event raised by the undelying fsnotify
// watcher.
func (f *FSNotifyWatcher) handleWatcherEvent(ev fsnotify.Event) {
parentDir, absPath, err := GetFileParts(ev.Name)
if err != nil {
log.Println("error: cannot determine abs path for event: ", ev.Name)
return
}
sources, ok := f.watchedDirs[parentDir]
if !ok {
return
}
for _, source := range sources {
fsource := source.(*FSNotifyEventSource)
if fsource.AbsFilePath == absPath {
go func() {
if err = fsource.handleFSNotifyEvent(ev); err != nil {
log.Println("Error in handling event: ", err.Error())
}
}()
}
}
}
// NewWatchDaemon builds a new WatchDaemon.
func NewWatchDaemon() WatchDaemon {
fsWatcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err) // FIME:
}
return &FSNotifyWatcher{
mux: sync.Mutex{},
sources: map[string]EventSource{},
watchedDirs: map[string][]EventSource{},
started: false,
watcher: fsWatcher,
done: make(chan bool),
}
}
// GetFileParts splits the file path to the absolute path and the parent
// directory path.
func GetFileParts(filePath string) (parentDir string, absPath string, err error) {
absPath, err = filepath.Abs(filePath)
if err != nil {
return
}
parentDir = filepath.Dir(absPath)
return
}