make throwing versions of read_resume_data

This commit is contained in:
arvidn 2018-10-07 02:44:17 +02:00 committed by Arvid Norberg
parent e612fee995
commit 2d99e994c3
6 changed files with 30 additions and 29 deletions

View File

@ -85,12 +85,7 @@ int main(int argc, char const* argv[]) try
std::vector<char> buf{std::istream_iterator<char>(ifs) std::vector<char> buf{std::istream_iterator<char>(ifs)
, std::istream_iterator<char>()}; , std::istream_iterator<char>()};
lt::error_code ec; lt::add_torrent_params atp = lt::read_resume_data(buf);
lt::add_torrent_params atp = lt::read_resume_data(buf, ec);
if (ec) {
std::cerr << "failed to read resume data: " << ec.message() << std::endl;
return 1;
}
lt::add_torrent_params magnet = lt::parse_magnet_uri(argv[1]); lt::add_torrent_params magnet = lt::parse_magnet_uri(argv[1]);
if (atp.info_hash != magnet.info_hash) { if (atp.info_hash != magnet.info_hash) {
atp = std::move(magnet); atp = std::move(magnet);

View File

@ -52,6 +52,8 @@ namespace libtorrent {
, error_code& ec); , error_code& ec);
TORRENT_EXPORT add_torrent_params read_resume_data(span<char const> buffer TORRENT_EXPORT add_torrent_params read_resume_data(span<char const> buffer
, error_code& ec); , error_code& ec);
TORRENT_EXPORT add_torrent_params read_resume_data(bdecode_node const& rd);
TORRENT_EXPORT add_torrent_params read_resume_data(span<char const> buffer);
} }
#endif #endif

View File

@ -354,4 +354,23 @@ namespace {
return read_resume_data(rd, ec); return read_resume_data(rd, ec);
} }
add_torrent_params read_resume_data(bdecode_node const& rd)
{
error_code ec;
auto ret = read_resume_data(rd, ec);
if (ec) throw system_error(ec);
return ret;
}
add_torrent_params read_resume_data(span<char const> buffer)
{
error_code ec;
bdecode_node rd = bdecode(buffer, ec);
if (ec) throw system_error(ec);
auto ret = read_resume_data(rd, ec);
if (ec) throw system_error(ec);
return ret;
}
} }

View File

@ -77,10 +77,7 @@ TORRENT_TEST(read_resume)
std::vector<char> resume_data; std::vector<char> resume_data;
bencode(std::back_inserter(resume_data), rd); bencode(std::back_inserter(resume_data), rd);
error_code ec; add_torrent_params atp = read_resume_data(resume_data);
add_torrent_params atp = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
TEST_EQUAL(atp.info_hash, sha1_hash("abcdefghijklmnopqrst")); TEST_EQUAL(atp.info_hash, sha1_hash("abcdefghijklmnopqrst"));
TEST_EQUAL(atp.have_pieces.size(), 6); TEST_EQUAL(atp.have_pieces.size(), 6);
@ -165,9 +162,7 @@ TORRENT_TEST(read_resume_mismatching_torrent)
// the info-hash field does not match the torrent in the "info" field, so it // the info-hash field does not match the torrent in the "info" field, so it
// will be ignored // will be ignored
error_code ec; add_torrent_params atp = read_resume_data(resume_data);
add_torrent_params atp = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
TEST_CHECK(!atp.ti); TEST_CHECK(!atp.ti);
} }
@ -213,9 +208,7 @@ TORRENT_TEST(read_resume_torrent)
// the info-hash field does not match the torrent in the "info" field, so it // the info-hash field does not match the torrent in the "info" field, so it
// will be ignored // will be ignored
error_code ec; add_torrent_params atp = read_resume_data(resume_data);
add_torrent_params atp = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
TEST_CHECK(atp.ti); TEST_CHECK(atp.ti);
TEST_EQUAL(atp.ti->info_hash(), ti->info_hash()); TEST_EQUAL(atp.ti->info_hash(), ti->info_hash());

View File

@ -150,9 +150,7 @@ torrent_handle test_resume_flags(lt::session& ses
else else
#endif #endif
{ {
error_code ec; p = read_resume_data(rd);
p = read_resume_data(rd, ec);
TEST_CHECK(!ec);
} }
p.ti = ti; p.ti = ti;
@ -243,9 +241,7 @@ void test_piece_priorities(bool test_deprecated = false)
else else
#endif #endif
{ {
error_code ec; p = read_resume_data(resume_data);
p = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
p.ti = ti; p.ti = ti;
p.save_path = "."; p.save_path = ".";
} }
@ -851,9 +847,7 @@ void test_zero_file_prio(bool test_deprecated = false, bool mix_prios = false)
else else
#endif #endif
{ {
error_code ec; p = read_resume_data(resume_data);
p = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
p.ti = ti; p.ti = ti;
p.save_path = "."; p.save_path = ".";
} }

View File

@ -747,8 +747,7 @@ void test_fastresume(bool const test_deprecated)
else else
#endif #endif
{ {
p = read_resume_data(resume_data, ec); p = read_resume_data(resume_data);
TEST_CHECK(!ec);
} }
p.flags &= ~torrent_flags::paused; p.flags &= ~torrent_flags::paused;
@ -928,8 +927,7 @@ void test_rename_file_fastresume(bool test_deprecated)
else else
#endif #endif
{ {
p = read_resume_data(resume_data, ec); p = read_resume_data(resume_data);
TEST_CHECK(!ec);
} }
p.ti = std::make_shared<torrent_info>(std::cref(*t)); p.ti = std::make_shared<torrent_info>(std::cref(*t));
p.save_path = combine_path(test_path, "tmp2"); p.save_path = combine_path(test_path, "tmp2");