-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
267 lines (220 loc) · 6.03 KB
/
watch.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
package path
import (
"context"
"fmt"
"io/fs"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"golang.org/x/exp/slices"
)
// WatchEvent is a wrapper for Entry and fsnotify.Op.
type WatchEvent struct {
Entry
fsnotify.Op
}
// WatchDir will watch a directory indefinitely for changes and publish them on the given files channel with optional filters.
func WatchDir(ctx context.Context, inputPath string, recursiveDepth uint8, includeRoot bool, files chan WatchEvent, errors chan error, filters ...WatchFilter) {
inputPath = filepath.Clean(strings.TrimSpace(inputPath))
var inputEntry, err = NewEntry(inputPath, recursiveDepth)
if err != nil {
errors <- fmt.Errorf("error with inputPath: %w", err)
return
}
defer close(files)
defer close(errors)
// Create new watcher.
watcher, err := fsnotify.NewWatcher()
if err != nil {
errors <- fmt.Errorf("error creating NewWatcher: %w", err)
return
}
defer watcher.Close()
// Start listening for events.
var wait = make(chan struct{})
go func() {
defer close(wait)
EventsLoop:
for {
select {
case <-ctx.Done():
return
case event, open := <-watcher.Events:
if !open {
return
}
// try all the filter funcs
for _, fn := range filters {
var accepted, err = fn.filter(event)
if err != nil {
errors <- err
}
if !accepted {
continue EventsLoop
}
}
if e, err := NewEntry(event.Name, recursiveDepth); err != nil {
errors <- err
} else {
files <- WatchEvent{Entry: e, Op: event.Op}
}
case err, open := <-watcher.Errors:
if !open {
return
}
errors <- err
}
}
}()
var entries []Entry
if recursiveDepth > 0 {
var rootEntry, err = NewEntry(inputPath, MaxDepth, NewDirEntitiesFilter())
if err != nil {
errors <- fmt.Errorf("error adding path to watcher: %w", err)
return
}
entries, err = rootEntry.Flatten(includeRoot)
if err != nil {
errors <- fmt.Errorf("error flattening entry: %w", err)
return
}
entries = append(entries, inputEntry)
} else {
entries = []Entry{inputEntry}
}
// Add paths.
for _, dir := range entries {
err = watcher.Add(dir.AbsolutePath)
if err != nil {
errors <- fmt.Errorf("error adding path to watcher: %w", err)
return
}
}
<-wait
}
//////////////////////////////////////////////////////////////////
// WatchFilter interface facilitates filtering of file events.
type WatchFilter interface {
filter(fsnotify.Event) (bool, error)
}
// RegexWatchFilter filters fs events by matching file names to a given regex.
type RegexWatchFilter struct {
regex *regexp.Regexp
}
func NewRegexWatchFilter(filterRegex *regexp.Regexp) RegexWatchFilter {
return RegexWatchFilter{regex: filterRegex}
}
func (rf RegexWatchFilter) filter(event fsnotify.Event) (bool, error) {
return rf.regex.MatchString(event.Name), nil
}
// DateWatchFilter filters fs events by matching ensuring ModTime is within the given date range.
type DateWatchFilter struct {
from time.Time
to time.Time
}
func NewDateWatchFilter(from, to time.Time) DateWatchFilter {
return DateWatchFilter{from: from, to: to}
}
func (df DateWatchFilter) filter(event fsnotify.Event) (bool, error) {
var entry, err = NewEntry(event.Name, 0)
if err != nil {
return false, err
}
if entry.FileInfo.ModTime().Before(df.from) || entry.FileInfo.ModTime().After(df.to) {
return false, nil
}
return true, nil
}
// SkipMapWatchFilter filters fs events by ensuring the given file is NOT within the given map.
type SkipMapWatchFilter struct {
skipMap map[string]struct{}
}
func NewSkipMapWatchFilter(skipMap map[string]struct{}) SkipMapWatchFilter {
return SkipMapWatchFilter{skipMap: skipMap}
}
func (smf SkipMapWatchFilter) filter(event fsnotify.Event) (bool, error) {
var entry, err = NewEntry(event.Name, 0)
if err != nil {
return false, err
}
if _, has := smf.skipMap[entry.AbsolutePath]; has {
return false, nil
}
return true, nil
}
// PermissionsWatchFilter filters fs events by ensuring the given file permissions are within the given range.
type PermissionsWatchFilter struct {
min uint32
max uint32
}
func NewPermissionsWatchFilter(min, max uint32) PermissionsWatchFilter {
return PermissionsWatchFilter{min: min, max: max}
}
func (pf PermissionsWatchFilter) filter(event fsnotify.Event) (bool, error) {
var entry, err = NewEntry(event.Name, 0)
if err != nil {
return false, err
}
if entry.FileInfo.Mode() < fs.FileMode(pf.min) || entry.FileInfo.Mode() > fs.FileMode(pf.max) {
return false, nil
}
return true, nil
}
// SizeWatchFilter filters fs events by ensuring the given file within the given size range (in bytes).
// Directories are always returned true.
type SizeWatchFilter struct {
min int64
max int64
}
func NewSizeWatchFilter(min, max int64) SizeWatchFilter {
return SizeWatchFilter{min: min, max: max}
}
func (pf SizeWatchFilter) filter(event fsnotify.Event) (bool, error) {
var entry, err = NewEntry(event.Name, 0)
if err != nil {
return false, err
}
if entry.FileInfo.IsDir() {
return true, nil
} else if entry.FileInfo.Size() < pf.min || entry.FileInfo.Size() > pf.max {
return false, nil
}
return true, nil
}
// OpWatchFilter filters fs events by fsnotify.Op event type.
type OpWatchFilter struct {
Ops []fsnotify.Op
}
func NewOpWatchFilter(ops ...fsnotify.Op) OpWatchFilter {
return OpWatchFilter{Ops: ops}
}
func (of OpWatchFilter) filter(event fsnotify.Event) (bool, error) {
if slices.Contains(of.Ops, event.Op) {
return true, nil
}
return false, nil
}
// DirWatchFilter only returns sub directories of the target.
type DirWatchFilter struct{}
func NewDirWatchFilter() DirWatchFilter {
return DirWatchFilter{}
}
func (df DirWatchFilter) filter(entry Entry) (bool, error) {
if entry.FileInfo.IsDir() {
return true, nil
}
return false, nil
}
// FileWatchFilter only returns files.
type FileWatchFilter struct{}
func NewFileWatchFilter() FileWatchFilter {
return FileWatchFilter{}
}
func (ff FileWatchFilter) filter(entry Entry) (bool, error) {
if entry.FileInfo.IsDir() {
return false, nil
}
return true, nil
}