2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2023-10-19 13:39:34 +00:00
|
|
|
#include <chrono>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2021-11-20 21:20:45 +00:00
|
|
|
#include <mutex>
|
2022-01-31 19:34:04 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <thread>
|
2021-12-29 19:20:09 +00:00
|
|
|
#include <vector>
|
2011-03-16 18:04:23 +00:00
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/completion.h"
|
|
|
|
#include "libtransmission/crypto-utils.h"
|
|
|
|
#include "libtransmission/file.h"
|
|
|
|
#include "libtransmission/verify.h"
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2023-01-12 18:03:14 +00:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
namespace
|
|
|
|
{
|
2023-01-12 18:03:14 +00:00
|
|
|
auto constexpr SleepPerSecondDuringVerify = 100ms;
|
2022-09-06 04:43:59 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
[[nodiscard]] auto current_time_secs()
|
2022-09-06 04:43:59 +00:00
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
return std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::steady_clock::now());
|
2022-09-06 04:43:59 +00:00
|
|
|
}
|
2023-10-19 13:39:34 +00:00
|
|
|
} // namespace
|
2009-09-06 14:05:06 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
void tr_verify_worker::verify_torrent(Mediator& verify_mediator, std::atomic<bool> const& abort_flag)
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
verify_mediator.on_verify_started();
|
2021-12-29 19:20:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_t fd = TR_BAD_SYS_FILE;
|
2023-10-19 13:39:34 +00:00
|
|
|
uint64_t file_pos = 0U;
|
|
|
|
uint32_t piece_pos = 0U;
|
|
|
|
tr_file_index_t file_index = 0U;
|
2022-03-30 18:25:41 +00:00
|
|
|
tr_file_index_t prev_file_index = ~file_index;
|
2023-10-19 13:39:34 +00:00
|
|
|
tr_piece_index_t piece = 0U;
|
|
|
|
auto buffer = std::vector<std::byte>(1024U * 256U);
|
2022-07-31 20:58:14 +00:00
|
|
|
auto sha = tr_sha1::create();
|
2023-10-19 13:39:34 +00:00
|
|
|
auto last_slept_at = current_time_secs();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
auto const& metainfo = verify_mediator.metainfo();
|
|
|
|
while (!abort_flag && piece < metainfo.piece_count())
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
auto const file_length = metainfo.file_size(file_index);
|
2009-04-06 04:02:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* if we're starting a new file... */
|
2022-03-30 18:25:41 +00:00
|
|
|
if (file_pos == 0 && fd == TR_BAD_SYS_FILE && file_index != prev_file_index)
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
auto const found = verify_mediator.find_file(file_index);
|
|
|
|
fd = !found ? TR_BAD_SYS_FILE : tr_sys_file_open(found->c_str(), TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0);
|
2022-03-30 18:25:41 +00:00
|
|
|
prev_file_index = file_index;
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* figure out how much we can read this pass */
|
2023-10-19 13:39:34 +00:00
|
|
|
uint64_t left_in_piece = metainfo.piece_size(piece) - piece_pos;
|
2022-03-30 18:25:41 +00:00
|
|
|
uint64_t left_in_file = file_length - file_pos;
|
|
|
|
uint64_t bytes_this_pass = std::min(left_in_file, left_in_piece);
|
|
|
|
bytes_this_pass = std::min(bytes_this_pass, uint64_t(std::size(buffer)));
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* read a bit */
|
|
|
|
if (fd != TR_BAD_SYS_FILE)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-03-30 18:25:41 +00:00
|
|
|
auto num_read = uint64_t{};
|
|
|
|
if (tr_sys_file_read_at(fd, std::data(buffer), bytes_this_pass, file_pos, &num_read) && num_read > 0)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-03-30 18:25:41 +00:00
|
|
|
bytes_this_pass = num_read;
|
2022-07-31 20:58:14 +00:00
|
|
|
sha->add(std::data(buffer), bytes_this_pass);
|
2022-03-30 18:25:41 +00:00
|
|
|
tr_sys_file_advise(fd, file_pos, bytes_this_pass, TR_SYS_FILE_ADVICE_DONT_NEED);
|
2010-10-19 13:56:58 +00:00
|
|
|
}
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* move our offsets */
|
2022-03-30 18:25:41 +00:00
|
|
|
left_in_piece -= bytes_this_pass;
|
|
|
|
left_in_file -= bytes_this_pass;
|
|
|
|
piece_pos += bytes_this_pass;
|
|
|
|
file_pos += bytes_this_pass;
|
2009-04-06 04:02:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* if we're finishing a piece... */
|
2022-03-30 18:25:41 +00:00
|
|
|
if (left_in_piece == 0)
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
auto const has_piece = sha->finish() == metainfo.piece_hash(piece);
|
|
|
|
verify_mediator.on_piece_checked(piece, has_piece);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* sleeping even just a few msec per second goes a long
|
|
|
|
* way towards reducing IO load... */
|
2023-10-19 13:39:34 +00:00
|
|
|
if (auto const now = current_time_secs(); last_slept_at != now)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-03-30 18:25:41 +00:00
|
|
|
last_slept_at = now;
|
2023-06-29 13:51:39 +00:00
|
|
|
std::this_thread::sleep_for(SleepPerSecondDuringVerify);
|
2009-09-06 14:05:06 +00:00
|
|
|
}
|
2009-08-14 20:55:22 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
sha->clear();
|
2021-11-25 18:26:51 +00:00
|
|
|
++piece;
|
2022-03-30 18:25:41 +00:00
|
|
|
piece_pos = 0;
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* if we're finishing a file... */
|
2022-03-30 18:25:41 +00:00
|
|
|
if (left_in_file == 0)
|
2009-04-06 04:02:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fd != TR_BAD_SYS_FILE)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2017-04-19 12:04:45 +00:00
|
|
|
fd = TR_BAD_SYS_FILE;
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-30 18:25:41 +00:00
|
|
|
++file_index;
|
|
|
|
file_pos = 0;
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* cleanup */
|
|
|
|
if (fd != TR_BAD_SYS_FILE)
|
|
|
|
{
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
verify_mediator.on_verify_done(abort_flag);
|
2009-04-06 04:02:51 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void tr_verify_worker::verify_thread_func()
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
auto const lock = std::lock_guard{ verify_mutex_ };
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2022-11-14 05:11:48 +00:00
|
|
|
if (stop_current_)
|
|
|
|
{
|
|
|
|
stop_current_ = false;
|
|
|
|
stop_current_cv_.notify_one();
|
|
|
|
}
|
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
if (std::empty(todo_))
|
2021-11-20 21:20:45 +00:00
|
|
|
{
|
2022-09-06 04:43:59 +00:00
|
|
|
current_node_.reset();
|
|
|
|
verify_thread_id_.reset();
|
2021-12-29 19:20:09 +00:00
|
|
|
return;
|
2021-11-20 21:20:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
current_node_ = std::move(todo_.extract(std::begin(todo_)).value());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-31 21:58:25 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
verify_torrent(*current_node_->mediator_, stop_current_);
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
void tr_verify_worker::add(std::unique_ptr<Mediator> mediator, tr_priority_t priority)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
auto const lock = std::lock_guard{ verify_mutex_ };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
mediator->on_verify_queued();
|
|
|
|
todo_.emplace(std::move(mediator), priority);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
if (!verify_thread_id_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-05-06 04:11:05 +00:00
|
|
|
auto thread = std::thread(&tr_verify_worker::verify_thread_func, this);
|
2022-09-06 04:43:59 +00:00
|
|
|
verify_thread_id_ = thread.get_id();
|
2022-01-31 19:34:04 +00:00
|
|
|
thread.detach();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
void tr_verify_worker::remove(tr_sha1_digest_t const& info_hash)
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2022-11-14 05:11:48 +00:00
|
|
|
auto lock = std::unique_lock(verify_mutex_);
|
2008-02-15 16:00:46 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
if (current_node_ && current_node_->matches(info_hash))
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2022-09-06 04:43:59 +00:00
|
|
|
stop_current_ = true;
|
2022-11-14 05:11:48 +00:00
|
|
|
stop_current_cv_.wait(lock, [this]() { return !stop_current_; });
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
2023-10-19 13:39:34 +00:00
|
|
|
else if (auto const iter = std::find_if(
|
|
|
|
std::begin(todo_),
|
|
|
|
std::end(todo_),
|
|
|
|
[&info_hash](auto const& node) { return node.matches(info_hash); });
|
|
|
|
iter != std::end(todo_))
|
2008-02-15 16:00:46 +00:00
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
iter->mediator_->on_verify_done(true /*aborted*/);
|
|
|
|
todo_.erase(iter);
|
2008-02-15 16:00:46 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2022-09-06 04:43:59 +00:00
|
|
|
tr_verify_worker::~tr_verify_worker()
|
2009-04-16 13:10:25 +00:00
|
|
|
{
|
2022-09-08 00:24:56 +00:00
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
auto const lock = std::lock_guard{ verify_mutex_ };
|
2022-09-08 00:24:56 +00:00
|
|
|
stop_current_ = true;
|
|
|
|
todo_.clear();
|
|
|
|
}
|
2022-09-06 04:43:59 +00:00
|
|
|
|
|
|
|
while (verify_thread_id_.has_value())
|
|
|
|
{
|
2023-06-29 13:51:39 +00:00
|
|
|
std::this_thread::sleep_for(20ms);
|
2022-09-06 04:43:59 +00:00
|
|
|
}
|
2009-04-16 13:10:25 +00:00
|
|
|
}
|
2023-10-19 13:39:34 +00:00
|
|
|
|
|
|
|
int tr_verify_worker::Node::compare(Node const& that) const noexcept
|
|
|
|
{
|
|
|
|
// prefer higher-priority torrents
|
|
|
|
if (priority_ != that.priority_)
|
|
|
|
{
|
|
|
|
return priority_ > that.priority_ ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefer smaller torrents, since they will verify faster
|
|
|
|
auto const& metainfo = mediator_->metainfo();
|
|
|
|
auto const& that_metainfo = that.mediator_->metainfo();
|
|
|
|
if (metainfo.total_size() != that_metainfo.total_size())
|
|
|
|
{
|
|
|
|
return metainfo.total_size() < that_metainfo.total_size() ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniqueness check
|
|
|
|
auto const& this_hash = metainfo.info_hash();
|
|
|
|
auto const& that_hash = that_metainfo.info_hash();
|
|
|
|
if (this_hash != that_hash)
|
|
|
|
{
|
|
|
|
return this_hash < that_hash ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|