Detect CAR files with zero blocks and handle them accordingly

This commit is contained in:
Max Ignatenko 2024-03-17 17:42:09 +00:00
parent 638bdcf515
commit 4c41389e9b
2 changed files with 12 additions and 2 deletions

View file

@ -250,6 +250,8 @@ func parseMap(node datamodel.Node) (map[string]datamodel.Node, error) {
return m, nil
}
var ErrZeroBlocks = fmt.Errorf("zero blocks found")
func GetRev(ctx context.Context, b io.Reader) (string, error) {
r, err := car.NewCarReader(b)
if err != nil {
@ -278,6 +280,10 @@ func GetRev(ctx context.Context, b io.Reader) (string, error) {
}
}
if len(blocks) == 0 {
return "", ErrZeroBlocks
}
builder := basicnode.Prototype.Any.NewBuilder()
if err := (&dagcbor.DecodeOptions{AllowLinks: true}).Decode(builder, bytes.NewReader(blocks[r.Header.Roots[0]])); err != nil {
return "", fmt.Errorf("unmarshaling %q: %w", r.Header.Roots[0].String(), err)