Skip to content

Commit d2ee00e

Browse files
authored
kqueue: emit events as "/path/dir/file" instead of "path/link/file" (#625)
When watching a symlink to a directory it would emit events with the target name, rather than the link name.
1 parent a56fe9b commit d2ee00e

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Unreleased
1515

1616
- kqueue: set O_CLOEXEC to prevent passing file descriptors to children ([#617])
1717

18+
- kqueue: emit events as "/path/dir/file" instead of "path/link/file" when watching a symlink ([#625])
19+
1820
- inotify: don't send event for IN_DELETE_SELF when also watching the parent ([#620])
1921

2022
- fen: allow watching subdirectories of watched directories ([#621])
@@ -25,6 +27,7 @@ Unreleased
2527
[#619]: https://github.com/fsnotify/fsnotify/pull/619
2628
[#620]: https://github.com/fsnotify/fsnotify/pull/620
2729
[#621]: https://github.com/fsnotify/fsnotify/pull/621
30+
[#625]: https://github.com/fsnotify/fsnotify/pull/625
2831

2932
1.7.0 - 2023-10-22
3033
------------------

backend_kqueue.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ type (
145145
watch struct {
146146
wd int
147147
name string
148+
linkName string // In case of links; name is the target, and this is the link.
148149
isDir bool
149150
dirFlags uint32
150151
}
@@ -208,12 +209,12 @@ func (w *watches) addLink(path string, fd int) {
208209
w.seen[path] = struct{}{}
209210
}
210211

211-
func (w *watches) add(path string, fd int, isDir bool) {
212+
func (w *watches) add(path, linkPath string, fd int, isDir bool) {
212213
w.mu.Lock()
213214
defer w.mu.Unlock()
214215

215216
w.path[path] = fd
216-
w.wd[fd] = watch{wd: fd, name: path, isDir: isDir}
217+
w.wd[fd] = watch{wd: fd, name: path, linkName: linkPath, isDir: isDir}
217218

218219
parent := filepath.Dir(path)
219220
byDir, ok := w.byDir[parent]
@@ -569,6 +570,7 @@ func (w *Watcher) addWatch(name string, flags uint32) (string, error) {
569570
return link, nil
570571
}
571572

573+
info.linkName = name
572574
name = link
573575
fi, err = os.Lstat(name)
574576
if err != nil {
@@ -600,7 +602,7 @@ func (w *Watcher) addWatch(name string, flags uint32) (string, error) {
600602
}
601603

602604
if !alreadyWatching {
603-
w.watches.add(name, info.wd, info.isDir)
605+
w.watches.add(name, info.linkName, info.wd, info.isDir)
604606
}
605607

606608
// Watch the directory if it has not been watched before, or if it was
@@ -678,7 +680,7 @@ func (w *Watcher) readEvents() {
678680
continue
679681
}
680682

681-
event := w.newEvent(path.name, mask)
683+
event := w.newEvent(path.name, path.linkName, mask)
682684

683685
if event.Has(Rename) || event.Has(Remove) {
684686
w.remove(event.Name, false)
@@ -733,8 +735,14 @@ func (w *Watcher) readEvents() {
733735
}
734736

735737
// newEvent returns an platform-independent Event based on kqueue Fflags.
736-
func (w *Watcher) newEvent(name string, mask uint32) Event {
738+
func (w *Watcher) newEvent(name, linkName string, mask uint32) Event {
737739
e := Event{Name: name}
740+
if linkName != "" {
741+
// If the user watched "/path/link" then emit events as "/path/link"
742+
// rather than "/path/target".
743+
e.Name = linkName
744+
}
745+
738746
if mask&unix.NOTE_DELETE == unix.NOTE_DELETE {
739747
e.Op |= Remove
740748
}

testdata/watch-symlink/to-dir

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,3 @@ touch /dir/file
99

1010
Output:
1111
create /link/file
12-
13-
kqueue: # TODO: Should use the link name.
14-
create /dir/file

testdata/watch-symlink/to-file

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ echo hello >>/file
1010
Output:
1111
write /link
1212

13-
kqueue: # TODO: Should use the link name.
14-
write /file
1513
windows: # TODO: investigate.
1614
no-events

0 commit comments

Comments
 (0)