]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Make io.EOF an expected error from storage.Piece.ReadAt
authorMatt Joiner <anacrolix@gmail.com>
Thu, 27 Feb 2020 05:45:57 +0000 (16:45 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 27 Feb 2020 05:45:57 +0000 (16:45 +1100)
Fixes #381.

storage/bolt_piece.go
storage/file.go
storage/file_test.go
storage/wrappers.go
test/transfer_test.go
torrent.go

index 179fe3bdab459cddc6b9e6c80f60e78f3d624301..c54a25e9348e9b0e70beb8a624fd3f554000b49d 100644 (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
index 415ac5df49f19a48b6c2d700eb8e5da66b9a216d..013351d77f9be42066cdd1ad7728ad5cf50543bf 100644 (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
index 1a5e1b606d7df3b579f57424720927d2296abf05..daa28accc7defe6960880e3c6885519ded9188a6 100644 (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)
+       }
 }
index 4fc0e10d6208445ac2be8332964e9ee57381ca1b..da52c89f37a8fe354d530e815e29e7bebde68cbb 100644 (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()
        }
index 32b9ed156c62388e0c7c6dc9c35940002ddd7d2f..4fe9d5623db684b55f27dcca9f9ff9aa89ded974 100644 (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)
        }
index f6627b2fb8f6d285beefcd00e405d278829bd753..42f5636e8acb071c22809bb0079c5a92f391f00d 100644 (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()