| 1 |
//go:build darwin |
| 2 |
|
| 3 |
package watchers |
| 4 |
|
| 5 |
import ( |
| 6 |
"time" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 9 |
"github.com/fsnotify/fsevents" |
| 10 |
) |
| 11 |
|
| 12 |
func init() { |
| 13 |
RegisterWatcherFactory(newDarwinWatcher) |
| 14 |
} |
| 15 |
|
| 16 |
type darwinWatcher struct { |
| 17 |
ch chan *FSEvent |
| 18 |
w *fsevents.EventStream |
| 19 |
watcherCh chan []fsevents.Event |
| 20 |
} |
| 21 |
|
| 22 |
func newDarwinWatcher() (FSWatcher, error) { |
| 23 |
watcher := &darwinWatcher{ |
| 24 |
watcherCh: make(chan []fsevents.Event), |
| 25 |
ch: make(chan *FSEvent), |
| 26 |
w: &fsevents.EventStream{ |
| 27 |
Flags: fsevents.WatchRoot, |
| 28 |
Latency: 500 * time.Millisecond, |
| 29 |
}, |
| 30 |
} |
| 31 |
return watcher, nil |
| 32 |
} |
| 33 |
|
| 34 |
func (w *darwinWatcher) watch() { |
| 35 |
defer log.PanicHandler() |
| 36 |
for events := range w.w.Events { |
| 37 |
for _, ev := range events { |
| 38 |
switch { |
| 39 |
case ev.Flags&fsevents.ItemCreated > 0: |
| 40 |
w.ch <- &FSEvent{ |
| 41 |
Operation: FSCreate, |
| 42 |
Path: ev.Path, |
| 43 |
} |
| 44 |
case ev.Flags&fsevents.ItemRenamed > 0: |
| 45 |
w.ch <- &FSEvent{ |
| 46 |
Operation: FSRename, |
| 47 |
Path: ev.Path, |
| 48 |
} |
| 49 |
case ev.Flags&fsevents.ItemRemoved > 0: |
| 50 |
w.ch <- &FSEvent{ |
| 51 |
Operation: FSRemove, |
| 52 |
Path: ev.Path, |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func (w *darwinWatcher) Configure(root string) error { |
| 60 |
dev, err := fsevents.DeviceForPath(root) |
| 61 |
if err != nil { |
| 62 |
return err |
| 63 |
} |
| 64 |
w.w.Device = dev |
| 65 |
w.w.Paths = []string{root} |
| 66 |
start_err := w.w.Start() |
| 67 |
if start_err != nil { |
| 68 |
return start_err |
| 69 |
} |
| 70 |
go w.watch() |
| 71 |
return nil |
| 72 |
} |
| 73 |
|
| 74 |
func (w *darwinWatcher) Events() chan *FSEvent { |
| 75 |
return w.ch |
| 76 |
} |
| 77 |
|
| 78 |
func (w *darwinWatcher) Add(p string) error { |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
func (w *darwinWatcher) Remove(p string) error { |
| 83 |
return nil |
| 84 |
} |