Skip to content

Commit

Permalink
windows: update watch paths when renaming directories with sub-watches (
Browse files Browse the repository at this point in the history
#370)

Fixes #259
Fixes #243

Co-authored-by: Martin Tournoij <martin@arp242.net>
  • Loading branch information
arnegroskurth and arp242 committed Jul 31, 2022
1 parent 87dc1fa commit f174f95
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
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

0 comments on commit f174f95

Please sign in to comment.