2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2021-2022 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 <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
#include "block-info.h"
|
|
|
|
#include "file-piece-map.h"
|
2022-01-15 19:33:57 +00:00
|
|
|
#include "torrent-metainfo.h"
|
2021-12-15 21:25:42 +00:00
|
|
|
#include "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
|
|
|
|
|
|
|
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;
|
2022-02-18 23:17:19 +00:00
|
|
|
auto const begin_piece = block_info.byteLoc(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{};
|
|
|
|
|
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;
|
2022-02-18 23:17:19 +00:00
|
|
|
auto const final_piece = block_info.byteLoc(final_byte).piece;
|
2021-11-29 01:12:54 +00:00
|
|
|
end_piece = final_piece + 1;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
auto const n = tm.fileCount();
|
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)
|
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
file_sizes[i] = tm.fileSize(i);
|
2022-01-08 18:53:35 +00:00
|
|
|
}
|
2022-01-15 19:33:57 +00:00
|
|
|
reset({ tm.totalSize(), tm.pieceSize() }, std::data(file_sizes), std::size(file_sizes));
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_file_piece_map::piece_span_t tr_file_piece_map::pieceSpan(tr_file_index_t file) const
|
|
|
|
{
|
2021-12-24 06:39:55 +00:00
|
|
|
return file_pieces_[file];
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_file_piece_map::file_span_t tr_file_piece_map::fileSpan(tr_piece_index_t piece) const
|
|
|
|
{
|
2021-12-24 06:39:55 +00:00
|
|
|
auto compare = CompareToSpan<tr_piece_index_t>{};
|
|
|
|
auto const begin = std::begin(file_pieces_);
|
|
|
|
auto const& [equal_begin, equal_end] = std::equal_range(begin, std::end(file_pieces_), piece, compare);
|
2021-12-01 23:11:57 +00:00
|
|
|
return { tr_piece_index_t(std::distance(begin, equal_begin)), tr_piece_index_t(std::distance(begin, equal_end)) };
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 06:39:55 +00:00
|
|
|
tr_file_piece_map::file_offset_t tr_file_piece_map::fileOffset(uint64_t offset) const
|
|
|
|
{
|
|
|
|
auto compare = CompareToSpan<uint64_t>{};
|
|
|
|
auto const begin = std::begin(file_bytes_);
|
|
|
|
auto const it = std::lower_bound(begin, std::end(file_bytes_), offset, compare);
|
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
tr_file_priorities::tr_file_priorities(tr_file_piece_map const* fpm)
|
|
|
|
{
|
|
|
|
reset(fpm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_file_priorities::reset(tr_file_piece_map const* fpm)
|
|
|
|
{
|
|
|
|
fpm_ = fpm;
|
|
|
|
|
|
|
|
auto const n = std::size(*fpm_);
|
|
|
|
priorities_.resize(n);
|
|
|
|
priorities_.shrink_to_fit();
|
|
|
|
std::fill_n(std::begin(priorities_), n, TR_PRI_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_file_priorities::set(tr_file_index_t file, tr_priority_t priority)
|
|
|
|
{
|
|
|
|
priorities_[file] = priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_file_priorities::set(tr_file_index_t const* files, size_t n, tr_priority_t priority)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
set(files[i], priority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_priority_t tr_file_priorities::filePriority(tr_file_index_t file) const
|
|
|
|
{
|
2021-12-08 16:55:52 +00:00
|
|
|
TR_ASSERT(file < std::size(priorities_));
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
return priorities_[file];
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_priority_t tr_file_priorities::piecePriority(tr_piece_index_t piece) const
|
|
|
|
{
|
|
|
|
auto const [begin_idx, end_idx] = fpm_->fileSpan(piece);
|
|
|
|
auto const begin = std::begin(priorities_) + begin_idx;
|
|
|
|
auto const end = std::begin(priorities_) + end_idx;
|
|
|
|
auto const it = std::max_element(begin, end);
|
|
|
|
if (it == end)
|
|
|
|
{
|
|
|
|
return TR_PRI_NORMAL;
|
|
|
|
}
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void tr_files_wanted::reset(tr_file_piece_map const* fpm)
|
|
|
|
{
|
|
|
|
fpm_ = fpm;
|
|
|
|
wanted_ = tr_bitfield{ std::size(*fpm) };
|
|
|
|
wanted_.setHasAll(); // by default we want all files
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_files_wanted::fileWanted(tr_file_index_t file) const
|
|
|
|
{
|
|
|
|
return wanted_.test(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tr_files_wanted::pieceWanted(tr_piece_index_t piece) const
|
|
|
|
{
|
|
|
|
auto const [begin, end] = fpm_->fileSpan(piece);
|
|
|
|
return wanted_.count(begin, end) != 0;
|
|
|
|
}
|