perf: do not use tr_variant when parsing .torrent files

This commit is contained in:
Charles Kerr 2022-05-23 23:05:16 -05:00 committed by GitHub
parent 19db28c04d
commit fe288b45e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 646 additions and 350 deletions

View File

@ -10,6 +10,7 @@
#include <cstddef> // size_t
#include <cstdint> // int64_t
#include <optional>
#include <string>
#include <string_view>
#include <utility> // make_pair
@ -129,6 +130,19 @@ struct BasicHandler : public Handler
return key(depth());
}
protected:
[[nodiscard]] std::string path() const
{
auto ret = std::string{};
for (size_t i = 0; i <= depth(); ++i)
{
ret += '[';
ret += key(i);
ret += ']';
}
return ret;
}
private:
constexpr void push() noexcept
{

View File

@ -161,12 +161,6 @@ bool tr_sys_path_copy(char const* src_path, char const* dst_path, struct tr_erro
*/
bool tr_sys_path_get_info(char const* path, int flags, tr_sys_path_info* info, struct tr_error** error = nullptr);
template<typename T, typename = std::enable_if<std::is_member_function_pointer<decltype(&T::c_str)>::value>>
bool tr_sys_path_get_info(T const& path, int flags, tr_sys_path_info* info, struct tr_error** error = nullptr)
{
return tr_sys_path_get_info(path.c_str(), flags, info, error);
}
/**
* @brief Portability wrapper for `access()`.
*

View File

@ -3,6 +3,7 @@
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <algorithm>
#include <array>
#include <cstring>
#include <iterator> // back_inserter
@ -181,6 +182,23 @@ tr_urlbuf tr_magnet_metainfo::magnet() const
return s;
}
void tr_magnet_metainfo::addWebseed(std::string_view webseed)
{
if (!tr_urlIsValid(webseed))
{
return;
}
auto& urls = webseed_urls_;
auto const it = std::find(std::begin(urls), std::end(urls), webseed);
if (it != std::end(urls))
{
return;
}
urls.emplace_back(std::string{ webseed });
}
bool tr_magnet_metainfo::parseMagnet(std::string_view magnet_link, tr_error** error)
{
magnet_link = tr_strvStrip(magnet_link);

View File

@ -20,6 +20,8 @@ struct tr_variant;
class tr_magnet_metainfo
{
friend struct MetainfoHandler;
public:
bool parseMagnet(std::string_view magnet_link, tr_error** error = nullptr);
@ -65,6 +67,8 @@ public:
name_ = name;
}
void addWebseed(std::string_view webseed);
protected:
tr_announce_list announce_list_;
std::vector<std::string> webseed_urls_;

View File

@ -58,6 +58,17 @@ public:
files_.at(file_index).setPath(path);
}
void insertSubpathPrefix(std::string_view path)
{
auto const buf = tr_pathbuf{ path, '/' };
for (auto& file : files_)
{
file.path_.insert(0, buf.sv());
file.path_.shrink_to_fit();
}
}
void reserve(size_t n_files)
{
files_.reserve(n_files);
@ -165,7 +176,11 @@ private:
public:
void setPath(std::string_view subpath)
{
path_ = subpath;
if (path_ != subpath)
{
path_ = subpath;
path_.shrink_to_fit();
}
}
file_t(std::string_view path, uint64_t size)

View File

@ -16,6 +16,7 @@
#include "transmission.h"
#include "benc.h"
#include "crypto-utils.h"
#include "error-types.h"
#include "error.h"
@ -24,8 +25,8 @@
#include "quark.h"
#include "torrent-metainfo.h"
#include "tr-assert.h"
#include "tr-strbuf.h"
#include "utils.h"
#include "variant.h"
#include "web-utils.h"
using namespace std::literals;
@ -155,329 +156,512 @@ std::string tr_torrent_metainfo::fixWebseedUrl(tr_torrent_metainfo const& tm, st
return std::string{ url };
}
void tr_torrent_metainfo::parseWebseeds(tr_torrent_metainfo& setme, tr_variant* meta)
{
setme.webseed_urls_.clear();
static auto constexpr MaxBencDepth = 32;
auto url = std::string_view{};
tr_variant* urls = nullptr;
if (tr_variantDictFindList(meta, TR_KEY_url_list, &urls))
{
size_t const n = tr_variantListSize(urls);
setme.webseed_urls_.reserve(n);
for (size_t i = 0; i < n; ++i)
{
if (tr_variantGetStrView(tr_variantListChild(urls, i), &url) && tr_urlIsValid(url))
{
setme.webseed_urls_.push_back(fixWebseedUrl(setme, url));
}
}
}
else if (tr_variantDictFindStrView(meta, TR_KEY_url_list, &url) && tr_urlIsValid(url)) // handle single items in webseeds
{
setme.webseed_urls_.push_back(fixWebseedUrl(setme, url));
}
bool tr_error_is_set(tr_error const* const* error)
{
return (error != nullptr) && (*error != nullptr);
}
bool tr_torrent_metainfo::parsePath(std::string_view root, tr_variant* path, std::string& setme)
struct MetainfoHandler final : public transmission::benc::BasicHandler<MaxBencDepth>
{
if (!tr_variantIsList(path))
using BasicHandler = transmission::benc::BasicHandler<MaxBencDepth>;
tr_torrent_metainfo& tm_;
int64_t piece_size_ = 0;
int64_t length_ = 0;
std::string encoding_ = "UTF-8";
std::string_view info_dict_begin_;
tr_tracker_tier_t tier_ = 0;
tr_pathbuf file_subpath_;
std::string_view pieces_root_;
int64_t file_length_ = 0;
enum class State
{
UsePath,
FileTree,
Files,
FilesIgnored,
PieceLayers,
};
State state_ = State::UsePath;
explicit MetainfoHandler(tr_torrent_metainfo& tm)
: tm_{ tm }
{
return false;
}
setme = root;
for (size_t i = 0, n = tr_variantListSize(path); i < n; ++i)
bool Key(std::string_view key, Context const& context) override
{
auto raw = std::string_view{};
return BasicHandler::Key(key, context);
}
if (!tr_variantGetStrView(tr_variantListChild(path, i), &raw))
bool StartDict(Context const& context) override
{
if (state_ == State::FileTree)
{
if (!std::empty(file_subpath_))
{
file_subpath_ += '/';
}
tr_torrent_files::makeSubpathPortable(currentKey(), file_subpath_);
}
else if (pathIs(InfoKey))
{
info_dict_begin_ = context.raw();
tm_.info_dict_offset_ = context.tokenSpan().first;
}
else if (pathIs(InfoKey, FileTreeKey))
{
state_ = State::FileTree;
file_subpath_.clear();
file_length_ = 0;
}
else if (pathIs(PieceLayersKey))
{
state_ = State::PieceLayers;
}
return BasicHandler::StartDict(context);
}
bool EndDict(Context const& context) override
{
BasicHandler::EndDict(context);
if (pathIs(InfoKey))
{
return finishInfoDict(context);
}
if (depth() == 0) // top
{
return finish(context);
}
if (state_ == State::FileTree) // bittorrent v2 format
{
if (!addFile(context))
{
return false;
}
file_subpath_.popdir();
if (file_subpath_ == "."sv)
{
file_subpath_.clear();
}
if (pathIs(InfoKey, FileTreeKey))
{
state_ = State::UsePath;
}
}
else if (state_ == State::Files) // bittorrent v1 format
{
if (!addFile(context))
{
return false;
}
file_subpath_.clear();
}
return depth() > 0;
}
bool StartArray(Context const& context) override
{
if (pathIs(InfoKey, FilesKey))
{
state_ = std::empty(tm_.files_) ? State::Files : State::FilesIgnored;
file_subpath_.clear();
file_length_ = 0;
}
return BasicHandler::StartArray(context);
}
bool EndArray(Context const& context) override
{
BasicHandler::EndArray(context);
if ((state_ == State::Files || state_ == State::FilesIgnored) && currentKey() == FilesKey) // bittorrent v1 format
{
state_ = State::UsePath;
return true;
}
if (depth() == 2 && key(1) == AnnounceListKey)
{
++tier_;
}
return true;
}
bool Int64(int64_t value, Context const& /*context*/) override
{
auto unhandled = bool{ false };
if (state_ == State::FilesIgnored)
{
// no-op
}
else if (state_ == State::FileTree || state_ == State::Files)
{
if (currentKey() == LengthKey)
{
file_length_ = value;
}
else if (pathIs(InfoKey, FilesKey, ""sv, MtimeKey))
{
// unused by Transmission
}
else
{
unhandled = true;
}
}
else if (pathIs(CreationDateKey))
{
tm_.date_created_ = value;
}
else if (pathIs(PrivateKey) || pathIs(InfoKey, PrivateKey))
{
tm_.is_private_ = value != 0;
}
else if (pathIs(PieceLengthKey) || pathIs(InfoKey, PieceLengthKey))
{
piece_size_ = value;
}
else if (pathIs(InfoKey, LengthKey))
{
length_ = value;
}
else if (pathIs(InfoKey, MetaVersionKey))
{
// currently unused. TODO support for bittorrent v2
// TODO https://github.com/transmission/transmission/issues/458
}
else if (
pathIs(DurationKey) || pathIs(EncodedRateKey) || pathIs(HeightKey) || pathIs(InfoKey, EntropyKey) ||
pathIs(ProfilesKey, HeightKey) || pathIs(ProfilesKey, WidthKey) || pathIs(WidthKey) ||
pathStartsWith(AzureusPropertiesKey) || pathStartsWith(InfoKey, FileDurationKey) ||
pathStartsWith(InfoKey, FileMediaKey) || pathStartsWith(InfoKey, ProfilesKey))
{
// unused by Transmission
}
else
{
unhandled = true;
}
if (unhandled)
{
tr_logAddWarn(fmt::format("unexpected: path '{}', int '{}'", path(), value));
}
return true;
}
bool String(std::string_view value, Context const& context) override
{
auto const curdepth = depth();
auto const current_key = currentKey();
auto unhandled = bool{ false };
if (state_ == State::FilesIgnored)
{
// no-op
}
else if (state_ == State::FileTree)
{
if (current_key == AttrKey || current_key == PiecesRootKey)
{
// currently unused. TODO support for bittorrent v2
// TODO https://github.com/transmission/transmission/issues/458
}
else
{
unhandled = true;
}
}
else if (state_ == State::Files)
{
if (curdepth > 1 && key(curdepth - 1) == PathKey)
{
if (!std::empty(file_subpath_))
{
file_subpath_ += '/';
}
tr_torrent_files::makeSubpathPortable(value, file_subpath_);
}
else if (current_key == AttrKey)
{
// currently unused. TODO support for bittorrent v2
// TODO https://github.com/transmission/transmission/issues/458
}
else
{
unhandled = true;
}
}
else if (pathIs(CommentKey) || pathIs(CommentUtf8Key))
{
tr_strvUtf8Clean(value, tm_.comment_);
}
else if (pathIs(CreatedByKey) || pathIs(CreatedByUtf8Key))
{
tr_strvUtf8Clean(value, tm_.creator_);
}
else if (pathIs(SourceKey) || pathIs(InfoKey, SourceKey) || pathIs(PublisherKey) || pathIs(InfoKey, PublisherKey))
{
// “publisher” is rare, but used by BitComet and appears
// to have the same use as the 'source' key
// http://wiki.bitcomet.com/inside_bitcomet
tr_strvUtf8Clean(value, tm_.source_);
}
else if (pathIs(AnnounceKey))
{
tm_.announceList().add(value, tier_);
}
else if (pathIs(EncodingKey))
{
encoding_ = tr_strvStrip(value);
}
else if (pathIs(UrlListKey))
{
tm_.addWebseed(value);
}
else if (pathIs(InfoKey, NameKey) || pathIs(InfoKey, NameUtf8Key))
{
tr_strvUtf8Clean(value, tm_.name_);
}
else if (pathIs(InfoKey, PiecesKey))
{
auto const n = std::size(value) / sizeof(tr_sha1_digest_t);
tm_.pieces_.resize(n);
std::copy_n(std::data(value), std::size(value), reinterpret_cast<char*>(std::data(tm_.pieces_)));
tm_.pieces_offset_ = context.tokenSpan().first;
}
else if (pathStartsWith(PieceLayersKey))
{
// currently unused. TODO support for bittorrent v2
// TODO https://github.com/transmission/transmission/issues/458
}
else if (pathStartsWith(AnnounceListKey))
{
tm_.announceList().add(value, tier_);
}
else if (
pathIs(ChecksumKey) || pathIs(InfoKey, FilesKey, ""sv, MtimeKey) || pathIs(InfoKey, PublisherUrlKey) ||
pathIs(PublisherUrlKey) || pathStartsWith(AzureusPropertiesKey) || pathStartsWith(InfoKey, ProfilesKey))
{
// unused by Transmission
}
else if (curdepth == 2 && (pathStartsWith(HttpSeedsKey) || pathStartsWith(UrlListKey)))
{
tm_.addWebseed(value);
}
else
{
unhandled = true;
}
if (unhandled)
{
tr_logAddWarn(fmt::format("unexpected: path '{}', str '{}'", path(), value));
}
return true;
}
private:
template<typename... Args>
[[nodiscard]] bool pathStartsWith(Args... args) const noexcept
{
auto i = 1U;
return (depth() >= sizeof...(args)) && ((key(i++) == args) && ...);
}
template<typename... Args>
[[nodiscard]] bool pathIs(Args... args) const noexcept
{
auto i = 1U;
return (depth() == sizeof...(args)) && ((key(i++) == args) && ...);
}
[[nodiscard]] bool addFile(Context const& context)
{
bool ok = true;
if (file_length_ == 0)
{
return ok;
}
// FIXME: Check to see if we already added this file. This is a safeguard
// for hybrid torrents with duplicate info between "file tree" and "files"
if (std::empty(file_subpath_))
{
tr_error_set(context.error, EINVAL, fmt::format("invalid path [{:s}]", file_subpath_));
ok = false;
}
else
{
tm_.files_.add(file_subpath_, file_length_);
}
file_length_ = 0;
pieces_root_ = {};
// NB: let caller decide how to clear file_tree_.
// if we're in "files" mode we clear it; if in "file tree" we pop it
return ok;
}
bool finishInfoDict(Context const& context)
{
if (std::empty(info_dict_begin_))
{
tr_error_set(context.error, EINVAL, "no info_dict found");
return false;
}
if (!std::empty(raw))
auto root = tr_pathbuf{};
tr_torrent_files::makeSubpathPortable(tm_.name_, root);
if (!std::empty(root))
{
setme += TR_PATH_DELIMITER;
setme += raw;
tm_.files_.insertSubpathPrefix(root);
}
}
auto const sanitized = tr_torrent_files::makeSubpathPortable(setme);
if (std::size(sanitized) <= std::size(root))
{
return false;
}
tr_strvUtf8Clean(sanitized, setme);
return true;
}
std::string_view tr_torrent_metainfo::parseFiles(tr_torrent_metainfo& setme, tr_variant* info_dict, uint64_t* setme_total_size)
{
auto total_size = uint64_t{ 0 };
setme.files_.clear();
auto const root_name = tr_torrent_files::makeSubpathPortable(setme.name_);
if (std::empty(root_name))
{
return "invalid name"sv;
}
// bittorrent 1.0 spec
// http://bittorrent.org/beps/bep_0003.html
//
// "There is also a key length or a key files, but not both or neither.
//
// "If length is present then the download represents a single file,
// otherwise it represents a set of files which go in a directory structure.
// In the single file case, length maps to the length of the file in bytes.
auto len = int64_t{};
tr_variant* files_entry = nullptr;
if (tr_variantDictFindInt(info_dict, TR_KEY_length, &len))
{
total_size = len;
setme.files_.add(root_name, len);
}
// "For the purposes of the other keys, the multi-file case is treated as
// only having a single file by concatenating the files in the order they
// appear in the files list. The files list is the value files maps to,
// and is a list of dictionaries containing the following keys:
// length - The length of the file, in bytes.
// path - A list of UTF-8 encoded strings corresponding to subdirectory
// names, the last of which is the actual file name (a zero length list
// is an error case).
// In the multifile case, the name key is the name of a directory."
else if (tr_variantDictFindList(info_dict, TR_KEY_files, &files_entry))
{
auto buf = std::string{};
buf.reserve(1024); // arbitrary
auto const n_files = size_t{ tr_variantListSize(files_entry) };
setme.files_.reserve(n_files);
for (size_t i = 0; i < n_files; ++i)
{
auto* const file_entry = tr_variantListChild(files_entry, i);
if (!tr_variantIsDict(file_entry))
{
return "'files' is not a dictionary";
}
if (!tr_variantDictFindInt(file_entry, TR_KEY_length, &len))
{
return "length";
}
tr_variant* path_variant = nullptr;
if (!tr_variantDictFindList(file_entry, TR_KEY_path_utf_8, &path_variant) &&
!tr_variantDictFindList(file_entry, TR_KEY_path, &path_variant))
{
return "path";
}
if (!parsePath(root_name, path_variant, buf))
{
return "path";
}
setme.files_.add(buf, len);
total_size += len;
}
}
else
{
// TODO: add support for 'file tree' BitTorrent 2 torrents / hybrid torrents.
// Patches welcomed!
// https://www.bittorrent.org/beps/bep_0052.html#info-dictionary
return "'info' dict has neither 'files' nor 'length' key";
}
*setme_total_size = total_size;
return {};
}
// https://www.bittorrent.org/beps/bep_0012.html
std::string_view tr_torrent_metainfo::parseAnnounce(tr_torrent_metainfo& setme, tr_variant* meta)
{
setme.announce_list_.clear();
auto url = std::string_view{};
// announce-list
// example: d['announce-list'] = [ [tracker1], [backup1], [backup2] ]
if (tr_variant* tiers = nullptr; tr_variantDictFindList(meta, TR_KEY_announce_list, &tiers))
{
for (size_t i = 0, n_tiers = tr_variantListSize(tiers); i < n_tiers; ++i)
{
tr_variant* tier_list = tr_variantListChild(tiers, i);
if (tier_list == nullptr)
{
continue;
}
for (size_t j = 0, jn = tr_variantListSize(tier_list); j < jn; ++j)
{
if (!tr_variantGetStrView(tr_variantListChild(tier_list, j), &url))
{
continue;
}
setme.announce_list_.add(url, i);
}
}
}
// single 'announce' url
if (std::empty(setme.announce_list_) && tr_variantDictFindStrView(meta, TR_KEY_announce, &url))
{
setme.announce_list_.add(url, 0);
}
return {};
}
std::string_view tr_torrent_metainfo::parseImpl(tr_torrent_metainfo& setme, tr_variant* meta, std::string_view benc)
{
int64_t i = 0;
auto sv = std::string_view{};
// info_hash: urlencoded 20-byte SHA1 hash of the value of the info key
// from the Metainfo file. Note that the value will be a bencoded
// dictionary, given the definition of the info key above.
tr_variant* info_dict = nullptr;
if (tr_variantDictFindDict(meta, TR_KEY_info, &info_dict))
{
// Calculate the hash of the `info` dict.
// This is the torrent's unique ID and is central to everything.
auto const info_dict_benc = tr_variantToStr(info_dict, TR_VARIANT_FMT_BENC);
TR_ASSERT(info_dict_begin_[0] == 'd');
TR_ASSERT(context.raw().back() == 'e');
char const* const begin = &info_dict_begin_.front();
char const* const end = &context.raw().back() + 1;
auto const info_dict_benc = std::string_view{ begin, size_t(end - begin) };
auto const hash = tr_sha1(info_dict_benc);
if (!hash)
{
return "bad info_dict checksum";
tr_error_set(context.error, EINVAL, "bad info_dict checksum");
}
setme.info_hash_ = *hash;
setme.info_hash_str_ = tr_sha1_to_string(setme.info_hash_);
tm_.info_hash_ = *hash;
tm_.info_hash_str_ = tr_sha1_to_string(tm_.info_hash_);
tm_.info_dict_size_ = std::size(info_dict_benc);
// Remember the offset and length of the bencoded info dict.
// This is important when providing metainfo to magnet peers
// (see http://bittorrent.org/beps/bep_0009.html for details).
return true;
}
bool finish(Context const& context)
{
// bittorrent 1.0 spec
// http://bittorrent.org/beps/bep_0003.html
//
// Calculating this later from scratch is kind of expensive,
// so do it here since we've already got the bencoded info dict.
auto const it = std::search(std::begin(benc), std::end(benc), std::begin(info_dict_benc), std::end(info_dict_benc));
setme.info_dict_offset_ = std::distance(std::begin(benc), it);
setme.info_dict_size_ = std::size(info_dict_benc);
// "There is also a key length or a key files, but not both or neither.
//
// "If length is present then the download represents a single file,
// otherwise it represents a set of files which go in a directory structure.
// In the single file case, length maps to the length of the file in bytes.
if (tm_.fileCount() == 0 && length_ != 0 && !std::empty(tm_.name_))
{
tm_.files_.add(tm_.name_, length_);
}
// In addition, remember the offset of the pieces dictionary entry.
// This will be useful when we load piece checksums on demand.
auto constexpr Key = "6:pieces"sv;
auto const pit = std::search(std::begin(benc), std::end(benc), std::begin(Key), std::end(Key));
setme.pieces_offset_ = std::distance(std::begin(benc), pit) + std::size(Key);
}
else
{
return "missing 'info' dictionary";
if (tm_.fileCount() == 0)
{
if (!tr_error_is_set(context.error))
{
tr_error_set(context.error, EINVAL, "no files found");
}
return false;
}
if (piece_size_ == 0)
{
if (!tr_error_is_set(context.error))
{
tr_error_set(context.error, EINVAL, fmt::format("invalid piece size: {}", piece_size_));
}
return false;
}
tm_.block_info_.initSizes(tm_.files_.totalSize(), piece_size_);
return true;
}
// name
if (tr_variantDictFindStrView(info_dict, TR_KEY_name_utf_8, &sv) || tr_variantDictFindStrView(info_dict, TR_KEY_name, &sv))
{
tr_strvUtf8Clean(sv, setme.name_);
}
else
{
return "'info' dictionary has neither 'name.utf-8' nor 'name'";
}
// comment (optional)
setme.comment_.clear();
if (tr_variantDictFindStrView(meta, TR_KEY_comment_utf_8, &sv) || tr_variantDictFindStrView(meta, TR_KEY_comment, &sv))
{
tr_strvUtf8Clean(sv, setme.comment_);
}
// created by (optional)
setme.creator_.clear();
if (tr_variantDictFindStrView(meta, TR_KEY_created_by_utf_8, &sv) ||
tr_variantDictFindStrView(meta, TR_KEY_created_by, &sv))
{
tr_strvUtf8Clean(sv, setme.creator_);
}
// creation date (optional)
setme.date_created_ = tr_variantDictFindInt(meta, TR_KEY_creation_date, &i) ? i : 0;
// private (optional)
setme.is_private_ = (tr_variantDictFindInt(info_dict, TR_KEY_private, &i) ||
tr_variantDictFindInt(meta, TR_KEY_private, &i)) &&
(i != 0);
// source (optional)
setme.source_.clear();
if (tr_variantDictFindStrView(info_dict, TR_KEY_source, &sv) || tr_variantDictFindStrView(meta, TR_KEY_source, &sv))
{
tr_strvUtf8Clean(sv, setme.source_);
}
// piece length
if (!tr_variantDictFindInt(info_dict, TR_KEY_piece_length, &i) || (i <= 0) || (i > UINT32_MAX))
{
return "'info' dict 'piece length' is missing or has an invalid value";
}
auto const piece_size = (uint32_t)i;
// pieces
if (!tr_variantDictFindStrView(info_dict, TR_KEY_pieces, &sv) || (std::size(sv) % sizeof(tr_sha1_digest_t) != 0))
{
return "'info' dict 'pieces' is missing or has an invalid value";
}
auto const n = std::size(sv) / sizeof(tr_sha1_digest_t);
setme.pieces_.resize(n);
std::copy_n(std::data(sv), std::size(sv), reinterpret_cast<char*>(std::data(setme.pieces_)));
// files
auto total_size = uint64_t{ 0 };
if (auto const errstr = parseFiles(setme, info_dict, &total_size); !std::empty(errstr))
{
return errstr;
}
if (std::empty(setme.files_))
{
return "no files found"sv;
}
// do the size and piece size match up?
setme.block_info_.initSizes(total_size, piece_size);
if (setme.block_info_.pieceCount() != std::size(setme.pieces_))
{
return "piece count and file sizes do not match";
}
parseAnnounce(setme, meta);
parseWebseeds(setme, meta);
return {};
}
static constexpr std::string_view AcodecKey = "acodec"sv;
static constexpr std::string_view AnnounceKey = "announce"sv;
static constexpr std::string_view AnnounceListKey = "announce-list"sv;
static constexpr std::string_view AttrKey = "attr"sv;
static constexpr std::string_view AzureusPropertiesKey = "azureus_properties"sv;
static constexpr std::string_view ChecksumKey = "checksum"sv;
static constexpr std::string_view CommentKey = "comment"sv;
static constexpr std::string_view CommentUtf8Key = "comment.utf-8"sv;
static constexpr std::string_view CreatedByKey = "created by"sv;
static constexpr std::string_view CreatedByUtf8Key = "created by.utf-8"sv;
static constexpr std::string_view CreationDateKey = "creation date"sv;
static constexpr std::string_view DurationKey = "duration"sv;
static constexpr std::string_view EncodedRateKey = "encoded rate"sv;
static constexpr std::string_view EncodingKey = "encoding"sv;
static constexpr std::string_view EntropyKey = "entropy"sv;
static constexpr std::string_view FileDurationKey = "file-duration"sv;
static constexpr std::string_view FileMediaKey = "file-media"sv;
static constexpr std::string_view FileTreeKey = "file tree"sv;
static constexpr std::string_view FilesKey = "files"sv;
static constexpr std::string_view HeightKey = "height"sv;
static constexpr std::string_view HttpSeedsKey = "httpseeds"sv;
static constexpr std::string_view InfoKey = "info"sv;
static constexpr std::string_view LengthKey = "length"sv;
static constexpr std::string_view Md5sumKey = "md5sum"sv;
static constexpr std::string_view MetaVersionKey = "meta version"sv;
static constexpr std::string_view MtimeKey = "mtime"sv;
static constexpr std::string_view NameKey = "name"sv;
static constexpr std::string_view NameUtf8Key = "name.utf-8"sv;
static constexpr std::string_view PathKey = "path"sv;
static constexpr std::string_view PieceLayersKey = "piece layers"sv;
static constexpr std::string_view PieceLengthKey = "piece length"sv;
static constexpr std::string_view PiecesKey = "pieces"sv;
static constexpr std::string_view PiecesRootKey = "pieces root"sv;
static constexpr std::string_view PrivateKey = "private"sv;
static constexpr std::string_view ProfilesKey = "profiles"sv;
static constexpr std::string_view PublisherKey = "publisher"sv;
static constexpr std::string_view PublisherUrlKey = "publisher-url"sv;
static constexpr std::string_view SourceKey = "source"sv;
static constexpr std::string_view UrlListKey = "url-list"sv;
static constexpr std::string_view VcodecKey = "vcodec"sv;
static constexpr std::string_view WidthKey = "width"sv;
};
bool tr_torrent_metainfo::parseBenc(std::string_view benc, tr_error** error)
{
auto top = tr_variant{};
if (!tr_variantFromBuf(&top, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, benc, nullptr, error))
auto stack = transmission::benc::ParserStack<MaxBencDepth>{};
auto handler = MetainfoHandler{ *this };
tr_error* my_error = nullptr;
if (error == nullptr)
{
error = &my_error;
}
auto const ok = transmission::benc::parse(benc, stack, handler, nullptr, error);
if (tr_error_is_set(error))
{
tr_logAddError(fmt::format("{} ({})", (*error)->message, (*error)->code));
}
tr_error_clear(&my_error);
if (!ok)
{
return false;
}
auto const errmsg = parseImpl(*this, &top, benc);
tr_variantFree(&top);
if (!std::empty(errmsg))
if (std::empty(name_))
{
tr_error_set(error, TR_ERROR_EINVAL, fmt::format(FMT_STRING("Error parsing metainfo: {:s}"), errmsg));
return false;
// TODO from first file
}
return true;
@ -509,8 +693,8 @@ tr_pathbuf tr_torrent_metainfo::makeFilename(
{
// `${dirname}/${name}.${info_hash}${suffix}`
// `${dirname}/${info_hash}${suffix}`
return format == BasenameFormat::Hash ? tr_pathbuf{ dirname, "/"sv, info_hash_string, suffix } :
tr_pathbuf{ dirname, "/"sv, name, "."sv, info_hash_string.substr(0, 16), suffix };
return format == BasenameFormat::Hash ? tr_pathbuf{ dirname, '/', info_hash_string, suffix } :
tr_pathbuf{ dirname, '/', name, '.', info_hash_string.substr(0, 16), suffix };
}
bool tr_torrent_metainfo::migrateFile(
@ -555,6 +739,9 @@ void tr_torrent_metainfo::removeFile(
std::string_view info_hash_string,
std::string_view suffix)
{
tr_sys_path_remove(makeFilename(dirname, name, info_hash_string, BasenameFormat::NameAndPartialHash, suffix));
tr_sys_path_remove(makeFilename(dirname, name, info_hash_string, BasenameFormat::Hash, suffix));
auto filename = makeFilename(dirname, name, info_hash_string, BasenameFormat::NameAndPartialHash, suffix);
tr_sys_path_remove(filename, nullptr);
filename = makeFilename(dirname, name, info_hash_string, BasenameFormat::Hash, suffix);
tr_sys_path_remove(filename, nullptr);
}

View File

@ -54,6 +54,7 @@ public:
{
return files().path(i);
}
void setFileSubpath(tr_file_index_t i, std::string_view subpath)
{
files_.setPath(i, subpath);
@ -181,12 +182,13 @@ public:
std::string_view suffix);
private:
static bool parsePath(std::string_view root, tr_variant* path, std::string& setme);
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);
static std::string fixWebseedUrl(tr_torrent_metainfo const& tm, std::string_view url);
static std::string_view parseFiles(tr_torrent_metainfo& setme, tr_variant* info_dict, uint64_t* setme_total_size);
static std::string_view parseImpl(tr_torrent_metainfo& setme, tr_variant* meta, std::string_view benc);
static std::string_view parseAnnounce(tr_torrent_metainfo& setme, tr_variant* meta);
static void parseWebseeds(tr_torrent_metainfo& setme, tr_variant* meta);
// 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);
enum class BasenameFormat
{

View File

@ -149,7 +149,7 @@ uint8_t* tr_loadFile(std::string_view path_in, size_t* size, tr_error** error)
/* try to stat the file */
auto info = tr_sys_path_info{};
tr_error* my_error = nullptr;
if (!tr_sys_path_get_info(path, 0, &info, &my_error))
if (!tr_sys_path_get_info(path.c_str(), 0, &info, &my_error))
{
tr_logAddError(fmt::format(
_("Couldn't read '{path}': {error} ({error_code})"),

View File

@ -21,8 +21,9 @@
#include "transmission.h"
#include "net.h"
#include "web-utils.h"
#include "tr-strbuf.h"
#include "utils.h"
#include "web-utils.h"
using namespace std::literals;
@ -251,6 +252,14 @@ bool tr_isValidTrackerScheme(std::string_view scheme)
return std::find(std::begin(Schemes), std::end(Schemes), scheme) != std::end(Schemes);
}
bool isAsciiLowerCase(std::string_view host)
{
return std::all_of(
std::begin(host),
std::end(host),
[](unsigned char ch) { return (ch < 128) && (std::islower(ch) != 0); });
}
// www.example.com -> example
// www.example.co.uk -> example
// 127.0.0.1 -> 127.0.0.1
@ -262,16 +271,25 @@ std::string_view getSiteName(std::string_view host)
return host;
}
// psl needs a zero-terminated hostname
auto const szhost = tr_urlbuf{ host };
// is it an IP?
auto addr = tr_address{};
auto const szhost = std::string(host);
if (tr_address_from_string(&addr, szhost.c_str()))
if (tr_address_from_string(&addr, std::data(szhost)))
{
return host;
}
// is it a registered name?
if (char* lower = nullptr; psl_str_to_utf8lower(szhost.c_str(), nullptr, nullptr, &lower) == PSL_SUCCESS)
if (isAsciiLowerCase(host))
{
if (char const* const top = psl_registrable_domain(psl_builtin(), std::data(szhost)); top != nullptr)
{
host.remove_prefix(top - std::data(szhost));
}
}
else if (char* lower = nullptr; psl_str_to_utf8lower(std::data(szhost), nullptr, nullptr, &lower) == PSL_SUCCESS)
{
// www.example.com -> example.com
if (char const* const top = psl_registrable_domain(psl_builtin(), lower); top != nullptr)

View File

@ -0,0 +1 @@
d8:announce31:http://www.example.com/announce10:created by16:buildtorrent/0.813:creation datei1646282405e4:infod5:filesld6:lengthi14e4:pathl5:worldeee4:name5:hello12:piece lengthi262144e6:pieces20:é —+<2B>U¢ï¨µÁVô‡¨):e8:url-listl27:http://www.webseed-one.com/23:http://webseed-two.com/ee

View File

@ -0,0 +1 @@
d8:announce31:http://www.example.com/announce10:created by16:buildtorrent/0.813:creation datei1646282405e4:infod5:filesld6:lengthi14e4:pathl5:worldeee4:name5:hello12:piece lengthi262144e6:pieces20:é —+<2B>U¢ï¨µÁVô‡¨):e8:url-list27:http://www.webseed-one.com/e

View File

@ -127,7 +127,8 @@ protected:
}
auto const path_part = std::string{ path, size_t(slash_pos - path + 1) };
if (!tr_sys_path_get_info(path_part, TR_SYS_PATH_NO_FOLLOW, &info) ||
if (!tr_sys_path_get_info(path_part.c_str(), TR_SYS_PATH_NO_FOLLOW, &info) ||
(info.type != TR_SYS_PATH_IS_FILE && info.type != TR_SYS_PATH_IS_DIRECTORY))
{
return false;
@ -182,32 +183,6 @@ protected:
}
}
static void testPathXname(
XnameTestData const* data,
size_t data_size,
std::string_view (*func)(std::string_view, tr_error**))
{
for (size_t i = 0; i < data_size; ++i)
{
tr_error* err = nullptr;
auto const name = func(data[i].input, &err);
std::cerr << __FILE__ << ':' << __LINE__ << " in [" << data[i].input << "] out [" << name << ']' << std::endl;
if (data[i].output != nullptr)
{
EXPECT_NE(""sv, name);
EXPECT_EQ(nullptr, err) << *err;
EXPECT_EQ(std::string{ data[i].output }, name);
}
else
{
EXPECT_EQ(""sv, name);
EXPECT_NE(nullptr, err);
tr_error_clear(&err);
}
}
}
static void testDirReadImpl(tr_pathbuf const& path, bool* have1, bool* have2)
{
*have1 = *have2 = false;

View File

@ -222,5 +222,38 @@ TEST_F(TorrentMetainfoTest, ctorSaveContents)
tr_ctorFree(ctor);
}
TEST_F(TorrentMetainfoTest, HoffmanStyleWebseeds)
{
auto const src_filename = tr_pathbuf{ LIBTRANSMISSION_TEST_ASSETS_DIR, "/debian-11.2.0-amd64-DVD-1.iso.torrent"sv };
auto tm = tr_torrent_metainfo{};
EXPECT_TRUE(tm.parseTorrentFile(src_filename));
EXPECT_EQ(2, tm.webseedCount());
EXPECT_EQ(
"https://cdimage.debian.org/cdimage/release/11.2.0//srv/cdbuilder.debian.org/dst/deb-cd/weekly-builds/amd64/iso-dvd/debian-11.2.0-amd64-DVD-1.iso"sv,
tm.webseed(0));
EXPECT_EQ(
"https://cdimage.debian.org/cdimage/archive/11.2.0//srv/cdbuilder.debian.org/dst/deb-cd/weekly-builds/amd64/iso-dvd/debian-11.2.0-amd64-DVD-1.iso"sv,
tm.webseed(1));
}
TEST_F(TorrentMetainfoTest, GetRightStyleWebseedList)
{
auto const src_filename = tr_pathbuf{ LIBTRANSMISSION_TEST_ASSETS_DIR, "/webseed-getright-list.torrent"sv };
auto tm = tr_torrent_metainfo{};
EXPECT_TRUE(tm.parseTorrentFile(src_filename));
EXPECT_EQ(2, tm.webseedCount());
EXPECT_EQ("http://www.webseed-one.com/"sv, tm.webseed(0));
EXPECT_EQ("http://webseed-two.com/"sv, tm.webseed(1));
}
TEST_F(TorrentMetainfoTest, GetRightStyleWebseedString)
{
auto const src_filename = tr_pathbuf{ LIBTRANSMISSION_TEST_ASSETS_DIR, "/webseed-getright-string.torrent"sv };
auto tm = tr_torrent_metainfo{};
EXPECT_TRUE(tm.parseTorrentFile(src_filename));
EXPECT_EQ(1, tm.webseedCount());
EXPECT_EQ("http://www.webseed-one.com/"sv, tm.webseed(0));
}
} // namespace test
} // namespace libtransmission

View File

@ -10,6 +10,7 @@ function(AddShowTest name file_basename)
)
endfunction()
AddShowTest(transmission-show-ubuntu ubuntu-20.04.3-desktop-amd64.iso)
AddShowTest(transmission-show-thor Thor_and_the_Amazon_Women.avi)
AddShowTest(transmission-show-bittorrent-v2 bittorrent-v2-test)
AddShowTest(transmission-show-inner-sanctum Inner_Sanctum_movie_archive)
AddShowTest(transmission-show-thor Thor_and_the_Amazon_Women.avi)
AddShowTest(transmission-show-ubuntu ubuntu-20.04.3-desktop-amd64.iso)

View File

@ -0,0 +1,31 @@
Name: bittorrent-v2-test
File: assets/bittorrent-v2-test.torrent
GENERAL
Name: bittorrent-v2-test
Hash: f987ab6bb50f831a861c3754ecd1b47dc2cf2e30
Created by: libtorrent
Created on: Thu May 21 21:40:57 2020
Piece Count: 366
Piece Size: 4.00 MiB
Total Size: 1.53 GB
Privacy: Public torrent
TRACKERS
FILES
bittorrent-v2-test/13.Popsy Team - ViP 2.vob.mp4 (27.55 MB)
bittorrent-v2-test/Chameleon by ASD (female voice).mov (91.86 MB)
bittorrent-v2-test/Darkroom (Stellar, 1994, Amiga ECS) HQ.mp4 (6.54 MB)
bittorrent-v2-test/Struct by Outracks (FullHD 1080p HQ demoscene).mov (141.1 MB)
bittorrent-v2-test/asd-rupture.mp4 (263.7 MB)
bittorrent-v2-test/cncd_fairlight-ceasefire_(all_falls_down)-1080p.mp4 (342.2 MB)
bittorrent-v2-test/crionics & silents - hardwired (1991, hpad, divx5).avi (135.0 MB)
bittorrent-v2-test/elevated_4000.avi (113.3 MB)
bittorrent-v2-test/luma - mercury _ 64k _ Final.mp4 (183.8 MB)
bittorrent-v2-test/readme.txt (0.06 kB)
bittorrent-v2-test/tbl-starstruck-2006.avi (229.1 MB)

Binary file not shown.

View File

@ -39,7 +39,7 @@ else()
find_program(DIFF_EXEC diff)
if (DIFF_EXEC)
message("DIFF:")
execute_process(COMMAND ${DIFF_EXEC} ${output_file} ${reference_file})
execute_process(COMMAND ${DIFF_EXEC} -u ${output_file} ${reference_file})
endif()
file(REMOVE ${output_file})

View File

@ -17,6 +17,7 @@
#include <event2/buffer.h>
#include <fmt/chrono.h>
#include <fmt/core.h>
#include <fmt/format.h>
#include <libtransmission/transmission.h>
@ -166,20 +167,21 @@ int parseCommandLine(app_opts& opts, int argc, char const* const* argv)
return now == 0 ? "Unknown" : fmt::format("{:%a %b %d %T %Y}", fmt::localtime(now));
}
bool compare_2nd_field(std::string_view const& l, std::string_view const& r)
bool compareSecondField(std::string_view l, std::string_view r)
{
auto l_ = l.find(" ");
auto r_ = r.find(" ");
if (l_ == std::string_view::npos)
auto const lpos = l.find(' ');
if (lpos == std::string_view::npos)
{
return false;
}
if (r_ == std::string_view::npos)
auto const rpos = r.find(' ');
if (rpos == std::string_view::npos)
{
return true;
}
return l.substr(l_) <= r.substr(r_);
return l.substr(lpos) <= r.substr(rpos);
}
void showInfo(app_opts const& opts, tr_torrent_metainfo const& metainfo)
@ -283,7 +285,7 @@ void showInfo(app_opts const& opts, tr_torrent_metainfo const& metainfo)
{
if (opts.show_bytesize)
{
std::sort(std::begin(filenames), std::end(filenames), compare_2nd_field);
std::sort(std::begin(filenames), std::end(filenames), compareSecondField);
}
else
{