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)
}
}
+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 {
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
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")
}