From 6d97d562f973cc44abe136b1c7c4d8a6321584b4 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 24 Sep 2008 17:05:12 +0000 Subject: [PATCH] added another constructor to create_torrent that copies a torrent_info. --- include/libtorrent/create_torrent.hpp | 6 ++++ src/create_torrent.cpp | 41 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index 108de8fda..58ec8092e 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -63,11 +63,13 @@ namespace libtorrent { namespace fs = boost::filesystem; namespace pt = boost::posix_time; + class torrent_info; struct TORRENT_EXPORT create_torrent { create_torrent(file_storage& fs, int piece_size); create_torrent(file_storage& fs); + create_torrent(torrent_info const& ti); entry generate() const; file_storage const& files() const { return m_files; } @@ -88,6 +90,10 @@ namespace libtorrent private: file_storage& m_files; + // if m_info_dict is initialized, it is + // used instead of m_files to generate + // the info dictionary + entry m_info_dict; // the urls to the trackers typedef std::pair announce_entry; diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index 67448bb7b..9eb617dad 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -100,6 +100,41 @@ namespace libtorrent (m_files.total_size() + m_files.piece_length() - 1) / m_files.piece_length())); m_piece_hash.resize(m_files.num_pieces()); } + + create_torrent::create_torrent(torrent_info const& ti) + : m_files(const_cast(ti.files())) + , m_creation_date(pt::second_clock::universal_time()) + , m_multifile(ti.num_files() > 1) + , m_private(ti.priv()) + { + TORRENT_ASSERT(ti.is_valid()); + if (ti.creation_date()) m_creation_date = *ti.creation_date(); + + if (!ti.creator().empty()) set_creator(ti.creator().c_str()); + if (!ti.comment().empty()) set_comment(ti.comment().c_str()); + + torrent_info::nodes_t const& nodes = ti.nodes(); + for (torrent_info::nodes_t::const_iterator i = nodes.begin() + , end(nodes.end()); i != end; ++i) + add_node(*i); + + std::vector const& trackers = ti.trackers(); + for (std::vector::const_iterator i = trackers.begin() + , end(trackers.end()); i != end; ++i) + add_tracker(i->url, i->tier); + + std::vector const& web_seeds = ti.url_seeds(); + for (std::vector::const_iterator i = web_seeds.begin() + , end(web_seeds.end()); i != end; ++i) + add_url_seed(*i); + + m_piece_hash.resize(m_files.num_pieces()); + for (int i = 0; i < num_pieces(); ++i) set_hash(i, ti.hash_for_piece(i)); + + m_info_dict = bdecode(&ti.metadata()[0], &ti.metadata()[0] + ti.metadata_size()); + m_info_hash = ti.info_hash(); + } + entry create_torrent::generate() const { TORRENT_ASSERT(m_files.piece_length() > 0); @@ -176,6 +211,12 @@ namespace libtorrent } entry& info = dict["info"]; + if (m_info_dict.type() == entry::dictionary_t) + { + info = m_info_dict; + return dict; + } + info["name"] = m_files.name(); if (m_private) info["private"] = 1;