Skip to content

Commit 2a5ac7b

Browse files
committed
Skip the folder in recursive mode if the user doesn't have permissions
1 parent 8d0c74f commit 2a5ac7b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

backend_inotify.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ func (w *inotify) AddWith(path string, opts ...addOpt) error {
248248
w.sendEvent(Event{Name: root, Op: Create})
249249
}
250250

251+
// If there's a dir for which we don't have read permissions,
252+
// skip it silently. If it's the root directory itself, return an error.
253+
_, infoErr := d.Info()
254+
if infoErr != nil && errors.Is(infoErr, os.ErrPermission) {
255+
if root == path {
256+
return fmt.Errorf("fsnotify: not a readable directory: %q", path)
257+
}
258+
259+
return filepath.SkipDir // Skip this directory silently.
260+
}
261+
251262
wf := flagRecurse
252263
if root == path {
253264
wf |= flagByUser
@@ -532,6 +543,13 @@ func (w *inotify) handleEvent(inEvent *unix.InotifyEvent, buf *[65536]byte, offs
532543
ev = w.newEvent(curDir, unix.IN_CREATE, 0)
533544
}
534545

546+
// If there's a dir for which we don't have read permissions,
547+
// skip it silently. If it's the root directory itself, return an error.
548+
_, infoErr := d.Info()
549+
if infoErr != nil && errors.Is(infoErr, os.ErrPermission) {
550+
return filepath.SkipDir // Skip this directory silently.
551+
}
552+
535553
if d.IsDir() {
536554
return w.register(curDir, watch.flags, flagRecurse)
537555
}

0 commit comments

Comments
 (0)