diff options
| author | RanXom <saiyedshizain@gmail.com> | 2026-07-14 12:35:51 +0530 |
|---|---|---|
| committer | RanXom <saiyedshizain@gmail.com> | 2026-07-14 12:35:51 +0530 |
| commit | d2c0c970f690d52ee3675f377a5eebc22f84afab (patch) | |
| tree | 100d08c4ac01c98f70801c633aa2a95690cc1592 | |
| parent | bd0b89ab49806b976eb0d50dd6f79afdec8b7d11 (diff) | |
| download | galleryd-d2c0c970f690d52ee3675f377a5eebc22f84afab.tar.xz galleryd-d2c0c970f690d52ee3675f377a5eebc22f84afab.zip | |
feat(watcher): watch newly created directories
| -rw-r--r-- | internal/watcher/fsnotify.go | 32 | ||||
| -rw-r--r-- | internal/watcher/watch.go | 26 |
2 files changed, 45 insertions, 13 deletions
diff --git a/internal/watcher/fsnotify.go b/internal/watcher/fsnotify.go index e051c52..7251907 100644 --- a/internal/watcher/fsnotify.go +++ b/internal/watcher/fsnotify.go @@ -3,9 +3,8 @@ package watcher import ( "context" "fmt" - "io/fs" "log" - "path/filepath" + "os" "github.com/fsnotify/fsnotify" ) @@ -24,17 +23,7 @@ func (w *Watcher) Run(ctx context.Context) error { }() 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 { + if err := watchTree(fswatcher, root); err != nil { return err } } @@ -51,6 +40,23 @@ func (w *Watcher) Run(ctx context.Context) error { log.Printf("watcher: %v %s", event.Op, event.Name) + 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 diff --git a/internal/watcher/watch.go b/internal/watcher/watch.go new file mode 100644 index 0000000..95146ce --- /dev/null +++ b/internal/watcher/watch.go @@ -0,0 +1,26 @@ +package watcher + +import ( + "io/fs" + "path/filepath" + + "github.com/fsnotify/fsnotify" +) + +// watchTree recursively watches root and all of its subdirectories. +func watchTree( + w *fsnotify.Watcher, + root string, +) error { + return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if !d.IsDir() { + return nil + } + + return w.Add(path) + }) +} |