| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"log" |
| 5 |
"maps" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"slices" |
| 9 |
"sync" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/fsnotify/fsnotify" |
| 13 |
) |
| 14 |
|
| 15 |
type watch struct { |
| 16 |
watcher *fsnotify.Watcher |
| 17 |
events <-chan fsnotify.Event |
| 18 |
quit chan struct{} |
| 19 |
pending map[watchUpdate]bool |
| 20 |
timeout chan watchUpdate |
| 21 |
dirChan chan<- *dir |
| 22 |
fileChan chan<- *file |
| 23 |
delChan chan<- string |
| 24 |
paths map[string]bool |
| 25 |
pathsLock sync.Mutex |
| 26 |
} |
| 27 |
|
| 28 |
func newWatch(dirChan chan<- *dir, fileChan chan<- *file, delChan chan<- string) *watch { |
| 29 |
watch := &watch{ |
| 30 |
quit: make(chan struct{}), |
| 31 |
pending: make(map[watchUpdate]bool), |
| 32 |
timeout: make(chan watchUpdate, 1024), |
| 33 |
dirChan: dirChan, |
| 34 |
fileChan: fileChan, |
| 35 |
delChan: delChan, |
| 36 |
paths: make(map[string]bool), |
| 37 |
} |
| 38 |
|
| 39 |
return watch |
| 40 |
} |
| 41 |
|
| 42 |
func (watch *watch) start() { |
| 43 |
if watch.watcher != nil { |
| 44 |
return |
| 45 |
} |
| 46 |
|
| 47 |
watcher, err := fsnotify.NewWatcher() |
| 48 |
if err != nil { |
| 49 |
log.Printf("start watcher: %s", err) |
| 50 |
return |
| 51 |
} |
| 52 |
|
| 53 |
watch.watcher = watcher |
| 54 |
watch.events = watcher.Events |
| 55 |
|
| 56 |
go watch.loop() |
| 57 |
} |
| 58 |
|
| 59 |
func (watch *watch) stop() { |
| 60 |
if watch.watcher == nil { |
| 61 |
return |
| 62 |
} |
| 63 |
|
| 64 |
watch.quit <- struct{}{} |
| 65 |
watch.watcher.Close() |
| 66 |
|
| 67 |
watch.watcher = nil |
| 68 |
watch.events = nil |
| 69 |
} |
| 70 |
|
| 71 |
func (watch *watch) add(path string) { |
| 72 |
if watch.watcher == nil { |
| 73 |
return |
| 74 |
} |
| 75 |
|
| 76 |
// ignore /dev since write updates to /dev/tty causes high cpu usage |
| 77 |
if path == "/dev" { |
| 78 |
return |
| 79 |
} |
| 80 |
|
| 81 |
if err := watch.watcher.Add(path); err != nil { |
| 82 |
log.Printf("watch path %s: %s", path, err) |
| 83 |
return |
| 84 |
} |
| 85 |
|
| 86 |
watch.pathsLock.Lock() |
| 87 |
watch.paths[path] = true |
| 88 |
watch.pathsLock.Unlock() |
| 89 |
} |
| 90 |
|
| 91 |
func (watch *watch) loop() { |
| 92 |
for { |
| 93 |
select { |
| 94 |
case ev := <-watch.events: |
| 95 |
if ev.Has(fsnotify.Create) { |
| 96 |
for _, path := range watch.getSameDirs(filepath.Dir(ev.Name)) { |
| 97 |
watch.addUpdate(watchUpdate{"dir", path}) |
| 98 |
watch.addUpdate(watchUpdate{"file", path}) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) { |
| 103 |
dir, file := filepath.Split(ev.Name) |
| 104 |
for _, path := range watch.getSameDirs(dir) { |
| 105 |
watch.delChan <- filepath.Join(path, file) |
| 106 |
watch.addUpdate(watchUpdate{"dir", path}) |
| 107 |
watch.addUpdate(watchUpdate{"file", path}) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if ev.Has(fsnotify.Write) || ev.Has(fsnotify.Chmod) { |
| 112 |
// skip write updates for the log file, otherwise it is possible |
| 113 |
// to have an infinite loop where writing to the log file causes |
| 114 |
// it to be reloaded, which in turn triggers more events that |
| 115 |
// are then logged |
| 116 |
if ev.Name == gLogPath && ev.Has(fsnotify.Write) { |
| 117 |
continue |
| 118 |
} |
| 119 |
|
| 120 |
dir, file := filepath.Split(ev.Name) |
| 121 |
for _, path := range watch.getSameDirs(dir) { |
| 122 |
watch.addUpdate(watchUpdate{"file", filepath.Join(path, file)}) |
| 123 |
} |
| 124 |
} |
| 125 |
case update := <-watch.timeout: |
| 126 |
if watch.pending[update] { |
| 127 |
watch.processUpdate(update) |
| 128 |
time.AfterFunc(100*time.Millisecond, func() { watch.timeout <- update }) |
| 129 |
watch.pending[update] = false |
| 130 |
} else { |
| 131 |
delete(watch.pending, update) |
| 132 |
} |
| 133 |
case <-watch.quit: |
| 134 |
watch.pathsLock.Lock() |
| 135 |
clear(watch.paths) |
| 136 |
watch.pathsLock.Unlock() |
| 137 |
return |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
type watchUpdate struct { |
| 143 |
kind string |
| 144 |
path string |
| 145 |
} |
| 146 |
|
| 147 |
func (watch *watch) addUpdate(update watchUpdate) { |
| 148 |
// process an update immediately if is the first time, otherwise store it |
| 149 |
// and process only after a timeout to reduce the number of actual loads |
| 150 |
if _, ok := watch.pending[update]; !ok { |
| 151 |
watch.processUpdate(update) |
| 152 |
time.AfterFunc(100*time.Millisecond, func() { watch.timeout <- update }) |
| 153 |
watch.pending[update] = false |
| 154 |
} else { |
| 155 |
watch.pending[update] = true |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func (watch *watch) processUpdate(update watchUpdate) { |
| 160 |
switch update.kind { |
| 161 |
case "dir": |
| 162 |
if _, err := os.Lstat(update.path); err == nil { |
| 163 |
watch.dirChan <- newDir(update.path) |
| 164 |
} |
| 165 |
case "file": |
| 166 |
if _, err := os.Lstat(update.path); err == nil { |
| 167 |
watch.fileChan <- newFile(update.path) |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// fsnotify silently aliases watches by inode, so two paths to the same |
| 173 |
// directory (a symlink and its target) end up as a single entry in |
| 174 |
// watcher.WatchList. Track every Added path ourselves so getSameDirs can |
| 175 |
// fan events out to all aliases. |
| 176 |
func (watch *watch) getSameDirs(dir string) []string { |
| 177 |
var paths []string |
| 178 |
|
| 179 |
dirStat, err := os.Stat(dir) |
| 180 |
if err != nil { |
| 181 |
return nil |
| 182 |
} |
| 183 |
|
| 184 |
watch.pathsLock.Lock() |
| 185 |
all := slices.Collect(maps.Keys(watch.paths)) |
| 186 |
watch.pathsLock.Unlock() |
| 187 |
|
| 188 |
for _, path := range all { |
| 189 |
if path == dir { |
| 190 |
paths = append(paths, path) |
| 191 |
continue |
| 192 |
} |
| 193 |
|
| 194 |
stat, err := os.Stat(path) |
| 195 |
if err != nil { |
| 196 |
continue |
| 197 |
} |
| 198 |
|
| 199 |
if os.SameFile(stat, dirStat) { |
| 200 |
paths = append(paths, path) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return paths |
| 205 |
} |