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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
package reload
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
)
var ErrorSkip = errors.New("file is skipped")
var NoWalkerConfig = errors.New("should add at least one walker config, when reload is set to true")
// SimpleHook is used to filter by simple criteria, CONTAINS
type SimpleHook func(filename string, pattern []string) error
// An Event describes an event that is received when files or directory
// changes occur. It includes the os.FileInfo of the changed file or
// directory and the type of event that's occurred and the full path of the file.
type Event struct {
path string
info os.FileInfo
service string // type of service, http, grpc, etc...
}
type WatcherConfig struct {
// service name
serviceName string
// recursive or just add by singe directory
recursive bool
// directories used per-service
directories []string
// simple hook, just CONTAINS
filterHooks func(filename string, pattern []string) error
// path to file with files
files map[string]os.FileInfo
// ignored directories, used map for O(1) amortized get
ignored map[string]struct{}
// filePatterns to ignore
filePatterns []string
}
type Watcher struct {
// main event channel
Event chan Event
close chan struct{}
//=============================
mu *sync.Mutex
// indicates is walker started or not
started bool
// config for each service
// need pointer here to assign files
watcherConfigs map[string]WatcherConfig
}
// Options is used to set Watcher Options
type Options func(*Watcher)
// NewWatcher returns new instance of File Watcher
func NewWatcher(configs []WatcherConfig, options ...Options) (*Watcher, error) {
w := &Watcher{
Event: make(chan Event),
mu: &sync.Mutex{},
close: make(chan struct{}),
//workingDir: workDir,
watcherConfigs: make(map[string]WatcherConfig),
}
// add watcherConfigs by service names
for _, v := range configs {
w.watcherConfigs[v.serviceName] = v
}
// apply options
for _, option := range options {
option(w)
}
err := w.initFs()
if err != nil {
return nil, err
}
return w, nil
}
// initFs makes initial map with files
func (w *Watcher) initFs() error {
for srvName, config := range w.watcherConfigs {
fileList, err := w.retrieveFileList(srvName, config)
if err != nil {
return err
}
// workaround. in golang you can't assign to map in struct field
tmp := w.watcherConfigs[srvName]
tmp.files = fileList
w.watcherConfigs[srvName] = tmp
}
return nil
}
// ConvertIgnored is used to convert slice to map with ignored files
func ConvertIgnored(ignored []string) (map[string]struct{}, error) {
if len(ignored) == 0 {
return nil, nil
}
ign := make(map[string]struct{}, len(ignored))
for i := 0; i < len(ignored); i++ {
abs, err := filepath.Abs(ignored[i])
if err != nil {
return nil, err
}
ign[abs] = struct{}{}
}
return ign, nil
}
// GetAllFiles returns all files initialized for particular company
func (w *Watcher) GetAllFiles(serviceName string) []os.FileInfo {
var ret []os.FileInfo
for _, v := range w.watcherConfigs[serviceName].files {
ret = append(ret, v)
}
return ret
}
// https://en.wikipedia.org/wiki/Inotify
// SetMaxFileEvents sets max file notify events for Watcher
// In case of file watch errors, this value can be increased system-wide
// For linux: set --> fs.inotify.max_user_watches = 600000 (under /etc/<choose_name_here>.conf)
// Add apply: sudo sysctl -p --system
//func SetMaxFileEvents(events int) Options {
// return func(watcher *Watcher) {
// watcher.maxFileWatchEvents = events
// }
//
//}
// pass map from outside
func (w *Watcher) retrieveFilesSingle(serviceName, path string) (map[string]os.FileInfo, error) {
stat, err := os.Stat(path)
if err != nil {
return nil, err
}
filesList := make(map[string]os.FileInfo, 10)
filesList[path] = stat
// if it's not a dir, return
if !stat.IsDir() {
return filesList, nil
}
fileInfoList, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
// recursive calls are slow in compare to goto
// so, we will add files with goto pattern
outer:
for i := 0; i < len(fileInfoList); i++ {
var pathToFile string
// BCE check elimination
// https://go101.org/article/bounds-check-elimination.html
if len(fileInfoList) != 0 && len(fileInfoList) >= i {
pathToFile = filepath.Join(pathToFile, fileInfoList[i].Name())
} else {
return nil, errors.New("file info list len")
}
// if file in ignored --> continue
if _, ignored := w.watcherConfigs[serviceName].ignored[path]; ignored {
continue
}
// if filename does not contain pattern --> ignore that file
if w.watcherConfigs[serviceName].filePatterns != nil && w.watcherConfigs[serviceName].filterHooks != nil {
err = w.watcherConfigs[serviceName].filterHooks(fileInfoList[i].Name(), w.watcherConfigs[serviceName].filePatterns)
if err == ErrorSkip {
continue outer
}
}
filesList[pathToFile] = fileInfoList[i]
}
return filesList, nil
}
func (w *Watcher) StartPolling(duration time.Duration) error {
w.mu.Lock()
if w.started {
w.mu.Unlock()
return errors.New("already started")
}
w.started = true
w.mu.Unlock()
return w.waitEvent(duration)
}
// this is blocking operation
func (w *Watcher) waitEvent(d time.Duration) error {
ticker := time.NewTicker(d)
for {
select {
case <-w.close:
ticker.Stop()
// just exit
// no matter for the pollEvents
return nil
case <-ticker.C:
// this is not very effective way
// because we have to wait on Lock
// better is to listen files in parallel, but, since that would be used in debug... TODO
for serviceName, config := range w.watcherConfigs {
go func(sn string, c WatcherConfig) {
fileList, _ := w.retrieveFileList(sn, c)
w.pollEvents(c.serviceName, fileList)
}(serviceName, config)
}
}
}
}
// retrieveFileList get file list for service
func (w *Watcher) retrieveFileList(serviceName string, config WatcherConfig) (map[string]os.FileInfo, error) {
w.mu.Lock()
defer w.mu.Unlock()
fileList := make(map[string]os.FileInfo)
if config.recursive {
// walk through directories recursively
for _, dir := range config.directories {
// full path is workdir/relative_path
fullPath, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
list, err := w.retrieveFilesRecursive(serviceName, fullPath)
if err != nil {
return nil, err
}
for k, v := range list {
fileList[k] = v
}
}
return fileList, nil
}
for _, dir := range config.directories {
// full path is workdir/relative_path
fullPath, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
// list is pathToFiles with files
list, err := w.retrieveFilesSingle(serviceName, fullPath)
if err != nil {
return nil, err
}
for pathToFile, file := range list {
fileList[pathToFile] = file
}
}
return fileList, nil
}
func (w *Watcher) retrieveFilesRecursive(serviceName, root string) (map[string]os.FileInfo, error) {
fileList := make(map[string]os.FileInfo)
return fileList, filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// If path is ignored and it's a directory, skip the directory. If it's
// ignored and it's a single file, skip the file.
_, ignored := w.watcherConfigs[serviceName].ignored[path]
if ignored {
if info.IsDir() {
// if it's dir, ignore whole
return filepath.SkipDir
}
return nil
}
// if filename does not contain pattern --> ignore that file
err = w.watcherConfigs[serviceName].filterHooks(info.Name(), w.watcherConfigs[serviceName].filePatterns)
if err == ErrorSkip {
return nil
}
// Add the path and it's info to the file list.
fileList[path] = info
return nil
})
}
func (w *Watcher) pollEvents(serviceName string, files map[string]os.FileInfo) {
w.mu.Lock()
defer w.mu.Unlock()
// Store create and remove events for use to check for rename events.
creates := make(map[string]os.FileInfo)
removes := make(map[string]os.FileInfo)
// Check for removed files.
for pth, info := range w.watcherConfigs[serviceName].files {
if _, found := files[pth]; !found {
removes[pth] = info
}
}
// Check for created files, writes and chmods.
for pth, info := range files {
if info.IsDir() {
continue
}
oldInfo, found := w.watcherConfigs[serviceName].files[pth]
if !found {
// A file was created.
creates[pth] = info
continue
}
if oldInfo.ModTime() != info.ModTime() {
w.watcherConfigs[serviceName].files[pth] = info
w.Event <- Event{
path: pth,
info: info,
service: serviceName,
}
}
if oldInfo.Mode() != info.Mode() {
w.watcherConfigs[serviceName].files[pth] = info
w.Event <- Event{
path: pth,
info: info,
service: serviceName,
}
}
}
//Check for renames and moves.
for path1, info1 := range removes {
for path2, info2 := range creates {
if sameFile(info1, info2) {
e := Event{
path: path2,
info: info2,
service: serviceName,
}
// remove initial path
delete(w.watcherConfigs[serviceName].files, path1)
// update with new
w.watcherConfigs[serviceName].files[path2] = info2
w.Event <- e
}
}
}
//Send all the remaining create and remove events.
for pth, info := range creates {
w.watcherConfigs[serviceName].files[pth] = info
w.Event <- Event{
path: pth,
info: info,
service: serviceName,
}
}
for pth, info := range removes {
delete(w.watcherConfigs[serviceName].files, pth)
w.Event <- Event{
path: pth,
info: info,
service: serviceName,
}
}
}
func (w *Watcher) Stop() {
w.close <- struct{}{}
}
|