aboutsummaryrefslogtreecommitdiff
path: root/internal/watcher/fsnotify.go
diff options
context:
space:
mode:
authorSaiyed Shizain <saiyedshizain@gmail.com>2026-07-17 11:10:53 +0530
committerGitHub <noreply@github.com>2026-07-17 11:10:53 +0530
commit6afb80ab754da137939dd523ee419818026f9fb5 (patch)
treef9f94ec69ac87abe8060cefca8708572bc13bdd6 /internal/watcher/fsnotify.go
parentbcfb199ac8e5399e1a92cfbbc28af3674f80959d (diff)
parent5ae787be3d1d9de3355f8b5662fea857204e693c (diff)
downloadgalleryd-6afb80ab754da137939dd523ee419818026f9fb5.tar.xz
galleryd-6afb80ab754da137939dd523ee419818026f9fb5.zip
Merge pull request #11 from RanXom/feat/live-gallery
Feat/live-gallery: live gallery with automatic filesystem reloads
Diffstat (limited to 'internal/watcher/fsnotify.go')
-rw-r--r--internal/watcher/fsnotify.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go
new file mode 100644
index 0000000..b759c50
--- /dev/null
+++ b/internal/watcher/fsnotify.go
@@ -0,0 +1,72 @@
+package watcher
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "os"
+
+ "github.com/fsnotify/fsnotify"
+)
+
+// Run starts watching the configured roots.
+//
+// Filesystem events are currently logged for debugging.
+// Future commits will debounce these events and invoke OnChange.
+func (w *Watcher) Run(ctx context.Context) error {
+ fswatcher, err := fsnotify.NewWatcher()
+ if err != nil {
+ return fmt.Errorf("create watcher: %w", err)
+ }
+ defer func() {
+ _ = fswatcher.Close()
+ }()
+
+ go w.debounceLoop(ctx)
+
+ for _, root := range w.config.Roots {
+ if err := watchTree(fswatcher, root); err != nil {
+ return err
+ }
+ }
+
+ for {
+ select {
+ case <-ctx.Done():
+ return nil
+
+ case event, ok := <-fswatcher.Events:
+ if !ok {
+ return nil
+ }
+
+ log.Printf("watcher: %v %s", event.Op, event.Name)
+
+ w.notify()
+
+ if !event.Has(fsnotify.Create) {
+ continue
+ }
+
+ info, err := os.Stat(event.Name)
+ if err != nil {
+ continue
+ }
+
+ if !info.IsDir() {
+ continue
+ }
+
+ if err := watchTree(fswatcher, event.Name); err != nil {
+ log.Printf("watch new directory: %v", err)
+ }
+
+ case err, ok := <-fswatcher.Errors:
+ if !ok {
+ return nil
+ }
+
+ log.Printf("watcher error: %v", err)
+ }
+ }
+}