Skip to content

Commit 3b0eaba

Browse files
dobracjakubno
authored andcommitted
Scan folder recursively on watch addition (#2)
1 parent cec3ac2 commit 3b0eaba

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

backend_inotify.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,35 @@ func (w *inotify) handleEvent(inEvent *unix.InotifyEvent, buf *[65536]byte, offs
511511
}
512512
}
513513
}
514+
// Register a newly created dir tree
515+
// This is for "mkdir -p one/two/three".
516+
// Usually all those directories will be created before we can set up
517+
// watchers on the subdirectories, so only "one" would be sent
518+
// as a Create event and not "one/two" and "one/two/three"
519+
// (inotifywait -r has the same problem).
520+
err = filepath.WalkDir(ev.Name, func(curDir string, d fs.DirEntry, err error) error {
521+
if err != nil {
522+
return err
523+
}
524+
525+
if ev.Name != curDir {
526+
// Send artificial create event.
527+
// We don't know what has really happened.
528+
529+
// Send the previous event first to maintain proper ordering, then create a new event for the current directory
530+
// The function expects to return only one event, so we need to send the previous event first and then create a new one
531+
w.sendEvent(ev)
532+
ev = w.newEvent(curDir, unix.IN_CREATE, 0)
533+
}
534+
535+
if d.IsDir() {
536+
return w.register(curDir, watch.flags, flagRecurse)
537+
}
538+
return nil
539+
})
540+
if !w.sendError(err) {
541+
return Event{}, false
542+
}
514543
}
515544
}
516545

0 commit comments

Comments
 (0)