Skip to content

Commit

Permalink
🐛 Fix find files panic for FS connections. (#4258)
Browse files Browse the repository at this point in the history
Signed-off-by: Preslav <preslav@mondoo.com>
  • Loading branch information
preslavgerchev authored Jun 18, 2024
1 parent cd82178 commit 1a4e816
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
7 changes: 6 additions & 1 deletion providers/os/fs/find_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ func FindFiles(iofs fs.FS, from string, r *regexp.Regexp, typ string, perm *uint
matcher := createFindFilesMatcher(iofs, typ, r, perm)
matchedPaths := []string{}
err := fs.WalkDir(iofs, from, func(p string, d fs.DirEntry, err error) error {
if err := handleFsError(err); err != nil {
skip, err := handleFsError(err)
if err != nil {
return err
}
if skip {
return nil
}

if matcher.Match(p, d.Type()) {
matchedPaths = append(matchedPaths, p)
}
Expand Down
11 changes: 6 additions & 5 deletions providers/os/fs/find_files_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ import (
"golang.org/x/sys/unix"
)

// handleFsError checks if the error is a permission denied or non-existent file error and returns nil in such cases.
func handleFsError(err error) error {
// handleFsError checks if the error is a permission denied or non-existent file error and returns nil in such cases. the bool
// indicates if the file should be skipped
func handleFsError(err error) (bool, error) {
if err != nil {
// Check for denied permissions and non-existent files. This can sometimes happen, especially for procfs
// We don't want to error out in such cases. We can safely skip over the file.
if errors.Is(err, os.ErrPermission) || errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrInvalid) || errors.Is(err, unix.EINVAL) {
return nil
return true, nil
}
return err
return true, err
}
return nil
return false, nil
}
8 changes: 4 additions & 4 deletions providers/os/fs/find_files_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
)

// handleFsError checks if the error is a permission denied or non-existent file error and returns nil in such cases.
func handleFsError(err error) error {
func handleFsError(err error) (bool, error) {
if err != nil {
// Check for denied permissions and non-existent files. This can sometimes happen, especially for procfs
// We don't want to error out in such cases. We can safely skip over the file.
if errors.Is(err, os.ErrPermission) || errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrInvalid) {
return nil
return true, nil
}
return err
return true, err
}
return nil
return false, nil
}

0 comments on commit 1a4e816

Please sign in to comment.