From e350e0b7352afd3f82da28b91591f597e1dfdc41 Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Sat, 22 Jun 2024 16:29:05 +1000 Subject: [PATCH] Use `fsnotify` for watching file updates (#1667) * Add watch option * Implement directory changes * Implement file changes * Fix for dircounts * Remove test code * fix dircounts * Set watches on cd instead of select * Set watches on startup too * Watch newly created directories * Add chmod events * Avoid using multiple threads Seems to cause issues if there are multiple threads and each one of them is loading the same new file, in which case there is a race condition. * Reduce logic when updating files * Throttle write updates * Use timer instead of ticker * Fix file write out-of-band timing issue * Fix cursor when renaming * Prevent file showing at bottom during rename * Watch new directory when created * Revert "Watch new directory when created" This reverts commit 68568043136f7f379c81fb0e3583daf7023d4006. * Reapply "Watch new directory when created" This reverts commit 1c29f349812320f58ca60ae558ef2a56a90e6331. * Try batching renew updates * Big rewrite * Reload individual directories * Change delay to 100 milliseconds * Use separate goroutine for loading files/dirs * Fix filter resetting * Add documentation --- app.go | 50 ++++++++++++++++ doc.md | 6 ++ eval.go | 11 ++++ go.mod | 1 + go.sum | 2 + nav.go | 172 ++++++++++++++++++++++++++++--------------------------- opts.go | 2 + watch.go | 125 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 285 insertions(+), 84 deletions(-) create mode 100644 watch.go diff --git a/app.go b/app.go index 90bc771b..04974052 100644 --- a/app.go +++ b/app.go @@ -34,6 +34,7 @@ type app struct { menuComps []string menuCompInd int selectionOut []string + watch *watch } func newApp(ui *ui, nav *nav) *app { @@ -44,6 +45,7 @@ func newApp(ui *ui, nav *nav) *app { nav: nav, ticker: new(time.Ticker), quitChan: quitChan, + watch: newWatch(nav.dirChan, nav.fileChan), } sigChan := make(chan os.Signal, 1) @@ -382,6 +384,8 @@ func (app *app) loop() { if ok { d.ind = prev.ind d.pos = prev.pos + d.filter = prev.filter + d.sort() d.sel(prev.name(), app.nav.height) } @@ -414,6 +418,8 @@ func (app *app) loop() { } } + app.setWatchPaths() + app.ui.draw(app.nav) case r := <-app.nav.regChan: app.nav.regCache[r.path] = r @@ -425,6 +431,31 @@ func (app *app) loop() { } } + app.ui.draw(app.nav) + case f := <-app.nav.fileChan: + dirs := app.nav.dirs + if app.ui.dirPrev != nil { + dirs = append(dirs, app.ui.dirPrev) + } + + for _, dir := range dirs { + if dir.path != filepath.Dir(f.path) { + continue + } + + for i := range dir.allFiles { + if dir.allFiles[i].path == f.path { + dir.allFiles[i] = f + break + } + } + + name := dir.name() + dir.sort() + dir.sel(name, app.nav.height) + } + + app.ui.loadFile(app, false) app.ui.draw(app.nav) case ev := <-app.ui.evChan: e := app.ui.readEvent(ev, app.nav) @@ -591,3 +622,22 @@ func (app *app) runShell(s string, args []string, prefix string) { }() } } + +func (app *app) setWatchPaths() { + if !gOpts.watch || len(app.nav.dirs) == 0 { + return + } + + paths := make(map[string]bool) + for _, dir := range app.nav.dirs { + paths[dir.path] = true + } + + for _, file := range app.nav.currDir().allFiles { + if file.IsDir() { + paths[file.path] = true + } + } + + app.watch.set(paths) +} diff --git a/doc.md b/doc.md index 39abf258..5092a9b6 100644 --- a/doc.md +++ b/doc.md @@ -202,6 +202,7 @@ The following options can be used to customize the behavior of lf: truncatechar string (default '~') truncatepct int (default 100) waitmsg string (default 'Press any key to continue') + watch bool (default false) wrapscan bool (default true) wrapscroll bool (default false) user_{option} string (default none) @@ -1016,6 +1017,11 @@ while a value of 0 will only show the end of the filename, e.g.: String shown after commands of shell-wait type. +## watch (bool) (default false) + +Watch the filesystem for changes using `fsnotify` to automatically refresh file information. +FUSE is currently not supported due to limitations in `fsnotify`. + ## wrapscan (bool) (default true) Searching can wrap around the file list. diff --git a/eval.go b/eval.go index 2de71745..b11ff6d7 100644 --- a/eval.go +++ b/eval.go @@ -196,6 +196,16 @@ func (e *setExpr) eval(app *app, args []string) { app.ui.sort() app.ui.loadFile(app, true) } + case "watch", "nowatch", "watch!": + err = applyBoolOpt(&gOpts.watch, e) + if err == nil { + if gOpts.watch { + app.watch.start() + app.setWatchPaths() + } else { + app.watch.stop() + } + } case "wrapscan", "nowrapscan", "wrapscan!": err = applyBoolOpt(&gOpts.wrapscan, e) case "wrapscroll", "nowrapscroll", "wrapscroll!": @@ -563,6 +573,7 @@ func preChdir(app *app) { func onChdir(app *app) { app.nav.addJumpList() + app.setWatchPaths() if cmd, ok := gOpts.cmds["on-cd"]; ok { cmd.eval(app, nil) } diff --git a/go.mod b/go.mod index 9e88e881..3659dfb8 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/djherbis/times v1.6.0 + github.com/fsnotify/fsnotify v1.7.0 github.com/gdamore/tcell/v2 v2.7.4 github.com/mattn/go-runewidth v0.0.15 golang.org/x/sys v0.21.0 diff --git a/go.sum b/go.sum index cda2b255..7c00b68d 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c= github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU= diff --git a/nav.go b/nav.go index 7a5fba6e..008466fd 100644 --- a/nav.go +++ b/nav.go @@ -39,6 +39,84 @@ type file struct { err error } +func newFile(path string) *file { + lstat, err := os.Lstat(path) + + if err != nil { + log.Printf("getting file information: %s", err) + return &file{ + FileInfo: &fakeStat{name: filepath.Base(path)}, + linkState: notLink, + linkTarget: "", + path: path, + dirCount: -1, + dirSize: -1, + accessTime: time.Unix(0, 0), + changeTime: time.Unix(0, 0), + ext: "", + err: err, + } + } + + var linkState linkState + var linkTarget string + + if lstat.Mode()&os.ModeSymlink != 0 { + stat, err := os.Stat(path) + if err == nil { + linkState = working + lstat = stat + } else { + linkState = broken + } + linkTarget, err = os.Readlink(path) + if err != nil { + log.Printf("reading link target: %s", err) + } + } + + ts := times.Get(lstat) + at := ts.AccessTime() + var ct time.Time + // from times docs: ChangeTime() panics unless HasChangeTime() is true + if ts.HasChangeTime() { + ct = ts.ChangeTime() + } else { + // fall back to ModTime if ChangeTime cannot be determined + ct = lstat.ModTime() + } + + dirCount := -1 + if lstat.IsDir() && gOpts.dircounts { + d, err := os.Open(path) + if err != nil { + dirCount = -2 + } else { + names, err := d.Readdirnames(1000) + d.Close() + + if names == nil && err != io.EOF { + dirCount = -2 + } else { + dirCount = len(names) + } + } + } + + return &file{ + FileInfo: lstat, + linkState: linkState, + linkTarget: linkTarget, + path: path, + dirCount: dirCount, + dirSize: -1, + accessTime: at, + changeTime: ct, + ext: getFileExtension(lstat), + err: nil, + } +} + func (file *file) TotalSize() int64 { if file.IsDir() { if file.dirSize >= 0 { @@ -70,87 +148,7 @@ func readdir(path string) ([]*file, error) { files := make([]*file, 0, len(names)) for _, fname := range names { - fpath := filepath.Join(path, fname) - - lstat, err := os.Lstat(fpath) - - if os.IsNotExist(err) { - continue - } - if err != nil { - log.Printf("getting file information: %s", err) - files = append(files, &file{ - FileInfo: &fakeStat{name: fname}, - linkState: notLink, - linkTarget: "", - path: fpath, - dirCount: -1, - dirSize: -1, - accessTime: time.Unix(0, 0), - changeTime: time.Unix(0, 0), - ext: "", - err: err, - }) - continue - } - - var linkState linkState - var linkTarget string - - if lstat.Mode()&os.ModeSymlink != 0 { - stat, err := os.Stat(fpath) - if err == nil { - linkState = working - lstat = stat - } else { - linkState = broken - } - linkTarget, err = os.Readlink(fpath) - if err != nil { - log.Printf("reading link target: %s", err) - } - } - - ts := times.Get(lstat) - at := ts.AccessTime() - var ct time.Time - // from times docs: ChangeTime() panics unless HasChangeTime() is true - if ts.HasChangeTime() { - ct = ts.ChangeTime() - } else { - // fall back to ModTime if ChangeTime cannot be determined - ct = lstat.ModTime() - } - - dirCount := -1 - if lstat.IsDir() && gOpts.dircounts { - d, err := os.Open(fpath) - if err != nil { - dirCount = -2 - } else { - names, err := d.Readdirnames(1000) - d.Close() - - if names == nil && err != io.EOF { - dirCount = -2 - } else { - dirCount = len(names) - } - } - } - - files = append(files, &file{ - FileInfo: lstat, - linkState: linkState, - linkTarget: linkTarget, - path: fpath, - dirCount: dirCount, - dirSize: -1, - accessTime: at, - changeTime: ct, - ext: getFileExtension(lstat), - err: nil, - }) + files = append(files, newFile(filepath.Join(path, fname))) } return files, err @@ -440,6 +438,7 @@ type nav struct { dirPreviewChan chan *dir dirChan chan *dir regChan chan *reg + fileChan chan *file dirCache map[string]*dir regCache map[string]*reg saves map[string]bool @@ -530,8 +529,6 @@ func (nav *nav) checkDir(dir *dir) { dir.loadTime = now go func() { nd := newDir(dir.path) - nd.filter = dir.filter - nd.sort() if gOpts.dirpreviews { nav.dirPreviewChan <- nd } @@ -587,6 +584,7 @@ func newNav(height int) *nav { dirPreviewChan: make(chan *dir, 1024), dirChan: make(chan *dir), regChan: make(chan *reg), + fileChan: make(chan *file), dirCache: make(map[string]*dir), regCache: make(map[string]*reg), saves: make(map[string]bool), @@ -1564,7 +1562,13 @@ func (nav *nav) rename() error { dir := nav.loadDir(filepath.Dir(newPath)) if dir.loading { - dir.files = append(dir.files, &file{FileInfo: lstat}) + for i := range dir.allFiles { + if dir.allFiles[i].path == oldPath { + dir.allFiles[i] = &file{FileInfo: lstat} + break + } + } + dir.sort() } dir.sel(lstat.Name(), nav.height) diff --git a/opts.go b/opts.go index 171c5d83..66f71a26 100644 --- a/opts.go +++ b/opts.go @@ -66,6 +66,7 @@ var gOpts struct { smartcase bool smartdia bool waitmsg string + watch bool wrapscan bool wrapscroll bool findlen int @@ -213,6 +214,7 @@ func init() { gOpts.smartcase = true gOpts.smartdia = false gOpts.waitmsg = "Press any key to continue" + gOpts.watch = false gOpts.wrapscan = true gOpts.wrapscroll = false gOpts.findlen = 1 diff --git a/watch.go b/watch.go new file mode 100644 index 00000000..0f2b55ec --- /dev/null +++ b/watch.go @@ -0,0 +1,125 @@ +package main + +import ( + "log" + "path/filepath" + "time" + + "github.com/fsnotify/fsnotify" +) + +type watch struct { + watcher *fsnotify.Watcher + events <-chan fsnotify.Event + quit chan struct{} + loads map[string]bool + loadTimer *time.Timer + updates map[string]bool + updateTimer *time.Timer + dirChan chan<- *dir + fileChan chan<- *file +} + +func newWatch(dirChan chan<- *dir, fileChan chan<- *file) *watch { + return &watch{ + quit: make(chan struct{}), + loads: make(map[string]bool), + loadTimer: time.NewTimer(0), + updates: make(map[string]bool), + updateTimer: time.NewTimer(0), + dirChan: dirChan, + fileChan: fileChan, + } +} + +func (watch *watch) start() { + if watch.watcher != nil { + return + } + + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Printf("start watcher: %s", err) + return + } + + watch.watcher = watcher + watch.events = watcher.Events + + go watch.loop() +} + +func (watch *watch) stop() { + if watch.watcher == nil { + return + } + + watch.quit <- struct{}{} + watch.watcher.Close() + + watch.watcher = nil + watch.events = nil +} + +func (watch *watch) set(paths map[string]bool) { + if watch.watcher == nil { + return + } + + for _, path := range watch.watcher.WatchList() { + if !paths[path] { + watch.watcher.Remove(path) + } + } + + for path := range paths { + watch.watcher.Add(path) + } +} + +func (watch *watch) loop() { + for { + select { + case ev := <-watch.events: + if ev.Has(fsnotify.Create) || ev.Has(fsnotify.Remove) || ev.Has(fsnotify.Rename) { + dir := filepath.Dir(ev.Name) + watch.addLoad(dir) + watch.addUpdate(dir) + } + + if ev.Has(fsnotify.Write) || ev.Has(fsnotify.Chmod) { + watch.addUpdate(ev.Name) + } + case <-watch.loadTimer.C: + for path := range watch.loads { + dir := newDir(path) + dir.sort() + watch.dirChan <- dir + } + watch.loads = make(map[string]bool) + case <-watch.updateTimer.C: + for path := range watch.updates { + watch.fileChan <- newFile(path) + } + watch.updates = make(map[string]bool) + case <-watch.quit: + return + } + } +} + +func (watch *watch) addLoad(path string) { + if len(watch.loads) == 0 { + watch.loadTimer.Stop() + watch.loadTimer.Reset(10 * time.Millisecond) + } + watch.loads[path] = true +} + +func (watch *watch) addUpdate(path string) { + if len(watch.updates) == 0 { + watch.updateTimer.Stop() + watch.updateTimer.Reset(10 * time.Millisecond) + } + watch.updates[path] = true +}