@@ -562,36 +562,49 @@ func getDigestsFromArchive(ctx context.Context, archivePath string) ([]file.Dige
562562}
563563
564564func (j * archiveParser ) getLicenseFromFileInArchive (ctx context.Context ) ([]pkg.License , error ) {
565- var out []pkg.License
566- var licenseMatches []string
567- for _ , f := range j .fileManifest .GlobMatch (true , "/META-INF/*" ) {
568- if licenses .IsLicenseFile (f ) {
569- licenseMatches = append (licenseMatches , f )
570- }
571- }
572- if len (licenseMatches ) == 0 {
573- for _ , f := range j .fileManifest .GlobMatch (true , "/*" ) {
574- if licenses .IsLicenseFile (f ) {
565+ // prefer identified licenses, fall back to unknown
566+ var identified []pkg.License
567+ var unidentified []pkg.License
568+
569+ for _ , glob := range []string {"/META-INF/*" , "/*" } {
570+ var licenseMatches []string
571+ for _ , f := range j .fileManifest .GlobMatch (true , glob ) {
572+ if licenses .IsLicenseFile (path .Base (f )) {
575573 licenseMatches = append (licenseMatches , f )
576574 }
577575 }
578- }
579- if len (licenseMatches ) > 0 {
580- contents , err := intFile .ContentsFromZip (j .archivePath , licenseMatches ... )
581- if err != nil {
582- return nil , fmt .Errorf ("unable to extract java license (%s): %w" , j .location , err )
583- }
584576
585- for _ , licenseMatch := range licenseMatches {
586- licenseContents := contents [licenseMatch ]
587- r := strings .NewReader (licenseContents )
588- lics := pkg .NewLicensesFromReadCloserWithContext (ctx , file .NewLocationReadCloser (j .location , io .NopCloser (r )))
589- if len (lics ) > 0 {
590- out = append (out , lics ... )
577+ if len (licenseMatches ) > 0 {
578+ contents , err := intFile .ContentsFromZip (j .archivePath , licenseMatches ... )
579+ if err != nil {
580+ return nil , fmt .Errorf ("unable to extract java license (%s): %w" , j .location , err )
581+ }
582+
583+ for _ , licenseMatch := range licenseMatches {
584+ licenseContents := contents [licenseMatch ]
585+ r := strings .NewReader (licenseContents )
586+ foundLicenses := pkg .NewLicensesFromReadCloserWithContext (ctx , file .NewLocationReadCloser (j .location , io .NopCloser (r )))
587+ for _ , l := range foundLicenses {
588+ if l .SPDXExpression != "" {
589+ identified = append (identified , l )
590+ } else {
591+ unidentified = append (unidentified , l )
592+ }
593+ }
594+ }
595+
596+ // prefer licenses found in /META-INF
597+ if len (identified ) > 0 {
598+ break
591599 }
592600 }
593601 }
594- return out , nil
602+
603+ if len (identified ) == 0 {
604+ return unidentified , nil
605+ }
606+
607+ return identified , nil
595608}
596609
597610func (j * archiveParser ) discoverPkgsFromNestedArchives (ctx context.Context , parentPkg * pkg.Package ) ([]pkg.Package , []artifact.Relationship , error ) {
0 commit comments