| 1 |
//go:build !darwin |
| 2 |
|
| 3 |
package watchers |
| 4 |
|
| 5 |
import ( |
| 6 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 7 |
"github.com/fsnotify/fsnotify" |
| 8 |
) |
| 9 |
|
| 10 |
func init() { |
| 11 |
RegisterWatcherFactory(newInotifyWatcher) |
| 12 |
} |
| 13 |
|
| 14 |
type inotifyWatcher struct { |
| 15 |
w *fsnotify.Watcher |
| 16 |
ch chan *FSEvent |
| 17 |
} |
| 18 |
|
| 19 |
func newInotifyWatcher() (FSWatcher, error) { |
| 20 |
watcher := &inotifyWatcher{ |
| 21 |
ch: make(chan *FSEvent), |
| 22 |
} |
| 23 |
w, err := fsnotify.NewWatcher() |
| 24 |
if err != nil { |
| 25 |
return nil, err |
| 26 |
} |
| 27 |
watcher.w = w |
| 28 |
|
| 29 |
go watcher.watch() |
| 30 |
return watcher, nil |
| 31 |
} |
| 32 |
|
| 33 |
func (w *inotifyWatcher) watch() { |
| 34 |
defer log.PanicHandler() |
| 35 |
for ev := range w.w.Events { |
| 36 |
// we only care about files being created, removed or renamed |
| 37 |
switch ev.Op { |
| 38 |
case fsnotify.Create: |
| 39 |
w.ch <- &FSEvent{ |
| 40 |
Operation: FSCreate, |
| 41 |
Path: ev.Name, |
| 42 |
} |
| 43 |
case fsnotify.Remove: |
| 44 |
w.ch <- &FSEvent{ |
| 45 |
Operation: FSRemove, |
| 46 |
Path: ev.Name, |
| 47 |
} |
| 48 |
case fsnotify.Rename: |
| 49 |
w.ch <- &FSEvent{ |
| 50 |
Operation: FSRename, |
| 51 |
Path: ev.Name, |
| 52 |
} |
| 53 |
default: |
| 54 |
continue |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func (w *inotifyWatcher) Configure(root string) error { |
| 60 |
return w.w.Add(root) |
| 61 |
} |
| 62 |
|
| 63 |
func (w *inotifyWatcher) Events() chan *FSEvent { |
| 64 |
return w.ch |
| 65 |
} |
| 66 |
|
| 67 |
func (w *inotifyWatcher) Add(p string) error { |
| 68 |
return w.w.Add(p) |
| 69 |
} |
| 70 |
|
| 71 |
func (w *inotifyWatcher) Remove(p string) error { |
| 72 |
return w.w.Remove(p) |
| 73 |
} |