]> go.fuhry.dev Git - fsnotify.git/commitdiff
BSD - Fix for rename test case provided by snoe
authorChris Howey <chris@howey.me>
Sat, 14 Jul 2012 16:23:14 +0000 (11:23 -0500)
committerChris Howey <chris@howey.me>
Sat, 14 Jul 2012 16:23:14 +0000 (11:23 -0500)
fsnotify.go
fsnotify_bsd.go
fsnotify_test.go

index 23973219c5b047425e61065c5ae4a0ab0908faa6..079869993ecd7626d52df38af18382148e20ad11 100644 (file)
@@ -30,6 +30,7 @@ func (w *Watcher) purgeEvents() {
                }
 
                if (fsnFlags&FSN_RENAME == FSN_RENAME) && ev.IsRename() {
+                       w.RemoveWatch(ev.Name)
                        sendEvent = true
                }
 
index 8348208dc6c7782370c1fab2870a40ac2a18ff9c..3053b32a206a433c17a8855654d2de3018b4ecd3 100644 (file)
@@ -166,7 +166,6 @@ func (w *Watcher) removeWatch(path string) error {
        if !ok {
                return errors.New(fmt.Sprintf("can't remove non-existent kevent watch for: %s", path))
        }
-       syscall.Close(watchfd)
        watchEntry := &w.kbuf[0]
        syscall.SetKevent(watchEntry, w.watches[path], syscall.EVFILT_VNODE, syscall.EV_DELETE)
        success, errno := syscall.Kevent(w.kq, w.kbuf[:], nil, nil)
@@ -175,6 +174,7 @@ func (w *Watcher) removeWatch(path string) error {
        } else if (watchEntry.Flags & syscall.EV_ERROR) == syscall.EV_ERROR {
                return errors.New("kevent rm error")
        }
+       syscall.Close(watchfd)
        delete(w.watches, path)
        return nil
 }
@@ -288,7 +288,8 @@ func (w *Watcher) sendDirectoryChangeEvents(dirPath string) {
        for _, fileInfo := range files {
                if fileInfo.IsDir() == false {
                        filePath := filepath.Join(dirPath, fileInfo.Name())
-                       if w.watches[filePath] == 0 {
+                       _, watchFound := w.watches[filePath]
+                       if watchFound == false {
                                w.fsnFlags[filePath] = FSN_ALL
                                // Send create event
                                fileEvent := new(FileEvent)
index eccf7366f9fade97e248c8e7f66246c8ef271cde..b54ebcea26c1ecd59f8fe2d050187871c4db2b2a 100644 (file)
@@ -100,19 +100,19 @@ func TestFsnotifyMultipleOperations(t *testing.T) {
                t.Fatalf("rename failed: %s", err)
        }
 
-  // Modify the file outside of the watched dir
+       // Modify the file outside of the watched dir
        f.WriteString("data")
        f.Sync()
-  f.Close()
+       f.Close()
 
        time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete
 
-  // Recreate the file that was moved
+       // Recreate the file that was moved
        f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666)
        if err != nil {
                t.Fatalf("creating test file failed: %s", err)
        }
-  f.Close()
+       f.Close()
        time.Sleep(50 * time.Millisecond) // give system time to sync write change before delete
 
        // We expect this event to be received almost immediately, but let's wait 500 ms to be sure
@@ -126,9 +126,9 @@ func TestFsnotifyMultipleOperations(t *testing.T) {
        if deleteReceived != 0 {
                t.Fatalf("incorrect number of delete events received after 500 ms (%d vs %d)", deleteReceived, 0)
        }
-  if renameReceived != 1 {
+       if renameReceived != 1 {
                t.Fatalf("incorrect number of rename events received after 500 ms (%d vs %d)", renameReceived, 1)
-  }
+       }
 
        // Try closing the fsnotify instance
        t.Log("calling Close()")
@@ -142,7 +142,6 @@ func TestFsnotifyMultipleOperations(t *testing.T) {
        }
 }
 
-
 func TestFsnotifyDirOnly(t *testing.T) {
        // Create an fsnotify watcher instance and initialize it
        watcher, err := NewWatcher()