Make io.EOF an expected error from storage.Piece.ReadAt

Fixes #381.
This commit is contained in:
Matt Joiner 2020-02-27 16:45:57 +11:00
parent ea71bf770c
commit 19ce53e69f
6 changed files with 14 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package storage
import (
"encoding/binary"
"io"
"github.com/anacrolix/missinggo/x"
bolt "github.com/etcd-io/bbolt"
@ -46,15 +47,16 @@ func (me *boltDBPiece) ReadAt(b []byte, off int64) (n int, err error) {
err = me.db.View(func(tx *bolt.Tx) error {
db := tx.Bucket(dataBucketKey)
if db == nil {
return nil
return io.EOF
}
ci := off / chunkSize
off %= chunkSize
for len(b) != 0 {
ck := me.chunkKey(int(ci))
_b := db.Get(ck[:])
// If the chunk is the wrong size, assume it's missing as we can't rely on the data.
if len(_b) != chunkSize {
break
return io.EOF
}
n1 := copy(b, _b[off:])
off = 0

View File

@ -169,10 +169,6 @@ func (fst fileTorrentImplIO) ReadAt(b []byte, off int64) (n int, err error) {
continue
}
err = err1
if err == io.EOF {
// Lies.
err = io.ErrUnexpectedEOF
}
return
}
off -= fi.Length

View File

@ -36,5 +36,9 @@ func TestShortFile(t *testing.T) {
p := info.Piece(0)
n, err := io.Copy(&buf, io.NewSectionReader(ts.Piece(p), 0, p.Length()))
assert.EqualValues(t, 1, n)
assert.Equal(t, io.ErrUnexpectedEOF, err)
switch err {
case nil, io.EOF:
default:
t.Errorf("expected nil or EOF error from truncated piece, got %v", err)
}
}

View File

@ -69,16 +69,10 @@ func (p Piece) ReadAt(b []byte, off int64) (n int, err error) {
if n > len(b) {
panic(n)
}
off += int64(n)
if err == io.EOF && off < p.mip.Length() {
err = io.ErrUnexpectedEOF
}
if err == nil && off >= p.mip.Length() {
err = io.EOF
}
if n == 0 && err == nil {
err = io.ErrUnexpectedEOF
panic("io.Copy will get stuck")
}
off += int64(n)
if off < p.mip.Length() && err != nil {
p.MarkNotComplete()
}

View File

@ -93,7 +93,6 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) {
cfg.DownloadRateLimiter = ps.LeecherDownloadRateLimiter
}
cfg.Seed = false
//cfg.Debug = true
if ps.ConfigureLeecher.Config != nil {
ps.ConfigureLeecher.Config(cfg)
}

View File

@ -1682,7 +1682,9 @@ func (t *Torrent) pieceHasher(index pieceIndex) {
p := t.piece(index)
sum, copyErr := t.hashPiece(index)
correct := sum == *p.hash
if !correct {
switch copyErr {
case nil, io.EOF:
default:
log.Fmsg("piece %v (%s) hash failure copy error: %v", p, p.hash.HexString(), copyErr).Log(t.logger)
}
t.storageLock.RUnlock()