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.
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-01-08 23:41:05 +00:00
|
|
|
#include <algorithm>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <cstdint>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstdlib>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2022-08-28 17:33:58 +00:00
|
|
|
#include <fstream>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <ios>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <memory>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <optional>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <string_view>
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <utility> // std::move
|
2010-12-20 02:07:51 +00:00
|
|
|
|
2022-03-15 14:52:16 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/crypto-utils.h" // for tr_sha1()
|
|
|
|
#include "libtransmission/error.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/file.h"
|
|
|
|
#include "libtransmission/quark.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/torrent-magnet.h"
|
|
|
|
#include "libtransmission/torrent-metainfo.h"
|
|
|
|
#include "libtransmission/torrent.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
#include "libtransmission/variant.h"
|
2021-12-27 22:47:25 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
#define tr_logAddDebugMagnet(magnet, msg) tr_logAddDebug(msg, (magnet)->log_name())
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// don't ask for the same metadata piece more than this often
|
2024-01-03 03:04:17 +00:00
|
|
|
auto constexpr MinRepeatIntervalSecs = time_t{ 3 };
|
2023-01-07 22:55:00 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] int64_t div_ceil(int64_t numerator, int64_t denominator)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
auto const [quot, rem] = std::lldiv(numerator, denominator);
|
|
|
|
return quot + (rem == 0 ? 0 : 1);
|
|
|
|
}
|
|
|
|
} // namespace
|
2022-07-27 19:15:06 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
void tr_metadata_download::create_all_needed(int64_t n_pieces) noexcept
|
|
|
|
{
|
|
|
|
pieces_needed_.clear();
|
|
|
|
pieces_needed_.resize(n_pieces);
|
2022-07-27 19:15:06 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
for (int64_t i = 0; i < n_pieces; ++i)
|
2022-07-27 19:15:06 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
pieces_needed_[i].piece = i;
|
2022-07-27 19:15:06 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
2023-04-27 00:40:49 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
tr_metadata_download::tr_metadata_download(std::string_view log_name, int64_t const size)
|
|
|
|
: log_name_{ std::string{ log_name } }
|
2023-04-27 00:40:49 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
TR_ASSERT(is_valid_metadata_size(size));
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
auto const n = div_ceil(size, MetadataPieceSize);
|
|
|
|
tr_logAddDebugMagnet(this, fmt::format("metadata is {} bytes in {} pieces", size, n));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
piece_count_ = n;
|
|
|
|
metadata_.resize(size);
|
|
|
|
create_all_needed(n);
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2024-05-24 20:50:01 +00:00
|
|
|
void tr_torrent::do_magnet_idle_work()
|
|
|
|
{
|
|
|
|
if (auto& m = metadata_download_; m && m->is_complete())
|
|
|
|
{
|
|
|
|
tr_logAddDebugTor(this, fmt::format("we now have all the metainfo!"));
|
|
|
|
on_have_all_metainfo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
void tr_torrent::maybe_start_metadata_transfer(int64_t const size) noexcept
|
|
|
|
{
|
|
|
|
if (has_metainfo() || metadata_download_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
if (!tr_metadata_download::is_valid_metadata_size(size))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
TR_ASSERT(false);
|
|
|
|
return;
|
2016-01-07 17:12:14 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
metadata_download_ = std::make_unique<tr_metadata_download>(name(), size);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] std::optional<tr_metadata_piece> tr_torrent::get_metadata_piece(int64_t const piece) const
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(piece >= 0);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
if (!has_metainfo())
|
2022-01-08 18:53:35 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
return {};
|
2022-01-08 18:53:35 +00:00
|
|
|
}
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
auto const info_dict_size = this->info_dict_size();
|
|
|
|
TR_ASSERT(info_dict_size > 0);
|
|
|
|
if (auto const n_pieces = std::max(int64_t{ 1 }, div_ceil(info_dict_size, MetadataPieceSize));
|
|
|
|
piece < 0 || piece >= n_pieces)
|
2022-08-27 22:06:20 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
auto in = std::ifstream{ torrent_file(), std::ios_base::in | std::ios_base::binary };
|
2022-08-28 17:33:58 +00:00
|
|
|
if (!in.is_open())
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
return {};
|
2022-01-08 18:53:35 +00:00
|
|
|
}
|
2024-01-03 03:04:17 +00:00
|
|
|
auto const offset_in_info_dict = piece * MetadataPieceSize;
|
|
|
|
if (auto const offset_in_file = info_dict_offset() + offset_in_info_dict; !in.seekg(offset_in_file))
|
2022-08-28 17:33:58 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
auto const piece_len = static_cast<size_t>(offset_in_info_dict + MetadataPieceSize) <= info_dict_size ?
|
|
|
|
MetadataPieceSize :
|
|
|
|
info_dict_size - offset_in_info_dict;
|
|
|
|
if (auto ret = tr_metadata_piece(piece_len); in.read(reinterpret_cast<char*>(std::data(ret)), std::size(ret)))
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
bool tr_torrent::use_metainfo_from_file(tr_torrent_metainfo const* metainfo, char const* filename_in, tr_error* error)
|
2023-01-07 22:55:00 +00:00
|
|
|
{
|
|
|
|
// add .torrent file
|
2024-01-03 03:04:17 +00:00
|
|
|
if (!tr_sys_path_copy(filename_in, torrent_file().c_str(), error))
|
2023-01-07 22:55:00 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove .magnet file
|
2024-01-03 03:04:17 +00:00
|
|
|
tr_sys_path_remove(magnet_file());
|
2023-01-07 22:55:00 +00:00
|
|
|
|
|
|
|
// tor should keep this metainfo
|
2024-01-03 03:04:17 +00:00
|
|
|
set_metainfo(*metainfo);
|
2023-01-07 22:55:00 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
metadata_download_.reset();
|
2023-01-07 22:55:00 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace set_metadata_piece_helpers
|
|
|
|
{
|
2023-09-30 12:31:43 +00:00
|
|
|
tr_variant build_metainfo_except_info_dict(tr_torrent_metainfo const& tm)
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2023-09-30 12:31:43 +00:00
|
|
|
auto top = tr_variant::Map{ 8U };
|
2022-01-11 14:28:14 +00:00
|
|
|
|
|
|
|
if (auto const& val = tm.comment(); !std::empty(val))
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2023-09-30 12:31:43 +00:00
|
|
|
top.try_emplace(TR_KEY_comment, val);
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (auto const& val = tm.source(); !std::empty(val))
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2023-09-30 12:31:43 +00:00
|
|
|
top.try_emplace(TR_KEY_source, val);
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (auto const& val = tm.creator(); !std::empty(val))
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2023-09-30 12:31:43 +00:00
|
|
|
top.try_emplace(TR_KEY_created_by, val);
|
2022-01-11 14:28:14 +00:00
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
if (auto const val = tm.date_created(); val != 0)
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
2023-09-30 12:31:43 +00:00
|
|
|
top.try_emplace(TR_KEY_creation_date, val);
|
2022-01-11 14:28:14 +00:00
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
if (auto const& announce_list = tm.announce_list(); !std::empty(announce_list))
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
2023-09-30 12:31:43 +00:00
|
|
|
announce_list.add_to_map(top);
|
2022-01-11 14:28:14 +00:00
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2023-09-30 12:31:43 +00:00
|
|
|
if (auto const n_webseeds = tm.webseed_count(); n_webseeds > 0U)
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
2023-09-30 12:31:43 +00:00
|
|
|
auto webseed_vec = tr_variant::Vector{};
|
|
|
|
webseed_vec.reserve(n_webseeds);
|
|
|
|
for (size_t i = 0U; i < n_webseeds; ++i)
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2023-09-30 12:31:43 +00:00
|
|
|
webseed_vec.emplace_back(tm.webseed(i));
|
2022-01-11 14:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-30 12:31:43 +00:00
|
|
|
|
|
|
|
return tr_variant{ std::move(top) };
|
2022-01-11 14:28:14 +00:00
|
|
|
}
|
2024-01-03 03:04:17 +00:00
|
|
|
} // namespace set_metadata_piece_helpers
|
|
|
|
} // namespace
|
2022-01-11 14:28:14 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] bool tr_torrent::use_new_metainfo(tr_error* error)
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
using namespace set_metadata_piece_helpers;
|
|
|
|
|
|
|
|
auto const& m = metadata_download_;
|
2023-07-29 13:49:52 +00:00
|
|
|
TR_ASSERT(m);
|
2023-07-15 20:38:38 +00:00
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
// test the info_dict checksum
|
2024-01-03 03:04:17 +00:00
|
|
|
if (tr_sha1::digest(m->get_metadata()) != info_hash())
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// checksum passed; now try to parse it as benc
|
2023-08-17 16:02:45 +00:00
|
|
|
auto serde = tr_variant_serde::benc().inplace();
|
2024-01-03 03:04:17 +00:00
|
|
|
auto info_dict_v = serde.parse(m->get_metadata());
|
2023-08-17 16:02:45 +00:00
|
|
|
if (!info_dict_v)
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
if (error != nullptr)
|
|
|
|
{
|
|
|
|
*error = std::move(serde.error_);
|
|
|
|
serde.error_ = {};
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
// yay we have an info dict. Let's make a torrent file
|
2024-01-03 03:04:17 +00:00
|
|
|
auto top_var = build_metainfo_except_info_dict(metainfo());
|
2023-09-30 12:31:43 +00:00
|
|
|
tr_variantMergeDicts(tr_variantDictAddDict(&top_var, TR_KEY_info, 0), &*info_dict_v);
|
|
|
|
auto const benc = serde.to_string(top_var);
|
2022-01-15 19:33:57 +00:00
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
// does this synthetic torrent file parse?
|
2022-01-15 19:33:57 +00:00
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
2023-04-23 01:25:55 +00:00
|
|
|
if (!metainfo.parse_benc(benc))
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
|
|
|
return false;
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
// save it
|
2024-01-03 03:04:17 +00:00
|
|
|
if (!tr_file_save(torrent_file(), benc, error))
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-02-24 21:52:29 +00:00
|
|
|
// remove .magnet file
|
2024-01-03 03:04:17 +00:00
|
|
|
tr_sys_path_remove(magnet_file());
|
2022-02-24 21:52:29 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
// tor should keep this metainfo
|
2024-01-03 03:04:17 +00:00
|
|
|
set_metainfo(metainfo);
|
2022-01-11 14:28:14 +00:00
|
|
|
|
|
|
|
return true;
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
void tr_torrent::on_have_all_metainfo()
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
auto& m = metadata_download_;
|
2023-07-29 13:49:52 +00:00
|
|
|
TR_ASSERT(m);
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto error = tr_error{}; !use_new_metainfo(&error)) /* drat. */
|
2023-12-16 14:47:55 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
auto msg = std::string_view{ error && !std::empty(error.message()) ? error.message() : "unknown error" };
|
2022-03-22 16:45:56 +00:00
|
|
|
tr_logAddWarnTor(
|
2024-01-03 03:04:17 +00:00
|
|
|
this,
|
2023-12-16 14:47:55 +00:00
|
|
|
fmt::format("Couldn't parse magnet metainfo: '{error}'. Redownloading metadata", fmt::arg("error", msg)));
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
2023-12-16 14:47:55 +00:00
|
|
|
|
|
|
|
m.reset();
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 20:50:01 +00:00
|
|
|
void tr_metadata_download::set_metadata_piece(int64_t const piece, void const* const data, size_t const len)
|
2023-02-13 18:33:33 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(data != nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// sanity test: is `piece` in range?
|
2024-01-03 03:04:17 +00:00
|
|
|
if (piece < 0 || piece >= piece_count_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-05-24 20:50:01 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// sanity test: is `len` the right size?
|
2024-01-03 03:04:17 +00:00
|
|
|
if (get_piece_length(piece) != len)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-05-24 20:50:01 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// do we need this piece?
|
2024-01-03 03:04:17 +00:00
|
|
|
auto& needed = pieces_needed_;
|
2022-07-27 19:15:06 +00:00
|
|
|
auto const iter = std::find_if(
|
|
|
|
std::begin(needed),
|
|
|
|
std::end(needed),
|
|
|
|
[piece](auto const& item) { return item.piece == piece; });
|
|
|
|
if (iter == std::end(needed))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2024-05-24 20:50:01 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
auto const offset = piece * MetadataPieceSize;
|
|
|
|
std::copy_n(reinterpret_cast<char const*>(data), len, std::begin(metadata_) + offset);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-07-27 19:15:06 +00:00
|
|
|
needed.erase(iter);
|
2024-01-03 03:04:17 +00:00
|
|
|
tr_logAddDebugMagnet(this, fmt::format("saving metainfo piece {}... {} remain", piece, std::size(needed)));
|
|
|
|
}
|
2023-01-07 22:55:00 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
void tr_torrent::set_metadata_piece(int64_t const piece, void const* const data, size_t const len)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
TR_ASSERT(data != nullptr);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
tr_logAddDebugTor(this, fmt::format("got metadata piece {} of {} bytes", piece, len));
|
|
|
|
|
2024-05-24 20:50:01 +00:00
|
|
|
if (auto& m = metadata_download_)
|
2022-08-14 19:41:57 +00:00
|
|
|
{
|
2024-05-24 20:50:01 +00:00
|
|
|
m->set_metadata_piece(piece, data, len);
|
2022-08-14 19:41:57 +00:00
|
|
|
}
|
2024-01-03 03:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] std::optional<int64_t> tr_metadata_download::get_next_metadata_request(time_t const now) noexcept
|
|
|
|
{
|
|
|
|
auto& needed = pieces_needed_;
|
2022-08-14 19:41:57 +00:00
|
|
|
if (std::empty(needed) || needed.front().requested_at + MinRepeatIntervalSecs >= now)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-08-14 19:41:57 +00:00
|
|
|
return {};
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
auto req = needed.front();
|
|
|
|
needed.pop_front();
|
|
|
|
req.requested_at = now;
|
|
|
|
needed.push_back(req);
|
2024-01-03 03:04:17 +00:00
|
|
|
tr_logAddDebugMagnet(this, fmt::format("next piece to request: {}", req.piece));
|
2022-08-14 19:41:57 +00:00
|
|
|
return req.piece;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
2009-11-24 17:10:40 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] std::optional<int64_t> tr_torrent::get_next_metadata_request(time_t const now) noexcept
|
2009-11-24 17:10:40 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto& m = metadata_download_; m)
|
|
|
|
{
|
|
|
|
return m->get_next_metadata_request(now);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] double tr_metadata_download::get_metadata_percent() const noexcept
|
|
|
|
{
|
|
|
|
if (auto const n = piece_count_; n != 0)
|
|
|
|
{
|
|
|
|
return (n - std::size(pieces_needed_)) / static_cast<double>(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] double tr_torrent::get_metadata_percent() const noexcept
|
|
|
|
{
|
|
|
|
if (has_metainfo())
|
2013-01-07 18:16:34 +00:00
|
|
|
{
|
2021-10-21 17:40:36 +00:00
|
|
|
return 1.0;
|
2013-01-07 18:16:34 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
if (auto const& m = metadata_download_; m)
|
2023-07-29 13:49:52 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
return m->get_metadata_percent();
|
2023-07-29 13:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
2009-11-24 17:10:40 +00:00
|
|
|
}
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
// ---
|
|
|
|
|
2022-08-17 00:28:57 +00:00
|
|
|
std::string tr_torrentGetMagnetLink(tr_torrent const* tor)
|
2021-12-14 21:59:44 +00:00
|
|
|
{
|
2023-11-15 22:24:33 +00:00
|
|
|
return tor->magnet();
|
2022-08-17 00:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t tr_torrentGetMagnetLinkToBuf(tr_torrent const* tor, char* buf, size_t buflen)
|
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
return tr_strv_to_buf(tr_torrentGetMagnetLink(tor), buf, buflen);
|
2021-12-14 21:59:44 +00:00
|
|
|
}
|