mirror of
https://github.com/transmission/transmission
synced 2024-12-30 19:46:56 +00:00
2977636d58
* fix: add-explicit-keyword-to-constructor warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX1tyuB-2farvsyMPiZl&open=AX1tyuB-2farvsyMPiZl
* fix: use a structured binding declaration
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX1tyuHH2farvsyMPiZm&open=AX1tyuHH2farvsyMPiZm
* Fix implicit-conversion-loses-precision warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX1tyuH22farvsyMPiZn&open=AX1tyuH22farvsyMPiZn
* fix use-init-statement warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX1tyuO42farvsyMPiZw&open=AX1tyuO42farvsyMPiZw
* fix implicit-conversion-loses-precision warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX1tyuO42farvsyMPiZs&open=AX1tyuO42farvsyMPiZs
* fix: implicit-conversion-loses-precision warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX1tyuO42farvsyMPiZt&open=AX1tyuO42farvsyMPiZt
* fix handle-this-exceeption-or-do-not-catch-it warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX1y7t12cIz3krp3mVpV&open=AX1y7t12cIz3krp3mVpV
* fix make-this-variable-a-reference-to-const warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX1y7t12cIz3krp3mVpW&open=AX1y7t12cIz3krp3mVpW
* fix replace-redundant-type-with-auto warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX1y7t12cIz3krp3mVpY&open=AX1y7t12cIz3krp3mVpY
* Fix use-try_emplace-instead-of-emplace warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX0WPk3QwxvzFbF6whWN&open=AX0WPk3QwxvzFbF6whWN
* Fix: make-this-a-pointer-to-const warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX0bZPUXjI_aUyxGBxB4&open=AX0bZPUXjI_aUyxGBxB4
* Fix use-std::make_unique warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX0WPk0twxvzFbF6whWC&open=AX0WPk0twxvzFbF6whWC
* Fix: replace-push_back-with-emplace_back warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX0BraL_0QLXO0pQ_Bfv&open=AX0BraL_0QLXO0pQ_Bfv
* Fix: use-init-statement warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX0bZPXajI_aUyxGBxB8&open=AX0bZPXajI_aUyxGBxB8
* Fix: replace-redundant-type-with-auto warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX0bZPXajI_aUyxGBxB9&open=AX0bZPXajI_aUyxGBxB9
* Fix: replace-emplace-with-try_emplace warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX0bZPXajI_aUyxGBxB_&open=AX0bZPXajI_aUyxGBxB_
* Fix: extract-assignment-from-operation warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX0bZPXajI_aUyxGBxCA&open=AX0bZPXajI_aUyxGBxCA
* Fix use-init-statement warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX1op6_hUBCnp-Grmra8&open=AX1op6_hUBCnp-Grmra8
* fix: use-init-statement warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX0L8qgMUnpKTxYiu5CN&open=AX0L8qgMUnpKTxYiu5CN
* Revert "Fix use-std::make_unique warning"
This reverts commit 463a742f34
.
* fix: make-parameter-a-pointer-to-const warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX06St241usi2gyYkPTZ&open=AX06St241usi2gyYkPTZ
* fix: make-parameter-a-pointer-to-const warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX06St241usi2gyYkPTa&open=AX06St241usi2gyYkPTa
* fix: use-init-statement warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX01Itn2f_SST5i7BN1o&open=AX01Itn2f_SST5i7BN1o
* fix: use-init-statement warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AXziv55-MuQoG-Y-NgLX&open=AXziv55-MuQoG-Y-NgLX
* fix: use-init-statement warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AX0_a_JGNJn7rAzml_68&open=AX0_a_JGNJn7rAzml_68
* fix: replace-redundant-type-with-auto warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AXzyMRYbK9dvryvWm8XA&open=AXzyMRYbK9dvryvWm8XA
* fix: use-init-statement warning
Xref: https://sonarcloud.io/project/issues?id=transmission_transmission&issues=AXziv52fMuQoG-Y-NgLW&open=AXziv52fMuQoG-Y-NgLW
180 lines
4.4 KiB
C++
180 lines
4.4 KiB
C++
/*
|
|
* This file Copyright (C) Mnemosyne LLC
|
|
*
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
*
|
|
*/
|
|
|
|
#include <algorithm>
|
|
#include <iterator>
|
|
#include <vector>
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "block-info.h"
|
|
#include "file-piece-map.h"
|
|
|
|
void tr_file_piece_map::reset(tr_block_info const& block_info, uint64_t const* file_sizes, size_t n_files)
|
|
{
|
|
files_.resize(n_files);
|
|
files_.shrink_to_fit();
|
|
|
|
uint64_t offset = 0;
|
|
for (tr_file_index_t i = 0; i < n_files; ++i)
|
|
{
|
|
auto const file_size = file_sizes[i];
|
|
auto const begin_piece = block_info.pieceOf(offset);
|
|
tr_piece_index_t end_piece = 0;
|
|
if (file_size != 0)
|
|
{
|
|
auto const last_byte = offset + file_size - 1;
|
|
auto const final_piece = block_info.pieceOf(last_byte);
|
|
end_piece = final_piece + 1;
|
|
}
|
|
else
|
|
{
|
|
end_piece = begin_piece + 1;
|
|
}
|
|
files_[i] = piece_span_t{ begin_piece, end_piece };
|
|
offset += file_size;
|
|
}
|
|
}
|
|
|
|
void tr_file_piece_map::reset(tr_info const& info)
|
|
{
|
|
tr_file_index_t const n = info.fileCount;
|
|
auto file_sizes = std::vector<uint64_t>(n);
|
|
std::transform(info.files, info.files + n, std::begin(file_sizes), [](tr_file const& file) { return file.length; });
|
|
reset({ info.totalSize, info.pieceSize }, std::data(file_sizes), std::size(file_sizes));
|
|
}
|
|
|
|
tr_file_piece_map::piece_span_t tr_file_piece_map::pieceSpan(tr_file_index_t file) const
|
|
{
|
|
return files_[file];
|
|
}
|
|
|
|
tr_file_piece_map::file_span_t tr_file_piece_map::fileSpan(tr_piece_index_t piece) const
|
|
{
|
|
struct Compare
|
|
{
|
|
int compare(tr_piece_index_t piece, piece_span_t span) const // <=>
|
|
{
|
|
if (piece < span.begin)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
if (piece >= span.end)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
bool operator()(tr_piece_index_t piece, piece_span_t span) const // <
|
|
{
|
|
return compare(piece, span) < 0;
|
|
}
|
|
|
|
int compare(piece_span_t span, tr_piece_index_t piece) const // <=>
|
|
{
|
|
return -compare(piece, span);
|
|
}
|
|
|
|
bool operator()(piece_span_t span, tr_piece_index_t piece) const // <
|
|
{
|
|
return compare(span, piece) < 0;
|
|
}
|
|
};
|
|
|
|
auto const begin = std::begin(files_);
|
|
auto const& [equal_begin, equal_end] = std::equal_range(begin, std::end(files_), piece, Compare{});
|
|
return { tr_piece_index_t(std::distance(begin, equal_begin)), tr_piece_index_t(std::distance(begin, equal_end)) };
|
|
}
|
|
|
|
/***
|
|
****
|
|
***/
|
|
|
|
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
|
|
{
|
|
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;
|
|
}
|