From 6e88771981b9d394d6914893241b6760b3106ac6 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 13 Feb 2016 13:41:38 -0800 Subject: [PATCH] transition merkle tree support over to the new read_resume_data() --- include/libtorrent/add_torrent_params.hpp | 5 ++++ src/read_resume_data.cpp | 26 +++------------------ src/session_handle.cpp | 2 ++ src/torrent.cpp | 28 +++++++++++++++++++---- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/include/libtorrent/add_torrent_params.hpp b/include/libtorrent/add_torrent_params.hpp index b347c1eff..604790053 100644 --- a/include/libtorrent/add_torrent_params.hpp +++ b/include/libtorrent/add_torrent_params.hpp @@ -468,6 +468,11 @@ namespace libtorrent // precedence. std::vector piece_priorities; + // if this is a merkle tree torrent, and you're seeding, this field must + // be set. It is all the hashes in the binary tree, with the root as the + // first entry. See torrent_info::set_merkle_tree() for more info. + std::vector merkle_tree; + #ifndef TORRENT_NO_DEPRECATE // The optional parameter, ``resume_data`` can be given if up to date // fast-resume data is available. The fast-resume data can be acquired diff --git a/src/read_resume_data.cpp b/src/read_resume_data.cpp index 2e042a80c..ed571b701 100644 --- a/src/read_resume_data.cpp +++ b/src/read_resume_data.cpp @@ -215,29 +215,9 @@ namespace libtorrent bdecode_node mt = rd.dict_find_string("merkle tree"); if (mt) { -#error add field for this - std::vector tree; - tree.resize(m_torrent_file->merkle_tree().size()); - std::memcpy(&tree[0], mt.string_ptr() - , (std::min)(mt.string_length(), int(tree.size()) * 20)); - if (mt.string_length() < int(tree.size()) * 20) - std::memset(&tree[0] + mt.string_length() / 20, 0 - , tree.size() - mt.string_length() / 20); - m_torrent_file->set_merkle_tree(tree); - } - - -#error this is the case where the torrent is a merkle torrent but the resume \ - data does not contain the merkle tree, we need some kind of check in the \ - torrent constructor and error reporting - { - // TODO: 0 if this is a merkle torrent and we can't - // restore the tree, we need to wipe all the - // bits in the have array, but not necessarily - // we might want to do a full check to see if we have - // all the pieces. This is low priority since almost - // no one uses merkle torrents - TORRENT_ASSERT(false); + ret.merkle_tree.resize(mt.string_length() / 20); + std::memcpy(&ret.merkle_tree[0], mt.string_ptr() + , int(ret.merkle_tree.size()) * 20); } // some sanity checking. Maybe we shouldn't be in seed mode anymore diff --git a/src/session_handle.cpp b/src/session_handle.cpp index b4e3f6576..53cef7351 100644 --- a/src/session_handle.cpp +++ b/src/session_handle.cpp @@ -237,6 +237,8 @@ namespace libtorrent atp.verified_pieces.swap(resume_data.verified_pieces); atp.piece_priorities.swap(resume_data.piece_priorities); + atp.merkle_tree.swap(resume_data.merkle_tree); + if ((atp.flags & add_torrent_params::flag_override_resume_data) == 0) { atp.download_limit = resume_data.download_limit; diff --git a/src/torrent.cpp b/src/torrent.cpp index 8c27463b0..2f5fac950 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -283,6 +283,7 @@ namespace libtorrent // being initialized, which happens after the constructor returns. // TODO: 3 we could probably get away with just saving a few fields here + // TODO: 2 p should probably be moved in here m_add_torrent_params.reset(new add_torrent_params(p)); if (m_pinned) @@ -367,7 +368,6 @@ namespace libtorrent e.source = announce_entry::source_magnet_link; e.tier = tier; m_trackers.push_back(e); - m_torrent_file->add_tracker(*i, tier); } std::sort(m_trackers.begin(), m_trackers.end(), boost::bind(&announce_entry::tier, _1) @@ -376,6 +376,29 @@ namespace libtorrent if (settings().get_bool(settings_pack::prefer_udp_trackers)) prioritize_udp_trackers(); + // --- MERKLE TREE --- + + if (m_torrent_file->is_valid() + && m_torrent_file->is_merkle_torrent()) + { + if (p.merkle_tree.size() == m_torrent_file->merkle_tree().size()) + { + // TODO: 2 set_merkle_tree should probably take the vector as && + std::vector tree(p.merkle_tree); + m_torrent_file->set_merkle_tree(tree); + } + else + { + // TODO: 0 if this is a merkle torrent and we can't + // restore the tree, we need to wipe all the + // bits in the have array, but not necessarily + // we might want to do a full check to see if we have + // all the pieces. This is low priority since almost + // no one uses merkle torrents + TORRENT_ASSERT(false); + } + } + if (m_torrent_file->is_valid()) { m_seed_mode = (p.flags & add_torrent_params::flag_seed_mode) != 0; @@ -398,9 +421,6 @@ namespace libtorrent m_verifying.resize(m_torrent_file->num_pieces(), false); } - if (settings().get_bool(settings_pack::prefer_udp_trackers)) - prioritize_udp_trackers(); - m_total_uploaded = p.total_uploaded; m_total_downloaded = p.total_downloaded;