refactor: tr_torrentIsSeed() -> tr_torrent::isDone() (#2313)

This commit is contained in:
Charles Kerr 2021-12-15 01:04:26 -06:00 committed by GitHub
parent 68518bc307
commit d00be0dec7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 38 additions and 95 deletions

View File

@ -1500,11 +1500,11 @@ static int compareAnnounceTiers(tr_tier const* a, tr_tier const* b)
}
/* prefer swarms where we might download */
bool const is_seed_a = tr_torrentIsSeed(a->tor);
bool const is_seed_b = tr_torrentIsSeed(b->tor);
if (is_seed_a != is_seed_b)
bool const is_done_a = a->tor->isDone();
bool const is_done_b = b->tor->isDone();
if (is_done_a != is_done_b)
{
return is_seed_a ? 1 : -1;
return is_done_a ? 1 : -1;
}
/* prefer larger stats, to help ensure stats get recorded when stopping on shutdown */

View File

@ -79,11 +79,6 @@ struct tr_completion
[[nodiscard]] uint64_t hasValid() const;
[[nodiscard]] bool isDone() const
{
return hasMetainfo() && leftUntilDone() == 0;
}
[[nodiscard]] uint64_t leftUntilDone() const;
[[nodiscard]] constexpr double percentComplete() const

View File

