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-03-01 23:06:29 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <algorithm>
|
2023-11-21 15:02:03 +00:00
|
|
|
#include <ctime>
|
2022-03-01 23:06:29 +00:00
|
|
|
#include <set>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-03-01 23:06:29 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/magnet-metainfo.h"
|
|
|
|
#include "libtransmission/torrent.h"
|
|
|
|
#include "libtransmission/torrents.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
2023-11-21 15:02:03 +00:00
|
|
|
#include "libtransmission/tr-macros.h"
|
2022-03-01 23:06:29 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2023-06-29 18:13:25 +00:00
|
|
|
constexpr struct
|
2022-03-01 23:06:29 +00:00
|
|
|
{
|
|
|
|
bool operator()(tr_sha1_digest_t const& a, tr_sha1_digest_t const& b) const
|
|
|
|
{
|
|
|
|
return a < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(tr_torrent const* a, tr_torrent const* b) const
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return (*this)(a->info_hash(), b->info_hash());
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(tr_torrent const* a, tr_sha1_digest_t const& b) const
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return (*this)(a->info_hash(), b);
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(tr_sha1_digest_t const& a, tr_torrent const* b) const
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return (*this)(a, b->info_hash());
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|
2023-06-29 18:13:25 +00:00
|
|
|
} CompareTorrentByHash{};
|
2022-03-01 23:06:29 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-11-12 17:53:04 +00:00
|
|
|
tr_torrent* tr_torrents::get(std::string_view magnet_link) const
|
2022-03-01 23:06:29 +00:00
|
|
|
{
|
|
|
|
auto magnet = tr_magnet_metainfo{};
|
2023-04-23 01:25:55 +00:00
|
|
|
return magnet.parseMagnet(magnet_link) ? get(magnet.info_hash()) : nullptr;
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-12 17:53:04 +00:00
|
|
|
tr_torrent* tr_torrents::get(tr_sha1_digest_t const& hash) const
|
2022-03-01 23:06:29 +00:00
|
|
|
{
|
2023-06-29 18:13:25 +00:00
|
|
|
auto [begin, end] = std::equal_range(std::cbegin(by_hash_), std::cend(by_hash_), hash, CompareTorrentByHash);
|
2022-03-01 23:06:29 +00:00
|
|
|
return begin == end ? nullptr : *begin;
|
|
|
|
}
|
|
|
|
|
2023-11-28 16:51:13 +00:00
|
|
|
tr_torrent* tr_torrents::find_from_obfuscated_hash(tr_sha1_digest_t const& obfuscated_hash) const
|
2023-10-24 19:24:52 +00:00
|
|
|
{
|
|
|
|
for (auto* const tor : *this)
|
|
|
|
{
|
2023-11-15 22:24:33 +00:00
|
|
|
if (tor->obfuscated_hash_equals(obfuscated_hash))
|
2023-10-24 19:24:52 +00:00
|
|
|
{
|
|
|
|
return tor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-06-17 15:43:04 +00:00
|
|
|
tr_torrent_id_t tr_torrents::add(tr_torrent* tor)
|
2022-03-01 23:06:29 +00:00
|
|
|
{
|
2022-06-17 15:43:04 +00:00
|
|
|
auto const id = static_cast<tr_torrent_id_t>(std::size(by_id_));
|
2022-03-01 23:06:29 +00:00
|
|
|
by_id_.push_back(tor);
|
2023-06-29 18:13:25 +00:00
|
|
|
by_hash_.insert(std::lower_bound(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash), tor);
|
2022-03-01 23:06:29 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void tr_torrents::remove(tr_torrent const* tor, time_t current_time)
|
2022-03-01 23:06:29 +00:00
|
|
|
{
|
|
|
|
TR_ASSERT(tor != nullptr);
|
2022-06-17 15:43:04 +00:00
|
|
|
TR_ASSERT(get(tor->id()) == tor);
|
2022-03-01 23:06:29 +00:00
|
|
|
|
2022-06-17 15:43:04 +00:00
|
|
|
by_id_[tor->id()] = nullptr;
|
2023-06-29 18:13:25 +00:00
|
|
|
auto const [begin, end] = std::equal_range(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash);
|
2022-03-01 23:06:29 +00:00
|
|
|
by_hash_.erase(begin, end);
|
2022-08-03 06:15:37 +00:00
|
|
|
removed_.emplace_back(tor->id(), current_time);
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 15:43:04 +00:00
|
|
|
std::vector<tr_torrent_id_t> tr_torrents::removedSince(time_t timestamp) const
|
2022-03-01 23:06:29 +00:00
|
|
|
{
|
2022-06-17 15:43:04 +00:00
|
|
|
auto ids = std::set<tr_torrent_id_t>{};
|
2022-03-01 23:06:29 +00:00
|
|
|
|
|
|
|
for (auto const& [id, removed_at] : removed_)
|
|
|
|
{
|
|
|
|
if (removed_at >= timestamp)
|
|
|
|
{
|
2022-04-22 13:08:02 +00:00
|
|
|
ids.insert(id);
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 13:08:02 +00:00
|
|
|
return { std::begin(ids), std::end(ids) };
|
2022-03-01 23:06:29 +00:00
|
|
|
}
|