mirror of
https://github.com/anchore/syft.git
synced 2025-11-18 00:43:20 +01:00
* split source.Location and create source.Coordinates for minimal path addressing Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * move coordinates into separate file Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * Update syft/source/coordinates.go Co-authored-by: Dan Luhring <luhring@users.noreply.github.com>
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package file
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"io"
|
|
|
|
"github.com/anchore/syft/internal"
|
|
|
|
"github.com/anchore/syft/internal/log"
|
|
"github.com/anchore/syft/syft/source"
|
|
)
|
|
|
|
type ContentsCataloger struct {
|
|
globs []string
|
|
skipFilesAboveSizeInBytes int64
|
|
}
|
|
|
|
func NewContentsCataloger(globs []string, skipFilesAboveSize int64) (*ContentsCataloger, error) {
|
|
return &ContentsCataloger{
|
|
globs: globs,
|
|
skipFilesAboveSizeInBytes: skipFilesAboveSize,
|
|
}, nil
|
|
}
|
|
|
|
func (i *ContentsCataloger) Catalog(resolver source.FileResolver) (map[source.Coordinates]string, error) {
|
|
results := make(map[source.Coordinates]string)
|
|
var locations []source.Location
|
|
|
|
locations, err := resolver.FilesByGlob(i.globs...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, location := range locations {
|
|
metadata, err := resolver.FileMetadataByLocation(location)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if i.skipFilesAboveSizeInBytes > 0 && metadata.Size > i.skipFilesAboveSizeInBytes {
|
|
continue
|
|
}
|
|
|
|
result, err := i.catalogLocation(resolver, location)
|
|
if internal.IsErrPathPermission(err) {
|
|
log.Debugf("file contents cataloger skipping - %+v", err)
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
results[location.Coordinates] = result
|
|
}
|
|
log.Debugf("file contents cataloger processed %d files", len(results))
|
|
|
|
return results, nil
|
|
}
|
|
|
|
func (i *ContentsCataloger) catalogLocation(resolver source.FileResolver, location source.Location) (string, error) {
|
|
contentReader, err := resolver.FileContentsByLocation(location)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer internal.CloseAndLogError(contentReader, location.VirtualPath)
|
|
|
|
buf := &bytes.Buffer{}
|
|
if _, err = io.Copy(base64.NewEncoder(base64.StdEncoding, buf), contentReader); err != nil {
|
|
return "", internal.ErrPath{Path: location.RealPath, Err: err}
|
|
}
|
|
|
|
return buf.String(), nil
|
|
}
|