2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* Copyright (c) 2005-2008 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
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
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_completion
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
unsigned int sizeWhenDoneIsDirty : 1;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor;
|
2007-06-30 21:11:17 +00:00
|
|
|
|
|
|
|
/* do we have this block? */
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield * blockBitfield;
|
2007-06-30 21:11:17 +00:00
|
|
|
|
|
|
|
/* do we have this piece? */
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield * pieceBitfield;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
/* a block is complete if and only if we have it */
|
2007-07-27 23:07:26 +00:00
|
|
|
uint16_t * completeBlocks;
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
/* number of bytes we'll have when done downloading. [0..info.totalSize]
|
|
|
|
DON'T access this directly; it's a lazy field.
|
|
|
|
use tr_cpSizeWhenDone() instead! */
|
|
|
|
uint64_t sizeWhenDoneLazy;
|
|
|
|
|
|
|
|
/* number of bytes we want or have now. [0..sizeWhenDone] */
|
|
|
|
uint64_t sizeNow;
|
2007-06-18 03:40:41 +00:00
|
|
|
};
|
|
|
|
|
2007-11-12 22:26:10 +00:00
|
|
|
static void
|
|
|
|
tr_cpReset( tr_completion * cp )
|
|
|
|
{
|
|
|
|
tr_bitfieldClear( cp->pieceBitfield );
|
|
|
|
tr_bitfieldClear( cp->blockBitfield );
|
2008-04-24 15:25:01 +00:00
|
|
|
memset( cp->completeBlocks, 0, sizeof(uint16_t) * cp->tor->info.pieceCount );
|
|
|
|
cp->sizeNow = 0;
|
|
|
|
cp->sizeWhenDoneIsDirty = 1;
|
2007-11-12 22:26:10 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
tr_completion *
|
|
|
|
tr_cpInit( tr_torrent * tor )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
tr_completion * cp = tr_new( tr_completion, 1 );
|
|
|
|
cp->tor = tor;
|
|
|
|
cp->blockBitfield = tr_bitfieldNew( tor->blockCount );
|
|
|
|
cp->pieceBitfield = tr_bitfieldNew( tor->info.pieceCount );
|
|
|
|
cp->completeBlocks = tr_new( uint16_t, tor->info.pieceCount );
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_cpReset( cp );
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
void
|
|
|
|
tr_cpClose( tr_completion * cp )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
tr_free ( cp->completeBlocks );
|
2007-03-05 23:03:38 +00:00
|
|
|
tr_bitfieldFree( cp->pieceBitfield );
|
2007-06-27 14:54:31 +00:00
|
|
|
tr_bitfieldFree( cp->blockBitfield );
|
2008-04-24 15:25:01 +00:00
|
|
|
tr_free ( cp );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
void
|
|
|
|
tr_cpInvalidateDND ( tr_completion * cp )
|
|
|
|
{
|
|
|
|
cp->sizeWhenDoneIsDirty = 1;
|
|
|
|
}
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
uint64_t
|
|
|
|
tr_cpSizeWhenDone( const tr_completion * ccp )
|
2007-07-28 15:43:34 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
if( ccp->sizeWhenDoneIsDirty )
|
2008-04-03 21:38:32 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
tr_completion * cp = (tr_completion *) ccp; /* mutable */
|
2008-04-26 14:52:05 +00:00
|
|
|
const tr_torrent * tor = cp->tor;
|
|
|
|
const tr_info * info = &tor->info;
|
2008-04-03 21:38:32 +00:00
|
|
|
tr_piece_index_t i;
|
2008-04-24 15:25:01 +00:00
|
|
|
uint64_t size = 0;
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
for( i=0; i<info->pieceCount; ++i )
|
2008-04-26 14:52:05 +00:00
|
|
|
{
|
|
|
|
if( !info->pieces[i].dnd ) {
|
2008-06-05 16:23:03 +00:00
|
|
|
/* we want the piece... */
|
2008-04-26 14:52:05 +00:00
|
|
|
size += tr_torPieceCountBytes( tor, i );
|
|
|
|
} else if( tr_cpPieceIsComplete( cp, i ) ) {
|
2008-06-05 16:23:03 +00:00
|
|
|
/* we have the piece... */
|
2008-04-26 14:52:05 +00:00
|
|
|
size += tr_torPieceCountBytes( tor, i );
|
|
|
|
} else if( cp->completeBlocks[i] ) {
|
2008-06-05 16:23:03 +00:00
|
|
|
/* we have part of the piece... */
|
2008-04-26 14:52:05 +00:00
|
|
|
const tr_block_index_t b = tr_torPieceFirstBlock( tor, i );
|
|
|
|
const tr_block_index_t e = b + tr_torPieceCountBlocks( tor, i );
|
|
|
|
tr_block_index_t j;
|
|
|
|
for( j=b; j<e; ++j )
|
|
|
|
if( tr_cpBlockIsComplete( cp, j ) )
|
|
|
|
size += tr_torBlockCountBytes( tor, j );
|
|
|
|
}
|
|
|
|
}
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
cp->sizeWhenDoneLazy = size;
|
|
|
|
cp->sizeWhenDoneIsDirty = 0;
|
2007-07-28 15:43:34 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
assert( ccp->sizeWhenDoneLazy <= ccp->tor->info.totalSize );
|
|
|
|
assert( ccp->sizeWhenDoneLazy >= ccp->sizeNow );
|
|
|
|
return ccp->sizeWhenDoneLazy;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_cpPieceIsComplete( const tr_completion * cp,
|
|
|
|
tr_piece_index_t piece )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-10-31 04:23:51 +00:00
|
|
|
return cp->completeBlocks[piece] == tr_torPieceCountBlocks(cp->tor,piece);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
const tr_bitfield *
|
|
|
|
tr_cpPieceBitfield( const tr_completion * cp )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return cp->pieceBitfield;
|
|
|
|
}
|
|
|
|
|
2008-01-27 16:08:20 +00:00
|
|
|
void
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_cpPieceAdd( tr_completion * cp, tr_piece_index_t piece )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_torrent * tor = cp->tor;
|
2008-03-22 18:10:59 +00:00
|
|
|
const tr_block_index_t start = tr_torPieceFirstBlock(tor,piece);
|
|
|
|
const tr_block_index_t end = start + tr_torPieceCountBlocks(tor, piece);
|
|
|
|
tr_block_index_t i;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
for( i=start; i<end; ++i )
|
|
|
|
tr_cpBlockAdd( cp, i );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-27 16:08:20 +00:00
|
|
|
void
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_cpPieceRem( tr_completion * cp, tr_piece_index_t piece )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_torrent * tor = cp->tor;
|
2008-04-24 15:25:01 +00:00
|
|
|
const tr_block_index_t start = tr_torPieceFirstBlock( tor, piece );
|
|
|
|
const tr_block_index_t end = start + tr_torPieceCountBlocks( tor, piece );
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_block_index_t block;
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( cp );
|
2007-07-24 20:49:47 +00:00
|
|
|
assert( piece < tor->info.pieceCount );
|
2007-07-28 15:43:34 +00:00
|
|
|
assert( start < tor->blockCount );
|
|
|
|
assert( start <= end );
|
|
|
|
assert( end <= tor->blockCount );
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
for( block=start; block<end; ++block )
|
|
|
|
if( tr_cpBlockIsComplete( cp, block ) )
|
|
|
|
cp->sizeNow -= tr_torBlockCountBytes( tor, block );
|
2007-07-24 20:49:47 +00:00
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
cp->sizeWhenDoneIsDirty = 1;
|
2007-06-18 03:40:41 +00:00
|
|
|
cp->completeBlocks[piece] = 0;
|
2007-07-28 15:43:34 +00:00
|
|
|
tr_bitfieldRemRange ( cp->blockBitfield, start, end );
|
2007-01-14 12:00:21 +00:00
|
|
|
tr_bitfieldRem( cp->pieceBitfield, piece );
|
|
|
|
}
|
|
|
|
|
2008-03-22 18:10:59 +00:00
|
|
|
int
|
|
|
|
tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t block )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
return tr_bitfieldHas( cp->blockBitfield, block );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
void
|
2008-03-22 18:10:59 +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 );
|
2007-07-28 15:43:34 +00:00
|
|
|
const int blockSize = tr_torBlockCountBytes( tor, block );
|
2007-07-24 11:50:29 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
++cp->completeBlocks[piece];
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
if( cp->completeBlocks[piece] == tr_torPieceCountBlocks( tor, piece ) )
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_bitfieldAdd( cp->pieceBitfield, piece );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_bitfieldAdd( cp->blockBitfield, block );
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
cp->sizeNow += blockSize;
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
cp->sizeWhenDoneIsDirty = 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-15 17:00:44 +00:00
|
|
|
const tr_bitfield *
|
|
|
|
tr_cpBlockBitfield( const tr_completion * cp )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-04-15 17:00:44 +00:00
|
|
|
assert( cp );
|
|
|
|
assert( cp->blockBitfield );
|
|
|
|
assert( cp->blockBitfield->bits );
|
2008-06-07 01:44:54 +00:00
|
|
|
assert( cp->blockBitfield->bitCount );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
return cp->blockBitfield;
|
|
|
|
}
|
|
|
|
|
2008-04-13 14:29:11 +00:00
|
|
|
tr_errno
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * bitfield )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_block_index_t i;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-04-15 17:00:44 +00:00
|
|
|
assert( cp );
|
|
|
|
assert( bitfield );
|
|
|
|
assert( cp->blockBitfield );
|
|
|
|
|
2008-06-11 02:19:40 +00:00
|
|
|
if( !tr_bitfieldTestFast( bitfield, cp->tor->blockCount-1 ) )
|
2008-04-13 14:29:11 +00:00
|
|
|
return TR_ERROR_ASSERT;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_cpReset( cp );
|
2008-06-09 00:59:33 +00:00
|
|
|
for( i=0; i<cp->tor->blockCount; ++i )
|
|
|
|
if( tr_bitfieldHasFast( bitfield, i ) )
|
|
|
|
tr_cpBlockAdd( cp, i );
|
2008-04-13 14:29:11 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-02-10 04:03:19 +00:00
|
|
|
int
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_cpMissingBlocksInPiece( const tr_completion * cp, tr_piece_index_t piece )
|
2008-02-10 04:03:19 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
return tr_torPieceCountBlocks( cp->tor, piece ) - cp->completeBlocks[piece];
|
2008-02-10 04:03:19 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
float
|
2008-04-24 15:25:01 +00:00
|
|
|
tr_cpPercentDone( const tr_completion * cp )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
return tr_getRatio( cp->sizeNow, tr_cpSizeWhenDone(cp) );
|
2007-07-28 15:43:34 +00:00
|
|
|
}
|
2007-06-30 21:11:17 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
float
|
2008-04-24 15:25:01 +00:00
|
|
|
tr_cpPercentComplete ( const tr_completion * cp )
|
2007-07-28 15:43:34 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
return tr_getRatio( cp->sizeNow, cp->tor->info.totalSize );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
uint64_t
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cpLeftUntilDone ( const tr_completion * cp )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
return tr_cpSizeWhenDone(cp) - cp->sizeNow;
|
2007-07-28 15:43:34 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-04-12 15:56:21 +00:00
|
|
|
uint64_t
|
2008-04-24 15:25:01 +00:00
|
|
|
tr_cpLeftUntilComplete ( const tr_completion * cp )
|
2008-04-12 15:56:21 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
return cp->tor->info.totalSize - cp->sizeNow;
|
2008-04-12 15:56:21 +00:00
|
|
|
}
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
cp_status_t
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cpGetStatus ( const tr_completion * cp )
|
2007-07-28 15:43:34 +00:00
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
if( cp->sizeNow == cp->tor->info.totalSize ) return TR_CP_COMPLETE;
|
|
|
|
if( cp->sizeNow == tr_cpSizeWhenDone(cp) ) return TR_CP_DONE;
|
|
|
|
return TR_CP_INCOMPLETE;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2007-09-26 01:55:04 +00:00
|
|
|
tr_cpHaveValid( const tr_completion * cp )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-07-26 19:15:36 +00:00
|
|
|
uint64_t b = 0;
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_piece_index_t i;
|
2008-04-24 15:25:01 +00:00
|
|
|
const tr_torrent * tor = cp->tor;
|
2007-06-30 21:11:17 +00:00
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
for( i=0; i<tor->info.pieceCount; ++i )
|
2007-07-26 19:15:36 +00:00
|
|
|
if( tr_cpPieceIsComplete( cp, i ) )
|
2008-04-24 15:25:01 +00:00
|
|
|
b += tr_torPieceCountBytes( tor, i );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-11-12 22:26:10 +00:00
|
|
|
return b;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2007-09-26 01:55:04 +00:00
|
|
|
|
|
|
|
uint64_t
|
|
|
|
tr_cpHaveTotal( const tr_completion * cp )
|
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
return cp->sizeNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_cpGetAmountDone( const tr_completion * cp, float * tab, int tabCount )
|
|
|
|
{
|
2008-06-09 23:05:14 +00:00
|
|
|
int i;
|
2008-06-09 23:58:31 +00:00
|
|
|
const tr_torrent * tor = cp->tor;
|
|
|
|
const float interval = tor->info.pieceCount / (float)tabCount;
|
|
|
|
const int isComplete = tr_cpGetStatus ( tor->completion ) == TR_CP_COMPLETE;
|
|
|
|
|
|
|
|
for( i=0; i<tabCount; ++i )
|
|
|
|
{
|
|
|
|
const tr_piece_index_t piece = i * interval;
|
|
|
|
|
|
|
|
if( tor == NULL )
|
2008-06-10 00:31:06 +00:00
|
|
|
tab[i] = 0.0f;
|
2008-06-09 23:58:31 +00:00
|
|
|
else if( isComplete || tr_cpPieceIsComplete( cp, piece ) )
|
2008-06-10 00:31:06 +00:00
|
|
|
tab[i] = 1.0f;
|
2008-06-09 23:58:31 +00:00
|
|
|
else
|
|
|
|
tab[i] = (float)cp->completeBlocks[piece] / tr_torPieceCountBlocks( tor, piece );
|
2008-04-24 15:25:01 +00:00
|
|
|
}
|
2007-09-26 01:55:04 +00:00
|
|
|
}
|