Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions syft/source/filesource/file_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import (
var _ source.Source = (*fileSource)(nil)

type Config struct {
Path string
Exclude source.ExcludeConfig
DigestAlgorithms []crypto.Hash
Alias source.Alias
Path string
Exclude source.ExcludeConfig
DigestAlgorithms []crypto.Hash
Alias source.Alias
SkipExtractArchive bool
}

type fileSource struct {
Expand Down Expand Up @@ -58,7 +59,7 @@ func New(cfg Config) (source.Source, error) {
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
if len(cfg.DigestAlgorithms) > 0 {
Expand Down Expand Up @@ -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
// contents have been made available. A cleanup function is provided for any temp files created (if any).
func fileAnalysisPath(path string) (string, func() error) {
var analysisPath = path
// Users can disable unpacking archives, allowing individual cataloguers to extract them instead (where
// supported)
func fileAnalysisPath(path string, skipExtractArchive bool) (string, func() error) {
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
// use the contents as the source. Note: this does NOT recursively unarchive contents, only the given path is
Expand Down
32 changes: 22 additions & 10 deletions syft/source/filesource/file_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ func TestNewFromFile_WithArchive(t *testing.T) {
testutil.Chdir(t, "..") // run with source/test-fixtures

testCases := []struct {
desc string
input string
expString string
inputPaths []string
expRefs int
layer2 bool
contents string
desc string
input string
expString string
inputPaths []string
expRefs int
layer2 bool
contents string
skipExtractArchive bool
}{
{
desc: "path detected",
Expand All @@ -121,14 +122,25 @@ func TestNewFromFile_WithArchive(t *testing.T) {
layer2: true,
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 {
t.Run(test.desc, func(t *testing.T) {
archivePath := setupArchiveTest(t, test.input, test.layer2)

src, err := New(Config{
Path: archivePath,
})
cfg := Config{
Path: archivePath,
SkipExtractArchive: test.skipExtractArchive,
}

src, err := New(cfg)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, src.Close())
Expand Down
Loading