]> Sergey Matveev's repositories - btrtrc.git/blobdiff - torrent.go
Disable data downloading on storage write errors
[btrtrc.git] / torrent.go
index 74688c0b2614b947206bacc5d309e2eabcf46587..cd136523f0d50c72a6163d2d93626569c0250f66 100644 (file)
@@ -40,7 +40,9 @@ type Torrent struct {
        cl     *Client
        logger log.Logger
 
-       networkingEnabled bool
+       networkingEnabled      bool
+       dataDownloadDisallowed bool
+       userOnWriteChunkErr    func(error)
 
        // Determines what chunks to request from peers.
        requestStrategy requestStrategy
@@ -1810,3 +1812,42 @@ func (cb torrentRequestStrategyCallbacks) requestTimedOut(r request) {
 func (t *Torrent) requestStrategyCallbacks() requestStrategyCallbacks {
        return torrentRequestStrategyCallbacks{t}
 }
+
+func (t *Torrent) onWriteChunkErr(err error) {
+       if t.userOnWriteChunkErr != nil {
+               go t.userOnWriteChunkErr(err)
+               return
+       }
+       t.disallowDataDownloadLocked()
+}
+
+func (t *Torrent) DisallowDataDownload() {
+       t.cl.lock()
+       defer t.cl.unlock()
+       t.disallowDataDownloadLocked()
+}
+
+func (t *Torrent) disallowDataDownloadLocked() {
+       log.Printf("disallowing data download")
+       t.dataDownloadDisallowed = true
+       for c := range t.conns {
+               c.updateRequests()
+       }
+}
+
+func (t *Torrent) AllowDataDownload() {
+       t.cl.lock()
+       defer t.cl.unlock()
+       log.Printf("AllowDataDownload")
+       t.dataDownloadDisallowed = false
+       for c := range t.conns {
+               c.updateRequests()
+       }
+
+}
+
+func (t *Torrent) SetOnWriteChunkError(f func(error)) {
+       t.cl.lock()
+       defer t.cl.unlock()
+       t.userOnWriteChunkErr = f
+}