]> Sergey Matveev's repositories - btrtrc.git/blobdiff - storage/wrappers.go
Update to multiple-blobs-per-value squirrel
[btrtrc.git] / storage / wrappers.go
index da52c89f37a8fe354d530e815e29e7bebde68cbb..a3907e1d2f4ed6a193c1a6cdedd85b9dbabf6e58 100644 (file)
@@ -4,7 +4,7 @@ import (
        "io"
        "os"
 
-       "github.com/anacrolix/missinggo"
+       "github.com/anacrolix/missinggo/v2"
 
        "github.com/anacrolix/torrent/metainfo"
 )
@@ -19,7 +19,10 @@ func NewClient(cl ClientImpl) *Client {
 
 func (cl Client) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (*Torrent, error) {
        t, err := cl.ci.OpenTorrent(info, infoHash)
-       return &Torrent{t}, err
+       if err != nil {
+               return nil, err
+       }
+       return &Torrent{t}, nil
 }
 
 type Torrent struct {
@@ -35,6 +38,19 @@ type Piece struct {
        mip metainfo.Piece
 }
 
+var _ io.WriterTo = Piece{}
+
+// Why do we have this wrapper? Well PieceImpl doesn't implement io.Reader, so we can't let io.Copy
+// and friends check for io.WriterTo and fallback for us since they expect an io.Reader.
+func (p Piece) WriteTo(w io.Writer) (int64, error) {
+       if i, ok := p.PieceImpl.(io.WriterTo); ok {
+               return i.WriteTo(w)
+       }
+       n := p.mip.Length()
+       r := io.NewSectionReader(p, 0, n)
+       return io.CopyN(w, r, n)
+}
+
 func (p Piece) WriteAt(b []byte, off int64) (n int, err error) {
        // Callers should not be writing to completed pieces, but it's too
        // expensive to be checking this on every single write using uncached
@@ -73,8 +89,15 @@ func (p Piece) ReadAt(b []byte, off int64) (n int, err error) {
                panic("io.Copy will get stuck")
        }
        off += int64(n)
-       if off < p.mip.Length() && err != nil {
-               p.MarkNotComplete()
+
+       // Doing this here may be inaccurate. There's legitimate reasons we may fail to read while the
+       // data is still there, such as too many open files. There should probably be a specific error
+       // to return if the data has been lost.
+       if off < p.mip.Length() {
+               if err == io.EOF {
+                       p.MarkNotComplete()
+               }
        }
+
        return
 }