fixup! refactor: `tr_torrent.incompleteMetadata` from raw pointer to `std::optional` (#5837)

This commit is contained in:
tearfur 2023-07-29 21:49:52 +08:00 committed by GitHub
parent dae1bb123c
commit 8b4d05d4e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 3 deletions

View File

@ -220,6 +220,7 @@ void build_metainfo_except_info_dict(tr_torrent_metainfo const& tm, tr_variant*
bool use_new_metainfo(tr_torrent* tor, tr_error** error) bool use_new_metainfo(tr_torrent* tor, tr_error** error)
{ {
auto const& m = tor->incomplete_metadata; auto const& m = tor->incomplete_metadata;
TR_ASSERT(m);
// test the info_dict checksum // test the info_dict checksum
if (tr_sha1::digest(m->metadata) != tor->info_hash()) if (tr_sha1::digest(m->metadata) != tor->info_hash())
@ -268,6 +269,7 @@ void on_have_all_metainfo(tr_torrent* tor)
{ {
tr_error* error = nullptr; tr_error* error = nullptr;
auto& m = tor->incomplete_metadata; auto& m = tor->incomplete_metadata;
TR_ASSERT(m);
if (use_new_metainfo(tor, &error)) if (use_new_metainfo(tor, &error))
{ {
m.reset(); m.reset();
@ -386,9 +388,15 @@ double tr_torrentGetMetadataPercent(tr_torrent const* tor)
return 1.0; return 1.0;
} }
auto const& m = tor->incomplete_metadata; if (auto const& m = tor->incomplete_metadata; m)
auto const& n = m->piece_count; {
return !m || n == 0 ? 0.0 : (n - std::size(m->pieces_needed)) / static_cast<double>(n); if (auto const& n = m->piece_count; n != 0)
{
return (n - std::size(m->pieces_needed)) / static_cast<double>(n);
}
}
return 0.0;
} }
std::string tr_torrentGetMagnetLink(tr_torrent const* tor) std::string tr_torrentGetMagnetLink(tr_torrent const* tor)