Fix a bug when remarking already have pieces broken

This commit is contained in:
Igor Katson 2024-01-02 18:52:00 +00:00
parent 0ff63d8a24
commit b808382169
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 14 additions and 10 deletions

View file

@ -157,16 +157,20 @@ impl ChunkTracker {
Some(true)
}
pub fn mark_piece_broken(&mut self, index: ValidPieceIndex) -> bool {
pub fn mark_piece_broken_if_not_have(&mut self, index: ValidPieceIndex) {
if self
.have
.get(index.get() as usize)
.map(|r| *r)
.unwrap_or_default()
{
return;
}
debug!("remarking piece={} as broken", index);
self.needed_pieces.set(index.get() as usize, true);
self.chunk_status
.get_mut(self.lengths.chunk_range(index))
.map(|s| {
s.fill(false);
true
})
.unwrap_or_default()
if let Some(s) = self.chunk_status.get_mut(self.lengths.chunk_range(index)) {
s.fill(false);
}
}
pub fn mark_piece_downloaded(&mut self, idx: ValidPieceIndex) {

View file

@ -748,7 +748,7 @@ impl TorrentStateLive {
.take()
.context("bug: pausing already paused torrent")?;
for piece_id in g.inflight_pieces.keys().copied() {
chunk_tracker.mark_piece_broken(piece_id);
chunk_tracker.mark_piece_broken_if_not_have(piece_id);
}
let have_bytes = chunk_tracker.calc_have_bytes();
let needed_bytes = chunk_tracker.calc_needed_bytes();
@ -1494,7 +1494,7 @@ impl PeerHandler {
self.state
.lock_write("mark_piece_broken")
.get_chunks_mut()?
.mark_piece_broken(chunk_info.piece_index);
.mark_piece_broken_if_not_have(chunk_info.piece_index);
}
};
Ok::<_, anyhow::Error>(())