1
0

Renamed FSWatcher to InotifyWatcher.

This commit is contained in:
Alexander Andreev 2022-03-07 23:12:02 +04:00
parent e32926dacf
commit f2cd801b05
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F
2 changed files with 18 additions and 18 deletions

View File

@ -85,7 +85,7 @@ func main() {
}
defer logDownload.Close()
watcha, err := watcher.NewFSWatcher()
watcha, err := watcher.NewInotifyWatcher()
if err != nil {
logErr.Fatalln(err)
}

View File

@ -11,42 +11,42 @@ const CrDelMask uint32 = syscall.IN_CREATE | syscall.IN_DELETE
const inotifyCount = 16
type FSWatcher struct {
type InotifyWatcher struct {
fd int
wds []int
closed bool
}
func NewFSWatcher() (fsw *FSWatcher, err error) {
fsw = &FSWatcher{closed: false}
func NewInotifyWatcher() (w *InotifyWatcher, err error) {
w = &InotifyWatcher{closed: false}
fsw.fd, err = syscall.InotifyInit()
w.fd, err = syscall.InotifyInit()
if err != nil {
return nil, errors.Wrap(err, "failed to initialise inotify watcher")
}
fsw.wds = make([]int, 0)
w.wds = make([]int, 0)
return fsw, nil
return w, nil
}
func (fsw *FSWatcher) AddWatch(path string, mask uint32) error {
wd, err := syscall.InotifyAddWatch(fsw.fd, path, mask)
func (w *InotifyWatcher) AddWatch(path string, mask uint32) error {
wd, err := syscall.InotifyAddWatch(w.fd, path, mask)
if err != nil {
return errors.Wrapf(err, "failed to set %s on watch", path)
}
fsw.wds = append(fsw.wds, wd)
w.wds = append(w.wds, wd)
return nil
}
// WatchForMask checking for events from mask and returns inotify mask to channel.
func (fsw *FSWatcher) WatchForMask(fired chan uint32, mask uint32) {
func (w *InotifyWatcher) WatchForMask(fired chan uint32, mask uint32) {
go func() {
for !fsw.closed {
for !w.closed {
buffer := make([]byte, syscall.SizeofInotifyEvent*inotifyCount)
n, err := syscall.Read(fsw.fd, buffer)
n, err := syscall.Read(w.fd, buffer)
if err != nil {
break
}
@ -65,18 +65,18 @@ func (fsw *FSWatcher) WatchForMask(fired chan uint32, mask uint32) {
}()
}
func (fsw *FSWatcher) Close() error {
for _, wd := range fsw.wds {
if _, err := syscall.InotifyRmWatch(fsw.fd, uint32(wd)); err != nil {
func (w *InotifyWatcher) Close() error {
for _, wd := range w.wds {
if _, err := syscall.InotifyRmWatch(w.fd, uint32(wd)); err != nil {
return err
}
}
if err := syscall.Close(fsw.fd); err != nil {
if err := syscall.Close(w.fd); err != nil {
return err
}
fsw.closed = true
w.closed = true
return nil
}