forked from synack/docker-rsync
/
watch.go
59 lines (52 loc) · 1.57 KB
/
watch.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
package main
import (
"github.com/mattes/fsevents"
"sort"
"time"
)
var noteDescription = map[fsevents.EventFlags]string{
fsevents.MustScanSubDirs: "MustScanSubdirs",
fsevents.UserDropped: "UserDropped",
fsevents.KernelDropped: "KernelDropped",
fsevents.EventIDsWrapped: "EventIDsWrapped",
fsevents.HistoryDone: "HistoryDone",
fsevents.RootChanged: "RootChanged",
fsevents.Mount: "Mount",
fsevents.Unmount: "Unmount",
fsevents.ItemCreated: "Created",
fsevents.ItemRemoved: "Removed",
fsevents.ItemInodeMetaMod: "InodeMetaMod",
fsevents.ItemRenamed: "Renamed",
fsevents.ItemModified: "Modified",
fsevents.ItemFinderInfoMod: "FinderInfoMod",
fsevents.ItemChangeOwner: "ChangeOwner",
fsevents.ItemXattrMod: "XAttrMod",
fsevents.ItemIsFile: "IsFile",
fsevents.ItemIsDir: "IsDir",
fsevents.ItemIsSymlink: "IsSymLink",
}
func Watch(path string, eventHandler func(id uint64, path string, flags []string)) {
dev, _ := fsevents.DeviceForPath(path)
fsevents.EventIDForDeviceBeforeTime(dev, time.Now())
es := &fsevents.EventStream{
Paths: []string{path},
Latency: 50 * time.Millisecond,
Device: dev,
Flags: fsevents.FileEvents | fsevents.WatchRoot}
es.Start()
ec := es.Events
for {
select {
case event := <-ec:
flags := make([]string, 0)
for bit, description := range noteDescription {
if event.Flags&bit == bit {
flags = append(flags, description)
}
}
sort.Sort(sort.StringSlice(flags))
go eventHandler(event.ID, event.Path, flags)
es.Flush(false)
}
}
}