-
Notifications
You must be signed in to change notification settings - Fork 45
/
car.go
273 lines (223 loc) · 5.8 KB
/
car.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package car
import (
"bufio"
"context"
"fmt"
"io"
"sync"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
util "github.com/ipld/go-car/util"
)
func init() {
cbor.RegisterCborType(CarHeader{})
}
type Store interface {
Put(context.Context, blocks.Block) error
}
type ReadStore interface {
Get(context.Context, cid.Cid) (blocks.Block, error)
}
type CarHeader struct {
Roots []cid.Cid
Version uint64
}
type carWriter struct {
ds format.NodeGetter
w io.Writer
walk WalkFunc
}
type WalkFunc func(format.Node) ([]*format.Link, error)
func WriteCar(ctx context.Context, ds format.NodeGetter, roots []cid.Cid, w io.Writer, options ...merkledag.WalkOption) error {
return WriteCarWithWalker(ctx, ds, roots, w, DefaultWalkFunc, options...)
}
func WriteCarWithWalker(ctx context.Context, ds format.NodeGetter, roots []cid.Cid, w io.Writer, walk WalkFunc, options ...merkledag.WalkOption) error {
h := &CarHeader{
Roots: roots,
Version: 1,
}
if err := WriteHeader(h, w); err != nil {
return fmt.Errorf("failed to write car header: %s", err)
}
cw := &carWriter{ds: ds, w: w, walk: walk}
seen := cid.NewSet()
for _, r := range roots {
if err := merkledag.Walk(ctx, cw.enumGetLinks, r, seen.Visit, options...); err != nil {
return err
}
}
return nil
}
func DefaultWalkFunc(nd format.Node) ([]*format.Link, error) {
return nd.Links(), nil
}
func ReadHeader(br *bufio.Reader) (*CarHeader, error) {
hb, err := util.LdRead(br)
if err != nil {
return nil, err
}
var ch CarHeader
if err := cbor.DecodeInto(hb, &ch); err != nil {
return nil, fmt.Errorf("invalid header: %v", err)
}
return &ch, nil
}
func WriteHeader(h *CarHeader, w io.Writer) error {
hb, err := cbor.DumpObject(h)
if err != nil {
return err
}
return util.LdWrite(w, hb)
}
func HeaderSize(h *CarHeader) (uint64, error) {
hb, err := cbor.DumpObject(h)
if err != nil {
return 0, err
}
return util.LdSize(hb), nil
}
func (cw *carWriter) enumGetLinks(ctx context.Context, c cid.Cid) ([]*format.Link, error) {
nd, err := cw.ds.Get(ctx, c)
if err != nil {
return nil, err
}
if err := cw.writeNode(ctx, nd); err != nil {
return nil, err
}
return cw.walk(nd)
}
func (cw *carWriter) writeNode(ctx context.Context, nd format.Node) error {
return util.LdWrite(cw.w, nd.Cid().Bytes(), nd.RawData())
}
var bufioReaderPool = sync.Pool{
New: func() any { return bufio.NewReader(nil) },
}
type CarReader struct {
br *bufio.Reader // set nil on EOF
Header *CarHeader
errorOnEmptyRoots bool
}
type CarReaderOption func(*CarReader) error
// WithErrorOnEmptyRoots is an option that can be passed to NewCarReader to
// specify the behavior when reading a car file that does not have any root
// cids set in the CAR header.
// Setting this option to true will cause CarReader to error on CAR that has
// no root CIDs listed in the header.
func WithErrorOnEmptyRoots(flag bool) CarReaderOption {
return func(cr *CarReader) error {
cr.errorOnEmptyRoots = flag
return nil
}
}
func NewCarReader(r io.Reader) (*CarReader, error) {
// BACKWARD COMPATIBILITY NOTE:
// WithErrorOnEmptyRoots(true) here is the legacy behavior
// which we need to keep for reasons described in
// https://github.com/ipfs/specs/pull/402#issuecomment-1599428849
// (mainly, we need to migrate Filecoin code to use explicit
// WithErrorOnEmptyRoots(true) before we can change the default here).
return NewCarReaderWithOptions(r, WithErrorOnEmptyRoots(true))
}
func NewCarReaderWithOptions(r io.Reader, opts ...CarReaderOption) (*CarReader, error) {
br := bufioReaderPool.Get().(*bufio.Reader)
br.Reset(r)
ch, err := ReadHeader(br)
if err != nil {
bufioReaderPool.Put(br)
return nil, err
}
if ch.Version != 1 {
return nil, fmt.Errorf("invalid car version: %d", ch.Version)
}
carReader := &CarReader{
br: br,
Header: ch,
}
for _, o := range opts {
err := o(carReader)
if err != nil {
return nil, err
}
}
if carReader.errorOnEmptyRoots && len(ch.Roots) == 0 {
return nil, fmt.Errorf("empty car, no roots")
}
return carReader, nil
}
func (cr *CarReader) Next() (blocks.Block, error) {
if cr.br == nil {
return nil, io.EOF
}
c, data, err := util.ReadNode(cr.br)
if err != nil {
if err == io.EOF {
// Common happy case: recycle the bufio.Reader.
// In the other error paths leaking it is fine.
bufioReaderPool.Put(cr.br)
cr.br = nil
}
return nil, err
}
hashed, err := c.Prefix().Sum(data)
if err != nil {
return nil, err
}
if !hashed.Equals(c) {
return nil, fmt.Errorf("mismatch in content integrity, name: %s, data: %s", c, hashed)
}
return blocks.NewBlockWithCid(data, c)
}
type batchStore interface {
PutMany(context.Context, []blocks.Block) error
}
func LoadCar(ctx context.Context, s Store, r io.Reader) (*CarHeader, error) {
cr, err := NewCarReader(r)
if err != nil {
return nil, err
}
if bs, ok := s.(batchStore); ok {
return loadCarFast(ctx, bs, cr)
}
return loadCarSlow(ctx, s, cr)
}
func loadCarFast(ctx context.Context, s batchStore, cr *CarReader) (*CarHeader, error) {
var buf []blocks.Block
for {
blk, err := cr.Next()
if err != nil {
if err == io.EOF {
if len(buf) > 0 {
if err := s.PutMany(ctx, buf); err != nil {
return nil, err
}
}
return cr.Header, nil
}
return nil, err
}
buf = append(buf, blk)
if len(buf) > 1000 {
if err := s.PutMany(ctx, buf); err != nil {
return nil, err
}
buf = buf[:0]
}
}
}
func loadCarSlow(ctx context.Context, s Store, cr *CarReader) (*CarHeader, error) {
for {
blk, err := cr.Next()
if err != nil {
if err == io.EOF {
return cr.Header, nil
}
return nil, err
}
if err := s.Put(ctx, blk); err != nil {
return nil, err
}
}
}