Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bug that prevented providing the final metadata piece #5460

Merged
merged 1 commit into from Apr 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions libtransmission/torrent-magnet.cc
Expand Up @@ -65,6 +65,12 @@ auto create_all_needed(int n_pieces)

return ret;
}

[[nodiscard]] int div_ceil(int numerator, int denominator)
{
auto const [quot, rem] = std::div(numerator, denominator);
return quot + (rem == 0 ? 0 : 1);
}
} // namespace

bool tr_torrentSetMetadataSizeHint(tr_torrent* tor, int64_t size)
Expand All @@ -79,12 +85,8 @@ bool tr_torrentSetMetadataSizeHint(tr_torrent* tor, int64_t size)
return false;
}

int const n = (size <= 0 || size > INT_MAX) ?
-1 :
static_cast<int>(size / METADATA_PIECE_SIZE + (size % METADATA_PIECE_SIZE != 0 ? 1 : 0));

int const n = (size <= 0 || size > INT_MAX) ? -1 : div_ceil(size, METADATA_PIECE_SIZE);
tr_logAddDebugTor(tor, fmt::format("metadata is {} bytes in {} pieces", size, n));

if (n <= 0)
{
return false;
Expand Down Expand Up @@ -121,7 +123,7 @@ std::optional<std::vector<std::byte>> tr_torrentGetMetadataPiece(tr_torrent cons
return {};
}

auto const n_pieces = std::max(1, static_cast<int>(tor->info_dict_size() / METADATA_PIECE_SIZE));
auto const n_pieces = std::max(1, div_ceil(tor->info_dict_size(), METADATA_PIECE_SIZE));
if (piece < 0 || piece >= n_pieces)
{
return {};
Expand Down