(trunk) #3339 "crash when download some magnet links" -- fixed

This commit is contained in:
Charles Kerr 2010-06-24 20:36:05 +00:00
parent 5a0d9351d2
commit 41f9ae0500
3 changed files with 27 additions and 12 deletions

View File

@ -253,6 +253,8 @@ tr_torrentSetMetadataPiece( tr_torrent * tor, int piece, const void * data, in
if( !tr_bencLoadFile( &newMetainfo, TR_FMT_BENC, path ) )
{
tr_bool hasInfo;
tr_info info;
int infoDictLength;
/* remove any old .torrent and .resume files */
remove( path );
@ -261,16 +263,27 @@ tr_torrentSetMetadataPiece( tr_torrent * tor, int piece, const void * data, in
dbgmsg( tor, "Saving completed metadata to \"%s\"", path );
tr_bencMergeDicts( tr_bencDictAddDict( &newMetainfo, "info", 0 ), &infoDict );
success = tr_metainfoParse( tor->session, &newMetainfo, &tor->info, &hasInfo, &tor->infoDictLength );
memset( &info, 0, sizeof( tr_info ) );
success = tr_metainfoParse( tor->session, &newMetainfo, &info, &hasInfo, &infoDictLength );
assert( hasInfo );
assert( success );
if( success && !tr_getBlockSize( info.pieceSize ) )
{
tr_torrentSetLocalError( tor, _( "Magnet torrent's metadata is not usable" ) );
success = FALSE;
}
/* save the new .torrent file */
tr_bencToFile( &newMetainfo, TR_FMT_BENC, tor->info.torrent );
tr_sessionSetTorrentFile( tor->session, tor->info.hashString, tor->info.torrent );
tr_torrentGotNewInfoDict( tor );
tr_torrentSetDirty( tor );
if( success )
{
/* keep the new info */
tor->info = info;
tor->infoDictLength = infoDictLength;
/* save the new .torrent file */
tr_bencToFile( &newMetainfo, TR_FMT_BENC, tor->info.torrent );
tr_sessionSetTorrentFile( tor->session, tor->info.hashString, tor->info.torrent );
tr_torrentGotNewInfoDict( tor );
tr_torrentSetDirty( tor );
}
tr_bencFree( &newMetainfo );
}

View File

@ -540,8 +540,8 @@ static void torrentStart( tr_torrent * tor );
* (1) most clients decline requests over 16 KiB
* (2) pieceSize must be a multiple of block size
*/
static uint32_t
getBlockSize( uint32_t pieceSize )
uint32_t
tr_getBlockSize( uint32_t pieceSize )
{
uint32_t b = pieceSize;
@ -561,7 +561,7 @@ torrentInitFromInfo( tr_torrent * tor )
uint64_t t;
tr_info * info = &tor->info;
tor->blockSize = getBlockSize( info->pieceSize );
tor->blockSize = tr_getBlockSize( info->pieceSize );
if( info->pieceSize )
tor->lastPieceSize = info->totalSize % info->pieceSize;
@ -756,7 +756,7 @@ torrentParseImpl( const tr_ctor * ctor, tr_info * setmeInfo,
if( !didParse )
result = TR_PARSE_ERR;
if( didParse && hasInfo && !getBlockSize( setmeInfo->pieceSize ) )
if( didParse && hasInfo && !tr_getBlockSize( setmeInfo->pieceSize ) )
result = TR_PARSE_ERR;
if( didParse && session && tr_torrentExists( session, setmeInfo->hash ) )

View File

@ -383,6 +383,8 @@ const char * tr_torrentName( const tr_torrent * tor )
return tor->info.name;
}
uint32_t tr_getBlockSize( uint32_t pieceSize );
/**
* Tell the tr_torrent that one of its files has become complete
*/