@ -803,7 +803,7 @@ static ReadState readCryptoProvide(tr_handshake* handshake, struct evbuffer* inb
if (tr_torrent const* const tor = tr_torrentFindFromObfuscatedHash(handshake->session, obfuscatedTorrentHash);
tor != nullptr)
{
bool const clientIsSeed = tr_torrentIsSeed(tor);
bool const clientIsSeed = tor->isDone();
bool const peerIsSeed = tr_peerMgrPeerIsSeed(tor, tr_peerIoGetAddress(handshake->io, nullptr));
dbgmsg(handshake, "got INCOMING connection's encrypted handshake for torrent [%s]", tr_torrentName(tor));
tr_peerIoSetTorrentHash(handshake->io, tor->info.hash);

View File

@ -522,7 +522,7 @@ static int countActiveWebseeds(tr_swarm* s)
{
int activeCount = 0;
if (s->tor->isRunning && !tr_torrentIsSeed(s->tor))
if (s->tor->isRunning && !s->tor->isDone())
{
uint64_t const now = tr_time_msec();
@ -1317,7 +1317,7 @@ static int compareAtomsByUsefulness(void const* va, void const* vb)
static bool isAtomInteresting(tr_torrent const* tor, struct peer_atom* atom)
{
if (tr_torrentIsSeed(tor) && atomIsSeed(atom))
if (tor->isDone() && atomIsSeed(atom))
{
return false;
}
@ -1648,7 +1648,7 @@ uint64_t tr_peerMgrGetDesiredAvailable(tr_torrent const* tor)
// common shortcuts...
if (!tor->isRunning || tor->isStopping || tr_torrentIsSeed(tor) || !tr_torrentHasMetadata(tor))
if (!tor->isRunning || tor->isStopping || tor->isDone() || !tr_torrentHasMetadata(tor))
{
return 0;
}
@ -1852,7 +1852,7 @@ void tr_peerMgrClearInterest(tr_torrent* tor)
static bool isPeerInteresting(tr_torrent* const tor, bool const* const piece_is_interesting, tr_peer const* const peer)
{
/* these cases should have already been handled by the calling code... */
TR_ASSERT(!tr_torrentIsSeed(tor));
TR_ASSERT(!tor->isDone());
TR_ASSERT(tr_torrentIsPieceTransferAllowed(tor, TR_PEER_TO_CLIENT));
if (tr_peerIsSeed(peer))
@ -1909,7 +1909,7 @@ static void rechokeDownloads(tr_swarm* s)
time_t const now = tr_time();
/* some cases where this function isn't necessary */
if (tr_torrentIsSeed(s->tor))
if (s->tor->isDone())
{
return;
}
@ -2120,7 +2120,7 @@ static int getRate(tr_torrent const* tor, struct peer_atom* atom, uint64_t now)
{
auto Bps = unsigned{};
if (tr_torrentIsSeed(tor))
if (tor->isDone())
{
Bps = tr_peerGetPieceSpeed_Bps(atom->peer, now, TR_CLIENT_TO_PEER);
}
@ -2317,7 +2317,7 @@ static bool shouldPeerBeClosed(tr_swarm const* s, tr_peer const* peer, int peerC
}
/* disconnect if we're both seeds and enough time has passed for PEX */
if (tr_torrentIsSeed(tor) && tr_peerIsSeed(peer))
if (tor->isDone() && tr_peerIsSeed(peer))
{
return !tor->allowsPex() || now - atom->time >= 30;
}
@ -2830,7 +2830,7 @@ static void atomPulse(evutil_socket_t /*fd*/, short /*what*/, void* vmgr)
static bool isPeerCandidate(tr_torrent const* tor, struct peer_atom* atom, time_t const now)
{
/* not if we're both seeds */
if (tr_torrentIsSeed(tor) && atomIsSeed(atom))
if (tor->isDone() && atomIsSeed(atom))
{
return false;
}
@ -2919,7 +2919,7 @@ static uint64_t getPeerCandidateScore(tr_torrent const* tor, struct peer_atom co
score = addValToKey(score, 1, i);
/* prefer torrents we're downloading with */
i = tr_torrentIsSeed(tor) ? 1 : 0;
i = tor->isDone() ? 1 : 0;
score = addValToKey(score, 1, i);
/* prefer peers that are known to be connectible */
@ -3003,7 +3003,7 @@ static std::vector<peer_candidate> getPeerCandidates(tr_session* session, size_t
/* if everyone in the swarm is seeds and pex is disabled because
* the torrent is private, then don't initiate connections */
bool const seeding = tr_torrentIsSeed(tor);
bool const seeding = tor->isDone();
if (seeding && swarmIsAllSeeds(tor->swarm) && tor->isPrivate())
{
continue;
@ -3101,7 +3101,7 @@ static void initiateCandidateConnection(tr_peerMgr* mgr, peer_candidate& c)
fprintf(stderr, "Starting an OUTGOING connection with %s - [%s] %s, %s\n", tr_atomAddrStr(c->atom),
tr_torrentName(c->tor), c->tor->isPrivate() ? "private" : "public",
tr_torrentIsSeed(c->tor) ? "seed" : "downloader");
c->tor->isDone() ? "seed" : "downloader");
#endif

View File

@ -541,7 +541,7 @@ private:
}
auto const active = is_client_interested() && !is_client_choked();
TR_ASSERT(!active || !tr_torrentIsSeed(torrent));
TR_ASSERT(!active || !torrent->isDone());
return active;
}
@ -1087,7 +1087,7 @@ static void sendLtepHandshake(tr_peerMsgsImpl* msgs)
// the extension handshake 'upload_only'. Setting the value of this
// key to 1 indicates that this peer is not interested in downloading
// anything.
tr_variantDictAddBool(&val, TR_KEY_upload_only, tr_torrentIsSeed(msgs->torrent));
tr_variantDictAddBool(&val, TR_KEY_upload_only, msgs->torrent->isDone());
if (allow_metadata_xfer || allow_pex)
{
@ -2004,7 +2004,7 @@ static void updateDesiredRequestCount(tr_peerMsgsImpl* msgs)
tr_torrent const* const torrent = msgs->torrent;
/* there are lots of reasons we might not want to request any blocks... */
if (tr_torrentIsSeed(torrent) || !tr_torrentHasMetadata(torrent) || msgs->client_is_choked_ || !msgs->client_is_interested_)
if (torrent->isDone() || !tr_torrentHasMetadata(torrent) || msgs->client_is_choked_ || !msgs->client_is_interested_)
{
msgs->desired_request_count = 0;
}

View File

@ -668,7 +668,7 @@ static void onNowTimer(evutil_socket_t /*fd*/, short /*what*/, void* vsession)
{
if (tor->isRunning)
{
if (tr_torrentIsSeed(tor))
if (tor->isDone())
{
++tor->secondsSeeding;
}
@ -2752,7 +2752,7 @@ std::vector<tr_torrent*> tr_sessionGetNextQueuedTorrents(tr_session* session, tr
candidates.reserve(tr_sessionCountTorrents(session));
for (auto* tor : session->torrents)
{
if (tr_torrentIsQueued(tor) && (direction == tr_torrentGetQueueDirection(tor)))
if (tr_torrentIsQueued(tor) && (direction == tor->queueDirection()))
{
candidates.push_back(tor);
}

View File

@ -371,7 +371,7 @@ static bool tr_torrentGetSeedRatioBytes(tr_torrent const* tor, uint64_t* setmeLe
*setmeGoal = goal;
}
seedRatioApplies = tr_torrentIsSeed(tor);
seedRatioApplies = tor->isDone();
}
return seedRatioApplies;
@ -475,7 +475,7 @@ void tr_torrentCheckSeedLimit(tr_torrent* tor)
{
TR_ASSERT(tr_isTorrent(tor));
if (!tor->isRunning || tor->isStopping || !tr_torrentIsSeed(tor))
if (!tor->isRunning || tor->isStopping || !tor->isDone())
{
return;
}
@ -954,7 +954,7 @@ tr_torrent_activity tr_torrentGetActivity(tr_torrent const* tor)
{
tr_torrent_activity ret = TR_STATUS_STOPPED;
bool const is_seed = tr_torrentIsSeed(tor);
bool const is_seed = tor->isDone();
if (tor->verifyState == TR_VERIFY_NOW)
{
@ -1435,7 +1435,7 @@ uint64_t tr_torrentGetCurrentSizeOnDisk(tr_torrent const* tor)
static bool torrentShouldQueue(tr_torrent const* tor)
{
tr_direction const dir = tr_torrentGetQueueDirection(tor);
tr_direction const dir = tor->queueDirection();
return tr_sessionCountQueueFreeSlots(tor->session, dir) == 0;
}
@ -1912,7 +1912,7 @@ void tr_torrent::recheckCompleteness()
if (new_completeness != completeness)
{
bool const recentChange = downloadedCur != 0;
bool const wasLeeching = !tr_torrentIsSeed(this);
bool const wasLeeching = !this->isDone();
bool const wasRunning = isRunning;
if (recentChange)
@ -1927,7 +1927,7 @@ void tr_torrent::recheckCompleteness()
this->completeness = new_completeness;
tr_fdTorrentClose(this->session, this->uniqueId);
if (tr_torrentIsSeed(this))
if (this->isDone())
{
if (recentChange)
{
@ -1949,7 +1949,7 @@ void tr_torrent::recheckCompleteness()
fireCompletenessChange(this, completeness, wasRunning);
if (tr_torrentIsSeed(this) && wasLeeching && wasRunning)
if (this->isDone() && wasLeeching && wasRunning)
{
/* if completeness was TR_LEECH, the seed limit check
will have been skipped in bandwidthPulse */
@ -1958,7 +1958,7 @@ void tr_torrent::recheckCompleteness()
this->setDirty();
if (tr_torrentIsSeed(this))
if (this->isDone())
{
tr_torrentSave(this);
callScriptIfEnabled(this, TR_SCRIPT_ON_TORRENT_DONE);
@ -3078,7 +3078,7 @@ static int renamePath(tr_torrent* tor, char const* oldpath, char const* newname)
{
int err = 0;
char const* const base = !tr_torrentIsSeed(tor) && tor->incompleteDir != nullptr ? tor->incompleteDir : tor->downloadDir;
char const* const base = !tor->isDone() && tor->incompleteDir != nullptr ? tor->incompleteDir : tor->downloadDir;
auto src = tr_strvPath(base, oldpath);

View File

@ -215,9 +215,9 @@ public:
return completion.createPieceBitfield();
}
[[nodiscard]] bool isDone() const
[[nodiscard]] constexpr bool isDone() const
{
return completion.isDone();
return completeness != TR_LEECH;
}
[[nodiscard]] tr_bitfield const& blocks() const
@ -421,6 +421,11 @@ public:
///
constexpr auto queueDirection() const
{
return this->isDone() ? TR_UP : TR_DOWN;
}
auto allowsPex() const
{
return this->isPublic() && this->session->isPexEnabled;
@ -619,12 +624,6 @@ constexpr tr_completeness tr_torrentGetCompleteness(tr_torrent const* tor)
return tor->completeness;
}
// TODO: rename this to tr_torrentIsDone()? both seed and partial seed return true
constexpr bool tr_torrentIsSeed(tr_torrent const* tor)
{
return tr_torrentGetCompleteness(tor) != TR_LEECH;
}
/***
****
***/
@ -699,9 +698,4 @@ constexpr bool tr_torrentIsQueued(tr_torrent const* tor)
return tor->isQueued;
}
constexpr tr_direction tr_torrentGetQueueDirection(tr_torrent const* tor)
{
return tr_torrentIsSeed(tor) ? TR_UP : TR_DOWN;
}
tr_info const* tr_torrentInfo(tr_torrent const* torrent);

View File

@ -373,7 +373,7 @@ static void on_idle(tr_webseed* w)
w->retry_challenge = running_tasks + w->idle_connections + 1;
}
if (tor != nullptr && tor->isRunning && !tr_torrentIsSeed(tor) && want > 0)
if (tor != nullptr && tor->isRunning && !tor->isDone() && want > 0)
{
auto n_tasks = size_t{};

View File

@ -48,7 +48,6 @@ TEST_F(CompletionTest, MagnetLink)
EXPECT_FALSE(completion.hasBlocks({ 0, 1 }));
EXPECT_FALSE(completion.hasBlocks({ 0, 1000 }));
EXPECT_FALSE(completion.hasPiece(0));
EXPECT_FALSE(completion.isDone());
EXPECT_DOUBLE_EQ(0.0, completion.percentDone());
EXPECT_DOUBLE_EQ(0.0, completion.percentComplete());
EXPECT_EQ(TR_LEECH, completion.status());
@ -167,51 +166,6 @@ TEST_F(CompletionTest, hasPiece)
EXPECT_EQ(PieceSize, completion.hasValid());
}
TEST_F(CompletionTest, isDone)
{
auto torrent = TestTorrent{};
auto constexpr TotalSize = uint64_t{ BlockSize * 4096 };
auto constexpr PieceSize = uint64_t{ BlockSize * 64 };
auto const block_info = tr_block_info{ TotalSize, PieceSize };
// check that in blank-slate initial state, isDone() is false
auto completion = tr_completion(&torrent, &block_info);
EXPECT_FALSE(completion.isDone());
EXPECT_EQ(TR_LEECH, completion.status());
EXPECT_EQ(block_info.total_size, completion.leftUntilDone());
// check that we're done if we have all the blocks
auto left = block_info.total_size;
for (size_t i = 1; i < block_info.n_blocks; ++i)
{
completion.addBlock(i);
left -= block_info.block_size;
EXPECT_EQ(left, completion.leftUntilDone());
}
EXPECT_FALSE(completion.isDone());
completion.addBlock(0);
EXPECT_EQ(0, completion.leftUntilDone());
EXPECT_TRUE(completion.isDone());
EXPECT_EQ(TR_SEED, completion.status());
// check that not having all the pieces (and we want all) means we're not done
completion.removePiece(0);
EXPECT_FALSE(completion.isDone());
EXPECT_EQ(TR_LEECH, completion.status());
// check that having all the pieces we want, even if it's not ALL pieces, means we're done
torrent.dnd_pieces.insert(0);
completion.invalidateSizeWhenDone();
EXPECT_TRUE(completion.isDone());
EXPECT_EQ(TR_PARTIAL_SEED, completion.status());
// but if we decide we do want that missing piece after all, then we're not done
torrent.dnd_pieces.erase(0);
completion.invalidateSizeWhenDone();
EXPECT_FALSE(completion.isDone());
EXPECT_EQ(TR_LEECH, completion.status());
}
TEST_F(CompletionTest, percentCompleteAndDone)
{
auto torrent = TestTorrent{};