Fix dagstore pieceReader burn logic

This commit is contained in:
Łukasz Magiera 2021-11-26 18:01:09 +01:00
parent 8d955d5f30
commit a9ee263682

View File

@ -119,11 +119,18 @@ func (p *pieceReader) ReadAt(b []byte, off int64) (n int, err error) {
// check if we need to burn some bytes
if off > p.rAt {
if _, err := io.CopyN(io.Discard, p.r, p.rAt-off); err != nil {
n, err := io.CopyN(io.Discard, p.r, off-p.rAt)
p.rAt += n
if err != nil {
return 0, xerrors.Errorf("discarding read gap: %w", err)
}
}
// sanity check
if off != p.rAt {
return 0, xerrors.Errorf("bad reader offset; requested %d; at %d", off, p.rAt)
}
// Read!
n, err = p.r.Read(b)
p.rAt += int64(n)