Skip to content

Commit 82acf5e

Browse files
committed
Skip the folder in recursive mode if the user doesn't have permissions
1 parent a3a856f commit 82acf5e

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
@@ -562,6 +573,13 @@ func (w *inotify) handleEvent(inEvent *unix.InotifyEvent, buf *[65536]byte, offs
562573
ev = w.newEvent(curDir, unix.IN_CREATE, 0)
563574
}
564575

576+
// If there's a dir for which we don't have read permissions,
577+
// skip it silently. If it's the root directory itself, return an error.
578+
_, infoErr := d.Info()
579+
if infoErr != nil && errors.Is(infoErr, os.ErrPermission) {
580+
return filepath.SkipDir // Skip this directory silently.
581+
}
582+
565583
if d.IsDir() {
566584
return w.register(curDir, watch.flags, flagRecurse)
567585
}

0 commit comments

Comments
 (0)