torrent_info fixes. make_torrent now builds

This commit is contained in:
Arvid Norberg 2008-05-20 09:49:40 +00:00
parent 31ab7f9773
commit dbb625a910
5 changed files with 49 additions and 79 deletions

View File

@ -1210,9 +1210,9 @@ class torrent_info
{ {
public: public:
torrent_info();
torrent_info(sha1_hash const& info_hash); torrent_info(sha1_hash const& info_hash);
torrent_info(entry const& torrent_file); torrent_info(lazy_entry const& torrent_file);
torrent_info(char const* buffer, int size);
torrent_info(char const* filename); torrent_info(char const* filename);
void add_tracker(std::string const& url, int tier = 0); void add_tracker(std::string const& url, int tier = 0);
@ -1256,8 +1256,6 @@ public:
boost::optional<boost::posix_time::ptime> boost::optional<boost::posix_time::ptime>
creation_date() const; creation_date() const;
void print(std::ostream& os) const;
int piece_size(unsigned int index) const; int piece_size(unsigned int index) const;
sha1_hash const& hash_for_piece(unsigned int index) const; sha1_hash const& hash_for_piece(unsigned int index) const;
char const* hash_for_piece_ptr(unsigned int index) const; char const* hash_for_piece_ptr(unsigned int index) const;
@ -1270,26 +1268,22 @@ public:
<h2>torrent_info()</h2> <h2>torrent_info()</h2>
<blockquote> <blockquote>
<pre class="literal-block"> <pre class="literal-block">
torrent_info();
torrent_info(sha1_hash const&amp; info_hash); torrent_info(sha1_hash const&amp; info_hash);
torrent_info(entry const&amp; torrent_file); torrent_info(lazy_entry const&amp; torrent_file);
torrent_info(char const* buffer, int size);
torrent_info(char const* filename); torrent_info(char const* filename);
</pre> </pre>
</blockquote> </blockquote>
<p>The default constructor of <tt class="docutils literal"><span class="pre">torrent_info</span></tt> is used when creating torrent files. It will <p>The constructor that takes an info-hash will initialize the info-hash to the given value,
initialize the object to an empty torrent, containing no files. The info hash will be set but leave all other fields empty. This is used internally when downloading torrents without
to 0 when this constructor is used. To use the empty <tt class="docutils literal"><span class="pre">torrent_info</span></tt> object, add files the metadata. The metadata will be created by libtorrent as soon as it has been downloaded
and piece hashes, announce URLs and optionally a creator tag and comment. To do this you from the swarm.</p>
use the members <tt class="docutils literal"><span class="pre">set_comment()</span></tt>, <tt class="docutils literal"><span class="pre">set_piece_size()</span></tt>, <tt class="docutils literal"><span class="pre">set_creator()</span></tt>, <tt class="docutils literal"><span class="pre">set_hash()</span></tt> <p>The constructor that takes a <tt class="docutils literal"><span class="pre">lazy_entry</span></tt> will create a <tt class="docutils literal"><span class="pre">torrent_info</span></tt> object from the
etc.</p> information found in the given torrent_file. The <tt class="docutils literal"><span class="pre">lazy_entry</span></tt> represents a tree node in
<p>The constructor that takes an info-hash is identical to the default constructor with the an bencoded file. To load an ordinary .torrent file
exception that it will initialize the info-hash to the given value. This is used internally into a <tt class="docutils literal"><span class="pre">lazy_entry</span></tt>, use lazy_bdecode(), see <a class="reference internal" href="#bdecode-bencode">bdecode() bencode()</a>.</p>
when downloading torrents without the metadata. The metadata will be created by libtorrent <p>The version that takes a buffer pointer and a size will decode it as a .torrent file and
as soon as it has been downloaded from the swarm.</p> initialize the torrent_info object for you.</p>
<p>The constructor that takes an entry, is the one that is used in most cases. It will create
a <tt class="docutils literal"><span class="pre">torrent_info</span></tt> object from the information found in the given torrent_file. The
<tt class="docutils literal"><span class="pre">entry</span></tt> represents a tree node in an bencoded file. To load an ordinary .torrent file
into an <tt class="docutils literal"><span class="pre">entry</span></tt>, use bdecode(), see <a class="reference internal" href="#bdecode-bencode">bdecode() bencode()</a>.</p>
<p>The version that takes a filename will simply load the torrent file and decode it inside <p>The version that takes a filename will simply load the torrent file and decode it inside
the constructor, for convenience. This might not be the most suitable for applications that the constructor, for convenience. This might not be the most suitable for applications that
want to be able to report detailed errors on what might go wrong.</p> want to be able to report detailed errors on what might go wrong.</p>
@ -1518,8 +1512,6 @@ piece and <tt class="docutils literal"><span class="pre">info_hash()</span></tt>
torrent file. For more information on the <tt class="docutils literal"><span class="pre">sha1_hash</span></tt>, see the <a class="reference internal" href="#big-number">big_number</a> class. torrent file. For more information on the <tt class="docutils literal"><span class="pre">sha1_hash</span></tt>, see the <a class="reference internal" href="#big-number">big_number</a> class.
<tt class="docutils literal"><span class="pre">hash_for_piece_ptr()</span></tt> returns a pointer to the 20 byte sha1 digest for the piece. <tt class="docutils literal"><span class="pre">hash_for_piece_ptr()</span></tt> returns a pointer to the 20 byte sha1 digest for the piece.
Note that the string is not null-terminated.</p> Note that the string is not null-terminated.</p>
<p><tt class="docutils literal"><span class="pre">info_hash()</span></tt> will only return a valid hash if the torrent_info was read from a
<tt class="docutils literal"><span class="pre">.torrent</span></tt> file or if an <tt class="docutils literal"><span class="pre">entry</span></tt> was created from it (through <tt class="docutils literal"><span class="pre">create_torrent</span></tt>).</p>
</div> </div>
<div class="section" id="name-comment-creation-date-creator"> <div class="section" id="name-comment-creation-date-creator">
<h2>name() comment() creation_date() creator()</h2> <h2>name() comment() creation_date() creator()</h2>

View File

@ -1096,9 +1096,9 @@ The ``torrent_info`` has the following synopsis::
{ {
public: public:
torrent_info();
torrent_info(sha1_hash const& info_hash); torrent_info(sha1_hash const& info_hash);
torrent_info(entry const& torrent_file); torrent_info(lazy_entry const& torrent_file);
torrent_info(char const* buffer, int size);
torrent_info(char const* filename); torrent_info(char const* filename);
void add_tracker(std::string const& url, int tier = 0); void add_tracker(std::string const& url, int tier = 0);
@ -1142,8 +1142,6 @@ The ``torrent_info`` has the following synopsis::
boost::optional<boost::posix_time::ptime> boost::optional<boost::posix_time::ptime>
creation_date() const; creation_date() const;
void print(std::ostream& os) const;
int piece_size(unsigned int index) const; int piece_size(unsigned int index) const;
sha1_hash const& hash_for_piece(unsigned int index) const; sha1_hash const& hash_for_piece(unsigned int index) const;
char const* hash_for_piece_ptr(unsigned int index) const; char const* hash_for_piece_ptr(unsigned int index) const;
@ -1157,27 +1155,23 @@ torrent_info()
:: ::
torrent_info();
torrent_info(sha1_hash const& info_hash); torrent_info(sha1_hash const& info_hash);
torrent_info(entry const& torrent_file); torrent_info(lazy_entry const& torrent_file);
torrent_info(char const* buffer, int size);
torrent_info(char const* filename); torrent_info(char const* filename);
The default constructor of ``torrent_info`` is used when creating torrent files. It will The constructor that takes an info-hash will initialize the info-hash to the given value,
initialize the object to an empty torrent, containing no files. The info hash will be set but leave all other fields empty. This is used internally when downloading torrents without
to 0 when this constructor is used. To use the empty ``torrent_info`` object, add files the metadata. The metadata will be created by libtorrent as soon as it has been downloaded
and piece hashes, announce URLs and optionally a creator tag and comment. To do this you from the swarm.
use the members ``set_comment()``, ``set_piece_size()``, ``set_creator()``, ``set_hash()``
etc.
The constructor that takes an info-hash is identical to the default constructor with the The constructor that takes a ``lazy_entry`` will create a ``torrent_info`` object from the
exception that it will initialize the info-hash to the given value. This is used internally information found in the given torrent_file. The ``lazy_entry`` represents a tree node in
when downloading torrents without the metadata. The metadata will be created by libtorrent an bencoded file. To load an ordinary .torrent file
as soon as it has been downloaded from the swarm. into a ``lazy_entry``, use lazy_bdecode(), see `bdecode() bencode()`_.
The constructor that takes an entry, is the one that is used in most cases. It will create The version that takes a buffer pointer and a size will decode it as a .torrent file and
a ``torrent_info`` object from the information found in the given torrent_file. The initialize the torrent_info object for you.
``entry`` represents a tree node in an bencoded file. To load an ordinary .torrent file
into an ``entry``, use bdecode(), see `bdecode() bencode()`_.
The version that takes a filename will simply load the torrent file and decode it inside The version that takes a filename will simply load the torrent file and decode it inside
the constructor, for convenience. This might not be the most suitable for applications that the constructor, for convenience. This might not be the most suitable for applications that
@ -1438,9 +1432,6 @@ torrent file. For more information on the ``sha1_hash``, see the big_number_ cla
``hash_for_piece_ptr()`` returns a pointer to the 20 byte sha1 digest for the piece. ``hash_for_piece_ptr()`` returns a pointer to the 20 byte sha1 digest for the piece.
Note that the string is not null-terminated. Note that the string is not null-terminated.
``info_hash()`` will only return a valid hash if the torrent_info was read from a
``.torrent`` file or if an ``entry`` was created from it (through ``create_torrent``).
name() comment() creation_date() creator() name() comment() creation_date() creator()
------------------------------------------ ------------------------------------------

View File

@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/storage.hpp" #include "libtorrent/storage.hpp"
#include "libtorrent/hasher.hpp" #include "libtorrent/hasher.hpp"
#include "libtorrent/file_pool.hpp" #include "libtorrent/file_pool.hpp"
#include "libtorrent/create_torrent.hpp"
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
@ -50,10 +51,7 @@ POSSIBILITY OF SUCH DAMAGE.
using namespace boost::filesystem; using namespace boost::filesystem;
using namespace libtorrent; using namespace libtorrent;
void add_files( void add_files(create_torrent& t, path const& p, path const& l)
torrent_info& t
, path const& p
, path const& l)
{ {
if (l.leaf()[0] == '.') return; if (l.leaf()[0] == '.') return;
path f(p / l); path f(p / l);
@ -89,40 +87,44 @@ int main(int argc, char* argv[])
try try
{ {
#endif #endif
boost::intrusive_ptr<torrent_info> t(new torrent_info); create_torrent t;
path full_path = complete(path(argv[3])); path full_path = complete(path(argv[3]));
ofstream out(complete(path(argv[1])), std::ios_base::binary);
int piece_size = 256 * 1024; int piece_size = 256 * 1024;
char const* creator_str = "libtorrent"; char const* creator_str = "libtorrent";
add_files(*t, full_path.branch_path(), full_path.leaf()); add_files(t, full_path.branch_path(), full_path.leaf());
t->set_piece_size(piece_size); t.set_piece_size(piece_size);
t.add_tracker(argv[2]);
std::vector<char> tmp;
bencode(std::back_inserter(tmp), t.generate());
boost::intrusive_ptr<torrent_info> info(new torrent_info(&tmp[0], tmp.size()));
file_pool fp; file_pool fp;
boost::scoped_ptr<storage_interface> st( boost::scoped_ptr<storage_interface> st(
default_storage_constructor(t, full_path.branch_path(), fp)); default_storage_constructor(info, full_path.branch_path(), fp));
t->add_tracker(argv[2]);
// calculate the hash for all pieces // calculate the hash for all pieces
int num = t->num_pieces(); int num = t.num_pieces();
std::vector<char> buf(piece_size); std::vector<char> buf(piece_size);
for (int i = 0; i < num; ++i) for (int i = 0; i < num; ++i)
{ {
st->read(&buf[0], i, 0, t->piece_size(i)); st->read(&buf[0], i, 0, t.piece_size(i));
hasher h(&buf[0], t->piece_size(i)); hasher h(&buf[0], t.piece_size(i));
t->set_hash(i, h.final()); t.set_hash(i, h.final());
std::cerr << (i+1) << "/" << num << "\r"; std::cerr << "\r" << (i+1) << "/" << num;
} }
std::cerr << std::endl;
t->set_creator(creator_str); t.set_creator(creator_str);
if (argc == 5) if (argc == 5)
t->add_url_seed(argv[4]); t.add_url_seed(argv[4]);
// create the torrent and print it to out // create the torrent and print it to out
entry e = t->create_torrent(); ofstream out(complete(path(argv[1])), std::ios_base::binary);
libtorrent::bencode(std::ostream_iterator<char>(out), e); bencode(std::ostream_iterator<char>(out), t.generate());
#ifndef BOOST_NO_EXCEPTIONS #ifndef BOOST_NO_EXCEPTIONS
} }
catch (std::exception& e) catch (std::exception& e)

View File

@ -110,7 +110,6 @@ namespace libtorrent
{ {
public: public:
torrent_info();
torrent_info(sha1_hash const& info_hash); torrent_info(sha1_hash const& info_hash);
torrent_info(lazy_entry const& torrent_file); torrent_info(lazy_entry const& torrent_file);
torrent_info(char const* buffer, int size); torrent_info(char const* buffer, int size);

View File

@ -302,20 +302,6 @@ namespace libtorrent
{ {
} }
torrent_info::torrent_info()
: m_piece_length(0)
, m_total_size(0)
, m_num_pieces(0)
, m_info_hash(0)
, m_name()
, m_creation_date(pt::second_clock::universal_time())
, m_multifile(false)
, m_private(false)
, m_info_section_size(0)
, m_piece_hashes(0)
{
}
torrent_info::torrent_info(char const* filename) torrent_info::torrent_info(char const* filename)
: m_num_pieces(0) : m_num_pieces(0)
, m_creation_date(pt::ptime(pt::not_a_date_time)) , m_creation_date(pt::ptime(pt::not_a_date_time))