fix: separate golang license caches from mod dir (#2852)

Signed-off-by: Keith Zantow <kzantow@gmail.com>
This commit is contained in:
Keith Zantow
2024-06-12 19:12:35 -04:00
committed by GitHub
parent dd723bb3c5
commit ca0cc52d47
31 changed files with 1496 additions and 149 deletions
@@ -0,0 +1,33 @@
package directorysource
import (
"os"
"strings"
"github.com/anchore/syft/internal/cache"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/internal/fileresolver"
)
// we do not want to cache things and then subsequently scan them, if, for example a user runs `syft /` twice
func excludeCachePathVisitors() []fileresolver.PathIndexVisitor {
var out []fileresolver.PathIndexVisitor
for _, dir := range cache.GetManager().RootDirs() {
out = append(out, excludeCacheDirPathVisitor{
dir: dir,
}.excludeCacheDir)
}
return out
}
type excludeCacheDirPathVisitor struct {
dir string
}
func (d excludeCacheDirPathVisitor) excludeCacheDir(_, path string, _ os.FileInfo, _ error) error {
if strings.HasPrefix(path, d.dir) {
log.Tracef("skipping cache path: %s", path)
return fileresolver.ErrSkipPath
}
return nil
}
@@ -142,6 +142,9 @@ func (s *directorySource) FileResolver(_ source.Scope) (file.Resolver, error) {
return nil, err
}
// this should be the only file resolver that might have overlap with where files are cached
exclusionFunctions = append(exclusionFunctions, excludeCachePathVisitors()...)
res, err := fileresolver.NewFromDirectory(s.config.Path, s.config.Base, exclusionFunctions...)
if err != nil {
return nil, fmt.Errorf("unable to create directory resolver: %w", err)