From f992161eaf4a961470f1d0cee5d0b4b8ffefeb6a Mon Sep 17 00:00:00 2001 From: Chris Howey Date: Sat, 14 Jul 2012 11:23:14 -0500 Subject: [PATCH] BSD - Fix for rename test case provided by snoe --- fsnotify.go | 1 + fsnotify_bsd.go | 5 +++-- fsnotify_test.go | 13 ++++++------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/fsnotify.go b/fsnotify.go index 2397321..0798699 100644 --- a/fsnotify.go +++ b/fsnotify.go @@ -30,6 +30,7 @@ func (w *Watcher) purgeEvents() { } if (fsnFlags&FSN_RENAME == FSN_RENAME) && ev.IsRename() { + w.RemoveWatch(ev.Name) sendEvent = true } diff --git a/fsnotify_bsd.go b/fsnotify_bsd.go index 8348208..3053b32 100644 --- a/fsnotify_bsd.go +++ b/fsnotify_bsd.go @@ -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) diff --git a/fsnotify_test.go b/fsnotify_test.go index eccf736..b54ebce 100644 --- a/fsnotify_test.go +++ b/fsnotify_test.go @@ -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() -- 2.50.1