-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
262 lines (223 loc) · 5.1 KB
/
watcher.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
package sdk
import (
"io/fs"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/tliron/kutil/util"
)
type Change int
const (
Added = Change(1)
Removed = Change(2)
Changed = Change(3)
)
// fmt.Stringer interface
func (self Change) String() string {
switch self {
case Added:
return "Added"
case Removed:
return "Removed"
case Changed:
return "Changed"
default:
return strconv.Itoa(int(self))
}
}
type OnChangedFunc func(change Change, identifier []string)
//
// Dir
//
type Dir []string
func NewDir(path string) Dir {
return Dir(strings.Split(path, string(os.PathSeparator)))
}
// fmt.Stringer interface
func (self Dir) String() string {
return filepath.Join(self...)
}
func (self Dir) Identifier() ([]string, bool) {
length := len(self)
if length == 1 {
return []string{"namespace", self[0]}, false
} else if length > 2 {
return []string{self[1], self[0], self[2]}, length > 3
} else {
return nil, false
}
}
func (self Dir) Equals(dir Dir) bool {
if len(self) != len(dir) {
return false
}
for index, segment := range self {
if segment != dir[index] {
return false
}
}
return true
}
//
// Watcher
//
type Watcher struct {
state *State
onChanged OnChangedFunc
watcher *fsnotify.Watcher
dirs []Dir
lock util.RWLocker
}
func NewWatcher(state *State, onChanged OnChangedFunc) (*Watcher, error) {
var self = Watcher{
state: state,
onChanged: onChanged,
lock: util.NewDefaultRWLocker(),
}
var err error
if self.watcher, err = fsnotify.NewWatcher(); err == nil {
if err := self.sync(); err == nil {
self.sync()
self.sync()
return &self, nil
} else {
return nil, err
}
} else {
return nil, err
}
}
func (self *Watcher) Start() {
watcherLog.Notice("starting watcher")
go func() {
for {
select {
case event, ok := <-self.watcher.Events:
if !ok {
watcherLog.Info("closed watcher")
return
}
// Ignore hidden files
if util.IsFileHidden(event.Name) {
continue
}
watcherLog.Debugf("%s %s", event.Op.String(), event.Name)
switch event.Op {
case fsnotify.Create:
if stat, err := os.Stat(event.Name); err == nil {
if stat.IsDir() {
dir := self.toDir(event.Name)
if err := self.add(dir); err != nil {
watcherLog.Warningf("%s", err.Error())
}
if identifier, packageFile := dir.Identifier(); identifier != nil {
if packageFile {
self.onChanged(Changed, identifier)
} else {
self.onChanged(Added, identifier)
}
}
}
} else {
watcherLog.Warningf("%s", err.Error())
}
case fsnotify.Remove, fsnotify.Rename:
// Note: we may receive this twice, once from the dir itself and once from its parent
if err := self.remove(self.toDir(event.Name)); err != nil {
watcherLog.Warningf("%s", err.Error())
}
dir := self.toDir(event.Name)
if identifier, packageFile := dir.Identifier(); identifier != nil {
if packageFile {
self.onChanged(Changed, identifier)
} else {
self.onChanged(Removed, identifier)
}
}
case fsnotify.Write, fsnotify.Chmod:
dir := self.toDir(event.Name)
if identifier, packageFile := dir.Identifier(); identifier != nil {
if packageFile {
self.onChanged(Changed, identifier)
}
}
}
case err, ok := <-self.watcher.Errors:
if !ok {
watcherLog.Info("closed watcher")
return
}
watcherLog.Errorf("watcher: %s", err.Error())
}
}
}()
}
func (self *Watcher) Stop() error {
return self.watcher.Close()
}
func (self *Watcher) sync() error {
self.lock.Lock()
defer self.lock.Unlock()
return filepath.WalkDir(self.state.RootDir, func(path string, dirEntry fs.DirEntry, err error) error {
if err != nil {
return err
}
if util.IsFileHidden(path) {
return fs.SkipDir
}
if dirEntry.IsDir() {
if err := self.watcher.Add(path); err == nil {
dir := NewDir(path)
watcherLog.Debugf("adding dir: %s", dir.String())
self.dirs = append(self.dirs, dir)
} else {
return err
}
}
return nil
})
}
func (self *Watcher) add(dir Dir) error {
self.lock.Lock()
defer self.lock.Unlock()
watcherLog.Debugf("adding dir: %s", dir.String())
self.dirs = append(self.dirs, dir)
if err := self.watcher.Add(self.toPath(dir)); err == nil {
return nil
} else {
return err
}
}
func (self *Watcher) remove(dir Dir) error {
self.lock.Lock()
defer self.lock.Unlock()
// TODO: improve this
var err error
var dirs []Dir
for _, dir_ := range self.dirs {
if dir_.Equals(dir) {
watcherLog.Debugf("removing dir: %s", dir.String())
self.watcher.Remove(self.toPath(dir)) // we are ignoring errors
} else {
dirs = append(dirs, dir_)
}
}
self.dirs = dirs
return err
}
func (self *Watcher) toDir(path string) Dir {
statePath := self.state.RootDir
length := len(statePath) + 1
if path == statePath {
path = ""
} else {
path = path[length:]
}
return NewDir(path)
}
func (self *Watcher) toPath(dir Dir) string {
segments := append([]string{self.state.RootDir}, dir...)
return filepath.Join(segments...)
}