From 338b153aa1b30e202a3571e821294b29b2266bb5 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 8 Feb 2022 02:13:15 +0400 Subject: [PATCH] Inotify works when it is an array. Experimentally was found that 16 events is enough. --- pkg/watcher/linux.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/watcher/linux.go b/pkg/watcher/linux.go index a3b4e02..f12fd04 100644 --- a/pkg/watcher/linux.go +++ b/pkg/watcher/linux.go @@ -9,6 +9,8 @@ import ( const CrDelMask uint32 = syscall.IN_CREATE | syscall.IN_DELETE +const inotifyCount = 16 + type FSWatcher struct { fd int wds []int @@ -41,9 +43,9 @@ func (fsw *FSWatcher) AddWatch(path string, mask uint32) error { // WatchForMask checking for events from mask and returns inotify mask to channel. func (fsw *FSWatcher) WatchForMask(fired chan uint32, mask uint32) { - buffer := make([]byte, syscall.SizeofInotifyEvent) go func() { for !fsw.closed { + buffer := make([]byte, syscall.SizeofInotifyEvent*inotifyCount) n, err := syscall.Read(fsw.fd, buffer) if err != nil { break @@ -53,9 +55,11 @@ func (fsw *FSWatcher) WatchForMask(fired chan uint32, mask uint32) { continue } - event := (*syscall.InotifyEvent)(unsafe.Pointer(&buffer[0])) - if event.Mask&mask > 0 { - fired <- event.Mask + for offset := 0; offset < len(buffer); offset += syscall.SizeofInotifyEvent { + event := (*syscall.InotifyEvent)(unsafe.Pointer(&buffer[offset])) + if event.Mask&mask > 0 { + fired <- event.Mask + } } } }()