@@ -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 }
0 commit comments