Browse Source

Optimize completed files handling

PR #16329.

Co-authored-by: Vladimir Golovnev (Glassez) <glassez@yandex.ru>
adaptive-webui-19844
Prince Gupta 3 years ago committed by GitHub
parent
commit
0012a3ede7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      src/base/bittorrent/torrentimpl.cpp
  2. 1
      src/base/bittorrent/torrentimpl.h

24
src/base/bittorrent/torrentimpl.cpp

@ -272,13 +272,16 @@ TorrentImpl::TorrentImpl(Session *session, lt::session *nativeSession
m_torrentInfo = TorrentInfo(*m_ltAddTorrentParams.ti); m_torrentInfo = TorrentInfo(*m_ltAddTorrentParams.ti);
Q_ASSERT(m_filePaths.isEmpty()); Q_ASSERT(m_filePaths.isEmpty());
Q_ASSERT(m_indexMap.isEmpty());
const int filesCount = m_torrentInfo.filesCount(); const int filesCount = m_torrentInfo.filesCount();
m_filePaths.reserve(filesCount); m_filePaths.reserve(filesCount);
m_indexMap.reserve(filesCount);
const std::shared_ptr<const lt::torrent_info> currentInfo = m_nativeHandle.torrent_file(); const std::shared_ptr<const lt::torrent_info> currentInfo = m_nativeHandle.torrent_file();
const lt::file_storage &fileStorage = currentInfo->files(); const lt::file_storage &fileStorage = currentInfo->files();
for (int i = 0; i < filesCount; ++i) for (int i = 0; i < filesCount; ++i)
{ {
const lt::file_index_t nativeIndex = m_torrentInfo.nativeIndexes().at(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))); 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); 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 QString TorrentImpl::actualFilePath(const int index) const
{ {
const auto nativeIndex = m_torrentInfo.nativeIndexes().at(index); 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 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) void TorrentImpl::endReceivedMetadataHandling(const QString &savePath, const QStringList &fileNames)
{ {
Q_ASSERT(m_filePaths.isEmpty()); Q_ASSERT(m_filePaths.isEmpty());
Q_ASSERT(m_indexMap.isEmpty());
lt::add_torrent_params &p = m_ltAddTorrentParams; lt::add_torrent_params &p = m_ltAddTorrentParams;
const std::shared_ptr<lt::torrent_info> metadata = std::const_pointer_cast<lt::torrent_info>(m_nativeHandle.torrent_file()); const std::shared_ptr<lt::torrent_info> metadata = std::const_pointer_cast<lt::torrent_info>(m_nativeHandle.torrent_file());
m_torrentInfo = TorrentInfo(*metadata); m_torrentInfo = TorrentInfo(*metadata);
m_indexMap.reserve(filesCount());
const auto nativeIndexes = m_torrentInfo.nativeIndexes(); const auto nativeIndexes = m_torrentInfo.nativeIndexes();
for (int i = 0; i < fileNames.size(); ++i) for (int i = 0; i < fileNames.size(); ++i)
{ {
const auto nativeIndex = nativeIndexes.at(i);
m_indexMap[nativeIndex] = i;
const QString filePath = fileNames.at(i); const QString filePath = fileNames.at(i);
m_filePaths.append(filePath.endsWith(QB_EXT, Qt::CaseInsensitive) ? filePath.chopped(QB_EXT.size()) : filePath); 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.save_path = Utils::Fs::toNativePath(savePath).toStdString();
p.ti = metadata; p.ti = metadata;
@ -1858,7 +1867,7 @@ void TorrentImpl::handleFastResumeRejectedAlert(const lt::fastresume_rejected_al
void TorrentImpl::handleFileRenamedAlert(const lt::file_renamed_alert *p) 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); Q_ASSERT(fileIndex >= 0);
// Remove empty leftover folders // 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) 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); Q_ASSERT(fileIndex >= 0);
LogMsg(tr("File rename failed. Torrent: \"%1\", file: \"%2\", reason: \"%3\"") 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) 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()) if (m_session->isAppendExtensionEnabled())
{ {
const int fileIndex = m_indexMap.value(p->index, -1);
Q_ASSERT(fileIndex >= 0);
const QString path = filePath(fileIndex); const QString path = filePath(fileIndex);
const QString actualPath = actualFilePath(fileIndex); const QString actualPath = actualFilePath(fileIndex);
if (actualPath != path) if (actualPath != path)

1
src/base/bittorrent/torrentimpl.h

@ -286,6 +286,7 @@ namespace BitTorrent
TorrentState m_state = TorrentState::Unknown; TorrentState m_state = TorrentState::Unknown;
TorrentInfo m_torrentInfo; TorrentInfo m_torrentInfo;
QStringList m_filePaths; QStringList m_filePaths;
QHash<lt::file_index_t, int> m_indexMap;
SpeedMonitor m_speedMonitor; SpeedMonitor m_speedMonitor;
InfoHash m_infoHash; InfoHash m_infoHash;

Loading…
Cancel
Save