refactor: remove last byte special case in tr_block_info::byte_loc() (#7064)

* refactor: remove last byte special case in `tr_block_info::byte_loc()`

* fix: handle 0-byte file at the end of torrent in fpm

* test: modify test for 0-byte file at the end of torrent

* fix: handle 0-byte file at the end of torrent in `block_span_for_file`
This commit is contained in:
Yat Ho
2024-08-23 08:03:14 +08:00
committed by GitHub
parent 10333d23b2
commit 3e5a77d176
4 changed files with 38 additions and 40 deletions

View File

@@ -83,16 +83,8 @@ public:
{
loc.byte = byte_idx;
if (byte_idx == total_size()) // handle 0-byte files at the end of a torrent
{
loc.block = block_count() - 1U;
loc.piece = piece_count() - 1U;
}
else
{
loc.block = static_cast<tr_block_index_t>(byte_idx / BlockSize);
loc.piece = static_cast<tr_piece_index_t>(byte_idx / piece_size());
}
loc.block = static_cast<tr_block_index_t>(byte_idx / BlockSize);
loc.piece = static_cast<tr_piece_index_t>(byte_idx / piece_size());
loc.block_offset = static_cast<uint32_t>(loc.byte - (uint64_t{ loc.block } * BlockSize));
loc.piece_offset = static_cast<uint32_t>(loc.byte - (uint64_t{ loc.piece } * piece_size()));