mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 11:44:51 +00:00
try some more tsting
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package inotify
|
||||
package fswatcher
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strconv"
|
||||
"unsafe"
|
||||
|
||||
"github.com/dominicbreuker/pspy/internal/fswatcher/inotify"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
@@ -35,7 +36,7 @@ var InotifyEvents = map[uint32]string{
|
||||
(unix.IN_OPEN | unix.IN_ISDIR): "OPEN DIR",
|
||||
}
|
||||
|
||||
func parseEvents(i *Inotify, dataCh chan []byte, eventCh chan string, errCh chan error) {
|
||||
func parseEvents(i *inotify.Inotify, dataCh chan []byte, eventCh chan string, errCh chan error) {
|
||||
for buf := range dataCh {
|
||||
n := len(buf)
|
||||
if n < unix.SizeofInotifyEvent {
|
||||
@@ -49,12 +50,12 @@ func parseEvents(i *Inotify, dataCh chan []byte, eventCh chan string, errCh chan
|
||||
sys := (*unix.InotifyEvent)(unsafe.Pointer(&buf[ptr]))
|
||||
ptr += unix.SizeofInotifyEvent
|
||||
|
||||
watcher, ok := i.watchers[int(sys.Wd)]
|
||||
watcher, ok := i.Watchers[int(sys.Wd)]
|
||||
if !ok {
|
||||
errCh <- fmt.Errorf("Inotify event parser: unknown watcher ID: %d", sys.Wd)
|
||||
continue
|
||||
}
|
||||
name = watcher.dir + "/"
|
||||
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
|
||||
@@ -1,4 +1,4 @@
|
||||
package inotify
|
||||
package fswatcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
67
internal/fswatcher/inotify/inotify.go
Normal file
67
internal/fswatcher/inotify/inotify.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package inotify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type InotifySyscalls interface {
|
||||
Init() (int, error)
|
||||
AddWatch(int, string) (int, error)
|
||||
Close(int) error
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
i.Watchers[wd] = &Watcher{
|
||||
WD: wd,
|
||||
Dir: dir,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Inotify) Close() error {
|
||||
if err := i.sys.Close(i.FD); err != nil {
|
||||
return fmt.Errorf("closing inotify file descriptor: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Inotify) NumWatchers() int {
|
||||
return len(i.Watchers)
|
||||
}
|
||||
|
||||
func (i *Inotify) String() string {
|
||||
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))
|
||||
}
|
||||
}
|
||||
49
internal/fswatcher/inotify/inotify_test.go
Normal file
49
internal/fswatcher/inotify/inotify_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package inotify
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewInotify(t *testing.T) {
|
||||
mis := &MockInotifySyscalls{fd: 1}
|
||||
|
||||
i, err := NewInotify(mis)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error")
|
||||
}
|
||||
if i.FD != mis.fd {
|
||||
t.Fatalf("Did not set FD of inotify object")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInotifyError(t *testing.T) {
|
||||
mis := &MockInotifySyscalls{fd: -1}
|
||||
|
||||
_, err := NewInotify(mis)
|
||||
if err == nil || err.Error() != "initializing inotify: syscall error" {
|
||||
t.Fatalf("Expected syscall error but did not get: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// mock
|
||||
|
||||
type MockInotifySyscalls struct {
|
||||
fd int
|
||||
}
|
||||
|
||||
func (mis *MockInotifySyscalls) Init() (int, error) {
|
||||
if mis.fd >= 0 {
|
||||
return mis.fd, nil
|
||||
} else {
|
||||
return -1, errors.New("syscall error")
|
||||
}
|
||||
}
|
||||
|
||||
func (mis *MockInotifySyscalls) AddWatch(fd int, dir string) (int, error) {
|
||||
return 2, nil
|
||||
}
|
||||
|
||||
func (mis *MockInotifySyscalls) Close(fd int) error {
|
||||
return nil
|
||||
}
|
||||
36
internal/fswatcher/inotify/sys/syscalls.go
Normal file
36
internal/fswatcher/inotify/sys/syscalls.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// +build linux
|
||||
|
||||
package sys
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const events = unix.IN_ALL_EVENTS
|
||||
|
||||
type InotifySyscallsUNIX struct{}
|
||||
|
||||
func (isu *InotifySyscallsUNIX) Init() (int, error) {
|
||||
fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC)
|
||||
if fd < 0 {
|
||||
return fd, fmt.Errorf("errno: %d", errno)
|
||||
}
|
||||
return fd, nil
|
||||
}
|
||||
|
||||
func (isu *InotifySyscallsUNIX) AddWatch(fd int, dir string) (int, error) {
|
||||
wd, errno := unix.InotifyAddWatch(fd, dir, events)
|
||||
if wd < 0 {
|
||||
return wd, fmt.Errorf("errno: %d", errno)
|
||||
}
|
||||
return wd, nil
|
||||
}
|
||||
|
||||
func (isu *InotifySyscallsUNIX) Close(fd int) error {
|
||||
if err := unix.Close(fd); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
45
internal/fswatcher/inotify/sys/syscalls_test.go
Normal file
45
internal/fswatcher/inotify/sys/syscalls_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// +build linux
|
||||
|
||||
package sys
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSyscalls(t *testing.T) {
|
||||
is := &InotifySyscallsUNIX{}
|
||||
|
||||
fd, err := is.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error for inotify init: %v", err)
|
||||
}
|
||||
|
||||
_, err = is.AddWatch(fd, "testdata")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error adding watch to dir 'testdata': %v", err)
|
||||
}
|
||||
|
||||
err = is.Close(fd)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error closing inotify: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyscallsError(t *testing.T) {
|
||||
is := &InotifySyscallsUNIX{}
|
||||
|
||||
fd, err := is.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error for inotify init: %v", err)
|
||||
}
|
||||
|
||||
_, err = is.AddWatch(fd, "non-existing-dir")
|
||||
if err == nil || err.Error() != "errno: 2" {
|
||||
t.Fatalf("Expected errno 2 for non-existing-dir but got: %v", err)
|
||||
}
|
||||
|
||||
err = is.Close(fd)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error closing inotify: %v", err)
|
||||
}
|
||||
}
|
||||
30
internal/fswatcher/inotify/watcher.go
Normal file
30
internal/fswatcher/inotify/watcher.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package inotify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const maximumWatchersFile = "/proc/sys/fs/inotify/max_user_watches"
|
||||
|
||||
type Watcher struct {
|
||||
WD int
|
||||
Dir string
|
||||
}
|
||||
|
||||
func GetLimit() (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
|
||||
}
|
||||
@@ -1,16 +1,17 @@
|
||||
package inotify
|
||||
package fswatcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/dominicbreuker/pspy/internal/fswatcher/inotify"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func Observe(i *Inotify, triggerCh chan struct{}, dataCh chan []byte, errCh chan error) {
|
||||
func Observe(i *inotify.Inotify, triggerCh chan struct{}, dataCh chan []byte, errCh chan error) {
|
||||
buf := make([]byte, 5*unix.SizeofInotifyEvent)
|
||||
|
||||
for {
|
||||
n, errno := unix.Read(i.fd, buf)
|
||||
n, errno := unix.Read(i.FD, buf)
|
||||
if n == -1 {
|
||||
errCh <- fmt.Errorf("reading from inotify fd: errno: %d", errno)
|
||||
return
|
||||
@@ -1,13 +1,15 @@
|
||||
package inotify
|
||||
package fswatcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/dominicbreuker/pspy/internal/inotify/walker"
|
||||
"github.com/dominicbreuker/pspy/internal/fswatcher/inotify"
|
||||
isys "github.com/dominicbreuker/pspy/internal/fswatcher/inotify/sys"
|
||||
"github.com/dominicbreuker/pspy/internal/fswatcher/walker"
|
||||
)
|
||||
|
||||
type InotifyWatcher struct {
|
||||
i *Inotify
|
||||
i *inotify.Inotify
|
||||
}
|
||||
|
||||
func (iw *InotifyWatcher) Close() {
|
||||
@@ -15,7 +17,7 @@ func (iw *InotifyWatcher) Close() {
|
||||
}
|
||||
|
||||
func NewInotifyWatcher() (*InotifyWatcher, error) {
|
||||
i, err := NewInotify()
|
||||
i, err := inotify.NewInotify(&isys.InotifySyscallsUNIX{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("setting up inotify: %v", err)
|
||||
}
|
||||
@@ -48,8 +50,8 @@ func (iw *InotifyWatcher) Setup(rdirs, dirs []string, errCh chan error) (chan st
|
||||
return triggerCh, eventCh, nil
|
||||
}
|
||||
|
||||
func addWatchers(dir string, depth int, i *Inotify, maxWatchers int, errCh chan error) {
|
||||
dirCh, doneCh := walker.Walk(dir, depth, errCh)
|
||||
func addWatchers(dir string, depth int, i *inotify.Inotify, maxWatchers int, errCh chan error) {
|
||||
dirCh, walkErrCh, doneCh := walker.Walk(dir, depth)
|
||||
loop:
|
||||
for {
|
||||
if maxWatchers > 0 && i.NumWatchers() >= maxWatchers {
|
||||
@@ -57,6 +59,8 @@ loop:
|
||||
break loop
|
||||
}
|
||||
select {
|
||||
case err := <-walkErrCh:
|
||||
errCh <- fmt.Errorf("adding inotift watchers: %v", err)
|
||||
case dir, ok := <-dirCh:
|
||||
if !ok {
|
||||
break loop
|
||||
@@ -9,24 +9,25 @@ import (
|
||||
|
||||
const maxInt = int(^uint(0) >> 1)
|
||||
|
||||
func Walk(root string, depth int, errCh chan error) (dirCh chan string, doneCh chan struct{}) {
|
||||
func Walk(root string, depth int) (dirCh chan string, errCh chan error, doneCh chan struct{}) {
|
||||
if depth < 0 {
|
||||
depth = maxInt
|
||||
}
|
||||
dirCh = make(chan string)
|
||||
errCh = make(chan error)
|
||||
doneCh = make(chan struct{})
|
||||
|
||||
go func() {
|
||||
defer close(dirCh)
|
||||
descent(root, depth-1, dirCh, errCh, doneCh)
|
||||
close(dirCh)
|
||||
}()
|
||||
return dirCh, doneCh
|
||||
return dirCh, errCh, doneCh
|
||||
}
|
||||
|
||||
func descent(dir string, depth int, dirCh chan string, errCh chan error, doneCh chan struct{}) {
|
||||
_, err := os.Stat(dir)
|
||||
if err != nil {
|
||||
errCh <- fmt.Errorf("Can't walk directory %s: %v", dir, err)
|
||||
errCh <- fmt.Errorf("visiting %s: %v", dir, err)
|
||||
return
|
||||
}
|
||||
select {
|
||||
@@ -35,12 +35,12 @@ func TestWalk(t *testing.T) {
|
||||
"testdata/subdir",
|
||||
"testdata/subdir/subsubdir",
|
||||
}, errs: []string{}},
|
||||
{root: "testdata/non-existing-dir", depth: 1, errCh: newErrCh(), result: []string{}, errs: []string{"Can't walk directory testdata/non-existing-dir"}},
|
||||
{root: "testdata/non-existing-dir", depth: 1, errCh: newErrCh(), result: []string{}, errs: []string{"visiting testdata/non-existing-dir"}},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
dirCh, doneCh := Walk(tt.root, tt.depth, tt.errCh)
|
||||
dirs, errs := getAllDirsAndErrors(dirCh, tt.errCh)
|
||||
dirCh, errCh, doneCh := Walk(tt.root, tt.depth)
|
||||
dirs, errs := getAllDirsAndErrors(dirCh, errCh)
|
||||
|
||||
if !reflect.DeepEqual(dirs, tt.result) {
|
||||
t.Fatalf("[%d] Wrong number of dirs found: %+v", i, dirs)
|
||||
@@ -59,20 +59,20 @@ func getAllDirsAndErrors(dirCh chan string, errCh chan error) ([]string, []strin
|
||||
|
||||
doneDirsCh := make(chan struct{})
|
||||
go func() {
|
||||
defer close(doneDirsCh)
|
||||
defer close(errCh)
|
||||
for d := range dirCh {
|
||||
dirs = append(dirs, d)
|
||||
}
|
||||
close(errCh)
|
||||
close(doneDirsCh)
|
||||
}()
|
||||
|
||||
doneErrsCh := make(chan struct{})
|
||||
go func() {
|
||||
defer close(doneErrsCh)
|
||||
for err := range errCh {
|
||||
tokens := strings.SplitN(err.Error(), ":", 2)
|
||||
errs = append(errs, tokens[0])
|
||||
}
|
||||
close(doneErrsCh)
|
||||
}()
|
||||
<-doneDirsCh
|
||||
<-doneErrsCh
|
||||
@@ -1,4 +1,4 @@
|
||||
package inotify
|
||||
package fswatcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
Reference in New Issue
Block a user