Prevent symlinks causing duplicate package-file relationships (#1168)

This commit is contained in:
Justin Chadwell 2022-08-22 15:29:00 +01:00 committed by GitHub
parent 21eb772060
commit f3c3d3d98e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -110,29 +110,36 @@ func packageFileOwnershipRelationships(p pkg.Package, resolver source.FilePathRe
return nil, nil return nil, nil
} }
var relationships []artifact.Relationship locations := map[artifact.ID]source.Location{}
for _, path := range fileOwner.OwnedFiles() { for _, path := range fileOwner.OwnedFiles() {
locations, err := resolver.FilesByPath(path) pathRefs, err := resolver.FilesByPath(path)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to find path for path=%q: %w", path, err) return nil, fmt.Errorf("unable to find path for path=%q: %w", path, err)
} }
if len(locations) == 0 { if len(pathRefs) == 0 {
// ideally we want to warn users about missing files from a package, however, it is very common for // ideally we want to warn users about missing files from a package, however, it is very common for
// container image authors to delete files that are not needed in order to keep image sizes small. Adding // container image authors to delete files that are not needed in order to keep image sizes small. Adding
// a warning here would be needlessly noisy (even for popular base images). // a warning here would be needlessly noisy (even for popular base images).
continue continue
} }
for _, l := range locations { for _, ref := range pathRefs {
relationships = append(relationships, artifact.Relationship{ if oldRef, ok := locations[ref.Coordinates.ID()]; ok {
From: p, log.Debugf("found path duplicate of %s", oldRef.RealPath)
To: l.Coordinates, }
Type: artifact.ContainsRelationship, locations[ref.Coordinates.ID()] = ref
})
} }
} }
var relationships []artifact.Relationship
for _, location := range locations {
relationships = append(relationships, artifact.Relationship{
From: p,
To: location.Coordinates,
Type: artifact.ContainsRelationship,
})
}
return relationships, nil return relationships, nil
} }