2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +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.
|
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-16 09:43:51 +00:00
|
|
|
#include <cstddef> // size_t
|
2023-11-03 17:03:26 +00:00
|
|
|
#include <cstdint> // uint64_t, uint16_t
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
2023-10-19 13:39:34 +00:00
|
|
|
#include <functional>
|
2023-10-05 02:38:14 +00:00
|
|
|
#include <memory>
|
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>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2021-10-16 14:04:19 +00:00
|
|
|
#include <vector>
|
2021-09-29 19:52:19 +00:00
|
|
|
|
2023-07-15 20:38:38 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
|
|
|
|
|
|
|
#include "libtransmission/announce-list.h"
|
|
|
|
#include "libtransmission/bandwidth.h"
|
|
|
|
#include "libtransmission/bitfield.h"
|
|
|
|
#include "libtransmission/block-info.h"
|
|
|
|
#include "libtransmission/completion.h"
|
|
|
|
#include "libtransmission/crypto-utils.h"
|
|
|
|
#include "libtransmission/file-piece-map.h"
|
|
|
|
#include "libtransmission/interned-string.h"
|
|
|
|
#include "libtransmission/log.h"
|
2023-10-19 13:39:34 +00:00
|
|
|
#include "libtransmission/observable.h"
|
2023-07-15 20:38:38 +00:00
|
|
|
#include "libtransmission/session.h"
|
2023-11-03 17:03:26 +00:00
|
|
|
#include "libtransmission/torrent-files.h"
|
2023-07-15 20:38:38 +00:00
|
|
|
#include "libtransmission/torrent-magnet.h"
|
|
|
|
#include "libtransmission/torrent-metainfo.h"
|
2023-10-04 01:18:55 +00:00
|
|
|
#include "libtransmission/tr-assert.h"
|
2023-07-15 20:38:38 +00:00
|
|
|
#include "libtransmission/tr-macros.h"
|
2023-10-19 13:39:34 +00:00
|
|
|
#include "libtransmission/verify.h"
|
2009-01-02 17:01:55 +00:00
|
|
|
|
2023-11-03 17:03:26 +00:00
|
|
|
struct tr_ctor;
|
2021-10-10 00:13:40 +00:00
|
|
|
class tr_swarm;
|
2021-12-15 21:25:42 +00:00
|
|
|
struct tr_error;
|
2021-10-29 18:24:30 +00:00
|
|
|
struct tr_torrent;
|
2022-01-23 05:41:01 +00:00
|
|
|
struct tr_torrent_announcer;
|
2008-11-06 02:56:51 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Package-visible
|
2008-10-20 16:00:14 +00:00
|
|
|
|
2022-11-04 21:20:27 +00:00
|
|
|
void tr_torrentFreeInSessionThread(tr_torrent* tor);
|
2012-12-14 20:04:37 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-10-20 16:00:14 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_torrentChangeMyPort(tr_torrent* tor);
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2023-11-23 05:41:12 +00:00
|
|
|
namespace libtransmission::test
|
|
|
|
{
|
|
|
|
|
|
|
|
class RenameTest_multifileTorrent_Test;
|
|
|
|
class RenameTest_singleFilenameTorrent_Test;
|
|
|
|
|
|
|
|
} // namespace libtransmission::test
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief Torrent object */
|
2024-03-30 19:45:00 +00:00
|
|
|
struct tr_torrent
|
2007-12-25 05:37:32 +00:00
|
|
|
{
|
2023-11-10 23:12:24 +00:00
|
|
|
using Speed = libtransmission::Values::Speed;
|
|
|
|
|
2023-11-23 05:41:12 +00:00
|
|
|
class ResumeHelper
|
|
|
|
{
|
|
|
|
public:
|
2024-05-25 15:08:53 +00:00
|
|
|
void load_checked_pieces(tr_bitfield const& checked, time_t const* mtimes /*file_count()*/);
|
2023-11-26 02:00:20 +00:00
|
|
|
void load_blocks(tr_bitfield blocks);
|
2023-11-24 01:52:53 +00:00
|
|
|
void load_date_added(time_t when) noexcept;
|
|
|
|
void load_date_done(time_t when) noexcept;
|
2023-11-26 02:00:20 +00:00
|
|
|
void load_download_dir(std::string_view dir) noexcept;
|
|
|
|
void load_incomplete_dir(std::string_view dir) noexcept;
|
2023-11-23 05:41:12 +00:00
|
|
|
void load_seconds_downloading_before_current_start(time_t when) noexcept;
|
|
|
|
void load_seconds_seeding_before_current_start(time_t when) noexcept;
|
2023-11-28 05:04:04 +00:00
|
|
|
void load_start_when_stable(bool val) noexcept;
|
2023-11-23 05:41:12 +00:00
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
[[nodiscard]] tr_bitfield const& blocks() const noexcept;
|
|
|
|
[[nodiscard]] tr_bitfield const& checked_pieces() const noexcept;
|
|
|
|
[[nodiscard]] std::vector<time_t> const& file_mtimes() const noexcept;
|
2023-11-24 01:52:53 +00:00
|
|
|
[[nodiscard]] time_t date_active() const noexcept;
|
|
|
|
[[nodiscard]] time_t date_added() const noexcept;
|
|
|
|
[[nodiscard]] time_t date_done() const noexcept;
|
2023-11-23 05:41:12 +00:00
|
|
|
[[nodiscard]] time_t seconds_downloading(time_t now) const noexcept;
|
|
|
|
[[nodiscard]] time_t seconds_seeding(time_t now) const noexcept;
|
2023-11-28 05:04:04 +00:00
|
|
|
[[nodiscard]] bool start_when_stable() const noexcept;
|
2023-11-23 05:41:12 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class libtransmission::test::RenameTest_multifileTorrent_Test;
|
|
|
|
friend class libtransmission::test::RenameTest_singleFilenameTorrent_Test;
|
|
|
|
friend struct tr_torrent;
|
|
|
|
|
|
|
|
ResumeHelper(tr_torrent& tor)
|
|
|
|
: tor_{ tor }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent& tor_;
|
|
|
|
};
|
|
|
|
|
2023-11-16 00:53:43 +00:00
|
|
|
class CumulativeCount
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
[[nodiscard]] constexpr auto start_new_session() noexcept
|
|
|
|
{
|
|
|
|
prev_ += cur_;
|
|
|
|
cur_ = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto during_this_session() const noexcept
|
|
|
|
{
|
|
|
|
return cur_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto ever() const noexcept
|
|
|
|
{
|
|
|
|
return cur_ + prev_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto& operator+=(uint64_t count) noexcept
|
|
|
|
{
|
|
|
|
cur_ += count;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void reduce(uint64_t count) // subtract w/underflow guard
|
|
|
|
{
|
|
|
|
cur_ = cur_ >= count ? cur_ - count : 0U;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void set_prev(uint64_t count) noexcept
|
|
|
|
{
|
|
|
|
prev_ = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint64_t prev_ = {};
|
|
|
|
uint64_t cur_ = {};
|
|
|
|
};
|
|
|
|
|
2023-10-23 22:11:28 +00:00
|
|
|
using labels_t = std::vector<tr_interned_string>;
|
2023-11-16 00:53:43 +00:00
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
using VerifyDoneCallback = std::function<void(tr_torrent*)>;
|
|
|
|
|
|
|
|
class VerifyMediator : public tr_verify_worker::Mediator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit VerifyMediator(tr_torrent* const tor)
|
|
|
|
: tor_{ tor }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~VerifyMediator() override = default;
|
|
|
|
|
|
|
|
[[nodiscard]] tr_torrent_metainfo const& metainfo() const override;
|
|
|
|
[[nodiscard]] std::optional<std::string> find_file(tr_file_index_t file_index) const override;
|
|
|
|
|
|
|
|
void on_verify_queued() override;
|
|
|
|
void on_verify_started() override;
|
|
|
|
void on_piece_checked(tr_piece_index_t piece, bool has_piece) override;
|
|
|
|
void on_verify_done(bool aborted) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
tr_torrent* const tor_;
|
|
|
|
std::optional<time_t> time_started_;
|
|
|
|
};
|
|
|
|
|
2023-11-16 00:53:43 +00:00
|
|
|
// ---
|
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
explicit tr_torrent(tr_torrent_metainfo&& tm)
|
|
|
|
: metainfo_{ std::move(tm) }
|
2024-03-30 19:45:00 +00:00
|
|
|
, completion_{ this, &metainfo_.block_info() }
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-10-22 02:33:41 +00:00
|
|
|
void set_location(std::string_view location, bool move_from_old_path, int volatile* setme_state);
|
2021-11-12 16:42:51 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void rename_path(
|
2021-11-16 04:10:36 +00:00
|
|
|
std::string_view oldpath,
|
|
|
|
std::string_view newname,
|
|
|
|
tr_torrent_rename_done_func callback,
|
|
|
|
void* callback_user_data);
|
|
|
|
|
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
|
2022-01-15 19:33:57 +00:00
|
|
|
|
2023-02-13 18:33:33 +00:00
|
|
|
// Used to add metainfo to a magnet torrent.
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_metainfo(tr_torrent_metainfo tm);
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2022-01-23 05:41:01 +00:00
|
|
|
[[nodiscard]] auto unique_lock() const
|
2021-11-20 21:20:45 +00:00
|
|
|
{
|
|
|
|
return session->unique_lock();
|
|
|
|
}
|
|
|
|
|
2023-11-23 05:41:12 +00:00
|
|
|
void save_resume_file();
|
|
|
|
|
2023-11-24 01:52:53 +00:00
|
|
|
[[nodiscard]] constexpr auto started_recently(time_t const now, time_t recent_secs = 120) const noexcept
|
|
|
|
{
|
|
|
|
return now - date_started_ <= recent_secs;
|
|
|
|
}
|
|
|
|
|
2021-12-16 08:49:04 +00:00
|
|
|
/// SPEED LIMIT
|
|
|
|
|
2023-01-07 22:55:00 +00:00
|
|
|
[[nodiscard]] constexpr auto& bandwidth() noexcept
|
|
|
|
{
|
|
|
|
return bandwidth_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& bandwidth() const noexcept
|
|
|
|
{
|
|
|
|
return bandwidth_;
|
|
|
|
}
|
|
|
|
|
2023-11-10 23:12:24 +00:00
|
|
|
constexpr void set_speed_limit(tr_direction dir, Speed limit)
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-11-10 23:12:24 +00:00
|
|
|
if (bandwidth().set_desired_speed(dir, limit))
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
set_dirty();
|
2022-11-03 23:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-16 08:49:04 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
constexpr void use_speed_limit(tr_direction dir, bool do_use)
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (bandwidth().set_limited(dir, do_use))
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
set_dirty();
|
2022-11-03 23:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 23:12:24 +00:00
|
|
|
[[nodiscard]] constexpr auto speed_limit(tr_direction dir) const
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-11-10 23:12:24 +00:00
|
|
|
return bandwidth().get_desired_speed(dir);
|
2022-11-03 23:08:02 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto uses_session_limits() const noexcept
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return bandwidth().are_parent_limits_honored(TR_UP);
|
2022-11-03 23:08:02 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto uses_speed_limit(tr_direction dir) const noexcept
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return bandwidth().is_limited(dir);
|
2022-11-03 23:08:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 19:13:37 +00:00
|
|
|
/// BLOCK INFO
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto const& block_info() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.block_info();
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto block_count() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.block_count();
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto byte_loc(uint64_t byte) const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.byte_loc(byte);
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto block_loc(tr_block_index_t block) const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.block_loc(block);
|
2022-02-18 23:17:19 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto piece_loc(tr_piece_index_t piece, uint32_t offset = 0, uint32_t length = 0) const noexcept
|
2022-02-18 23:17:19 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.piece_loc(piece, offset, length);
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto block_size(tr_block_index_t block) const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.block_size(block);
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto block_span_for_piece(tr_piece_index_t piece) const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.block_span_for_piece(piece);
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto piece_count() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.piece_count();
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto piece_size() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.piece_size();
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto piece_size(tr_piece_index_t piece) const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.piece_size(piece);
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto total_size() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.total_size();
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
[[nodiscard]] tr_block_span_t block_span_for_file(tr_file_index_t file) const noexcept;
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
/// COMPLETION
|
2021-11-15 20:53:53 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto left_until_done() const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.left_until_done();
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
2021-11-15 20:53:53 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto size_when_done() const
|
2022-03-17 18:41:48 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.size_when_done();
|
2022-03-17 18:41:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto has_metainfo() const noexcept
|
2022-03-29 04:29:35 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.has_metainfo();
|
2022-03-29 04:29:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto has_all() const noexcept
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.has_all();
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
2021-11-15 20:53:53 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto has_none() const noexcept
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.has_none();
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto has_piece(tr_piece_index_t piece) const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.has_piece(piece);
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto has_block(tr_block_index_t block) const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.has_block(block);
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto count_missing_blocks_in_piece(tr_piece_index_t piece) const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.count_missing_blocks_in_piece(piece);
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto count_missing_bytes_in_piece(tr_piece_index_t piece) const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.count_missing_bytes_in_piece(piece);
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto has_total() const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.has_total();
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto create_piece_bitfield() const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.create_piece_bitfield();
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr bool is_done() const noexcept
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-28 05:04:04 +00:00
|
|
|
return completeness_ != TR_LEECH;
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr bool is_seed() const noexcept
|
2021-12-16 08:49:04 +00:00
|
|
|
{
|
2023-11-28 05:04:04 +00:00
|
|
|
return completeness_ == TR_SEED;
|
2021-12-16 08:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr bool is_partial_seed() const noexcept
|
2021-12-16 08:49:04 +00:00
|
|
|
{
|
2023-11-28 05:04:04 +00:00
|
|
|
return completeness_ == TR_PARTIAL_SEED;
|
2021-12-16 08:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void amount_done_bins(float* tab, int n_tabs) const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2023-11-26 02:00:20 +00:00
|
|
|
return completion_.amount_done(tab, n_tabs);
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
/// FILE <-> PIECE
|
|
|
|
|
2023-12-06 04:06:27 +00:00
|
|
|
[[nodiscard]] auto piece_span_for_file(tr_file_index_t file) const
|
2021-10-29 18:24:30 +00:00
|
|
|
{
|
2023-12-06 04:06:27 +00:00
|
|
|
return fpm_.piece_span_for_file(file);
|
2021-10-29 18:24:30 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 16:51:13 +00:00
|
|
|
[[nodiscard]] auto file_offset(tr_block_info::Location loc) const
|
2021-12-24 06:39:55 +00:00
|
|
|
{
|
2023-11-28 16:51:13 +00:00
|
|
|
return fpm_.file_offset(loc.byte);
|
2021-12-24 06:39:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
/// WANTED
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2024-03-30 19:45:00 +00:00
|
|
|
[[nodiscard]] bool piece_is_wanted(tr_piece_index_t piece) const
|
2021-10-29 18:24:30 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return files_wanted_.piece_wanted(piece);
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 bool file_is_wanted(tr_file_index_t file) const
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return files_wanted_.file_wanted(file);
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void init_files_wanted(tr_file_index_t const* files, size_t n_files, bool wanted)
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
set_files_wanted(files, n_files, wanted, /*is_bootstrapping*/ true);
|
2021-10-29 18:24:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_files_wanted(tr_file_index_t const* files, size_t n_files, bool wanted)
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
set_files_wanted(files, n_files, wanted, /*is_bootstrapping*/ false);
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// PRIORITIES
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] tr_priority_t piece_priority(tr_piece_index_t piece) const
|
2021-10-29 18:24:30 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return file_priorities_.piece_priority(piece);
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 06:14:34 +00:00
|
|
|
void set_file_priorities(tr_file_index_t const* files, tr_file_index_t file_count, tr_priority_t priority);
|
2021-11-29 01:12:54 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_file_priority(tr_file_index_t file, tr_priority_t priority)
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2024-02-05 06:14:34 +00:00
|
|
|
if (priority != file_priorities_.file_priority(file))
|
|
|
|
{
|
|
|
|
file_priorities_.set(file, priority);
|
|
|
|
priority_changed_.emit(this, &file, 1U, priority);
|
|
|
|
set_dirty();
|
|
|
|
}
|
2021-10-29 18:24:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
/// LOCATION
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr tr_interned_string current_dir() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return current_dir_;
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr tr_interned_string download_dir() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return download_dir_;
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr tr_interned_string incomplete_dir() const noexcept
|
2021-12-23 17:16:05 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return incomplete_dir_;
|
2021-12-23 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
/// METAINFO
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& metainfo() const noexcept
|
|
|
|
{
|
|
|
|
return metainfo_;
|
|
|
|
}
|
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
/// METAINFO - FILES
|
2021-12-07 04:18:17 +00:00
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto const& files() const noexcept
|
|
|
|
{
|
|
|
|
return metainfo_.files();
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto file_count() const noexcept
|
2021-12-07 04:18:17 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.file_count();
|
2021-12-07 04:18:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto const& file_subpath(tr_file_index_t i) const
|
2021-12-07 04:18:17 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.file_subpath(i);
|
2021-12-07 04:18:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto file_size(tr_file_index_t i) const
|
2021-12-07 04:18:17 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.file_size(i);
|
2021-12-07 04:18:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_file_subpath(tr_file_index_t i, std::string_view subpath)
|
2022-01-15 19:33:57 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
metainfo_.set_file_subpath(i, subpath);
|
2022-01-15 19:33:57 +00:00
|
|
|
}
|
2022-01-02 01:25:25 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] std::optional<tr_torrent_files::FoundFile> find_file(tr_file_index_t file_index) const;
|
2022-04-02 14:06:02 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] bool has_any_local_data() const;
|
2021-12-07 04:18:17 +00:00
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
/// METAINFO - TRACKERS
|
2021-12-14 20:59:40 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto const& announce_list() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.announce_list();
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-27 22:42:31 +00:00
|
|
|
bool set_announce_list(tr_announce_list announce_list);
|
2022-02-18 17:52:01 +00:00
|
|
|
|
2023-11-27 22:42:31 +00:00
|
|
|
bool set_announce_list(std::string_view announce_list_str);
|
2022-02-18 17:52:01 +00:00
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
/// METAINFO - WEBSEEDS
|
2021-12-07 18:11:28 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto webseed_count() const noexcept
|
2021-12-07 18:11:28 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.webseed_count();
|
2021-12-07 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 21:21:40 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto const& webseed(size_t i) const
|
2021-12-07 18:11:28 +00:00
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
return metainfo_.webseed(i);
|
2021-12-07 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 15:53:20 +00:00
|
|
|
/// METAINFO - OTHER
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
[[nodiscard]] auto piece_hash(tr_piece_index_t i) const
|
|
|
|
{
|
|
|
|
return metainfo_.piece_hash(i);
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_name(std::string_view name)
|
2022-01-15 19:33:57 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
metainfo_.set_name(name);
|
2022-01-15 19:33:57 +00:00
|
|
|
}
|
2021-12-24 21:12:33 +00:00
|
|
|
|
2022-08-31 22:33:51 +00:00
|
|
|
[[nodiscard]] constexpr auto const& name() const noexcept
|
2022-01-08 23:41:05 +00:00
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
return metainfo_.name();
|
2022-01-08 23:41:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto const& info_hash() const noexcept
|
2021-12-21 22:14:15 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.info_hash();
|
2021-12-21 22:14:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto is_private() const noexcept
|
2021-12-15 15:53:20 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.is_private();
|
2021-12-15 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto is_public() const noexcept
|
2021-12-15 15:53:20 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return !this->is_private();
|
2021-12-15 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto const& info_hash_string() const noexcept
|
2022-01-08 23:41:05 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.info_hash_string();
|
2022-01-08 23:41:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto date_created() const noexcept
|
2021-12-15 15:53:20 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.date_created();
|
2021-12-15 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto torrent_file() const
|
2021-12-15 15:53:20 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.torrent_file(session->torrentDir());
|
2022-01-15 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto magnet_file() const
|
2022-02-24 21:52:29 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.magnet_file(session->torrentDir());
|
2022-02-24 21:52:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto resume_file() const
|
2022-01-15 19:33:57 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.resume_file(session->resumeDir());
|
2022-01-08 23:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 21:52:29 +00:00
|
|
|
[[nodiscard]] auto magnet() const
|
|
|
|
{
|
|
|
|
return metainfo_.magnet();
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:33:51 +00:00
|
|
|
[[nodiscard]] constexpr auto const& comment() const noexcept
|
2022-01-08 23:41:05 +00:00
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
return metainfo_.comment();
|
2022-01-08 23:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 22:33:51 +00:00
|
|
|
[[nodiscard]] constexpr auto const& creator() const noexcept
|
2022-01-08 23:41:05 +00:00
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
return metainfo_.creator();
|
2022-01-08 23:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 22:33:51 +00:00
|
|
|
[[nodiscard]] constexpr auto const& source() const noexcept
|
2022-01-08 23:41:05 +00:00
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
return metainfo_.source();
|
2021-12-15 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto info_dict_size() const noexcept
|
2021-12-24 21:12:33 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.info_dict_size();
|
2022-01-08 20:05:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto info_dict_offset() const noexcept
|
2022-01-08 20:05:38 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return metainfo_.info_dict_offset();
|
2021-12-24 21:12:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 21:31:25 +00:00
|
|
|
/// METAINFO - PIECE CHECKSUMS
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] bool ensure_piece_is_checked(tr_piece_index_t piece);
|
2021-10-29 18:24:30 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
/// METAINFO - MAGNET
|
|
|
|
|
|
|
|
void maybe_start_metadata_transfer(int64_t size) noexcept;
|
|
|
|
|
|
|
|
[[nodiscard]] std::optional<tr_metadata_piece> get_metadata_piece(int64_t piece) const;
|
|
|
|
|
|
|
|
void set_metadata_piece(int64_t piece, void const* data, size_t len);
|
|
|
|
|
|
|
|
[[nodiscard]] std::optional<int64_t> get_next_metadata_request(time_t now) noexcept;
|
|
|
|
|
|
|
|
[[nodiscard]] double get_metadata_percent() const noexcept;
|
|
|
|
|
2021-12-15 05:48:56 +00:00
|
|
|
///
|
|
|
|
|
2023-10-21 01:35:59 +00:00
|
|
|
[[nodiscard]] tr_stat stats() const;
|
|
|
|
|
2023-11-17 05:55:29 +00:00
|
|
|
[[nodiscard]] constexpr auto queue_direction() const noexcept
|
|
|
|
{
|
|
|
|
return is_done() ? TR_UP : TR_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_queued(tr_direction const dir) const noexcept
|
2021-12-16 08:49:04 +00:00
|
|
|
{
|
2023-11-17 05:55:29 +00:00
|
|
|
return is_queued_ && dir == queue_direction();
|
2021-12-16 08:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 22:24:33 +00:00
|
|
|
void set_is_queued(bool queued = true) noexcept
|
|
|
|
{
|
|
|
|
if (is_queued_ != queued)
|
|
|
|
{
|
|
|
|
is_queued_ = queued;
|
|
|
|
mark_changed();
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto allows_pex() const noexcept
|
2021-12-15 05:48:56 +00:00
|
|
|
{
|
2023-07-14 15:51:52 +00:00
|
|
|
return this->is_public() && this->session->allows_pex();
|
2021-12-15 05:48:56 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto allows_dht() const noexcept
|
2021-12-15 05:48:56 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return this->is_public() && this->session->allowsDHT();
|
2021-12-15 05:48:56 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto allows_lpd() const noexcept // local peer discovery
|
2021-12-15 05:48:56 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return this->is_public() && this->session->allowsLPD();
|
2021-12-15 05:48:56 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr bool client_can_download() const
|
2021-12-17 04:51:53 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return this->is_piece_transfer_allowed(TR_PEER_TO_CLIENT);
|
2021-12-17 04:51:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr bool client_can_upload() const
|
2021-12-17 04:51:53 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return this->is_piece_transfer_allowed(TR_CLIENT_TO_PEER);
|
2021-12-17 04:51:53 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_download_dir(std::string_view path, bool is_new_torrent = false);
|
2022-04-15 23:39:04 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void refresh_current_dir();
|
2022-04-15 23:39:04 +00:00
|
|
|
|
2023-07-05 00:47:51 +00:00
|
|
|
[[nodiscard]] constexpr auto id() const noexcept
|
2022-06-17 15:43:04 +00:00
|
|
|
{
|
|
|
|
return unique_id_;
|
|
|
|
}
|
|
|
|
|
2023-10-21 02:23:14 +00:00
|
|
|
void init_id(tr_torrent_id_t id)
|
|
|
|
{
|
|
|
|
TR_ASSERT(unique_id_ == tr_torrent_id_t{});
|
|
|
|
TR_ASSERT(id != tr_torrent_id_t{});
|
|
|
|
unique_id_ = id;
|
|
|
|
}
|
|
|
|
|
2023-10-22 03:23:08 +00:00
|
|
|
constexpr void set_date_active(time_t when) noexcept
|
2022-08-31 22:33:51 +00:00
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
this->date_active_ = when;
|
2022-08-31 22:33:51 +00:00
|
|
|
|
2023-10-22 03:23:08 +00:00
|
|
|
bump_date_changed(when);
|
2023-11-28 05:04:04 +00:00
|
|
|
set_dirty();
|
2022-08-31 22:33:51 +00:00
|
|
|
}
|
2021-12-16 09:43:51 +00:00
|
|
|
|
2022-11-22 20:00:09 +00:00
|
|
|
[[nodiscard]] constexpr auto activity() const noexcept
|
|
|
|
{
|
2023-10-19 13:39:34 +00:00
|
|
|
if (verify_state_ == VerifyState::Active)
|
2022-11-22 20:00:09 +00:00
|
|
|
{
|
|
|
|
return TR_STATUS_CHECK;
|
|
|
|
}
|
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
if (verify_state_ == VerifyState::Queued)
|
2022-11-22 20:00:09 +00:00
|
|
|
{
|
|
|
|
return TR_STATUS_CHECK_WAIT;
|
|
|
|
}
|
|
|
|
|
2023-11-17 05:55:29 +00:00
|
|
|
if (is_running())
|
2022-11-22 20:00:09 +00:00
|
|
|
{
|
2023-11-17 05:55:29 +00:00
|
|
|
return is_done() ? TR_STATUS_SEED : TR_STATUS_DOWNLOAD;
|
2022-11-22 20:00:09 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 05:55:29 +00:00
|
|
|
if (is_queued(TR_UP) && session->queueEnabled(TR_UP))
|
2022-11-22 20:00:09 +00:00
|
|
|
{
|
2023-11-17 05:55:29 +00:00
|
|
|
return TR_STATUS_SEED_WAIT;
|
|
|
|
}
|
2022-11-22 20:00:09 +00:00
|
|
|
|
2023-11-17 05:55:29 +00:00
|
|
|
if (is_queued(TR_DOWN) && session->queueEnabled(TR_DOWN))
|
|
|
|
{
|
|
|
|
return TR_STATUS_DOWNLOAD_WAIT;
|
2022-11-22 20:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TR_STATUS_STOPPED;
|
|
|
|
}
|
|
|
|
|
2023-10-23 22:11:28 +00:00
|
|
|
[[nodiscard]] constexpr auto const& labels() const noexcept
|
|
|
|
{
|
|
|
|
return labels_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_labels(labels_t const& new_labels);
|
2022-04-18 05:53:48 +00:00
|
|
|
|
2021-12-16 09:43:51 +00:00
|
|
|
/** Return the mime-type (e.g. "audio/x-flac") that matches more of the
|
|
|
|
torrent's content than any other mime-type. */
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] std::string_view primary_mime_type() const;
|
2021-12-16 09:43:51 +00:00
|
|
|
|
2024-02-05 06:14:34 +00:00
|
|
|
void set_sequential_download(bool is_sequential) noexcept
|
2023-04-14 16:47:54 +00:00
|
|
|
{
|
2024-02-05 06:14:34 +00:00
|
|
|
if (is_sequential != sequential_download_)
|
|
|
|
{
|
|
|
|
sequential_download_ = is_sequential;
|
|
|
|
sequential_download_changed_.emit(this, is_sequential);
|
2024-03-31 20:01:05 +00:00
|
|
|
set_dirty();
|
2024-02-05 06:14:34 +00:00
|
|
|
}
|
2023-04-14 16:47:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto is_sequential_download() const noexcept
|
2023-04-14 16:47:54 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return sequential_download_;
|
2023-04-14 16:47:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr bool is_running() const noexcept
|
2022-04-28 13:44:24 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return is_running_;
|
2022-04-28 13:44:24 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto is_stopping() const noexcept
|
|
|
|
{
|
|
|
|
return is_stopping_;
|
|
|
|
}
|
|
|
|
|
2023-11-15 22:24:33 +00:00
|
|
|
constexpr void stop_soon() noexcept
|
|
|
|
{
|
|
|
|
is_stopping_ = true;
|
|
|
|
}
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
void start(bool bypass_queue, std::optional<bool> has_any_local_data);
|
|
|
|
|
2023-10-22 03:23:08 +00:00
|
|
|
[[nodiscard]] constexpr auto has_changed_since(time_t when) const noexcept
|
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
return date_changed_ > when;
|
2023-10-22 03:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_bandwidth_group(std::string_view group_name) noexcept;
|
2022-04-28 13:44:24 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto get_priority() const noexcept
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return bandwidth().get_priority();
|
2022-11-03 23:08:02 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto const& bandwidth_group() const noexcept
|
2022-04-28 13:44:24 +00:00
|
|
|
{
|
|
|
|
return bandwidth_group_;
|
|
|
|
}
|
|
|
|
|
2023-10-04 01:18:55 +00:00
|
|
|
[[nodiscard]] constexpr auto peer_limit() const noexcept
|
|
|
|
{
|
|
|
|
return max_connected_peers_;
|
|
|
|
}
|
|
|
|
|
2023-11-28 05:04:04 +00:00
|
|
|
constexpr void set_peer_limit(uint16_t val) noexcept
|
|
|
|
{
|
|
|
|
if (max_connected_peers_ != val)
|
|
|
|
{
|
|
|
|
max_connected_peers_ = val;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 01:18:55 +00:00
|
|
|
// --- idleness
|
|
|
|
|
|
|
|
void set_idle_limit_mode(tr_idlelimit mode) noexcept
|
|
|
|
{
|
|
|
|
auto const is_valid = mode == TR_IDLELIMIT_GLOBAL || mode == TR_IDLELIMIT_SINGLE || mode == TR_IDLELIMIT_UNLIMITED;
|
|
|
|
TR_ASSERT(is_valid);
|
|
|
|
if (idle_limit_mode_ != mode && is_valid)
|
|
|
|
{
|
|
|
|
idle_limit_mode_ = mode;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto idle_limit_mode() const noexcept
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
|
|
|
return idle_limit_mode_;
|
|
|
|
}
|
|
|
|
|
2023-10-04 01:18:55 +00:00
|
|
|
constexpr void set_idle_limit_minutes(uint16_t idle_minutes) noexcept
|
|
|
|
{
|
|
|
|
if ((idle_limit_minutes_ != idle_minutes) && (idle_minutes > 0))
|
|
|
|
{
|
|
|
|
idle_limit_minutes_ = idle_minutes;
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto idle_limit_minutes() const noexcept
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
|
|
|
return idle_limit_minutes_;
|
|
|
|
}
|
|
|
|
|
2023-11-17 05:55:29 +00:00
|
|
|
[[nodiscard]] constexpr std::optional<size_t> idle_seconds(time_t now) const noexcept
|
|
|
|
{
|
|
|
|
auto const activity = this->activity();
|
|
|
|
|
|
|
|
if (activity == TR_STATUS_DOWNLOAD || activity == TR_STATUS_SEED)
|
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
if (auto const latest = std::max(date_started_, date_active_); latest != 0)
|
2023-11-17 05:55:29 +00:00
|
|
|
{
|
|
|
|
TR_ASSERT(now >= latest);
|
|
|
|
return now - latest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-10-04 01:18:55 +00:00
|
|
|
// --- seed ratio
|
|
|
|
|
|
|
|
constexpr void set_seed_ratio_mode(tr_ratiolimit mode) noexcept
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-10-04 01:18:55 +00:00
|
|
|
auto const is_valid = mode == TR_RATIOLIMIT_GLOBAL || mode == TR_RATIOLIMIT_SINGLE || mode == TR_RATIOLIMIT_UNLIMITED;
|
|
|
|
TR_ASSERT(is_valid);
|
|
|
|
if (seed_ratio_mode_ != mode && is_valid)
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-10-04 01:18:55 +00:00
|
|
|
seed_ratio_mode_ = mode;
|
2023-04-23 01:25:55 +00:00
|
|
|
set_dirty();
|
2022-11-03 23:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 01:18:55 +00:00
|
|
|
[[nodiscard]] constexpr auto seed_ratio_mode() const noexcept
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-10-04 01:18:55 +00:00
|
|
|
return seed_ratio_mode_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void set_seed_ratio(double desired_ratio)
|
|
|
|
{
|
|
|
|
if (static_cast<int>(seed_ratio_ * 100.0) != static_cast<int>(desired_ratio * 100.0))
|
2022-11-03 23:08:02 +00:00
|
|
|
{
|
2023-10-04 01:18:55 +00:00
|
|
|
seed_ratio_ = desired_ratio;
|
2023-04-23 01:25:55 +00:00
|
|
|
set_dirty();
|
2022-11-03 23:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 01:18:55 +00:00
|
|
|
[[nodiscard]] auto seed_ratio() const noexcept
|
|
|
|
{
|
|
|
|
return seed_ratio_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr std::optional<double> effective_seed_ratio() const noexcept
|
|
|
|
{
|
|
|
|
auto const mode = seed_ratio_mode();
|
|
|
|
|
|
|
|
if (mode == TR_RATIOLIMIT_SINGLE)
|
|
|
|
{
|
|
|
|
return seed_ratio_;
|
|
|
|
}
|
|
|
|
|
2023-10-04 21:33:04 +00:00
|
|
|
if (mode == TR_RATIOLIMIT_GLOBAL && session->isRatioLimited())
|
2023-10-04 01:18:55 +00:00
|
|
|
{
|
|
|
|
return session->desiredRatio();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2022-12-21 23:21:28 +00:00
|
|
|
void do_idle_work()
|
|
|
|
{
|
2024-05-24 20:50:01 +00:00
|
|
|
do_magnet_idle_work();
|
|
|
|
|
2022-12-21 23:21:28 +00:00
|
|
|
if (needs_completeness_check_)
|
|
|
|
{
|
|
|
|
needs_completeness_check_ = false;
|
2023-04-23 01:25:55 +00:00
|
|
|
recheck_completeness();
|
2022-12-21 23:21:28 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
if (is_stopping_)
|
2022-12-21 23:21:28 +00:00
|
|
|
{
|
|
|
|
tr_torrentStop(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-01 19:22:50 +00:00
|
|
|
[[nodiscard]] constexpr auto announce_key() const noexcept
|
|
|
|
{
|
|
|
|
return announce_key_;
|
|
|
|
}
|
|
|
|
|
2023-03-16 01:53:48 +00:00
|
|
|
[[nodiscard]] constexpr tr_peer_id_t const& peer_id() const noexcept
|
|
|
|
{
|
|
|
|
return peer_id_;
|
|
|
|
}
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
void on_block_received(tr_block_index_t block);
|
|
|
|
|
2023-10-09 02:33:34 +00:00
|
|
|
[[nodiscard]] constexpr auto& error() noexcept
|
|
|
|
{
|
|
|
|
return error_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& error() const noexcept
|
|
|
|
{
|
|
|
|
return error_;
|
|
|
|
}
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
void stop_if_seed_limit_reached();
|
2023-11-24 01:52:53 +00:00
|
|
|
|
2023-11-15 22:24:33 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto obfuscated_hash_equals(tr_sha1_digest_t const& test) const noexcept
|
|
|
|
{
|
|
|
|
return obfuscated_hash_ == test;
|
|
|
|
}
|
|
|
|
|
2023-11-27 07:27:57 +00:00
|
|
|
// --- queue position
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto queue_position() const noexcept
|
|
|
|
{
|
|
|
|
return queue_position_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_unique_queue_position(size_t const new_pos);
|
|
|
|
|
|
|
|
static inline constexpr struct
|
|
|
|
{
|
|
|
|
constexpr bool operator()(tr_torrent const* a, tr_torrent const* b) const noexcept
|
|
|
|
{
|
|
|
|
return a->queue_position_ < b->queue_position_;
|
|
|
|
}
|
|
|
|
} CompareQueuePosition{};
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2023-07-04 20:47:18 +00:00
|
|
|
libtransmission::SimpleObservable<tr_torrent*, bool /*because_downloaded_last_piece*/> done_;
|
|
|
|
libtransmission::SimpleObservable<tr_torrent*, tr_piece_index_t> got_bad_piece_;
|
|
|
|
libtransmission::SimpleObservable<tr_torrent*, tr_piece_index_t> piece_completed_;
|
|
|
|
libtransmission::SimpleObservable<tr_torrent*> doomed_;
|
|
|
|
libtransmission::SimpleObservable<tr_torrent*> got_metainfo_;
|
|
|
|
libtransmission::SimpleObservable<tr_torrent*> started_;
|
|
|
|
libtransmission::SimpleObservable<tr_torrent*> stopped_;
|
|
|
|
libtransmission::SimpleObservable<tr_torrent*> swarm_is_all_seeds_;
|
2024-02-05 06:14:34 +00:00
|
|
|
libtransmission::SimpleObservable<tr_torrent*, tr_file_index_t const*, tr_file_index_t, tr_priority_t> priority_changed_;
|
|
|
|
libtransmission::SimpleObservable<tr_torrent*, bool> sequential_download_changed_;
|
2023-07-04 20:47:18 +00:00
|
|
|
|
2023-11-16 00:53:43 +00:00
|
|
|
CumulativeCount bytes_corrupt_;
|
|
|
|
CumulativeCount bytes_downloaded_;
|
|
|
|
CumulativeCount bytes_uploaded_;
|
|
|
|
|
2023-10-05 02:38:14 +00:00
|
|
|
tr_session* session = nullptr;
|
|
|
|
|
|
|
|
tr_torrent_announcer* torrent_announcer = nullptr;
|
|
|
|
|
|
|
|
tr_swarm* swarm = nullptr;
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
time_t lpdAnnounceAt = 0;
|
2007-12-25 05:37:32 +00:00
|
|
|
|
2021-11-12 16:42:51 +00:00
|
|
|
private:
|
2024-01-03 03:04:17 +00:00
|
|
|
friend bool tr_torrentSetMetainfoFromFile(tr_torrent* tor, tr_torrent_metainfo const* metainfo, char const* filename);
|
2023-10-21 18:21:05 +00:00
|
|
|
friend tr_file_view tr_torrentFile(tr_torrent const* tor, tr_file_index_t file);
|
2023-10-04 21:33:04 +00:00
|
|
|
friend tr_stat const* tr_torrentStat(tr_torrent* tor);
|
2023-10-19 13:39:34 +00:00
|
|
|
friend tr_torrent* tr_torrentNew(tr_ctor* ctor, tr_torrent** setme_duplicate_of);
|
2023-10-21 18:21:05 +00:00
|
|
|
friend uint64_t tr_torrentGetBytesLeftToAllocate(tr_torrent const* tor);
|
2023-11-15 22:24:33 +00:00
|
|
|
friend void tr_torrentFreeInSessionThread(tr_torrent* tor);
|
|
|
|
friend void tr_torrentRemove(tr_torrent* tor, bool delete_flag, tr_fileFunc delete_func, void* user_data);
|
2023-11-26 02:00:20 +00:00
|
|
|
friend void tr_torrentSetDownloadDir(tr_torrent* tor, char const* path);
|
2023-11-28 05:04:04 +00:00
|
|
|
friend void tr_torrentSetPriority(tr_torrent* tor, tr_priority_t priority);
|
|
|
|
friend void tr_torrentStart(tr_torrent* tor);
|
|
|
|
friend void tr_torrentStartNow(tr_torrent* tor);
|
2023-11-15 22:24:33 +00:00
|
|
|
friend void tr_torrentStop(tr_torrent* tor);
|
2023-11-28 05:04:04 +00:00
|
|
|
friend void tr_torrentUseSessionLimits(tr_torrent* tor, bool enabled);
|
2023-11-25 22:20:17 +00:00
|
|
|
friend void tr_torrentVerify(tr_torrent* tor);
|
2023-10-19 13:39:34 +00:00
|
|
|
|
|
|
|
enum class VerifyState : uint8_t
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Queued,
|
|
|
|
Active
|
|
|
|
};
|
2023-10-04 21:33:04 +00:00
|
|
|
|
2023-10-09 02:33:34 +00:00
|
|
|
// Tracks a torrent's error state, either local (e.g. file IO errors)
|
|
|
|
// or tracker errors (e.g. warnings returned by a tracker).
|
|
|
|
class Error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
[[nodiscard]] constexpr auto empty() const noexcept
|
|
|
|
{
|
|
|
|
return error_type_ == TR_STAT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto error_type() const noexcept
|
|
|
|
{
|
|
|
|
return error_type_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& announce_url() const noexcept
|
|
|
|
{
|
|
|
|
return announce_url_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& errmsg() const noexcept
|
|
|
|
{
|
|
|
|
return errmsg_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_tracker_warning(tr_interned_string announce_url, std::string_view errmsg);
|
|
|
|
void set_tracker_error(tr_interned_string announce_url, std::string_view errmsg);
|
|
|
|
void set_local_error(std::string_view errmsg);
|
|
|
|
|
|
|
|
void clear() noexcept;
|
|
|
|
void clear_if_tracker() noexcept;
|
|
|
|
|
|
|
|
private:
|
|
|
|
tr_interned_string announce_url_; // the source for tracker errors/warnings
|
|
|
|
std::string errmsg_;
|
|
|
|
tr_stat_errtype error_type_ = TR_STAT_OK;
|
|
|
|
};
|
|
|
|
|
2023-10-04 22:40:46 +00:00
|
|
|
// Helper class to smooth out speed estimates.
|
|
|
|
// Used to prevent temporary speed changes from skewing the ETA too much.
|
|
|
|
class SimpleSmoothedSpeed
|
|
|
|
{
|
|
|
|
public:
|
2023-11-10 23:12:24 +00:00
|
|
|
constexpr auto update(uint64_t time_msec, Speed speed)
|
2023-10-04 22:40:46 +00:00
|
|
|
{
|
|
|
|
// If the old speed is too old, just replace it
|
|
|
|
if (timestamp_msec_ + MaxAgeMSec <= time_msec)
|
|
|
|
{
|
|
|
|
timestamp_msec_ = time_msec;
|
2023-11-10 23:12:24 +00:00
|
|
|
speed_ = speed;
|
2023-10-04 22:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// To prevent the smoothing from being overwhelmed by frequent calls
|
|
|
|
// to update(), do nothing if not enough time elapsed since last update.
|
|
|
|
else if (timestamp_msec_ + MinUpdateMSec <= time_msec)
|
|
|
|
{
|
|
|
|
timestamp_msec_ = time_msec;
|
2023-11-10 23:12:24 +00:00
|
|
|
speed_ = (speed_ * 4U + speed) / 5U;
|
2023-10-04 22:40:46 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 23:12:24 +00:00
|
|
|
return speed_;
|
2023-10-04 22:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static auto constexpr MaxAgeMSec = 4000U;
|
|
|
|
static auto constexpr MinUpdateMSec = 800U;
|
|
|
|
|
|
|
|
uint64_t timestamp_msec_ = {};
|
2023-11-10 23:12:24 +00:00
|
|
|
Speed speed_ = {};
|
2023-10-04 22:40:46 +00:00
|
|
|
};
|
|
|
|
|
2023-11-23 05:41:12 +00:00
|
|
|
[[nodiscard]] constexpr auto seconds_downloading(time_t now) const noexcept
|
|
|
|
{
|
|
|
|
auto n_secs = seconds_downloading_before_current_start_;
|
|
|
|
|
|
|
|
if (is_running())
|
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
if (date_done_ > date_started_)
|
2023-11-23 05:41:12 +00:00
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
n_secs += date_done_ - date_started_;
|
2023-11-23 05:41:12 +00:00
|
|
|
}
|
2023-11-24 01:52:53 +00:00
|
|
|
else if (date_done_ == 0)
|
2023-11-23 05:41:12 +00:00
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
n_secs += now - date_started_;
|
2023-11-23 05:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n_secs;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto seconds_seeding(time_t now) const noexcept
|
|
|
|
{
|
|
|
|
auto n_secs = seconds_seeding_before_current_start_;
|
|
|
|
|
|
|
|
if (is_running())
|
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
if (date_done_ > date_started_)
|
2023-11-23 05:41:12 +00:00
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
n_secs += now - date_done_;
|
2023-11-23 05:41:12 +00:00
|
|
|
}
|
2023-11-24 01:52:53 +00:00
|
|
|
else if (date_done_ != 0)
|
2023-11-23 05:41:12 +00:00
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
n_secs += now - date_started_;
|
2023-11-23 05:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n_secs;
|
|
|
|
}
|
|
|
|
|
2023-10-30 06:32:29 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 bool is_piece_checked(tr_piece_index_t piece) const
|
|
|
|
{
|
|
|
|
return checked_pieces_.test(piece);
|
|
|
|
}
|
|
|
|
|
2023-12-02 20:16:36 +00:00
|
|
|
[[nodiscard]] bool check_piece(tr_piece_index_t piece) const;
|
2023-10-30 06:32:29 +00:00
|
|
|
|
2023-10-04 01:18:55 +00:00
|
|
|
[[nodiscard]] constexpr std::optional<uint16_t> effective_idle_limit_minutes() const noexcept
|
|
|
|
{
|
|
|
|
auto const mode = idle_limit_mode();
|
|
|
|
|
|
|
|
if (mode == TR_IDLELIMIT_SINGLE)
|
|
|
|
{
|
|
|
|
return idle_limit_minutes();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == TR_IDLELIMIT_GLOBAL && session->isIdleLimited())
|
|
|
|
{
|
|
|
|
return session->idleLimitMinutes();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
[[nodiscard]] constexpr std::optional<size_t> idle_seconds_left(time_t now) const noexcept
|
|
|
|
{
|
|
|
|
auto const idle_limit_minutes = effective_idle_limit_minutes();
|
|
|
|
if (!idle_limit_minutes)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const idle_seconds = this->idle_seconds(now);
|
|
|
|
if (!idle_seconds)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const idle_limit_seconds = size_t{ *idle_limit_minutes } * 60U;
|
|
|
|
return idle_limit_seconds > *idle_seconds ? idle_limit_seconds - *idle_seconds : 0U;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr bool is_piece_transfer_allowed(tr_direction direction) const noexcept
|
2023-01-02 17:34:36 +00:00
|
|
|
{
|
2023-11-16 04:15:40 +00:00
|
|
|
if (uses_speed_limit(direction) && speed_limit(direction).is_zero())
|
2023-01-02 17:34:36 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
if (uses_session_limits())
|
2023-01-02 17:34:36 +00:00
|
|
|
{
|
2023-11-16 04:15:40 +00:00
|
|
|
if (auto const limit = session->active_speed_limit(direction); limit && limit->is_zero())
|
2023-01-02 17:34:36 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
constexpr void set_needs_completeness_check() noexcept
|
|
|
|
{
|
|
|
|
needs_completeness_check_ = true;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_files_wanted(tr_file_index_t const* files, size_t n_files, bool wanted, bool is_bootstrapping)
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
|
|
|
auto const lock = unique_lock();
|
|
|
|
|
|
|
|
files_wanted_.set(files, n_files, wanted);
|
2023-11-26 02:00:20 +00:00
|
|
|
completion_.invalidate_size_when_done();
|
2021-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
if (!is_bootstrapping)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
set_dirty();
|
|
|
|
recheck_completeness();
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-21 23:21:28 +00:00
|
|
|
|
2023-10-22 03:23:08 +00:00
|
|
|
constexpr void bump_date_changed(time_t when)
|
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
if (date_changed_ < when)
|
2023-10-22 03:23:08 +00:00
|
|
|
{
|
2023-11-24 01:52:53 +00:00
|
|
|
date_changed_ = when;
|
2023-10-22 03:23:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
void set_verify_state(VerifyState state);
|
|
|
|
|
2023-10-30 06:32:29 +00:00
|
|
|
[[nodiscard]] constexpr std::optional<float> verify_progress() const noexcept
|
|
|
|
{
|
|
|
|
if (verify_state_ == VerifyState::Active)
|
|
|
|
{
|
|
|
|
return verify_progress_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-11-27 22:42:31 +00:00
|
|
|
// must be called after the torrent's announce list changes.
|
|
|
|
void on_announce_list_changed();
|
|
|
|
|
2023-12-06 04:06:27 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto byte_span_for_file(tr_file_index_t file) const
|
2023-11-28 05:04:04 +00:00
|
|
|
{
|
2023-12-06 04:06:27 +00:00
|
|
|
return fpm_.byte_span_for_file(file);
|
2023-11-28 05:04:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
bool use_metainfo_from_file(tr_torrent_metainfo const* metainfo, char const* filename, tr_error* error);
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
// ---
|
|
|
|
|
|
|
|
void set_has_piece(tr_piece_index_t piece, bool has)
|
|
|
|
{
|
|
|
|
completion_.set_has_piece(piece, has);
|
|
|
|
}
|
|
|
|
|
2023-11-28 05:04:04 +00:00
|
|
|
void mark_changed();
|
|
|
|
void mark_edited();
|
|
|
|
|
|
|
|
constexpr void set_dirty(bool dirty = true) noexcept
|
|
|
|
{
|
|
|
|
is_dirty_ = dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto is_dirty() const noexcept
|
|
|
|
{
|
|
|
|
return is_dirty_;
|
|
|
|
}
|
|
|
|
|
2023-11-26 21:43:37 +00:00
|
|
|
void init(tr_ctor const& ctor);
|
2023-11-26 02:00:20 +00:00
|
|
|
|
2023-10-21 18:21:05 +00:00
|
|
|
void on_metainfo_updated();
|
2023-11-24 01:52:53 +00:00
|
|
|
void on_metainfo_completed();
|
2024-01-03 03:04:17 +00:00
|
|
|
void on_have_all_metainfo();
|
2023-11-26 02:00:20 +00:00
|
|
|
void on_piece_completed(tr_piece_index_t piece);
|
|
|
|
void on_piece_failed(tr_piece_index_t piece);
|
|
|
|
void on_file_completed(tr_file_index_t file);
|
|
|
|
void on_tracker_response(tr_tracker_event const* event);
|
|
|
|
|
2023-11-28 16:51:13 +00:00
|
|
|
void create_empty_files() const;
|
2023-11-26 02:00:20 +00:00
|
|
|
void recheck_completeness();
|
|
|
|
|
2024-05-24 20:50:01 +00:00
|
|
|
void do_magnet_idle_work();
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] bool use_new_metainfo(tr_error* error);
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
void set_location_in_session_thread(std::string_view path, bool move_from_old_path, int volatile* setme_state);
|
|
|
|
|
2023-11-28 05:04:04 +00:00
|
|
|
void rename_path_in_session_thread(
|
|
|
|
std::string_view oldpath,
|
|
|
|
std::string_view newname,
|
|
|
|
tr_torrent_rename_done_func callback,
|
|
|
|
void* const callback_user_data);
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
void start_in_session_thread();
|
2023-10-21 18:21:05 +00:00
|
|
|
|
2023-11-15 22:24:33 +00:00
|
|
|
void stop_now();
|
|
|
|
|
2023-11-24 01:52:53 +00:00
|
|
|
[[nodiscard]] bool is_new_torrent_a_seed();
|
|
|
|
|
2023-10-21 01:35:59 +00:00
|
|
|
tr_stat stats_ = {};
|
|
|
|
|
2023-10-09 02:33:34 +00:00
|
|
|
Error error_;
|
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
VerifyDoneCallback verify_done_callback_;
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
// true iff the piece was verified more recently than any of the piece's
|
|
|
|
// files' mtimes (file_mtimes_). If checked_pieces_.test(piece) is false,
|
|
|
|
// it means that piece needs to be checked before its data is used.
|
|
|
|
tr_bitfield checked_pieces_ = tr_bitfield{ 0 };
|
|
|
|
|
2023-10-23 22:11:28 +00:00
|
|
|
labels_t labels_;
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
tr_torrent_metainfo metainfo_;
|
|
|
|
|
2024-01-03 03:04:17 +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 */
|
|
|
|
std::unique_ptr<tr_metadata_download> metadata_download_;
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
tr_bandwidth bandwidth_;
|
|
|
|
|
|
|
|
tr_completion completion_;
|
|
|
|
|
|
|
|
tr_file_piece_map fpm_ = tr_file_piece_map{ metainfo_ };
|
|
|
|
|
|
|
|
// when Transmission thinks the torrent's files were last changed
|
|
|
|
std::vector<time_t> file_mtimes_;
|
|
|
|
|
2023-10-05 02:38:14 +00:00
|
|
|
tr_interned_string bandwidth_group_;
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
// Where the files are when the torrent is complete.
|
|
|
|
tr_interned_string download_dir_;
|
|
|
|
|
|
|
|
// Where the files are when the torrent is incomplete.
|
|
|
|
// a value of TR_KEY_NONE indicates the 'incomplete_dir' feature is unused
|
|
|
|
tr_interned_string incomplete_dir_;
|
|
|
|
|
|
|
|
// Where the files are now.
|
|
|
|
// Will equal either download_dir or incomplete_dir
|
|
|
|
tr_interned_string current_dir_;
|
|
|
|
|
2023-11-15 22:24:33 +00:00
|
|
|
tr_sha1_digest_t obfuscated_hash_ = {};
|
|
|
|
|
2023-10-21 01:35:59 +00:00
|
|
|
mutable SimpleSmoothedSpeed eta_speed_;
|
2023-10-04 22:40:46 +00:00
|
|
|
|
2023-10-21 18:21:05 +00:00
|
|
|
tr_files_wanted files_wanted_{ &fpm_ };
|
|
|
|
tr_file_priorities file_priorities_{ &fpm_ };
|
|
|
|
|
2023-03-16 01:53:48 +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.
|
|
|
|
*/
|
|
|
|
tr_peer_id_t peer_id_ = tr_peerIdInit();
|
|
|
|
|
2023-11-27 07:27:57 +00:00
|
|
|
size_t queue_position_ = 0;
|
|
|
|
|
2023-11-24 01:52:53 +00:00
|
|
|
time_t date_active_ = 0;
|
|
|
|
time_t date_added_ = 0;
|
|
|
|
time_t date_changed_ = 0;
|
|
|
|
time_t date_done_ = 0;
|
|
|
|
time_t date_edited_ = 0;
|
|
|
|
time_t date_started_ = 0;
|
2023-10-22 03:23:08 +00:00
|
|
|
|
2023-11-23 05:41:12 +00:00
|
|
|
time_t seconds_downloading_before_current_start_ = 0;
|
|
|
|
time_t seconds_seeding_before_current_start_ = 0;
|
|
|
|
|
2023-10-04 01:18:55 +00:00
|
|
|
float verify_progress_ = -1.0F;
|
|
|
|
float seed_ratio_ = 0.0F;
|
|
|
|
|
2023-01-01 19:22:50 +00:00
|
|
|
tr_announce_key_t announce_key_ = tr_rand_obj<tr_announce_key_t>();
|
|
|
|
|
2023-10-21 02:23:14 +00:00
|
|
|
tr_torrent_id_t unique_id_ = 0;
|
|
|
|
|
2023-10-04 01:18:55 +00:00
|
|
|
tr_ratiolimit seed_ratio_mode_ = TR_RATIOLIMIT_GLOBAL;
|
|
|
|
|
|
|
|
tr_idlelimit idle_limit_mode_ = TR_IDLELIMIT_GLOBAL;
|
|
|
|
|
2023-10-19 13:39:34 +00:00
|
|
|
VerifyState verify_state_ = VerifyState::None;
|
2023-10-05 02:38:14 +00:00
|
|
|
|
2023-11-28 05:04:04 +00:00
|
|
|
tr_completeness completeness_ = TR_LEECH;
|
|
|
|
|
2023-10-05 02:38:14 +00:00
|
|
|
uint16_t idle_limit_minutes_ = 0;
|
|
|
|
|
2023-11-28 05:04:04 +00:00
|
|
|
uint16_t max_connected_peers_ = TR_DEFAULT_PEER_LIMIT_TORRENT;
|
|
|
|
|
2023-11-15 22:24:33 +00:00
|
|
|
bool is_deleting_ = false;
|
|
|
|
bool is_dirty_ = false;
|
|
|
|
bool is_queued_ = false;
|
2023-11-26 02:00:20 +00:00
|
|
|
bool is_running_ = false;
|
2023-11-15 22:24:33 +00:00
|
|
|
bool is_stopping_ = false;
|
|
|
|
|
2023-11-26 02:00:20 +00:00
|
|
|
bool finished_seeding_by_idle_ = false;
|
|
|
|
|
2022-12-21 23:21:28 +00:00
|
|
|
bool needs_completeness_check_ = true;
|
2023-04-14 16:47:54 +00:00
|
|
|
|
|
|
|
bool sequential_download_ = false;
|
2023-11-28 05:04:04 +00:00
|
|
|
|
|
|
|
// start the torrent after all the startup scaffolding is done,
|
|
|
|
// e.g. fetching metadata from peers and/or verifying the torrent
|
|
|
|
bool start_when_stable_ = false;
|
2007-12-25 05:37:32 +00:00
|
|
|
};
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
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
|
|
|
{
|
2022-08-14 14:16:08 +00:00
|
|
|
return tor != nullptr && tor->session != nullptr;
|
2009-01-30 00:41:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 22:39:06 +00:00
|
|
|
#define tr_logAddCriticalTor(tor, msg) tr_logAddCritical(msg, (tor)->name())
|
|
|
|
#define tr_logAddErrorTor(tor, msg) tr_logAddError(msg, (tor)->name())
|
|
|
|
#define tr_logAddWarnTor(tor, msg) tr_logAddWarn(msg, (tor)->name())
|
|
|
|
#define tr_logAddInfoTor(tor, msg) tr_logAddInfo(msg, (tor)->name())
|
|
|
|
#define tr_logAddDebugTor(tor, msg) tr_logAddDebug(msg, (tor)->name())
|
|
|
|
#define tr_logAddTraceTor(tor, msg) tr_logAddTrace(msg, (tor)->name())
|