github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/kernel/package.go (about)

     1  package kernel
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/anchore/packageurl-go"
     7  	"github.com/anchore/syft/syft/file"
     8  	"github.com/anchore/syft/syft/pkg"
     9  )
    10  
    11  const linuxKernelPackageName = "linux-kernel"
    12  
    13  func newLinuxKernelPackage(metadata pkg.LinuxKernelMetadata, archiveLocation file.Location) pkg.Package {
    14  	p := pkg.Package{
    15  		Name:         linuxKernelPackageName,
    16  		Version:      metadata.Version,
    17  		Locations:    file.NewLocationSet(archiveLocation.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)),
    18  		PURL:         packageURL(linuxKernelPackageName, metadata.Version),
    19  		Type:         pkg.LinuxKernelPkg,
    20  		MetadataType: pkg.LinuxKernelMetadataType,
    21  		Metadata:     metadata,
    22  	}
    23  
    24  	p.SetID()
    25  
    26  	return p
    27  }
    28  
    29  func newLinuxKernelModulePackage(metadata pkg.LinuxKernelModuleMetadata, kmLocation file.Location) pkg.Package {
    30  	p := pkg.Package{
    31  		Name:         metadata.Name,
    32  		Version:      metadata.Version,
    33  		Locations:    file.NewLocationSet(kmLocation.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)),
    34  		Licenses:     pkg.NewLicenseSet(pkg.NewLicensesFromLocation(kmLocation, metadata.License)...),
    35  		PURL:         packageURL(metadata.Name, metadata.Version),
    36  		Type:         pkg.LinuxKernelModulePkg,
    37  		MetadataType: pkg.LinuxKernelModuleMetadataType,
    38  		Metadata:     metadata,
    39  	}
    40  
    41  	p.SetID()
    42  
    43  	return p
    44  }
    45  
    46  // packageURL returns the PURL for the specific Kernel package (see https://github.com/package-url/purl-spec)
    47  func packageURL(name, version string) string {
    48  	var namespace string
    49  
    50  	fields := strings.SplitN(name, "/", 2)
    51  	if len(fields) > 1 {
    52  		namespace = fields[0]
    53  		name = fields[1]
    54  	}
    55  
    56  	return packageurl.NewPackageURL(
    57  		packageurl.TypeGeneric,
    58  		namespace,
    59  		name,
    60  		version,
    61  		nil,
    62  		"",
    63  	).ToString()
    64  }