mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
(feat): support skipping archive extraction with file source (#3795)
Signed-off-by: Adam McClenaghan <adam@mcclenaghan.co.uk>
This commit is contained in:
parent
df18edf905
commit
61a3d1784a
@ -30,6 +30,7 @@ type Config struct {
|
|||||||
Exclude source.ExcludeConfig
|
Exclude source.ExcludeConfig
|
||||||
DigestAlgorithms []crypto.Hash
|
DigestAlgorithms []crypto.Hash
|
||||||
Alias source.Alias
|
Alias source.Alias
|
||||||
|
SkipExtractArchive bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type fileSource struct {
|
type fileSource struct {
|
||||||
@ -58,7 +59,7 @@ func New(cfg Config) (source.Source, error) {
|
|||||||
return nil, fmt.Errorf("given path is a directory: %q", cfg.Path)
|
return nil, fmt.Errorf("given path is a directory: %q", cfg.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
analysisPath, cleanupFn := fileAnalysisPath(cfg.Path)
|
analysisPath, cleanupFn := fileAnalysisPath(cfg.Path, cfg.SkipExtractArchive)
|
||||||
|
|
||||||
var digests []file.Digest
|
var digests []file.Digest
|
||||||
if len(cfg.DigestAlgorithms) > 0 {
|
if len(cfg.DigestAlgorithms) > 0 {
|
||||||
@ -206,9 +207,15 @@ func (s *fileSource) Close() error {
|
|||||||
|
|
||||||
// fileAnalysisPath returns the path given, or in the case the path is an archive, the location where the archive
|
// fileAnalysisPath returns the path given, or in the case the path is an archive, the location where the archive
|
||||||
// contents have been made available. A cleanup function is provided for any temp files created (if any).
|
// contents have been made available. A cleanup function is provided for any temp files created (if any).
|
||||||
func fileAnalysisPath(path string) (string, func() error) {
|
// Users can disable unpacking archives, allowing individual cataloguers to extract them instead (where
|
||||||
var analysisPath = path
|
// supported)
|
||||||
|
func fileAnalysisPath(path string, skipExtractArchive bool) (string, func() error) {
|
||||||
var cleanupFn = func() error { return nil }
|
var cleanupFn = func() error { return nil }
|
||||||
|
var analysisPath = path
|
||||||
|
|
||||||
|
if skipExtractArchive {
|
||||||
|
return analysisPath, cleanupFn
|
||||||
|
}
|
||||||
|
|
||||||
// if the given file is an archive (as indicated by the file extension and not MIME type) then unarchive it and
|
// if the given file is an archive (as indicated by the file extension and not MIME type) then unarchive it and
|
||||||
// use the contents as the source. Note: this does NOT recursively unarchive contents, only the given path is
|
// use the contents as the source. Note: this does NOT recursively unarchive contents, only the given path is
|
||||||
|
|||||||
@ -106,6 +106,7 @@ func TestNewFromFile_WithArchive(t *testing.T) {
|
|||||||
expRefs int
|
expRefs int
|
||||||
layer2 bool
|
layer2 bool
|
||||||
contents string
|
contents string
|
||||||
|
skipExtractArchive bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "path detected",
|
desc: "path detected",
|
||||||
@ -121,14 +122,25 @@ func TestNewFromFile_WithArchive(t *testing.T) {
|
|||||||
layer2: true,
|
layer2: true,
|
||||||
contents: "Another .vimrc file",
|
contents: "Another .vimrc file",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
desc: "skip extract archive",
|
||||||
|
input: "test-fixtures/path-detected",
|
||||||
|
inputPaths: []string{"/.vimrc"},
|
||||||
|
expRefs: 0,
|
||||||
|
layer2: false,
|
||||||
|
skipExtractArchive: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
archivePath := setupArchiveTest(t, test.input, test.layer2)
|
archivePath := setupArchiveTest(t, test.input, test.layer2)
|
||||||
|
|
||||||
src, err := New(Config{
|
cfg := Config{
|
||||||
Path: archivePath,
|
Path: archivePath,
|
||||||
})
|
SkipExtractArchive: test.skipExtractArchive,
|
||||||
|
}
|
||||||
|
|
||||||
|
src, err := New(cfg)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
require.NoError(t, src.Close())
|
require.NoError(t, src.Close())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user