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"
|
2008-05-20 11:49:40 +02:00
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2004-10-10 02:42:48 +02:00
|
|
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2008-05-28 10:44:40 +02:00
|
|
|
#include <boost/bind.hpp>
|
2004-10-10 02:42:48 +02:00
|
|
|
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
// do not include files and folders whose
|
|
|
|
// name starts with a .
|
|
|
|
bool file_filter(boost::filesystem::path const& filename)
|
2004-10-10 02:42:48 +02:00
|
|
|
{
|
2008-05-28 10:44:40 +02:00
|
|
|
if (filename.leaf()[0] == '.') return false;
|
|
|
|
std::cerr << filename << std::endl;
|
|
|
|
return true;
|
2004-10-10 02:42:48 +02:00
|
|
|
}
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
void print_progress(int i, int num)
|
|
|
|
{
|
|
|
|
std::cerr << "\r" << (i+1) << "/" << num;
|
|
|
|
}
|
2004-10-10 02:42:48 +02:00
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
int piece_size = 256 * 1024;
|
|
|
|
char const* creator_str = "libtorrent";
|
|
|
|
|
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
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage fs;
|
|
|
|
file_pool fp;
|
2005-09-14 21:33:16 +02:00
|
|
|
path full_path = complete(path(argv[3]));
|
2004-10-10 02:42:48 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
add_files(fs, full_path, file_filter);
|
2004-10-10 02:42:48 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
create_torrent t(fs, piece_size);
|
2008-05-20 11:49:40 +02:00
|
|
|
t.add_tracker(argv[2]);
|
2008-05-28 10:44:40 +02:00
|
|
|
set_piece_hashes(t, full_path.branch_path()
|
|
|
|
, boost::bind(&print_progress, _1, t.num_pieces()));
|
2008-05-20 11:49:40 +02:00
|
|
|
std::cerr << std::endl;
|
|
|
|
t.set_creator(creator_str);
|
2004-10-10 02:42:48 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
if (argc == 5) 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
|
2008-05-20 11:49:40 +02:00
|
|
|
ofstream out(complete(path(argv[1])), std::ios_base::binary);
|
|
|
|
bencode(std::ostream_iterator<char>(out), t.generate());
|
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;
|
|
|
|
}
|
|
|
|
|