/
filewatcher.go
287 lines (242 loc) · 6.7 KB
/
filewatcher.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
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package filewatcher
import (
"bufio"
"crypto/md5"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"github.com/fsnotify/fsnotify"
multierror "github.com/hashicorp/go-multierror"
)
// FileWatcher is an interface that watches a set of files,
// delivering events to related channel.
type FileWatcher interface {
Add(path string) error
Remove(path string) error
Close() error
Events(path string) chan fsnotify.Event
Errors(path string) chan error
}
type fsNotifyWatcher struct {
mu sync.RWMutex
// The watcher maintain a map of workers,
// keyed by watched dir (parent dir of watched files).
workers map[string]*worker
}
type worker struct {
// watcher is a fsnotify watcher that watches the parent
// dir of watchedFiles.
watcher *fsnotify.Watcher
// The worker maintain a map of event channel (included in fileTracker),
// keyed by watched file path.
// The worker watches parent path of given path,
// and filter out events of given path, then redirect
// to the result channel.
// Note that for symlink files, the content in received events
// do not have to be related to the file itself.
watchedFiles map[string]*fileTracker
}
type fileTracker struct {
events chan fsnotify.Event
errors chan error
// md5 sum to indicate if a file has been updated.
md5Sum string
}
// NewWatcher return with a FileWatcher instance that implemented with fsnotify.
func NewWatcher() FileWatcher {
w := &fsNotifyWatcher{
workers: map[string]*worker{},
}
return w
}
// Add is implementation of Add interface of FileWatcher.
func (w *fsNotifyWatcher) Add(path string) error {
w.mu.Lock()
defer w.mu.Unlock()
cleanedPath, parentPath := formatPath(path)
if worker, workerExist := w.workers[parentPath]; workerExist {
if _, pathExist := worker.watchedFiles[cleanedPath]; pathExist {
return fmt.Errorf("path %s already added", path)
}
// And the path into worker maps if not exist.
md5Sum, err := getMd5Sum(cleanedPath)
if err != nil {
return fmt.Errorf("failed to get md5 sum for %s: %v", path, err)
}
worker.watchedFiles[cleanedPath] = &fileTracker{
events: make(chan fsnotify.Event, 1),
errors: make(chan error, 1),
md5Sum: md5Sum,
}
return nil
}
// Build a new worker if not exist.
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
if err = watcher.Add(parentPath); err != nil {
watcher.Close()
return err
}
var md5Sum string
if _, err = os.Stat(cleanedPath); err == nil {
md5Sum, err = getMd5Sum(cleanedPath)
if err != nil {
watcher.Close()
return fmt.Errorf("failed to get md5 sum for %s: %v", path, err)
}
} else {
if !os.IsNotExist(err) {
watcher.Close()
return fmt.Errorf("failed to stat file: %s: %v", path, err)
}
// if the file doesn't exist, ignore the md5 sum.
}
wk := &worker{
watcher: watcher,
watchedFiles: map[string]*fileTracker{
cleanedPath: {
events: make(chan fsnotify.Event, 1),
errors: make(chan error, 1),
md5Sum: md5Sum,
},
},
}
w.workers[parentPath] = wk
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok { // 'Events' channel is closed
return
}
for path, tracker := range wk.watchedFiles {
newSum, _ := getMd5Sum(path)
if newSum != "" && newSum != tracker.md5Sum {
tracker.md5Sum = newSum
tracker.events <- event
break
}
}
case err, ok := <-watcher.Errors:
if !ok {
// 'Errors' channel is closed. Construct an error for it.
// We'll only close worker errors channel in "Remove" for consistency.
err = errors.New("channel closed")
}
for _, tracker := range wk.watchedFiles {
tracker.errors <- err
}
return
}
}
}()
return nil
}
// Remove is implementation of Remove interface of FileWatcher.
func (w *fsNotifyWatcher) Remove(path string) error {
w.mu.Lock()
defer w.mu.Unlock()
return w.remove(path)
}
func (w *fsNotifyWatcher) remove(path string) error {
cleanedPath, parentPath := formatPath(path)
worker, workerExist := w.workers[parentPath]
if !workerExist {
return fmt.Errorf("path %s already removed", path)
}
tracker, pathExist := worker.watchedFiles[cleanedPath]
if !pathExist {
return fmt.Errorf("path %s already removed", path)
}
delete(worker.watchedFiles, cleanedPath)
close(tracker.events)
close(tracker.errors)
if len(worker.watchedFiles) == 0 {
// Remove the watch if all of its paths have been removed.
delete(w.workers, parentPath)
return worker.watcher.Close()
}
return nil
}
// Close is implementation of Close interface of FileWatcher.
func (w *fsNotifyWatcher) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
var errors *multierror.Error
for _, worker := range w.workers {
for path := range worker.watchedFiles {
errors = multierror.Append(errors, w.remove(path))
}
}
return errors.ErrorOrNil()
}
// Events is implementation of Events interface of FileWatcher.
func (w *fsNotifyWatcher) Events(path string) chan fsnotify.Event {
w.mu.RLock()
defer w.mu.RUnlock()
cleanedPath, parentPath := formatPath(path)
worker, workerExist := w.workers[parentPath]
if !workerExist {
return nil
}
tracker, pathExist := worker.watchedFiles[cleanedPath]
if !pathExist {
return nil
}
return tracker.events
}
// Errors is implementation of Errors interface of FileWatcher.
func (w *fsNotifyWatcher) Errors(path string) chan error {
w.mu.RLock()
defer w.mu.RUnlock()
cleanedPath, parentPath := formatPath(path)
worker, workerExist := w.workers[parentPath]
if !workerExist {
return nil
}
tracker, pathExist := worker.watchedFiles[cleanedPath]
if !pathExist {
return nil
}
return tracker.errors
}
// getMd5Sum is a helper func to calculate md5 sum.
func getMd5Sum(file string) (string, error) {
f, err := os.Open(file)
if err != nil {
return "", err
}
defer f.Close()
r := bufio.NewReader(f)
h := md5.New()
_, err = io.Copy(h, r)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
// formatPath return with the path after Clean,
// as well as its parent path.
func formatPath(path string) (string, string) {
cleanedPath := filepath.Clean(path)
parentPath, _ := filepath.Split(cleanedPath)
return cleanedPath, parentPath
}