2004-10-10 02:42:48 +02:00
|
|
|
/*
|
|
|
|
|
2006-11-14 16:53:38 +01:00
|
|
|
Copyright (c) 2006, Arvid Norberg
|
2004-10-10 02:42:48 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/torrent_info.hpp"
|
|
|
|
#include "libtorrent/file.hpp"
|
|
|
|
#include "libtorrent/storage.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
2006-11-14 16:53:38 +01:00
|
|
|
#include "libtorrent/file_pool.hpp"
|
2004-10-10 02:42:48 +02:00
|
|
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
|
|
|
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
void add_files(
|
|
|
|
torrent_info& t
|
|
|
|
, path const& p
|
|
|
|
, path const& l)
|
|
|
|
{
|
2006-12-21 00:02:41 +01:00
|
|
|
if (l.leaf()[0] == '.') return;
|
2004-10-10 02:42:48 +02:00
|
|
|
path f(p / l);
|
|
|
|
if (is_directory(f))
|
|
|
|
{
|
|
|
|
for (directory_iterator i(f), end; i != end; ++i)
|
|
|
|
add_files(t, p, l / i->leaf());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-05-12 01:03:12 +02:00
|
|
|
std::cerr << "adding \"" << l.string() << "\"\n";
|
2005-11-18 02:12:21 +01:00
|
|
|
t.add_file(l, file_size(f));
|
2004-10-10 02:42:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
path::default_name_check(no_check);
|
|
|
|
|
2007-02-12 06:46:29 +01:00
|
|
|
if (argc != 4 && argc != 5)
|
2004-10-10 02:42:48 +02:00
|
|
|
{
|
2006-04-25 23:04:48 +02:00
|
|
|
std::cerr << "usage: make_torrent <output torrent-file> "
|
2007-02-12 06:46:29 +01:00
|
|
|
"<announce url> <file or directory to create torrent from> "
|
|
|
|
"[url-seed]\n";
|
2004-10-10 02:42:48 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-12-30 02:59:10 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2004-10-10 02:42:48 +02:00
|
|
|
try
|
|
|
|
{
|
2007-12-30 02:59:10 +01:00
|
|
|
#endif
|
2007-09-01 05:00:31 +02:00
|
|
|
boost::intrusive_ptr<torrent_info> t(new torrent_info);
|
2005-09-14 21:33:16 +02:00
|
|
|
path full_path = complete(path(argv[3]));
|
|
|
|
ofstream out(complete(path(argv[1])), std::ios_base::binary);
|
2004-10-10 02:42:48 +02:00
|
|
|
|
|
|
|
int piece_size = 256 * 1024;
|
|
|
|
char const* creator_str = "libtorrent";
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
add_files(*t, full_path.branch_path(), full_path.leaf());
|
|
|
|
t->set_piece_size(piece_size);
|
2004-10-10 02:42:48 +02:00
|
|
|
|
2006-11-14 16:53:38 +01:00
|
|
|
file_pool fp;
|
2007-03-16 06:29:23 +01:00
|
|
|
boost::scoped_ptr<storage_interface> st(
|
|
|
|
default_storage_constructor(t, full_path.branch_path(), fp));
|
2007-09-01 05:00:31 +02:00
|
|
|
t->add_tracker(argv[2]);
|
2004-10-10 02:42:48 +02:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
2007-09-01 05:00:31 +02:00
|
|
|
int num = t->num_pieces();
|
2004-10-10 02:42:48 +02:00
|
|
|
std::vector<char> buf(piece_size);
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
2007-09-01 05:00:31 +02:00
|
|
|
st->read(&buf[0], i, 0, t->piece_size(i));
|
|
|
|
hasher h(&buf[0], t->piece_size(i));
|
|
|
|
t->set_hash(i, h.final());
|
2004-10-10 02:42:48 +02:00
|
|
|
std::cerr << (i+1) << "/" << num << "\r";
|
|
|
|
}
|
|
|
|
|
2007-09-01 05:00:31 +02:00
|
|
|
t->set_creator(creator_str);
|
2004-10-10 02:42:48 +02:00
|
|
|
|
2007-02-12 06:46:29 +01:00
|
|
|
if (argc == 5)
|
2007-09-01 05:00:31 +02:00
|
|
|
t->add_url_seed(argv[4]);
|
2007-02-12 06:46:29 +01:00
|
|
|
|
2004-11-18 23:33:50 +01:00
|
|
|
// create the torrent and print it to out
|
2007-09-01 05:00:31 +02:00
|
|
|
entry e = t->create_torrent();
|
2004-10-10 02:42:48 +02:00
|
|
|
libtorrent::bencode(std::ostream_iterator<char>(out), e);
|
2007-12-30 02:59:10 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2004-10-10 02:42:48 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2005-07-06 02:58:23 +02:00
|
|
|
std::cerr << e.what() << "\n";
|
2004-10-10 02:42:48 +02:00
|
|
|
}
|
2007-12-30 02:59:10 +01:00
|
|
|
#endif
|
2004-10-10 02:42:48 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|