]> go.fuhry.dev Git - fsnotify.git/commitdiff
BSD - Add files when watch directory
authorChris Howey <howeyc@gmail.com>
Sun, 19 Feb 2012 22:00:38 +0000 (16:00 -0600)
committerChris Howey <howeyc@gmail.com>
Sun, 19 Feb 2012 22:00:38 +0000 (16:00 -0600)
fsnotify_bsd.go
fsnotify_test.go

index 83256d7bd07dc9bca7224744775a33967316f0be..5d48e167cd8f136e2c61834a0662fea05e3d1bb4 100644 (file)
@@ -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
index cdc46939544e7291cc0befd1afd2afdbae6b59bc..6d26d99923e3e88d2cb596c7957fdbd6849b48ab 100644 (file)
@@ -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")
        }