refactor: use tr_block_info::Location in inout.cc (#2658)

This commit is contained in:
Charles Kerr 2022-02-19 10:07:08 -06:00 committed by GitHub
parent 023fbd69f6
commit 78ad90a4c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 40 deletions

View File

@ -163,8 +163,7 @@ static int flushContiguous(tr_cache* cache, int pos, int n)
auto* b = blocks[pos];
auto* const tor = b->tor;
auto const piece = b->loc.piece;
auto const offset = b->loc.piece_offset;
auto const loc = b->loc;
for (int i = 0; i < n; ++i)
{
@ -177,7 +176,7 @@ static int flushContiguous(tr_cache* cache, int pos, int n)
tr_ptrArrayErase(&cache->blocks, pos, pos + n);
err = tr_ioWrite(tor, piece, offset, walk - buf, buf);
err = tr_ioWrite(tor, loc, walk - buf, buf);
tr_free(buf);
++cache->disk_writes;
@ -352,7 +351,7 @@ int tr_cacheReadBlock(tr_cache* cache, tr_torrent* torrent, tr_block_info::Locat
}
else
{
err = tr_ioRead(torrent, loc.piece, loc.piece_offset, len, setme);
err = tr_ioRead(torrent, loc, len, setme);
}
return err;
@ -364,7 +363,7 @@ int tr_cachePrefetchBlock(tr_cache* cache, tr_torrent* torrent, tr_block_info::L
if (auto const* const cb = findBlock(cache, torrent, loc); cb == nullptr)
{
err = tr_ioPrefetch(torrent, loc.piece, loc.piece_offset, len);
err = tr_ioPrefetch(torrent, loc, len);
}
return err;

View File

@ -148,22 +148,16 @@ static int readOrWriteBytes(
}
/* returns 0 on success, or an errno on failure */
static int readOrWritePiece(
tr_torrent* tor,
int ioMode,
tr_piece_index_t pieceIndex,
uint32_t pieceOffset,
uint8_t* buf,
size_t buflen)
static int readOrWritePiece(tr_torrent* tor, int ioMode, tr_block_info::Location loc, uint8_t* buf, size_t buflen)
{
int err = 0;
if (pieceIndex >= tor->pieceCount())
if (loc.piece >= tor->pieceCount())
{
return EINVAL;
}
auto [file_index, file_offset] = tor->fileOffset(pieceIndex, pieceOffset);
auto [file_index, file_offset] = tor->fileOffset(loc);
while (buflen != 0 && err == 0)
{
@ -186,19 +180,19 @@ static int readOrWritePiece(
return err;
}
int tr_ioRead(tr_torrent* tor, tr_piece_index_t pieceIndex, uint32_t begin, uint32_t len, uint8_t* buf)
int tr_ioRead(tr_torrent* tor, tr_block_info::Location loc, uint32_t len, uint8_t* buf)
{
return readOrWritePiece(tor, TR_IO_READ, pieceIndex, begin, buf, len);
return readOrWritePiece(tor, TR_IO_READ, loc, buf, len);
}
int tr_ioPrefetch(tr_torrent* tor, tr_piece_index_t pieceIndex, uint32_t begin, uint32_t len)
int tr_ioPrefetch(tr_torrent* tor, tr_block_info::Location loc, uint32_t len)
{
return readOrWritePiece(tor, TR_IO_PREFETCH, pieceIndex, begin, nullptr, len);
return readOrWritePiece(tor, TR_IO_PREFETCH, loc, nullptr, len);
}
int tr_ioWrite(tr_torrent* tor, tr_piece_index_t pieceIndex, uint32_t begin, uint32_t len, uint8_t const* buf)
int tr_ioWrite(tr_torrent* tor, tr_block_info::Location loc, uint32_t len, uint8_t const* buf)
{
return readOrWritePiece(tor, TR_IO_WRITE, pieceIndex, begin, (uint8_t*)buf, len);
return readOrWritePiece(tor, TR_IO_WRITE, loc, (uint8_t*)buf, len);
}
/****
@ -211,25 +205,22 @@ static std::optional<tr_sha1_digest_t> recalculateHash(tr_torrent* tor, tr_piece
TR_ASSERT(piece < tor->pieceCount());
auto bytes_left = size_t(tor->pieceSize(piece));
auto offset = uint32_t{};
tr_ioPrefetch(tor, piece, offset, bytes_left);
auto loc = tor->pieceLoc(piece);
tr_ioPrefetch(tor, loc, bytes_left);
auto sha = tr_sha1_init();
auto buffer = std::vector<uint8_t>(tor->blockSize());
while (bytes_left != 0)
{
size_t const len = std::min(bytes_left, std::size(buffer));
if (auto const
success = tr_cacheReadBlock(tor->session->cache, tor, tor->pieceLoc(piece, offset), len, std::data(buffer)) ==
0;
!success)
if (auto const success = tr_cacheReadBlock(tor->session->cache, tor, loc, len, std::data(buffer)) == 0; !success)
{
tr_sha1_final(sha);
return {};
}
tr_sha1_update(sha, std::data(buffer), len);
offset += len;
loc = tor->byteLoc(loc.byte + len);
bytes_left -= len;
}

View File

@ -9,6 +9,10 @@
#error only libtransmission should #include this header.
#endif
#include "transmission.h"
#include "block-info.h"
struct tr_torrent;
/**
@ -20,15 +24,15 @@ struct tr_torrent;
* Reads the block specified by the piece index, offset, and length.
* @return 0 on success, or an errno value on failure.
*/
int tr_ioRead(struct tr_torrent* tor, tr_piece_index_t pieceIndex, uint32_t offset, uint32_t len, uint8_t* setme);
int tr_ioRead(struct tr_torrent* tor, tr_block_info::Location loc, uint32_t len, uint8_t* setme);
int tr_ioPrefetch(tr_torrent* tor, tr_piece_index_t pieceIndex, uint32_t begin, uint32_t len);
int tr_ioPrefetch(tr_torrent* tor, tr_block_info::Location loc, uint32_t len);
/**
* Writes the block specified by the piece index, offset, and length.
* @return 0 on success, or an errno value on failure.
*/
int tr_ioWrite(struct tr_torrent* tor, tr_piece_index_t pieceIndex, uint32_t offset, uint32_t len, uint8_t const* writeme);
int tr_ioWrite(struct tr_torrent* tor, tr_block_info::Location loc, uint32_t len, uint8_t const* writeme);
/**
* @brief Test to see if the piece matches its metainfo's SHA1 checksum.

View File

@ -52,7 +52,7 @@ public:
{
return blockInfo().blockLoc(block);
}
[[nodiscard]] auto pieceLoc(tr_piece_index_t piece, uint32_t offset, uint32_t length = 0) const
[[nodiscard]] auto pieceLoc(tr_piece_index_t piece, uint32_t offset = 0, uint32_t length = 0) const
{
return blockInfo().pieceLoc(piece, offset, length);
}

View File

@ -163,7 +163,7 @@ public:
{
return metainfo_.blockLoc(block);
}
[[nodiscard]] auto pieceLoc(tr_piece_index_t piece, uint32_t offset, uint32_t length = 0) const
[[nodiscard]] auto pieceLoc(tr_piece_index_t piece, uint32_t offset = 0, uint32_t length = 0) const
{
return metainfo_.pieceLoc(piece, offset, length);
}
@ -282,14 +282,9 @@ public:
return fpm_.pieceSpan(file);
}
[[nodiscard]] auto fileOffset(uint64_t offset) const
[[nodiscard]] auto fileOffset(tr_block_info::Location loc) const
{
return fpm_.fileOffset(offset);
}
[[nodiscard]] auto fileOffset(tr_piece_index_t piece, uint32_t piece_offset) const
{
return fpm_.fileOffset(this->pieceLoc(piece, piece_offset).byte);
return fpm_.fileOffset(loc.byte);
}
/// WANTED

View File

@ -502,7 +502,7 @@ void task_request_next_chunk(tr_webseed_task* t)
tr_piece_index_t const step_piece = total_offset / piece_size;
uint64_t const step_piece_offset = total_offset - uint64_t(piece_size) * step_piece;
auto const [file_index, file_offset] = tor->fileOffset(step_piece, step_piece_offset);
auto const [file_index, file_offset] = tor->fileOffset(tor->pieceLoc(step_piece, step_piece_offset));
uint64_t this_pass = std::min(remain, tor->fileSize(file_index) - file_offset);
auto const url = make_url(t->webseed, tor->fileSubpath(file_index));