diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index 6613dd44e..f89a88e98 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -272,13 +272,16 @@ TorrentImpl::TorrentImpl(Session *session, lt::session *nativeSession m_torrentInfo = TorrentInfo(*m_ltAddTorrentParams.ti); Q_ASSERT(m_filePaths.isEmpty()); + Q_ASSERT(m_indexMap.isEmpty()); const int filesCount = m_torrentInfo.filesCount(); m_filePaths.reserve(filesCount); + m_indexMap.reserve(filesCount); const std::shared_ptr currentInfo = m_nativeHandle.torrent_file(); const lt::file_storage &fileStorage = currentInfo->files(); for (int i = 0; i < filesCount; ++i) { const lt::file_index_t nativeIndex = m_torrentInfo.nativeIndexes().at(i); + m_indexMap[nativeIndex] = i; const QString filePath = Utils::Fs::toUniformPath(QString::fromStdString(fileStorage.file_path(nativeIndex))); m_filePaths.append(filePath.endsWith(QB_EXT, Qt::CaseInsensitive) ? filePath.chopped(QB_EXT.size()) : filePath); } @@ -801,7 +804,8 @@ QString TorrentImpl::filePath(const int index) const QString TorrentImpl::actualFilePath(const int index) const { const auto nativeIndex = m_torrentInfo.nativeIndexes().at(index); - return QString::fromStdString(m_nativeHandle.torrent_file()->files().file_path(nativeIndex)); + const std::string filePath = m_nativeHandle.torrent_file()->files().file_path(nativeIndex); + return Utils::Fs::toUniformPath(QString::fromStdString(filePath)); } qlonglong TorrentImpl::fileSize(const int index) const @@ -1482,17 +1486,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_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; + const QString filePath = fileNames.at(i); m_filePaths.append(filePath.endsWith(QB_EXT, Qt::CaseInsensitive) ? filePath.chopped(QB_EXT.size()) : filePath); - p.renamed_files[nativeIndexes[i]] = filePath.toStdString(); + p.renamed_files[nativeIndex] = filePath.toStdString(); } p.save_path = Utils::Fs::toNativePath(savePath).toStdString(); p.ti = metadata; @@ -1858,7 +1867,7 @@ void TorrentImpl::handleFastResumeRejectedAlert(const lt::fastresume_rejected_al void TorrentImpl::handleFileRenamedAlert(const lt::file_renamed_alert *p) { - const int fileIndex = m_torrentInfo.nativeIndexes().indexOf(p->index); + const int fileIndex = m_indexMap.value(p->index, -1); Q_ASSERT(fileIndex >= 0); // Remove empty leftover folders @@ -1907,7 +1916,7 @@ void TorrentImpl::handleFileRenamedAlert(const lt::file_renamed_alert *p) void TorrentImpl::handleFileRenameFailedAlert(const lt::file_rename_failed_alert *p) { - const int fileIndex = m_torrentInfo.nativeIndexes().indexOf(p->index); + const int fileIndex = m_indexMap.value(p->index, -1); Q_ASSERT(fileIndex >= 0); LogMsg(tr("File rename failed. Torrent: \"%1\", file: \"%2\", reason: \"%3\"") @@ -1922,12 +1931,11 @@ void TorrentImpl::handleFileRenameFailedAlert(const lt::file_rename_failed_alert void TorrentImpl::handleFileCompletedAlert(const lt::file_completed_alert *p) { - const int fileIndex = m_torrentInfo.nativeIndexes().indexOf(p->index); - Q_ASSERT(fileIndex >= 0); - - qDebug("A file completed download in torrent \"%s\"", qUtf8Printable(name())); if (m_session->isAppendExtensionEnabled()) { + const int fileIndex = m_indexMap.value(p->index, -1); + Q_ASSERT(fileIndex >= 0); + const QString path = filePath(fileIndex); const QString actualPath = actualFilePath(fileIndex); if (actualPath != path) diff --git a/src/base/bittorrent/torrentimpl.h b/src/base/bittorrent/torrentimpl.h index 9c953741f..c665c897a 100644 --- a/src/base/bittorrent/torrentimpl.h +++ b/src/base/bittorrent/torrentimpl.h @@ -286,6 +286,7 @@ namespace BitTorrent TorrentState m_state = TorrentState::Unknown; TorrentInfo m_torrentInfo; QStringList m_filePaths; + QHash m_indexMap; SpeedMonitor m_speedMonitor; InfoHash m_infoHash;