Ability to change the list of files at any time, including through UI (#115)

* Now can update the list of files without pausing/unpausing

* Shrink a few functions

* Reopen write when updating files

* Todos

* opened_file abstraction

* iter_pieces_within iterator

* Simplify iter_pieces_within

* Simplify iter_pieces_within

* Add "iter_file_details"

* temporarily broken: readonly by default

* Live torrent - reopen files

* Reopen files after changing the list

* Now reopening files read only when they are completed

* Fix a bug in opened_file.rs

* update todos

* update help

* Reconnect all peers that are idling

* Add a couple fields to OpenedFile

* Add a couple fields to OpenedFile

* Small cleanups - use the new iterator where possible

* size_of_piece_in_file function

* Updating have

* Include file progress

* Almost nothing

* ugly progress bars

* bad UI, saving

* its not so bad

* Works now

* update progress bar a bit

* Reopen read-only on pause

* Zero bytes isnt too bad! Doesnt break anything

* fix per file progress bars

* progress bar not as ugly anymore?

* ui tweaks

* fix a react bug

* TODO.md update

* Fix js + TODOs

* Compute per-file progress on init

* Fix stats updating live

* Nothing

* Nothing

* cleanup ui a bit

* Nothing

* Final fixes

* Trying to fix rust 1.73

* Sorting filenames

* remove unnecessary indentation

* Remove unnecessary comment
This commit is contained in:
Igor Katson 2024-04-06 09:20:03 +01:00 committed by GitHub
parent d7380217f6
commit 5eb01ac226
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 865 additions and 512 deletions

View file

@ -153,6 +153,25 @@ impl Lengths {
})
}
// A helper to iterate over pieces in a file.
pub(crate) fn iter_pieces_within_offset(
&self,
offset_bytes: u64,
len: u64,
) -> std::ops::Range<u32> {
// Validation and correction
let offset_bytes = offset_bytes.min(self.total_length);
let end_bytes = (offset_bytes + len).min(self.total_length);
let start_piece_id = (offset_bytes / self.piece_length as u64) as u32;
let end_piece_id = if end_bytes == offset_bytes {
start_piece_id
} else {
end_bytes.div_ceil(self.piece_length as u64) as u32
};
start_piece_id..end_piece_id
}
pub fn iter_chunk_infos(&self, index: ValidPieceIndex) -> impl Iterator<Item = ChunkInfo> {
let mut remaining = self.piece_length(index);
let absolute_offset = index.0 * self.chunks_per_piece;
@ -230,6 +249,19 @@ impl Lengths {
}
return None;
}
// How many bytes out of the given piece are present in the given file (by offset and len).
pub fn size_of_piece_in_file(&self, piece_id: u32, file_offset: u64, file_len: u64) -> u64 {
let piece_offset = piece_id as u64 * self.default_piece_length() as u64;
let piece_end = piece_offset + self.default_piece_length() as u64;
let file_end = file_offset + file_len;
let offset = file_offset.max(piece_offset);
let end = file_end.min(piece_end);
end.saturating_sub(offset)
}
}
#[cfg(test)]
@ -535,4 +567,71 @@ mod tests {
assert_eq!(l.chunks_per_piece(l.last_piece_id()), 1);
}
#[test]
fn test_iter_pieces_within() {
// Macro to preserve line numbers
macro_rules! check {
($l:expr, $offset:expr, $len:expr, $expected:expr) => {
let e: &[u32] = $expected;
println!("case: offset={}, len={}, expected={:?}", $offset, $len, e);
assert_eq!(
&$l.iter_pieces_within_offset($offset, $len)
.collect::<Vec<_>>()[..],
$expected
);
};
}
let l = Lengths::new(21, 10).unwrap();
check!(&l, 0, 5, &[0]);
check!(&l, 0, 10, &[0]);
check!(&l, 0, 11, &[0, 1]);
check!(&l, 0, 0, &[]);
check!(&l, 10, 0, &[]);
check!(&l, 10, 1, &[1]);
check!(&l, 10, 10, &[1]);
check!(&l, 10, 11, &[1, 2]);
check!(&l, 5, 5, &[0]);
check!(&l, 5, 6, &[0, 1]);
check!(&l, 5, 15, &[0, 1]);
check!(&l, 5, 16, &[0, 1, 2]);
check!(&l, 20, 1, &[2]);
check!(&l, 20, 2, &[2]);
check!(&l, 20, 1000, &[2]);
check!(&l, 21, 0, &[]);
check!(&l, 21, 1, &[]);
check!(&l, 22, 0, &[]);
check!(&l, 22, 1, &[]);
}
#[test]
fn test_size_of_piece_in_file() {
let l = Lengths::new(10, 5).unwrap();
assert_eq!(l.size_of_piece_in_file(0, 0, 10), 5);
assert_eq!(l.size_of_piece_in_file(0, 1, 10), 4);
assert_eq!(l.size_of_piece_in_file(0, 5, 10), 0);
assert_eq!(l.size_of_piece_in_file(0, 6, 10), 0);
assert_eq!(l.size_of_piece_in_file(0, 0, 0), 0);
assert_eq!(l.size_of_piece_in_file(0, 1, 0), 0);
assert_eq!(l.size_of_piece_in_file(0, 5, 0), 0);
assert_eq!(l.size_of_piece_in_file(0, 6, 0), 0);
assert_eq!(l.size_of_piece_in_file(1, 0, 10), 5);
assert_eq!(l.size_of_piece_in_file(1, 4, 10), 5);
assert_eq!(l.size_of_piece_in_file(1, 5, 10), 5);
assert_eq!(l.size_of_piece_in_file(1, 6, 10), 4);
assert_eq!(l.size_of_piece_in_file(1, 9, 10), 1);
assert_eq!(l.size_of_piece_in_file(1, 10, 10), 0);
// garbage data
assert_eq!(l.size_of_piece_in_file(2, 0, 10), 0);
assert_eq!(l.size_of_piece_in_file(3, 0, 10), 0);
assert_eq!(l.size_of_piece_in_file(0, 10, 0), 0);
assert_eq!(l.size_of_piece_in_file(0, 10, 5), 0);
}
}