Skip to content

Commit

Permalink
vm: improve inotify file comparison (abiosoft#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
abiosoft authored Mar 20, 2023
1 parent 30efd6c commit e60e116
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions daemon/process/inotify/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}

Expand Down

0 comments on commit e60e116

Please sign in to comment.