2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2012-2023 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>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <climits> /* INT_MAX */
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstdlib>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2022-07-27 19:15:06 +00:00
|
|
|
#include <deque>
|
2022-08-28 17:33:58 +00:00
|
|
|
#include <fstream>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2022-07-27 19:15:06 +00:00
|
|
|
#include <vector>
|
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
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// don't ask for the same metadata piece more than this often
|
|
|
|
auto constexpr MinRepeatIntervalSecs = int{ 3 };
|
|
|
|
|
|
|
|
auto create_all_needed(int n_pieces)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2023-07-15 20:38:38 +00:00
|
|
|
auto ret = std::deque<tr_incomplete_metadata::metadata_node>{};
|
2022-07-27 19:15:06 +00:00
|
|
|
|
|
|
|
ret.resize(n_pieces);
|
|
|
|
|
|
|
|
for (int i = 0; i < n_pieces; ++i)
|
|
|
|
{
|
|
|
|
ret[i].piece = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
2023-04-27 00:40:49 +00:00
|
|
|
|
|
|
|
[[nodiscard]] int div_ceil(int numerator, int denominator)
|
|
|
|
{
|
|
|
|
auto const [quot, rem] = std::div(numerator, denominator);
|
|
|
|
return quot + (rem == 0 ? 0 : 1);
|
|
|
|
}
|
2023-01-07 22:55:00 +00:00
|
|
|
} // namespace
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_torrentSetMetadataSizeHint(tr_torrent* tor, int64_t size)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (tor->has_metainfo())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
if (tor->incomplete_metadata)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2023-07-17 13:56:57 +00:00
|
|
|
int const n = (size <= 0 || size > INT_MAX) ? -1 : div_ceil(size, MetadataPieceSize);
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebugTor(tor, fmt::format("metadata is {} bytes in {} pieces", size, n));
|
2017-04-19 12:04:45 +00:00
|
|
|
if (n <= 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
auto m = tr_incomplete_metadata{};
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
m.piece_count = n;
|
|
|
|
m.metadata.resize(size);
|
|
|
|
m.pieces_needed = create_all_needed(n);
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
if (std::empty(m.metadata) || std::empty(m.pieces_needed))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2016-01-07 17:12:14 +00:00
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
tor->incomplete_metadata = std::move(m);
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 12:36:16 +00:00
|
|
|
bool tr_torrentGetMetadataPiece(tr_torrent const* tor, int piece, tr_metadata_piece& setme)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
|
|
|
TR_ASSERT(piece >= 0);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
if (!tor->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
|
|
|
|
2023-07-17 13:56:57 +00:00
|
|
|
auto const n_pieces = std::max(1, div_ceil(tor->info_dict_size(), MetadataPieceSize));
|
2022-08-27 22:06:20 +00:00
|
|
|
if (piece < 0 || piece >= n_pieces)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto in = std::ifstream{ tor->torrent_file(), std::ios_base::in };
|
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
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const info_dict_size = tor->info_dict_size();
|
2022-01-08 20:05:38 +00:00
|
|
|
TR_ASSERT(info_dict_size > 0);
|
2023-07-17 13:56:57 +00:00
|
|
|
auto const offset_in_info_dict = static_cast<uint64_t>(piece) * MetadataPieceSize;
|
2023-04-23 01:25:55 +00:00
|
|
|
if (auto const offset_in_file = tor->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
|
|
|
|
2023-07-17 13:56:57 +00:00
|
|
|
auto const piece_len = offset_in_info_dict + MetadataPieceSize <= info_dict_size ? MetadataPieceSize :
|
|
|
|
info_dict_size - offset_in_info_dict;
|
2023-07-12 12:36:16 +00:00
|
|
|
setme.resize(piece_len);
|
|
|
|
return !!in.read(reinterpret_cast<char*>(std::data(setme)), std::size(setme));
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
bool tr_torrentUseMetainfoFromFile(
|
|
|
|
tr_torrent* tor,
|
|
|
|
tr_torrent_metainfo const* metainfo,
|
|
|
|
char const* filename_in,
|
|
|
|
tr_error** error)
|
|
|
|
{
|
|
|
|
// add .torrent file
|
2023-04-23 01:25:55 +00:00
|
|
|
if (!tr_sys_path_copy(filename_in, tor->torrent_file(), error))
|
2023-01-07 22:55:00 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove .magnet file
|
2023-04-23 01:25:55 +00:00
|
|
|
tr_sys_path_remove(tor->magnet_file());
|
2023-01-07 22:55:00 +00:00
|
|
|
|
|
|
|
// tor should keep this metainfo
|
2023-04-23 01:25:55 +00:00
|
|
|
tor->set_metainfo(*metainfo);
|
2023-01-07 22:55:00 +00:00
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
tor->incomplete_metadata.reset();
|
2023-01-07 22:55:00 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace set_metadata_piece_helpers
|
|
|
|
{
|
2023-07-15 20:38:38 +00:00
|
|
|
[[nodiscard]] constexpr size_t get_piece_length(tr_incomplete_metadata const& m, int piece)
|
2020-11-04 00:59:19 +00:00
|
|
|
{
|
2023-07-15 20:38:38 +00:00
|
|
|
return piece + 1 == m.piece_count ? // last piece
|
2023-07-17 13:56:57 +00:00
|
|
|
std::size(m.metadata) - (piece * MetadataPieceSize) :
|
|
|
|
MetadataPieceSize;
|
2020-11-04 00:59:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
void build_metainfo_except_info_dict(tr_torrent_metainfo const& tm, tr_variant* top)
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantInitDict(top, 6);
|
|
|
|
|
|
|
|
if (auto const& val = tm.comment(); !std::empty(val))
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantDictAddStr(top, 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
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantDictAddStr(top, 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
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantDictAddStr(top, TR_KEY_created_by, val);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
tr_variantDictAddInt(top, TR_KEY_creation_date, val);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
auto const n = std::size(announce_list);
|
|
|
|
if (n == 1)
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-04-16 20:41:51 +00:00
|
|
|
tr_variantDictAddStr(top, TR_KEY_announce, announce_list.at(0).announce.sv());
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
2022-01-11 14:28:14 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto* const announce_list_variant = tr_variantDictAddList(top, TR_KEY_announce_list, n);
|
|
|
|
tr_variant* tier_variant = nullptr;
|
|
|
|
auto current_tier = std::optional<tr_tracker_tier_t>{};
|
|
|
|
for (auto const& tracker : announce_list)
|
|
|
|
{
|
|
|
|
if (!current_tier || *current_tier != tracker.tier)
|
|
|
|
{
|
|
|
|
tier_variant = tr_variantListAddList(announce_list_variant, n);
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:41:51 +00:00
|
|
|
tr_variantListAddStr(tier_variant, tracker.announce.sv());
|
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 n_webseeds = tm.webseed_count(); n_webseeds > 0)
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
|
|
|
auto* const webseeds_variant = tr_variantDictAddList(top, TR_KEY_url_list, n_webseeds);
|
|
|
|
for (size_t i = 0; i < n_webseeds; ++i)
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantListAddStr(webseeds_variant, tm.webseed(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
bool use_new_metainfo(tr_torrent* tor, tr_error** error)
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
2023-07-15 20:38:38 +00:00
|
|
|
auto const& m = tor->incomplete_metadata;
|
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
|
2023-04-23 01:25:55 +00:00
|
|
|
if (tr_sha1::digest(m->metadata) != tor->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();
|
|
|
|
auto info_dict_v = serde.parse(m->metadata);
|
|
|
|
if (!info_dict_v)
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
tr_error_propagate(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
|
2022-01-11 14:28:14 +00:00
|
|
|
auto top_v = tr_variant{};
|
2023-01-07 22:55:00 +00:00
|
|
|
build_metainfo_except_info_dict(tor->metainfo_, &top_v);
|
2023-08-17 16:02:45 +00:00
|
|
|
tr_variantMergeDicts(tr_variantDictAddDict(&top_v, TR_KEY_info, 0), &*info_dict_v);
|
|
|
|
auto const benc = serde.to_string(top_v);
|
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
|
2023-06-30 14:49:58 +00:00
|
|
|
if (!tr_file_save(tor->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
|
2023-04-23 01:25:55 +00:00
|
|
|
tr_sys_path_remove(tor->magnet_file());
|
2022-02-24 21:52:29 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
// tor should keep this metainfo
|
2023-04-23 01:25:55 +00:00
|
|
|
tor->set_metainfo(metainfo);
|
2022-01-11 14:28:14 +00:00
|
|
|
|
|
|
|
return true;
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
void on_have_all_metainfo(tr_torrent* tor)
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_error* error = nullptr;
|
2023-07-15 20:38:38 +00:00
|
|
|
auto& m = tor->incomplete_metadata;
|
2023-07-29 13:49:52 +00:00
|
|
|
TR_ASSERT(m);
|
2023-07-15 20:38:38 +00:00
|
|
|
if (use_new_metainfo(tor, &error))
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2023-07-15 20:38:38 +00:00
|
|
|
m.reset();
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
else /* drat. */
|
|
|
|
{
|
2023-07-15 20:38:38 +00:00
|
|
|
auto const n = m->piece_count;
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-07-27 19:15:06 +00:00
|
|
|
m->pieces_needed = create_all_needed(n);
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
char const* const msg = error != nullptr && error->message != nullptr ? error->message : "unknown error";
|
2022-03-22 16:45:56 +00:00
|
|
|
tr_logAddWarnTor(
|
|
|
|
tor,
|
|
|
|
fmt::format(
|
2022-12-26 21:13:21 +00:00
|
|
|
tr_ngettext(
|
2022-03-22 16:45:56 +00:00
|
|
|
"Couldn't parse magnet metainfo: '{error}'. Redownloading {piece_count} piece",
|
|
|
|
"Couldn't parse magnet metainfo: '{error}'. Redownloading {piece_count} pieces",
|
|
|
|
n),
|
|
|
|
fmt::arg("error", msg),
|
|
|
|
fmt::arg("piece_count", n)));
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_error_clear(&error);
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-07 22:55:00 +00:00
|
|
|
} // namespace set_metadata_piece_helpers
|
|
|
|
} // namespace
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2023-02-13 18:33:33 +00:00
|
|
|
void tr_torrentMagnetDoIdleWork(tr_torrent* const tor)
|
|
|
|
{
|
|
|
|
using namespace set_metadata_piece_helpers;
|
|
|
|
|
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
if (auto const& m = tor->incomplete_metadata; m && std::empty(m->pieces_needed))
|
2023-02-13 18:33:33 +00:00
|
|
|
{
|
|
|
|
tr_logAddDebugTor(tor, fmt::format("we now have all the metainfo!"));
|
2023-07-15 20:38:38 +00:00
|
|
|
on_have_all_metainfo(tor);
|
2023-02-13 18:33:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 00:12:37 +00:00
|
|
|
void tr_torrentSetMetadataPiece(tr_torrent* tor, int piece, void const* data, size_t len)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2023-01-07 22:55:00 +00:00
|
|
|
using namespace set_metadata_piece_helpers;
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(data != nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebugTor(tor, fmt::format("got metadata piece {} of {} bytes", piece, len));
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// are we set up to download metadata?
|
2023-07-15 20:38:38 +00:00
|
|
|
auto& m = tor->incomplete_metadata;
|
|
|
|
if (!m)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// sanity test: is `piece` in range?
|
2022-07-27 19:15:06 +00:00
|
|
|
if ((piece < 0) || (piece >= m->piece_count))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// sanity test: is `len` the right size?
|
2023-07-15 20:38:38 +00:00
|
|
|
if (get_piece_length(*m, piece) != len)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
// do we need this piece?
|
2022-07-27 19:15:06 +00:00
|
|
|
auto& needed = m->pieces_needed;
|
|
|
|
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
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-07-17 13:56:57 +00:00
|
|
|
size_t const offset = piece * MetadataPieceSize;
|
2022-07-27 19:15:06 +00:00
|
|
|
std::copy_n(reinterpret_cast<char const*>(data), len, std::begin(m->metadata) + offset);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-07-27 19:15:06 +00:00
|
|
|
needed.erase(iter);
|
|
|
|
tr_logAddDebugTor(tor, fmt::format("saving metainfo piece {}... {} remain", piece, std::size(needed)));
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
// ---
|
|
|
|
|
2022-08-14 19:41:57 +00:00
|
|
|
std::optional<int> tr_torrentGetNextMetadataRequest(tr_torrent* tor, time_t now)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
auto& m = tor->incomplete_metadata;
|
|
|
|
if (!m)
|
2022-08-14 19:41:57 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-07-27 19:15:06 +00:00
|
|
|
auto& needed = m->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);
|
|
|
|
tr_logAddDebugTor(tor, fmt::format("next piece to request: {}", req.piece));
|
|
|
|
return req.piece;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
2009-11-24 17:10:40 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
double tr_torrentGetMetadataPercent(tr_torrent const* tor)
|
2009-11-24 17:10:40 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (tor->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
|
|
|
}
|
|
|
|
|
2023-07-29 13:49:52 +00:00
|
|
|
if (auto const& m = tor->incomplete_metadata; m)
|
|
|
|
{
|
|
|
|
if (auto const& n = m->piece_count; n != 0)
|
|
|
|
{
|
|
|
|
return (n - std::size(m->pieces_needed)) / static_cast<double>(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
2009-11-24 17:10:40 +00:00
|
|
|
}
|
2009-11-29 08:27:55 +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
|
|
|
{
|
2022-08-17 00:28:57 +00:00
|
|
|
return std::string{ tor->metainfo_.magnet().sv() };
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|