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.
|
2021-12-25 21:21:13 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cstdint> // uint32_t, uint64_t
|
2021-12-25 21:21:13 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-11-03 17:03:26 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2021-12-25 21:21:13 +00:00
|
|
|
|
2023-11-03 17:03:26 +00:00
|
|
|
#include "libtransmission/block-info.h"
|
|
|
|
#include "libtransmission/magnet-metainfo.h"
|
|
|
|
#include "libtransmission/torrent-files.h"
|
|
|
|
#include "libtransmission/tr-macros.h"
|
2021-12-25 21:21:13 +00:00
|
|
|
|
|
|
|
struct tr_error;
|
|
|
|
|
|
|
|
struct tr_torrent_metainfo : public tr_magnet_metainfo
|
|
|
|
{
|
|
|
|
public:
|
2022-12-23 21:21:40 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto empty() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return std::empty(files_);
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
bool parse_benc(std::string_view benc, tr_error* error = nullptr);
|
2021-12-25 21:21:13 +00:00
|
|
|
|
|
|
|
// Helper function wrapper around parseBenc().
|
|
|
|
// If you're looping through several files, passing in a non-nullptr
|
2023-01-23 16:26:11 +00:00
|
|
|
// `contents` can reduce the number of memory allocations needed to
|
2021-12-25 21:21:13 +00:00
|
|
|
// load multiple files.
|
2023-11-04 16:39:41 +00:00
|
|
|
bool parse_torrent_file(std::string_view benc_filename, std::vector<char>* contents = nullptr, tr_error* error = nullptr);
|
2021-12-25 21:21:13 +00:00
|
|
|
|
2022-04-12 15:00:02 +00:00
|
|
|
// FILES
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& files() const noexcept
|
|
|
|
{
|
|
|
|
return files_;
|
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto file_count() const noexcept
|
2022-04-12 15:00:02 +00:00
|
|
|
{
|
2022-04-15 23:39:04 +00:00
|
|
|
return files().fileCount();
|
2022-04-12 15:00:02 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto file_size(tr_file_index_t i) const
|
2022-04-12 15:00:02 +00:00
|
|
|
{
|
2022-04-15 23:39:04 +00:00
|
|
|
return files().fileSize(i);
|
2022-04-12 15:00:02 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto const& file_subpath(tr_file_index_t i) const
|
2022-04-12 15:00:02 +00:00
|
|
|
{
|
|
|
|
return files().path(i);
|
|
|
|
}
|
2022-05-24 04:05:16 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_file_subpath(tr_file_index_t i, std::string_view subpath)
|
2022-04-12 15:00:02 +00:00
|
|
|
{
|
|
|
|
files_.setPath(i, subpath);
|
|
|
|
}
|
|
|
|
|
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-08 18:53:35 +00:00
|
|
|
{
|
|
|
|
return block_info_;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto block_count() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return block_info().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 nth_byte) const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return block_info().byte_loc(nth_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 block_info().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 block_info().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 block_info().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 block_info().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
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return block_info().piece_count();
|
2021-12-25 21:21:13 +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 block_info().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 block_info().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 block_info().total_size();
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 15:00:02 +00:00
|
|
|
// OTHER PROPERTIES
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr auto const& comment() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return comment_;
|
|
|
|
}
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr auto const& creator() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return creator_;
|
|
|
|
}
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr auto const& source() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
|
|
|
return source_;
|
|
|
|
}
|
2021-12-25 21:21:13 +00:00
|
|
|
|
2023-07-05 00:47:51 +00:00
|
|
|
[[nodiscard]] constexpr auto is_private() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return is_private_;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 tr_sha1_digest_t const& piece_hash(tr_piece_index_t piece) const
|
2023-01-27 20:25:08 +00:00
|
|
|
{
|
|
|
|
return pieces_[piece];
|
|
|
|
}
|
2021-12-25 21:21:13 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 bool has_v1_metadata() const noexcept
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
|
|
|
// need 'pieces' field and 'files' or 'length'
|
|
|
|
// TODO check for 'files' or 'length'
|
2022-08-31 04:17:23 +00:00
|
|
|
return !std::empty(pieces_);
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr bool has_v2_metadata() const noexcept
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
|
|
|
return is_v2_;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto const& date_created() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return date_created_;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto info_dict_size() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return info_dict_size_;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto info_dict_offset() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return info_dict_offset_;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr auto pieces_offset() const noexcept
|
2022-01-29 21:37:42 +00:00
|
|
|
{
|
|
|
|
return pieces_offset_;
|
|
|
|
}
|
|
|
|
|
2022-04-12 15:00:02 +00:00
|
|
|
// UTILS
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto torrent_file(std::string_view torrent_dir) const
|
2022-01-15 19:33:57 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return make_filename(torrent_dir, name(), info_hash_string(), BasenameFormat::Hash, ".torrent");
|
2022-01-15 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto magnet_file(std::string_view torrent_dir) const
|
2022-02-24 21:52:29 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return make_filename(torrent_dir, name(), info_hash_string(), BasenameFormat::Hash, ".magnet");
|
2022-02-24 21:52:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto resume_file(std::string_view resume_dir) const
|
2022-01-15 19:33:57 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return make_filename(resume_dir, name(), info_hash_string(), BasenameFormat::Hash, ".resume");
|
2022-01-15 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
static bool migrate_file(
|
2022-01-18 02:36:41 +00:00
|
|
|
std::string_view dirname,
|
|
|
|
std::string_view name,
|
|
|
|
std::string_view info_hash_string,
|
2022-01-23 22:47:41 +00:00
|
|
|
std::string_view suffix);
|
2022-01-18 02:36:41 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
static void remove_file(
|
2022-01-15 19:33:57 +00:00
|
|
|
std::string_view dirname,
|
|
|
|
std::string_view name,
|
|
|
|
std::string_view info_hash_string,
|
|
|
|
std::string_view suffix);
|
|
|
|
|
2021-12-25 21:21:13 +00:00
|
|
|
private:
|
2022-05-24 04:05:16 +00:00
|
|
|
friend struct MetainfoHandler;
|
2023-11-04 16:39:41 +00:00
|
|
|
static bool parse_impl(tr_torrent_metainfo& setme, std::string_view benc, tr_error* error);
|
2023-04-23 01:25:55 +00:00
|
|
|
static std::string fix_webseed_url(tr_torrent_metainfo const& tm, std::string_view url);
|
2021-12-25 21:21:13 +00:00
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
enum class BasenameFormat
|
|
|
|
{
|
|
|
|
Hash,
|
|
|
|
NameAndPartialHash
|
|
|
|
};
|
|
|
|
|
2023-08-30 02:58:31 +00:00
|
|
|
[[nodiscard]] static std::string make_filename(
|
2022-01-15 19:33:57 +00:00
|
|
|
std::string_view dirname,
|
|
|
|
std::string_view name,
|
|
|
|
std::string_view info_hash_string,
|
|
|
|
BasenameFormat format,
|
|
|
|
std::string_view suffix);
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] auto make_filename(std::string_view dirname, BasenameFormat format, std::string_view suffix) const
|
2022-01-15 19:33:57 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return make_filename(dirname, name(), info_hash_string(), format, suffix);
|
2022-01-15 19:33:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 21:21:13 +00:00
|
|
|
tr_block_info block_info_ = tr_block_info{ 0, 0 };
|
|
|
|
|
2022-04-15 23:39:04 +00:00
|
|
|
tr_torrent_files files_;
|
2022-04-12 15:00:02 +00:00
|
|
|
|
2021-12-25 21:21:13 +00:00
|
|
|
std::vector<tr_sha1_digest_t> pieces_;
|
|
|
|
|
|
|
|
std::string comment_;
|
|
|
|
std::string creator_;
|
|
|
|
std::string source_;
|
|
|
|
|
|
|
|
time_t date_created_ = 0;
|
|
|
|
|
|
|
|
// Offset + size of the bencoded info dict subset of the bencoded data.
|
|
|
|
// Used when loading pieces of it to sent to magnet peers.
|
|
|
|
// See http://bittorrent.org/beps/bep_0009.html
|
|
|
|
uint64_t info_dict_size_ = 0;
|
|
|
|
uint64_t info_dict_offset_ = 0;
|
|
|
|
|
|
|
|
// Offset of the bencoded 'pieces' checksums subset of the bencoded data.
|
|
|
|
// Used when loading piece checksums on demand.
|
|
|
|
uint64_t pieces_offset_ = 0;
|
|
|
|
|
2023-02-13 00:10:54 +00:00
|
|
|
bool has_magnet_info_hash_ = false;
|
2021-12-25 21:21:13 +00:00
|
|
|
bool is_private_ = false;
|
2022-07-01 14:49:33 +00:00
|
|
|
bool is_v2_ = false;
|
2021-12-25 21:21:13 +00:00
|
|
|
};
|