diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 962f5c6e0..7fa4015e9 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -1300,13 +1300,21 @@ void TorrentHandle::toggleSequentialDownload() void TorrentHandle::setFirstLastPiecePriority(const bool enabled) { + setFirstLastPiecePriorityImpl(enabled); +} + +void TorrentHandle::setFirstLastPiecePriorityImpl(const bool enabled, const QVector &updatedFilePrio) +{ + // Download first and last pieces first for every file in the torrent + if (!hasMetadata()) { m_needsToSetFirstLastPiecePriority = enabled; return; } - // Download first and last pieces first for every file in the torrent - const std::vector filePriorities = nativeHandle().file_priorities(); + // Updating file priorities is an async operation in libtorrent, when we just updated it and immediately query it + // we might get the old/wrong values, so we rely on `updatedFilePrio` in this case. + const std::vector filePriorities = !updatedFilePrio.isEmpty() ? updatedFilePrio.toStdVector() : nativeHandle().file_priorities(); std::vector piecePriorities = nativeHandle().piece_priorities(); for (int index = 0; index < static_cast(filePriorities.size()); ++index) { const int filePrio = filePriorities[index]; @@ -2064,7 +2072,7 @@ void TorrentHandle::prioritizeFiles(const QVector &priorities) // Restore first/last piece first option if necessary if (firstLastPieceFirst) - setFirstLastPiecePriority(true); + setFirstLastPiecePriorityImpl(true, priorities); updateStatus(); } diff --git a/src/base/bittorrent/torrenthandle.h b/src/base/bittorrent/torrenthandle.h index d5ad32bf7..88fe62946 100644 --- a/src/base/bittorrent/torrenthandle.h +++ b/src/base/bittorrent/torrenthandle.h @@ -420,6 +420,7 @@ namespace BitTorrent bool addTracker(const TrackerEntry &tracker); bool addUrlSeed(const QUrl &urlSeed); bool removeUrlSeed(const QUrl &urlSeed); + void setFirstLastPiecePriorityImpl(bool enabled, const QVector &updatedFilePrio = {}); Session *const m_session; libtorrent::torrent_handle m_nativeHandle;