From: Matt Joiner Date: Mon, 9 May 2022 02:01:14 +0000 (+1000) Subject: cmd/torrent download: Fix waiting for pieces when some are already complete X-Git-Tag: v1.43.0~19 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=d5d940e64367b3ed6237fda3056c41edc140ad23;p=btrtrc.git cmd/torrent download: Fix waiting for pieces when some are already complete --- 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 }