00001
00002
00003
00004
00005
00006
00007
00008 #include <iostream>
00009 #include <fstream>
00010 #include <iterator>
00011 #include <iomanip>
00012
00013 #include "libtorrent/entry.hpp"
00014 #include "libtorrent/bencode.hpp"
00015 #include "libtorrent/torrent_info.hpp"
00016 #include "libtorrent/file.hpp"
00017 #include "libtorrent/storage.hpp"
00018 #include "libtorrent/hasher.hpp"
00019 #include "libtorrent/file_pool.hpp"
00020
00021 #include <boost/filesystem/operations.hpp>
00022 #include <boost/filesystem/path.hpp>
00023 #include <boost/filesystem/fstream.hpp>
00024
00025 using namespace boost::filesystem;
00026 using namespace libtorrent;
00027
00028 void add_files(
00029 torrent_info& t
00030 , path const& p
00031 , path const& l)
00032 {
00033 if (l.leaf()[0] == '.') return;
00034 path f(p / l);
00035 if (is_directory(f))
00036 {
00037 for (directory_iterator i(f), end; i != end; ++i)
00038 add_files(t, p, l / i->leaf());
00039 }
00040 else
00041 {
00042 std::cerr << "adding \"" << l.string() << "\"\n";
00043 t.add_file(l, file_size(f));
00044 }
00045 }
00046
00047 int main(int argc, char* argv[])
00048 {
00049 using namespace libtorrent;
00050 using namespace boost::filesystem;
00051
00052 path::default_name_check(no_check);
00053
00054 if (argc != 4 && argc != 5)
00055 {
00056 std::cerr << "usage: make_torrent <output torrent-file> "
00057 "<announce url> <file or directory to create torrent from> "
00058 "[url-seed]\n";
00059 return 1;
00060 }
00061
00062 try
00063 {
00064 torrent_info t;
00065 path full_path = complete(path(argv[3]));
00066 ofstream out(complete(path(argv[1])), std::ios_base::binary);
00067
00068 int piece_size = 256 * 1024;
00069 char const* creator_str = "libtorrent";
00070
00071 add_files(t, full_path.branch_path(), full_path.leaf());
00072 t.set_piece_size(piece_size);
00073
00074 file_pool fp;
00075 storage st(t, full_path.branch_path(), fp);
00076 t.add_tracker(argv[2]);
00077
00078
00079 int num = t.num_pieces();
00080 std::vector<char> buf(piece_size);
00081 for (int i = 0; i < num; ++i)
00082 {
00083 st.read(&buf[0], i, 0, t.piece_size(i));
00084 hasher h(&buf[0], t.piece_size(i));
00085 t.set_hash(i, h.final());
00086 std::cerr << (i+1) << "/" << num << "\r";
00087 }
00088
00089 t.set_creator(creator_str);
00090
00091 if (argc == 5)
00092 t.add_url_seed(argv[4]);
00093
00094
00095 entry e = t.create_torrent();
00096 libtorrent::bencode(std::ostream_iterator<char>(out), e);
00097 }
00098 catch (std::exception& e)
00099 {
00100 std::cerr << e.what() << "\n";
00101 }
00102
00103 return 0;
00104 }
00105