diff --git a/integration_test.go b/integration_test.go index d07273dd..c1f5c2e6 100644 --- a/integration_test.go +++ b/integration_test.go @@ -1246,7 +1246,6 @@ func TestRemoveWithClose(t *testing.T) { } func TestMoveWatchedDirectory(t *testing.T) { - testDir := tempMkdir(t) defer os.RemoveAll(testDir) @@ -1266,24 +1265,29 @@ func TestMoveWatchedDirectory(t *testing.T) { addWatch(t, watcher, testDir) - if err := os.Mkdir(testDir+"/dir", 0o775); err != nil { + dir1 := filepath.Join(testDir, "dir") + dir2 := filepath.Join(testDir, "dir2") + + if err := os.Mkdir(dir1, 0o775); err != nil { t.Fatal(err) } time.Sleep(10 * time.Millisecond) - addWatch(t, watcher, testDir+"/dir") - if err := os.Rename(testDir+"/dir", testDir+"/dir2"); err != nil { + addWatch(t, watcher, dir1) + if err := os.Rename(dir1, dir2); err != nil { t.Fatal(err) } + time.Sleep(10 * time.Millisecond) - if err := ioutil.WriteFile(testDir+"/dir2/file.ext", []byte(""), 0o664); err != nil { + err := ioutil.WriteFile(filepath.Join(dir2, "file.ext"), []byte(""), 0o664) + if err != nil { t.Fatal(err) } if err := watcher.Close(); err != nil { t.Fatal(err) } - time.Sleep(10 * time.Millisecond) + time.Sleep(10 * time.Millisecond) if len(events) != 4 { t.Fatalf("Expected 4 events. Got: %d", len(events)) } diff --git a/windows.go b/windows.go index f5196a00..25d9c814 100644 --- a/windows.go +++ b/windows.go @@ -501,9 +501,9 @@ func (w *Watcher) readEvents() { case syscall.FILE_ACTION_RENAMED_OLD_NAME: watch.rename = name case syscall.FILE_ACTION_RENAMED_NEW_NAME: - - // update saved path of all sub-watches + // Update saved path of all sub-watches. oldFullName := filepath.Join(watch.path, watch.rename) + w.mu.Lock() for _, watchMap := range w.watches { for _, otherWatch := range watchMap { if strings.HasPrefix(otherWatch.path, oldFullName) { @@ -511,6 +511,7 @@ func (w *Watcher) readEvents() { } } } + w.mu.Unlock() if watch.names[watch.rename] != 0 { watch.names[name] |= watch.names[watch.rename]