From 4f106a7e3be5a3a8995558008219849a956efe44 Mon Sep 17 00:00:00 2001 From: Nathan Youngman Date: Thu, 12 Jun 2014 22:40:19 -0600 Subject: [PATCH] newEvent helper in preparation for Op --- fsnotify_bsd.go | 24 ++++++++++++++++-------- fsnotify_linux.go | 24 +++++++++++++++++------- fsnotify_windows.go | 10 +++++++--- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/fsnotify_bsd.go b/fsnotify_bsd.go index 752bbf5..08e8bfa 100644 --- a/fsnotify_bsd.go +++ b/fsnotify_bsd.go @@ -34,11 +34,19 @@ const ( ) 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 } @@ -337,13 +345,15 @@ func (w *Watcher) readEvents() { // 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 @@ -458,10 +468,8 @@ func (w *Watcher) sendDirectoryChangeEvents(dirPath string) { _, 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() diff --git a/fsnotify_linux.go b/fsnotify_linux.go index 14c028f..7426187 100644 --- a/fsnotify_linux.go +++ b/fsnotify_linux.go @@ -57,9 +57,17 @@ const ( ) 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 @@ -235,24 +243,26 @@ func (w *Watcher) readEvents() { 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 diff --git a/fsnotify_windows.go b/fsnotify_windows.go index f74b8eb..d3c3934 100644 --- a/fsnotify_windows.go +++ b/fsnotify_windows.go @@ -49,9 +49,13 @@ const ( // 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 @@ -469,7 +473,7 @@ func (w *Watcher) readEvents() { 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 } @@ -543,7 +547,7 @@ func (w *Watcher) sendEvent(name string, mask uint64) bool { 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++ -- 2.50.1