2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2012-2022 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 */
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2022-07-27 19:15:06 +00:00
|
|
|
#include <deque>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
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>
|
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "transmission.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "crypto-utils.h" /* tr_sha1() */
|
2022-01-11 14:28:14 +00:00
|
|
|
#include "error.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2021-11-10 02:42:18 +00:00
|
|
|
#include "magnet-metainfo.h"
|
2010-02-06 05:22:27 +00:00
|
|
|
#include "resume.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "torrent-magnet.h"
|
2022-01-11 14:28:14 +00:00
|
|
|
#include "torrent-metainfo.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2009-11-24 02:16:31 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2021-12-27 22:47:25 +00:00
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
/* don't ask for the same metadata piece more than this often */
|
|
|
|
static auto constexpr MinRepeatIntervalSecs = int{ 3 };
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
struct metadata_node
|
|
|
|
{
|
2022-07-27 19:15:06 +00:00
|
|
|
time_t requested_at = 0U;
|
|
|
|
int piece = 0;
|
2009-11-24 02:16:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_incomplete_metadata
|
|
|
|
{
|
2022-07-27 19:15:06 +00:00
|
|
|
std::vector<char> metadata;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/** sorted from least to most recently requested */
|
2022-07-27 19:15:06 +00:00
|
|
|
std::deque<metadata_node> pieces_needed;
|
|
|
|
|
|
|
|
int piece_count = 0;
|
2009-11-24 02:16:31 +00:00
|
|
|
};
|
|
|
|
|
2022-07-27 19:15:06 +00:00
|
|
|
static auto create_all_needed(int n_pieces)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-07-27 19:15:06 +00:00
|
|
|
auto ret = std::deque<metadata_node>{};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-03-29 04:29:35 +00:00
|
|
|
if (tor->hasMetainfo())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tor->incompleteMetadata != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int const n = (size <= 0 || size > INT_MAX) ? -1 : size / METADATA_PIECE_SIZE + (size % METADATA_PIECE_SIZE != 0 ? 1 : 0);
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebugTor(tor, fmt::format("metadata is {} bytes in {} pieces", size, n));
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (n <= 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2022-07-27 19:15:06 +00:00
|
|
|
auto* const m = new tr_incomplete_metadata{};
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (m == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-07 17:12:14 +00:00
|
|
|
|
2022-07-27 19:15:06 +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
|
|
|
|
2022-07-27 19:15:06 +00:00
|
|
|
if (std::empty(m->metadata) || std::empty(m->pieces_needed))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-07-27 19:15:06 +00:00
|
|
|
delete m;
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tor->incompleteMetadata = m;
|
|
|
|
return true;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
void* tr_torrentGetMetadataPiece(tr_torrent const* tor, int piece, size_t* len)
|
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);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(len != nullptr);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-03-29 04:29:35 +00:00
|
|
|
if (!tor->hasMetainfo())
|
2022-01-08 18:53:35 +00:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
auto const fd = tr_sys_file_open(tor->torrentFile(), TR_SYS_FILE_READ, 0);
|
2022-01-08 18:53:35 +00:00
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-01-08 18:53:35 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-01-08 20:05:38 +00:00
|
|
|
auto const info_dict_size = tor->infoDictSize();
|
|
|
|
TR_ASSERT(info_dict_size > 0);
|
2010-06-16 03:05:23 +00:00
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
char* ret = nullptr;
|
2022-07-27 14:03:13 +00:00
|
|
|
if (size_t const o = piece * METADATA_PIECE_SIZE; tr_sys_file_seek(fd, tor->infoDictOffset() + o, TR_SEEK_SET, nullptr))
|
2022-01-08 18:53:35 +00:00
|
|
|
{
|
2022-01-08 20:05:38 +00:00
|
|
|
size_t const l = o + METADATA_PIECE_SIZE <= info_dict_size ? METADATA_PIECE_SIZE : info_dict_size - o;
|
2022-01-08 18:53:35 +00:00
|
|
|
|
|
|
|
if (0 < l && l <= METADATA_PIECE_SIZE)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* buf = tr_new(char, l);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
if (auto n = uint64_t{}; tr_sys_file_read(fd, buf, l, &n) && n == l)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-01-08 18:53:35 +00:00
|
|
|
*len = l;
|
|
|
|
ret = buf;
|
|
|
|
buf = nullptr;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
tr_free(buf);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2015-05-09 08:37:55 +00:00
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
TR_ASSERT(ret == nullptr || *len > 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
static int getPieceLength(struct tr_incomplete_metadata const* m, int piece)
|
|
|
|
{
|
2022-07-27 19:15:06 +00:00
|
|
|
return piece + 1 == m->piece_count ? // last piece
|
|
|
|
std::size(m->metadata) - (piece * METADATA_PIECE_SIZE) :
|
2020-11-04 00:59:19 +00:00
|
|
|
METADATA_PIECE_SIZE;
|
|
|
|
}
|
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
static void tr_buildMetainfoExceptInfoDict(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
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (auto const val = tm.dateCreated(); val != 0)
|
|
|
|
{
|
|
|
|
tr_variantDictAddInt(top, TR_KEY_creation_date, val);
|
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (auto const& announce_list = tm.announceList(); !std::empty(announce_list))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
if (auto const n_webseeds = tm.webseedCount(); n_webseeds > 0)
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:47:57 +00:00
|
|
|
bool tr_torrentUseMetainfoFromFile(
|
|
|
|
tr_torrent* tor,
|
|
|
|
tr_torrent_metainfo const* metainfo,
|
|
|
|
char const* filename_in,
|
|
|
|
tr_error** error)
|
|
|
|
{
|
|
|
|
// add .torrent file
|
|
|
|
if (!tr_sys_path_copy(filename_in, tor->torrentFile(), error))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove .magnet file
|
|
|
|
tr_sys_path_remove(tor->magnetFile());
|
|
|
|
|
|
|
|
// tor should keep this metainfo
|
|
|
|
tor->setMetainfo(*metainfo);
|
|
|
|
|
2022-07-05 23:32:30 +00:00
|
|
|
if (tor->incompleteMetadata != nullptr)
|
2022-06-21 21:47:57 +00:00
|
|
|
{
|
2022-07-27 19:15:06 +00:00
|
|
|
delete tor->incompleteMetadata;
|
2022-06-21 21:47:57 +00:00
|
|
|
tor->incompleteMetadata = nullptr;
|
|
|
|
}
|
|
|
|
tor->isStopping = true;
|
|
|
|
tor->magnetVerify = true;
|
|
|
|
if (tr_sessionGetPaused(tor->session))
|
|
|
|
{
|
|
|
|
tor->startAfterVerify = false;
|
|
|
|
}
|
|
|
|
tor->markEdited();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
static bool useNewMetainfo(tr_torrent* tor, tr_incomplete_metadata const* m, tr_error** error)
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
// test the info_dict checksum
|
2022-07-31 20:58:14 +00:00
|
|
|
if (tr_sha1::digest(m->metadata) != tor->infoHash())
|
2022-01-11 14:28:14 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// checksum passed; now try to parse it as benc
|
|
|
|
auto info_dict_v = tr_variant{};
|
2022-01-17 18:39:50 +00:00
|
|
|
if (!tr_variantFromBuf(
|
|
|
|
&info_dict_v,
|
|
|
|
TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE,
|
2022-07-27 19:15:06 +00:00
|
|
|
{ std::data(m->metadata), std::size(m->metadata) },
|
2022-01-17 18:39:50 +00:00
|
|
|
nullptr,
|
|
|
|
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{};
|
2022-01-15 19:33:57 +00:00
|
|
|
tr_buildMetainfoExceptInfoDict(tor->metainfo_, &top_v);
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_variantMergeDicts(tr_variantDictAddDict(&top_v, TR_KEY_info, 0), &info_dict_v);
|
|
|
|
auto const benc = tr_variantToStr(&top_v, TR_VARIANT_FMT_BENC);
|
|
|
|
tr_variantFree(&top_v);
|
|
|
|
tr_variantFree(&info_dict_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{};
|
|
|
|
if (!metainfo.parseBenc(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
|
2022-03-28 22:13:32 +00:00
|
|
|
if (!tr_saveFile(tor->torrentFile(), 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
|
2022-03-28 22:13:32 +00:00
|
|
|
tr_sys_path_remove(tor->magnetFile());
|
2022-02-24 21:52:29 +00:00
|
|
|
|
2022-01-11 14:28:14 +00:00
|
|
|
// tor should keep this metainfo
|
2022-01-15 19:33:57 +00:00
|
|
|
tor->setMetainfo(metainfo);
|
2022-01-11 14:28:14 +00:00
|
|
|
|
|
|
|
return true;
|
2022-01-09 19:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void onHaveAllMetainfo(tr_torrent* tor, tr_incomplete_metadata* m)
|
|
|
|
{
|
2022-01-11 14:28:14 +00:00
|
|
|
tr_error* error = nullptr;
|
|
|
|
if (useNewMetainfo(tor, m, &error))
|
2022-01-09 19:17:53 +00:00
|
|
|
{
|
2022-07-27 19:15:06 +00:00
|
|
|
delete tor->incompleteMetadata;
|
2022-01-09 19:17:53 +00:00
|
|
|
tor->incompleteMetadata = nullptr;
|
|
|
|
tor->isStopping = true;
|
|
|
|
tor->magnetVerify = true;
|
2022-04-18 06:03:44 +00:00
|
|
|
if (tr_sessionGetPaused(tor->session))
|
|
|
|
{
|
|
|
|
tor->startAfterVerify = false;
|
|
|
|
}
|
2022-01-09 19:17:53 +00:00
|
|
|
tor->markEdited();
|
|
|
|
}
|
|
|
|
else /* drat. */
|
|
|
|
{
|
2022-07-27 19:15:06 +00:00
|
|
|
int 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(
|
|
|
|
ngettext(
|
|
|
|
"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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_torrentSetMetadataPiece(tr_torrent* tor, int piece, void const* data, int len)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
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-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(len >= 0);
|
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?
|
2022-01-09 19:17:53 +00:00
|
|
|
tr_incomplete_metadata* const m = tor->incompleteMetadata;
|
2021-09-15 00:18:09 +00:00
|
|
|
if (m == nullptr)
|
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?
|
|
|
|
if (getPieceLength(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
|
|
|
|
2020-11-04 00:59:19 +00:00
|
|
|
size_t const offset = piece * METADATA_PIECE_SIZE;
|
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
|
|
|
|
2022-07-27 19:15:06 +00:00
|
|
|
// are we done?
|
|
|
|
if (std::empty(needed))
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddDebugTor(tor, fmt::format("metainfo piece {} was the last one", piece));
|
2022-01-09 19:17:53 +00:00
|
|
|
onHaveAllMetainfo(tor, m);
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_torrentGetNextMetadataRequest(tr_torrent* tor, time_t now, int* setme_piece)
|
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
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
bool have_request = false;
|
2022-02-01 17:30:51 +00:00
|
|
|
struct tr_incomplete_metadata* m = tor->incompleteMetadata;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-07-27 19:15:06 +00:00
|
|
|
auto& needed = m->pieces_needed;
|
|
|
|
if (m != nullptr && !std::empty(needed) && needed.front().requested_at + MinRepeatIntervalSecs < now)
|
2009-11-24 02:16:31 +00:00
|
|
|
{
|
2022-07-27 19:15:06 +00:00
|
|
|
auto req = needed.front();
|
|
|
|
needed.pop_front();
|
|
|
|
req.requested_at = now;
|
|
|
|
needed.push_back(req);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2022-07-27 19:15:06 +00:00
|
|
|
tr_logAddDebugTor(tor, fmt::format("next piece to request: {}", req.piece));
|
|
|
|
*setme_piece = req.piece;
|
2017-04-19 12:04:45 +00:00
|
|
|
have_request = true;
|
2009-11-24 02:16:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return have_request;
|
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
|
|
|
{
|
2022-03-29 04:29:35 +00:00
|
|
|
if (tor->hasMetainfo())
|
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
|
|
|
}
|
|
|
|
|
2021-10-21 17:40:36 +00:00
|
|
|
auto const* const m = tor->incompleteMetadata;
|
2022-07-27 19:15:06 +00:00
|
|
|
return m == nullptr || m->piece_count == 0 ? 0.0 : (m->piece_count - std::size(m->pieces_needed)) / (double)m->piece_count;
|
2009-11-24 17:10:40 +00:00
|
|
|
}
|
2009-11-29 08:27:55 +00:00
|
|
|
|
2021-12-14 21:59:44 +00:00
|
|
|
char* tr_torrentGetMagnetLink(tr_torrent const* tor)
|
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
return tr_strvDup(tor->metainfo_.magnet());
|
2021-12-14 21:59:44 +00:00
|
|
|
}
|