back port file progress invariant checking to 1.1

This commit is contained in:
arvidn 2017-01-29 11:00:07 -05:00 committed by Arvid Norberg
parent ffca4afaa6
commit 3339b2abe1
3 changed files with 46 additions and 16 deletions

View File

@ -38,6 +38,11 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/export.hpp" #include "libtorrent/export.hpp"
#if TORRENT_USE_INVARIANT_CHECKS
#include "libtorrent/invariant_check.hpp"
#include "libtorrent/bitfield.hpp"
#endif
namespace libtorrent namespace libtorrent
{ {
class piece_picker; class piece_picker;
@ -62,10 +67,6 @@ namespace aux
void update(file_storage const& fs, int index void update(file_storage const& fs, int index
, alert_manager* alerts, torrent_handle const& h); , alert_manager* alerts, torrent_handle const& h);
#if TORRENT_USE_INVARIANT_CHECKS
void check_invariant(file_storage const& fs) const;
#endif
private: private:
// this vector contains the number of bytes completely // this vector contains the number of bytes completely
@ -74,6 +75,18 @@ namespace aux
// the vector is allocated lazily, when file progress // the vector is allocated lazily, when file progress
// is first queried by the client // is first queried by the client
std::vector<boost::uint64_t> m_file_progress; std::vector<boost::uint64_t> m_file_progress;
#if TORRENT_USE_INVARIANT_CHECKS
friend class libtorrent::invariant_access;
void check_invariant() const;
// this is used to assert we never add the same piece twice
bitfield m_have_pieces;
// to make sure we never say we've downloaded more bytes of a file than
// its file size
std::vector<std::int64_t> m_file_sizes;
#endif
}; };
} } } }

View File

@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/alert_manager.hpp" #include "libtorrent/alert_manager.hpp"
#include "libtorrent/aux_/file_progress.hpp" #include "libtorrent/aux_/file_progress.hpp"
#include "libtorrent/alert_types.hpp" #include "libtorrent/alert_types.hpp"
#include "libtorrent/invariant_check.hpp"
namespace libtorrent { namespace aux namespace libtorrent { namespace aux
{ {
@ -47,8 +48,17 @@ namespace libtorrent { namespace aux
{ {
if (!m_file_progress.empty()) return; if (!m_file_progress.empty()) return;
int num_pieces = fs.num_pieces(); int const num_pieces = fs.num_pieces();
int num_files = fs.num_files(); int const num_files = fs.num_files();
#if TORRENT_USE_INVARIANT_CHECKS
m_have_pieces.clear();
m_have_pieces.resize(num_pieces, false);
m_file_sizes.clear();
m_file_sizes.reserve(num_files);
for (int i = 0; i < int(fs.num_files()); ++i)
m_file_sizes.push_back(fs.file_size(i));
#endif
m_file_progress.resize(num_files, 0); m_file_progress.resize(num_files, 0);
std::fill(m_file_progress.begin(), m_file_progress.end(), 0); std::fill(m_file_progress.begin(), m_file_progress.end(), 0);
@ -75,6 +85,9 @@ namespace libtorrent { namespace aux
if (!picker.have_piece(piece)) continue; if (!picker.have_piece(piece)) continue;
#if TORRENT_USE_INVARIANT_CHECKS
m_have_pieces.set_bit(piece);
#endif
int size = (std::min)(boost::uint64_t(piece_size), total_size - off); int size = (std::min)(boost::uint64_t(piece_size), total_size - off);
TORRENT_ASSERT(size >= 0); TORRENT_ASSERT(size >= 0);
@ -108,6 +121,9 @@ namespace libtorrent { namespace aux
void file_progress::clear() void file_progress::clear()
{ {
std::vector<boost::uint64_t>().swap(m_file_progress); std::vector<boost::uint64_t>().swap(m_file_progress);
#if TORRENT_USE_INVARIANT_CHECKS
m_have_pieces.clear();
#endif
} }
// update the file progress now that we just completed downloading piece // update the file progress now that we just completed downloading piece
@ -115,6 +131,13 @@ namespace libtorrent { namespace aux
void file_progress::update(file_storage const& fs, int index void file_progress::update(file_storage const& fs, int index
, alert_manager* alerts, torrent_handle const& h) , alert_manager* alerts, torrent_handle const& h)
{ {
INVARIANT_CHECK;
#if TORRENT_USE_INVARIANT_CHECKS
TORRENT_ASSERT(m_have_pieces.get_bit(index) == false);
m_have_pieces.set_bit(index);
#endif
if (m_file_progress.empty()) if (m_file_progress.empty())
return; return;
@ -153,16 +176,12 @@ namespace libtorrent { namespace aux
} }
#if TORRENT_USE_INVARIANT_CHECKS #if TORRENT_USE_INVARIANT_CHECKS
void file_progress::check_invariant(file_storage const& fs) const void file_progress::check_invariant() const
{ {
if (!m_file_progress.empty()) if (m_file_progress.empty()) return;
for (int i = 0; i < int(m_file_progress.size()); ++i)
{ {
for (std::vector<boost::uint64_t>::const_iterator i = m_file_progress.begin() TORRENT_ASSERT(m_file_progress[i] <= m_file_sizes[i]);
, end(m_file_progress.end()); i != end; ++i)
{
int index = i - m_file_progress.begin();
TORRENT_ASSERT(*i <= fs.file_size(index));
}
} }
} }
#endif #endif

View File

@ -9209,8 +9209,6 @@ namespace libtorrent
{ {
TORRENT_ASSERT(block_size() > 0); TORRENT_ASSERT(block_size() > 0);
} }
m_file_progress.check_invariant(m_torrent_file->files());
} }
#endif #endif