]> go.fuhry.dev Git - fsnotify.git/commitdiff
newEvent helper
authorNathan Youngman <git@nathany.com>
Fri, 13 Jun 2014 04:40:19 +0000 (22:40 -0600)
committerNathan Youngman <git@nathany.com>
Fri, 13 Jun 2014 04:40:19 +0000 (22:40 -0600)
in preparation for Op

fsnotify_bsd.go
fsnotify_linux.go
fsnotify_windows.go

index 752bbf52bca9a1b9eb1cc856b01a07156e8af0d2..08e8bfa8c07bf8011b31780dff31d0194169d103 100644 (file)
@@ -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()
index 14c028fac7a3020d3355cfd53142a6233c932eae..74261872a000a28cfabc3ed0274e08f24202ac52 100644 (file)
@@ -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
index f74b8eba2c09b670c8ee2ab64e16cdbe2e0140d2..d3c39344e1e403e70daead65a76d7f439f4b4ce2 100644 (file)
@@ -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++