From 049e3769535ca5a0b95f914b8b8d9c4978da3988 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 14 Mar 2022 12:03:54 +0300 Subject: [PATCH] Properly handle metadata download for an existing torrent --- src/base/bittorrent/torrentimpl.cpp | 38 +++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index f2c03abcd..897621fd3 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -236,6 +236,14 @@ namespace status.pieces = params.have_pieces; status.verified_pieces = params.verified_pieces; } + + template + Vector resized(const Vector &inVector, const typename Vector::size_type size, const typename Vector::value_type &defaultValue) + { + Vector outVector = inVector; + outVector.resize(size, defaultValue); + return outVector; + } } // TorrentImpl @@ -1504,27 +1512,22 @@ void TorrentImpl::fileSearchFinished(const QString &savePath, const QStringList void TorrentImpl::endReceivedMetadataHandling(const QString &savePath, const QStringList &fileNames) { Q_ASSERT(m_filePaths.isEmpty()); - Q_ASSERT(m_indexMap.isEmpty()); lt::add_torrent_params &p = m_ltAddTorrentParams; const std::shared_ptr metadata = std::const_pointer_cast(m_nativeHandle.torrent_file()); m_torrentInfo = TorrentInfo(*metadata); m_filePaths = fileNames; - m_indexMap.reserve(filesCount()); const auto nativeIndexes = m_torrentInfo.nativeIndexes(); for (int i = 0; i < fileNames.size(); ++i) { const auto nativeIndex = nativeIndexes.at(i); - m_indexMap[nativeIndex] = i; p.renamed_files[nativeIndex] = fileNames[i].toStdString(); } p.save_path = Utils::Fs::toNativePath(savePath).toStdString(); p.ti = metadata; - - const int internalFilesCount = p.ti->files().num_files(); // including .pad files - // Use qBittorrent default priority rather than libtorrent's (4) - p.file_priorities = std::vector(internalFilesCount, LT::toNative(DownloadPriority::Normal)); + p.file_priorities = resized(p.file_priorities, metadata->files().num_files() + , LT::toNative(p.file_priorities.empty() ? DownloadPriority::Normal : DownloadPriority::Ignored)); reload(); @@ -1566,7 +1569,8 @@ void TorrentImpl::reload() } m_nativeHandle = m_nativeSession->add_torrent(p); - m_nativeHandle.queue_position_set(queuePos); + if (queuePos >= lt::queue_position_t {}) + m_nativeHandle.queue_position_set(queuePos); } void TorrentImpl::pause() @@ -1781,6 +1785,8 @@ void TorrentImpl::handleSaveResumeDataAlert(const lt::save_resume_data_alert *p) { if (m_maintenanceJob == MaintenanceJob::HandleMetadata) { + Q_ASSERT(m_indexMap.isEmpty()); + m_ltAddTorrentParams = p->params; m_ltAddTorrentParams.have_pieces.clear(); @@ -1790,6 +1796,19 @@ void TorrentImpl::handleSaveResumeDataAlert(const lt::save_resume_data_alert *p) QStringList filePaths = metadata.filePaths(); applyContentLayout(filePaths, m_contentLayout); + + const auto nativeIndexes = metadata.nativeIndexes(); + const auto &renamedFiles = m_ltAddTorrentParams.renamed_files; + m_indexMap.reserve(filePaths.size()); + for (int i = 0; i < filePaths.size(); ++i) + { + const auto nativeIndex = nativeIndexes.at(i); + m_indexMap[nativeIndex] = i; + + if (const auto it = renamedFiles.find(nativeIndex); it != renamedFiles.cend()) + filePaths[i] = QString::fromStdString(it->second); + } + m_session->findIncompleteFiles(metadata, savePath(), downloadPath(), filePaths); } else @@ -1929,6 +1948,9 @@ void TorrentImpl::handleFileRenameFailedAlert(const lt::file_rename_failed_alert void TorrentImpl::handleFileCompletedAlert(const lt::file_completed_alert *p) { + if (m_maintenanceJob == MaintenanceJob::HandleMetadata) + return; + if (m_session->isAppendExtensionEnabled()) { const int fileIndex = m_indexMap.value(p->index, -1);