Skip to content

Commit 86d6af2

Browse files
committed
fix: BlockMetadata#Offset should be for section, not block data
1 parent 5b2e696 commit 86d6af2

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

v2/block_reader.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,25 @@ func (br *BlockReader) Next() (blocks.Block, error) {
143143

144144
// BlockMetadata contains metadata about a block's section in a CAR file/stream.
145145
//
146-
// There are two offsets for the block data which will be the same if the
147-
// original CAR is a CARv1, but will differ if the original CAR is a CARv2. In
148-
// the case of a CARv2, SourceOffset will be the offset from the beginning of
146+
// There are two offsets for the block section which will be the same if the
147+
// original CAR is a CARv1, but will differ if the original CAR is a CARv2.
148+
//
149+
// The block section offset is position where the CAR section begins; that is,
150+
// the begining of the length prefix (varint) prior to the CID and the block
151+
// data. Reading the varint at the offset will give the length of the rest of
152+
// the section (CID+data).
153+
//
154+
// In the case of a CARv2, SourceOffset will be the offset from the beginning of
149155
// the file/steam, and Offset will be the offset from the beginning of the CARv1
150156
// payload container within the CARv2.
151157
//
152158
// Offset is useful for index generation which requires an offset from the CARv1
153-
// payload; while SourceOffset is useful for direct block reads out of the
159+
// payload; while SourceOffset is useful for direct section reads out of the
154160
// source file/stream regardless of version.
155161
type BlockMetadata struct {
156162
cid.Cid
157-
Offset uint64 // Offset of the block data in the container CARv1
158-
SourceOffset uint64 // SourceOffset is the offset of block data in the source file/stream
163+
Offset uint64 // Offset of the section data in the container CARv1
164+
SourceOffset uint64 // SourceOffset is the offset of section data in the source file/stream
159165
Size uint64
160166
}
161167

@@ -185,7 +191,7 @@ func (br *BlockReader) SkipNext() (*BlockMetadata, error) {
185191
}
186192

187193
blockSize := sectionSize - uint64(cidSize)
188-
blockOffset := br.offset + lenSize + uint64(cidSize)
194+
blockOffset := br.offset
189195

190196
// move our reader forward; either by seeking or slurping
191197

@@ -231,7 +237,7 @@ func (br *BlockReader) SkipNext() (*BlockMetadata, error) {
231237
}
232238
}
233239

234-
br.offset = blockOffset + blockSize
240+
br.offset = br.offset + lenSize + uint64(cidSize) + blockSize
235241

236242
return &BlockMetadata{
237243
Cid: c,

v2/block_reader_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,17 @@ func TestBlockReader(t *testing.T) {
245245
vb := make([]byte, 2)
246246
for i := 0; i < 100; i++ {
247247
blk := randBlock(100 + i) // we should cross the varint two-byte boundary in here somewhere
248+
blks[i] = struct {
249+
block blocks.Block
250+
dataOffset uint64
251+
}{block: blk, dataOffset: uint64(v1buf.Len())}
248252
vn := varint.PutUvarint(vb, uint64(len(blk.Cid().Bytes())+len(blk.RawData())))
249253
n, err := v1buf.Write(vb[:vn])
250254
req.NoError(err)
251255
req.Equal(n, vn)
252256
n, err = v1buf.Write(blk.Cid().Bytes())
253257
req.NoError(err)
254258
req.Equal(len(blk.Cid().Bytes()), n)
255-
blks[i] = struct {
256-
block blocks.Block
257-
dataOffset uint64
258-
}{block: blk, dataOffset: uint64(v1buf.Len())}
259259
n, err = v1buf.Write(blk.RawData())
260260
req.NoError(err)
261261
req.Equal(len(blk.RawData()), n)

0 commit comments

Comments
 (0)