diff --git a/daemon/process/inotify/watch.go b/daemon/process/inotify/watch.go index 963ef1efd..44e47559f 100644 --- a/daemon/process/inotify/watch.go +++ b/daemon/process/inotify/watch.go @@ -11,21 +11,26 @@ import ( "github.com/sirupsen/logrus" ) +type modTime struct { + path string // filename + time.Time +} + func (f *inotifyProcess) watchFiles(ctx context.Context) error { log := f.log log.Trace("begin inotify watcher") fileMap := map[string]time.Time{} - changed := make(chan string) + mod := make(chan modTime) - go func(changed chan string) { + go func(mod chan modTime) { var last time.Time for { select { case <-ctx.Done(): - close(changed) + close(mod) return - case path := <-changed: + case ev := <-mod: now := time.Now() if now.Sub(last) < time.Millisecond*500 { continue @@ -34,15 +39,15 @@ func (f *inotifyProcess) watchFiles(ctx context.Context) error { // construct date time in the format YYYY-MM-DD HH:MM:SS // for busybox - t := now.Add(-5 * time.Second).Format("2006-01-02 15:04:05") - log.Infof("setting modtime to %s for %s ", t, path) - if err := f.guest.Run("/bin/touch", "-d", t, path); err != nil { + t := ev.Format("2006-01-02 15:04:05") + log.Infof("setting modtime to %s for %s ", t, ev.path) + if err := f.guest.Run("/bin/touch", "-d", t, ev.path); err != nil { log.Error(err) } } } - }(changed) + }(mod) for { select { @@ -65,14 +70,14 @@ func (f *inotifyProcess) watchFiles(ctx context.Context) error { continue } - if err := doWatch(dirs, fileMap, changed); err != nil { + if err := doWatch(dirs, fileMap, mod); err != nil { log.Errorf("error during directory watch: %v", err) } } } } -func doWatch(dirs []string, fileMap map[string]time.Time, changed chan<- string) error { +func doWatch(dirs []string, fileMap map[string]time.Time, changed chan<- modTime) error { for _, dir := range dirs { err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { if err != nil { @@ -97,10 +102,10 @@ func doWatch(dirs []string, fileMap map[string]time.Time, changed chan<- string) currentTime, ok := fileMap[path] newTime := info.ModTime() - if ok && newTime.After(currentTime) { + if ok && newTime.After(currentTime.Add(time.Millisecond*500)) { go func(path string) { logrus.Tracef("changed file modTime %v->%v: %s", currentTime, newTime, path) - changed <- path + changed <- modTime{path: path, Time: newTime} }(path) }