Fix a bug in lengths computation
This commit is contained in:
parent
37af0a4afb
commit
cc8011cf9a
2 changed files with 13 additions and 1 deletions
|
|
@ -78,6 +78,7 @@ impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
|
||||||
let mut have_bytes = 0u64;
|
let mut have_bytes = 0u64;
|
||||||
let mut needed_bytes = 0u64;
|
let mut needed_bytes = 0u64;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct CurrentFile<'a> {
|
struct CurrentFile<'a> {
|
||||||
index: usize,
|
index: usize,
|
||||||
fd: &'a Arc<Mutex<File>>,
|
fd: &'a Arc<Mutex<File>>,
|
||||||
|
|
@ -132,6 +133,7 @@ impl<'a, Sha1Impl: ISha1> FileOps<'a, Sha1Impl> {
|
||||||
while piece_remaining > 0 {
|
while piece_remaining > 0 {
|
||||||
let mut to_read_in_file =
|
let mut to_read_in_file =
|
||||||
std::cmp::min(current_file.remaining(), piece_remaining as u64) as usize;
|
std::cmp::min(current_file.remaining(), piece_remaining as u64) as usize;
|
||||||
|
|
||||||
while to_read_in_file == 0 {
|
while to_read_in_file == 0 {
|
||||||
current_file = file_iterator
|
current_file = file_iterator
|
||||||
.next()
|
.next()
|
||||||
|
|
|
||||||
|
|
@ -82,12 +82,16 @@ impl Lengths {
|
||||||
piece_length
|
piece_length
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if total_length == 0 {
|
||||||
|
anyhow::bail!("torrent with 0 length")
|
||||||
|
}
|
||||||
|
let total_pieces = ceil_div_u64(total_length as u64, piece_length as u64) as u32;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
chunk_length,
|
chunk_length,
|
||||||
piece_length,
|
piece_length,
|
||||||
total_length,
|
total_length,
|
||||||
chunks_per_piece: ceil_div_u64(piece_length as u64, chunk_length as u64) as u32,
|
chunks_per_piece: ceil_div_u64(piece_length as u64, chunk_length as u64) as u32,
|
||||||
last_piece_id: ((total_length + 1) / piece_length as u64) as u32,
|
last_piece_id: total_pieces - 1,
|
||||||
last_piece_length: last_element_size_u64(total_length, piece_length as u64) as u32,
|
last_piece_length: last_element_size_u64(total_length, piece_length as u64) as u32,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -248,6 +252,12 @@ mod tests {
|
||||||
assert_eq!(l.total_pieces(), 4480);
|
assert_eq!(l.total_pieces(), 4480);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_total_pieces_2() {
|
||||||
|
let l = Lengths::new(4148166656, 2097152, None).unwrap();
|
||||||
|
assert_eq!(l.total_pieces(), 1978);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_piece_length() {
|
fn test_piece_length() {
|
||||||
let l = make_lengths();
|
let l = make_lengths();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue