1
0

Added comments for inotify watcher.

This commit is contained in:
Alexander Andreev 2022-05-25 18:51:01 +04:00
parent 024cd7ff5e
commit 7e689438ae
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -7,8 +7,12 @@ import (
"github.com/pkg/errors"
)
// CrDelMask predefined constant with create and delete events.
const CrDelMask uint32 = syscall.IN_CREATE | syscall.IN_DELETE
// inotifyCount is amount of InotifyEvent structures
// we read in WatchForMask(). 16 was chosen experimentally,
// with lesser amount events are often missing.
const inotifyCount = 16
type InotifyWatcher struct {
@ -17,6 +21,8 @@ type InotifyWatcher struct {
closed bool
}
// NewInotifyWatcher initialises inotify mechanism and returns
// an instance of InotifyWatcher.
func NewInotifyWatcher() (w *InotifyWatcher, err error) {
w = &InotifyWatcher{closed: false}
@ -30,6 +36,8 @@ func NewInotifyWatcher() (w *InotifyWatcher, err error) {
return w, nil
}
// AddWatch sets on watch file or directory specified in `path`
// for syscall.IN_XXXX events specified in `mask`.
func (w *InotifyWatcher) AddWatch(path string, mask uint32) error {
wd, err := syscall.InotifyAddWatch(w.fd, path, mask)
if err != nil {
@ -41,7 +49,8 @@ func (w *InotifyWatcher) AddWatch(path string, mask uint32) error {
return nil
}
// WatchForMask checking for events from mask and returns inotify mask to channel.
// WatchForMask checks for events specified in `mask` and sends them
// to channel `fired`. See `man inotify` for events.
func (w *InotifyWatcher) WatchForMask(fired chan uint32, mask uint32) {
go func() {
for !w.closed {
@ -65,6 +74,8 @@ func (w *InotifyWatcher) WatchForMask(fired chan uint32, mask uint32) {
}()
}
// Close removes all watchers, closes inotify descriptor and stops all
// WatchForMask() event loops.
func (w *InotifyWatcher) Close() error {
for _, wd := range w.wds {
if _, err := syscall.InotifyRmWatch(w.fd, uint32(wd)); err != nil {