1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package repo
import (
"bytes"
"context"
"fmt"
"io"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
car "github.com/ipld/go-car"
carutil "github.com/ipld/go-car/util"
)
func ReadCAR(ctx context.Context, r io.Reader) (cid.Cid, []blocks.Block, error) {
cr, err := car.NewCarReader(r)
if err != nil {
return cid.Undef, nil, fmt.Errorf("reading car header: %w", err)
}
if cr.Header.Version != 1 {
return cid.Undef, nil, fmt.Errorf("unsupported car version: %d", cr.Header.Version)
}
if len(cr.Header.Roots) == 0 {
return cid.Undef, nil, fmt.Errorf("car has no roots")
}
out := make([]blocks.Block, 0, 16)
for {
blk, err := cr.Next()
if err == io.EOF {
break
}
if err != nil {
return cid.Undef, nil, fmt.Errorf("reading car block: %w", err)
}
out = append(out, blk)
}
return cr.Header.Roots[0], out, nil
}
func ReadCARBytes(ctx context.Context, data []byte) (cid.Cid, []blocks.Block, error) {
return ReadCAR(ctx, bytes.NewReader(data))
}
func VerifyCAR(ctx context.Context, data []byte) (cid.Cid, error) {
root, _, err := ReadCARBytes(ctx, data)
return root, err
}
func WriteCAR(root cid.Cid, blks []blocks.Block) ([]byte, error) {
if !root.Defined() {
return nil, fmt.Errorf("car root is not defined")
}
buf := new(bytes.Buffer)
header := &car.CarHeader{
Roots: []cid.Cid{root},
Version: 1,
}
hb, err := cbor.DumpObject(header)
if err != nil {
return nil, fmt.Errorf("encoding car header: %w", err)
}
if err := carutil.LdWrite(buf, hb); err != nil {
return nil, fmt.Errorf("writing car header: %w", err)
}
seen := make(map[cid.Cid]bool, len(blks))
for _, blk := range blks {
c := blk.Cid()
if seen[c] {
continue
}
seen[c] = true
if err := carutil.LdWrite(buf, c.Bytes(), blk.RawData()); err != nil {
return nil, fmt.Errorf("writing car block %s: %w", c, err)
}
}
return buf.Bytes(), nil
}