cleanup examples a bit

This commit is contained in:
arvidn 2017-12-29 21:36:22 +01:00 committed by Arvid Norberg
parent 4565a2f375
commit d427572247
3 changed files with 219 additions and 326 deletions

View File

@ -1,6 +1,6 @@
/*
Copyright (c) 2003, Arvid Norberg
Copyright (c) 2003-2017, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -37,74 +37,25 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/bdecode.hpp"
#include "libtorrent/magnet_uri.hpp"
int load_file(std::string const& filename, std::vector<char>& v
, libtorrent::error_code& ec, int limit = 8000000)
#include <fstream>
std::vector<char> load_file(std::string const& filename)
{
ec.clear();
FILE* f = fopen(filename.c_str(), "rb");
if (f == NULL)
{
ec.assign(errno, boost::system::system_category());
return -1;
}
int r = fseek(f, 0, SEEK_END);
if (r != 0)
{
ec.assign(errno, boost::system::system_category());
fclose(f);
return -1;
}
long s = ftell(f);
if (s < 0)
{
ec.assign(errno, boost::system::system_category());
fclose(f);
return -1;
}
if (s > limit)
{
fclose(f);
return -2;
}
r = fseek(f, 0, SEEK_SET);
if (r != 0)
{
ec.assign(errno, boost::system::system_category());
fclose(f);
return -1;
}
v.resize(s);
if (s == 0)
{
fclose(f);
return 0;
}
r = fread(&v[0], 1, v.size(), f);
if (r < 0)
{
ec.assign(errno, boost::system::system_category());
fclose(f);
return -1;
}
fclose(f);
if (r != s) return -3;
return 0;
std::vector<char> ret;
std::fstream in;
in.exceptions(std::ifstream::failbit);
in.open(filename.c_str(), std::ios_base::in | std::ios_base::binary);
in.seekg(0, std::ios_base::end);
size_t const size = in.tellg();
in.seekg(0, std::ios_base::beg);
ret.resize(size);
in.read(ret.data(), ret.size());
return ret;
}
int main(int argc, char* argv[])
int main(int argc, char* argv[]) try
{
using namespace libtorrent;
if (argc < 2 || argc > 4)
{
if (argc < 2 || argc > 4) {
fputs("usage: dump_torrent torrent-file [total-items-limit] [recursion-limit]\n", stderr);
return 1;
}
@ -115,41 +66,23 @@ int main(int argc, char* argv[])
if (argc > 2) item_limit = atoi(argv[2]);
if (argc > 3) depth_limit = atoi(argv[3]);
std::vector<char> buf;
error_code ec;
int ret = load_file(argv[1], buf, ec, 40 * 1000000);
if (ret == -1)
{
fprintf(stderr, "file too big, aborting\n");
return 1;
}
if (ret != 0)
{
fprintf(stderr, "failed to load file: %s\n", ec.message().c_str());
return 1;
}
bdecode_node e;
std::vector<char> buf = load_file(argv[1]);
lt::bdecode_node e;
int pos = -1;
printf("decoding. recursion limit: %d total item count limit: %d\n"
, depth_limit, item_limit);
ret = bdecode(&buf[0], &buf[0] + buf.size(), e, ec, &pos
lt::error_code ec;
std::cout << "decoding. recursion limit: " << depth_limit
<< " total item count limit: " << item_limit << "\n";
int const ret = lt::bdecode(&buf[0], &buf[0] + buf.size(), e, ec, &pos
, depth_limit, item_limit);
printf("\n\n----- raw info -----\n\n%s\n", print_entry(e).c_str());
if (ret != 0)
{
fprintf(stderr, "failed to decode: '%s' at character: %d\n", ec.message().c_str(), pos);
if (ret != 0) {
std::cerr << "failed to decode: '" << ec.message() << "' at character: " << pos<< "\n";
return 1;
}
torrent_info t(e, ec);
if (ec)
{
fprintf(stderr, "%s\n", ec.message().c_str());
return 1;
}
lt::torrent_info const t(e);
e.clear();
std::vector<char>().swap(buf);
@ -165,14 +98,14 @@ int main(int argc, char* argv[])
printf("%s: %d\n", i->first.c_str(), i->second);
}
puts("trackers:\n");
for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
for (std::vector<lt::announce_entry>::const_iterator i = t.trackers().begin();
i != t.trackers().end(); ++i)
{
printf("%2d: %s\n", i->tier, i->url.c_str());
}
char ih[41];
to_hex((char const*)&t.info_hash()[0], 20, ih);
lt::to_hex(t.info_hash().data(), 20, ih);
printf("number of pieces: %d\n"
"piece length: %d\n"
"info hash: %s\n"
@ -190,27 +123,31 @@ int main(int argc, char* argv[])
, make_magnet_uri(t).c_str()
, t.name().c_str()
, t.num_files());
file_storage const& st = t.files();
lt::file_storage const& st = t.files();
for (int i = 0; i < st.num_files(); ++i)
{
int first = st.map_file(i, 0, 0).piece;
int last = st.map_file(i, (std::max)(boost::int64_t(st.file_size(i))-1, boost::int64_t(0)), 0).piece;
int flags = st.file_flags(i);
int const first = st.map_file(i, 0, 0).piece;
int const last = st.map_file(i, (std::max)(boost::int64_t(st.file_size(i))-1, boost::int64_t(0)), 0).piece;
int const flags = st.file_flags(i);
printf(" %8" PRIx64 " %11" PRId64 " %c%c%c%c [ %5d, %5d ] %7u %s %s %s%s\n"
, st.file_offset(i)
, st.file_size(i)
, ((flags & file_storage::flag_pad_file)?'p':'-')
, ((flags & file_storage::flag_executable)?'x':'-')
, ((flags & file_storage::flag_hidden)?'h':'-')
, ((flags & file_storage::flag_symlink)?'l':'-')
, ((flags & lt::file_storage::flag_pad_file)?'p':'-')
, ((flags & lt::file_storage::flag_executable)?'x':'-')
, ((flags & lt::file_storage::flag_hidden)?'h':'-')
, ((flags & lt::file_storage::flag_symlink)?'l':'-')
, first, last
, boost::uint32_t(st.mtime(i))
, st.hash(i) != sha1_hash(0) ? to_hex(st.hash(i).to_string()).c_str() : ""
, st.hash(i) != lt::sha1_hash(0) ? lt::to_hex(st.hash(i).to_string()).c_str() : ""
, st.file_path(i).c_str()
, (flags & file_storage::flag_symlink) ? "-> " : ""
, (flags & file_storage::flag_symlink) ? st.symlink(i).c_str() : "");
, (flags & lt::file_storage::flag_symlink) ? "-> " : ""
, (flags & lt::file_storage::flag_symlink) ? st.symlink(i).c_str() : "");
}
return 0;
}
catch (std::exception const& e)
{
std::cerr << "ERROR: " << e.what() << "\n";
}

View File

@ -1,6 +1,6 @@
/*
Copyright (c) 2006, Arvid Norberg
Copyright (c) 2006-2017, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -48,8 +48,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <direct.h> // for _getcwd
#endif
using namespace libtorrent;
std::vector<char> load_file(std::string const& filename)
{
std::vector<char> ret;
@ -159,219 +157,188 @@ void print_usage()
, stderr);
}
int main(int argc, char* argv[])
int main(int argc, char* argv[]) try
{
using namespace libtorrent;
std::string creator_str = "libtorrent";
std::string comment_str;
if (argc < 2)
{
if (argc < 2) {
print_usage();
return 1;
}
#ifndef BOOST_NO_EXCEPTIONS
try
{
#endif
std::vector<std::string> web_seeds;
std::vector<std::string> trackers;
std::vector<std::string> collections;
std::vector<sha1_hash> similar;
int pad_file_limit = -1;
int piece_size = 0;
int flags = 0;
std::string root_cert;
std::vector<std::string> web_seeds;
std::vector<std::string> trackers;
std::vector<std::string> collections;
std::vector<lt::sha1_hash> similar;
int pad_file_limit = -1;
int piece_size = 0;
int flags = 0;
std::string root_cert;
std::string outfile;
std::string merklefile;
std::string outfile;
std::string merklefile;
#ifdef TORRENT_WINDOWS
// don't ever write binary data to the console on windows
// it will just be interpreted as text and corrupted
outfile = "a.torrent";
// don't ever write binary data to the console on windows
// it will just be interpreted as text and corrupted
outfile = "a.torrent";
#endif
for (int i = 2; i < argc; ++i)
{
if (argv[i][0] != '-')
{
print_usage();
return 1;
}
std::string full_path = argv[1];
argv += 2;
argc -= 2;
switch (argv[i][1])
{
case 'w':
++i;
web_seeds.push_back(argv[i]);
break;
case 't':
++i;
trackers.push_back(argv[i]);
break;
case 'M':
flags |= create_torrent::mutable_torrent_support;
pad_file_limit = 0x4000;
break;
case 'p':
++i;
pad_file_limit = atoi(argv[i]);
flags |= create_torrent::optimize_alignment;
break;
case 's':
++i;
piece_size = atoi(argv[i]);
break;
case 'm':
++i;
merklefile = argv[i];
flags |= create_torrent::merkle;
break;
case 'o':
++i;
outfile = argv[i];
break;
case 'l':
flags |= create_torrent::symlinks;
break;
case 'C':
++i;
creator_str = argv[i];
break;
case 'c':
++i;
comment_str = argv[i];
break;
case 'r':
++i;
root_cert = argv[i];
break;
case 'S':
{
++i;
if (strlen(argv[i]) != 40)
{
fprintf(stderr, "invalid info-hash for -S. "
"Expected 40 hex characters\n");
print_usage();
return 1;
}
sha1_hash ih;
if (!from_hex(argv[i], 40, (char*)&ih[0]))
{
fprintf(stderr, "invalid info-hash for -S\n");
print_usage();
return 1;
}
similar.push_back(ih);
}
break;
case 'L':
++i;
collections.push_back(argv[i]);
break;
default:
for (; argc > 0; --argc, ++argv) {
if (argv[0][0] != '-') {
print_usage();
return 1;
}
char const flag = argv[0][1];
switch (flag)
{
case 'M':
flags |= lt::create_torrent::mutable_torrent_support;
pad_file_limit = 0x4000;
continue;
case 'l':
flags |= lt::create_torrent::symlinks;
continue;
}
if (argc < 2) {
print_usage();
return 1;
}
switch (flag)
{
case 'w': web_seeds.push_back(argv[1]); break;
case 't': trackers.push_back(argv[1]); break;
case 's': piece_size = atoi(argv[1]); break;
case 'o': outfile = argv[1]; break;
case 'C': creator_str = argv[1]; break;
case 'c': comment_str = argv[1]; break;
case 'r': root_cert = argv[1]; break;
case 'L': collections.push_back(argv[1]); break;
case 'p':
pad_file_limit = atoi(argv[1]);
flags |= lt::create_torrent::optimize_alignment;
break;
case 'm':
merklefile = argv[1];
flags |= lt::create_torrent::merkle;
break;
case 'S':
{
if (strlen(argv[1]) != 40)
{
std::cerr << "invalid info-hash for -S. "
"Expected 40 hex characters\n";
print_usage();
return 1;
}
}
lt::sha1_hash ih;
if (!lt::from_hex(argv[1], 40, ih.data()))
{
std::cerr << "invalid info-hash for -S\n";
print_usage();
return 1;
}
similar.push_back(ih);
}
break;
default:
print_usage();
return 1;
}
file_storage fs;
std::string full_path = argv[1];
#ifdef TORRENT_WINDOWS
if (full_path[1] != ':')
#else
if (full_path[0] != '/')
#endif
{
char cwd[TORRENT_MAX_PATH];
#ifdef TORRENT_WINDOWS
_getcwd(cwd, sizeof(cwd));
full_path = cwd + ("\\" + full_path);
#else
getcwd(cwd, sizeof(cwd));
full_path = cwd + ("/" + full_path);
#endif
}
add_files(fs, full_path, file_filter, flags);
if (fs.num_files() == 0)
{
fputs("no files specified.\n", stderr);
return 1;
}
create_torrent t(fs, piece_size, pad_file_limit, flags);
int tier = 0;
for (std::vector<std::string>::iterator i = trackers.begin()
, end(trackers.end()); i != end; ++i, ++tier)
t.add_tracker(*i, tier);
for (std::vector<std::string>::iterator i = web_seeds.begin()
, end(web_seeds.end()); i != end; ++i)
t.add_url_seed(*i);
for (std::vector<std::string>::iterator i = collections.begin()
, end(collections.end()); i != end; ++i)
t.add_collection(*i);
for (std::vector<sha1_hash>::iterator i = similar.begin()
, end(similar.end()); i != end; ++i)
t.add_similar_torrent(*i);
error_code ec;
set_piece_hashes(t, branch_path(full_path)
, boost::bind(&print_progress, _1, t.num_pieces()), ec);
if (ec)
{
fprintf(stderr, "%s\n", ec.message().c_str());
return 1;
}
fprintf(stderr, "\n");
t.set_creator(creator_str.c_str());
if (!comment_str.empty())
t.set_comment(comment_str.c_str());
if (!root_cert.empty())
{
std::vector<char> pem = load_file(root_cert);
t.set_root_cert(std::string(&pem[0], pem.size()));
}
// create the torrent and print it to stdout
std::vector<char> torrent;
bencode(back_inserter(torrent), t.generate());
if (!outfile.empty())
{
std::fstream out;
out.exceptions(std::ifstream::failbit);
out.open(outfile.c_str(), std::ios_base::out | std::ios_base::binary);
out.write(&torrent[0], torrent.size());
}
else
{
fwrite(&torrent[0], 1, torrent.size(), stdout);
}
if (!merklefile.empty())
{
std::fstream merkle;
merkle.exceptions(std::ifstream::failbit);
merkle.open(merklefile.c_str(), std::ios_base::out | std::ios_base::binary);
merkle.write(reinterpret_cast<char const*>(&t.merkle_tree()[0]), t.merkle_tree().size() * 20);
}
#ifndef BOOST_NO_EXCEPTIONS
++argv;
--argc;
}
catch (std::exception& e)
lt::file_storage fs;
#ifdef TORRENT_WINDOWS
if (full_path[1] != ':')
#else
if (full_path[0] != '/')
#endif
{
fprintf(stderr, "%s\n", e.what());
}
char cwd[TORRENT_MAX_PATH];
#ifdef TORRENT_WINDOWS
_getcwd(cwd, sizeof(cwd));
full_path = cwd + ("\\" + full_path);
#else
getcwd(cwd, sizeof(cwd));
full_path = cwd + ("/" + full_path);
#endif
}
lt::add_files(fs, full_path, file_filter, flags);
if (fs.num_files() == 0) {
std::cerr << "no files specified.\n";
return 1;
}
lt::create_torrent t(fs, piece_size, pad_file_limit, flags);
int tier = 0;
for (std::vector<std::string>::iterator i = trackers.begin()
, end(trackers.end()); i != end; ++i, ++tier)
t.add_tracker(*i, tier);
for (std::vector<std::string>::iterator i = web_seeds.begin()
, end(web_seeds.end()); i != end; ++i)
t.add_url_seed(*i);
for (std::vector<std::string>::iterator i = collections.begin()
, end(collections.end()); i != end; ++i)
t.add_collection(*i);
for (std::vector<lt::sha1_hash>::iterator i = similar.begin()
, end(similar.end()); i != end; ++i)
t.add_similar_torrent(*i);
lt::error_code ec;
set_piece_hashes(t, branch_path(full_path)
, boost::bind(&print_progress, _1, t.num_pieces()), ec);
if (ec) {
std::cerr << ec.message() << "\n";
return 1;
}
fprintf(stderr, "\n");
t.set_creator(creator_str.c_str());
if (!comment_str.empty()) {
t.set_comment(comment_str.c_str());
}
if (!root_cert.empty()) {
std::vector<char> const pem = load_file(root_cert);
t.set_root_cert(std::string(&pem[0], pem.size()));
}
// create the torrent and print it to stdout
std::vector<char> torrent;
lt::bencode(back_inserter(torrent), t.generate());
if (!outfile.empty()) {
std::fstream out;
out.exceptions(std::ifstream::failbit);
out.open(outfile.c_str(), std::ios_base::out | std::ios_base::binary);
out.write(&torrent[0], torrent.size());
}
else {
std::cout.write(&torrent[0], torrent.size());
}
if (!merklefile.empty()) {
std::fstream merkle;
merkle.exceptions(std::ifstream::failbit);
merkle.open(merklefile.c_str(), std::ios_base::out | std::ios_base::binary);
merkle.write(reinterpret_cast<char const*>(&t.merkle_tree()[0]), t.merkle_tree().size() * 20);
}
return 0;
}
catch (std::exception& e) {
std::cerr << "ERROR: " << e.what() << "\n";
return 1;
}

View File

@ -1,6 +1,6 @@
/*
Copyright (c) 2003, Arvid Norberg
Copyright (c) 2003-2017, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -37,40 +37,29 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/session.hpp"
#include "libtorrent/torrent_info.hpp"
int main(int argc, char* argv[])
int main(int argc, char* argv[]) try
{
using namespace libtorrent;
namespace lt = libtorrent;
if (argc != 2)
{
fputs("usage: ./simple_client torrent-file\n"
"to stop the client, press return.\n", stderr);
std::cerr << "usage: ./simple_client torrent-file\n"
"to stop the client, press return.\n";
return 1;
}
settings_pack sett;
sett.set_str(settings_pack::listen_interfaces, "0.0.0.0:6881");
lt::session s(sett);
add_torrent_params p;
lt::session s;
lt::add_torrent_params p;
p.save_path = "./";
error_code ec;
p.ti = boost::make_shared<torrent_info>(std::string(argv[1]), boost::ref(ec), 0);
if (ec)
{
fprintf(stderr, "%s\n", ec.message().c_str());
return 1;
}
s.add_torrent(p, ec);
if (ec)
{
fprintf(stderr, "%s\n", ec.message().c_str());
return 1;
}
lt::error_code ec;
p.ti = boost::make_shared<lt::torrent_info>(std::string(argv[1]), 0);
s.add_torrent(p);
// wait for the user to end
char a;
scanf("%c\n", &a);
return 0;
}
catch (std::exception const& e)
{
std::cerr << "ERROR: " << e.what() << "\n";
}