2015-06-29 04:47:11 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2015-2016, Arvid Norberg
|
2015-06-29 04:47:11 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/piece_picker.hpp"
|
|
|
|
#include "libtorrent/file_storage.hpp"
|
|
|
|
#include "libtorrent/alert_manager.hpp"
|
|
|
|
#include "libtorrent/aux_/file_progress.hpp"
|
|
|
|
#include "libtorrent/alert_types.hpp"
|
2016-06-05 20:06:11 +02:00
|
|
|
#include "libtorrent/invariant_check.hpp"
|
2015-06-29 04:47:11 +02:00
|
|
|
|
|
|
|
namespace libtorrent { namespace aux
|
|
|
|
{
|
|
|
|
|
|
|
|
file_progress::file_progress()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_progress::init(piece_picker const& picker, file_storage const& fs)
|
|
|
|
{
|
2016-06-05 20:06:11 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2015-06-29 04:47:11 +02:00
|
|
|
if (!m_file_progress.empty()) return;
|
|
|
|
|
2016-06-05 20:06:11 +02:00
|
|
|
int const num_files = fs.num_files();
|
|
|
|
|
2016-07-02 01:46:59 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2016-12-22 16:42:33 +01:00
|
|
|
int const num_pieces = fs.num_pieces();
|
2016-06-05 20:06:11 +02:00
|
|
|
m_have_pieces.clear();
|
|
|
|
m_have_pieces.resize(num_pieces, false);
|
|
|
|
m_file_sizes.clear();
|
|
|
|
m_file_sizes.reserve(num_files);
|
2016-12-22 16:42:33 +01:00
|
|
|
for (file_index_t i(0); i < fs.end_file(); ++i)
|
2016-06-05 20:06:11 +02:00
|
|
|
m_file_sizes.push_back(fs.file_size(i));
|
|
|
|
#endif
|
2015-06-29 04:47:11 +02:00
|
|
|
|
|
|
|
m_file_progress.resize(num_files, 0);
|
|
|
|
std::fill(m_file_progress.begin(), m_file_progress.end(), 0);
|
|
|
|
|
|
|
|
// initialize the progress of each file
|
|
|
|
|
2016-06-05 20:06:11 +02:00
|
|
|
int const piece_size = fs.piece_length();
|
2016-11-21 07:49:56 +01:00
|
|
|
std::int64_t off = 0;
|
|
|
|
std::int64_t const total_size = fs.total_size();
|
2016-12-22 16:42:33 +01:00
|
|
|
file_index_t file_index(0);
|
|
|
|
for (piece_index_t piece(0); piece < fs.end_piece(); ++piece, off += piece_size)
|
2015-06-29 04:47:11 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT(file_index < fs.end_file());
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t file_offset = off - fs.file_offset(file_index);
|
2015-06-29 04:47:11 +02:00
|
|
|
TORRENT_ASSERT(file_offset >= 0);
|
|
|
|
while (file_offset >= fs.file_size(file_index))
|
|
|
|
{
|
|
|
|
++file_index;
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT(file_index < fs.end_file());
|
2015-06-29 04:47:11 +02:00
|
|
|
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;
|
|
|
|
|
2016-07-02 01:46:59 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2016-06-05 20:06:11 +02:00
|
|
|
m_have_pieces.set_bit(piece);
|
|
|
|
#endif
|
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
TORRENT_ASSERT(total_size >= off);
|
2016-11-21 07:49:56 +01:00
|
|
|
std::int64_t size = (std::min)(std::int64_t(piece_size), total_size - off);
|
2015-06-29 04:47:11 +02:00
|
|
|
TORRENT_ASSERT(size >= 0);
|
|
|
|
|
|
|
|
while (size)
|
|
|
|
{
|
2016-07-06 00:03:06 +02:00
|
|
|
std::int64_t const add = (std::min)(size, fs.file_size(file_index) - file_offset);
|
2015-06-29 04:47:11 +02:00
|
|
|
TORRENT_ASSERT(add >= 0);
|
|
|
|
m_file_progress[file_index] += add;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(m_file_progress[file_index]
|
|
|
|
<= fs.file_size(file_index));
|
|
|
|
|
|
|
|
size -= add;
|
|
|
|
TORRENT_ASSERT(size >= 0);
|
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
++file_index;
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT(file_index < fs.end_file());
|
2015-06-29 04:47:11 +02:00
|
|
|
file_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void file_progress::export_progress(vector<std::int64_t, file_index_t>& fp)
|
2015-06-29 04:47:11 +02:00
|
|
|
{
|
2016-06-05 20:06:11 +02:00
|
|
|
INVARIANT_CHECK;
|
2015-06-29 04:47:11 +02:00
|
|
|
fp.resize(m_file_progress.size(), 0);
|
|
|
|
std::copy(m_file_progress.begin(), m_file_progress.end(), fp.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_progress::clear()
|
|
|
|
{
|
2016-06-05 20:06:11 +02:00
|
|
|
INVARIANT_CHECK;
|
2016-11-21 07:49:56 +01:00
|
|
|
m_file_progress.clear();
|
|
|
|
m_file_progress.shrink_to_fit();
|
2016-07-02 01:46:59 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2016-06-05 20:06:11 +02:00
|
|
|
m_have_pieces.clear();
|
|
|
|
#endif
|
2015-06-29 04:47:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the file progress now that we just completed downloading piece
|
|
|
|
// 'index'
|
2016-12-22 16:42:33 +01:00
|
|
|
void file_progress::update(file_storage const& fs, piece_index_t const index
|
2015-06-29 04:47:11 +02:00
|
|
|
, alert_manager* alerts, torrent_handle const& h)
|
|
|
|
{
|
2016-06-05 20:06:11 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
if (m_file_progress.empty()) return;
|
|
|
|
|
2016-07-02 01:46:59 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2016-06-05 20:06:11 +02:00
|
|
|
// if this assert fires, we've told the file_progress object that we have
|
|
|
|
// a piece twice. That violates its precondition and will cause incorect
|
|
|
|
// accounting
|
|
|
|
TORRENT_ASSERT(m_have_pieces.get_bit(index) == false);
|
|
|
|
#endif
|
2015-06-29 04:47:11 +02:00
|
|
|
|
2016-06-05 20:06:11 +02:00
|
|
|
int const piece_size = fs.piece_length();
|
2016-12-22 16:42:33 +01:00
|
|
|
std::int64_t off = std::int64_t(static_cast<int>(index)) * piece_size;
|
|
|
|
file_index_t file_index = fs.file_index_at_offset(off);
|
2016-12-09 14:23:54 +01:00
|
|
|
std::int64_t size = fs.piece_size(index);
|
2015-06-29 04:47:11 +02:00
|
|
|
for (; size > 0; ++file_index)
|
|
|
|
{
|
2016-12-10 20:15:25 +01:00
|
|
|
std::int64_t const file_offset = off - fs.file_offset(file_index);
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT(file_index != fs.end_file());
|
2015-06-29 04:47:11 +02:00
|
|
|
TORRENT_ASSERT(file_offset <= fs.file_size(file_index));
|
2016-12-22 16:42:33 +01:00
|
|
|
std::int64_t const add = std::min(fs.file_size(file_index)
|
2016-12-12 03:37:07 +01:00
|
|
|
- file_offset, size);
|
2015-06-29 04:47:11 +02:00
|
|
|
m_file_progress[file_index] += add;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(m_file_progress[file_index]
|
|
|
|
<= fs.file_size(file_index));
|
|
|
|
|
|
|
|
// TODO: it would be nice to not depend on alert_manager here
|
|
|
|
if (m_file_progress[file_index] >= fs.file_size(file_index) && alerts)
|
|
|
|
{
|
|
|
|
if (!fs.pad_file_at(file_index))
|
|
|
|
{
|
|
|
|
if (alerts->should_post<file_completed_alert>())
|
|
|
|
{
|
|
|
|
// this file just completed, post alert
|
|
|
|
alerts->emplace_alert<file_completed_alert>(h, file_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size -= add;
|
|
|
|
off += add;
|
|
|
|
TORRENT_ASSERT(size >= 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2016-06-05 20:06:11 +02:00
|
|
|
void file_progress::check_invariant() const
|
2015-06-29 04:47:11 +02:00
|
|
|
{
|
2016-06-05 20:06:11 +02:00
|
|
|
if (m_file_progress.empty()) return;
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
file_index_t index(0);
|
2016-11-21 07:49:56 +01:00
|
|
|
for (std::int64_t progress : m_file_progress)
|
2015-06-29 04:47:11 +02:00
|
|
|
{
|
2016-06-05 20:06:11 +02:00
|
|
|
TORRENT_ASSERT(progress <= m_file_sizes[index++]);
|
2015-06-29 04:47:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} }
|