-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
175 lines (144 loc) · 3.97 KB
/
file.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
package watch
import (
"context"
"fmt"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
"os"
"strings"
"sync"
"time"
)
type callbacks struct {
onChange func(name string)
onDelete func(name string)
}
type fileWatcher struct {
watcher *fsnotify.Watcher
callbacks map[string]callbacks
callbackMutex sync.RWMutex
creationWatchCtx context.Context
creationWatchCancelFn context.CancelFunc
creationWatch map[string]callbacks
creationWatchMutex sync.RWMutex
}
func NewFileWatcher() (*fileWatcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("unable to establish file watcher: %w", err)
}
result := &fileWatcher{
watcher: watcher,
callbacks: map[string]callbacks{},
callbackMutex: sync.RWMutex{},
creationWatch: map[string]callbacks{},
creationWatchMutex: sync.RWMutex{},
}
go result.startWatch()
result.creationWatchCtx, result.creationWatchCancelFn = context.WithCancel(context.Background())
go result.startWatchCreation()
return result, nil
}
func (f *fileWatcher) startWatch() {
for {
select {
case event, ok := <-f.watcher.Events:
if !ok {
return
}
f.callbackMutex.RLock()
callbacks, isWatched := f.callbacks[event.Name]
f.callbackMutex.RUnlock()
if isWatched {
if isTypeOf(&event, fsnotify.Write) {
callbacks.onChange(event.Name)
} else if isTypeOf(&event, fsnotify.Remove) || isTypeOf(&event, fsnotify.Rename) {
callbacks.onDelete(event.Name)
//rewatch file -> otherwise we will not be notified
err := f.Unwatch(event.Name)
if err != nil && strings.HasSuffix(err.Error(), "can't remove non-existent inotify watch for:") {
log.WithError(err).WithField("path", event.Name).Warn("Unable to unwatch file.")
}
f.watchCreation(event.Name, callbacks.onChange, callbacks.onDelete)
}
}
case err, ok := <-f.watcher.Errors:
if !ok {
return
}
log.WithError(err).Error("Watcher error occurred!")
}
}
}
func isTypeOf(event *fsnotify.Event, op fsnotify.Op) bool {
return event.Op&op == op
}
func (f *fileWatcher) startWatchCreation() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-f.creationWatchCtx.Done():
return
case <-ticker.C:
f.creationWatchMutex.RLock()
for path, callbacks := range f.creationWatch {
go f.checkForCreation(path, callbacks.onChange, callbacks.onDelete)
}
f.creationWatchMutex.RUnlock()
}
}
}
func (f *fileWatcher) checkForCreation(path string, onChange, onDelete func(name string)) {
if fileExists(path) {
err := f.watchChange(path, onChange, onDelete)
if err != nil {
log.WithError(err).WithField("path", path).Error("Unable to watch file!")
}
onChange(path)
f.creationWatchMutex.Lock()
defer f.creationWatchMutex.Unlock()
delete(f.creationWatch, path)
}
}
func (f *fileWatcher) Watch(path string, onChange, onDelete func(name string)) error {
if !fileExists(path) {
//watch for creation
f.watchCreation(path, onChange, onDelete)
return nil
}
return f.watchChange(path, onChange, onDelete)
}
func (f *fileWatcher) watchChange(path string, onChange, onDelete func(name string)) error {
f.callbackMutex.Lock()
defer f.callbackMutex.Unlock()
f.callbacks[path] = callbacks{
onChange: onChange,
onDelete: onDelete,
}
return f.watcher.Add(path)
}
func (f *fileWatcher) watchCreation(path string, onChange, onDelete func(name string)) {
f.creationWatchMutex.Lock()
defer f.creationWatchMutex.Unlock()
f.creationWatch[path] = callbacks{
onChange: onChange,
onDelete: onDelete,
}
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func (f *fileWatcher) Unwatch(path string) error {
f.callbackMutex.Lock()
defer f.callbackMutex.Unlock()
delete(f.callbacks, path)
return f.watcher.Remove(path)
}
func (f *fileWatcher) Close() error {
//stop creation watch
f.creationWatchCancelFn()
//stop fsnotify
return f.watcher.Close()
}