From cf65f85dc3ee476c76dc856bac1f5b5391afbffd Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 3 Jul 2016 14:48:18 -0400 Subject: [PATCH] fix file_completed_alert (#880) --- ChangeLog | 2 ++ include/libtorrent/aux_/file_progress.hpp | 1 + include/libtorrent/torrent.hpp | 4 +++ simulation/test_swarm.cpp | 35 +++++++++++++++++++++++ src/file_progress.cpp | 2 +- src/torrent.cpp | 15 +++++----- 6 files changed, 50 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8008b89a8..8646b6fd0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 1.1.1 release + * fix bug where file_completed_alert would not be posted unless file_progress + had been queries by the client * move files one-by-one when moving storage for a torrent * fix bug in enum_net() for BSD and Mac * fix bug in python binding of announce_entry diff --git a/include/libtorrent/aux_/file_progress.hpp b/include/libtorrent/aux_/file_progress.hpp index ba7bf2825..c4ddf9bf4 100644 --- a/include/libtorrent/aux_/file_progress.hpp +++ b/include/libtorrent/aux_/file_progress.hpp @@ -56,6 +56,7 @@ namespace aux void export_progress(std::vector &fp); + bool empty() const { return m_file_progress.empty(); } void clear(); void update(file_storage const& fs, int index diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index fb4e1bfa3..0e168ddc6 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -801,10 +801,14 @@ namespace libtorrent return std::binary_search(m_predictive_pieces.begin(), m_predictive_pieces.end(), index); } + private: + // called when we learn that we have a piece // only once per piece void we_have(int index); + public: + int num_have() const { // pretend we have every piece when in seed mode diff --git a/simulation/test_swarm.cpp b/simulation/test_swarm.cpp index bf9f217a8..a7ca939aa 100644 --- a/simulation/test_swarm.cpp +++ b/simulation/test_swarm.cpp @@ -414,6 +414,41 @@ TORRENT_TEST(delete_partfile) TEST_CHECK(!ec); } +TORRENT_TEST(torrent_completed_alert) +{ + int num_file_completed = false; + + setup_swarm(2, swarm_test::download + // add session + , [](lt::settings_pack& pack) + { + pack.set_int(lt::settings_pack::alert_mask, alert::progress_notification); + } + // add torrent + , [](lt::add_torrent_params&) {} + // on alert + , [&](lt::alert const* a, lt::session&) + { + auto tc = alert_cast(a); + if (tc == nullptr) return; + ++num_file_completed; + } + // terminate + , [](int ticks, lt::session& ses) -> bool + { + if (ticks > 80) + { + TEST_ERROR("timeout"); + return true; + } + if (!is_seed(ses)) return false; + printf("completed in %d ticks\n", ticks); + return true; + }); + + TEST_EQUAL(num_file_completed, 1); +} + // TODO: add test that makes sure a torrent in graceful pause mode won't make // outgoing connections // TODO: add test that makes sure a torrent in graceful pause mode won't accept diff --git a/src/file_progress.cpp b/src/file_progress.cpp index 7aaa8f45a..e416d70d1 100644 --- a/src/file_progress.cpp +++ b/src/file_progress.cpp @@ -80,7 +80,7 @@ namespace libtorrent { namespace aux while (size) { - int add = (std::min)(boost::int64_t(size), fs.file_size(file_index) - file_offset); + int const add = (std::min)(boost::int64_t(size), fs.file_size(file_index) - file_offset); TORRENT_ASSERT(add >= 0); m_file_progress[file_index] += add; diff --git a/src/torrent.cpp b/src/torrent.cpp index 5670a0b15..7218ddb5c 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1354,6 +1354,13 @@ namespace libtorrent + block_size() - 1) / block_size(); m_picker->init(blocks_per_piece, blocks_in_last_piece, m_torrent_file->num_pieces()); + // initialize the file progress too + if (m_file_progress.empty()) + { + TORRENT_ASSERT(has_picker()); + m_file_progress.init(picker(), m_torrent_file->files()); + } + update_gauge(); for (peer_iterator i = m_connections.begin() @@ -2612,7 +2619,6 @@ namespace libtorrent // removing the piece picker will clear the user priorities // instead, just clear which pieces we have -// m_picker.reset(); if (m_picker) { int blocks_per_piece = (m_torrent_file->piece_length() + block_size() - 1) / block_size(); @@ -11693,13 +11699,6 @@ namespace libtorrent return; } - // if this is the first time the client asks for file progress. - // allocate it and make sure it's up to date - - // we cover the case where we're a seed above - TORRENT_ASSERT(has_picker()); - m_file_progress.init(picker(), m_torrent_file->files()); - m_file_progress.export_progress(fp); if (flags & torrent_handle::piece_granularity)