diff options
| author | RanXom <saiyedshizain@gmail.com> | 2026-07-14 12:03:34 +0530 |
|---|---|---|
| committer | RanXom <saiyedshizain@gmail.com> | 2026-07-14 12:03:34 +0530 |
| commit | b336f4a1328e24ac77a1cfee8c52b18caa555620 (patch) | |
| tree | 991aecc2f1366ea621f5866f03913851db566c13 /internal/watcher/fsnotify.go | |
| parent | 9fd2f9eabe93713c0e8fb7dd094e77109db84265 (diff) | |
| download | galleryd-b336f4a1328e24ac77a1cfee8c52b18caa555620.tar.xz galleryd-b336f4a1328e24ac77a1cfee8c52b18caa555620.zip | |
feat(watcher): watch configured directories
Diffstat (limited to 'internal/watcher/fsnotify.go')
| -rw-r--r-- | internal/watcher/fsnotify.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go new file mode 100644 index 0000000..e051c52 --- /dev/null +++ b/internal/watcher/fsnotify.go @@ -0,0 +1,62 @@ +package watcher + +import ( + "context" + "fmt" + "io/fs" + "log" + "path/filepath" + + "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() + }() + + for _, root := range w.config.Roots { + if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if !d.IsDir() { + return nil + } + + return fswatcher.Add(path) + }); 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) + + case err, ok := <-fswatcher.Errors: + if !ok { + return nil + } + + log.Printf("watcher error: %v", err) + } + } +} |