2009-01-07 06:53:29 +00:00
|
|
|
/*
|
2021-10-29 18:24:30 +00:00
|
|
|
* This file Copyright (C) 2009-2021 Mnemosyne LLC
|
2007-12-25 05:37:32 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2007-12-25 05:37:32 +00:00
|
|
|
*
|
2009-01-07 06:53:29 +00:00
|
|
|
*/
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2021-10-22 02:40:55 +00:00
|
|
|
#include <optional>
|
2021-09-29 19:52:19 +00:00
|
|
|
#include <string>
|
2021-10-20 02:30:50 +00:00
|
|
|
#include <string_view>
|
2021-09-29 19:52:19 +00:00
|
|
|
#include <unordered_set>
|
2021-10-16 14:04:19 +00:00
|
|
|
#include <vector>
|
2021-09-29 19:52:19 +00:00
|
|
|
|
2021-11-24 14:48:52 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
#include "announce-list.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "bandwidth.h"
|
2021-10-29 18:24:30 +00:00
|
|
|
#include "bitfield.h"
|
2021-11-24 14:48:52 +00:00
|
|
|
#include "block-info.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "completion.h"
|
2021-10-31 16:38:10 +00:00
|
|
|
#include "file.h"
|
2021-11-29 01:12:54 +00:00
|
|
|
#include "file-piece-map.h"
|
2021-11-02 23:00:01 +00:00
|
|
|
#include "quark.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "session.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "tr-macros.h"
|
2009-01-02 17:01:55 +00:00
|
|
|
|
2021-10-10 00:13:40 +00:00
|
|
|
class tr_swarm;
|
2021-12-15 21:25:42 +00:00
|
|
|
struct tr_error;
|
2021-11-09 03:30:03 +00:00
|
|
|
struct tr_magnet_info;
|
2021-11-12 16:42:51 +00:00
|
|
|
struct tr_metainfo_parsed;
|
2021-11-09 03:30:03 +00:00
|
|
|
struct tr_session;
|
2021-10-29 18:24:30 +00:00
|
|
|
struct tr_torrent;
|
2021-12-14 20:59:40 +00:00
|
|
|
struct tr_announcer_tiers;
|
2008-11-06 02:56:51 +00:00
|
|
|
|
2008-10-20 16:00:14 +00:00
|
|
|
/**
|
|
|
|
*** Package-visible ctor API
|
|
|
|
**/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_torrentFree(tr_torrent* tor);
|
2012-12-14 20:04:37 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ctorSetSave(tr_ctor* ctor, bool saveMetadataInOurTorrentsDir);
|
2008-10-20 16:00:14 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_ctorGetSave(tr_ctor const* ctor);
|
2008-10-20 16:00:14 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_ctorInitTorrentPriorities(tr_ctor const* ctor, tr_torrent* tor);
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_ctorInitTorrentWanted(tr_ctor const* ctor, tr_torrent* tor);
|
2009-04-02 20:43:42 +00:00
|
|
|
|
2021-12-05 01:32:35 +00:00
|
|
|
bool tr_ctorSaveContents(tr_ctor const* ctor, char const* filename, tr_error** error);
|
|
|
|
|
|
|
|
bool tr_ctorGetMetainfo(tr_ctor const* ctor, tr_variant const** setme);
|
|
|
|
|
|
|
|
tr_session* tr_ctorGetSession(tr_ctor const* ctor);
|
|
|
|
|
|
|
|
bool tr_ctorGetIncompleteDir(tr_ctor const* ctor, char const** setmeIncompleteDir);
|
|
|
|
|
2008-10-20 16:00:14 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2021-09-29 19:52:19 +00:00
|
|
|
using tr_labels_t = std::unordered_set<std::string>;
|
|
|
|
|
|
|
|
void tr_torrentSetLabels(tr_torrent* tor, tr_labels_t&& labels);
|
2019-02-17 09:33:57 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_torrentChangeMyPort(tr_torrent* session);
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2021-11-04 00:55:04 +00:00
|
|
|
tr_sha1_digest_t tr_torrentInfoHash(tr_torrent const* torrent);
|
|
|
|
|
|
|
|
tr_torrent* tr_torrentFindFromHash(tr_session* session, tr_sha1_digest_t const& info_dict_hah);
|
|
|
|
|
2021-11-02 00:18:09 +00:00
|
|
|
tr_torrent* tr_torrentFindFromHashString(tr_session* session, std::string_view hash_string);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_torrent* tr_torrentFindFromObfuscatedHash(tr_session* session, uint8_t const* hash);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_torrentIsPieceTransferAllowed(tr_torrent const* torrent, tr_direction direction);
|
2008-11-07 04:10:27 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_torrentReqIsValid(tr_torrent const* tor, tr_piece_index_t index, uint32_t offset, uint32_t length);
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t tr_pieceOffset(tr_torrent const* tor, tr_piece_index_t index, uint32_t offset, uint32_t length);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void tr_torrentGetBlockLocation(
|
|
|
|
tr_torrent const* tor,
|
|
|
|
tr_block_index_t block,
|
|
|
|
tr_piece_index_t* piece,
|
|
|
|
uint32_t* offset,
|
2017-04-19 12:04:45 +00:00
|
|
|
uint32_t* length);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_block_span_t tr_torGetFileBlockSpan(tr_torrent const* tor, tr_file_index_t const file);
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_torrentCheckSeedLimit(tr_torrent* tor);
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2009-08-13 14:47:56 +00:00
|
|
|
/** save a torrent's .resume file if it's changed since the last time it was saved */
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_torrentSave(tr_torrent* tor);
|
2009-03-01 13:56:22 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_torrentSetLocalError(tr_torrent* tor, char const* fmt, ...) TR_GNUC_PRINTF(2, 3);
|
2009-01-13 16:39:19 +00:00
|
|
|
|
fix: gcc warnings in libtransmission/ and utils/ (#843)
* fix: __attribute__(__printf__) warnings
* fix: implicit fallthrough warning
* fixup! fix: implicit fallthrough warning
* fix: disable warnings for 3rd party code
Since we want to leave upstream code as-is
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: disable warnings for 3rd party code
* silence spurious alignment warning
Xrefs
Discussion: https://stackoverflow.com/a/35554349
Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: implicit fallthrough warning
* make uncrustify happy
* remove uncrustify-test.sh
that's probably off-topic for this PR
* fixup! fix: __attribute__(__printf__) warnings
* Update libtransmission/CMakeLists.txt
Co-Authored-By: ckerr <ckerr@github.com>
* fixup! silence spurious alignment warning
* use -w for DISABLE_WARNINGS in Clang
* refactor: fix libtransmission deprecation warnings
* fix: pthread_create's start_routine's return value
This was defined as `void` on non-Windows but should have been `void*`
* chore: uncrustify
* fix: add DISABLE_WARNINGS option for SunPro Studio
* fix "unused in lambda capture" warnings by clang++
* fix 'increases required alignment' warning
Caused from storing int16_t's in a char array.
* fix net.c 'increases required alignment' warning
The code passes in a `struct sockaddr_storage*` which is a padded struct
large enough for the necessary alignment. Unfortunately it was recast as
a `struct sockaddr*` which has less padding and a smaller alignment. The
warning occrred because of these differing alignments.
* make building quieter so warnings are more visible
* fixup! fix 'increases required alignment' warning
* Fix -Wcast-function-type warnings in GTK+ app code
https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both
the issue and its solution.
GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib
applications because it's idiomatic there to recast function signatures,
e.g. `g_slist_free(list, (GFunc)g_free, NULL);`.
Disabling the warning with pragmas causes "unrecognized pragma" warnings
on clang and older versions of gcc, and disabling the warning could miss
actual bugs. GCC defines `void (*)(void)` as a special case that matches
anything so we can silence warnings by double-casting through GCallback.
In the previous example, the warning is silenced by changing the code to
read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`).
* fixup! fix "unused in lambda capture" warnings by clang++
* fixup! fix "unused in lambda capture" warnings by clang++
* fix two more libtransmission compiler warnings
* fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
|
|
|
void tr_torrentSetDateAdded(tr_torrent* torrent, time_t addedDate);
|
|
|
|
|
|
|
|
void tr_torrentSetDateActive(tr_torrent* torrent, time_t activityDate);
|
|
|
|
|
|
|
|
void tr_torrentSetDateDone(tr_torrent* torrent, time_t doneDate);
|
|
|
|
|
2020-10-13 15:33:56 +00:00
|
|
|
/** Return the mime-type (e.g. "audio/x-flac") that matches more of the
|
|
|
|
torrent's content than any other mime-type. */
|
2021-10-20 02:30:50 +00:00
|
|
|
std::string_view tr_torrentPrimaryMimeType(tr_torrent const* tor);
|
2020-10-13 15:33:56 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_verify_state
|
2007-12-25 05:37:32 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
TR_VERIFY_NONE,
|
|
|
|
TR_VERIFY_WAIT,
|
|
|
|
TR_VERIFY_NOW
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_torrentSetVerifyState(tr_torrent* tor, tr_verify_state state);
|
2009-03-26 18:06:54 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
tr_torrent_activity tr_torrentGetActivity(tr_torrent const* tor);
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
struct tr_incomplete_metadata;
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief Torrent object */
|
2021-11-25 18:26:51 +00:00
|
|
|
struct tr_torrent
|
|
|
|
: public tr_block_info
|
|
|
|
, public tr_completion::torrent_view
|
2007-12-25 05:37:32 +00:00
|
|
|
{
|
2021-11-15 20:53:53 +00:00
|
|
|
public:
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_torrent(tr_info const& inf)
|
|
|
|
: tr_block_info{ inf.totalSize, inf.pieceSize }
|
|
|
|
, completion{ this, this }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-28 01:58:35 +00:00
|
|
|
virtual ~tr_torrent() override = default;
|
2021-11-25 18:26:51 +00:00
|
|
|
|
2021-11-15 20:53:53 +00:00
|
|
|
void setLocation(
|
|
|
|
std::string_view location,
|
|
|
|
bool move_from_current_location,
|
|
|
|
double volatile* setme_progress,
|
|
|
|
int volatile* setme_state);
|
2021-11-12 16:42:51 +00:00
|
|
|
|
2021-11-16 04:10:36 +00:00
|
|
|
void renamePath(
|
|
|
|
std::string_view oldpath,
|
|
|
|
std::string_view newname,
|
|
|
|
tr_torrent_rename_done_func callback,
|
|
|
|
void* callback_user_data);
|
|
|
|
|
2021-11-12 16:42:51 +00:00
|
|
|
tr_sha1_digest_t pieceHash(tr_piece_index_t i) const
|
|
|
|
{
|
|
|
|
TR_ASSERT(i < std::size(this->piece_checksums_));
|
|
|
|
return this->piece_checksums_[i];
|
|
|
|
}
|
|
|
|
|
2021-11-15 20:53:53 +00:00
|
|
|
// these functions should become private when possible,
|
|
|
|
// but more refactoring is needed before that can happen
|
|
|
|
// because much of tr_torrent's impl is in the non-member C bindings
|
|
|
|
//
|
|
|
|
// private:
|
2021-11-24 20:12:32 +00:00
|
|
|
void swapMetainfo(tr_metainfo_parsed& parsed);
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
auto unique_lock() const
|
|
|
|
{
|
|
|
|
return session->unique_lock();
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
/// COMPLETION
|
2021-11-15 20:53:53 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
[[nodiscard]] uint64_t leftUntilDone() const
|
|
|
|
{
|
|
|
|
return completion.leftUntilDone();
|
|
|
|
}
|
2021-11-15 20:53:53 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
[[nodiscard]] bool hasAll() const
|
|
|
|
{
|
|
|
|
return completion.hasAll();
|
|
|
|
}
|
2021-11-15 20:53:53 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
[[nodiscard]] bool hasNone() const
|
|
|
|
{
|
|
|
|
return completion.hasNone();
|
|
|
|
}
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
[[nodiscard]] bool hasPiece(tr_piece_index_t piece) const
|
|
|
|
{
|
|
|
|
return completion.hasPiece(piece);
|
|
|
|
}
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
[[nodiscard]] bool hasBlock(tr_block_index_t block) const
|
|
|
|
{
|
|
|
|
return completion.hasBlock(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] size_t countMissingBlocksInPiece(tr_piece_index_t piece) const
|
|
|
|
{
|
|
|
|
return completion.countMissingBlocksInPiece(piece);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] size_t countMissingBytesInPiece(tr_piece_index_t piece) const
|
|
|
|
{
|
|
|
|
return completion.countMissingBytesInPiece(piece);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] uint64_t hasTotal() const
|
|
|
|
{
|
|
|
|
return completion.hasTotal();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::vector<uint8_t> createPieceBitfield() const
|
|
|
|
{
|
|
|
|
return completion.createPieceBitfield();
|
|
|
|
}
|
|
|
|
|
2021-12-15 07:04:26 +00:00
|
|
|
[[nodiscard]] constexpr bool isDone() const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2021-12-15 07:04:26 +00:00
|
|
|
return completeness != TR_LEECH;
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
[[nodiscard]] tr_bitfield const& blocks() const
|
|
|
|
{
|
|
|
|
return completion.blocks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void amountDoneBins(float* tab, int n_tabs) const
|
|
|
|
{
|
|
|
|
return completion.amountDone(tab, n_tabs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBlocks(tr_bitfield blocks)
|
|
|
|
{
|
|
|
|
completion.setBlocks(std::move(blocks));
|
|
|
|
}
|
|
|
|
|
|
|
|
void setHasPiece(tr_piece_index_t piece, bool has)
|
|
|
|
{
|
|
|
|
completion.setHasPiece(piece, has);
|
|
|
|
}
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
/// FILE <-> PIECE
|
|
|
|
|
|
|
|
auto piecesInFile(tr_file_index_t file) const
|
2021-10-29 18:24:30 +00:00
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
return fpm_.pieceSpan(file);
|
2021-10-29 18:24:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
/// WANTED
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2021-12-06 03:12:21 +00:00
|
|
|
bool pieceIsWanted(tr_piece_index_t piece) const final
|
2021-10-29 18:24:30 +00:00
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
return files_wanted_.pieceWanted(piece);
|
|
|
|
}
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
bool fileIsWanted(tr_file_index_t file) const
|
|
|
|
{
|
|
|
|
return files_wanted_.fileWanted(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initFilesWanted(tr_file_index_t const* files, size_t n_files, bool wanted)
|
|
|
|
{
|
|
|
|
setFilesWanted(files, n_files, wanted, /*is_bootstrapping*/ true);
|
2021-10-29 18:24:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
void setFilesWanted(tr_file_index_t const* files, size_t n_files, bool wanted)
|
|
|
|
{
|
|
|
|
setFilesWanted(files, n_files, wanted, /*is_bootstrapping*/ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void recheckCompleteness(); // TODO(ckerr): should be private
|
|
|
|
|
|
|
|
/// PRIORITIES
|
|
|
|
|
2021-10-29 18:24:30 +00:00
|
|
|
tr_priority_t piecePriority(tr_piece_index_t piece) const
|
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
return file_priorities_.piecePriority(piece);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setFilePriorities(tr_file_index_t const* files, tr_file_index_t fileCount, tr_priority_t priority)
|
|
|
|
{
|
|
|
|
file_priorities_.set(files, fileCount, priority);
|
|
|
|
setDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setFilePriority(tr_file_index_t file, tr_priority_t priority)
|
|
|
|
{
|
|
|
|
file_priorities_.set(file, priority);
|
|
|
|
setDirty();
|
2021-10-29 18:24:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
/// METAINFO - FILES
|
2021-12-07 04:18:17 +00:00
|
|
|
|
|
|
|
tr_file_index_t fileCount() const
|
|
|
|
{
|
|
|
|
return info.fileCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& file(tr_file_index_t i)
|
|
|
|
{
|
|
|
|
TR_ASSERT(i < this->fileCount());
|
|
|
|
|
|
|
|
return info.files[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const& file(tr_file_index_t i) const
|
|
|
|
{
|
|
|
|
TR_ASSERT(i < this->fileCount());
|
|
|
|
|
|
|
|
return info.files[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tr_found_file_t : public tr_sys_path_info
|
|
|
|
{
|
|
|
|
std::string& filename; // /home/foo/Downloads/torrent/01-file-one.txt
|
|
|
|
std::string_view base; // /home/foo/Downloads
|
|
|
|
std::string_view subpath; // /torrent/01-file-one.txt
|
|
|
|
|
|
|
|
tr_found_file_t(tr_sys_path_info info, std::string& f, std::string_view b)
|
|
|
|
: tr_sys_path_info{ info }
|
|
|
|
, filename{ f }
|
|
|
|
, base{ b }
|
|
|
|
, subpath{ f.c_str() + std::size(b) + 1 }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::optional<tr_found_file_t> findFile(std::string& filename, tr_file_index_t i) const;
|
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
/// METAINFO - TRACKERS
|
2021-12-14 20:59:40 +00:00
|
|
|
|
2021-12-14 21:59:44 +00:00
|
|
|
auto trackerCount() const
|
2021-12-14 20:59:40 +00:00
|
|
|
{
|
|
|
|
return std::size(*info.announce_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const& tracker(size_t i) const
|
|
|
|
{
|
|
|
|
return info.announce_list->at(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tiers() const
|
|
|
|
{
|
|
|
|
return info.announce_list->tiers();
|
|
|
|
}
|
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
/// METAINFO - WEBSEEDS
|
2021-12-07 18:11:28 +00:00
|
|
|
|
|
|
|
auto webseedCount() const
|
|
|
|
{
|
|
|
|
return info.webseedCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const& webseed(size_t i) const
|
|
|
|
{
|
|
|
|
TR_ASSERT(i < webseedCount());
|
|
|
|
|
|
|
|
return info.webseeds[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& webseed(size_t i)
|
|
|
|
{
|
|
|
|
TR_ASSERT(i < webseedCount());
|
|
|
|
|
|
|
|
return info.webseeds[i];
|
|
|
|
}
|
|
|
|
|
2021-12-15 15:53:20 +00:00
|
|
|
/// METAINFO - OTHER
|
|
|
|
|
|
|
|
auto isPrivate() const
|
|
|
|
{
|
|
|
|
return this->info.isPrivate;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto isPublic() const
|
|
|
|
{
|
|
|
|
return !this->isPrivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pieceCount() const
|
|
|
|
{
|
|
|
|
return this->info.pieceCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pieceSize() const
|
|
|
|
{
|
|
|
|
return this->info.pieceSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pieceSize(tr_piece_index_t i) const
|
|
|
|
{
|
|
|
|
return tr_block_info::pieceSize(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto totalSize() const
|
|
|
|
{
|
|
|
|
return this->info.totalSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto hashString() const
|
|
|
|
{
|
|
|
|
return this->info.hashString;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const& announceList() const
|
|
|
|
{
|
|
|
|
return *this->info.announce_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& announceList()
|
|
|
|
{
|
|
|
|
return *this->info.announce_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const& torrentFile() const
|
|
|
|
{
|
|
|
|
return this->info.torrent;
|
|
|
|
}
|
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
/// METAINFO - CHECKSUMS
|
2021-10-29 18:24:30 +00:00
|
|
|
|
|
|
|
bool ensurePieceIsChecked(tr_piece_index_t piece)
|
|
|
|
{
|
2021-12-15 15:53:20 +00:00
|
|
|
TR_ASSERT(piece < this->pieceCount());
|
2021-10-29 18:24:30 +00:00
|
|
|
|
|
|
|
if (checked_pieces_.test(piece))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool const checked = checkPiece(piece);
|
2021-12-15 21:25:42 +00:00
|
|
|
this->markChanged();
|
2021-10-29 18:24:30 +00:00
|
|
|
this->setDirty();
|
|
|
|
|
|
|
|
checked_pieces_.set(piece, checked);
|
|
|
|
return checked;
|
|
|
|
}
|
|
|
|
|
2021-12-10 02:54:44 +00:00
|
|
|
void initCheckedPieces(tr_bitfield const& checked, time_t const* mtimes /*fileCount()*/)
|
2021-10-29 18:24:30 +00:00
|
|
|
{
|
2021-12-15 15:53:20 +00:00
|
|
|
TR_ASSERT(std::size(checked) == this->pieceCount());
|
2021-10-29 18:24:30 +00:00
|
|
|
checked_pieces_ = checked;
|
|
|
|
|
2021-10-31 16:38:10 +00:00
|
|
|
auto filename = std::string{};
|
2021-12-06 21:26:04 +00:00
|
|
|
for (size_t i = 0, n = this->fileCount(); i < n; ++i)
|
2021-10-29 18:24:30 +00:00
|
|
|
{
|
2021-10-31 16:38:10 +00:00
|
|
|
auto const found = this->findFile(filename, i);
|
|
|
|
auto const mtime = found ? found->last_modified_at : 0;
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2021-12-07 04:18:17 +00:00
|
|
|
this->file(i).priv.mtime = mtime;
|
2021-10-29 18:24:30 +00:00
|
|
|
|
|
|
|
// if a file has changed, mark its pieces as unchecked
|
2021-10-31 16:38:10 +00:00
|
|
|
if (mtime == 0 || mtime != mtimes[i])
|
2021-10-29 18:24:30 +00:00
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
auto const [begin, end] = piecesInFile(i);
|
2021-11-25 18:26:51 +00:00
|
|
|
checked_pieces_.unsetSpan(begin, end);
|
2021-10-29 18:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
///
|
|
|
|
|
2021-12-15 07:04:26 +00:00
|
|
|
constexpr auto queueDirection() const
|
|
|
|
{
|
|
|
|
return this->isDone() ? TR_UP : TR_DOWN;
|
|
|
|
}
|
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
auto allowsPex() const
|
|
|
|
{
|
|
|
|
return this->isPublic() && this->session->isPexEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto allowsDht() const
|
|
|
|
{
|
|
|
|
return this->isPublic() && tr_sessionAllowsDHT(this->session);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto allowsLpd() const // local peer discovery
|
|
|
|
{
|
|
|
|
return this->isPublic() && tr_sessionAllowsLPD(this->session);
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_info info = {};
|
|
|
|
|
|
|
|
tr_bitfield checked_pieces_ = tr_bitfield{ 0 };
|
|
|
|
|
|
|
|
// TODO(ckerr): make private once some of torrent.cc's `tr_torrentFoo()` methods are member functions
|
|
|
|
tr_completion completion;
|
|
|
|
|
|
|
|
tr_session* session = nullptr;
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
tr_announcer_tiers* announcer_tiers = nullptr;
|
2021-11-25 18:26:51 +00:00
|
|
|
|
|
|
|
// Changed to non-owning pointer temporarily till tr_torrent becomes C++-constructible and destructible
|
|
|
|
// TODO: change tr_bandwidth* to owning pointer to the bandwidth, or remove * and own the value
|
|
|
|
Bandwidth* bandwidth = nullptr;
|
|
|
|
|
|
|
|
tr_swarm* swarm = nullptr;
|
|
|
|
|
2021-11-26 00:14:25 +00:00
|
|
|
// TODO: is this actually still needed?
|
|
|
|
int const magicNumber = MagicNumber;
|
2021-11-25 18:26:51 +00:00
|
|
|
|
|
|
|
std::optional<double> verify_progress;
|
|
|
|
|
|
|
|
tr_stat_errtype error = TR_STAT_OK;
|
|
|
|
char errorString[128] = {};
|
|
|
|
tr_quark error_announce_url = TR_KEY_NONE;
|
|
|
|
|
|
|
|
bool checkPiece(tr_piece_index_t piece);
|
|
|
|
|
|
|
|
uint8_t obfuscatedHash[SHA_DIGEST_LENGTH] = {};
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
/* Used when the torrent has been created with a magnet link
|
|
|
|
* and we're in the process of downloading the metainfo from
|
|
|
|
* other peers */
|
2021-11-25 18:26:51 +00:00
|
|
|
struct tr_incomplete_metadata* incompleteMetadata = nullptr;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2008-12-04 05:27:59 +00:00
|
|
|
/* If the initiator of the connection receives a handshake in which the
|
|
|
|
* peer_id does not match the expected peerid, then the initiator is
|
|
|
|
* expected to drop the connection. Note that the initiator presumably
|
|
|
|
* received the peer information from the tracker, which includes the
|
|
|
|
* peer_id that was registered by the peer. The peer_id from the tracker
|
|
|
|
* and in the handshake are expected to match.
|
|
|
|
*/
|
2021-10-22 02:40:55 +00:00
|
|
|
std::optional<tr_peer_id_t> peer_id;
|
2013-02-02 13:42:50 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
time_t peer_id_creation_time = 0;
|
2008-12-04 05:27:59 +00:00
|
|
|
|
2009-10-19 05:05:00 +00:00
|
|
|
/* Where the files will be when it's complete */
|
2021-11-25 18:26:51 +00:00
|
|
|
char* downloadDir = nullptr;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-10-19 05:05:00 +00:00
|
|
|
/* Where the files are when the torrent is incomplete */
|
2021-11-25 18:26:51 +00:00
|
|
|
char* incompleteDir = nullptr;
|
|
|
|
|
|
|
|
/* Where the files are now.
|
|
|
|
* This pointer will be equal to downloadDir or incompleteDir */
|
|
|
|
char const* currentDir = nullptr;
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2010-06-16 03:05:23 +00:00
|
|
|
/* Length, in bytes, of the "info" dict in the .torrent file. */
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t infoDictLength = 0;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2010-06-16 03:05:23 +00:00
|
|
|
/* Offset, in bytes, of the beginning of the "info" dict in the .torrent file.
|
|
|
|
*
|
|
|
|
* Used by the torrent-magnet code for serving metainfo to peers.
|
|
|
|
* This field is lazy-generated and might not be initialized yet. */
|
2021-11-25 18:26:51 +00:00
|
|
|
size_t infoDictOffset = 0;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_completeness completeness = TR_LEECH;
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
time_t dhtAnnounceAt = 0;
|
|
|
|
time_t dhtAnnounce6At = 0;
|
|
|
|
bool dhtAnnounceInProgress = false;
|
|
|
|
bool dhtAnnounce6InProgress = false;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
time_t lpdAnnounceAt = 0;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t downloadedCur = 0;
|
|
|
|
uint64_t downloadedPrev = 0;
|
|
|
|
uint64_t uploadedCur = 0;
|
|
|
|
uint64_t uploadedPrev = 0;
|
|
|
|
uint64_t corruptCur = 0;
|
2021-11-26 00:14:25 +00:00
|
|
|
uint64_t corruptPrev = 0;
|
2010-12-12 16:43:19 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t etaDLSpeedCalculatedAt = 0;
|
|
|
|
uint64_t etaULSpeedCalculatedAt = 0;
|
|
|
|
unsigned int etaDLSpeed_Bps = 0;
|
|
|
|
unsigned int etaULSpeed_Bps = 0;
|
2009-05-19 18:38:26 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
time_t activityDate = 0;
|
|
|
|
time_t addedDate = 0;
|
|
|
|
time_t anyDate = 0;
|
|
|
|
time_t doneDate = 0;
|
|
|
|
time_t editDate = 0;
|
|
|
|
time_t startDate = 0;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
int secondsDownloading = 0;
|
|
|
|
int secondsSeeding = 0;
|
2009-07-02 01:50:07 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
int queuePosition = 0;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_torrent_metadata_func metadata_func = nullptr;
|
|
|
|
void* metadata_func_user_data = nullptr;
|
2010-12-23 19:32:59 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_torrent_completeness_func completeness_func = nullptr;
|
|
|
|
void* completeness_func_user_data = nullptr;
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_torrent_ratio_limit_hit_func ratio_limit_hit_func = nullptr;
|
|
|
|
void* ratio_limit_hit_func_user_data = nullptr;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_torrent_idle_limit_hit_func idle_limit_hit_func = nullptr;
|
|
|
|
void* idle_limit_hit_func_user_data = nullptr;
|
2009-11-26 17:58:44 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
void* queue_started_user_data = nullptr;
|
|
|
|
void (*queue_started_callback)(tr_torrent*, void* queue_started_user_data) = nullptr;
|
2009-02-13 18:23:56 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
bool isDeleting = false;
|
|
|
|
bool isDirty = false;
|
|
|
|
bool isQueued = false;
|
|
|
|
bool isRunning = false;
|
|
|
|
bool isStopping = false;
|
|
|
|
bool startAfterVerify = false;
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
bool prefetchMagnetMetadata = false;
|
|
|
|
bool magnetVerify = false;
|
2011-08-03 03:14:57 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
// TODO(ckerr) use std::optional
|
|
|
|
bool infoDictOffsetIsCached = false;
|
2021-10-29 18:24:30 +00:00
|
|
|
|
|
|
|
void setDirty()
|
|
|
|
{
|
|
|
|
this->isDirty = true;
|
|
|
|
}
|
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
void markEdited();
|
|
|
|
void markChanged();
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint16_t maxConnectedPeers = TR_DEFAULT_PEER_LIMIT_TORRENT;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_verify_state verifyState = TR_VERIFY_NONE;
|
2014-01-19 05:03:27 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
time_t lastStatTime = 0;
|
|
|
|
tr_stat stats = {};
|
2009-06-16 19:31:17 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
int uniqueId = 0;
|
2009-02-13 18:23:56 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
float desiredRatio = 0.0F;
|
|
|
|
tr_ratiolimit ratioLimitMode = TR_RATIOLIMIT_GLOBAL;
|
2009-08-13 17:50:41 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint16_t idleLimitMinutes = 0;
|
|
|
|
tr_idlelimit idleLimitMode = TR_IDLELIMIT_GLOBAL;
|
|
|
|
bool finishedSeedingByIdle = false;
|
2019-02-17 09:33:57 +00:00
|
|
|
|
2021-09-29 19:52:19 +00:00
|
|
|
tr_labels_t labels;
|
2021-11-12 16:42:51 +00:00
|
|
|
|
2021-11-26 00:14:25 +00:00
|
|
|
static auto constexpr MagicNumber = int{ 95549 };
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
tr_file_piece_map fpm_ = tr_file_piece_map{ info };
|
|
|
|
tr_file_priorities file_priorities_{ &fpm_ };
|
|
|
|
tr_files_wanted files_wanted_{ &fpm_ };
|
|
|
|
|
2021-11-12 16:42:51 +00:00
|
|
|
private:
|
2021-11-29 01:12:54 +00:00
|
|
|
void setFilesWanted(tr_file_index_t const* files, size_t n_files, bool wanted, bool is_bootstrapping)
|
|
|
|
{
|
|
|
|
auto const lock = unique_lock();
|
|
|
|
|
|
|
|
files_wanted_.set(files, n_files, wanted);
|
|
|
|
completion.invalidateSizeWhenDone();
|
|
|
|
|
|
|
|
if (!is_bootstrapping)
|
|
|
|
{
|
|
|
|
setDirty();
|
|
|
|
recheckCompleteness();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 16:42:51 +00:00
|
|
|
mutable std::vector<tr_sha1_digest_t> piece_checksums_;
|
2007-12-25 05:37:32 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline bool tr_torrentExists(tr_session const* session, uint8_t const* torrentHash)
|
2009-01-02 17:01:55 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return tr_torrentFindFromHash((tr_session*)session, torrentHash) != nullptr;
|
2009-01-02 17:01:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
constexpr tr_completeness tr_torrentGetCompleteness(tr_torrent const* tor)
|
2013-08-18 13:06:39 +00:00
|
|
|
{
|
|
|
|
return tor->completeness;
|
|
|
|
}
|
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
constexpr bool tr_isTorrent(tr_torrent const* tor)
|
2009-01-30 00:41:08 +00:00
|
|
|
{
|
2021-11-26 00:14:25 +00:00
|
|
|
return tor != nullptr && tor->magicNumber == tr_torrent::MagicNumber && tr_isSession(tor->session);
|
2009-01-30 00:41:08 +00:00
|
|
|
}
|
|
|
|
|
2009-10-19 05:05:00 +00:00
|
|
|
/**
|
2013-01-26 23:08:51 +00:00
|
|
|
* Tell the tr_torrent that it's gotten a block
|
2009-10-19 05:05:00 +00:00
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_torrentGotBlock(tr_torrent* tor, tr_block_index_t blockIndex);
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2009-10-20 04:43:51 +00:00
|
|
|
/**
|
2020-11-02 15:16:12 +00:00
|
|
|
* @brief Like tr_torrentFindFile(), but splits the filename into base and subpath.
|
2009-10-20 04:43:51 +00:00
|
|
|
*
|
2021-10-06 16:32:17 +00:00
|
|
|
* If the file is found, "tr_buildPath(base, subpath, nullptr)"
|
2009-10-20 04:43:51 +00:00
|
|
|
* will generate the complete filename.
|
|
|
|
*
|
|
|
|
* @return true if the file is found, false otherwise.
|
|
|
|
*
|
|
|
|
* @param base if the torrent is found, this will be either
|
|
|
|
* tor->downloadDir or tor->incompleteDir
|
|
|
|
* @param subpath on success, this pointer is assigned a newly-allocated
|
|
|
|
* string holding the second half of the filename.
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_torrentFindFile2(tr_torrent const*, tr_file_index_t fileNo, char const** base, char** subpath, time_t* mtime);
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2009-10-20 04:43:51 +00:00
|
|
|
/* Returns a newly-allocated version of the tr_file.name string
|
|
|
|
* that's been modified to denote that it's not a complete file yet.
|
|
|
|
* In the current implementation this is done by appending ".part"
|
|
|
|
* a la Firefox. */
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_torrentBuildPartial(tr_torrent const*, tr_file_index_t fileNo);
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
/* for when the info dict has been fundamentally changed wrt files,
|
|
|
|
* piece size, etc. such as in BEP 9 where peers exchange metadata */
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_torrentGotNewInfoDict(tr_torrent* tor);
|
2009-10-20 03:14:44 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_torrentSetSpeedLimit_Bps(tr_torrent*, tr_direction, unsigned int Bps);
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned int tr_torrentGetSpeedLimit_Bps(tr_torrent const*, tr_direction);
|
2010-07-04 06:07:21 +00:00
|
|
|
|
2010-12-09 20:43:23 +00:00
|
|
|
/**
|
|
|
|
* @brief Test a piece against its info dict checksum
|
|
|
|
* @return true if the piece's passes the checksum test
|
2010-12-12 16:43:19 +00:00
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
bool tr_torrentCheckPiece(tr_torrent* tor, tr_piece_index_t pieceIndex);
|
2010-12-09 20:43:23 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t tr_torrentGetCurrentSizeOnDisk(tr_torrent const* tor);
|
2010-12-09 20:43:23 +00:00
|
|
|
|
2021-10-22 02:40:55 +00:00
|
|
|
tr_peer_id_t const& tr_torrentGetPeerId(tr_torrent* tor);
|
2013-02-02 13:42:50 +00:00
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
constexpr bool tr_torrentIsQueued(tr_torrent const* tor)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tor->isQueued;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 21:59:44 +00:00
|
|
|
tr_info const* tr_torrentInfo(tr_torrent const* torrent);
|