2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +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-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2023-07-04 02:57:09 +00:00
|
|
|
#include <small/set.hpp>
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2021-11-29 01:12:54 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/bitfield.h"
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/block-info.h"
|
|
|
|
#include "libtransmission/file-piece-map.h"
|
|
|
|
#include "libtransmission/torrent-metainfo.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
2021-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
void tr_file_piece_map::reset(tr_block_info const& block_info, uint64_t const* file_sizes, size_t n_files)
|
|
|
|
{
|
2021-12-24 06:39:55 +00:00
|
|
|
file_bytes_.resize(n_files);
|
|
|
|
file_bytes_.shrink_to_fit();
|
|
|
|
|
|
|
|
file_pieces_.resize(n_files);
|
|
|
|
file_pieces_.shrink_to_fit();
|
2021-11-29 01:12:54 +00:00
|
|
|
|
2023-07-04 02:57:09 +00:00
|
|
|
auto edge_pieces = small::set<tr_piece_index_t, 1024U>{};
|
|
|
|
edge_pieces.reserve(n_files * 2U);
|
2023-03-22 15:24:10 +00:00
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
uint64_t offset = 0;
|
|
|
|
for (tr_file_index_t i = 0; i < n_files; ++i)
|
|
|
|
{
|
|
|
|
auto const file_size = file_sizes[i];
|
2021-12-24 06:39:55 +00:00
|
|
|
auto const begin_byte = offset;
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const begin_piece = block_info.byte_loc(begin_byte).piece;
|
2021-12-24 06:39:55 +00:00
|
|
|
auto end_byte = tr_byte_index_t{};
|
|
|
|
auto end_piece = tr_piece_index_t{};
|
|
|
|
|
2023-03-22 15:24:10 +00:00
|
|
|
edge_pieces.insert(begin_piece);
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
if (file_size != 0)
|
|
|
|
{
|
2021-12-24 06:39:55 +00:00
|
|
|
end_byte = offset + file_size;
|
|
|
|
auto const final_byte = end_byte - 1;
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const final_piece = block_info.byte_loc(final_byte).piece;
|
2021-11-29 01:12:54 +00:00
|
|
|
end_piece = final_piece + 1;
|
2023-03-22 15:24:10 +00:00
|
|
|
|
|
|
|
edge_pieces.insert(final_piece);
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-24 06:39:55 +00:00
|
|
|
end_byte = begin_byte;
|
|
|
|
// TODO(ckerr): should end_piece == begin_piece, same as _bytes are?
|
2021-11-29 01:12:54 +00:00
|
|
|
end_piece = begin_piece + 1;
|
|
|
|
}
|
2021-12-24 06:39:55 +00:00
|
|
|
file_pieces_[i] = piece_span_t{ begin_piece, end_piece };
|
|
|
|
file_bytes_[i] = byte_span_t{ begin_byte, end_byte };
|
2021-11-29 01:12:54 +00:00
|
|
|
offset += file_size;
|
|
|
|
}
|
2023-03-22 15:24:10 +00:00
|
|
|
|
|
|
|
edge_pieces_.assign(std::begin(edge_pieces), std::end(edge_pieces));
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
void tr_file_piece_map::reset(tr_torrent_metainfo const& tm)
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const n = tm.file_count();
|
2021-11-29 01:12:54 +00:00
|
|
|
auto file_sizes = std::vector<uint64_t>(n);
|
2022-01-08 18:53:35 +00:00
|
|
|
for (tr_file_index_t i = 0; i < n; ++i)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
file_sizes[i] = tm.file_size(i);
|
2022-01-08 18:53:35 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
reset({ tm.total_size(), tm.piece_size() }, std::data(file_sizes), std::size(file_sizes));
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
tr_file_piece_map::file_span_t tr_file_piece_map::file_span(tr_piece_index_t piece) const
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2023-03-21 02:08:16 +00:00
|
|
|
constexpr auto Compare = CompareToSpan<tr_piece_index_t>{};
|
2021-12-24 06:39:55 +00:00
|
|
|
auto const begin = std::begin(file_pieces_);
|
2023-03-21 02:08:16 +00:00
|
|
|
auto const& [equal_begin, equal_end] = std::equal_range(begin, std::end(file_pieces_), piece, Compare);
|
|
|
|
return { tr_piece_index_t(equal_begin - begin), tr_piece_index_t(equal_end - begin) };
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
tr_file_piece_map::file_offset_t tr_file_piece_map::file_offset(uint64_t offset) const
|
2021-12-24 06:39:55 +00:00
|
|
|
{
|
2023-03-21 02:08:16 +00:00
|
|
|
constexpr auto Compare = CompareToSpan<uint64_t>{};
|
2021-12-24 06:39:55 +00:00
|
|
|
auto const begin = std::begin(file_bytes_);
|
2023-03-21 02:08:16 +00:00
|
|
|
auto const it = std::lower_bound(begin, std::end(file_bytes_), offset, Compare);
|
2021-12-24 06:39:55 +00:00
|
|
|
tr_file_index_t const file_index = std::distance(begin, it);
|
|
|
|
auto const file_offset = offset - it->begin;
|
|
|
|
return file_offset_t{ file_index, file_offset };
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2021-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
void tr_file_priorities::reset(tr_file_piece_map const* fpm)
|
|
|
|
{
|
|
|
|
fpm_ = fpm;
|
2022-04-26 16:11:21 +00:00
|
|
|
priorities_ = {};
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 16:11:21 +00:00
|
|
|
void tr_file_priorities::set(tr_file_index_t file, tr_priority_t new_priority)
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2022-04-26 16:11:21 +00:00
|
|
|
if (std::empty(priorities_))
|
|
|
|
{
|
|
|
|
if (new_priority == TR_PRI_NORMAL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
priorities_.assign(std::size(*fpm_), TR_PRI_NORMAL);
|
|
|
|
priorities_.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
|
|
|
priorities_[file] = new_priority;
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 16:11:21 +00:00
|
|
|
void tr_file_priorities::set(tr_file_index_t const* files, size_t n, tr_priority_t new_priority)
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
2022-04-26 16:11:21 +00:00
|
|
|
set(files[i], new_priority);
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
tr_priority_t tr_file_priorities::file_priority(tr_file_index_t file) const
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2022-04-26 16:11:21 +00:00
|
|
|
TR_ASSERT(file < std::size(*fpm_));
|
|
|
|
|
|
|
|
if (std::empty(priorities_))
|
|
|
|
{
|
|
|
|
return TR_PRI_NORMAL;
|
|
|
|
}
|
2021-12-08 16:55:52 +00:00
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
return priorities_[file];
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
tr_priority_t tr_file_priorities::piece_priority(tr_piece_index_t piece) const
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2023-03-06 21:50:19 +00:00
|
|
|
// increase priority if a file begins or ends in this piece
|
|
|
|
// because that makes life easier for code/users using at incomplete files.
|
|
|
|
// Xrefs: f2daeb242, https://forum.transmissionbt.com/viewtopic.php?t=10473
|
2023-03-22 15:24:10 +00:00
|
|
|
if (fpm_->is_edge_piece(piece))
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2023-03-06 21:50:19 +00:00
|
|
|
return TR_PRI_HIGH;
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
2023-03-06 21:50:19 +00:00
|
|
|
|
|
|
|
// check the priorities of the files that touch this piece
|
2023-04-23 01:25:55 +00:00
|
|
|
if (auto const [begin_file, end_file] = fpm_->file_span(piece); end_file <= std::size(priorities_))
|
2023-03-06 21:50:19 +00:00
|
|
|
{
|
|
|
|
auto const begin = std::begin(priorities_) + begin_file;
|
|
|
|
auto const end = std::begin(priorities_) + end_file;
|
|
|
|
if (auto const it = std::max_element(begin, end); it != end)
|
|
|
|
{
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TR_PRI_NORMAL;
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2021-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
void tr_files_wanted::reset(tr_file_piece_map const* fpm)
|
|
|
|
{
|
|
|
|
fpm_ = fpm;
|
|
|
|
wanted_ = tr_bitfield{ std::size(*fpm) };
|
2023-04-23 01:25:55 +00:00
|
|
|
wanted_.set_has_all(); // by default we want all files
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_files_wanted::set(tr_file_index_t file, bool wanted)
|
|
|
|
{
|
|
|
|
wanted_.set(file, wanted);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_files_wanted::set(tr_file_index_t const* files, size_t n, bool wanted)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
set(files[i], wanted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
bool tr_files_wanted::piece_wanted(tr_piece_index_t piece) const
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (wanted_.has_all())
|
2022-04-26 16:11:21 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const [begin, end] = fpm_->file_span(piece);
|
2021-11-29 01:12:54 +00:00
|
|
|
return wanted_.count(begin, end) != 0;
|
|
|
|
}
|