diff options
author | Tim Culverhouse <tim@timculverhouse.com> | 2023-03-02 16:46:02 -0600 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-03-07 16:37:02 +0100 |
commit | 27cacdfca581d1f51f3e99a8f94c859fe8576790 (patch) | |
tree | 2a77ea0b05ce11aa845d37e0ee5520fca09a0882 /worker | |
parent | 261c388c7325f07efb58afe97ddcf83e6fa99ce7 (diff) | |
download | aerc-27cacdfca581d1f51f3e99a8f94c859fe8576790.tar.gz |
fswatcher: add a darwin fswatcher implementation
Add a darwin implementation of FSWatcher using the fsevents package. The
implementation is behind a darwin build flag.
Co-authored-by: Ben Cohen <ben@bencohen.net>
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Tested-by: Ben Lee-Cohen <ben@lee-cohen.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker')
-rw-r--r-- | worker/lib/watchers/darwin/darwin.go | 84 | ||||
-rw-r--r-- | worker/maildir/worker.go | 4 | ||||
-rw-r--r-- | worker/watcher_darwin.go | 6 |
3 files changed, 94 insertions, 0 deletions
diff --git a/worker/lib/watchers/darwin/darwin.go b/worker/lib/watchers/darwin/darwin.go new file mode 100644 index 00000000..e20761f3 --- /dev/null +++ b/worker/lib/watchers/darwin/darwin.go @@ -0,0 +1,84 @@ +//go:build darwin +// +build darwin + +package darwin + +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("darwin", 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/maildir/worker.go b/worker/maildir/worker.go index 11bab4ea..a94792bc 100644 --- a/worker/maildir/worker.go +++ b/worker/maildir/worker.go @@ -313,6 +313,10 @@ func (w *Worker) handleConfigure(msg *types.Configure) error { return err } w.c = c + err = w.watcher.Configure(dir) + if err != nil { + return err + } log.Debugf("configured base maildir: %s", dir) return nil } diff --git a/worker/watcher_darwin.go b/worker/watcher_darwin.go new file mode 100644 index 00000000..fa1af712 --- /dev/null +++ b/worker/watcher_darwin.go @@ -0,0 +1,6 @@ +//go:build darwin +// +build darwin + +package worker + +import _ "git.sr.ht/~rjarry/aerc/worker/lib/watchers/darwin" |