2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2022-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-03-01 23:06:29 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#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"
|
2022-03-01 23:06:29 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
struct CompareTorrentByHash
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
tr_torrent* tr_torrents::get(std::string_view magnet_link)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent* tr_torrents::get(tr_sha1_digest_t const& hash)
|
|
|
|
{
|
|
|
|
auto [begin, end] = std::equal_range(std::begin(by_hash_), std::end(by_hash_), hash, CompareTorrentByHash{});
|
|
|
|
return begin == end ? nullptr : *begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent const* tr_torrents::get(tr_sha1_digest_t const& hash) const
|
|
|
|
{
|
|
|
|
auto [begin, end] = std::equal_range(std::cbegin(by_hash_), std::cend(by_hash_), hash, CompareTorrentByHash{});
|
|
|
|
return begin == end ? nullptr : *begin;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
by_hash_.insert(std::lower_bound(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash{}), tor);
|
|
|
|
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;
|
2022-03-01 23:06:29 +00:00
|
|
|
auto const [begin, end] = std::equal_range(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash{});
|
|
|
|
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
|
|
|
}
|