1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-12 15:14:12 +00:00

#781: experimental commit on the "failed data" issue.

This commit is contained in:
Charles Kerr 2008-03-13 00:38:16 +00:00
parent b6f2796e40
commit e3c1d221fb
2 changed files with 29 additions and 22 deletions

View file

@ -12,17 +12,18 @@
#include <assert.h>
#include <errno.h>
#include <stdlib.h> /* realloc */
#include <string.h> /* memcmp */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <openssl/sha.h>
#include "transmission.h"
#include "crypto.h"
#include "fdlimit.h"
#include "inout.h"
#include "platform.h"
#include "stats.h"
#include "torrent.h"
#include "utils.h"
@ -220,33 +221,38 @@ tr_ioRecalculateHash( const tr_torrent * tor,
int pieceIndex,
uint8_t * setme )
{
int offset;
int bytesLeft;
uint8_t buf[4096];
static uint8_t * buf = NULL;
static int buflen = 0;
static tr_lock * lock = NULL;
int n;
tr_errno err;
const tr_info * info;
SHA_CTX sha;
/* only check one block at a time to prevent disk thrashing.
* this also lets us reuse the same buffer each time. */
if( lock == NULL )
lock = tr_lockNew( );
tr_lockLock( lock );
assert( tor != NULL );
assert( setme != NULL );
assert( 0<=pieceIndex && pieceIndex<tor->info.pieceCount );
info = &tor->info;
offset = 0;
bytesLeft = tr_torPieceCountBytes( tor, pieceIndex );
SHA1_Init( &sha );
n = tr_torPieceCountBytes( tor, pieceIndex );
while( bytesLeft > 0 )
{
const int bytesThisPass = MIN( bytesLeft, (int)sizeof(buf) );
tr_errno err = tr_ioRead( tor, pieceIndex, offset, bytesThisPass, buf );
if( err )
return err;
SHA1_Update( &sha, buf, bytesThisPass );
bytesLeft -= bytesThisPass;
offset += bytesThisPass;
if( buflen < n ) {
buflen = n;
buf = tr_renew( uint8_t, buf, buflen );
}
err = tr_ioRead( tor, pieceIndex, 0, n, buf );
if( !err )
tr_sha1( setme, buf, n, NULL );
SHA1_Final( setme, &sha );
tr_lockUnlock( lock );
return 0;
}

View file

@ -41,6 +41,8 @@
#include "utils.h"
#include "verify.h"
#define MAX_BLOCK_SIZE (1024*16)
/***
****
***/
@ -289,7 +291,7 @@ torrentRealInit( tr_handle * h,
* (2) pieceSize must be a multiple of block size
*/
tor->blockSize = info->pieceSize;
while( tor->blockSize > (1024*16) )
while( tor->blockSize > MAX_BLOCK_SIZE )
tor->blockSize /= 2;
tor->lastPieceSize = info->totalSize % info->pieceSize;
@ -1295,14 +1297,13 @@ tr_torrentReqIsValid( const tr_torrent * tor,
uint32_t offset,
uint32_t length )
{
static const uint32_t MAX_REQUEST_BYTE_COUNT = (16 * 1024);
int err = 0;
if( index >= (uint32_t) tor->info.pieceCount )
err = 1;
else if ( (int)offset >= tr_torPieceCountBytes( tor, (int)index ) )
err = 2;
else if( length > MAX_REQUEST_BYTE_COUNT )
else if( length > MAX_BLOCK_SIZE )
err = 3;
else if( tr_pieceOffset( tor, index, offset, length ) > tor->info.totalSize )
err = 4;