improve console output

This commit is contained in:
Dominic Breuker
2018-02-12 23:39:04 +01:00
parent 9bc66835a6
commit 56f706b7e2
6 changed files with 200 additions and 156 deletions

88
internal/inotify/event.go Normal file
View File

@@ -0,0 +1,88 @@
package inotify
import (
"bytes"
"fmt"
"log"
"strconv"
"unsafe"
"golang.org/x/sys/unix"
)
var InotifyEvents = map[uint32]string{
unix.IN_ACCESS: "ACCESS",
unix.IN_ATTRIB: "ATTRIB",
unix.IN_CLOSE_NOWRITE: "CLOSE_NOWRITE",
unix.IN_CLOSE_WRITE: "CLOSE_WRITE",
unix.IN_CREATE: "CREATE",
unix.IN_DELETE: "DELETE",
unix.IN_DELETE_SELF: "DELETE_SELF",
unix.IN_MODIFY: "MODIFY",
unix.IN_MOVED_FROM: "MOVED_FROM",
unix.IN_MOVED_TO: "MOVED_TO",
unix.IN_MOVE_SELF: "MOVE_SELF",
unix.IN_OPEN: "OPEN",
(unix.IN_ACCESS | unix.IN_ISDIR): "ACCESS DIR",
(unix.IN_ATTRIB | unix.IN_ISDIR): "ATTRIB DIR",
(unix.IN_CLOSE_NOWRITE | unix.IN_ISDIR): "CLOSE_NOWRITE DIR",
(unix.IN_CLOSE_WRITE | unix.IN_ISDIR): "CLOSE_WRITE DIR",
(unix.IN_CREATE | unix.IN_ISDIR): "CREATE DIR",
(unix.IN_DELETE | unix.IN_ISDIR): "DELETE DIR",
(unix.IN_DELETE_SELF | unix.IN_ISDIR): "DELETE_SELF DIR",
(unix.IN_MODIFY | unix.IN_ISDIR): "MODIFY DIR",
(unix.IN_MOVED_FROM | unix.IN_ISDIR): "MOVED_FROM DIR",
(unix.IN_MOVE_SELF | unix.IN_ISDIR): "MODE_SELF DIR",
(unix.IN_OPEN | unix.IN_ISDIR): "OPEN DIR",
}
type Event struct {
name string
op string
}
func (e Event) String() string {
return fmt.Sprintf("%20s | %s", e.op, e.name)
}
func newEvent(name string, mask uint32) Event {
e := Event{name: name}
op, ok := InotifyEvents[mask]
if !ok {
op = strconv.FormatInt(int64(mask), 2)
}
e.op = op
return e
}
func eventLogger(i *Inotify, buffers chan bufRead) {
for bf := range buffers {
n := bf.n
buf := bf.buf
if n < unix.SizeofInotifyEvent {
// incomplete or erroneous read
continue
}
var ptr uint32
var name string
for ptr <= uint32(n-unix.SizeofInotifyEvent) {
sys := (*unix.InotifyEvent)(unsafe.Pointer(&buf[ptr]))
ptr += unix.SizeofInotifyEvent
watcher, ok := i.watchers[int(sys.Wd)]
if !ok {
continue
}
name = watcher.dir + "/"
if sys.Len > 0 && len(buf) >= int(ptr+sys.Len) {
name += string(bytes.TrimRight(buf[ptr:ptr+sys.Len], "\x00"))
ptr += sys.Len
}
ev := newEvent(name, sys.Mask)
log.Printf("\x1b[32;1mFS: %+v\x1b[0m", ev)
}
}
}

View File

@@ -2,9 +2,6 @@ package inotify
import (
"fmt"
"log"
"strings"
"unsafe"
"golang.org/x/sys/unix"
)
@@ -57,6 +54,10 @@ func (i *Inotify) UnPause() {
i.paused = false
}
func (i *Inotify) NumWatchers() int {
return len(i.watchers)
}
func (i *Inotify) String() string {
if len(i.watchers) < 20 {
dirs := make([]string, 0)
@@ -75,9 +76,9 @@ type bufRead struct {
}
func watch(i *Inotify) {
buf := make([]byte, 20*unix.SizeofInotifyEvent)
buf := make([]byte, 5*unix.SizeofInotifyEvent)
buffers := make(chan bufRead)
go verboseWatcher(i, buffers)
go eventLogger(i, buffers)
for {
n, _ := unix.Read(i.fd, buf)
if !i.paused {
@@ -89,73 +90,3 @@ func watch(i *Inotify) {
}
}
}
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
}