refactor inotify implementation

This commit is contained in:
Dominic Breuker
2018-02-26 09:29:06 +01:00
parent 6c79b80623
commit d38cb18712
9 changed files with 154 additions and 235 deletions

View File

@@ -1,40 +1,50 @@
package inotify
import (
"bytes"
"fmt"
"io/ioutil"
"strconv"
"strings"
"unsafe"
"golang.org/x/sys/unix"
)
type InotifySyscalls interface {
Init() (int, error)
AddWatch(int, string) (int, error)
Close(int) error
}
const maximumWatchersFile = "/proc/sys/fs/inotify/max_user_watches"
type Inotify struct {
FD int
Watchers map[int]*Watcher
sys InotifySyscalls
}
func NewInotify(isys InotifySyscalls) (*Inotify, error) {
fd, err := isys.Init()
if err != nil {
return nil, fmt.Errorf("initializing inotify: %v", err)
type Watcher struct {
WD int
Dir string
}
type Event struct {
Name string
Op string
}
func NewInotify() (*Inotify, error) {
fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC)
if fd < 0 {
return nil, fmt.Errorf("initializing inotify: errno: %d", errno)
}
i := &Inotify{
FD: fd,
Watchers: make(map[int]*Watcher),
sys: isys,
}
return i, nil
}
func (i *Inotify) Watch(dir string) error {
wd, err := i.sys.AddWatch(i.FD, dir)
if err != nil {
return fmt.Errorf("adding watcher on %s: %v", dir, err)
wd, errno := unix.InotifyAddWatch(i.FD, dir, unix.IN_ALL_EVENTS)
if wd < 0 {
return fmt.Errorf("adding watch: errno: %d", errno)
}
i.Watchers[wd] = &Watcher{
WD: wd,
@@ -43,9 +53,45 @@ func (i *Inotify) Watch(dir string) error {
return nil
}
func (i *Inotify) Read(buf []byte) (int, error) {
n, errno := unix.Read(i.FD, buf)
if n < 0 {
return n, fmt.Errorf("reading from inotify fd %d: errno: %d", i.FD, errno)
}
return n, nil
}
func (i *Inotify) ParseNextEvent(buf []byte) (*Event, uint32, error) {
n := len(buf)
if n < unix.SizeofInotifyEvent {
return nil, uint32(n), fmt.Errorf("incomplete read: n=%d", n)
}
sys := (*unix.InotifyEvent)(unsafe.Pointer(&buf[0]))
offset := unix.SizeofInotifyEvent + sys.Len
watcher, ok := i.Watchers[int(sys.Wd)]
if !ok {
return nil, offset, fmt.Errorf("unknown watcher ID: %d", sys.Wd)
}
name := watcher.Dir + "/"
if sys.Len > 0 && len(buf) >= int(offset) {
name += string(bytes.TrimRight(buf[unix.SizeofInotifyEvent:offset], "\x00"))
}
op, ok := InotifyEvents[sys.Mask]
if !ok {
op = strconv.FormatInt(int64(sys.Mask), 2)
}
return &Event{
Name: name,
Op: op,
}, offset, nil
}
func (i *Inotify) Close() error {
if err := i.sys.Close(i.FD); err != nil {
return fmt.Errorf("closing inotify file descriptor: %v", err)
if err := unix.Close(i.FD); err != nil {
return fmt.Errorf("closing inotify fd: %v", err)
}
return nil
}
@@ -65,3 +111,18 @@ func (i *Inotify) String() string {
return fmt.Sprintf("Watching %d directories", len(i.Watchers))
}
}
func GetMaxWatchers() (int, error) {
b, err := ioutil.ReadFile(maximumWatchersFile)
if err != nil {
return 0, fmt.Errorf("reading from %s: %v", maximumWatchersFile, err)
}
s := strings.TrimSpace(string(b))
m, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("converting to integer: %v", err)
}
return m, nil
}