/
dirmonitor.go
212 lines (188 loc) · 5.77 KB
/
dirmonitor.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
package filetypestats
import (
"fmt"
"time"
"github.com/Rainc1oud/filetypestats/notifywatch"
ggu "github.com/Rainc1oud/gogenutils"
"github.com/rjeczalik/notify"
)
type TDirMonitor struct {
notifywatch.NotifyWatcher // embed NotifyWatcher, because TDirMonitor is just a Watcer with added state and access/info methods
tstarted time.Time
tfinished time.Time
dlastscan time.Duration
dirty bool
}
func newDirMonitor(dir string, recursive bool, handler notifywatch.NotifyHandlerFun, events ...notify.Event) *TDirMonitor {
dm := &TDirMonitor{
*notifywatch.NewNotifyWatcher(dir, recursive, handler, events...),
time.Time{},
time.Time{},
time.Duration(0),
false,
}
return dm
}
func (t *TDirMonitor) scanRunning() bool {
return t.tstarted.After(t.tfinished)
}
func (t *TDirMonitor) scanStart() {
t.tstarted = time.Now()
}
func (t *TDirMonitor) scanFinish() {
t.tfinished = time.Now()
t.dlastscan = time.Since(t.tstarted)
}
func (t *TDirMonitor) scanStarted() time.Time {
return t.tstarted
}
func (t *TDirMonitor) scanFinished() time.Time {
return t.tfinished
}
func (t *TDirMonitor) isDirty() bool {
return t.dirty
}
type TDirMonitors map[string]*TDirMonitor
type TDirMonitorsStatus struct {
Dirty bool
ScanStartedLast time.Time
ScanFinishedLast time.Time
ScanLongestLast time.Duration // the longest duration of all last dir scans
}
// TODO: this is a generic function for any map[string]interface{}, handle after generics support is here (go1.18)
func (dm *TDirMonitors) keys() []string {
s := make([]string, len(*dm))
i := 0
for k := range *dm {
s[i] = k
i++
}
return s
}
// NewDirMonitors constructor
func NewDirMonitors() *TDirMonitors {
tds := make(TDirMonitors)
return &tds
}
func (dm *TDirMonitors) getItem(dir string) *TDirMonitor {
if v, ok := (*dm)[dir]; ok {
return v
}
return &TDirMonitor{
notifywatch.NotifyWatcher{},
time.Time{},
time.Time{},
time.Duration(0),
false,
}
}
func (dm *TDirMonitors) Status() *TDirMonitorsStatus {
dms := &TDirMonitorsStatus{
Dirty: false,
ScanStartedLast: time.Time{},
ScanFinishedLast: time.Time{},
ScanLongestLast: time.Duration(0),
}
for _, k := range dm.keys() {
if dms.ScanStartedLast.Before((*dm)[k].scanStarted()) {
dms.ScanStartedLast = (*dm)[k].scanStarted()
}
if dms.ScanFinishedLast.Before((*dm)[k].scanFinished()) {
dms.ScanFinishedLast = (*dm)[k].scanFinished()
}
if dms.ScanLongestLast < (*dm)[k].dlastscan {
dms.ScanLongestLast = (*dm)[k].dlastscan
}
dms.Dirty = dms.Dirty || (*dm)[k].isDirty()
}
return dms
}
// overlappedDirs returns all dirs that should be removed from the set {dir, Dirs()} because they are overlapped by a parent from the set (i.e. the returned list contains all entries that are under other entries in dir hierarchy)
func (dm *TDirMonitors) overlappedDirs(dir string) []string {
alldirs := append(dm.Dirs(), dir)
filtdirs := ggu.FilterCommonRootDirs(alldirs)
rmdirs := []string{}
for _, d := range alldirs {
if !ggu.InSlice(d, filtdirs) {
rmdirs = append(rmdirs, d)
}
}
return rmdirs
}
// AddDir adds dir to the DirMonitors collection with a new DirMonitor instance, while removing all overlapping dirs
func (dm *TDirMonitors) AddDir(dir string, recursive bool, handler notifywatch.NotifyHandlerFun, events ...notify.Event) *TDirMonitor {
unwanted := dm.overlappedDirs(dir)
if ggu.InSlice(dir, unwanted) {
unwanted = ggu.RemoveFromStringSlice(dir, unwanted)
}
if len(unwanted) > 0 {
dm.RemoveDirs(unwanted...)
return nil
}
if v, ok := (*dm)[dir]; ok {
return v // ignore if exists
}
(*dm)[dir] = newDirMonitor(dir, recursive, handler, events...)
return (*dm)[dir]
}
// RemoveDirs removes dirs from the container
func (dm *TDirMonitors) RemoveDirs(dirs ...string) error {
errs := ggu.NewErrors()
for _, d := range dirs {
errs.AddIf(dm.RemoveDir(d))
}
return errs.Err()
}
// RemoveDir removes dir from the container
func (dm *TDirMonitors) RemoveDir(dir string) error {
if _, ok := (*dm)[dir]; !ok {
return fmt.Errorf("monitor for %s doesn't exist, watcher not removed", dir)
}
err := (*dm)[dir].Stop() // TBC: do we need to handle the error? probably not, because it's basically a warning (channel already closed)
delete(*dm, dir) // no need to check existence, delete non-existing is no-op
return err
}
// Dirs returns a slice of all registered dirs
func (dm *TDirMonitors) Dirs() []string {
return dm.keys()
}
func (dm *TDirMonitors) hasElem(key string) bool {
_, ok := (*dm)[key]
return ok
}
// Contains returns whether dir is contained in the registered dirs
func (dm *TDirMonitors) Contains(dir string) bool {
return dm.hasElem(dir)
}
// ScanRunning reports whether a ssscan on dir is currently running
func (dm *TDirMonitors) ScanRunning(dir string) bool {
return dm.getItem(dir).scanRunning()
}
// ScanFinish updates start time for dir
func (dm *TDirMonitors) ScanStart(dir string) {
if v, ok := (*dm)[dir]; ok {
v.scanStart()
v.dirty = true
}
// completely ignored if dir is not registered (= a pain for debugging?)
}
// ScanFinish updates finished time for dir
func (dm *TDirMonitors) ScanFinish(dir string) {
if v, ok := (*dm)[dir]; ok {
v.scanFinish()
v.dirty = false
}
// completely ignored if dir is not registered (= a pain for debugging?)
}
// ScanStarted returns the time the last scan was started
func (dm *TDirMonitors) ScanStarted(dir string) time.Time {
return dm.getItem(dir).scanStarted()
}
// ScanFinished returns the time the last scan was started
func (dm *TDirMonitors) ScanFinished(dir string) time.Time {
return dm.getItem(dir).scanFinished()
}
// IsDirty reports dirty status, i.e. if the DB for dir is up to date or being updated
func (dm *TDirMonitors) IsDirty(dir string) bool {
return dm.getItem(dir).isDirty()
}