mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 23:16:30 +01:00
fix: index file itself when file scan path has symlink (#2359)
Previously, building the index of the filesystem when source was file would fail if part of the path syft was passed to the file included a symlinked directory, resulting in cataloging misses. --------- Signed-off-by: Will Murphy <will.murphy@anchore.com>
This commit is contained in:
@@ -159,11 +159,10 @@ func (s FileSource) FileResolver(_ Scope) (file.Resolver, error) {
|
||||
}
|
||||
isArchiveAnalysis := fi.IsDir()
|
||||
|
||||
absAnalysisPath, err := filepath.Abs(s.analysisPath)
|
||||
absParentDir, err := absoluteSymlinkFreePathToParent(s.analysisPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to get absolute path for analysis path=%q: %w", s.analysisPath, err)
|
||||
return nil, err
|
||||
}
|
||||
absParentDir := filepath.Dir(absAnalysisPath)
|
||||
|
||||
var res *fileresolver.Directory
|
||||
if isArchiveAnalysis {
|
||||
@@ -210,6 +209,18 @@ func (s FileSource) FileResolver(_ Scope) (file.Resolver, error) {
|
||||
return s.resolver, nil
|
||||
}
|
||||
|
||||
func absoluteSymlinkFreePathToParent(path string) (string, error) {
|
||||
absAnalysisPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to get absolute path for analysis path=%q: %w", path, err)
|
||||
}
|
||||
dereferencedAbsAnalysisPath, err := filepath.EvalSymlinks(absAnalysisPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to get absolute path for analysis path=%q: %w", path, err)
|
||||
}
|
||||
return filepath.Dir(dereferencedAbsAnalysisPath), nil
|
||||
}
|
||||
|
||||
func (s *FileSource) Close() error {
|
||||
if s.closer == nil {
|
||||
return nil
|
||||
|
||||
@@ -48,6 +48,22 @@ func TestNewFromFile(t *testing.T) {
|
||||
},
|
||||
expRefs: 1,
|
||||
},
|
||||
{
|
||||
desc: "normal path",
|
||||
input: "test-fixtures/actual-path/empty",
|
||||
testPathFn: func(resolver file.Resolver) ([]file.Location, error) {
|
||||
return resolver.FilesByPath("empty")
|
||||
},
|
||||
expRefs: 1,
|
||||
},
|
||||
{
|
||||
desc: "path containing symlink",
|
||||
input: "test-fixtures/symlink/empty",
|
||||
testPathFn: func(resolver file.Resolver) ([]file.Location, error) {
|
||||
return resolver.FilesByPath("empty")
|
||||
},
|
||||
expRefs: 1,
|
||||
},
|
||||
}
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
actual-path
|
||||
Reference in New Issue
Block a user