diff options
author | Robin Jarry <robin@jarry.cc> | 2023-06-29 16:53:38 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-08-04 11:32:40 +0200 |
commit | a3e811e00d8a7fe0f37d85557d7db60087967171 (patch) | |
tree | fc5e3e35487499e9b78a3f4f875f8bfb8f807526 /worker | |
parent | d29c9d1a2ff82234ad1810abc6a57199340e7fd5 (diff) | |
download | aerc-a3e811e00d8a7fe0f37d85557d7db60087967171.tar.gz aerc-a3e811e00d8a7fe0f37d85557d7db60087967171.zip |
watchers: move filesystem monitoring stuff in lib
No functional change. This will allow reuse in other parts of aerc.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Koni Marti <koni.marti@gmail.com>
Diffstat (limited to 'worker')
-rw-r--r-- | worker/handlers/register.go | 16 | ||||
-rw-r--r-- | worker/lib/watchers/fsevents.go | 84 | ||||
-rw-r--r-- | worker/lib/watchers/inotify.go | 76 | ||||
-rw-r--r-- | worker/maildir/worker.go | 7 | ||||
-rw-r--r-- | worker/notmuch/worker.go | 5 | ||||
-rw-r--r-- | worker/types/watcher.go | 24 | ||||
-rw-r--r-- | worker/worker_enabled.go | 1 |
7 files changed, 7 insertions, 206 deletions
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" ) |