add some hacky experiments for inotify event parsing

This commit is contained in:
Dominic Breuker
2018-02-11 22:15:10 +01:00
parent 38c5d42bb4
commit 9bc66835a6
8 changed files with 214 additions and 31 deletions

View File

@@ -2,13 +2,16 @@ package inotify
import (
"fmt"
"log"
"strings"
"unsafe"
"golang.org/x/sys/unix"
)
type Inotify struct {
fd int
watchers []*watcher
watchers map[int]*watcher
ping chan struct{}
paused bool
}
@@ -20,9 +23,10 @@ func NewInotify(ping chan struct{}) (*Inotify, error) {
}
i := &Inotify{
fd: fd,
ping: ping,
paused: false,
fd: fd,
watchers: make(map[int]*watcher),
ping: ping,
paused: false,
}
go watch(i)
@@ -34,7 +38,14 @@ func (i *Inotify) Watch(dir string) error {
if err != nil {
return fmt.Errorf("creating watcher: %v", err)
}
i.watchers = append(i.watchers, w)
i.watchers[w.wd] = w
return nil
}
func (i *Inotify) Close() error {
if err := unix.Close(i.fd); err != nil {
return fmt.Errorf("closing inotify file descriptor: %v", err)
}
return nil
}
@@ -47,19 +58,104 @@ func (i *Inotify) UnPause() {
}
func (i *Inotify) String() string {
dirs := make([]string, 0)
for _, w := range i.watchers {
dirs = append(dirs, w.dir)
if len(i.watchers) < 20 {
dirs := make([]string, 0)
for _, w := range i.watchers {
dirs = append(dirs, w.dir)
}
return fmt.Sprintf("Watching: %v", dirs)
} else {
return fmt.Sprintf("Watching %d directories", len(i.watchers))
}
return fmt.Sprintf("Watching: %v", dirs)
}
type bufRead struct {
n int
buf []byte
}
func watch(i *Inotify) {
buf := make([]byte, 1024)
buf := make([]byte, 20*unix.SizeofInotifyEvent)
buffers := make(chan bufRead)
go verboseWatcher(i, buffers)
for {
_, _ = unix.Read(i.fd, buf)
n, _ := unix.Read(i.fd, buf)
if !i.paused {
i.ping <- struct{}{}
}
buffers <- bufRead{
n: n,
buf: buf,
}
}
}
func verboseWatcher(i *Inotify, buffers chan bufRead) {
for bf := range buffers {
n := bf.n
buf := bf.buf
if n < unix.SizeofInotifyEvent {
if n == 0 {
// If EOF is received. This should really never happen.
panic(fmt.Sprintf("No bytes read from fd"))
} else if n < 0 {
// If an error occurred while reading.
log.Printf("ERROR: reading from inotify: %d", n)
} else {
// Read was too short.
log.Printf("ERROR: Short read")
}
continue
}
var offset uint32
for offset <= uint32(n-unix.SizeofInotifyEvent) {
raw := (*unix.InotifyEvent)(unsafe.Pointer(&buf[offset]))
mask := uint32(raw.Mask)
nameLen := uint32(raw.Len)
name := i.watchers[int(raw.Wd)].dir
if nameLen > 0 {
bytes := (*[unix.PathMax]byte)(unsafe.Pointer(&buf[offset+unix.SizeofInotifyEvent]))
if uint32(len(bytes)) > nameLen {
name += "/" + strings.TrimRight(string(bytes[0:nameLen]), "\000")
}
}
ev := newEvent(name, mask)
log.Printf("### %+v", ev)
offset += unix.SizeofInotifyEvent + nameLen
}
}
}
type Event struct {
name string
op string
}
func (e Event) String() string {
return fmt.Sprintf("%10s | %s", e.op, e.name)
}
func newEvent(name string, mask uint32) Event {
e := Event{name: name}
if mask&unix.IN_CREATE == unix.IN_CREATE || mask&unix.IN_MOVED_TO == unix.IN_MOVED_TO {
e.op = "CREATE"
}
if mask&unix.IN_DELETE_SELF == unix.IN_DELETE_SELF || mask&unix.IN_DELETE == unix.IN_DELETE {
e.op = "REMOVE"
}
if mask&unix.IN_MODIFY == unix.IN_MODIFY {
e.op = "WRITE"
}
if mask&unix.IN_MOVE_SELF == unix.IN_MOVE_SELF || mask&unix.IN_MOVED_FROM == unix.IN_MOVED_FROM {
e.op = "RENAME"
}
if mask&unix.IN_ATTRIB == unix.IN_ATTRIB {
e.op = "CHMOD"
}
return e
}