aboutsummaryrefslogtreecommitdiff
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
parentd29c9d1a2ff82234ad1810abc6a57199340e7fd5 (diff)
downloadaerc-a3e811e00d8a7fe0f37d85557d7db60087967171.tar.gz
aerc-a3e811e00d8a7fe0f37d85557d7db60087967171.zip
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>
-rw-r--r--lib/watchers/fsevents.go (renamed from worker/lib/watchers/fsevents.go)24
-rw-r--r--lib/watchers/inotify.go (renamed from worker/lib/watchers/inotify.go)24
-rw-r--r--lib/watchers/watchers.go (renamed from worker/types/watcher.go)22
-rw-r--r--worker/handlers/register.go16
-rw-r--r--worker/maildir/worker.go7
-rw-r--r--worker/notmuch/worker.go5
-rw-r--r--worker/worker_enabled.go1
7 files changed, 50 insertions, 49 deletions
diff --git a/worker/lib/watchers/fsevents.go b/lib/watchers/fsevents.go
index 7b657999..905db2af 100644
--- a/worker/lib/watchers/fsevents.go
+++ b/lib/watchers/fsevents.go
@@ -7,25 +7,23 @@ 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)
+ RegisterWatcherFactory(newDarwinWatcher)
}
type darwinWatcher struct {
- ch chan *types.FSEvent
+ ch chan *FSEvent
w *fsevents.EventStream
watcherCh chan []fsevents.Event
}
-func newDarwinWatcher() (types.FSWatcher, error) {
+func newDarwinWatcher() (FSWatcher, error) {
watcher := &darwinWatcher{
watcherCh: make(chan []fsevents.Event),
- ch: make(chan *types.FSEvent),
+ ch: make(chan *FSEvent),
w: &fsevents.EventStream{
Flags: fsevents.FileEvents | fsevents.WatchRoot,
Latency: 500 * time.Millisecond,
@@ -40,18 +38,18 @@ func (w *darwinWatcher) watch() {
for _, ev := range events {
switch {
case ev.Flags&fsevents.ItemCreated > 0:
- w.ch <- &types.FSEvent{
- Operation: types.FSCreate,
+ w.ch <- &FSEvent{
+ Operation: FSCreate,
Path: ev.Path,
}
case ev.Flags&fsevents.ItemRenamed > 0:
- w.ch <- &types.FSEvent{
- Operation: types.FSRename,
+ w.ch <- &FSEvent{
+ Operation: FSRename,
Path: ev.Path,
}
case ev.Flags&fsevents.ItemRemoved > 0:
- w.ch <- &types.FSEvent{
- Operation: types.FSRemove,
+ w.ch <- &FSEvent{
+ Operation: FSRemove,
Path: ev.Path,
}
}
@@ -71,7 +69,7 @@ func (w *darwinWatcher) Configure(root string) error {
return nil
}
-func (w *darwinWatcher) Events() chan *types.FSEvent {
+func (w *darwinWatcher) Events() chan *FSEvent {
return w.ch
}
diff --git a/worker/lib/watchers/inotify.go b/lib/watchers/inotify.go
index 027ef4f2..22290307 100644
--- a/worker/lib/watchers/inotify.go
+++ b/lib/watchers/inotify.go
@@ -5,23 +5,21 @@ package watchers
import (
"git.sr.ht/~rjarry/aerc/log"
- "git.sr.ht/~rjarry/aerc/worker/handlers"
- "git.sr.ht/~rjarry/aerc/worker/types"
"github.com/fsnotify/fsnotify"
)
func init() {
- handlers.RegisterWatcherFactory(newInotifyWatcher)
+ RegisterWatcherFactory(newInotifyWatcher)
}
type inotifyWatcher struct {
w *fsnotify.Watcher
- ch chan *types.FSEvent
+ ch chan *FSEvent
}
-func newInotifyWatcher() (types.FSWatcher, error) {
+func newInotifyWatcher() (FSWatcher, error) {
watcher := &inotifyWatcher{
- ch: make(chan *types.FSEvent),
+ ch: make(chan *FSEvent),
}
w, err := fsnotify.NewWatcher()
if err != nil {
@@ -39,18 +37,18 @@ func (w *inotifyWatcher) watch() {
// we only care about files being created, removed or renamed
switch ev.Op {
case fsnotify.Create:
- w.ch <- &types.FSEvent{
- Operation: types.FSCreate,
+ w.ch <- &FSEvent{
+ Operation: FSCreate,
Path: ev.Name,
}
case fsnotify.Remove:
- w.ch <- &types.FSEvent{
- Operation: types.FSRemove,
+ w.ch <- &FSEvent{
+ Operation: FSRemove,
Path: ev.Name,
}
case fsnotify.Rename:
- w.ch <- &types.FSEvent{
- Operation: types.FSRename,
+ w.ch <- &FSEvent{
+ Operation: FSRename,
Path: ev.Name,
}
default:
@@ -63,7 +61,7 @@ func (w *inotifyWatcher) Configure(root string) error {
return w.w.Add(root)
}
-func (w *inotifyWatcher) Events() chan *types.FSEvent {
+func (w *inotifyWatcher) Events() chan *FSEvent {
return w.ch
}
diff --git a/worker/types/watcher.go b/lib/watchers/watchers.go
index b06d12d3..06ef985c 100644
--- a/worker/types/watcher.go
+++ b/lib/watchers/watchers.go
@@ -1,4 +1,9 @@
-package types
+package watchers
+
+import (
+ "fmt"
+ "runtime"
+)
// FSWatcher is a file system watcher
type FSWatcher interface {
@@ -22,3 +27,18 @@ type FSEvent struct {
Operation FSOperation
Path string
}
+
+type WatcherFactoryFunc func() (FSWatcher, error)
+
+var watcherFactory WatcherFactoryFunc
+
+func RegisterWatcherFactory(fn WatcherFactoryFunc) {
+ watcherFactory = fn
+}
+
+func NewWatcher() (FSWatcher, error) {
+ if watcherFactory == nil {
+ return nil, fmt.Errorf("Unsupported OS: %s", runtime.GOOS)
+ }
+ return watcherFactory()
+}
diff --git a/worker/handlers/register.go b/worker/handlers/register.go
index 4123665f..c871f07b 100644
--- a/worker/handlers/register.go
+++ b/worker/handlers/register.go
@@ -2,7 +2,6 @@ package handlers
import (
"fmt"
- "runtime"
"git.sr.ht/~rjarry/aerc/worker/types"
)
@@ -26,18 +25,3 @@ func GetHandlerForScheme(scheme string, worker *types.Worker) (types.Backend, er
}
return backend, nil
}
-
-type WatcherFactoryFunc func() (types.FSWatcher, error)
-
-var watcherFactory WatcherFactoryFunc
-
-func RegisterWatcherFactory(fn WatcherFactoryFunc) {
- watcherFactory = fn
-}
-
-func NewWatcher() (types.FSWatcher, error) {
- if watcherFactory == nil {
- return nil, fmt.Errorf("Unsupported OS: %s", runtime.GOOS)
- }
- return watcherFactory()
-}
diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go
index 7e75a8ca..e8da6fbd 100644
--- a/worker/maildir/worker.go
+++ b/worker/maildir/worker.go
@@ -22,6 +22,7 @@ import (
aercLib "git.sr.ht/~rjarry/aerc/lib"
"git.sr.ht/~rjarry/aerc/lib/iterator"
+ "git.sr.ht/~rjarry/aerc/lib/watchers"
"git.sr.ht/~rjarry/aerc/log"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/handlers"
@@ -44,7 +45,7 @@ type Worker struct {
selectedName string
selectedInfo *models.DirectoryInfo
worker types.WorkerInteractor
- watcher types.FSWatcher
+ watcher watchers.FSWatcher
watcherDebounce *time.Timer
fsEvents chan struct{}
currentSortCriteria []*types.SortCriterion
@@ -56,7 +57,7 @@ type Worker struct {
// NewWorker creates a new maildir worker with the provided worker.
func NewWorker(worker *types.Worker) (types.Backend, error) {
- watch, err := handlers.NewWatcher()
+ watch, err := watchers.NewWatcher()
if err != nil {
return nil, fmt.Errorf("could not create file system watcher: %w", err)
}
@@ -73,7 +74,7 @@ func NewWorker(worker *types.Worker) (types.Backend, error) {
// NewMaildirppWorker creates a new Maildir++ worker with the provided worker.
func NewMaildirppWorker(worker *types.Worker) (types.Backend, error) {
- watch, err := handlers.NewWatcher()
+ watch, err := watchers.NewWatcher()
if err != nil {
return nil, fmt.Errorf("could not create file system watcher: %w", err)
}
diff --git a/worker/notmuch/worker.go b/worker/notmuch/worker.go
index 1e5f4c2d..cf58d987 100644
--- a/worker/notmuch/worker.go
+++ b/worker/notmuch/worker.go
@@ -19,6 +19,7 @@ import (
"time"
"git.sr.ht/~rjarry/aerc/config"
+ "git.sr.ht/~rjarry/aerc/lib/watchers"
"git.sr.ht/~rjarry/aerc/log"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/handlers"
@@ -45,7 +46,7 @@ type worker struct {
db *notmuch.DB
setupErr error
currentSortCriteria []*types.SortCriterion
- watcher types.FSWatcher
+ watcher watchers.FSWatcher
watcherDebounce *time.Timer
capabilities *models.Capabilities
headers []string
@@ -55,7 +56,7 @@ type worker struct {
// NewWorker creates a new notmuch worker with the provided worker.
func NewWorker(w *types.Worker) (types.Backend, error) {
events := make(chan eventType, 20)
- watcher, err := handlers.NewWatcher()
+ watcher, err := watchers.NewWatcher()
if err != nil {
return nil, fmt.Errorf("could not create file system watcher: %w", err)
}
diff --git a/worker/worker_enabled.go b/worker/worker_enabled.go
index e53c06ea..1eafe408 100644
--- a/worker/worker_enabled.go
+++ b/worker/worker_enabled.go
@@ -4,7 +4,6 @@ package worker
import (
_ "git.sr.ht/~rjarry/aerc/worker/imap"
_ "git.sr.ht/~rjarry/aerc/worker/jmap"
- _ "git.sr.ht/~rjarry/aerc/worker/lib/watchers"
_ "git.sr.ht/~rjarry/aerc/worker/maildir"
_ "git.sr.ht/~rjarry/aerc/worker/mbox"
)