From 389664213b7983c80c43008cb0b3074d0f88b7bd Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sun, 29 Nov 2020 18:40:49 +0300 Subject: [PATCH] Don't rewrite TorrentInfo instance if it's valid --- src/base/bittorrent/torrenthandleimpl.cpp | 30 +++++++---------------- src/base/bittorrent/torrenthandleimpl.h | 1 - 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/base/bittorrent/torrenthandleimpl.cpp b/src/base/bittorrent/torrenthandleimpl.cpp index f9928584a..792a5a72a 100644 --- a/src/base/bittorrent/torrenthandleimpl.cpp +++ b/src/base/bittorrent/torrenthandleimpl.cpp @@ -123,8 +123,10 @@ TorrentHandleImpl::TorrentHandleImpl(Session *session, const lt::torrent_handle if (m_useAutoTMM) m_savePath = Utils::Fs::toNativePath(m_session->categorySavePath(m_category)); + m_hash = InfoHash {m_nativeHandle.info_hash()}; + m_torrentInfo = TorrentInfo {m_nativeHandle.torrent_file()}; + updateStatus(); - m_hash = InfoHash(m_nativeStatus.info_hash); if (hasMetadata()) { @@ -792,7 +794,7 @@ void TorrentHandleImpl::updateState() bool TorrentHandleImpl::hasMetadata() const { - return m_nativeStatus.has_metadata; + return m_torrentInfo.isValid(); } bool TorrentHandleImpl::hasMissingFiles() const @@ -1560,12 +1562,8 @@ void TorrentHandleImpl::handleFastResumeRejectedAlert(const lt::fastresume_rejec void TorrentHandleImpl::handleFileRenamedAlert(const lt::file_renamed_alert *p) { - // We don't really need to call updateStatus() in this place. - // All we need to do is make sure we have a valid instance of the TorrentInfo object. - m_torrentInfo = TorrentInfo {m_nativeHandle.torrent_file()}; - - // remove empty leftover folders - // for example renaming "a/b/c" to "d/b/c", then folders "a/b" and "a" will + // Remove empty leftover folders + // For example renaming "a/b/c" to "d/b/c", then folders "a/b" and "a" will // be removed if they are empty const QString oldFilePath = m_oldPath[p->index].takeFirst(); const QString newFilePath = Utils::Fs::toUniformPath(p->new_name()); @@ -1626,10 +1624,6 @@ void TorrentHandleImpl::handleFileRenameFailedAlert(const lt::file_rename_failed void TorrentHandleImpl::handleFileCompletedAlert(const lt::file_completed_alert *p) { - // We don't really need to call updateStatus() in this place. - // All we need to do is make sure we have a valid instance of the TorrentInfo object. - m_torrentInfo = TorrentInfo {m_nativeHandle.torrent_file()}; - qDebug("A file completed download in torrent \"%s\"", qUtf8Printable(name())); if (m_session->isAppendExtensionEnabled()) { @@ -1647,8 +1641,10 @@ void TorrentHandleImpl::handleFileCompletedAlert(const lt::file_completed_alert void TorrentHandleImpl::handleMetadataReceivedAlert(const lt::metadata_received_alert *p) { Q_UNUSED(p); + qDebug("Metadata received for torrent %s.", qUtf8Printable(name())); - updateStatus(); + m_torrentInfo = TorrentInfo {m_nativeHandle.torrent_file()}; + if (m_session->isAppendExtensionEnabled()) manageIncompleteFiles(); if (!m_hasRootFolder) @@ -1814,13 +1810,6 @@ lt::torrent_handle TorrentHandleImpl::nativeHandle() const return m_nativeHandle; } -void TorrentHandleImpl::updateTorrentInfo() -{ - if (!hasMetadata()) return; - - m_torrentInfo = TorrentInfo(m_nativeStatus.torrent_file.lock()); -} - bool TorrentHandleImpl::isMoveInProgress() const { return m_storageIsMoving; @@ -1841,7 +1830,6 @@ void TorrentHandleImpl::updateStatus(const lt::torrent_status &nativeStatus) m_nativeStatus = nativeStatus; updateState(); - updateTorrentInfo(); // NOTE: Don't change the order of these conditionals! // Otherwise it will not work properly since torrent can be CheckingDownloading. diff --git a/src/base/bittorrent/torrenthandleimpl.h b/src/base/bittorrent/torrenthandleimpl.h index d0cfb7d31..1c813a1a4 100644 --- a/src/base/bittorrent/torrenthandleimpl.h +++ b/src/base/bittorrent/torrenthandleimpl.h @@ -250,7 +250,6 @@ namespace BitTorrent void updateStatus(); void updateStatus(const lt::torrent_status &nativeStatus); void updateState(); - void updateTorrentInfo(); void handleFastResumeRejectedAlert(const lt::fastresume_rejected_alert *p); void handleFileCompletedAlert(const lt::file_completed_alert *p);