Skip to content

Commit

Permalink
raise the max piece size to 512 MiB and fix check when loading torrents
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Sep 29, 2024
1 parent 0ca20a3 commit 82528e3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* raise the max piece size to 512 MiB and fix check when loading torrents
* back-port fix last_upload and last_download resume data fields to use posix time
* back-port treat CGNAT address range as local IPs

Expand Down
16 changes: 7 additions & 9 deletions include/libtorrent/block_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,14 @@ namespace aux {
// bitfield
std::uint16_t blocks_in_piece = 0;

// this is the number of threads that are currently holding
// a reference to this piece. A piece may not be removed from
// the cache while this is > 0
std::uint8_t piece_refcount = 0;

// 8 bytes padding
// the number of blocks in the cache for this piece
std::uint16_t num_blocks = 0;

// ---- 64 bit boundary ----

// the number of dirty blocks in this piece
std::uint32_t num_dirty:14;

// the number of blocks in the cache for this piece
std::uint32_t num_blocks:14;

// while we have an outstanding async hash operation
// working on this piece, 'hashing' is set to 1
// When the operation returns, this is set to 0.
Expand All @@ -259,6 +252,11 @@ namespace aux {
// piece, and returned it. This is set to one
std::uint32_t hashing_done:1;

// this is the number of threads that are currently holding
// a reference to this piece. A piece may not be removed from
// the cache while this is > 0
std::uint32_t piece_refcount:8;

// if this is true, whenever refcount hits 0,
// this piece should be deleted from the cache
// (not just demoted)
Expand Down
4 changes: 2 additions & 2 deletions include/libtorrent/piece_picker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ namespace libtorrent {
// priority factor
prio_factor = 3,
// max blocks per piece
// there are counters in downloading_piece that only have 15 bits to
// there are counters in downloading_piece that only have 16 bits to
// count blocks per piece, that's restricting this
max_blocks_per_piece = (1 << 15) - 1
max_blocks_per_piece = (1 << 16) - 1
};

struct block_info
Expand Down
2 changes: 1 addition & 1 deletion src/block_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ static_assert(int(job_action_name.size()) == static_cast<int>(job_action_t::num_

cached_piece_entry::cached_piece_entry()
: num_dirty(0)
, num_blocks(0)
, hashing(0)
, hashing_done(0)
, piece_refcount(0)
, marked_for_deletion(false)
, need_readback(false)
, cache_state(none)
Expand Down
7 changes: 6 additions & 1 deletion src/torrent_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,11 +1027,16 @@ namespace {

// extract piece length
std::int64_t piece_length = info.dict_find_int_value("piece length", -1);
if (piece_length <= 0 || piece_length > std::numeric_limits<int>::max())
if (piece_length <= 0)
{
ec = errors::torrent_missing_piece_length;
return false;
}
if (piece_length > std::numeric_limits<int>::max())
{
ec = errors::invalid_piece_size;
return false;
}
file_storage files;
files.set_piece_length(static_cast<int>(piece_length));

Expand Down
18 changes: 13 additions & 5 deletions test/test_torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void test_running_torrent(std::shared_ptr<torrent_info> info, std::int64_t file_
TEST_CHECK(h.get_file_priorities() == prio);
}

void test_large_piece_size(int const size)
void test_large_piece_size(std::int64_t const size)
{
entry torrent;
entry& info = torrent["info"];
Expand All @@ -179,7 +179,15 @@ void test_large_piece_size(int const size)
std::vector<char> buf;
bencode(std::back_inserter(buf), torrent);
add_torrent_params atp;
atp.ti = std::make_shared<torrent_info>(buf, from_span);
try
{
atp.ti = std::make_shared<torrent_info>(buf, from_span);
}
catch (lt::system_error const& e)
{
TEST_CHECK(e.code() == error_code(lt::errors::invalid_piece_size));
return;
}
atp.save_path = ".";

lt::session ses;
Expand Down Expand Up @@ -212,9 +220,9 @@ TORRENT_TEST(long_names)

TORRENT_TEST(large_piece_size)
{
test_large_piece_size(32768 * 16 * 1024);
test_large_piece_size(65536 * 16 * 1024);
test_large_piece_size(65537 * 16 * 1024);
test_large_piece_size(0x40000000 + 0x4000);
test_large_piece_size(0x80000000);
test_large_piece_size(0x100000000);
}

TORRENT_TEST(total_wanted)
Expand Down

0 comments on commit 82528e3

Please sign in to comment.