pass through arguments from command line

This commit is contained in:
Dominic Breuker
2018-02-13 21:41:41 +01:00
parent 2ed0991b9c
commit 91381d10a0
6 changed files with 60 additions and 23 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log"
"strconv"
"time"
"unsafe"
"golang.org/x/sys/unix"
@@ -55,7 +56,13 @@ func newEvent(name string, mask uint32) Event {
return e
}
func eventLogger(i *Inotify, buffers chan bufRead) {
func eventLogger(i *Inotify, buffers chan bufRead, print bool) {
// enable printing only after delay since setting up watchers causes flood of events
printEnabled := false
go func() {
<-time.After(1 * time.Second)
printEnabled = print
}()
for bf := range buffers {
n := bf.n
buf := bf.buf
@@ -82,7 +89,9 @@ func eventLogger(i *Inotify, buffers chan bufRead) {
}
ev := newEvent(name, sys.Mask)
log.Printf("\x1b[32;1mFS: %+v\x1b[0m", ev)
if printEnabled {
log.Printf("\x1b[32;1mFS: %+v\x1b[0m", ev)
}
}
}
}

View File

@@ -13,7 +13,7 @@ type Inotify struct {
paused bool
}
func NewInotify(ping chan struct{}) (*Inotify, error) {
func NewInotify(ping chan struct{}, print bool) (*Inotify, error) {
fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC)
if fd == -1 {
return nil, fmt.Errorf("Can't init inotify: %d", errno)
@@ -25,15 +25,11 @@ func NewInotify(ping chan struct{}) (*Inotify, error) {
ping: ping,
paused: false,
}
go watch(i)
go watch(i, print)
return i, nil
}
func (i *Inotify) Start() {
go watch(i)
}
func (i *Inotify) Watch(dir string) error {
w, err := newWatcher(i.fd, dir, i.ping)
if err != nil {
@@ -79,10 +75,10 @@ type bufRead struct {
buf []byte
}
func watch(i *Inotify) {
func watch(i *Inotify, print bool) {
buf := make([]byte, 5*unix.SizeofInotifyEvent)
buffers := make(chan bufRead)
go eventLogger(i, buffers)
go eventLogger(i, buffers, print)
for {
n, _ := unix.Read(i.fd, buf)
if !i.paused {