github.com/anchore/syft@v1.38.2/syft/file/resolver.go (about)

     1  package file
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  )
     7  
     8  // Resolver is an interface that encompasses how to get specific file references and file contents for a generic data source.
     9  type Resolver interface {
    10  	ContentResolver
    11  	PathResolver
    12  	LocationResolver
    13  	MetadataResolver
    14  }
    15  
    16  // ContentResolver knows how to get file content for a given Location
    17  type ContentResolver interface {
    18  	FileContentsByLocation(Location) (io.ReadCloser, error)
    19  }
    20  
    21  // MetadataResolver provides file metadata lookup by location.
    22  type MetadataResolver interface {
    23  	FileMetadataByLocation(Location) (Metadata, error)
    24  }
    25  
    26  // PathResolver knows how to get a Location for given string paths and globs
    27  type PathResolver interface {
    28  	// HasPath indicates if the given path exists in the underlying source.
    29  	// The implementation for this may vary, however, generally the following considerations should be made:
    30  	// - full symlink resolution should be performed on all requests
    31  	// - returns locations for any file or directory
    32  	HasPath(string) bool
    33  
    34  	// FilesByPath fetches a set of file references which have the given path (for an image, there may be multiple matches).
    35  	// The implementation for this may vary, however, generally the following considerations should be made:
    36  	// - full symlink resolution should be performed on all requests
    37  	// - only returns locations to files (NOT directories)
    38  	FilesByPath(paths ...string) ([]Location, error)
    39  
    40  	// FilesByGlob fetches a set of file references for the given glob matches
    41  	// The implementation for this may vary, however, generally the following considerations should be made:
    42  	// - full symlink resolution should be performed on all requests
    43  	// - if multiple paths to the same file are found, the best single match should be returned
    44  	// - only returns locations to files (NOT directories)
    45  	FilesByGlob(patterns ...string) ([]Location, error)
    46  
    47  	// FilesByMIMEType fetches a set of file references which the contents have been classified as one of the given MIME Types.
    48  	FilesByMIMEType(types ...string) ([]Location, error)
    49  
    50  	// RelativeFileByPath fetches a single file at the given path relative to the layer squash of the given reference.
    51  	// This is helpful when attempting to find a file that is in the same layer or lower as another file.
    52  	RelativeFileByPath(_ Location, path string) *Location
    53  }
    54  
    55  // LocationResolver provides iteration over all file locations in a source.
    56  type LocationResolver interface {
    57  	// AllLocations returns a channel of all file references from the underlying source.
    58  	// The implementation for this may vary, however, generally the following considerations should be made:
    59  	// - NO symlink resolution should be performed on results
    60  	// - returns locations for any file or directory
    61  	AllLocations(ctx context.Context) <-chan Location
    62  }
    63  
    64  // WritableResolver extends Resolver with the ability to write file content.
    65  type WritableResolver interface {
    66  	Resolver
    67  
    68  	Write(location Location, reader io.Reader) error
    69  }