)
type Event struct {
- mask uint32 // Mask of events
Name string // File name (optional)
+ mask uint32 // Mask of events
create bool // set by fsnotify package if found new file
}
+func newEvent(name string, mask uint32, create bool) *Event {
+ e := new(Event)
+ e.Name = name
+ e.mask = mask
+ e.create = create
+ return e
+}
+
// IsCreate reports whether the Event was triggered by a creation
func (e *Event) IsCreate() bool { return e.create }
// Flush the events we received to the events channel
for len(events) > 0 {
- fileEvent := new(Event)
watchEvent := &events[0]
- fileEvent.mask = uint32(watchEvent.Fflags)
+ mask := uint32(watchEvent.Fflags)
w.pmut.Lock()
- fileEvent.Name = w.paths[int(watchEvent.Ident)]
+ name := w.paths[int(watchEvent.Ident)]
fileInfo := w.finfo[int(watchEvent.Ident)]
w.pmut.Unlock()
+
+ fileEvent := newEvent(name, mask, false)
+
if fileInfo != nil && fileInfo.IsDir() && !fileEvent.IsDelete() {
// Double check to make sure the directory exist. This can happen when
// we do a rm -fr on a recursively watched folders and we receive a
_, doesExist := w.fileExists[filePath]
w.femut.Unlock()
if !doesExist {
- // Send create event
- fileEvent := new(Event)
- fileEvent.Name = filePath
- fileEvent.create = true
+ // Send create event (mask=0)
+ fileEvent := newEvent(filePath, 0, true)
w.Events <- fileEvent
}
w.femut.Lock()
)
type Event struct {
+ Name string // File name (optional)
mask uint32 // Mask of events
cookie uint32 // Unique cookie associating related events (for rename(2))
- Name string // File name (optional)
+}
+
+func newEvent(name string, mask uint32, cookie uint32) *Event {
+ e := new(Event)
+ e.Name = name
+ e.mask = mask
+ e.cookie = cookie
+ return e
}
// IsCreate reports whether the Event was triggered by a creation
for offset <= uint32(n-syscall.SizeofInotifyEvent) {
// Point "raw" to the event in the buffer
raw := (*syscall.InotifyEvent)(unsafe.Pointer(&buf[offset]))
- event := new(Event)
- event.mask = uint32(raw.Mask)
- event.cookie = uint32(raw.Cookie)
+
+ mask := uint32(raw.Mask)
+ cookie := uint32(raw.Cookie)
nameLen := uint32(raw.Len)
// If the event happened to the watched directory or the watched file, the kernel
// doesn't append the filename to the event, but we would like to always fill the
// the "Name" field with a valid filename. We retrieve the path of the watch from
// the "paths" map.
w.mu.Lock()
- event.Name = w.paths[int(raw.Wd)]
+ name := w.paths[int(raw.Wd)]
w.mu.Unlock()
if nameLen > 0 {
// Point "bytes" at the first byte of the filename
bytes := (*[syscall.PathMax]byte)(unsafe.Pointer(&buf[offset+syscall.SizeofInotifyEvent]))
- // The filename is padded with NUL bytes. TrimRight() gets rid of those.
- event.Name += "/" + strings.TrimRight(string(bytes[0:nameLen]), "\000")
+ // The filename is padded with NULL bytes. TrimRight() gets rid of those.
+ name += "/" + strings.TrimRight(string(bytes[0:nameLen]), "\000")
}
+ event := newEvent(name, mask, cookie)
+
// Send the events that are not ignored on the events channel
if !event.ignoreLinux() {
w.Events <- event
// Event is the type of the notification messages
// received on the watcher's Events channel.
type Event struct {
+ Name string // File name (optional)
mask uint32 // Mask of events
cookie uint32 // Unique cookie associating related events (for rename)
- Name string // File name (optional)
+}
+
+func newEvent(name string, mask uint32) *Event {
+ return &Event{mask: mask, Name: name}
}
// IsCreate reports whether the Event was triggered by a creation
var offset uint32
for {
if n == 0 {
- w.Events <- &Event{mask: sys_FS_Q_OVERFLOW}
+ w.Events <- newEvent("", sys_FS_Q_OVERFLOW)
w.Errors <- errors.New("short read in readEvents()")
break
}
if mask == 0 {
return false
}
- event := &Event{mask: uint32(mask), Name: name}
+ event := newEvent(name, uint32(mask))
if mask&sys_FS_MOVE != 0 {
if mask&sys_FS_MOVED_FROM != 0 {
w.cookie++