2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2007-2023 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-06-18 03:40:41 +00:00
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <cstddef> // std::byte
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <cstdint>
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <future>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <utility> // std::pair
|
|
|
|
#include <vector>
|
2022-04-08 01:50:26 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/announce-list.h"
|
|
|
|
#include "libtransmission/block-info.h"
|
|
|
|
#include "libtransmission/file.h"
|
|
|
|
#include "libtransmission/torrent-files.h"
|
|
|
|
#include "libtransmission/tr-macros.h" // TR_CONSTEXPR20
|
|
|
|
#include "libtransmission/utils.h" // for tr_file_save()
|
|
|
|
|
|
|
|
struct tr_error;
|
2008-03-24 17:18:08 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
class tr_metainfo_builder
|
2021-12-14 20:59:40 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
public:
|
2022-08-06 01:37:21 +00:00
|
|
|
explicit tr_metainfo_builder(std::string_view single_file_or_parent_directory);
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
tr_metainfo_builder(tr_metainfo_builder&&) = delete;
|
|
|
|
tr_metainfo_builder(tr_metainfo_builder const&) = delete;
|
|
|
|
tr_metainfo_builder& operator=(tr_metainfo_builder&&) = delete;
|
|
|
|
tr_metainfo_builder& operator=(tr_metainfo_builder const&) = delete;
|
|
|
|
|
|
|
|
// Generate piece checksums asynchronously.
|
|
|
|
// - This must be done before calling `benc()` or `save()`.
|
|
|
|
// - Runs in a worker thread because it can be time-consuming.
|
|
|
|
// - Can be cancelled with `cancelChecksums()` and polled with `checksumStatus()`
|
|
|
|
// - Resolves with a `tr_error*` which is set on failure or nullptr on success.
|
2023-05-06 04:11:05 +00:00
|
|
|
std::future<tr_error*> make_checksums()
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
return std::async(
|
|
|
|
std::launch::async,
|
|
|
|
[this]()
|
|
|
|
{
|
|
|
|
tr_error* error = nullptr;
|
2023-05-06 04:11:05 +00:00
|
|
|
blocking_make_checksums(&error);
|
2022-08-02 19:46:08 +00:00
|
|
|
return error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the status of a `makeChecksums()` call:
|
|
|
|
// The current piece being tested and the total number of pieces in the torrent.
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr std::pair<tr_piece_index_t, tr_piece_index_t> checksum_status() const noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return std::make_pair(checksum_piece_, block_info_.piece_count());
|
2022-08-02 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the `makeChecksums()` worker thread to cleanly exit ASAP.
|
2023-05-06 04:11:05 +00:00
|
|
|
constexpr void cancel_checksums() noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
cancel_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate the metainfo
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::string benc(tr_error** error = nullptr) const;
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
// generate the metainfo and save it to a torrent file
|
|
|
|
bool save(std::string_view filename, tr_error** error = nullptr) const
|
|
|
|
{
|
2023-06-30 14:49:58 +00:00
|
|
|
return tr_file_save(filename, benc(error), error);
|
2022-08-02 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// setters
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_announce_list(tr_announce_list announce)
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
announce_ = std::move(announce);
|
|
|
|
}
|
|
|
|
|
|
|
|
// whether or not to include User-Agent and creation time
|
2023-05-06 04:11:05 +00:00
|
|
|
constexpr void set_anonymize(bool anonymize) noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
anonymize_ = anonymize;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_comment(std::string_view comment)
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
comment_ = comment;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
bool set_piece_size(uint32_t piece_size) noexcept;
|
2022-08-02 19:46:08 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
constexpr void set_private(bool is_private) noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
is_private_ = is_private;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_source(std::string_view source)
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
source_ = source;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
void set_webseeds(std::vector<std::string> webseeds)
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
webseeds_ = std::move(webseeds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getters
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto const& announce_list() const noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
return announce_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& anonymize() const noexcept
|
|
|
|
{
|
|
|
|
return anonymize_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& comment() const noexcept
|
|
|
|
{
|
|
|
|
return comment_;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto file_count() const noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
return files_.fileCount();
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto file_size(tr_file_index_t i) const noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
return files_.fileSize(i);
|
|
|
|
}
|
|
|
|
|
2023-07-05 00:47:51 +00:00
|
|
|
[[nodiscard]] constexpr auto is_private() const noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
return is_private_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto name() const noexcept
|
|
|
|
{
|
|
|
|
return tr_sys_path_basename(top_);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] auto const& path(tr_file_index_t i) const noexcept
|
|
|
|
{
|
|
|
|
return files_.path(i);
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto piece_size() const noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return block_info_.piece_size();
|
2022-08-02 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto piece_count() const noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return block_info_.piece_count();
|
2022-08-02 19:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& source() const noexcept
|
|
|
|
{
|
|
|
|
return source_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& top() const noexcept
|
|
|
|
{
|
|
|
|
return top_;
|
|
|
|
}
|
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto total_size() const noexcept
|
2022-08-02 19:46:08 +00:00
|
|
|
{
|
|
|
|
return files_.totalSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto const& webseeds() const noexcept
|
|
|
|
{
|
|
|
|
return webseeds_;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
2023-06-22 16:50:57 +00:00
|
|
|
[[nodiscard]] static uint32_t default_piece_size(uint64_t total_size) noexcept;
|
2022-08-02 19:46:08 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr static bool is_legal_piece_size(uint32_t x)
|
|
|
|
{
|
|
|
|
// It must be a power of two and at least 16KiB
|
|
|
|
auto const MinSize = uint32_t{ 1024U * 16U };
|
|
|
|
auto const is_power_of_two = (x & (x - 1)) == 0;
|
|
|
|
return x >= MinSize && is_power_of_two;
|
|
|
|
}
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
private:
|
2023-05-06 04:11:05 +00:00
|
|
|
bool blocking_make_checksums(tr_error** error = nullptr);
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
std::string top_;
|
|
|
|
tr_torrent_files files_;
|
|
|
|
tr_announce_list announce_;
|
|
|
|
tr_block_info block_info_;
|
|
|
|
std::vector<std::byte> piece_hashes_;
|
|
|
|
std::vector<std::string> webseeds_;
|
|
|
|
|
|
|
|
std::string comment_;
|
|
|
|
std::string source_;
|
|
|
|
|
|
|
|
tr_piece_index_t checksum_piece_ = 0;
|
|
|
|
|
|
|
|
bool is_private_ = false;
|
|
|
|
bool anonymize_ = false;
|
|
|
|
bool cancel_ = false;
|
2021-12-14 20:59:40 +00:00
|
|
|
};
|