forked from fsnotify/fsevents
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fsevents.go
189 lines (155 loc) · 4.23 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// +build darwin
// Package fsevents provides file system notifications on macOS.
package fsevents
import (
"sync"
"syscall"
"time"
)
// CreateFlags for creating a New stream.
type CreateFlags uint32
// kFSEventStreamCreateFlag...
const (
// use CoreFoundation types instead of raw C types (disabled)
useCFTypes CreateFlags = 1 << iota
// NoDefer sends events on the leading edge (for interactive applications).
// By default events are delivered after latency seconds (for background tasks).
NoDefer
// WatchRoot for a change to occur to a directory along the path being watched.
WatchRoot
// IgnoreSelf doesn't send events triggered by the current process (macOS 10.6+).
IgnoreSelf
// FileEvents sends events about individual files, generating significantly
// more events (macOS 10.7+) than directory level notifications.
FileEvents
)
// EventFlags passed to the FSEventStreamCallback function.
type EventFlags uint32
// kFSEventStreamEventFlag...
const (
// MustScanSubDirs indicates that events were coalesced hierarchically.
MustScanSubDirs EventFlags = 1 << iota
// UserDropped or KernelDropped is set alongside MustScanSubDirs
// to help diagnose the problem.
UserDropped
KernelDropped
// EventIDsWrapped indicates the 64-bit event ID counter wrapped around.
EventIDsWrapped
// HistoryDone is a sentinel event when retrieving events sinceWhen.
HistoryDone
// RootChanged indicates a change to a directory along the path being watched.
RootChanged
// Mount for a volume mounted underneath the path being monitored.
Mount
// Unmount event occurs after a volume is unmounted.
Unmount
// The following flags are only set when using FileEvents.
ItemCreated
ItemRemoved
ItemInodeMetaMod
ItemRenamed
ItemModified
ItemFinderInfoMod
ItemChangeOwner
ItemXattrMod
ItemIsFile
ItemIsDir
ItemIsSymlink
)
// Event represents a single file system notification.
type Event struct {
Path string
Flags EventFlags
ID uint64
}
// DeviceForPath returns the device ID for the specified volume.
func DeviceForPath(path string) (int32, error) {
stat := syscall.Stat_t{}
if err := syscall.Lstat(path, &stat); err != nil {
return 0, err
}
return stat.Dev, nil
}
// EventStream is the primary interface to FSEvents
// You can provide your own event channel if you wish (or one will be
// created on Start).
//
// es := &EventStream{Paths: []string{"/tmp"}, Flags: 0}
// es.Start()
// es.Stop()
// ...
type EventStream struct {
stream FSEventStreamRef
rlref CFRunLoopRef
hasFinalizer bool
registryID uintptr
uuid string
Events chan []Event
Paths []string
Flags CreateFlags
EventID uint64
Resume bool
Latency time.Duration
// syscall represents this with an int32
Device int32
}
// eventStreamRegistry is a lookup table for EventStream references passed to
// cgo. In Go 1.6+ passing a Go pointer to a Go pointer to cgo is not allowed.
// To get around this issue, we pass only an integer.
type eventStreamRegistry struct {
sync.Mutex
m map[uintptr]*EventStream
lastID uintptr
}
var registry = eventStreamRegistry{m: map[uintptr]*EventStream{}}
func (r *eventStreamRegistry) Add(e *EventStream) uintptr {
r.Lock()
defer r.Unlock()
r.lastID++
r.m[r.lastID] = e
return r.lastID
}
func (r *eventStreamRegistry) Get(i uintptr) *EventStream {
r.Lock()
defer r.Unlock()
return r.m[i]
}
func (r *eventStreamRegistry) Delete(i uintptr) {
r.Lock()
defer r.Unlock()
delete(r.m, i)
}
// Start listening to an event stream.
func (es *EventStream) Start() {
if es.Events == nil {
es.Events = make(chan []Event)
}
// register eventstream in the local registry for later lookup
// in C callback
cbInfo := registry.Add(es)
es.registryID = cbInfo
if es.Device != 0 {
es.uuid = GetDeviceUUID(es.Device)
}
es.start(es.Paths, cbInfo)
}
// Flush events that have occurred but haven't been delivered.
func (es *EventStream) Flush(sync bool) {
flush(es.stream, sync)
}
// Stop listening to the event stream.
func (es *EventStream) Stop() {
if es.stream != nil {
stop(es.stream, es.rlref)
es.stream = nil
}
// Remove eventstream from the registry
registry.Delete(es.registryID)
es.registryID = 0
}
// Restart listening.
func (es *EventStream) Restart() {
es.Stop()
es.Resume = true
es.Start()
}