summaryrefslogtreecommitdiff
path: root/inotify/inutil/inotify_util.go
blob: 46146f548903ac786e34617d8d252016f25bd88f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package inutil

import (
        "inotify"
        "os"
        "syscall"
)

const (
	// Flags for the parameter of InotifyInit1().
	// These, oddly, appear to be 24-bit numbers.
	IN_CLOEXEC  = inotify.IN_CLOEXEC
)

type Watcher struct {
        Events <-chan inotify.Event
        events chan<- inotify.Event
        Errors <-chan error
        errors chan<- error
        in *inotify.Inotify
}

func WatcherInit() (*Watcher, error) {
        in, err := inotify.InotifyInit()
        return newWatcher(in, err)
}

func WatcherInit1(flags int) (*Watcher, error) {
        in, err := inotify.InotifyInit1(flags&^inotify.IN_NONBLOCK)
        return newWatcher(in, err)
}

func newWatcher(in *inotify.Inotify, err error) (*Watcher, error) {
        events := make(chan inotify.Event, 1)
        errors := make(chan error, 1)
        o := &Watcher{
                Events: events,
                events: events,
                Errors: errors,
                errors: errors,
                in: in,
        }
        go o.worker()
        return o, err
}

func (o *Watcher) AddWatch(path string, mask inotify.Mask) (inotify.Wd, error) {
        return o.in.AddWatch(path, mask);
}

func (o *Watcher) RmWatch(wd inotify.Wd) error {
        return o.in.RmWatch(wd);
}

func (o *Watcher) Close() {
        func() {
                defer recover()
                close(o.events)
                close(o.errors)
        }()
        go o.in.Close()
}

func (o *Watcher) worker() {
        defer recover()
        for {
		ev, err := o.in.Read();
		if ev.Wd >= 0 {
                        o.events <- ev
                }
                if err != nil {
                        if err.(*os.SyscallError).Err == syscall.EBADF {
                                o.Close()
                        }
                        o.errors <- err
		}
	}
}