2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2005-2022 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>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
#include "block-info.h"
|
|
|
|
#include "magnet-metainfo.h"
|
2022-04-15 23:39:04 +00:00
|
|
|
#include "torrent-files.h"
|
2022-03-28 22:13:32 +00:00
|
|
|
#include "tr-strbuf.h"
|
2021-12-25 21:21:13 +00:00
|
|
|
|
|
|
|
struct tr_error;
|
|
|
|
|
|
|
|
struct tr_torrent_metainfo : public tr_magnet_metainfo
|
|
|
|
{
|
|
|
|
public:
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr auto empty() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return std::empty(files_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseBenc(std::string_view benc, tr_error** error = nullptr);
|
|
|
|
|
|
|
|
// Helper function wrapper around parseBenc().
|
|
|
|
// If you're looping through several files, passing in a non-nullptr
|
|
|
|
// `buffer` can reduce the number of memory allocations needed to
|
|
|
|
// load multiple files.
|
2022-08-03 06:15:37 +00:00
|
|
|
bool parseTorrentFile(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_;
|
|
|
|
}
|
|
|
|
[[nodiscard]] auto fileCount() const noexcept
|
|
|
|
{
|
2022-04-15 23:39:04 +00:00
|
|
|
return files().fileCount();
|
2022-04-12 15:00:02 +00:00
|
|
|
}
|
|
|
|
[[nodiscard]] auto fileSize(tr_file_index_t i) const
|
|
|
|
{
|
2022-04-15 23:39:04 +00:00
|
|
|
return files().fileSize(i);
|
2022-04-12 15:00:02 +00:00
|
|
|
}
|
|
|
|
[[nodiscard]] auto const& fileSubpath(tr_file_index_t i) const
|
|
|
|
{
|
|
|
|
return files().path(i);
|
|
|
|
}
|
2022-05-24 04:05:16 +00:00
|
|
|
|
2022-04-12 15:00:02 +00:00
|
|
|
void setFileSubpath(tr_file_index_t i, std::string_view subpath)
|
|
|
|
{
|
|
|
|
files_.setPath(i, subpath);
|
|
|
|
}
|
|
|
|
|
2022-01-07 19:13:37 +00:00
|
|
|
/// BLOCK INFO
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr auto const& blockInfo() const noexcept
|
2022-01-08 18:53:35 +00:00
|
|
|
{
|
|
|
|
return block_info_;
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr auto blockCount() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
2022-01-07 19:13:37 +00:00
|
|
|
return blockInfo().blockCount();
|
|
|
|
}
|
2022-03-18 21:31:25 +00:00
|
|
|
[[nodiscard]] auto byteLoc(uint64_t nth_byte) const
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2022-03-18 21:31:25 +00:00
|
|
|
return blockInfo().byteLoc(nth_byte);
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
2022-02-18 23:17:19 +00:00
|
|
|
[[nodiscard]] auto blockLoc(tr_block_index_t block) const
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2022-02-18 23:17:19 +00:00
|
|
|
return blockInfo().blockLoc(block);
|
|
|
|
}
|
2022-02-19 16:07:08 +00:00
|
|
|
[[nodiscard]] auto pieceLoc(tr_piece_index_t piece, uint32_t offset = 0, uint32_t length = 0) const
|
2022-02-18 23:17:19 +00:00
|
|
|
{
|
|
|
|
return blockInfo().pieceLoc(piece, offset, length);
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr auto blockSize(tr_block_index_t block) const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
|
|
|
return blockInfo().blockSize(block);
|
|
|
|
}
|
2022-04-14 01:22:59 +00:00
|
|
|
[[nodiscard]] auto blockSpanForPiece(tr_piece_index_t piece) const
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
|
|
|
return blockInfo().blockSpanForPiece(piece);
|
|
|
|
}
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr auto pieceCount() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
2022-01-07 19:13:37 +00:00
|
|
|
return blockInfo().pieceCount();
|
2021-12-25 21:21:13 +00:00
|
|
|
}
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr auto pieceSize() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
|
|
|
return blockInfo().pieceSize();
|
|
|
|
}
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr auto pieceSize(tr_piece_index_t piece) const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
|
|
|
return blockInfo().pieceSize(piece);
|
|
|
|
}
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr auto totalSize() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
|
|
|
return blockInfo().totalSize();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr auto const& isPrivate() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return is_private_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] tr_sha1_digest_t const& pieceHash(tr_piece_index_t piece) const;
|
|
|
|
|
2022-07-01 14:49:33 +00:00
|
|
|
[[nodiscard]] bool hasV1Metadata() const
|
|
|
|
{
|
|
|
|
// need 'pieces' field and 'files' or 'length'
|
|
|
|
// TODO check for 'files' or 'length'
|
|
|
|
return pieces_.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool hasV2Metadata() const
|
|
|
|
{
|
|
|
|
return is_v2_;
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr auto const& dateCreated() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return date_created_;
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr auto infoDictSize() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return info_dict_size_;
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr auto infoDictOffset() const noexcept
|
2021-12-25 21:21:13 +00:00
|
|
|
{
|
|
|
|
return info_dict_offset_;
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr auto piecesOffset() const noexcept
|
2022-01-29 21:37:42 +00:00
|
|
|
{
|
|
|
|
return pieces_offset_;
|
|
|
|
}
|
|
|
|
|
2022-04-12 15:00:02 +00:00
|
|
|
// UTILS
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
[[nodiscard]] auto torrentFile(std::string_view torrent_dir) const
|
2022-01-15 19:33:57 +00:00
|
|
|
{
|
|
|
|
return makeFilename(torrent_dir, name(), infoHashString(), BasenameFormat::Hash, ".torrent");
|
|
|
|
}
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
[[nodiscard]] auto magnetFile(std::string_view torrent_dir) const
|
2022-02-24 21:52:29 +00:00
|
|
|
{
|
|
|
|
return makeFilename(torrent_dir, name(), infoHashString(), BasenameFormat::Hash, ".magnet");
|
|
|
|
}
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
[[nodiscard]] auto resumeFile(std::string_view resume_dir) const
|
2022-01-15 19:33:57 +00:00
|
|
|
{
|
|
|
|
return makeFilename(resume_dir, name(), infoHashString(), BasenameFormat::Hash, ".resume");
|
|
|
|
}
|
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
static bool migrateFile(
|
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
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
static void removeFile(
|
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;
|
|
|
|
static bool parseImpl(tr_torrent_metainfo& setme, std::string_view benc, tr_error** error);
|
|
|
|
// static bool parsePath(std::string_view root, tr_variant* path, std::string& setme);
|
2021-12-25 21:21:13 +00:00
|
|
|
static std::string fixWebseedUrl(tr_torrent_metainfo const& tm, std::string_view url);
|
2022-05-24 04:05:16 +00:00
|
|
|
// static std::string_view parseFiles(tr_torrent_metainfo& setme, tr_variant* info_dict, uint64_t* setme_total_size);
|
|
|
|
// static std::string_view parseAnnounce(tr_torrent_metainfo& setme, tr_variant* meta);
|
|
|
|
// static void parseWebseeds(tr_torrent_metainfo& setme, tr_variant* meta);
|
2021-12-25 21:21:13 +00:00
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
enum class BasenameFormat
|
|
|
|
{
|
|
|
|
Hash,
|
|
|
|
NameAndPartialHash
|
|
|
|
};
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
[[nodiscard]] static tr_pathbuf makeFilename(
|
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);
|
|
|
|
|
2022-03-28 22:13:32 +00:00
|
|
|
auto makeFilename(std::string_view dirname, BasenameFormat format, std::string_view suffix) const
|
2022-01-15 19:33:57 +00:00
|
|
|
{
|
|
|
|
return makeFilename(dirname, name(), infoHashString(), format, suffix);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
bool is_private_ = false;
|
2022-07-01 14:49:33 +00:00
|
|
|
bool is_v2_ = false;
|
2021-12-25 21:21:13 +00:00
|
|
|
};
|