diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 2efbb735b..b890a614e 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -288,13 +288,13 @@ namespace libtorrent int piece_priority(int index) const; void prioritize_pieces(std::vector const& pieces); - void piece_priorities(std::vector&) const; + void piece_priorities(std::vector*) const; void set_file_priority(int index, int priority); int file_priority(int index) const; void prioritize_files(std::vector const& files); - void file_priorities(std::vector&) const; + void file_priorities(std::vector*) const; void set_piece_deadline(int piece, int t, int flags); void update_piece_priorities(); diff --git a/src/torrent.cpp b/src/torrent.cpp index 3164ebe16..3f0a4bc11 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -3418,7 +3418,7 @@ namespace libtorrent } } - void torrent::piece_priorities(std::vector& pieces) const + void torrent::piece_priorities(std::vector* pieces) const { INVARIANT_CHECK; @@ -3426,13 +3426,13 @@ namespace libtorrent TORRENT_ASSERT(valid_metadata()); if (is_seed()) { - pieces.clear(); - pieces.resize(m_torrent_file->num_pieces(), 1); + pieces->clear(); + pieces->resize(m_torrent_file->num_pieces(), 1); return; } TORRENT_ASSERT(m_picker.get()); - m_picker->piece_priorities(pieces); + m_picker->piece_priorities(*pieces); } namespace @@ -3456,7 +3456,18 @@ namespace libtorrent if (m_torrent_file->num_pieces() == 0) return; - std::copy(files.begin(), files.end(), m_file_priority.begin()); + int limit = int(files.size()); + if (valid_metadata() && limit > m_torrent_file->num_files()) + limit = m_torrent_file->num_files(); + + if (m_file_priority.size() < limit) + m_file_priority.resize(limit); + + std::copy(files.begin(), files.begin() + limit, m_file_priority.begin()); + + if (valid_metadata() && m_torrent_file->num_files() > int(m_file_priority.size())) + m_file_priority.resize(m_torrent_file->num_files(), 1); + update_piece_priorities(); } @@ -3486,11 +3497,20 @@ namespace libtorrent return m_file_priority[index]; } - void torrent::file_priorities(std::vector& files) const + void torrent::file_priorities(std::vector* files) const { INVARIANT_CHECK; - files.resize(m_file_priority.size()); - std::copy(m_file_priority.begin(), m_file_priority.end(), files.begin()); + if (!valid_metadata()) + { + files->resize(m_file_priority.size()); + std::copy(m_file_priority.begin(), m_file_priority.end(), files->begin()); + return; + } + + files->resize(m_torrent_file->num_files()); + std::copy(m_file_priority.begin(), m_file_priority.end(), files->begin()); + if (m_file_priority.size() < m_torrent_file->num_files()) + std::fill(files->begin() + m_file_priority.size(), files->end(), 1); } void torrent::update_piece_priorities() diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index c1b50a747..c910f8bed 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -522,7 +522,7 @@ namespace libtorrent { INVARIANT_CHECK; std::vector ret; - TORRENT_SYNC_CALL1(piece_priorities, boost::ref(ret)); + TORRENT_SYNC_CALL1(piece_priorities, &ret); return ret; } @@ -549,7 +549,7 @@ namespace libtorrent { INVARIANT_CHECK; std::vector ret; - TORRENT_SYNC_CALL1(file_priorities, ret); + TORRENT_SYNC_CALL1(file_priorities, &ret); return ret; } diff --git a/test/test_torrent.cpp b/test/test_torrent.cpp index d6900da62..3a6cdfe9b 100644 --- a/test/test_torrent.cpp +++ b/test/test_torrent.cpp @@ -67,6 +67,9 @@ void test_running_torrent(boost::intrusive_ptr info, size_type fil std::vector prio(3, 1); prio[0] = 0; h.prioritize_files(prio); + std::cout << "prio: " << prio.size() << std::endl; + std::cout << "ret prio: " << h.file_priorities().size() << std::endl; + TEST_CHECK(h.file_priorities().size() == info->num_files()); test_sleep(500); st = h.status();