From 5407c44e0c143150d9bba3d80db40cb3f842be59 Mon Sep 17 00:00:00 2001 From: Richard Gomez <32133502+rgmz@users.noreply.github.com> Date: Tue, 22 Oct 2024 09:23:10 -0400 Subject: [PATCH] chore: log skipped symlinks (#1591) --- detect/directory.go | 16 +++++++++------- sources/directory.go | 9 +++++++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/detect/directory.go b/detect/directory.go index 9e39f6a2..53fb399b 100644 --- a/detect/directory.go +++ b/detect/directory.go @@ -13,10 +13,10 @@ import ( func (d *Detector) DetectFiles(paths <-chan sources.ScanTarget) ([]report.Finding, error) { for pa := range paths { - p := pa d.Sema.Go(func() error { - - f, err := os.Open(p.Path) + logger := log.With().Str("path", pa.Path).Logger() + log.Trace().Msgf("Scanning path: %s", pa) + f, err := os.Open(pa.Path) if err != nil { return err } @@ -31,7 +31,9 @@ func (d *Detector) DetectFiles(paths <-chan sources.ScanTarget) ([]report.Findin if d.MaxTargetMegaBytes > 0 { rawLength := fileSize / 1000000 if rawLength > int64(d.MaxTargetMegaBytes) { - log.Debug().Msgf("skipping file: %s scan due to size: %d", p.Path, rawLength) + logger.Debug(). + Int64("size", rawLength). + Msgf("Skipping file: exceeds --max-target-megabytes") return nil } } @@ -62,10 +64,10 @@ func (d *Detector) DetectFiles(paths <-chan sources.ScanTarget) ([]report.Findin totalLines += linesInChunk fragment := Fragment{ Raw: string(buf[:n]), - FilePath: p.Path, + FilePath: pa.Path, } - if p.Symlink != "" { - fragment.SymlinkFile = p.Symlink + if pa.Symlink != "" { + fragment.SymlinkFile = pa.Symlink } for _, finding := range d.Detect(fragment) { // need to add 1 since line counting starts at 1 diff --git a/sources/directory.go b/sources/directory.go index 30fe349a..806f052f 100644 --- a/sources/directory.go +++ b/sources/directory.go @@ -1,12 +1,12 @@ package sources import ( + "github.com/rs/zerolog/log" "io/fs" "os" "path/filepath" "github.com/fatih/semgroup" - "github.com/rs/zerolog/log" ) type ScanTarget struct { @@ -35,7 +35,12 @@ func DirectoryTargets(source string, s *semgroup.Group, followSymlinks bool) (<- Symlink: "", } } - if fInfo.Mode().Type() == fs.ModeSymlink && followSymlinks { + if fInfo.Mode().Type() == fs.ModeSymlink { + if !followSymlinks { + log.Debug().Str("path", path).Msg("Skipping symlink") + return nil + } + realPath, err := filepath.EvalSymlinks(path) if err != nil { return err