I'm playing around with the inotify functionality in golang's syscall library. I'm able to setup the inotify functionality with InotifyInit
, add a file to watch with InotifyAddWatch
and detect file changes with the Read
function. The problem that I'm having is that the Read
function only reads into a byte array containing info about the inotify events. I'd like to convert/cast that byte array into an InotifyEvent
structure so that I can access information about the inotify events properly
Below is what I have so far:
package main
import (
"fmt"
"syscall"
)
func main() {
buff := make([]byte, 64)
inotefd, err := syscall.InotifyInit()
if err != nil {
fmt.Println(err)
}
_, err = syscall.InotifyAddWatch(inotefd, "/home/me/foo", syscall.IN_MODIFY)
if err != nil {
fmt.Println(err)
}
for {
n, err := syscall.Read(inotefd, buff)
if err != nil {
fmt.Println(err)
return
}
if n < 0 {
fmt.Println("Read Error")
return
}
fmt.Printf("Buffer: %v
", buff)
//can't cast []buff into InotifyEvent struct
fmt.Printf("Cookie: %v
", buff[0:4])
fmt.Printf("Len: %v
", buff[4:8])
fmt.Printf("Mask: %v
", buff[8:12])
fmt.Printf("Name: %v
", buff[12:13])
fmt.Printf("Wd: %v
", buff[13:17])
}
}
Thanks for your help !