Add helpers to get InfoHash from libtorrent classes

This commit is contained in:
Vladimir Golovnev (Glassez) 2025-05-02 17:11:17 +03:00
parent e7dee969e1
commit 76bb4e5f98
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
2 changed files with 74 additions and 79 deletions

View file

@ -30,6 +30,7 @@
#include "sessionimpl.h" #include "sessionimpl.h"
#include <algorithm> #include <algorithm>
#include <concepts>
#include <cstdint> #include <cstdint>
#include <ctime> #include <ctime>
#include <queue> #include <queue>
@ -315,6 +316,44 @@ namespace
break; break;
} }
} }
#ifdef QBT_USES_LIBTORRENT2
template <typename T>
concept HasInfoHashMember = requires (T t) { { t.info_hashes } -> std::convertible_to<InfoHash>; };
template <typename T>
concept HasInfoHashMemberFn = requires (T t) { { t.info_hashes() } -> std::convertible_to<InfoHash>; };
template <HasInfoHashMember T>
InfoHash getInfoHash(const T &t) { return t.info_hashes; }
template <HasInfoHashMemberFn T>
InfoHash getInfoHash(const T &t) { return t.info_hashes(); }
InfoHash getInfoHash(const lt::add_torrent_params &addTorrentParams)
{
const bool hasMetadata = (addTorrentParams.ti && addTorrentParams.ti->is_valid());
return hasMetadata ? getInfoHash(*addTorrentParams.ti) : InfoHash(addTorrentParams.info_hashes);
}
#else
template <typename T>
concept HasInfoHashMember = requires (T t) { { t.info_hash } -> std::convertible_to<InfoHash>; };
template <typename T>
concept HasInfoHashMemberFn = requires (T t) { { t.info_hash() } -> std::convertible_to<InfoHash>; };
template <HasInfoHashMember T>
InfoHash getInfoHash(const T &t) { return t.info_hash; }
template <HasInfoHashMemberFn T>
InfoHash getInfoHash(const T &t) { return t.info_hash(); }
InfoHash getInfoHash(const lt::add_torrent_params &addTorrentParams)
{
const bool hasMetadata = (addTorrentParams.ti && addTorrentParams.ti->is_valid());
return hasMetadata ? getInfoHash(*addTorrentParams.ti) : InfoHash(addTorrentParams.info_hash);
}
#endif
} }
struct BitTorrent::SessionImpl::ResumeSessionContext final : public QObject struct BitTorrent::SessionImpl::ResumeSessionContext final : public QObject
@ -1443,10 +1482,8 @@ void SessionImpl::processNextResumeData(ResumeSessionContext *context)
LoadTorrentParams resumeData = *loadResumeDataResult; LoadTorrentParams resumeData = *loadResumeDataResult;
bool needStore = false; bool needStore = false;
const InfoHash infoHash = getInfoHash(resumeData.ltAddTorrentParams);
#ifdef QBT_USES_LIBTORRENT2 #ifdef QBT_USES_LIBTORRENT2
const InfoHash infoHash {(resumeData.ltAddTorrentParams.ti
? resumeData.ltAddTorrentParams.ti->info_hashes()
: resumeData.ltAddTorrentParams.info_hashes)};
const bool isHybrid = infoHash.isHybrid(); const bool isHybrid = infoHash.isHybrid();
const auto torrentIDv2 = TorrentID::fromInfoHash(infoHash); const auto torrentIDv2 = TorrentID::fromInfoHash(infoHash);
const auto torrentIDv1 = TorrentID::fromSHA1Hash(infoHash.v1()); const auto torrentIDv1 = TorrentID::fromSHA1Hash(infoHash.v1());
@ -1494,9 +1531,6 @@ void SessionImpl::processNextResumeData(ResumeSessionContext *context)
return; return;
} }
#else #else
const lt::sha1_hash infoHash = (resumeData.ltAddTorrentParams.ti
? resumeData.ltAddTorrentParams.ti->info_hash()
: resumeData.ltAddTorrentParams.info_hash);
if (torrentID != TorrentID::fromInfoHash(infoHash)) if (torrentID != TorrentID::fromInfoHash(infoHash))
{ {
LogMsg(tr("Failed to resume torrent: inconsistent torrent ID is detected. Torrent: \"%1\"") LogMsg(tr("Failed to resume torrent: inconsistent torrent ID is detected. Torrent: \"%1\"")
@ -2398,7 +2432,7 @@ Torrent *SessionImpl::getTorrent(const TorrentID &id) const
Torrent *SessionImpl::findTorrent(const InfoHash &infoHash) const Torrent *SessionImpl::findTorrent(const InfoHash &infoHash) const
{ {
const auto id = TorrentID::fromInfoHash(infoHash); const auto id = TorrentID::fromInfoHash(infoHash);
if (Torrent *torrent = m_torrents.value(id); torrent) if (Torrent *torrent = m_torrents.value(id))
return torrent; return torrent;
if (!infoHash.isHybrid()) if (!infoHash.isHybrid())
@ -2515,7 +2549,7 @@ bool SessionImpl::cancelDownloadMetadata(const TorrentID &id)
return true; return true;
#ifdef QBT_USES_LIBTORRENT2 #ifdef QBT_USES_LIBTORRENT2
const InfoHash infoHash {nativeHandle.info_hashes()}; const InfoHash infoHash = getInfoHash(nativeHandle);
if (infoHash.isHybrid()) if (infoHash.isHybrid())
{ {
// if magnet link was hybrid initially then it is indexed also by v1 info hash // if magnet link was hybrid initially then it is indexed also by v1 info hash
@ -5417,13 +5451,8 @@ bool SessionImpl::addMoveTorrentStorageJob(TorrentImpl *torrent, const Path &new
void SessionImpl::moveTorrentStorage(const MoveStorageJob &job) const void SessionImpl::moveTorrentStorage(const MoveStorageJob &job) const
{ {
#ifdef QBT_USES_LIBTORRENT2 const TorrentImpl *torrent = getTorrent(job.torrentHandle);
const auto id = TorrentID::fromInfoHash(job.torrentHandle.info_hashes()); const QString torrentName = (torrent ? torrent->name() : getInfoHash(job.torrentHandle).toTorrentID().toString());
#else
const auto id = TorrentID::fromInfoHash(job.torrentHandle.info_hash());
#endif
const TorrentImpl *torrent = m_torrents.value(id);
const QString torrentName = (torrent ? torrent->name() : id.toString());
LogMsg(tr("Start moving torrent. Torrent: \"%1\". Destination: \"%2\"").arg(torrentName, job.path.toString())); LogMsg(tr("Start moving torrent. Torrent: \"%1\". Destination: \"%2\"").arg(torrentName, job.path.toString()));
job.torrentHandle.move_storage(job.path.toString().toStdString(), toNative(job.mode)); job.torrentHandle.move_storage(job.path.toString().toStdString(), toNative(job.mode));
@ -5443,7 +5472,7 @@ void SessionImpl::handleMoveTorrentStorageJobFinished(const Path &newPath)
const bool torrentHasOutstandingJob = (iter != m_moveStorageQueue.cend()); const bool torrentHasOutstandingJob = (iter != m_moveStorageQueue.cend());
TorrentImpl *torrent = m_torrents.value(finishedJob.torrentHandle.info_hash()); TorrentImpl *torrent = getTorrent(finishedJob.torrentHandle);
if (torrent) if (torrent)
{ {
torrent->handleMoveStorageJobFinished(newPath, finishedJob.context, torrentHasOutstandingJob); torrent->handleMoveStorageJobFinished(newPath, finishedJob.context, torrentHasOutstandingJob);
@ -5451,8 +5480,9 @@ void SessionImpl::handleMoveTorrentStorageJobFinished(const Path &newPath)
else if (!torrentHasOutstandingJob) else if (!torrentHasOutstandingJob)
{ {
// Last job is completed for torrent that being removing, so actually remove it // Last job is completed for torrent that being removing, so actually remove it
const lt::torrent_handle nativeHandle {finishedJob.torrentHandle}; const lt::torrent_handle nativeHandle = finishedJob.torrentHandle;
const RemovingTorrentData &removingTorrentData = m_removingTorrents[nativeHandle.info_hash()]; const TorrentID torrentID = getInfoHash(nativeHandle).toTorrentID();
const RemovingTorrentData &removingTorrentData = m_removingTorrents[torrentID];
if (removingTorrentData.removeOption == TorrentRemoveOption::KeepContent) if (removingTorrentData.removeOption == TorrentRemoveOption::KeepContent)
m_nativeSession->remove_torrent(nativeHandle, lt::session::delete_partfile); m_nativeSession->remove_torrent(nativeHandle, lt::session::delete_partfile);
} }
@ -5749,15 +5779,10 @@ void SessionImpl::handleAddTorrentAlert(const lt::add_torrent_alert *alert)
LogMsg(tr("Failed to load torrent. Reason: \"%1\"").arg(msg), Log::WARNING); LogMsg(tr("Failed to load torrent. Reason: \"%1\"").arg(msg), Log::WARNING);
emit loadTorrentFailed(msg); emit loadTorrentFailed(msg);
const lt::add_torrent_params &params = alert->params; const InfoHash infoHash = getInfoHash(alert->params);
const bool hasMetadata = (params.ti && params.ti->is_valid());
#ifdef QBT_USES_LIBTORRENT2 #ifdef QBT_USES_LIBTORRENT2
const InfoHash infoHash {(hasMetadata ? params.ti->info_hashes() : params.info_hashes)};
if (infoHash.isHybrid()) if (infoHash.isHybrid())
m_hybridTorrentsByAltID.remove(TorrentID::fromSHA1Hash(infoHash.v1())); m_hybridTorrentsByAltID.remove(TorrentID::fromSHA1Hash(infoHash.v1()));
#else
const InfoHash infoHash {(hasMetadata ? params.ti->info_hash() : params.info_hash)};
#endif #endif
if (const auto loadingTorrentsIter = m_loadingTorrents.constFind(TorrentID::fromInfoHash(infoHash)) if (const auto loadingTorrentsIter = m_loadingTorrents.constFind(TorrentID::fromInfoHash(infoHash))
; loadingTorrentsIter != m_loadingTorrents.cend()) ; loadingTorrentsIter != m_loadingTorrents.cend())
@ -5782,11 +5807,7 @@ void SessionImpl::handleAddTorrentAlert(const lt::add_torrent_alert *alert)
return; return;
} }
#ifdef QBT_USES_LIBTORRENT2 const InfoHash infoHash = getInfoHash(alert->handle);
const InfoHash infoHash {alert->handle.info_hashes()};
#else
const InfoHash infoHash {alert->handle.info_hash()};
#endif
const auto torrentID = TorrentID::fromInfoHash(infoHash); const auto torrentID = TorrentID::fromInfoHash(infoHash);
if (const auto loadingTorrentsIter = m_loadingTorrents.constFind(torrentID) if (const auto loadingTorrentsIter = m_loadingTorrents.constFind(torrentID)
@ -5930,12 +5951,11 @@ void SessionImpl::dispatchTorrentAlert(const lt::torrent_alert *alert)
--m_numResumeData; --m_numResumeData;
} }
const TorrentID torrentID {alert->handle.info_hash()}; TorrentImpl *torrent = getTorrent(alert->handle);
TorrentImpl *torrent = m_torrents.value(torrentID);
#ifdef QBT_USES_LIBTORRENT2 #ifdef QBT_USES_LIBTORRENT2
if (!torrent && (alert->type() == lt::metadata_received_alert::alert_type)) if (!torrent && (alert->type() == lt::metadata_received_alert::alert_type))
{ {
const InfoHash infoHash {alert->handle.info_hashes()}; const InfoHash infoHash = getInfoHash(alert->handle);
if (infoHash.isHybrid()) if (infoHash.isHybrid())
torrent = m_torrents.value(TorrentID::fromSHA1Hash(infoHash.v1())); torrent = m_torrents.value(TorrentID::fromSHA1Hash(infoHash.v1()));
} }
@ -6000,6 +6020,11 @@ TorrentImpl *SessionImpl::createTorrent(const lt::torrent_handle &nativeHandle,
return torrent; return torrent;
} }
TorrentImpl *SessionImpl::getTorrent(const lt::torrent_handle &nativeHandle) const
{
return m_torrents.value(getInfoHash(nativeHandle).toTorrentID());
}
void SessionImpl::handleTorrentRemovedAlert(const lt::torrent_removed_alert */*alert*/) void SessionImpl::handleTorrentRemovedAlert(const lt::torrent_removed_alert */*alert*/)
{ {
// We cannot consider `torrent_removed_alert` as a starting point for removing content, // We cannot consider `torrent_removed_alert` as a starting point for removing content,
@ -6009,34 +6034,19 @@ void SessionImpl::handleTorrentRemovedAlert(const lt::torrent_removed_alert */*a
void SessionImpl::handleTorrentDeletedAlert(const lt::torrent_deleted_alert *alert) void SessionImpl::handleTorrentDeletedAlert(const lt::torrent_deleted_alert *alert)
{ {
#ifdef QBT_USES_LIBTORRENT2 handleRemovedTorrent(getInfoHash(*alert).toTorrentID());
const auto torrentID = TorrentID::fromInfoHash(alert->info_hashes);
#else
const auto torrentID = TorrentID::fromInfoHash(alert->info_hash);
#endif
handleRemovedTorrent(torrentID);
} }
void SessionImpl::handleTorrentDeleteFailedAlert(const lt::torrent_delete_failed_alert *alert) void SessionImpl::handleTorrentDeleteFailedAlert(const lt::torrent_delete_failed_alert *alert)
{ {
#ifdef QBT_USES_LIBTORRENT2 const TorrentID torrentID = getInfoHash(*alert).toTorrentID();
const auto torrentID = TorrentID::fromInfoHash(alert->info_hashes);
#else
const auto torrentID = TorrentID::fromInfoHash(alert->info_hash);
#endif
const auto errorMessage = alert->error ? Utils::String::fromLocal8Bit(alert->error.message()) : QString(); const auto errorMessage = alert->error ? Utils::String::fromLocal8Bit(alert->error.message()) : QString();
handleRemovedTorrent(torrentID, errorMessage); handleRemovedTorrent(torrentID, errorMessage);
} }
void SessionImpl::handleTorrentNeedCertAlert(const lt::torrent_need_cert_alert *alert) void SessionImpl::handleTorrentNeedCertAlert(const lt::torrent_need_cert_alert *alert)
{ {
#ifdef QBT_USES_LIBTORRENT2 const TorrentID torrentID = getInfoHash(alert->handle).toTorrentID();
const InfoHash infoHash {alert->handle.info_hashes()};
#else
const InfoHash infoHash {alert->handle.info_hash()};
#endif
const auto torrentID = TorrentID::fromInfoHash(infoHash);
TorrentImpl *const torrent = m_torrents.value(torrentID); TorrentImpl *const torrent = m_torrents.value(torrentID);
if (!torrent) [[unlikely]] if (!torrent) [[unlikely]]
return; return;
@ -6050,7 +6060,8 @@ void SessionImpl::handleTorrentNeedCertAlert(const lt::torrent_need_cert_alert *
void SessionImpl::handleMetadataReceivedAlert(const lt::metadata_received_alert *alert) void SessionImpl::handleMetadataReceivedAlert(const lt::metadata_received_alert *alert)
{ {
const TorrentID torrentID {alert->handle.info_hash()}; const InfoHash infoHash = getInfoHash(alert->handle);
const TorrentID torrentID = infoHash.toTorrentID();
bool found = false; bool found = false;
if (const auto iter = m_downloadedMetadata.constFind(torrentID); iter != m_downloadedMetadata.cend()) if (const auto iter = m_downloadedMetadata.constFind(torrentID); iter != m_downloadedMetadata.cend())
@ -6059,7 +6070,6 @@ void SessionImpl::handleMetadataReceivedAlert(const lt::metadata_received_alert
m_downloadedMetadata.erase(iter); m_downloadedMetadata.erase(iter);
} }
#ifdef QBT_USES_LIBTORRENT2 #ifdef QBT_USES_LIBTORRENT2
const InfoHash infoHash {alert->handle.info_hashes()};
if (infoHash.isHybrid()) if (infoHash.isHybrid())
{ {
const auto altID = TorrentID::fromSHA1Hash(infoHash.v1()); const auto altID = TorrentID::fromSHA1Hash(infoHash.v1());
@ -6081,7 +6091,7 @@ void SessionImpl::handleMetadataReceivedAlert(const lt::metadata_received_alert
void SessionImpl::handleFileErrorAlert(const lt::file_error_alert *alert) void SessionImpl::handleFileErrorAlert(const lt::file_error_alert *alert)
{ {
TorrentImpl *const torrent = m_torrents.value(alert->handle.info_hash()); TorrentImpl *const torrent = getTorrent(alert->handle);
if (!torrent) if (!torrent)
return; return;
@ -6152,7 +6162,7 @@ void SessionImpl::handlePeerBanAlert(const lt::peer_ban_alert *alert)
void SessionImpl::handleUrlSeedAlert(const lt::url_seed_alert *alert) void SessionImpl::handleUrlSeedAlert(const lt::url_seed_alert *alert)
{ {
const TorrentImpl *torrent = m_torrents.value(alert->handle.info_hash()); const TorrentImpl *torrent = getTorrent(alert->handle);
if (!torrent) if (!torrent)
return; return;
@ -6331,14 +6341,8 @@ void SessionImpl::handleStorageMovedAlert(const lt::storage_moved_alert *alert)
const Path newPath {QString::fromUtf8(alert->storage_path())}; const Path newPath {QString::fromUtf8(alert->storage_path())};
Q_ASSERT(newPath == currentJob.path); Q_ASSERT(newPath == currentJob.path);
#ifdef QBT_USES_LIBTORRENT2 TorrentImpl *torrent = getTorrent(currentJob.torrentHandle);
const auto id = TorrentID::fromInfoHash(currentJob.torrentHandle.info_hashes()); const QString torrentName = (torrent ? torrent->name() : getInfoHash(currentJob.torrentHandle).toTorrentID().toString());
#else
const auto id = TorrentID::fromInfoHash(currentJob.torrentHandle.info_hash());
#endif
TorrentImpl *torrent = m_torrents.value(id);
const QString torrentName = (torrent ? torrent->name() : id.toString());
LogMsg(tr("Moved torrent successfully. Torrent: \"%1\". Destination: \"%2\"").arg(torrentName, newPath.toString())); LogMsg(tr("Moved torrent successfully. Torrent: \"%1\". Destination: \"%2\"").arg(torrentName, newPath.toString()));
handleMoveTorrentStorageJobFinished(newPath); handleMoveTorrentStorageJobFinished(newPath);
@ -6351,14 +6355,8 @@ void SessionImpl::handleStorageMovedFailedAlert(const lt::storage_moved_failed_a
const MoveStorageJob &currentJob = m_moveStorageQueue.constFirst(); const MoveStorageJob &currentJob = m_moveStorageQueue.constFirst();
Q_ASSERT(currentJob.torrentHandle == alert->handle); Q_ASSERT(currentJob.torrentHandle == alert->handle);
#ifdef QBT_USES_LIBTORRENT2 TorrentImpl *torrent = getTorrent(currentJob.torrentHandle);
const auto id = TorrentID::fromInfoHash(currentJob.torrentHandle.info_hashes()); const QString torrentName = (torrent ? torrent->name() : getInfoHash(currentJob.torrentHandle).toTorrentID().toString());
#else
const auto id = TorrentID::fromInfoHash(currentJob.torrentHandle.info_hash());
#endif
TorrentImpl *torrent = m_torrents.value(id);
const QString torrentName = (torrent ? torrent->name() : id.toString());
const Path currentLocation = (torrent ? torrent->actualStorageLocation() const Path currentLocation = (torrent ? torrent->actualStorageLocation()
: Path(alert->handle.status(lt::torrent_handle::query_save_path).save_path)); : Path(alert->handle.status(lt::torrent_handle::query_save_path).save_path));
const QString errorMessage = QString::fromStdString(alert->message()); const QString errorMessage = QString::fromStdString(alert->message());
@ -6375,12 +6373,7 @@ void SessionImpl::handleStateUpdateAlert(const lt::state_update_alert *alert)
for (const lt::torrent_status &status : alert->status) for (const lt::torrent_status &status : alert->status)
{ {
#ifdef QBT_USES_LIBTORRENT2 TorrentImpl *const torrent = getTorrent(status.handle);
const auto id = TorrentID::fromInfoHash(status.info_hashes);
#else
const auto id = TorrentID::fromInfoHash(status.info_hash);
#endif
TorrentImpl *const torrent = m_torrents.value(id);
if (!torrent) if (!torrent)
continue; continue;
@ -6424,7 +6417,7 @@ void SessionImpl::handleI2PAlert(const lt::i2p_alert *alert) const
void SessionImpl::handleTrackerAlert(const lt::tracker_alert *alert) void SessionImpl::handleTrackerAlert(const lt::tracker_alert *alert)
{ {
TorrentImpl *torrent = m_torrents.value(alert->handle.info_hash()); TorrentImpl *torrent = getTorrent(alert->handle);
if (!torrent) if (!torrent)
return; return;
@ -6450,8 +6443,9 @@ void SessionImpl::handleTrackerAlert(const lt::tracker_alert *alert)
#ifdef QBT_USES_LIBTORRENT2 #ifdef QBT_USES_LIBTORRENT2
void SessionImpl::handleTorrentConflictAlert(const lt::torrent_conflict_alert *alert) void SessionImpl::handleTorrentConflictAlert(const lt::torrent_conflict_alert *alert)
{ {
const auto torrentIDv1 = TorrentID::fromSHA1Hash(alert->metadata->info_hashes().v1); const InfoHash infoHash = getInfoHash(*alert->metadata);
const auto torrentIDv2 = TorrentID::fromSHA256Hash(alert->metadata->info_hashes().v2); const auto torrentIDv1 = TorrentID::fromSHA1Hash(infoHash.v1());
const auto torrentIDv2 = TorrentID::fromSHA256Hash(infoHash.v2());
TorrentImpl *torrent1 = m_torrents.value(torrentIDv1); TorrentImpl *torrent1 = m_torrents.value(torrentIDv1);
TorrentImpl *torrent2 = m_torrents.value(torrentIDv2); TorrentImpl *torrent2 = m_torrents.value(torrentIDv2);
if (torrent2) if (torrent2)

View file

@ -606,6 +606,7 @@ namespace BitTorrent
#endif #endif
TorrentImpl *createTorrent(const lt::torrent_handle &nativeHandle, const LoadTorrentParams &params); TorrentImpl *createTorrent(const lt::torrent_handle &nativeHandle, const LoadTorrentParams &params);
TorrentImpl *getTorrent(const lt::torrent_handle &nativeHandle) const;
void saveResumeData(); void saveResumeData();
void saveTorrentsQueue(); void saveTorrentsQueue();