]> go.fuhry.dev Git - fsnotify.git/commitdiff
Op constants and Event.Op
authorNathan Youngman <git@nathany.com>
Fri, 13 Jun 2014 04:50:14 +0000 (22:50 -0600)
committerNathan Youngman <git@nathany.com>
Fri, 13 Jun 2014 04:50:14 +0000 (22:50 -0600)
fsnotify.go
fsnotify_bsd.go
fsnotify_linux.go
fsnotify_symlink_test.go
fsnotify_test.go
fsnotify_windows.go

index 8f51a3c944480f552799ca5f22b536be058d630d..22e105ef1f4c3da4133f0056a56a9391cbd64c90 100644 (file)
@@ -7,6 +7,18 @@ package fsnotify
 
 import "fmt"
 
+// Op describes a set of file operations.
+type Op uint32
+
+// These are the file operations that can trigger a notification.
+const (
+       Create Op = 1 << iota
+       Write
+       Remove
+       Rename
+       Chmod
+)
+
 // Add starts watching for operations on the named file.
 func (w *Watcher) Add(path string) error {
        return w.watch(path)
@@ -18,28 +30,28 @@ func (w *Watcher) Remove(path string) error {
 }
 
 // String formats the event e in the form
-// "filename: DELETE|MODIFY|..."
+// "filename: REMOVE|WRITE|..."
 func (e *Event) String() string {
        var events string = ""
 
-       if e.IsCreate() {
+       if e.Op&Create == Create {
                events += "|" + "CREATE"
        }
 
-       if e.IsDelete() {
-               events += "|" + "DELETE"
+       if e.Op&Remove == Remove {
+               events += "|" + "REMOVE"
        }
 
-       if e.IsModify() {
-               events += "|" + "MODIFY"
+       if e.Op&Write == Write {
+               events += "|" + "WRITE"
        }
 
-       if e.IsRename() {
+       if e.Op&Rename == Rename {
                events += "|" + "RENAME"
        }
 
-       if e.IsAttrib() {
-               events += "|" + "ATTRIB"
+       if e.Op&Chmod == Chmod {
+               events += "|" + "CHMOD"
        }
 
        if len(events) > 0 {
index 08e8bfa8c07bf8011b31780dff31d0194169d103..0b38d1dbc0a4464b8de362015e962c8c9d3895f7 100644 (file)
@@ -34,7 +34,8 @@ const (
 )
 
 type Event struct {
-       Name   string // File name (optional)
+       Name   string // Relative path to the file/directory.
+       Op     Op     // Platform-independent bitmask.
        mask   uint32 // Mask of events
        create bool   // set by fsnotify package if found new file
 }
@@ -44,6 +45,21 @@ func newEvent(name string, mask uint32, create bool) *Event {
        e.Name = name
        e.mask = mask
        e.create = create
+       if e.IsCreate() {
+               e.Op |= Create
+       }
+       if e.IsDelete() {
+               e.Op |= Remove
+       }
+       if e.IsModify() {
+               e.Op |= Write
+       }
+       if e.IsRename() {
+               e.Op |= Rename
+       }
+       if e.IsAttrib() {
+               e.Op |= Chmod
+       }
        return e
 }
 
index 74261872a000a28cfabc3ed0274e08f24202ac52..e7ca83829be92712b9b395bd74f264f71ae2bba4 100644 (file)
@@ -57,7 +57,8 @@ const (
 )
 
 type Event struct {
-       Name   string // File name (optional)
+       Name   string // Relative path to the file/directory.
+       Op     Op     // Platform-independent bitmask.
        mask   uint32 // Mask of events
        cookie uint32 // Unique cookie associating related events (for rename(2))
 }
@@ -67,6 +68,21 @@ func newEvent(name string, mask uint32, cookie uint32) *Event {
        e.Name = name
        e.mask = mask
        e.cookie = cookie
+       if e.IsCreate() {
+               e.Op |= Create
+       }
+       if e.IsDelete() {
+               e.Op |= Remove
+       }
+       if e.IsModify() {
+               e.Op |= Write
+       }
+       if e.IsRename() {
+               e.Op |= Rename
+       }
+       if e.IsAttrib() {
+               e.Op |= Chmod
+       }
        return e
 }
 
index 21253f9bc7dd6daeb31e57812ef471cd76c19b7b..b5354b5b57bfe10c53b8c3155451711fe21c28d4 100644 (file)
@@ -34,7 +34,7 @@ func TestFsnotifyFakeSymlink(t *testing.T) {
        go func() {
                for ev := range watcher.Events {
                        t.Logf("event received: %s", ev)
-                       if ev.IsCreate() {
+                       if ev.Op&Create == Create {
                                createEventsReceived.increment()
                        } else {
                                otherEventsReceived.increment()
index d9541f26032d34fea2ba90c01e6c347ebf16f506..b4240f3ad85df155b401062a45d6f0746917daea 100644 (file)
@@ -89,16 +89,16 @@ func TestFsnotifyMultipleOperations(t *testing.T) {
                        // Only count relevant events
                        if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) {
                                t.Logf("event received: %s", event)
-                               if event.IsDelete() {
+                               if event.Op&Remove == Remove {
                                        deleteReceived.increment()
                                }
-                               if event.IsModify() {
+                               if event.Op&Write == Write {
                                        modifyReceived.increment()
                                }
-                               if event.IsCreate() {
+                               if event.Op&Create == Create {
                                        createReceived.increment()
                                }
-                               if event.IsRename() {
+                               if event.Op&Rename == Rename {
                                        renameReceived.increment()
                                }
                        } else {
@@ -202,13 +202,13 @@ func TestFsnotifyMultipleCreates(t *testing.T) {
                        // Only count relevant events
                        if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) {
                                t.Logf("event received: %s", event)
-                               if event.IsDelete() {
+                               if event.Op&Remove == Remove {
                                        deleteReceived.increment()
                                }
-                               if event.IsCreate() {
+                               if event.Op&Create == Create {
                                        createReceived.increment()
                                }
-                               if event.IsModify() {
+                               if event.Op&Write == Write {
                                        modifyReceived.increment()
                                }
                        } else {
@@ -341,13 +341,13 @@ func TestFsnotifyDirOnly(t *testing.T) {
                        // Only count relevant events
                        if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) || event.Name == filepath.Clean(testFileAlreadyExists) {
                                t.Logf("event received: %s", event)
-                               if event.IsDelete() {
+                               if event.Op&Remove == Remove {
                                        deleteReceived.increment()
                                }
-                               if event.IsModify() {
+                               if event.Op&Write == Write {
                                        modifyReceived.increment()
                                }
-                               if event.IsCreate() {
+                               if event.Op&Create == Create {
                                        createReceived.increment()
                                }
                        } else {
@@ -443,7 +443,7 @@ func TestFsnotifyDeleteWatchedDir(t *testing.T) {
                        // Only count relevant events
                        if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFileAlreadyExists) {
                                t.Logf("event received: %s", event)
-                               if event.IsDelete() {
+                               if event.Op&Remove == Remove {
                                        deleteReceived.increment()
                                }
                        } else {
@@ -489,10 +489,10 @@ func TestFsnotifySubDir(t *testing.T) {
                        // Only count relevant events
                        if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testSubDir) || event.Name == filepath.Clean(testFile1) {
                                t.Logf("event received: %s", event)
-                               if event.IsCreate() {
+                               if event.Op&Create == Create {
                                        createReceived.increment()
                                }
-                               if event.IsDelete() {
+                               if event.Op&Remove == Remove {
                                        deleteReceived.increment()
                                }
                        } else {
@@ -583,7 +583,7 @@ func TestFsnotifyRename(t *testing.T) {
                for event := range eventstream {
                        // Only count relevant events
                        if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) || event.Name == filepath.Clean(testFileRenamed) {
-                               if event.IsRename() {
+                               if event.Op&Rename == Rename {
                                        renameReceived.increment()
                                }
                                t.Logf("event received: %s", event)
@@ -665,7 +665,7 @@ func TestFsnotifyRenameToCreate(t *testing.T) {
                for event := range eventstream {
                        // Only count relevant events
                        if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) || event.Name == filepath.Clean(testFileRenamed) {
-                               if event.IsCreate() {
+                               if event.Op&Create == Create {
                                        createReceived.increment()
                                }
                                t.Logf("event received: %s", event)
@@ -879,10 +879,10 @@ func TestFsnotifyAttrib(t *testing.T) {
                for event := range eventstream {
                        // Only count relevant events
                        if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFile) {
-                               if event.IsModify() {
+                               if event.Op&Write == Write {
                                        modifyReceived.increment()
                                }
-                               if event.IsAttrib() {
+                               if event.Op&Chmod == Chmod {
                                        attribReceived.increment()
                                }
                                t.Logf("event received: %s", event)
index d3c39344e1e403e70daead65a76d7f439f4b4ce2..501d87d000e08013ddd5be256d5ec9868fb786b5 100644 (file)
@@ -49,13 +49,30 @@ const (
 // Event is the type of the notification messages
 // received on the watcher's Events channel.
 type Event struct {
-       Name   string // File name (optional)
+       Name   string // Relative path to the file/directory.
+       Op     Op     // Platform-independent bitmask.
        mask   uint32 // Mask of events
        cookie uint32 // Unique cookie associating related events (for rename)
 }
 
 func newEvent(name string, mask uint32) *Event {
-       return &Event{mask: mask, Name: name}
+       e := &Event{mask: mask, Name: name}
+       if e.IsCreate() {
+               e.Op |= Create
+       }
+       if e.IsDelete() {
+               e.Op |= Remove
+       }
+       if e.IsModify() {
+               e.Op |= Write
+       }
+       if e.IsRename() {
+               e.Op |= Rename
+       }
+       if e.IsAttrib() {
+               e.Op |= Chmod
+       }
+       return e
 }
 
 // IsCreate reports whether the Event was triggered by a creation