aboutsummaryrefslogtreecommitdiffstats
path: root/lib/watchers/fsevents.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-06-29 16:53:38 +0200
committerRobin Jarry <robin@jarry.cc>2023-08-04 11:32:40 +0200
commita3e811e00d8a7fe0f37d85557d7db60087967171 (patch)
treefc5e3e35487499e9b78a3f4f875f8bfb8f807526 /lib/watchers/fsevents.go
parentd29c9d1a2ff82234ad1810abc6a57199340e7fd5 (diff)
downloadaerc-a3e811e00d8a7fe0f37d85557d7db60087967171.tar.gz
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 'lib/watchers/fsevents.go')
-rw-r--r--lib/watchers/fsevents.go82
1 files changed, 82 insertions, 0 deletions
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
+}