Skip to content

Commit

Permalink
[bug] - Correctly reset reader before handling archive chunk data (#1636
Browse files Browse the repository at this point in the history
)

* Correctly reset reader before handling archive chunk.

* stop the re-reader.
  • Loading branch information
ahrav authored Aug 17, 2023
1 parent b8bb94f commit f3c2d5e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 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
16 changes: 11 additions & 5 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}

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

0 comments on commit f3c2d5e

Please sign in to comment.