-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocal.go
332 lines (290 loc) · 6.58 KB
/
local.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package cloudwatcher
import (
"encoding/json"
"fmt"
"github.com/fsnotify/fsnotify"
"os"
"path/filepath"
"sync/atomic"
"time"
)
// LocalWatcher is the specialized watcher for the local FS
type LocalWatcher struct {
WatcherBase
syncing uint32
watcher *fsnotify.Watcher
ticker *time.Ticker
stop chan bool
config *localConfiguration
cache map[string]*LocalObject
}
// LocalObject is the object that contains the info of the file
type LocalObject struct {
Key string
Size int64
LastModified time.Time
FileMode os.FileMode
}
type localConfiguration struct {
Debug Bool `json:"debug"`
DisableFsNotify Bool `json:"disable_fsnotify"`
}
func newLocalWatcher(dir string, interval time.Duration) (Watcher, error) {
w := &LocalWatcher{
config: &localConfiguration{},
stop: make(chan bool, 1),
cache: make(map[string]*LocalObject),
WatcherBase: WatcherBase{
Events: make(chan Event, 100),
Errors: make(chan error, 100),
watchDir: dir,
pollingTime: interval,
},
}
if _, err := os.Stat(dir); os.IsNotExist(err) {
return nil, fmt.Errorf("directory '%s' not found", dir)
}
return w, nil
}
// SetConfig is used to configure the LocalWatcher
func (w *LocalWatcher) SetConfig(m map[string]string) error {
j, err := json.Marshal(m)
if err != nil {
return err
}
config := localConfiguration{}
if err := json.Unmarshal(j, &config); err != nil {
return err
}
w.config = &config
return nil
}
// Start launches the polling process
func (w *LocalWatcher) Start() error {
if _, err := os.Stat(w.watchDir); os.IsNotExist(err) {
return fmt.Errorf("directory '%s' not found", w.watchDir)
}
if w.config.DisableFsNotify {
w.ticker = time.NewTicker(w.pollingTime)
go func() {
// launch synchronization also the first time
w.sync(true)
for {
select {
case <-w.ticker.C:
w.sync(false)
case <-w.stop:
close(w.Events)
close(w.Errors)
return
}
}
}()
} else {
var err error
if w.watcher == nil {
w.watcher, err = fsnotify.NewWatcher()
if err != nil {
return err
}
}
go func() {
for {
select {
case event, ok := <-w.watcher.Events:
if !ok {
return
}
obj := &LocalObject{
Key: event.Name,
Size: 0,
LastModified: time.Now(),
FileMode: 0,
}
e := Event{}
var t Op
if event.Op&fsnotify.Write == fsnotify.Write {
t = FileChanged
} else if event.Op&fsnotify.Create == fsnotify.Create {
t = FileCreated
} else if event.Op&fsnotify.Remove == fsnotify.Remove {
t = FileDeleted
} else if event.Op&fsnotify.Chmod == fsnotify.Chmod {
t = TagsChanged
} else {
// ignoring other events
continue
}
switch t {
case FileDeleted:
e = Event{
Key: obj.Key,
Object: obj,
Type: t,
}
// we don't know if it was a folder...
w.rmRecursive(event.Name)
case FileCreated, FileChanged, TagsChanged:
fi, err := os.Stat(event.Name)
if err != nil {
w.Errors <- err
continue
}
// create listener on subfolders
if fi.IsDir() {
if err := w.addRecursive(event.Name); err != nil {
w.Errors <- err
}
}
obj = &LocalObject{
Key: event.Name,
Size: fi.Size(),
LastModified: fi.ModTime(),
FileMode: fi.Mode(),
}
e = Event{
Key: obj.Key,
Object: obj,
Type: t,
}
}
w.Events <- e
case err, ok := <-w.watcher.Errors:
if !ok {
return
}
w.Errors <- err
case <-w.stop:
close(w.Events)
close(w.Errors)
w.rmRecursive(w.watchDir)
w.watcher.Close()
return
}
}
}()
if err = w.addRecursive(w.watchDir); err != nil {
w.Close()
return err
}
}
return nil
}
// Close stop the polling process
func (w *LocalWatcher) Close() {
w.stop <- true
}
func (w *LocalWatcher) sync(firstSync bool) {
// allow only one sync at same time
if !atomic.CompareAndSwapUint32(&w.syncing, 0, 1) {
return
}
defer atomic.StoreUint32(&w.syncing, 0)
if _, err := os.Stat(w.watchDir); os.IsNotExist(err) {
w.Errors <- fmt.Errorf("directory '%s' not found", w.watchDir)
}
fileList := make(map[string]*LocalObject, 0)
err := filepath.Walk(w.watchDir, func(walkPath string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore the changes on the watched dir
if walkPath == w.watchDir {
return nil
}
obj := &LocalObject{
Key: walkPath,
Size: fi.Size(),
LastModified: fi.ModTime(),
FileMode: fi.Mode(),
}
fileList[walkPath] = obj
if !firstSync {
// Check if the object is cached by Key
cached := w.getCachedObject(obj)
// Object has been cached previously by Key
if cached != nil {
// Check if the LastModified has been changed
if !cached.LastModified.Equal(obj.LastModified) || (cached.Size != obj.Size) {
event := Event{
Key: obj.Key,
Type: FileChanged,
Object: obj,
}
w.Events <- event
}
// Check if the file modes have been updated
if cached.FileMode != obj.FileMode {
event := Event{
Key: obj.Key,
Type: TagsChanged,
Object: obj,
}
w.Events <- event
}
} else {
event := Event{
Key: obj.Key,
Type: FileCreated,
Object: obj,
}
w.Events <- event
}
}
w.cache[obj.Key] = obj
return nil
})
if err != nil {
w.Errors <- err
return
}
for k, o := range w.cache {
if _, found := fileList[k]; !found {
// file not found in the list...deleting it
delete(w.cache, k)
event := Event{
Key: o.Key,
Type: FileDeleted,
Object: o,
}
w.Events <- event
}
}
}
func (w *LocalWatcher) getCachedObject(o *LocalObject) *LocalObject {
if cachedObject, ok := w.cache[o.Key]; ok {
return cachedObject
}
return nil
}
func (w *LocalWatcher) addRecursive(dir string) error {
err := filepath.Walk(dir, func(walkPath string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
if err = w.watcher.Add(walkPath); err != nil {
return err
}
}
return nil
})
return err
}
func (w *LocalWatcher) rmRecursive(dir string) error {
err := filepath.Walk(dir, func(walkPath string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
if err = w.watcher.Remove(walkPath); err != nil {
return err
}
}
return nil
})
return err
}
func init() {
supportedServices["local"] = newLocalWatcher
}