@@ -36,6 +36,7 @@ import (
36
36
"unsafe"
37
37
)
38
38
39
+ // Event represents a notification
39
40
type Event struct {
40
41
Mask uint32 // Mask of events
41
42
Cookie uint32 // Unique cookie associating related events (for rename(2))
@@ -47,6 +48,7 @@ type watch struct {
47
48
flags uint32 // inotify flags of this watch (see inotify(7) for the list of valid flags)
48
49
}
49
50
51
+ // Watcher represents an inotify instance
50
52
type Watcher struct {
51
53
mu sync.Mutex
52
54
fd int // File descriptor (as returned by the inotify_init() syscall)
@@ -130,14 +132,14 @@ func (w *Watcher) AddWatch(path string, flags uint32) error {
130
132
131
133
// Watch adds path to the watched file set, watching all events.
132
134
func (w * Watcher ) Watch (path string ) error {
133
- return w .AddWatch (path , IN_ALL_EVENTS )
135
+ return w .AddWatch (path , InAllEvents )
134
136
}
135
137
136
138
// RemoveWatch removes path from the watched file set.
137
139
func (w * Watcher ) RemoveWatch (path string ) error {
138
140
watch , ok := w .watches [path ]
139
141
if ! ok {
140
- return errors . New ( fmt .Sprintf ("can't remove non-existent inotify watch for: %s" , path ) )
142
+ return fmt .Errorf ("can't remove non-existent inotify watch for: %s" , path )
141
143
}
142
144
success , errno := syscall .InotifyRmWatch (w .fd , watch .wd )
143
145
if success == - 1 {
@@ -186,7 +188,7 @@ func (w *Watcher) readEvents() {
186
188
continue
187
189
}
188
190
189
- var offset uint32 = 0
191
+ var offset uint32
190
192
// We don't know how many events we just read into the buffer
191
193
// While the offset points to at least one whole event...
192
194
for offset <= uint32 (n - syscall .SizeofInotifyEvent ) {
@@ -223,7 +225,7 @@ func (w *Watcher) readEvents() {
223
225
// String formats the event e in the form
224
226
// "filename: 0xEventMask = IN_ACCESS|IN_ATTRIB_|..."
225
227
func (e * Event ) String () string {
226
- var events string = ""
228
+ var events string
227
229
228
230
m := e .Mask
229
231
for _ , b := range eventBits {
@@ -249,58 +251,83 @@ const (
249
251
// IN_NONBLOCK uint32 = syscall.IN_NONBLOCK
250
252
251
253
// Options for AddWatch
252
- IN_DONT_FOLLOW uint32 = syscall .IN_DONT_FOLLOW
253
- IN_ONESHOT uint32 = syscall .IN_ONESHOT
254
- IN_ONLYDIR uint32 = syscall .IN_ONLYDIR
254
+
255
+ // InDontFollow : Don't dereference pathname if it is a symbolic link
256
+ InDontFollow uint32 = syscall .IN_DONT_FOLLOW
257
+ // InOneshot : Monitor the filesystem object corresponding to pathname for one event, then remove from watch list
258
+ InOneshot uint32 = syscall .IN_ONESHOT
259
+ // InOnlydir : Watch pathname only if it is a directory
260
+ InOnlydir uint32 = syscall .IN_ONLYDIR
255
261
256
262
// The "IN_MASK_ADD" option is not exported, as AddWatch
257
263
// adds it automatically, if there is already a watch for the given path
258
264
// IN_MASK_ADD uint32 = syscall.IN_MASK_ADD
259
265
260
266
// Events
261
- IN_ACCESS uint32 = syscall .IN_ACCESS
262
- IN_ALL_EVENTS uint32 = syscall .IN_ALL_EVENTS
263
- IN_ATTRIB uint32 = syscall .IN_ATTRIB
264
- IN_CLOSE uint32 = syscall .IN_CLOSE
265
- IN_CLOSE_NOWRITE uint32 = syscall .IN_CLOSE_NOWRITE
266
- IN_CLOSE_WRITE uint32 = syscall .IN_CLOSE_WRITE
267
- IN_CREATE uint32 = syscall .IN_CREATE
268
- IN_DELETE uint32 = syscall .IN_DELETE
269
- IN_DELETE_SELF uint32 = syscall .IN_DELETE_SELF
270
- IN_MODIFY uint32 = syscall .IN_MODIFY
271
- IN_MOVE uint32 = syscall .IN_MOVE
272
- IN_MOVED_FROM uint32 = syscall .IN_MOVED_FROM
273
- IN_MOVED_TO uint32 = syscall .IN_MOVED_TO
274
- IN_MOVE_SELF uint32 = syscall .IN_MOVE_SELF
275
- IN_OPEN uint32 = syscall .IN_OPEN
267
+
268
+ // InAccess : File was accessed
269
+ InAccess uint32 = syscall .IN_ACCESS
270
+ // InAllEvents : Bit mask for all notify events
271
+ InAllEvents uint32 = syscall .IN_ALL_EVENTS
272
+ // InAttrib : Metadata changed
273
+ InAttrib uint32 = syscall .IN_ATTRIB
274
+ // InClose : Equates to IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
275
+ InClose uint32 = syscall .IN_CLOSE
276
+ // InCloseNowrite : File or directory not opened for writing was closed
277
+ InCloseNowrite uint32 = syscall .IN_CLOSE_NOWRITE
278
+ // InCloseWrite : File opened for writing was closed
279
+ InCloseWrite uint32 = syscall .IN_CLOSE_WRITE
280
+ // InCreate : File/directory created in watched directory
281
+ InCreate uint32 = syscall .IN_CREATE
282
+ // InDelete : File/directory deleted from watched directory
283
+ InDelete uint32 = syscall .IN_DELETE
284
+ // InDeleteSelf : Watched file/directory was itself deleted
285
+ InDeleteSelf uint32 = syscall .IN_DELETE_SELF
286
+ // InModify : File was modified
287
+ InModify uint32 = syscall .IN_MODIFY
288
+ // InMove : Equates to IN_MOVED_FROM | IN_MOVED_TO
289
+ InMove uint32 = syscall .IN_MOVE
290
+ // InMovedFrom : Generated for the directory containing the old filename when a file is renamed
291
+ InMovedFrom uint32 = syscall .IN_MOVED_FROM
292
+ // InMovedTo : Generated for the directory containing the new filename when a file is renamed
293
+ InMovedTo uint32 = syscall .IN_MOVED_TO
294
+ // InMoveSelf : Watched file/directory was itself moved
295
+ InMoveSelf uint32 = syscall .IN_MOVE_SELF
296
+ // InOpen : File or directory was opened
297
+ InOpen uint32 = syscall .IN_OPEN
276
298
277
299
// Special events
278
- IN_ISDIR uint32 = syscall .IN_ISDIR
279
- IN_IGNORED uint32 = syscall .IN_IGNORED
280
- IN_Q_OVERFLOW uint32 = syscall .IN_Q_OVERFLOW
281
- IN_UNMOUNT uint32 = syscall .IN_UNMOUNT
300
+
301
+ // InIsdir : Subject of this event is a directory
302
+ InIsdir uint32 = syscall .IN_ISDIR
303
+ // InIgnored : Watch was removed explicitly or automatically
304
+ InIgnored uint32 = syscall .IN_IGNORED
305
+ // InQOverflow : Event queue overflowed
306
+ InQOverflow uint32 = syscall .IN_Q_OVERFLOW
307
+ // InUnmount : Filesystem containing watched object was unmounted
308
+ InUnmount uint32 = syscall .IN_UNMOUNT
282
309
)
283
310
284
311
var eventBits = []struct {
285
312
Value uint32
286
313
Name string
287
314
}{
288
- {IN_ACCESS , "IN_ACCESS" },
289
- {IN_ATTRIB , "IN_ATTRIB" },
290
- {IN_CLOSE , "IN_CLOSE" },
291
- {IN_CLOSE_NOWRITE , "IN_CLOSE_NOWRITE" },
292
- {IN_CLOSE_WRITE , "IN_CLOSE_WRITE" },
293
- {IN_CREATE , "IN_CREATE" },
294
- {IN_DELETE , "IN_DELETE" },
295
- {IN_DELETE_SELF , "IN_DELETE_SELF" },
296
- {IN_MODIFY , "IN_MODIFY" },
297
- {IN_MOVE , "IN_MOVE" },
298
- {IN_MOVED_FROM , "IN_MOVED_FROM" },
299
- {IN_MOVED_TO , "IN_MOVED_TO" },
300
- {IN_MOVE_SELF , "IN_MOVE_SELF" },
301
- {IN_OPEN , "IN_OPEN" },
302
- {IN_ISDIR , "IN_ISDIR" },
303
- {IN_IGNORED , "IN_IGNORED" },
304
- {IN_Q_OVERFLOW , "IN_Q_OVERFLOW" },
305
- {IN_UNMOUNT , "IN_UNMOUNT" },
315
+ {InAccess , "IN_ACCESS" },
316
+ {InAttrib , "IN_ATTRIB" },
317
+ {InClose , "IN_CLOSE" },
318
+ {InCloseNowrite , "IN_CLOSE_NOWRITE" },
319
+ {InCloseWrite , "IN_CLOSE_WRITE" },
320
+ {InCreate , "IN_CREATE" },
321
+ {InDelete , "IN_DELETE" },
322
+ {InDeleteSelf , "IN_DELETE_SELF" },
323
+ {InModify , "IN_MODIFY" },
324
+ {InMove , "IN_MOVE" },
325
+ {InMovedFrom , "IN_MOVED_FROM" },
326
+ {InMovedTo , "IN_MOVED_TO" },
327
+ {InMoveSelf , "IN_MOVE_SELF" },
328
+ {InOpen , "IN_OPEN" },
329
+ {InIsdir , "IN_ISDIR" },
330
+ {InIgnored , "IN_IGNORED" },
331
+ {InQOverflow , "IN_Q_OVERFLOW" },
332
+ {InUnmount , "IN_UNMOUNT" },
306
333
}
0 commit comments