From 8725c67e1888a68d04897ec884dd1c43177f04f6 Mon Sep 17 00:00:00 2001 From: Chris Howey Date: Sun, 19 Feb 2012 16:00:38 -0600 Subject: [PATCH] BSD - Add files when watch directory --- fsnotify_bsd.go | 30 ++++++++++++++++++++++++------ fsnotify_test.go | 9 +++++---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/fsnotify_bsd.go b/fsnotify_bsd.go index 83256d7..5d48e16 100644 --- a/fsnotify_bsd.go +++ b/fsnotify_bsd.go @@ -129,6 +129,9 @@ func (w *Watcher) addWatch(path string, flags uint32) error { fi, _ := os.Stat(path) w.finfo[watchfd] = fi + if fi.IsDir() { + w.watchDirectoryFiles(path) + } } syscall.SetKevent(watchEntry, watchfd, syscall.EVFILT_VNODE, syscall.EV_ADD|syscall.EV_CLEAR) @@ -233,11 +236,32 @@ func (w *Watcher) readEvents() { } } +func (w *Watcher) watchDirectoryFiles(dirPath string) { + // Get all files + files, err := ioutil.ReadDir(dirPath) + if err != nil { + w.Error <- err + } + + // Search for new files + for _, fileInfo := range files { + if fileInfo.IsDir() == false { + filePath := path.Join(dirPath, fileInfo.Name()) + // Watch file to mimic linux fsnotify + e := w.addWatch(filePath, NOTE_DELETE|NOTE_WRITE|NOTE_RENAME) + if e != nil { + w.Error <- e + } + } + } +} + // sendDirectoryEvents searches the directory for newly created files // and sends them over the event channel. This functionality is to have // the BSD version of fsnotify mach linux fsnotify which provides a // create event for files created in a watched directory. func (w *Watcher) sendDirectoryChangeEvents(dirPath string) { + w.watchDirectoryFiles(dirPath) // Get all files files, err := ioutil.ReadDir(dirPath) if err != nil { @@ -249,12 +273,6 @@ func (w *Watcher) sendDirectoryChangeEvents(dirPath string) { if fileInfo.IsDir() == false { filePath := path.Join(dirPath, fileInfo.Name()) if w.watches[filePath] == 0 { - // Watch file to mimic linux fsnotify - e := w.addWatch(filePath, NOTE_DELETE|NOTE_WRITE|NOTE_RENAME) - if e != nil { - w.Error <- e - } - // Send create event fileEvent := new(FileEvent) fileEvent.Name = filePath diff --git a/fsnotify_test.go b/fsnotify_test.go index cdc4693..6d26d99 100644 --- a/fsnotify_test.go +++ b/fsnotify_test.go @@ -83,19 +83,20 @@ func TestFsnotifyDirOnly(t *testing.T) { t.Fatalf("creating test file failed: %s", err) } f.Sync() - time.Sleep(200e6) // give system time to sync write change before delete f.WriteString("data") f.Sync() f.Close() + time.Sleep(200e6) // give system time to sync write change before delete + os.Remove(testFile) // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500e6) // 500 ms -// if createReceived == 0 { -// t.Fatal("fsnotify create events have not been received after 500 ms") -// } + if createReceived > 0 { + t.Fatal("fsnotify create events should not occur in this test!") + } if modifyReceived == 0 { t.Fatal("fsnotify modify events have not been received after 500 ms") } -- 2.50.1