-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsevents.go
173 lines (145 loc) · 3.71 KB
/
fsevents.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//go:build darwin
// +build darwin
package rnotify
import (
"log"
"math"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fsnotify/fsevents"
"github.com/fsnotify/fsnotify"
)
const unsupportedOp = math.MaxUint32
// Watcher watches files and directories, delivering events to a channel.
type Watcher struct {
Events chan fsnotify.Event
Errors chan error
mu sync.Mutex
watches map[string]*fsevents.EventStream
ignore map[string]struct{}
}
// NewWatcher builds a new watcher.
func NewWatcher() (*Watcher, error) {
w := &Watcher{
Events: make(chan fsnotify.Event),
Errors: make(chan error),
watches: make(map[string]*fsevents.EventStream),
ignore: map[string]struct{}{},
}
return w, nil
}
// Add starts watching the directory (recursively).
func (w *Watcher) Add(name string) error {
absPath, err := filepath.EvalSymlinks(name)
if err != nil {
return err
}
dev, err := fsevents.DeviceForPath(absPath)
if err != nil {
return err
}
es := &fsevents.EventStream{
Paths: []string{absPath},
Latency: 500 * time.Millisecond,
Device: dev,
Flags: fsevents.FileEvents}
w.mu.Lock()
w.watches[name] = es
w.mu.Unlock()
es.Start()
ec := es.Events
go func() {
for msg := range ec {
for _, event := range msg {
skip := false
for ignorePath := range w.ignore {
if strings.Contains(event.Path, ignorePath) {
skip = true
break
}
}
op := w.translateEvent(event.Flags)
if !skip && op != unsupportedOp {
// FIXME(y-yagi) Is '/' really need?
e := fsnotify.Event{Name: "/" + event.Path, Op: op}
w.Events <- e
}
}
}
}()
return nil
}
// Close stops watching.
func (w *Watcher) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
if len(w.watches) == 0 {
return nil
}
for path, es := range w.watches {
es.Stop()
delete(w.watches, path)
}
return nil
}
// Ignore specifies directories to ignore.
func (w *Watcher) Ignore(paths []string) {
for _, path := range paths {
w.ignore[path] = struct{}{}
}
}
func (w *Watcher) translateEvent(event fsevents.EventFlags) fsnotify.Op {
if event&fsevents.ItemRemoved == fsevents.ItemRemoved {
return fsnotify.Remove
}
if event&fsevents.ItemRenamed == fsevents.ItemRenamed {
return fsnotify.Rename
}
if event&fsevents.ItemChangeOwner == fsevents.ItemChangeOwner {
return fsnotify.Chmod
}
if event&fsevents.ItemModified == fsevents.ItemModified {
if event&fsevents.ItemCreated == fsevents.ItemCreated {
return fsnotify.Create
}
return fsnotify.Write
}
if event&fsevents.ItemCreated == fsevents.ItemCreated {
return fsnotify.Create
}
return unsupportedOp
}
//nolint:unused
var noteDescription = map[fsevents.EventFlags]string{
fsevents.MustScanSubDirs: "MustScanSubdirs",
fsevents.UserDropped: "UserDropped",
fsevents.KernelDropped: "KernelDropped",
fsevents.EventIDsWrapped: "EventIDsWrapped",
fsevents.HistoryDone: "HistoryDone",
fsevents.RootChanged: "RootChanged",
fsevents.Mount: "Mount",
fsevents.Unmount: "Unmount",
fsevents.ItemCreated: "Created",
fsevents.ItemRemoved: "Removed",
fsevents.ItemInodeMetaMod: "InodeMetaMod",
fsevents.ItemRenamed: "Renamed",
fsevents.ItemModified: "Modified",
fsevents.ItemFinderInfoMod: "FinderInfoMod",
fsevents.ItemChangeOwner: "ChangeOwner",
fsevents.ItemXattrMod: "XAttrMod",
fsevents.ItemIsFile: "IsFile",
fsevents.ItemIsDir: "IsDir",
fsevents.ItemIsSymlink: "IsSymLink",
}
//nolint:unused
func (w *Watcher) logEvent(event fsevents.Event) {
note := ""
for bit, description := range noteDescription {
if event.Flags&bit == bit {
note += description + " "
}
}
log.Printf("EventID: %d Path: %s Flags: %s", event.ID, event.Path, note)
}