Add workaround for equivalent paths for watch option (#1895)

* Add workaround for equivalent paths for watch option

* Add comment

* Simplify path watching
This commit is contained in:
Joe Lim authored and GitHub committed 2025-01-30 09:53:50 +11:00
1 parent 2745c1ab22
commit 7595e05957
3 files changed
+57 -27

No files matched your search

+47 -14
View File
@@ -64,16 +64,14 @@ func (watch *watch) stop() {
watch.events = nil
}
func (watch *watch) add(paths map[string]bool) {
func (watch *watch) add(path string) {
if watch.watcher == nil {
return
}
for path := range paths {
// ignore /dev since write updates to /dev/tty causes high cpu usage
if path != "/dev" {
watch.watcher.Add(path)
}
// ignore /dev since write updates to /dev/tty causes high cpu usage
if path != "/dev" {
watch.watcher.Add(path)
}
}
@@ -82,20 +80,26 @@ func (watch *watch) loop() {
select {
case ev := <-watch.events:
if ev.Has(fsnotify.Create) {
dir := filepath.Dir(ev.Name)
watch.addLoad(dir)
watch.addUpdate(dir)
for _, path := range watch.getSameDirs(filepath.Dir(ev.Name)) {
watch.addLoad(path)
watch.addUpdate(path)
}
}
if ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) {
watch.delChan <- ev.Name
dir := filepath.Dir(ev.Name)
watch.addLoad(dir)
watch.addUpdate(dir)
dir, file := filepath.Split(ev.Name)
for _, path := range watch.getSameDirs(dir) {
watch.delChan <- filepath.Join(path, file)
watch.addLoad(path)
watch.addUpdate(path)
}
}
if ev.Has(fsnotify.Write) || ev.Has(fsnotify.Chmod) {
watch.addUpdate(ev.Name)
dir, file := filepath.Split(ev.Name)
for _, path := range watch.getSameDirs(dir) {
watch.addUpdate(filepath.Join(path, file))
}
}
case <-watch.loadTimer.C:
for path := range watch.loads {
@@ -136,3 +140,32 @@ func (watch *watch) addUpdate(path string) {
}
watch.updates[path] = true
}
// Hacky workaround since fsnotify reports changes for only one path if a
// directory is located at more than one path (e.g. bind mounts).
func (watch *watch) getSameDirs(dir string) []string {
var paths []string
dirStat, err := os.Stat(dir)
if err != nil {
return nil
}
for _, path := range watch.watcher.WatchList() {
if path == dir {
paths = append(paths, path)
continue
}
stat, err := os.Stat(path)
if err != nil {
continue
}
if os.SameFile(stat, dirStat) {
paths = append(paths, path)
}
}
return paths
}