Skip to content

Commit

Permalink
Correctly reset reader before handling archive chunk.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrav committed Aug 17, 2023
1 parent b8bb94f commit c1e1726
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
22 changes: 22 additions & 0 deletions pkg/handlers/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := 361
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")
Expand Down
13 changes: 9 additions & 4 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,17 @@ 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
}
return handleChunks(aCtx, h.FromFile(ctx, reReader), chunkSkel, chunksChan)
}

return false
}

Expand Down
Binary file added pkg/handlers/testdata/test.tgz
Binary file not shown.

0 comments on commit c1e1726

Please sign in to comment.