diff --git a/pkg/handlers/archive_test.go b/pkg/handlers/archive_test.go index 9b43582983e6..d0b0ed7ce69c 100644 --- a/pkg/handlers/archive_test.go +++ b/pkg/handlers/archive_test.go @@ -144,6 +144,28 @@ func TestExtractDebContent(t *testing.T) { assert.Equal(t, expectedLength, len(string(content))) } +func TestExtractTarContent(t *testing.T) { + file, err := os.Open("testdata/test.tgz") + assert.Nil(t, err) + defer file.Close() + + ctx := context.Background() + + chunkCh := make(chan *sources.Chunk) + go func() { + defer close(chunkCh) + ok := HandleFile(ctx, file, &sources.Chunk{}, chunkCh) + assert.True(t, ok) + }() + + wantCount := 4 + count := 0 + for range chunkCh { + count++ + } + assert.Equal(t, wantCount, count) +} + func TestExtractRPMContent(t *testing.T) { // Open the sample .rpm file from the testdata folder. file, err := os.Open("testdata/test.rpm") diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.go index cb18f7dd8eb9..cce0fe1bf0d0 100644 --- a/pkg/handlers/handlers.go +++ b/pkg/handlers/handlers.go @@ -26,7 +26,7 @@ type SpecializedHandler interface { } type Handler interface { - FromFile(context.Context, io.Reader) chan ([]byte) + FromFile(context.Context, io.Reader) chan []byte IsFiletype(context.Context, io.Reader) (io.Reader, bool) New() } @@ -68,12 +68,18 @@ func HandleFile(ctx context.Context, file io.Reader, chunkSkel *sources.Chunk, c aCtx.Logger().Error(err, "error resetting re-reader") return false } - reReader.Stop() - var isType bool - if file, isType = h.IsFiletype(aCtx, reReader); isType { - return handleChunks(aCtx, h.FromFile(ctx, file), chunkSkel, chunksChan) + if _, isType := h.IsFiletype(aCtx, reReader); !isType { + continue + } + + if err := reReader.Reset(); err != nil { + aCtx.Logger().Error(err, "error resetting re-reader") + return false } + reReader.Stop() + return handleChunks(aCtx, h.FromFile(ctx, reReader), chunkSkel, chunksChan) } + return false } diff --git a/pkg/handlers/testdata/test.tgz b/pkg/handlers/testdata/test.tgz new file mode 100644 index 000000000000..61717fb821ab Binary files /dev/null and b/pkg/handlers/testdata/test.tgz differ