-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheyenotify_darwin.go
88 lines (80 loc) · 1.76 KB
/
eyenotify_darwin.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
package main
import (
"io/ioutil"
"log"
"runtime/debug"
"strings"
"syscall"
)
func processEvent() {
restartProc()
}
func runKqueue() {
fd, err := syscall.Kqueue()
evTrackList := make([]syscall.Kevent_t, 0)
if err != nil {
log.Fatal("error initializing Kqueue: ", err)
return
}
addFilesToKqueue(evTrackList, path)
// configure timeout
timeout := syscall.Timespec{
Sec: 0,
Nsec: 0,
}
// wait for events
for {
// create kevent
events := make([]syscall.Kevent_t, 10)
_, err := syscall.Kevent(fd, evTrackList, events, &timeout)
if err != nil {
debug.PrintStack()
log.Fatal("Error creating kevent: ", err)
}
// check if there was an event and process it
if len(events) > 0 && events[0].Ident > 0 {
processEvent()
}
}
}
func addFilesToKqueue(evTrackList []syscall.Kevent_t, dirPath string) {
fileList, err := ioutil.ReadDir(dirPath)
if err != nil {
log.Fatal("ReadDir failed: ", err)
}
for _, file := range fileList {
newPath := dirPath + "/" + file.Name()
if file.IsDir() && file.Name() != ".git" {
addFilesToPoll(newPath)
} else {
fileName := file.Name()
if len(strings.Split(fileName, ".")) > 1 {
fileExt := strings.Split(fileName, ".")[1]
if fileExt == ext {
fd, err := syscall.Open(newPath, syscall.O_RDONLY, 0)
if err != nil {
log.Fatal("Open failed: ", err)
}
// build kevent
ev := syscall.Kevent_t{
Ident: uint64(fd),
Filter: syscall.EVFILT_VNODE,
Flags: syscall.EV_ADD | syscall.EV_ENABLE | syscall.EV_ONESHOT,
Fflags: syscall.NOTE_DELETE | syscall.NOTE_WRITE,
Data: 0,
Udata: nil,
}
evTrackList = append(evTrackList, ev)
}
}
}
}
}
func main() {
startProc()
if polling {
runPolling()
} else {
runKqueue()
}
}