]> Sergey Matveev's repositories - btrtrc.git/commitdiff
cmd/torrent download: Fix waiting for pieces when some are already complete
authorMatt Joiner <anacrolix@gmail.com>
Mon, 9 May 2022 02:01:14 +0000 (12:01 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 9 May 2022 02:05:12 +0000 (12:05 +1000)
cmd/torrent/download.go

index 6df12216b14d7f961221da2e9090d85a71b49e05..927b22bf03acf941a61ad337ae2189e23d1073ac 100644 (file)
@@ -195,23 +195,25 @@ func addTorrents(ctx context.Context, client *torrent.Client, flags downloadFlag
 func waitForPieces(ctx context.Context, t *torrent.Torrent, beginIndex, endIndex int) {
        sub := t.SubscribePieceStateChanges()
        defer sub.Close()
-       pending := make(map[int]struct{})
-       for i := beginIndex; i < endIndex; i++ {
-               pending[i] = struct{}{}
-       }
        expected := storage.Completion{
                Complete: true,
                Ok:       true,
        }
+       pending := make(map[int]struct{})
+       for i := beginIndex; i < endIndex; i++ {
+               if t.Piece(i).State().Completion != expected {
+                       pending[i] = struct{}{}
+               }
+       }
        for {
+               if len(pending) == 0 {
+                       return
+               }
                select {
                case ev := <-sub.Values:
                        if ev.Completion == expected {
                                delete(pending, ev.Index)
                        }
-                       if len(pending) == 0 {
-                               return
-                       }
                case <-ctx.Done():
                        return
                }