correct error introduced in add_torrent_impl refactor, with regards t… (#846)

correct error introduced in add_torrent_impl refactor, with regards to not having flag_duplicate_is_error set (which is not set by default)
This commit is contained in:
Arvid Norberg 2016-06-23 13:19:35 -04:00 committed by GitHub
parent 916f8043c1
commit 6917a11c6b
5 changed files with 107 additions and 26 deletions

View File

@ -406,7 +406,10 @@ namespace libtorrent
#endif
torrent_handle add_torrent(add_torrent_params const&, error_code& ec);
boost::shared_ptr<torrent> 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<boost::shared_ptr<torrent>, 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);

View File

@ -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
};

View File

@ -4784,7 +4784,9 @@ retry:
{
// params is updated by add_torrent_impl()
add_torrent_params params = p;
boost::shared_ptr<torrent> const torrent_ptr = add_torrent_impl(params, ec);
boost::shared_ptr<torrent> torrent_ptr;
bool added;
boost::tie(torrent_ptr, added) = add_torrent_impl(params, ec);
torrent_handle const handle(torrent_ptr);
m_alerts.emplace_alert<add_torrent_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<std::pair<std::string, int> >::const_iterator i = nodes.begin()
, end(nodes.end()); i != end; ++i)
{
add_dht_node_name(*i);
}
}
#endif
if (m_alerts.should_post<torrent_added_alert>())
m_alerts.emplace_alert<torrent_added_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<std::pair<std::string, int> >::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<torrent> session_impl::add_torrent_impl(
std::pair<boost::shared_ptr<torrent>, 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<torrent_info> t = boost::make_shared<torrent_info>(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<torrent>(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()

View File

@ -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 "

View File

@ -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 <boost/tuple/tuple.hpp>
#include <boost/make_shared.hpp>
#include <iostream>
@ -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<libtorrent::torrent_plugin>
operator()(torrent_handle const&, void*)
{
++m_called;
return boost::make_shared<test_plugin>();
}
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<char> 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<char> tmp;
std::back_insert_iterator<std::vector<char> > 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<torrent_info>(&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;