Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fs): handle default skip dirs properly #6628

Merged
merged 2 commits into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions pkg/fanal/walker/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func NewFS() *FS {
// Walk walks the filesystem rooted at root, calling fn for each unfiltered file.
func (w *FS) Walk(root string, opt Option, fn WalkFunc) error {
opt.SkipFiles = w.BuildSkipPaths(root, opt.SkipFiles)
opt.SkipDirs = append(opt.SkipDirs, defaultSkipDirs...)
opt.SkipDirs = w.BuildSkipPaths(root, opt.SkipDirs)
opt.SkipDirs = append(opt.SkipDirs, defaultSkipDirs...)

walkDirFunc := w.WalkDirFunc(root, fn, opt)
walkDirFunc = w.onError(walkDirFunc)
Expand All @@ -50,24 +50,24 @@ func (w *FS) WalkDirFunc(root string, fn WalkFunc, opt Option) fs.WalkDirFunc {
}
relPath = filepath.ToSlash(relPath)

info, err := d.Info()
if err != nil {
return xerrors.Errorf("file info error: %w", err)
}

// Skip unnecessary files
switch {
case info.IsDir():
case d.IsDir():
if SkipPath(relPath, opt.SkipDirs) {
return filepath.SkipDir
}
return nil
case !info.Mode().IsRegular():
case !d.Type().IsRegular():
return nil
case SkipPath(relPath, opt.SkipFiles):
return nil
}

info, err := d.Info()
if err != nil {
return xerrors.Errorf("file info error: %w", err)
}

if err = fn(relPath, info, fileOpener(filePath)); err != nil {
return xerrors.Errorf("failed to analyze file: %w", err)
}
Expand All @@ -83,7 +83,7 @@ func (w *FS) onError(wrapped fs.WalkDirFunc) fs.WalkDirFunc {
// Unwrap fs.SkipDir error
case errors.Is(err, fs.SkipDir):
return fs.SkipDir
// ignore permission errors
// Ignore permission errors
case os.IsPermission(err):
return nil
case err != nil:
Expand Down