From d5d940e64367b3ed6237fda3056c41edc140ad23 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 9 May 2022 12:01:14 +1000 Subject: [PATCH] cmd/torrent download: Fix waiting for pieces when some are already complete --- cmd/torrent/download.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/cmd/torrent/download.go b/cmd/torrent/download.go index 6df12216..927b22bf 100644 --- a/cmd/torrent/download.go +++ b/cmd/torrent/download.go @@ -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 } -- 2.44.0