From a3e811e00d8a7fe0f37d85557d7db60087967171 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Thu, 29 Jun 2023 16:53:38 +0200 Subject: watchers: move filesystem monitoring stuff in lib No functional change. This will allow reuse in other parts of aerc. Signed-off-by: Robin Jarry Tested-by: Koni Marti --- lib/watchers/fsevents.go | 82 ++++++++++++++++++++++++++++++++++++++++ lib/watchers/inotify.go | 74 ++++++++++++++++++++++++++++++++++++ lib/watchers/watchers.go | 44 +++++++++++++++++++++ worker/handlers/register.go | 16 -------- worker/lib/watchers/fsevents.go | 84 ----------------------------------------- worker/lib/watchers/inotify.go | 76 ------------------------------------- worker/maildir/worker.go | 7 ++-- worker/notmuch/worker.go | 5 ++- worker/types/watcher.go | 24 ------------ worker/worker_enabled.go | 1 - 10 files changed, 207 insertions(+), 206 deletions(-) create mode 100644 lib/watchers/fsevents.go create mode 100644 lib/watchers/inotify.go create mode 100644 lib/watchers/watchers.go delete mode 100644 worker/lib/watchers/fsevents.go delete mode 100644 worker/lib/watchers/inotify.go delete mode 100644 worker/types/watcher.go diff --git a/lib/watchers/fsevents.go b/lib/watchers/fsevents.go new file mode 100644 index 00000000..905db2af --- /dev/null +++ b/lib/watchers/fsevents.go @@ -0,0 +1,82 @@ +//go:build darwin +// +build darwin + +package watchers + +import ( + "time" + + "git.sr.ht/~rjarry/aerc/log" + "github.com/fsnotify/fsevents" +) + +func init() { + RegisterWatcherFactory(newDarwinWatcher) +} + +type darwinWatcher struct { + ch chan *FSEvent + w *fsevents.EventStream + watcherCh chan []fsevents.Event +} + +func newDarwinWatcher() (FSWatcher, error) { + watcher := &darwinWatcher{ + watcherCh: make(chan []fsevents.Event), + ch: make(chan *FSEvent), + w: &fsevents.EventStream{ + Flags: fsevents.FileEvents | fsevents.WatchRoot, + Latency: 500 * time.Millisecond, + }, + } + return watcher, nil +} + +func (w *darwinWatcher) watch() { + defer log.PanicHandler() + for events := range w.w.Events { + for _, ev := range events { + switch { + case ev.Flags&fsevents.ItemCreated > 0: + w.ch <- &FSEvent{ + Operation: FSCreate, + Path: ev.Path, + } + case ev.Flags&fsevents.ItemRenamed > 0: + w.ch <- &FSEvent{ + Operation: FSRename, + Path: ev.Path, + } + case ev.Flags&fsevents.ItemRemoved > 0: + w.ch <- &FSEvent{ + Operation: FSRemove, + Path: ev.Path, + } + } + } + } +} + +func (w *darwinWatcher) Configure(root string) error { + dev, err := fsevents.DeviceForPath(root) + if err != nil { + return err + } + w.w.Device = dev + w.w.Paths = []string{root} + w.w.Start() + go w.watch() + return nil +} + +func (w *darwinWatcher) Events() chan *FSEvent { + return w.ch +} + +func (w *darwinWatcher) Add(p string) error { + return nil +} + +func (w *darwinWatcher) Remove(p string) error { + return nil +} diff --git a/lib/watchers/inotify.go b/lib/watchers/inotify.go new file mode 100644 index 00000000..22290307 --- /dev/null +++ b/lib/watchers/inotify.go @@ -0,0 +1,74 @@ +//go:build !darwin +// +build !darwin + +package watchers + +import ( + "git.sr.ht/~rjarry/aerc/log" + "github.com/fsnotify/fsnotify" +) + +func init() { + RegisterWatcherFactory(newInotifyWatcher) +} + +type inotifyWatcher struct { + w *fsnotify.Watcher + ch chan *FSEvent +} + +func newInotifyWatcher() (FSWatcher, error) { + watcher := &inotifyWatcher{ + ch: make(chan *FSEvent), + } + w, err := fsnotify.NewWatcher() + if err != nil { + return nil, err + } + watcher.w = w + + go watcher.watch() + return watcher, nil +} + +func (w *inotifyWatcher) watch() { + defer log.PanicHandler() + for ev := range w.w.Events { + // we only care about files being created, removed or renamed + switch ev.Op { + case fsnotify.Create: + w.ch <- &FSEvent{ + Operation: FSCreate, + Path: ev.Name, + } + case fsnotify.Remove: + w.ch <- &FSEvent{ + Operation: FSRemove, + Path: ev.Name, + } + case fsnotify.Rename: + w.ch <- &FSEvent{ + Operation: FSRename, + Path: ev.Name, + } + default: + continue + } + } +} + +func (w *inotifyWatcher) Configure(root string) error { + return w.w.Add(root) +} + +func (w *inotifyWatcher) Events() chan *FSEvent { + return w.ch +} + +func (w *inotifyWatcher) Add(p string) error { + return w.w.Add(p) +} + +func (w *inotifyWatcher) Remove(p string) error { + return w.w.Remove(p) +} diff --git a/lib/watchers/watchers.go b/lib/watchers/watchers.go new file mode 100644 index 00000000..06ef985c --- /dev/null +++ b/lib/watchers/watchers.go @@ -0,0 +1,44 @@ +package watchers + +import ( + "fmt" + "runtime" +) + +// FSWatcher is a file system watcher +type FSWatcher interface { + Configure(string) error + Events() chan *FSEvent + // Adds a directory or file to the watcher + Add(string) error + // Removes a directory or file from the watcher + Remove(string) error +} + +type FSOperation int + +const ( + FSCreate FSOperation = iota + FSRemove + FSRename +) + +type FSEvent struct { + Operation FSOperation + Path string +} + +type WatcherFactoryFunc func() (FSWatcher, error) + +var watcherFactory WatcherFactoryFunc + +func RegisterWatcherFactory(fn WatcherFactoryFunc) { + watcherFactory = fn +} + +func NewWatcher() (FSWatcher, error) { + if watcherFactory == nil { + return nil, fmt.Errorf("Unsupported OS: %s", runtime.GOOS) + } + return watcherFactory() +} diff --git a/worker/handlers/register.go b/worker/handlers/register.go index 4123665f..c871f07b 100644 --- a/worker/handlers/register.go +++ b/worker/handlers/register.go @@ -2,7 +2,6 @@ package handlers import ( "fmt" - "runtime" "git.sr.ht/~rjarry/aerc/worker/types" ) @@ -26,18 +25,3 @@ func GetHandlerForScheme(scheme string, worker *types.Worker) (types.Backend, er } return backend, nil } - -type WatcherFactoryFunc func() (types.FSWatcher, error) - -var watcherFactory WatcherFactoryFunc - -func RegisterWatcherFactory(fn WatcherFactoryFunc) { - watcherFactory = fn -} - -func NewWatcher() (types.FSWatcher, error) { - if watcherFactory == nil { - return nil, fmt.Errorf("Unsupported OS: %s", runtime.GOOS) - } - return watcherFactory() -} diff --git a/worker/lib/watchers/fsevents.go b/worker/lib/watchers/fsevents.go deleted file mode 100644 index 7b657999..00000000 --- a/worker/lib/watchers/fsevents.go +++ /dev/null @@ -1,84 +0,0 @@ -//go:build darwin -// +build darwin - -package watchers - -import ( - "time" - - "git.sr.ht/~rjarry/aerc/log" - "git.sr.ht/~rjarry/aerc/worker/handlers" - "git.sr.ht/~rjarry/aerc/worker/types" - "github.com/fsnotify/fsevents" -) - -func init() { - handlers.RegisterWatcherFactory(newDarwinWatcher) -} - -type darwinWatcher struct { - ch chan *types.FSEvent - w *fsevents.EventStream - watcherCh chan []fsevents.Event -} - -func newDarwinWatcher() (types.FSWatcher, error) { - watcher := &darwinWatcher{ - watcherCh: make(chan []fsevents.Event), - ch: make(chan *types.FSEvent), - w: &fsevents.EventStream{ - Flags: fsevents.FileEvents | fsevents.WatchRoot, - Latency: 500 * time.Millisecond, - }, - } - return watcher, nil -} - -func (w *darwinWatcher) watch() { - defer log.PanicHandler() - for events := range w.w.Events { - for _, ev := range events { - switch { - case ev.Flags&fsevents.ItemCreated > 0: - w.ch <- &types.FSEvent{ - Operation: types.FSCreate, - Path: ev.Path, - } - case ev.Flags&fsevents.ItemRenamed > 0: - w.ch <- &types.FSEvent{ - Operation: types.FSRename, - Path: ev.Path, - } - case ev.Flags&fsevents.ItemRemoved > 0: - w.ch <- &types.FSEvent{ - Operation: types.FSRemove, - Path: ev.Path, - } - } - } - } -} - -func (w *darwinWatcher) Configure(root string) error { - dev, err := fsevents.DeviceForPath(root) - if err != nil { - return err - } - w.w.Device = dev - w.w.Paths = []string{root} - w.w.Start() - go w.watch() - return nil -} - -func (w *darwinWatcher) Events() chan *types.FSEvent { - return w.ch -} - -func (w *darwinWatcher) Add(p string) error { - return nil -} - -func (w *darwinWatcher) Remove(p string) error { - return nil -} diff --git a/worker/lib/watchers/inotify.go b/worker/lib/watchers/inotify.go deleted file mode 100644 index 027ef4f2..00000000 --- a/worker/lib/watchers/inotify.go +++ /dev/null @@ -1,76 +0,0 @@ -//go:build !darwin -// +build !darwin - -package watchers - -import ( - "git.sr.ht/~rjarry/aerc/log" - "git.sr.ht/~rjarry/aerc/worker/handlers" - "git.sr.ht/~rjarry/aerc/worker/types" - "github.com/fsnotify/fsnotify" -) - -func init() { - handlers.RegisterWatcherFactory(newInotifyWatcher) -} - -type inotifyWatcher struct { - w *fsnotify.Watcher - ch chan *types.FSEvent -} - -func newInotifyWatcher() (types.FSWatcher, error) { - watcher := &inotifyWatcher{ - ch: make(chan *types.FSEvent), - } - w, err := fsnotify.NewWatcher() - if err != nil { - return nil, err - } - watcher.w = w - - go watcher.watch() - return watcher, nil -} - -func (w *inotifyWatcher) watch() { - defer log.PanicHandler() - for ev := range w.w.Events { - // we only care about files being created, removed or renamed - switch ev.Op { - case fsnotify.Create: - w.ch <- &types.FSEvent{ - Operation: types.FSCreate, - Path: ev.Name, - } - case fsnotify.Remove: - w.ch <- &types.FSEvent{ - Operation: types.FSRemove, - Path: ev.Name, - } - case fsnotify.Rename: - w.ch <- &types.FSEvent{ - Operation: types.FSRename, - Path: ev.Name, - } - default: - continue - } - } -} - -func (w *inotifyWatcher) Configure(root string) error { - return w.w.Add(root) -} - -func (w *inotifyWatcher) Events() chan *types.FSEvent { - return w.ch -} - -func (w *inotifyWatcher) Add(p string) error { - return w.w.Add(p) -} - -func (w *inotifyWatcher) Remove(p string) error { - return w.w.Remove(p) -} diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go index 7e75a8ca..e8da6fbd 100644 --- a/worker/maildir/worker.go +++ b/worker/maildir/worker.go @@ -22,6 +22,7 @@ import ( aercLib "git.sr.ht/~rjarry/aerc/lib" "git.sr.ht/~rjarry/aerc/lib/iterator" + "git.sr.ht/~rjarry/aerc/lib/watchers" "git.sr.ht/~rjarry/aerc/log" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/handlers" @@ -44,7 +45,7 @@ type Worker struct { selectedName string selectedInfo *models.DirectoryInfo worker types.WorkerInteractor - watcher types.FSWatcher + watcher watchers.FSWatcher watcherDebounce *time.Timer fsEvents chan struct{} currentSortCriteria []*types.SortCriterion @@ -56,7 +57,7 @@ type Worker struct { // NewWorker creates a new maildir worker with the provided worker. func NewWorker(worker *types.Worker) (types.Backend, error) { - watch, err := handlers.NewWatcher() + watch, err := watchers.NewWatcher() if err != nil { return nil, fmt.Errorf("could not create file system watcher: %w", err) } @@ -73,7 +74,7 @@ func NewWorker(worker *types.Worker) (types.Backend, error) { // NewMaildirppWorker creates a new Maildir++ worker with the provided worker. func NewMaildirppWorker(worker *types.Worker) (types.Backend, error) { - watch, err := handlers.NewWatcher() + watch, err := watchers.NewWatcher() if err != nil { return nil, fmt.Errorf("could not create file system watcher: %w", err) } diff --git a/worker/notmuch/worker.go b/worker/notmuch/worker.go index 1e5f4c2d..cf58d987 100644 --- a/worker/notmuch/worker.go +++ b/worker/notmuch/worker.go @@ -19,6 +19,7 @@ import ( "time" "git.sr.ht/~rjarry/aerc/config" + "git.sr.ht/~rjarry/aerc/lib/watchers" "git.sr.ht/~rjarry/aerc/log" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/handlers" @@ -45,7 +46,7 @@ type worker struct { db *notmuch.DB setupErr error currentSortCriteria []*types.SortCriterion - watcher types.FSWatcher + watcher watchers.FSWatcher watcherDebounce *time.Timer capabilities *models.Capabilities headers []string @@ -55,7 +56,7 @@ type worker struct { // NewWorker creates a new notmuch worker with the provided worker. func NewWorker(w *types.Worker) (types.Backend, error) { events := make(chan eventType, 20) - watcher, err := handlers.NewWatcher() + watcher, err := watchers.NewWatcher() if err != nil { return nil, fmt.Errorf("could not create file system watcher: %w", err) } diff --git a/worker/types/watcher.go b/worker/types/watcher.go deleted file mode 100644 index b06d12d3..00000000 --- a/worker/types/watcher.go +++ /dev/null @@ -1,24 +0,0 @@ -package types - -// FSWatcher is a file system watcher -type FSWatcher interface { - Configure(string) error - Events() chan *FSEvent - // Adds a directory or file to the watcher - Add(string) error - // Removes a directory or file from the watcher - Remove(string) error -} - -type FSOperation int - -const ( - FSCreate FSOperation = iota - FSRemove - FSRename -) - -type FSEvent struct { - Operation FSOperation - Path string -} diff --git a/worker/worker_enabled.go b/worker/worker_enabled.go index e53c06ea..1eafe408 100644 --- a/worker/worker_enabled.go +++ b/worker/worker_enabled.go @@ -4,7 +4,6 @@ package worker import ( _ "git.sr.ht/~rjarry/aerc/worker/imap" _ "git.sr.ht/~rjarry/aerc/worker/jmap" - _ "git.sr.ht/~rjarry/aerc/worker/lib/watchers" _ "git.sr.ht/~rjarry/aerc/worker/maildir" _ "git.sr.ht/~rjarry/aerc/worker/mbox" ) -- cgit v1.2.3-54-g00ecf