diff --git a/libtransmission/announcer.cc b/libtransmission/announcer.cc index 4a22495d2..14aea1510 100644 --- a/libtransmission/announcer.cc +++ b/libtransmission/announcer.cc @@ -881,7 +881,7 @@ static tr_announce_request* announce_request_new( req->up = tier->byteCounts[TR_ANN_UP]; req->down = tier->byteCounts[TR_ANN_DOWN]; req->corrupt = tier->byteCounts[TR_ANN_CORRUPT]; - req->leftUntilComplete = tor->hasMetadata() ? tor->totalSize() - tor->hasTotal() : INT64_MAX; + req->leftUntilComplete = tor->hasMetainfo() ? tor->totalSize() - tor->hasTotal() : INT64_MAX; req->event = event; req->numwant = event == TR_ANNOUNCE_EVENT_STOPPED ? 0 : Numwant; req->key = announcer->key; diff --git a/libtransmission/completion.h b/libtransmission/completion.h index 522163be0..c6d6d9d8d 100644 --- a/libtransmission/completion.h +++ b/libtransmission/completion.h @@ -129,12 +129,12 @@ struct tr_completion [[nodiscard]] uint64_t countHasBytesInSpan(tr_byte_span_t) const; -private: - [[nodiscard]] constexpr bool hasMetainfo() const + [[nodiscard]] constexpr bool hasMetainfo() const noexcept { return !std::empty(blocks_); } +private: [[nodiscard]] uint64_t computeHasValid() const; [[nodiscard]] uint64_t computeSizeWhenDone() const; [[nodiscard]] uint64_t countHasBytesInBlocks(tr_block_span_t) const; diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index 98c8985d2..4f813748f 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -1481,7 +1481,7 @@ void tr_peerUpdateProgress(tr_torrent* tor, tr_peer* peer) { float const true_count = have->count(); - if (tor->hasMetadata()) + if (tor->hasMetainfo()) { peer->progress = true_count / float(tor->pieceCount()); } @@ -1531,7 +1531,7 @@ void tr_peerMgrTorrentAvailability(tr_torrent const* tor, int8_t* tab, unsigned std::fill_n(tab, tabCount, int8_t{}); - if (tor->hasMetadata()) + if (tor->hasMetainfo()) { int const peerCount = tr_ptrArraySize(&tor->swarm->peers); auto const** peers = (tr_peer const**)tr_ptrArrayBase(&tor->swarm->peers); @@ -1599,7 +1599,7 @@ uint64_t tr_peerMgrGetDesiredAvailable(tr_torrent const* tor) // common shortcuts... - if (!tor->isRunning || tor->isStopping || tor->isDone() || !tor->hasMetadata()) + if (!tor->isRunning || tor->isStopping || tor->isDone() || !tor->hasMetainfo()) { return 0; } diff --git a/libtransmission/peer-msgs.cc b/libtransmission/peer-msgs.cc index fbe8fd8dd..48dded12f 100644 --- a/libtransmission/peer-msgs.cc +++ b/libtransmission/peer-msgs.cc @@ -536,7 +536,7 @@ private: // TR_PEER_TO_CLIENT - if (!torrent->hasMetadata()) + if (!torrent->hasMetainfo()) { return true; } @@ -1039,7 +1039,7 @@ static void sendLtepHandshake(tr_peerMsgsImpl* msgs) // "m" dictionary) specifying an integer value of the number of // bytes of the metadata. auto const info_dict_size = msgs->torrent->infoDictSize(); - if (allow_metadata_xfer && msgs->torrent->hasMetadata() && info_dict_size > 0) + if (allow_metadata_xfer && msgs->torrent->hasMetainfo() && info_dict_size > 0) { tr_variantDictAddInt(&val, TR_KEY_metadata_size, info_dict_size); } @@ -1237,7 +1237,7 @@ static void parseUtMetadata(tr_peerMsgsImpl* msgs, uint32_t msglen, struct evbuf /* NOOP */ } - if (msg_type == MetadataMsgType::Data && !msgs->torrent->hasMetadata() && msg_end - benc_end <= METADATA_PIECE_SIZE && + if (msg_type == MetadataMsgType::Data && !msgs->torrent->hasMetainfo() && msg_end - benc_end <= METADATA_PIECE_SIZE && piece * METADATA_PIECE_SIZE + (msg_end - benc_end) <= total_size) { int const pieceLen = msg_end - benc_end; @@ -1246,7 +1246,7 @@ static void parseUtMetadata(tr_peerMsgsImpl* msgs, uint32_t msglen, struct evbuf if (msg_type == MetadataMsgType::Request) { - if (piece >= 0 && msgs->torrent->hasMetadata() && msgs->torrent->isPublic() && + if (piece >= 0 && msgs->torrent->hasMetainfo() && msgs->torrent->isPublic() && msgs->peerAskedForMetadataCount < MetadataReqQ) { msgs->peerAskedForMetadata[msgs->peerAskedForMetadataCount++] = piece; @@ -1510,7 +1510,7 @@ static bool messageLengthIsCorrect(tr_peerMsgsImpl const* msg, uint8_t id, uint3 return len == 5; case BtPeerMsgs::Bitfield: - if (msg->torrent->hasMetadata()) + if (msg->torrent->hasMetainfo()) { return len == (msg->torrent->pieceCount() >> 3) + ((msg->torrent->pieceCount() & 7) != 0 ? 1 : 0) + 1U; } @@ -1672,7 +1672,7 @@ static ReadState readBtMessage(tr_peerMsgsImpl* msgs, struct evbuffer* inbuf, si tr_peerIoReadUint32(msgs->io, inbuf, &ui32); logtrace(msgs, "got Have: %u", ui32); - if (msgs->torrent->hasMetadata() && ui32 >= msgs->torrent->pieceCount()) + if (msgs->torrent->hasMetainfo() && ui32 >= msgs->torrent->pieceCount()) { msgs->publishError(ERANGE); return READ_ERR; @@ -1690,6 +1690,13 @@ static ReadState readBtMessage(tr_peerMsgsImpl* msgs, struct evbuffer* inbuf, si case BtPeerMsgs::Bitfield: { + TR_ASSERT_MSG( + !msgs->torrent->hasMetainfo() || msglen == msgs->torrent->pieceCount() / 8, + "pieceCount:%zu msglen:%zu peer_id:%s", + size_t(msgs->torrent->pieceCount()), + size_t(msglen), + msgs->client.c_str()); + auto* const tmp = tr_new(uint8_t, msglen); logtrace(msgs, "got a bitfield"); tr_peerIoReadBytes(msgs->io, inbuf, tmp, msglen); @@ -1983,7 +1990,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 (torrent->isDone() || !torrent->hasMetadata() || msgs->client_is_choked_ || !msgs->client_is_interested_) + if (torrent->isDone() || !torrent->hasMetainfo() || msgs->client_is_choked_ || !msgs->client_is_interested_) { msgs->desired_request_count = 0; } @@ -2306,7 +2313,7 @@ static void gotError(tr_peerIo* /*io*/, short what, void* vmsgs) static void sendBitfield(tr_peerMsgsImpl* msgs) { - TR_ASSERT(msgs->torrent->hasMetadata()); + TR_ASSERT(msgs->torrent->hasMetainfo()); struct evbuffer* out = msgs->outMessages; diff --git a/libtransmission/resume.cc b/libtransmission/resume.cc index c5112ab0d..c52a5f50b 100644 --- a/libtransmission/resume.cc +++ b/libtransmission/resume.cc @@ -943,7 +943,7 @@ void save(tr_torrent* tor) tr_variantDictAddBool(&top, TR_KEY_paused, !tor->isRunning && !tor->isQueued()); savePeers(&top, tor); - if (tor->hasMetadata()) + if (tor->hasMetainfo()) { saveFilePriorities(&top, tor); saveDND(&top, tor); diff --git a/libtransmission/rpcimpl.cc b/libtransmission/rpcimpl.cc index b7cd0069b..041f282d2 100644 --- a/libtransmission/rpcimpl.cc +++ b/libtransmission/rpcimpl.cc @@ -676,7 +676,7 @@ static void initField(tr_torrent const* const tor, tr_stat const* const st, tr_v break; case TR_KEY_pieces: - if (tor->hasMetadata()) + if (tor->hasMetainfo()) { auto const bytes = tor->createPieceBitfield(); auto const enc = tr_base64_encode({ reinterpret_cast(std::data(bytes)), std::size(bytes) }); diff --git a/libtransmission/torrent-magnet.cc b/libtransmission/torrent-magnet.cc index 42beed88b..c3a69f5b3 100644 --- a/libtransmission/torrent-magnet.cc +++ b/libtransmission/torrent-magnet.cc @@ -64,7 +64,7 @@ static void incompleteMetadataFree(struct tr_incomplete_metadata* m) bool tr_torrentSetMetadataSizeHint(tr_torrent* tor, int64_t size) { - if (tor->hasMetadata()) + if (tor->hasMetainfo()) { return false; } @@ -118,7 +118,7 @@ void* tr_torrentGetMetadataPiece(tr_torrent const* tor, int piece, size_t* len) TR_ASSERT(piece >= 0); TR_ASSERT(len != nullptr); - if (!tor->hasMetadata()) + if (!tor->hasMetainfo()) { return nullptr; } @@ -400,7 +400,7 @@ bool tr_torrentGetNextMetadataRequest(tr_torrent* tor, time_t now, int* setme_pi double tr_torrentGetMetadataPercent(tr_torrent const* tor) { - if (tor->hasMetadata()) + if (tor->hasMetainfo()) { return 1.0; } diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index c7197b643..32bb2a8f2 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -637,7 +637,7 @@ static bool setLocalErrorIfFilesDisappeared(tr_torrent* tor, std::optional */ static bool isNewTorrentASeed(tr_torrent* tor) { - if (!tor->hasMetadata()) + if (!tor->hasMetainfo()) { return false; } @@ -778,7 +778,7 @@ static void torrentInit(tr_torrent* tor, tr_ctor const* ctor) [](auto mtime) { return mtime > 0; }); } - auto const filename = tor->hasMetadata() ? tor->torrentFile() : tor->magnetFile(); + auto const filename = tor->hasMetainfo() ? tor->torrentFile() : tor->magnetFile(); // if we don't have a local .torrent or .magnet file already, // assume the torrent is new @@ -788,7 +788,7 @@ static void torrentInit(tr_torrent* tor, tr_ctor const* ctor) { tr_error* error = nullptr; - if (tor->hasMetadata()) // torrent file + if (tor->hasMetainfo()) // torrent file { tr_ctorSaveContents(ctor, filename, &error); } @@ -815,12 +815,12 @@ static void torrentInit(tr_torrent* tor, tr_ctor const* ctor) if (is_new_torrent) { - if (tor->hasMetadata()) + if (tor->hasMetainfo()) { callScriptIfEnabled(tor, TR_SCRIPT_ON_TORRENT_ADDED); } - if (!tor->hasMetadata() && !doStart) + if (!tor->hasMetainfo() && !doStart) { tor->prefetchMagnetMetadata = true; @@ -2088,7 +2088,7 @@ bool tr_torrent::setTrackerList(std::string_view text) return false; } - auto const has_metadata = this->hasMetadata(); + auto const has_metadata = this->hasMetainfo(); if (has_metadata && !announce_list.save(torrentFile())) { return false; @@ -2257,7 +2257,7 @@ static void deleteLocalData(tr_torrent const* tor, tr_fileFunc func) } /* if it's a magnet link, there's nothing to move... */ - if (!tor->hasMetadata()) + if (!tor->hasMetainfo()) { return; } @@ -2761,7 +2761,7 @@ static void refreshCurrentDir(tr_torrent* tor) { dir = tor->downloadDir(); } - else if (!tor->hasMetadata()) /* no files to find */ + else if (!tor->hasMetainfo()) /* no files to find */ { dir = tor->incompleteDir(); } @@ -3161,7 +3161,7 @@ void tr_torrentSetFilePriorities( bool tr_torrentHasMetadata(tr_torrent const* tor) { - return tor->hasMetadata(); + return tor->hasMetainfo(); } void tr_torrent::markEdited() diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 25ea6692c..cae614a76 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -199,6 +199,11 @@ public: return completion.sizeWhenDone(); } + [[nodiscard]] auto hasMetainfo() const noexcept + { + return completion.hasMetainfo() > 0; + } + [[nodiscard]] auto hasAll() const { return completion.hasAll(); @@ -500,11 +505,6 @@ public: return metainfo_.source(); } - [[nodiscard]] auto hasMetadata() const - { - return fileCount() > 0; - } - [[nodiscard]] auto infoDictSize() const { return metainfo_.infoDictSize();