-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
86 lines (78 loc) · 2.02 KB
/
worker.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
package gorsn
import (
"io/fs"
"sync/atomic"
)
func (sn *snotifier) workers(done *atomic.Bool) {
var i uint32
max := sn.opts.maxworkers.Load()
for i < max {
sn.wg.Add(1)
go sn.work(done)
i++
}
}
func (sn *snotifier) work(done *atomic.Bool) {
var fi fs.FileInfo
var pt pathType
var err error
for sn.running.Load() && !sn.stopping.Load() {
select {
case fse := <-sn.iqueue:
fi, err = fse.d.Info()
if err != nil {
// emit ERROR event earlier since no futuer check could be done.
if !sn.opts.event.ignoreErrors.Load() {
sn.queueEvent(Event{fse.path, getPathType(fi.Mode().Type()), ERROR, err})
}
continue
}
pt = getPathType(fse.d.Type())
// use check to support the dynamic nature of `sn.opts` value.
// pass nil since `fse.err` is used to build the event later.
if ignore, _ := sn.check(fse.path, pt, nil); ignore {
continue
}
sn.event(pt, fse, fi)
default:
// default ensures usage & non-blocking of select.
if len(sn.iqueue) == 0 && done.Load() {
sn.wg.Done()
return
}
}
}
sn.wg.Done()
}
// event processes the path based on its recent state and emit or
// not an appropriate event to the external queue.
func (sn *snotifier) event(pt pathType, fse *fsEntry, fi fs.FileInfo) {
val, exists := sn.paths.Load(fse.path)
if !exists {
sn.paths.Store(fse.path, &pathInfos{fi.ModTime(), fi.Mode().Type(), true})
if !sn.opts.event.ignoreCreate.Load() {
sn.queueEvent(Event{fse.path, pt, CREATE, fse.err})
}
return
}
pi := val.(*pathInfos)
pi.visited = true
change := false
if fi.Mode().Type().Perm() != pi.mode.Perm() {
change = true
pi.mode = fi.Mode().Type()
if !sn.opts.event.ignorePerm.Load() {
sn.queueEvent(Event{fse.path, pt, PERM, fse.err})
}
}
if fi.ModTime() != pi.modTime {
change = true
pi.modTime = fi.ModTime()
if !sn.opts.event.ignoreModify.Load() {
sn.queueEvent(Event{fse.path, pt, MODIFY, fse.err})
}
}
if !change && !sn.opts.event.ignoreNoChange.Load() {
sn.queueEvent(Event{fse.path, pt, NOCHANGE, fse.err})
}
}