mirror of
https://github.com/transmission/transmission
synced 2025-03-03 10:15:45 +00:00
make the code a little easier to read. make tr_ioFileTest()'s return value consistent with other inout funcs.
This commit is contained in:
parent
e52c1d9727
commit
c144470c70
4 changed files with 42 additions and 50 deletions
|
@ -221,7 +221,7 @@ tr_ioWrite( tr_torrent * tor,
|
||||||
*****
|
*****
|
||||||
****/
|
****/
|
||||||
|
|
||||||
static int
|
static tr_errno
|
||||||
tr_ioRecalculateHash( const tr_torrent * tor,
|
tr_ioRecalculateHash( const tr_torrent * tor,
|
||||||
int pieceIndex,
|
int pieceIndex,
|
||||||
uint8_t * setme )
|
uint8_t * setme )
|
||||||
|
@ -244,7 +244,7 @@ tr_ioRecalculateHash( const tr_torrent * tor,
|
||||||
while( bytesLeft > 0 )
|
while( bytesLeft > 0 )
|
||||||
{
|
{
|
||||||
const int bytesThisPass = MIN( bytesLeft, (int)sizeof(buf) );
|
const int bytesThisPass = MIN( bytesLeft, (int)sizeof(buf) );
|
||||||
int err = tr_ioRead( tor, pieceIndex, offset, bytesThisPass, buf );
|
tr_errno err = tr_ioRead( tor, pieceIndex, offset, bytesThisPass, buf );
|
||||||
if( err )
|
if( err )
|
||||||
return err;
|
return err;
|
||||||
SHA1_Update( &sha, buf, bytesThisPass );
|
SHA1_Update( &sha, buf, bytesThisPass );
|
||||||
|
@ -256,38 +256,19 @@ tr_ioRecalculateHash( const tr_torrent * tor,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
tr_errno
|
||||||
tr_ioTestPiece( const tr_torrent * tor, int pieceIndex )
|
tr_ioTestPiece( const tr_torrent * tor, int pieceIndex )
|
||||||
{
|
{
|
||||||
|
int err;
|
||||||
uint8_t hash[SHA_DIGEST_LENGTH];
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
||||||
const int ret = tr_ioRecalculateHash( tor, pieceIndex, hash )
|
|
||||||
|| memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH );
|
err = tr_ioRecalculateHash( tor, pieceIndex, hash );
|
||||||
|
|
||||||
|
if( !err && memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH ) )
|
||||||
|
err = TR_ERROR;
|
||||||
|
|
||||||
tr_dbg ("torrent [%s] piece %d hash check: %s",
|
tr_dbg ("torrent [%s] piece %d hash check: %s",
|
||||||
tor->info.name, pieceIndex, ( ret ? "FAILED" : "OK" ));
|
tor->info.name, pieceIndex, ( err ? "FAILED" : "OK" ));
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
return err;
|
||||||
tr_ioHash( tr_torrent * tor, int pieceIndex )
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
const int success = !tr_ioTestPiece( tor, pieceIndex );
|
|
||||||
|
|
||||||
if( success )
|
|
||||||
{
|
|
||||||
tr_dbg( "Piece %d hash OK", pieceIndex );
|
|
||||||
tr_cpPieceAdd( tor->completion, pieceIndex );
|
|
||||||
ret = TR_OK;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tr_err( "Piece %d hash FAILED", pieceIndex );
|
|
||||||
tr_cpPieceRem( tor->completion, pieceIndex );
|
|
||||||
ret = TR_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
tr_peerMgrSetBlame( tor->handle->peerMgr, tor->info.hash,
|
|
||||||
pieceIndex, success );
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,17 +50,11 @@ tr_errno tr_ioWrite ( struct tr_torrent * tor,
|
||||||
const uint8_t * writeme );
|
const uint8_t * writeme );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns true if the piece matches its metainfo's SHA1 checksum,
|
* returns 0 if the piece matches its metainfo's SHA1 checksum,
|
||||||
* false otherwise.
|
* or TR_ERROR_IO_* if there was a problem reading the piece,
|
||||||
|
* or TR_ERROR if the checksum didn't match.
|
||||||
*/
|
*/
|
||||||
int tr_ioTestPiece( const tr_torrent*, int piece );
|
tr_errno tr_ioTestPiece( const tr_torrent*, int piece );
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* tests the specified piece and uses the results to
|
|
||||||
* update the torrent's "completion" and "blame" fields.
|
|
||||||
*/
|
|
||||||
int tr_ioHash ( tr_torrent*, int piece );
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1455,14 +1455,16 @@ clientGotBlock( tr_peermsgs * msgs,
|
||||||
|
|
||||||
if( tr_cpPieceIsComplete( tor->completion, req->index ) )
|
if( tr_cpPieceIsComplete( tor->completion, req->index ) )
|
||||||
{
|
{
|
||||||
if( tr_ioHash( tor, req->index ) )
|
const tr_errno err = tr_ioTestPiece( tor, req->index );
|
||||||
{
|
|
||||||
gotBadPiece( msgs, req->index );
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
tr_torrentSetHasPiece( tor, req->index, !err );
|
||||||
tr_torrentSetPieceChecked( tor, req->index, TRUE );
|
tr_torrentSetPieceChecked( tor, req->index, TRUE );
|
||||||
|
tr_peerMgrSetBlame( tor->handle->peerMgr, tor->info.hash, req->index, !err );
|
||||||
|
|
||||||
|
if( !err )
|
||||||
fireClientHave( msgs, req->index );
|
fireClientHave( msgs, req->index );
|
||||||
|
else
|
||||||
|
gotBadPiece( msgs, req->index );
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -90,8 +90,23 @@ checkFile( tr_torrent * tor,
|
||||||
}
|
}
|
||||||
else if( !tr_torrentIsPieceChecked( tor, i ) )
|
else if( !tr_torrentIsPieceChecked( tor, i ) )
|
||||||
{
|
{
|
||||||
const int check = tr_ioTestPiece( tor, i );
|
const tr_errno err = tr_ioTestPiece( tor, i );
|
||||||
tr_torrentSetHasPiece( tor, i, !check );
|
|
||||||
|
if( !err ) /* yay */
|
||||||
|
{
|
||||||
|
tr_torrentSetHasPiece( tor, i, TRUE );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* if we were wrong about it being complete,
|
||||||
|
* reset and start again. if we were right about
|
||||||
|
* it being incomplete, do nothing -- we don't
|
||||||
|
* want to lose blocks in those incomplete pieces */
|
||||||
|
|
||||||
|
if( tr_cpPieceIsComplete( tor->completion, i ) )
|
||||||
|
tr_torrentSetHasPiece( tor, i, FALSE );
|
||||||
|
}
|
||||||
|
|
||||||
tr_torrentSetPieceChecked( tor, i, TRUE );
|
tr_torrentSetPieceChecked( tor, i, TRUE );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue