refactor event parsing

This commit is contained in:
Dominic Breuker
2018-03-12 08:47:29 +01:00
parent cc846bdad1
commit f0474514f0

View File

@@ -97,19 +97,26 @@ func (i *Inotify) ParseNextEvent(buf []byte) (*Event, uint32, error) {
return nil, offset, fmt.Errorf("unknown watcher ID: %d", sys.Wd) return nil, offset, fmt.Errorf("unknown watcher ID: %d", sys.Wd)
} }
return &Event{
Name: getEventName(watcher, sys, buf, offset),
Op: getEventOp(sys),
}, offset, nil
}
func getEventName(watcher *Watcher, sys *unix.InotifyEvent, buf []byte, offset uint32) string {
name := watcher.Dir + "/" name := watcher.Dir + "/"
if sys.Len > 0 && len(buf) >= int(offset) { if sys.Len > 0 && len(buf) >= int(offset) {
name += string(bytes.TrimRight(buf[unix.SizeofInotifyEvent:offset], "\x00")) name += string(bytes.TrimRight(buf[unix.SizeofInotifyEvent:offset], "\x00"))
} }
return name
}
func getEventOp(sys *unix.InotifyEvent) string {
op, ok := InotifyEvents[sys.Mask] op, ok := InotifyEvents[sys.Mask]
if !ok { if !ok {
op = strconv.FormatInt(int64(sys.Mask), 2) op = strconv.FormatInt(int64(sys.Mask), 2)
} }
return op
return &Event{
Name: name,
Op: op,
}, offset, nil
} }
func (i *Inotify) Close() error { func (i *Inotify) Close() error {