From 2514e7a0e2b33f7f8db8e6de700e3a061c5a4b3d Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 11 Nov 2014 09:08:47 +0000 Subject: [PATCH] fix file_progress --- src/torrent.cpp | 15 +++++++++------ test/test_torrent.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/torrent.cpp b/src/torrent.cpp index aeadd9b9b..008c2a823 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -10883,6 +10883,7 @@ namespace libtorrent int num_files = fs.num_files(); file_progress.resize(num_files, 0); + std::fill(file_progress.begin(), file_progress.end(), 0); // initialize the progress of each file @@ -10894,29 +10895,36 @@ namespace libtorrent { TORRENT_ASSERT(file_index < fs.num_files()); size_type file_offset = off - fs.file_offset(file_index); + TORRENT_ASSERT(file_offset >= 0); while (file_offset >= fs.file_size(file_index)) { ++file_index; + TORRENT_ASSERT(file_index < fs.num_files()); file_offset = off - fs.file_offset(file_index); + TORRENT_ASSERT(file_offset >= 0); } TORRENT_ASSERT(file_offset <= fs.file_size(file_index)); if (!picker.have_piece(piece)) continue; int size = (std::min)(boost::uint64_t(piece_size), total_size - off); + TORRENT_ASSERT(size >= 0); while (size) { int add = (std::min)(boost::int64_t(size), fs.file_size(file_index) - file_offset); + TORRENT_ASSERT(add >= 0); file_progress[file_index] += add; TORRENT_ASSERT(file_progress[file_index] <= fs.file_size(file_index)); size -= add; - if (size >= 0) + TORRENT_ASSERT(size >= 0); + if (size > 0) { ++file_index; + TORRENT_ASSERT(file_index < fs.num_files()); file_offset = 0; } } @@ -10946,11 +10954,6 @@ namespace libtorrent return; } - // we're not a seed and we don't have a picker, that means we donn't - // have any piece yet. - if (!has_picker()) - return; - if (num_have() == 0) { // if we don't have any pieces, just return zeroes diff --git a/test/test_torrent.cpp b/test/test_torrent.cpp index 4df914926..64307bba6 100644 --- a/test/test_torrent.cpp +++ b/test/test_torrent.cpp @@ -245,6 +245,35 @@ int test_main() } } + { + // test the initialize_file_progress function to make sure it assigns + // the correct number of bytes across the files + const int piece_size = 256; + + file_storage fs; + fs.add_file("torrent/1", 100000); + fs.add_file("torrent/2", 10); + fs.set_piece_length(piece_size); + fs.set_num_pieces((fs.total_size() + piece_size - 1) / piece_size); + + for (int idx = 0; idx < fs.num_pieces(); ++idx) + { + piece_picker picker; + picker.init(4, fs.total_size() % 4, fs.num_pieces()); + picker.we_have(idx); + + std::vector fp; + + initialize_file_progress(fp, picker, fs); + + boost::uint64_t sum = 0; + for (int i = 0; i < fp.size(); ++i) + sum += fp[i]; + + TEST_EQUAL(sum, fs.piece_size(idx)); + } + } + return 0; }