-
-
Save nbari/386af0fa667ae03daf3fbc80e3838ab0 to your computer and use it in GitHub Desktop.
Golang Kqueue Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// helpful links: | |
// https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/EV_SET.2.html | |
// http://julipedia.meroh.net/2004/10/example-of-kqueue.html | |
// create kqueue | |
kq, err := syscall.Kqueue() | |
if err != nil { | |
log.Println("Error creating Kqueue descriptor!") | |
return | |
} | |
// open folder | |
fd, err := syscall.Open("/home/test", syscall.O_RDONLY, 0) | |
if err != nil { | |
log.Println("Error opening folder descriptor!") | |
return | |
} | |
// build kevent | |
ev1 := 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, | |
} | |
// configure timeout | |
timeout := syscall.Timespec{ | |
Sec: 0, | |
Nsec: 0, | |
} | |
// wait for events | |
for { | |
// create kevent | |
events := make([]syscall.Kevent_t, 10) | |
nev, err := syscall.Kevent(kq, []syscall.Kevent_t{ev1}, events, &timeout) | |
if err != nil { | |
log.Println("Error creating kevent") | |
} | |
// check if there was an event | |
for i := 0; i < nev; i++ { | |
// log | |
log.Printf("Event [%d] -> %+v", i, events[i]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment