-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwatch_linux.go
58 lines (51 loc) · 1.07 KB
/
watch_linux.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
package changes
import (
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/convox/inotify"
)
var (
dirCreateFlags = inotify.IN_CREATE | inotify.IN_ISDIR
dirDeleteFlags = inotify.IN_DELETE | inotify.IN_ISDIR
watcher *inotify.Watcher
lock sync.Mutex
)
func init() {
watcher, _ = inotify.NewWatcher()
}
func startScanner(dir string) {
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info != nil && info.IsDir() {
lock.Lock()
// watcher.AddWatch(path, inotify.IN_CREATE|inotify.IN_DELETE|inotify.IN_MODIFY|inotify.IN_ATTRIB)
watcher.Watch(path)
lock.Unlock()
}
return nil
})
}
func waitForNextScan(dir string) {
tick := time.Tick(900 * time.Millisecond)
fired := false
for {
select {
case ev := <-watcher.Event:
if strings.HasPrefix(ev.Name, dir) {
if ev.Mask|dirCreateFlags == dirCreateFlags {
startScanner(ev.Name)
}
if ev.Mask|dirDeleteFlags == dirDeleteFlags {
watcher.RemoveWatch(ev.Name)
}
fired = true
}
case <-tick:
if fired {
return
}
}
}
}