Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Update watch paths when renaming directories with sub-watches on Windows #369

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
# Changelog

* Windows: Fixed persistence of original path-name in watches within renamed directory (#259, #243)

## v1.4.7 / 2018-01-09

* BSD/macOS: Fix possible deadlock on closing the watcher on kqueue (thanks @nhooyr and @glycerine)
Expand Down
50 changes: 50 additions & 0 deletions integration_test.go
Expand Up @@ -13,6 +13,7 @@ import (
"path"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -1226,6 +1227,55 @@ func TestRemoveWithClose(t *testing.T) {
}
}

func TestMoveWatchedDirectory(t *testing.T) {

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

watcher := newWatcher(t)

// event recording
var events []Event
go func() {
for {
event, ok := <-watcher.Events
if !ok {
return
}
events = append(events, event)
}
}()

addWatch(t, watcher, testDir)

if err := os.Mkdir(testDir+"/dir", 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 {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
if err := ioutil.WriteFile(testDir+"/dir2/file.ext", []byte(""), 0o664); err != nil {
t.Fatal(err)
}

if err := watcher.Close(); err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)

if len(events) != 4 {
t.Fatalf("Expected 4 events. Got: %d", len(events))
}

expectedSuffix := filepath.Join("dir2", "file.ext")
if !strings.HasSuffix(events[3].Name, expectedSuffix) {
t.Fatalf("Expected suffix %s, Got: %s", expectedSuffix, events[3].Name)
}
}

func testRename(file1, file2 string) error {
switch runtime.GOOS {
case "windows", "plan9":
Expand Down
12 changes: 12 additions & 0 deletions windows.go
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
"unsafe"
Expand Down Expand Up @@ -464,6 +465,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
oldFullName := filepath.Join(watch.path, watch.rename)
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))
}
}
}

if watch.names[watch.rename] != 0 {
watch.names[name] |= watch.names[watch.rename]
delete(watch.names, watch.rename)
Expand Down