mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 11:44:51 +00:00
restructure inotify package and add some tests
This commit is contained in:
@@ -6,26 +6,12 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
type InotifyWatcher struct{}
|
||||
|
||||
func NewInotifyWatcher() *InotifyWatcher {
|
||||
return &InotifyWatcher{}
|
||||
}
|
||||
|
||||
func (i *InotifyWatcher) Setup(rdirs, dirs []string) (chan struct{}, chan string, error) {
|
||||
triggerCh := make(chan struct{})
|
||||
fsEventCh := make(chan string)
|
||||
return triggerCh, fsEventCh, nil
|
||||
}
|
||||
|
||||
type Inotify struct {
|
||||
fd int
|
||||
watchers map[int]*watcher
|
||||
ping chan struct{}
|
||||
paused bool
|
||||
}
|
||||
|
||||
func NewInotify(ping chan struct{}, print bool) (*Inotify, error) {
|
||||
func NewInotify() (*Inotify, error) {
|
||||
fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC)
|
||||
if fd == -1 {
|
||||
return nil, fmt.Errorf("Can't init inotify: %d", errno)
|
||||
@@ -34,16 +20,13 @@ func NewInotify(ping chan struct{}, print bool) (*Inotify, error) {
|
||||
i := &Inotify{
|
||||
fd: fd,
|
||||
watchers: make(map[int]*watcher),
|
||||
ping: ping,
|
||||
paused: false,
|
||||
}
|
||||
go watch(i, print)
|
||||
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (i *Inotify) Watch(dir string) error {
|
||||
w, err := newWatcher(i.fd, dir, i.ping)
|
||||
w, err := newWatcher(i.fd, dir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating watcher: %v", err)
|
||||
}
|
||||
@@ -58,14 +41,6 @@ func (i *Inotify) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Inotify) Pause() {
|
||||
i.paused = true
|
||||
}
|
||||
|
||||
func (i *Inotify) UnPause() {
|
||||
i.paused = false
|
||||
}
|
||||
|
||||
func (i *Inotify) NumWatchers() int {
|
||||
return len(i.watchers)
|
||||
}
|
||||
@@ -81,24 +56,3 @@ func (i *Inotify) String() string {
|
||||
return fmt.Sprintf("Watching %d directories", len(i.watchers))
|
||||
}
|
||||
}
|
||||
|
||||
type bufRead struct {
|
||||
n int
|
||||
buf []byte
|
||||
}
|
||||
|
||||
func watch(i *Inotify, print bool) {
|
||||
buf := make([]byte, 5*unix.SizeofInotifyEvent)
|
||||
buffers := make(chan bufRead)
|
||||
go eventLogger(i, buffers, print)
|
||||
for {
|
||||
n, _ := unix.Read(i.fd, buf)
|
||||
if !i.paused {
|
||||
i.ping <- struct{}{}
|
||||
}
|
||||
buffers <- bufRead{
|
||||
n: n,
|
||||
buf: buf,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user