From 6917a11c6bd91987cbed7fca6674753924f42bba Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 23 Jun 2016 13:19:35 -0400 Subject: [PATCH 1/2] =?UTF-8?q?correct=20error=20introduced=20in=20add=5Ft?= =?UTF-8?q?orrent=5Fimpl=20refactor,=20with=20regards=20t=E2=80=A6=20(#846?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit correct error introduced in add_torrent_impl refactor, with regards to not having flag_duplicate_is_error set (which is not set by default) --- include/libtorrent/aux_/session_impl.hpp | 5 +- include/libtorrent/torrent.hpp | 3 ++ src/session_impl.cpp | 51 ++++++++++--------- src/torrent.cpp | 11 +++-- test/test_torrent.cpp | 63 ++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 26 deletions(-) diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index fe00f8e9a..93e906885 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -406,7 +406,10 @@ namespace libtorrent #endif torrent_handle add_torrent(add_torrent_params const&, error_code& ec); - boost::shared_ptr add_torrent_impl(add_torrent_params& p, error_code& ec); + // second return value is true if the torrent was added and false if an + // existing one was found. + std::pair, bool> + add_torrent_impl(add_torrent_params& p, error_code& ec); void async_add_torrent(add_torrent_params* params); void on_async_load_torrent(disk_io_job const* j); diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 8c0c00d92..fb4e1bfa3 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -1717,6 +1717,9 @@ namespace libtorrent public: // set to false until we've loaded resume data bool m_resume_data_loaded; + + // set to true when torrent is start()ed. It may only be started once + bool m_was_started; #endif }; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 58286ec56..efd1ae0f4 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -4784,7 +4784,9 @@ retry: { // params is updated by add_torrent_impl() add_torrent_params params = p; - boost::shared_ptr const torrent_ptr = add_torrent_impl(params, ec); + boost::shared_ptr torrent_ptr; + bool added; + boost::tie(torrent_ptr, added) = add_torrent_impl(params, ec); torrent_handle const handle(torrent_ptr); m_alerts.emplace_alert(handle, params, ec); @@ -4807,9 +4809,25 @@ retry: if (!peers.empty()) torrent_ptr->update_want_peers(); +#ifndef TORRENT_DISABLE_DHT + if (params.ti) + { + torrent_info::nodes_t const& nodes = params.ti->nodes(); + for (std::vector >::const_iterator i = nodes.begin() + , end(nodes.end()); i != end; ++i) + { + add_dht_node_name(*i); + } + } +#endif + if (m_alerts.should_post()) m_alerts.emplace_alert(handle); + // if this was an existing torrent, we can't start it again, or add + // another set of plugins etc. we're done + if (!added) return handle; + torrent_ptr->set_ip_filter(m_ip_filter); torrent_ptr->start(params); @@ -4827,18 +4845,6 @@ retry: add_extensions_to_torrent(torrent_ptr, params.userdata); #endif -#ifndef TORRENT_DISABLE_DHT - if (params.ti) - { - torrent_info::nodes_t const& nodes = params.ti->nodes(); - for (std::vector >::const_iterator i = nodes.begin() - , end(nodes.end()); i != end; ++i) - { - add_dht_node_name(*i); - } - } -#endif - #if TORRENT_HAS_BOOST_UNORDERED sha1_hash next_lsd(0); sha1_hash next_dht(0); @@ -4919,7 +4925,8 @@ retry: return handle; } - boost::shared_ptr session_impl::add_torrent_impl( + std::pair, bool> + session_impl::add_torrent_impl( add_torrent_params& params , error_code& ec) { @@ -4934,7 +4941,7 @@ retry: if (string_begins_no_case("magnet:", params.url.c_str())) { parse_magnet_uri(params.url, params, ec); - if (ec) return ptr_t(); + if (ec) return std::make_pair(ptr_t(), false); params.url.clear(); } @@ -4942,7 +4949,7 @@ retry: { std::string filename = resolve_file_url(params.url); boost::shared_ptr t = boost::make_shared(filename, boost::ref(ec), 0); - if (ec) return ptr_t(); + if (ec) return std::make_pair(ptr_t(), false); params.url.clear(); params.ti = t; } @@ -4950,13 +4957,13 @@ retry: if (params.ti && !params.ti->is_valid()) { ec = errors::no_metadata; - return ptr_t(); + return std::make_pair(ptr_t(), false); } if (params.ti && params.ti->is_valid() && params.ti->num_files() == 0) { ec = errors::no_files_in_torrent; - return ptr_t(); + return std::make_pair(ptr_t(), false); } #ifndef TORRENT_DISABLE_DHT @@ -4976,7 +4983,7 @@ retry: if (is_aborted()) { ec = errors::session_is_closing; - return ptr_t(); + return std::make_pair(ptr_t(), false); } // figure out the info hash of the torrent and make sure params.info_hash @@ -5086,11 +5093,11 @@ retry: torrent_ptr->set_url(params.url); if (!params.source_feed_url.empty() && torrent_ptr->source_feed_url().empty()) torrent_ptr->set_source_feed_url(params.source_feed_url); - return torrent_ptr; + return std::make_pair(torrent_ptr, false); } ec = errors::duplicate_torrent; - return ptr_t(); + return std::make_pair(ptr_t(), false); } int queue_pos = ++m_max_queue_pos; @@ -5098,7 +5105,7 @@ retry: torrent_ptr = boost::make_shared(boost::ref(*this) , 16 * 1024, queue_pos, boost::cref(params), boost::cref(params.info_hash)); - return torrent_ptr; + return std::make_pair(torrent_ptr, true); } void session_impl::update_outgoing_interfaces() diff --git a/src/torrent.cpp b/src/torrent.cpp index d6ec09053..5670a0b15 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -282,6 +282,10 @@ namespace libtorrent , m_use_resume_save_path((p.flags & add_torrent_params::flag_use_resume_save_path) != 0) , m_merge_resume_http_seeds((p.flags & add_torrent_params::flag_merge_resume_http_seeds) != 0) , m_stop_when_ready((p.flags & add_torrent_params::flag_stop_when_ready) != 0) +#if TORRENT_USE_ASSERTS + , m_resume_data_loaded(false) + , m_was_started(false) +#endif { // we cannot log in the constructor, because it relies on shared_from_this // being initialized, which happens after the constructor returns. @@ -296,9 +300,6 @@ namespace libtorrent if (!p.resume_data.empty() && (p.flags & add_torrent_params::flag_override_resume_data) == 0) m_need_save_resume_data = false; -#if TORRENT_USE_ASSERTS - m_resume_data_loaded = false; -#endif #if TORRENT_USE_UNC_PATHS m_save_path = canonicalize_path(m_save_path); #endif @@ -718,6 +719,10 @@ namespace libtorrent void torrent::start(add_torrent_params const& p) { TORRENT_ASSERT(is_single_thread()); + TORRENT_ASSERT(m_was_started == false); +#if TORRENT_USE_ASSERTS + m_was_started = true; +#endif #ifndef TORRENT_DISABLE_LOGGING debug_log("creating torrent: %s max-uploads: %d max-connections: %d " diff --git a/test/test_torrent.cpp b/test/test_torrent.cpp index dfc87951b..fd7adb108 100644 --- a/test/test_torrent.cpp +++ b/test/test_torrent.cpp @@ -39,6 +39,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/thread.hpp" #include "libtorrent/torrent.hpp" #include "libtorrent/peer_info.hpp" +#include "libtorrent/extensions.hpp" #include #include #include @@ -304,6 +305,68 @@ TORRENT_TEST(torrent) } } +#ifndef TORRENT_DISABLE_EXTENSIONS +struct test_plugin : libtorrent::torrent_plugin {}; + +struct plugin_creator +{ + plugin_creator(int& c) : m_called(c) {} + + boost::shared_ptr + operator()(torrent_handle const&, void*) + { + ++m_called; + return boost::make_shared(); + } + + int& m_called; +}; + +TORRENT_TEST(duplicate_is_not_error) +{ + file_storage fs; + + fs.add_file("test_torrent_dir2/tmp1", 1024); + libtorrent::create_torrent t(fs, 128 * 1024, 6); + + std::vector piece(128 * 1024); + for (int i = 0; i < int(piece.size()); ++i) + piece[i] = (i % 26) + 'A'; + + // calculate the hash for all pieces + sha1_hash ph = hasher(&piece[0], piece.size()).final(); + int num = t.num_pieces(); + TEST_CHECK(t.num_pieces() > 0); + for (int i = 0; i < num; ++i) + t.set_hash(i, ph); + + std::vector tmp; + std::back_insert_iterator > out(tmp); + bencode(out, t.generate()); + error_code ec; + + int called = 0; + plugin_creator creator(called); + + add_torrent_params p; + p.ti = boost::make_shared(&tmp[0], tmp.size(), boost::ref(ec), 0); + p.flags &= ~add_torrent_params::flag_paused; + p.flags &= ~add_torrent_params::flag_auto_managed; + p.flags &= ~add_torrent_params::flag_duplicate_is_error; + p.save_path = "."; + p.extensions.push_back(creator); + + lt::session ses; + ses.async_add_torrent(p); + ses.async_add_torrent(p); + + wait_for_downloading(ses, "ses"); + + // we should only have added the plugin once + TEST_EQUAL(called, 1); +} +#endif + TORRENT_TEST(torrent_total_size_zero) { file_storage fs; From f56d698db6f1d3aa3657b868dfce1207c20f1b84 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 25 Jun 2016 17:29:40 -0400 Subject: [PATCH 2/2] fix asio-debugging --- include/libtorrent/socks5_stream.hpp | 3 +++ src/session.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/libtorrent/socks5_stream.hpp b/include/libtorrent/socks5_stream.hpp index 40bd08202..5be4ef06d 100644 --- a/include/libtorrent/socks5_stream.hpp +++ b/include/libtorrent/socks5_stream.hpp @@ -120,6 +120,9 @@ public: // to avoid unnecessary copying of the handler, // store it in a shaed_ptr error_code e; +#if defined TORRENT_ASIO_DEBUGGING + add_outstanding_async("socks5_stream::connect1"); +#endif connect1(e, boost::make_shared(handler)); } diff --git a/src/session.cpp b/src/session.cpp index 2278afd16..a0bac8e5f 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -389,7 +389,13 @@ namespace libtorrent int counter = 0; while (log_async()) { - sleep(1000); +#if defined TORRENT_WINDOWS || defined TORRENT_CYGWIN + Sleep(1000); +#elif defined TORRENT_BEOS + snooze_until(system_time() + 1000000, B_SYSTEM_TIMEBASE); +#else + usleep(1000000); +#endif ++counter; printf("\x1b[2J\x1b[0;0H\x1b[33m==== Waiting to shut down: %d ==== \x1b[0m\n\n" , counter);