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 #370

Merged
merged 4 commits into from Jul 31, 2022
Merged
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
6 changes: 6 additions & 0 deletions helpers_test.go
Expand Up @@ -411,6 +411,12 @@ func newEvents(t *testing.T, s string) Events {
if e, ok := events[runtime.GOOS]; ok {
return e
}
switch runtime.GOOS {
case "freebsd", "netbsd", "openbsd", "dragonfly", "darwin":
if e, ok := events["kqueue"]; ok {
return e
}
}
return events[""]
}

Expand Down
30 changes: 30 additions & 0 deletions integration_test.go
Expand Up @@ -164,6 +164,36 @@ func TestWatchRename(t *testing.T) {
windows:
create /renamed
`},

{"rename watched directory", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)

dir := filepath.Join(tmp, "dir")
mkdir(t, dir)
addWatch(t, w, dir)

mv(t, dir, tmp, "dir-renamed")
touch(t, tmp, "dir-renamed/file")
}, `
CREATE "/dir" # mkdir
RENAME "/dir" # mv
CREATE "/dir-renamed"
RENAME "/dir"
CREATE "/dir/file" # touch

windows:
CREATE "/dir" # mkdir
RENAME "/dir" # mv
CREATE "/dir-renamed"
CREATE "/dir-renamed/file" # touch

# TODO: no results for the touch; this is probably a bug; windows
# was fixed in #370.
kqueue:
CREATE "/dir" # mkdir
CREATE "/dir-renamed" # mv
REMOVE|RENAME "/dir"
`},
}

for _, tt := range tests {
Expand Down
13 changes: 13 additions & 0 deletions windows.go
Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"
"reflect"
"runtime"
"strings"
"sync"
"syscall"
"unsafe"
Expand Down Expand Up @@ -500,6 +501,18 @@ 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.
old := filepath.Join(watch.path, watch.rename)
w.mu.Lock()
for _, watchMap := range w.watches {
for _, ww := range watchMap {
if strings.HasPrefix(ww.path, old) {
ww.path = filepath.Join(fullname, strings.TrimPrefix(ww.path, old))
}
}
}
w.mu.Unlock()

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