-
Notifications
You must be signed in to change notification settings - Fork 3
/
notify_linux.go
82 lines (65 loc) · 1.36 KB
/
notify_linux.go
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
79
80
81
82
// +build linux
package main
import (
fsnotify "gopkg.in/fsnotify.v1"
"log"
"os"
)
type event fsnotify.Event
func (ev event) handle(c *crawler) {
if ev.Name == "" {
return
}
info, err := os.Stat(ev.Name)
if err != nil || info.IsDir() {
return
}
file, err := makeMailFile(ev.Name)
if err != nil {
log.Print(ev.Name, ": error parsing file ", err)
return
}
if ev.Op&fsnotify.Remove == fsnotify.Remove ||
ev.Op&fsnotify.Rename == fsnotify.Rename {
files := make([]mailFile, 1)
files[0] = file
c.cache.removeCh <- files
c.remove(files)
}
if ev.Op&fsnotify.Rename == fsnotify.Rename ||
ev.Op&fsnotify.Write == fsnotify.Write ||
ev.Op&fsnotify.Create == fsnotify.Create {
c.markAdded(file, info)
}
}
type notify struct {
events chan *event
watcher *fsnotify.Watcher
}
func newNotify(dir string) (*notify, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
if err := watcher.Add(dir); err != nil {
return nil, err
}
n := ¬ify{
events: make(chan *event),
watcher: watcher,
}
// Listen to events forever, no need to close this watcher.
go func() {
for ev := range n.watcher.Events {
ev := event(ev)
n.events <- &ev
}
}()
return n, nil
}
func (i *notify) eventsChannel() chan *event {
return i.events
}
func (i *notify) errorsChannel() chan error {
return i.watcher.Errors
}