github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/internal/binutils/branching_matcher.go (about)

     1  package binutils
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/anchore/syft/internal"
     7  	"github.com/anchore/syft/syft/internal/unionreader"
     8  	"github.com/anchore/syft/syft/pkg"
     9  )
    10  
    11  // BranchingEvidenceMatcher loads the contents of the reader into memory, assuming that all
    12  // classifier matchers are going to be reading the same file, e.g. a "java" binary, continuing to
    13  // try classifier EvidenceMatcher until the first set of packages is found
    14  func BranchingEvidenceMatcher(classifiers ...Classifier) EvidenceMatcher {
    15  	return func(_ Classifier, context MatcherContext) ([]pkg.Package, error) {
    16  		// we are scanning the same contents multiple times, so read it into memory for a reader that can reset
    17  		rdr, err := getReader(context)
    18  		if err != nil {
    19  			return nil, err
    20  		}
    21  		defer internal.CloseAndLogError(rdr, context.Location.RealPath)
    22  		if err != nil {
    23  			return nil, err
    24  		}
    25  		for _, c := range classifiers {
    26  			pkgs, err := c.EvidenceMatcher(c, MatcherContext{
    27  				Resolver: context.Resolver,
    28  				Location: context.Location,
    29  				GetReader: func(_ MatcherContext) (unionreader.UnionReader, error) {
    30  					_, err := rdr.Seek(0, io.SeekStart)
    31  					if err != nil {
    32  						return nil, err
    33  					}
    34  					return &nonClosingUnionReader{rdr}, nil
    35  				},
    36  			})
    37  			if len(pkgs) > 0 || err != nil {
    38  				return pkgs, err
    39  			}
    40  		}
    41  		return nil, nil
    42  	}
    43  }
    44  
    45  type nonClosingUnionReader struct {
    46  	unionreader.UnionReader
    47  }
    48  
    49  func (c *nonClosingUnionReader) Close() error {
    50  	return nil
    51  }