2009-01-07 06:53:29 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2009-01-07 06:53:29 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2009-01-07 06:53:29 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2011-03-16 18:04:23 +00:00
|
|
|
#include <string.h> /* memcpy() */
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "completion.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-11-12 22:26:10 +00:00
|
|
|
static void
|
|
|
|
tr_cpReset( tr_completion * cp )
|
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_bitsetSetHaveNone( &cp->blockBitset );
|
|
|
|
tr_free( cp->completeBlocks );
|
|
|
|
cp->completeBlocks = NULL;
|
2008-04-24 15:25:01 +00:00
|
|
|
cp->sizeNow = 0;
|
2011-02-23 03:54:04 +00:00
|
|
|
cp->sizeWhenDoneIsDirty = TRUE;
|
|
|
|
cp->blocksWantedIsDirty = TRUE;
|
|
|
|
cp->haveValidIsDirty = TRUE;
|
2007-11-12 22:26:10 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
tr_completion *
|
2009-01-02 17:01:55 +00:00
|
|
|
tr_cpConstruct( tr_completion * cp, tr_torrent * tor )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-01-02 17:01:55 +00:00
|
|
|
cp->tor = tor;
|
2011-02-23 03:54:04 +00:00
|
|
|
cp->completeBlocks = NULL;
|
|
|
|
tr_bitsetConstruct( &cp->blockBitset, tor->blockCount );
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_cpReset( cp );
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
tr_completion*
|
|
|
|
tr_cpDestruct( tr_completion * cp )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-12-29 09:51:54 +00:00
|
|
|
tr_free( cp->completeBlocks );
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_bitsetDestruct( &cp->blockBitset );
|
2009-01-02 17:01:55 +00:00
|
|
|
return cp;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static inline tr_bool
|
|
|
|
isSeed( const tr_completion * cp )
|
|
|
|
{
|
|
|
|
return cp->blockBitset.haveAll;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_completeness
|
|
|
|
tr_cpGetStatus( const tr_completion * cp )
|
|
|
|
{
|
|
|
|
if( !tr_torrentHasMetadata( cp->tor ) ) return TR_LEECH;
|
|
|
|
if( cp->sizeNow == cp->tor->info.totalSize ) return TR_SEED;
|
|
|
|
if( cp->sizeNow == tr_cpSizeWhenDone( cp ) ) return TR_PARTIAL_SEED;
|
|
|
|
return TR_LEECH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* how many blocks are in this piece? */
|
|
|
|
static inline uint16_t
|
|
|
|
countBlocksInPiece( const tr_torrent * tor, const tr_piece_index_t piece )
|
|
|
|
{
|
|
|
|
return piece + 1 == tor->info.pieceCount ? tor->blockCountInLastPiece
|
|
|
|
: tor->blockCountInPiece;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t *
|
|
|
|
getCompleteBlocks( const tr_completion * ccp )
|
|
|
|
{
|
|
|
|
if( ccp->completeBlocks == NULL )
|
|
|
|
{
|
|
|
|
tr_completion * cp = (tr_completion*) ccp;
|
|
|
|
cp->completeBlocks = tr_new0( uint16_t, ccp->tor->info.pieceCount );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ccp->completeBlocks;
|
|
|
|
}
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_cpInvalidateDND( tr_completion * cp )
|
2008-04-24 15:25:01 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
cp->sizeWhenDoneIsDirty = TRUE;
|
|
|
|
cp->blocksWantedIsDirty = TRUE;
|
2011-02-15 16:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_block_index_t
|
|
|
|
tr_cpBlocksMissing( const tr_completion * ccp )
|
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
if( isSeed( ccp ) )
|
|
|
|
return 0;
|
|
|
|
|
2011-02-15 16:04:56 +00:00
|
|
|
if( ccp->blocksWantedIsDirty )
|
|
|
|
{
|
|
|
|
tr_piece_index_t i;
|
|
|
|
tr_block_index_t wanted = 0;
|
|
|
|
tr_block_index_t complete = 0;
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_completion * cp = (tr_completion *) ccp; /* mutable */
|
|
|
|
const uint16_t * complete_blocks = getCompleteBlocks( cp );
|
|
|
|
const tr_torrent * tor = ccp->tor;
|
|
|
|
const tr_info * info = &tor->info;
|
2011-02-15 16:04:56 +00:00
|
|
|
|
|
|
|
for( i = 0; i < info->pieceCount; ++i )
|
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
if( !info->pieces[i].dnd )
|
|
|
|
{
|
|
|
|
wanted += countBlocksInPiece( tor, i );
|
|
|
|
complete += complete_blocks[i];
|
|
|
|
}
|
2011-02-15 16:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cp->blocksWantedLazy = wanted;
|
|
|
|
cp->blocksWantedCompleteLazy = complete;
|
|
|
|
cp->blocksWantedIsDirty = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ccp->blocksWantedLazy - ccp->blocksWantedCompleteLazy;
|
2008-04-24 15:25:01 +00:00
|
|
|
}
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
void
|
|
|
|
tr_cpPieceRem( tr_completion * cp, tr_piece_index_t piece )
|
2007-07-28 15:43:34 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_block_index_t i;
|
|
|
|
tr_block_index_t first;
|
|
|
|
tr_block_index_t last;
|
|
|
|
const tr_torrent * tor = cp->tor;
|
|
|
|
uint16_t * complete_blocks = getCompleteBlocks( cp );
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_torGetPieceBlockRange( cp->tor, piece, &first, &last );
|
|
|
|
for( i=first; i<=last; ++i )
|
|
|
|
if( tr_cpBlockIsComplete( cp, i ) )
|
|
|
|
cp->sizeNow -= tr_torBlockCountBytes( tor, i );
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
if( !tor->info.pieces[piece].dnd )
|
|
|
|
cp->blocksWantedCompleteLazy -= complete_blocks[piece];
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
cp->sizeWhenDoneIsDirty = TRUE;
|
|
|
|
cp->haveValidIsDirty = TRUE;
|
|
|
|
complete_blocks[piece] = 0;
|
|
|
|
tr_bitsetRemRange( &cp->blockBitset, first, last+1 );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-27 16:08:20 +00:00
|
|
|
void
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_cpPieceAdd( tr_completion * cp, tr_piece_index_t piece )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_block_index_t i;
|
|
|
|
tr_block_index_t first;
|
|
|
|
tr_block_index_t last;
|
|
|
|
tr_torGetPieceBlockRange( cp->tor, piece, &first, &last );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
for( i=first; i<=last; ++i )
|
2007-07-28 15:43:34 +00:00
|
|
|
tr_cpBlockAdd( cp, i );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
void
|
2009-01-07 02:22:11 +00:00
|
|
|
tr_cpBlockAdd( tr_completion * cp, tr_block_index_t block )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_torrent * tor = cp->tor;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
if( !tr_cpBlockIsComplete( cp, block ) )
|
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
const tr_piece_index_t piece = tr_torBlockPiece( tor, block );
|
2011-02-23 03:54:04 +00:00
|
|
|
const int blockSize = tr_torBlockCountBytes( tor, block );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
getCompleteBlocks(cp)[piece]++;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_bitsetAdd( &cp->blockBitset, block );
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
cp->sizeNow += blockSize;
|
2011-02-15 16:04:56 +00:00
|
|
|
if( !tor->info.pieces[piece].dnd )
|
|
|
|
cp->blocksWantedCompleteLazy++;
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
cp->sizeWhenDoneIsDirty = TRUE;
|
|
|
|
cp->haveValidIsDirty = TRUE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-15 23:29:56 +00:00
|
|
|
|
2009-01-07 02:22:11 +00:00
|
|
|
tr_bool
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_cpBlockBitsetInit( tr_completion * cp, const tr_bitset * blocks )
|
2009-01-06 07:45:15 +00:00
|
|
|
{
|
2010-06-30 16:40:19 +00:00
|
|
|
tr_bool success = FALSE;
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_torrent * tor = cp->tor;
|
2009-01-06 07:45:15 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
/* start cp with a state where it thinks we have nothing */
|
|
|
|
tr_cpReset( cp );
|
2009-01-06 07:45:15 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
if( blocks->haveAll )
|
2009-01-06 07:45:15 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_bitsetSetHaveAll( &cp->blockBitset );
|
|
|
|
cp->sizeNow = tor->info.totalSize;
|
2009-01-06 07:45:15 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
success = TRUE;
|
|
|
|
}
|
|
|
|
else if( blocks->haveNone )
|
|
|
|
{
|
|
|
|
/* already reset... */
|
|
|
|
success = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const tr_bitfield * src = &blocks->bitfield;
|
|
|
|
tr_bitfield * tgt = &cp->blockBitset.bitfield;
|
2009-01-09 17:55:48 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_bitfieldConstruct( tgt, tor->blockCount );
|
2009-01-07 02:22:11 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
/* The bitfield of block flags is typically loaded from a resume file.
|
|
|
|
Test the bitfield's length in case the resume file is corrupt */
|
|
|
|
if(( success = src->byteCount == tgt->byteCount ))
|
2009-01-06 07:45:15 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
size_t i = 0;
|
|
|
|
uint16_t * complete_blocks_in_piece = getCompleteBlocks( cp );
|
|
|
|
|
|
|
|
/* init our block bitfield from the one passed in */
|
|
|
|
memcpy( tgt->bits, src->bits, src->byteCount );
|
|
|
|
|
|
|
|
/* update cp.sizeNow and the cp.blockBitset flags */
|
|
|
|
i = tr_bitfieldCountTrueBits( tgt );
|
|
|
|
if( i == tor->blockCount ) {
|
|
|
|
tr_bitsetSetHaveAll( &cp->blockBitset );
|
|
|
|
cp->sizeNow = cp->tor->info.totalSize;
|
|
|
|
} else if( !i ) {
|
|
|
|
tr_bitsetSetHaveNone( &cp->blockBitset );
|
|
|
|
cp->sizeNow = 0;
|
|
|
|
} else {
|
|
|
|
cp->blockBitset.haveAll = cp->blockBitset.haveNone = FALSE;
|
|
|
|
cp->sizeNow = tr_bitfieldCountRange( tgt, 0, tor->blockCount-1 );
|
|
|
|
cp->sizeNow *= tor->blockSize;
|
|
|
|
if( tr_bitfieldHas( tgt, tor->blockCount-1 ) )
|
|
|
|
cp->sizeNow += tr_torBlockCountBytes( tor, tor->blockCount-1 );
|
2009-01-07 02:22:11 +00:00
|
|
|
}
|
2009-01-06 07:45:15 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
/* update complete_blocks_in_piece */
|
|
|
|
for( i=0; i<tor->info.pieceCount; ++i ) {
|
|
|
|
tr_block_index_t first, last;
|
|
|
|
tr_torGetPieceBlockRange( tor, i, &first, &last );
|
|
|
|
complete_blocks_in_piece[i] = tr_bitfieldCountRange( src, first, last+1 );
|
|
|
|
}
|
2009-01-07 02:22:11 +00:00
|
|
|
}
|
2009-01-06 07:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
uint64_t
|
|
|
|
tr_cpHaveValid( const tr_completion * ccp )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
if( ccp->haveValidIsDirty )
|
|
|
|
{
|
|
|
|
tr_piece_index_t i;
|
|
|
|
uint64_t size = 0;
|
|
|
|
tr_completion * cp = (tr_completion *) ccp; /* mutable */
|
|
|
|
const tr_torrent * tor = ccp->tor;
|
|
|
|
const tr_info * info = &tor->info;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
for( i=0; i<info->pieceCount; ++i )
|
|
|
|
if( tr_cpPieceIsComplete( ccp, i ) )
|
|
|
|
size += tr_torPieceCountBytes( tor, i );
|
2008-11-03 17:01:08 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
cp->haveValidIsDirty = FALSE;
|
|
|
|
cp->haveValidLazy = size;
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
return ccp->haveValidLazy;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2007-09-26 01:55:04 +00:00
|
|
|
|
2008-11-05 04:50:03 +00:00
|
|
|
uint64_t
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_cpSizeWhenDone( const tr_completion * ccp )
|
2008-11-05 04:50:03 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
if( ccp->sizeWhenDoneIsDirty )
|
2008-11-05 04:50:03 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_piece_index_t i;
|
|
|
|
uint64_t size = 0;
|
|
|
|
tr_completion * cp = (tr_completion *) ccp; /* mutable */
|
|
|
|
const tr_torrent * tor = ccp->tor;
|
|
|
|
const tr_info * info = &tor->info;
|
|
|
|
|
|
|
|
for( i=0; i<info->pieceCount; ++i )
|
|
|
|
if( !info->pieces[i].dnd || tr_cpPieceIsComplete( cp, i ) )
|
|
|
|
size += tr_torPieceCountBytes( tor, i );
|
|
|
|
|
|
|
|
cp->sizeWhenDoneIsDirty = FALSE;
|
|
|
|
cp->sizeWhenDoneLazy = size;
|
2008-11-05 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
return ccp->sizeWhenDoneLazy;
|
2008-11-05 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
void
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_cpGetAmountDone( const tr_completion * cp, float * tab, int tabCount )
|
2008-04-24 15:25:01 +00:00
|
|
|
{
|
2011-03-05 03:51:57 +00:00
|
|
|
int i;
|
|
|
|
const float interval = cp->tor->info.pieceCount / (float)tabCount;
|
|
|
|
const tr_bool seed = isSeed( cp );
|
|
|
|
|
|
|
|
for( i=0; i<tabCount; ++i ) {
|
|
|
|
if( seed )
|
|
|
|
tab[i] = 1.0f;
|
|
|
|
else {
|
|
|
|
const tr_piece_index_t piece = (tr_piece_index_t)i * interval;
|
|
|
|
tab[i] = getCompleteBlocks(cp)[piece] / (float)countBlocksInPiece( cp->tor, piece );
|
|
|
|
}
|
|
|
|
}
|
2007-09-26 01:55:04 +00:00
|
|
|
}
|
2009-01-02 17:01:55 +00:00
|
|
|
|
|
|
|
int
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_cpMissingBlocksInPiece( const tr_completion * cp, tr_piece_index_t i )
|
2009-01-02 17:01:55 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
if( isSeed( cp ) )
|
|
|
|
return 0;
|
2009-01-02 17:01:55 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
return countBlocksInPiece( cp->tor, i ) - getCompleteBlocks(cp)[i];
|
2009-01-02 17:01:55 +00:00
|
|
|
}
|
2009-01-12 21:59:53 +00:00
|
|
|
|
|
|
|
tr_bool
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t i )
|
2009-01-12 21:59:53 +00:00
|
|
|
{
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_block_index_t f, l;
|
2009-01-12 21:59:53 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
if( cp->tor->info.files[i].length == 0 )
|
|
|
|
return TRUE;
|
2009-01-12 21:59:53 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
tr_torGetFileBlockRange( cp->tor, i, &f, &l );
|
|
|
|
return tr_bitsetCountRange( &cp->blockBitset, f, l+1 ) == (l+1-f);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bitfield *
|
|
|
|
tr_cpCreatePieceBitfield( const tr_completion * cp )
|
|
|
|
{
|
|
|
|
tr_piece_index_t i;
|
|
|
|
const tr_piece_index_t n = cp->tor->info.pieceCount;
|
|
|
|
tr_bitfield * bf = tr_bitfieldNew( n );
|
2009-01-12 21:59:53 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
if( tr_cpPieceIsComplete( cp, i ) )
|
|
|
|
tr_bitfieldAdd( bf, i );
|
2011-01-31 16:43:37 +00:00
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
return bf;
|
2009-01-12 21:59:53 +00:00
|
|
|
}
|