Skip to content

Commit

Permalink
Add lock, use filepath.Join in test
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Jul 22, 2022
1 parent 61d73cc commit ff67a55
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
16 changes: 10 additions & 6 deletions integration_test.go
Expand Up @@ -1246,7 +1246,6 @@ func TestRemoveWithClose(t *testing.T) {
}

func TestMoveWatchedDirectory(t *testing.T) {

testDir := tempMkdir(t)
defer os.RemoveAll(testDir)

Expand All @@ -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))
}
Expand Down
5 changes: 3 additions & 2 deletions windows.go
Expand Up @@ -501,16 +501,17 @@ 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) {
otherWatch.path = filepath.Join(fullname, strings.TrimPrefix(otherWatch.path, oldFullName))
}
}
}
w.mu.Unlock()

if watch.names[watch.rename] != 0 {
watch.names[name] |= watch.names[watch.rename]
Expand Down

0 comments on commit ff67a55

Please sign in to comment.