aboutsummaryrefslogtreecommitdiff
path: root/worker/lib/watchers/fsevents.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-03-09 22:26:03 +0100
committerRobin Jarry <robin@jarry.cc>2023-03-09 22:29:48 +0100
commit5a6a11ddad23e24ffaf2eabaef60f07c20138992 (patch)
tree2724d705377aeb645b525f548ad6c4306b603640 /worker/lib/watchers/fsevents.go
parent96d85a905f33c5b85b558c1059284db719292096 (diff)
downloadaerc-fs.tar.gz
aerc-fs.zip
fswatcher: fix bsd supportfs
Fix the following error when running maildir on freebsd: could not create file system watcher: Unsupported OS: freebsd Do not register based on os type. Register based on supported API. Rename linux -> inotify and darwin -> fsevents. Only build fsevents on darwin, and inotify on all other platforms. Fixes: a0935a3de0ce ("worker/lib: implement an fswatcher interface") Reported-by: Jens Grassel <jens@wegtam.com> Signed-off-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker/lib/watchers/fsevents.go')
-rw-r--r--worker/lib/watchers/fsevents.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/worker/lib/watchers/fsevents.go b/worker/lib/watchers/fsevents.go
new file mode 100644
index 00000000..7b657999
--- /dev/null
+++ b/worker/lib/watchers/fsevents.go
@@ -0,0 +1,84 @@
+//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
+}