Skip to content

Commit

Permalink
merge RC_1_2 into RC_2_0
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jul 23, 2020
2 parents 6c81bf3 + c7bc15c commit 7471847
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ script:
travis_retry ${B2} -j2 warnings-as-errors=on ${B2_ARGS} test_lsd -l300;
fi &&
if [ "$coverage" == "1" ]; then
codecov --root .. --gcov-exec gcov-5;
codecov --root .. >/dev/null;
fi;
fi'

Expand Down
3 changes: 2 additions & 1 deletion src/add_torrent_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ namespace aux {
// information about which pieces we have.
bool contains_resume_data(add_torrent_params const& atp)
{
return !atp.have_pieces.empty();
return !atp.have_pieces.empty()
|| (atp.flags & torrent_flags::seed_mode);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/session_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3156,6 +3156,8 @@ namespace {

void session_impl::sent_bytes(int bytes_payload, int bytes_protocol)
{
TORRENT_ASSERT(bytes_payload >= 0);
TORRENT_ASSERT(bytes_protocol >= 0);
m_stats_counters.inc_stats_counter(counters::sent_bytes
, bytes_payload + bytes_protocol);
m_stats_counters.inc_stats_counter(counters::sent_payload_bytes
Expand All @@ -3166,6 +3168,8 @@ namespace {

void session_impl::received_bytes(int bytes_payload, int bytes_protocol)
{
TORRENT_ASSERT(bytes_payload >= 0);
TORRENT_ASSERT(bytes_protocol >= 0);
m_stats_counters.inc_stats_counter(counters::recv_bytes
, bytes_payload + bytes_protocol);
m_stats_counters.inc_stats_counter(counters::recv_payload_bytes
Expand All @@ -3176,6 +3180,7 @@ namespace {

void session_impl::trancieve_ip_packet(int bytes, bool ipv6)
{
TORRENT_ASSERT(bytes >= 0);
// one TCP/IP packet header for the packet
// sent or received, and one for the ACK
// The IPv4 header is 20 bytes
Expand Down
83 changes: 52 additions & 31 deletions src/storage_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,32 @@ namespace libtorrent { namespace aux {
}
}

namespace {

std::int64_t get_filesize(stat_cache& stat, file_index_t const file_index
, file_storage const& fs, std::string const& save_path, storage_error& ec)
{
error_code error;
std::int64_t const size = stat.get_filesize(file_index, fs, save_path, error);

if (size >= 0) return size;
if (error != boost::system::errc::no_such_file_or_directory)
{
ec.ec = error;
ec.file(file_index);
ec.operation = operation_t::file_stat;
}
else
{
ec.ec = errors::mismatching_file_size;
ec.file(file_index);
ec.operation = operation_t::file_stat;
}
return -1;
}

}

bool verify_resume_data(add_torrent_params const& rd
, aux::vector<std::string, file_index_t> const& links
, file_storage const& fs
Expand Down Expand Up @@ -514,8 +540,30 @@ namespace libtorrent { namespace aux {
}
#endif // TORRENT_DISABLE_MUTABLE_TORRENTS

bool const seed = rd.have_pieces.all_set()
&& rd.have_pieces.size() >= fs.num_pieces();
bool const seed = (rd.have_pieces.size() >= fs.num_pieces()
&& rd.have_pieces.all_set())
|| (rd.flags & torrent_flags::seed_mode);

if (seed)
{
for (file_index_t const file_index : fs.file_range())
{
if (fs.pad_file_at(file_index)) continue;

std::int64_t const size = get_filesize(stat, file_index, fs
, save_path, ec);
if (size < 0) return false;

if (size != fs.file_size(file_index))
{
ec.ec = errors::mismatching_file_size;
ec.file(file_index);
ec.operation = operation_t::check_resume;
return false;
}
}
return true;
}

// parse have bitmask. Verify that the files we expect to have
// actually do exist
Expand All @@ -536,37 +584,10 @@ namespace libtorrent { namespace aux {
&& file_priority[file_index] == dont_download)
continue;

error_code error;
std::int64_t const size = stat.get_filesize(f[0].file_index
, fs, save_path, error);
if (fs.pad_file_at(file_index)) continue;

if (size < 0)
{
if (error != boost::system::errc::no_such_file_or_directory)
{
ec.ec = error;
ec.file(file_index);
ec.operation = operation_t::file_stat;
return false;
}
else
{
ec.ec = errors::mismatching_file_size;
ec.file(file_index);
ec.operation = operation_t::file_stat;
return false;
}
}

if (seed && size != fs.file_size(file_index))
{
// the resume data indicates we're a seed, but this file has
// the wrong size. Reject the resume data
ec.ec = errors::mismatching_file_size;
ec.file(file_index);
ec.operation = operation_t::check_resume;
if (get_filesize(stat, file_index, fs, save_path, ec) < 0)
return false;
}

// OK, this file existed, good. Now, skip all remaining pieces in
// this file. We're just sanity-checking whether the files exist
Expand Down
57 changes: 31 additions & 26 deletions src/torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1753,7 +1753,6 @@ bool is_downloading_state(int const st)
update_gauge();
}


if (m_seed_mode)
{
m_have_all = true;
Expand Down Expand Up @@ -2041,12 +2040,11 @@ bool is_downloading_state(int const st)
}
#endif

bool should_start_full_check = (status != status_t::no_error)
&& !m_seed_mode;
bool should_start_full_check = (status != status_t::no_error);

// if we got a partial pieces bitfield, it means we were in the middle of
// checking this torrent. pick it up where we left off
if (!should_start_full_check
if (status == status_t::no_error
&& m_add_torrent_params
&& !m_add_torrent_params->have_pieces.empty()
&& m_add_torrent_params->have_pieces.size() < m_torrent_file->num_pieces())
Expand All @@ -2060,18 +2058,30 @@ bool is_downloading_state(int const st)
// that when the resume data check fails. For instance, if the resume data
// is incorrect, but we don't have any files, we skip the check and initialize
// the storage to not have anything.
if (m_seed_mode)
{
m_have_all = true;
update_gauge();
update_state_list();
}
else if (status == status_t::no_error)
if (status == status_t::no_error)
{
// there are either no files for this torrent
// or the resume_data was accepted

if (!error && m_add_torrent_params)
if (m_seed_mode)
{
m_have_all = true;
update_gauge();
update_state_list();

if (!error && m_add_torrent_params)
{
int const num_pieces2 = std::min(m_add_torrent_params->verified_pieces.size()
, torrent_file().num_pieces());
for (piece_index_t i = piece_index_t(0);
i < piece_index_t(num_pieces2); ++i)
{
if (!m_add_torrent_params->verified_pieces[i]) continue;
m_verified.set_bit(i);
}
}
}
else if (!error && m_add_torrent_params)
{
// --- PIECES ---

Expand All @@ -2087,18 +2097,6 @@ bool is_downloading_state(int const st)
we_have(i);
}

if (m_seed_mode)
{
int const num_pieces2 = std::min(m_add_torrent_params->verified_pieces.size()
, torrent_file().num_pieces());
for (piece_index_t i = piece_index_t(0);
i < piece_index_t(num_pieces2); ++i)
{
if (!m_add_torrent_params->verified_pieces[i]) continue;
m_verified.set_bit(i);
}
}

// --- UNFINISHED PIECES ---

int const num_blocks_per_piece = torrent_file().piece_length() / block_size();
Expand Down Expand Up @@ -2140,11 +2138,18 @@ bool is_downloading_state(int const st)
}
}
}

if (should_start_full_check)
else
{
m_seed_mode = false;
// either the fastresume data was rejected or there are
// some files
m_have_all = false;
update_gauge();
update_state_list();
}

if (should_start_full_check)
{
set_state(torrent_status::checking_files);
if (should_check_files()) start_checking();

Expand Down
6 changes: 5 additions & 1 deletion test/setup_transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,12 @@ std::shared_ptr<lt::torrent_info> make_torrent(span<const int> const file_sizes
{
using namespace lt;
file_storage fs = make_file_storage(file_sizes, piece_size);
return make_torrent(fs);
}

lt::create_torrent ct(fs, piece_size);
std::shared_ptr<lt::torrent_info> make_torrent(lt::file_storage& fs)
{
lt::create_torrent ct(fs, fs.piece_length());

for (auto const i : fs.piece_range())
{
Expand Down
1 change: 1 addition & 0 deletions test/setup_transfer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ EXPORT lt::file_storage make_file_storage(lt::span<const int> file_sizes
, int const piece_size, std::string base_name = "test_dir-");
EXPORT std::shared_ptr<lt::torrent_info> make_torrent(lt::span<const int> file_sizes
, int piece_size);
EXPORT std::shared_ptr<lt::torrent_info> make_torrent(lt::file_storage& fs);
EXPORT void create_random_files(std::string const& path, lt::span<const int> file_sizes
, lt::file_storage* fs = nullptr);

Expand Down
35 changes: 25 additions & 10 deletions test/test_fast_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,26 @@ std::shared_ptr<torrent_info> setup_peer(tcp::socket& s, io_context& ioc
, torrent_flags_t const flags = torrent_flags_t{}
, torrent_handle* th = nullptr)
{
std::shared_ptr<torrent_info> t = ::create_torrent();
std::ofstream out_file;
std::ofstream* file = nullptr;
if (flags & torrent_flags::seed_mode)
{
error_code ec;
create_directory("tmp1_fast", ec);
out_file.open(combine_path("tmp1_fast", "temporary").c_str(), std::ios_base::trunc | std::ios_base::binary);
file = &out_file;
}
else
{
error_code ec;
remove(combine_path("tmp1_fast","temporary").c_str(), ec);
if (ec) log("remove(): %s", ec.message().c_str());
}

std::shared_ptr<torrent_info> t = ::create_torrent(file);
out_file.close();
ih = t->info_hashes();

settings_pack sett = settings();
sett.set_str(settings_pack::listen_interfaces, "0.0.0.0:48900");
sett.set_bool(settings_pack::enable_upnp, false);
Expand All @@ -459,7 +477,6 @@ std::shared_ptr<torrent_info> setup_peer(tcp::socket& s, io_context& ioc
#endif
ses.reset(new lt::session(sett));

error_code ec;
add_torrent_params p;
p.flags &= ~torrent_flags::paused;
p.flags &= ~torrent_flags::auto_managed;
Expand All @@ -468,19 +485,17 @@ std::shared_ptr<torrent_info> setup_peer(tcp::socket& s, io_context& ioc
p.info_hash = ih;
else
p.ti = t;
p.save_path = "./tmp1_fast";
p.save_path = "tmp1_fast";

remove("./tmp1_fast/temporary", ec);
if (ec) log("remove(): %s", ec.message().c_str());
ec.clear();
torrent_handle ret = ses->add_torrent(p, ec);
torrent_handle ret = ses->add_torrent(p);
if (th) *th = ret;

// wait for the torrent to be ready
wait_for_downloading(*ses, "ses");

if (incoming)
{
error_code ec;
s.connect(ep("127.0.0.1", ses->listen_port()), ec);
if (ec) TEST_ERROR(ec.message());
}
Expand Down Expand Up @@ -1088,13 +1103,13 @@ void have_all_test(bool const incoming)
TORRENT_TEST(outgoing_have_all)
{
std::cout << "\n === test outgoing have-all ===\n" << std::endl;
have_all_test(true);
have_all_test(false);
}

TORRENT_TEST(incoming_have_all)
{
std::cout << "\n === test outgoing have-all ===\n" << std::endl;
have_all_test(false);
std::cout << "\n === test incoming have-all ===\n" << std::endl;
have_all_test(true);
}

TORRENT_TEST(dht_port_no_support)
Expand Down
6 changes: 6 additions & 0 deletions test/test_flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ void test_add_and_get_flags(torrent_flags_t const flags)
error_code ec;
p.ti = std::make_shared<torrent_info>(file("base.torrent"),
std::ref(ec));
if (flags & torrent_flags::seed_mode)
{
std::ofstream file("temp");
std::vector<char> temp(425);
file.write(temp.data(), std::streamsize(temp.size()));
}
TEST_CHECK(!ec);
p.flags = flags;
const torrent_handle h = ses.add_torrent(p);
Expand Down
Loading

0 comments on commit 7471847

Please sign in to comment.