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
|
|
|
{
|
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
|
|
|
|
|
|
|
uint8_t doneDirty;
|
|
|
|
uint64_t doneHave;
|
|
|
|
uint64_t doneTotal;
|
|
|
|
uint64_t completeHave;
|
2007-06-18 03:40:41 +00:00
|
|
|
};
|
|
|
|
|
2007-11-12 22:26:10 +00:00
|
|
|
static void
|
|
|
|
tr_cpReset( tr_completion * cp )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = cp->tor;
|
|
|
|
|
|
|
|
tr_bitfieldClear( cp->pieceBitfield );
|
|
|
|
tr_bitfieldClear( cp->blockBitfield );
|
|
|
|
memset( cp->completeBlocks, 0, sizeof(uint16_t) * tor->info.pieceCount );
|
|
|
|
|
|
|
|
cp->doneDirty = TRUE;
|
|
|
|
cp->doneHave = 0;
|
|
|
|
cp->doneTotal = 0;
|
|
|
|
cp->completeHave = 0;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_completion * tr_cpInit( tr_torrent * tor )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_completion * cp;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
cp = tr_new( tr_completion, 1 );
|
2006-07-16 19:39:23 +00:00
|
|
|
cp->tor = tor;
|
2007-03-05 23:03:38 +00:00
|
|
|
cp->blockBitfield = tr_bitfieldNew( tor->blockCount );
|
|
|
|
cp->pieceBitfield = tr_bitfieldNew( tor->info.pieceCount );
|
2007-07-27 23:07:26 +00:00
|
|
|
cp->completeBlocks = tr_new( uint16_t, tor->info.pieceCount );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_cpReset( cp );
|
|
|
|
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_cpClose( tr_completion * cp )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-27 14:54:31 +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 );
|
|
|
|
tr_free( cp );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cpEnsureDoneValid( const tr_completion * ccp )
|
2007-07-28 15:43:34 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_torrent * tor = ccp->tor;
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_info * info = &tor->info;
|
2007-07-28 15:43:34 +00:00
|
|
|
uint64_t have=0, total=0;
|
|
|
|
int i;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_completion * cp ;
|
2007-07-28 15:43:34 +00:00
|
|
|
|
|
|
|
if( !ccp->doneDirty )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* too bad C doesn't have 'mutable' */
|
2007-09-20 16:32:01 +00:00
|
|
|
cp = (tr_completion*) ccp;
|
2007-07-28 15:43:34 +00:00
|
|
|
cp->doneDirty = FALSE;
|
|
|
|
|
|
|
|
for( i=0; i<info->pieceCount; ++i ) {
|
|
|
|
if( !info->pieces[i].dnd ) {
|
|
|
|
total += info->pieceSize;
|
|
|
|
have += cp->completeBlocks[ i ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
have *= tor->blockSize;
|
|
|
|
|
|
|
|
/* the last piece/block is probably smaller than the others */
|
|
|
|
if( !info->pieces[info->pieceCount-1].dnd ) {
|
2007-08-08 05:17:51 +00:00
|
|
|
total -= ( info->pieceSize - tr_torPieceCountBytes(tor,info->pieceCount-1) );
|
2007-07-28 15:43:34 +00:00
|
|
|
if( tr_cpBlockIsComplete( cp, tor->blockCount-1 ) )
|
2007-08-08 05:17:51 +00:00
|
|
|
have -= ( tor->blockSize - tr_torBlockCountBytes(tor,tor->blockCount-1) );
|
2007-07-28 15:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert( have <= total );
|
|
|
|
assert( total <= info->totalSize );
|
|
|
|
|
|
|
|
cp->doneHave = have;
|
|
|
|
cp->doneTotal = total;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cpInvalidateDND ( tr_completion * cp )
|
2007-07-28 15:43:34 +00:00
|
|
|
{
|
|
|
|
cp->doneDirty = TRUE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int
|
|
|
|
tr_cpPieceIsComplete( const tr_completion * cp, int piece )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-10-31 04:23:51 +00:00
|
|
|
assert( piece >= 0 );
|
|
|
|
assert( piece < cp->tor->info.pieceCount );
|
|
|
|
assert( cp->completeBlocks[piece] <= tr_torPieceCountBlocks(cp->tor,piece) );
|
|
|
|
|
|
|
|
return cp->completeBlocks[piece] == tr_torPieceCountBlocks(cp->tor,piece);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_bitfield * tr_cpPieceBitfield( const tr_completion * cp )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return cp->pieceBitfield;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_cpPieceAdd( tr_completion * cp, int piece )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_torrent * tor = cp->tor;
|
2007-07-28 15:43:34 +00:00
|
|
|
const int start = tr_torPieceFirstBlock(tor,piece);
|
|
|
|
const int end = start + tr_torPieceCountBlocks(tor,piece);
|
|
|
|
int 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
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_cpPieceRem( tr_completion * cp, int piece )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_torrent * tor = cp->tor;
|
2007-07-28 15:43:34 +00:00
|
|
|
const int start = tr_torPieceFirstBlock(tor,piece);
|
|
|
|
const int end = start + tr_torPieceCountBlocks(tor,piece);
|
|
|
|
int block;
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-07-24 20:49:47 +00:00
|
|
|
assert( cp != NULL );
|
|
|
|
assert( 0 <= piece );
|
|
|
|
assert( piece < tor->info.pieceCount );
|
2007-07-28 15:43:34 +00:00
|
|
|
assert( 0 <= start );
|
|
|
|
assert( start < tor->blockCount );
|
|
|
|
assert( start <= end );
|
|
|
|
assert( end <= tor->blockCount );
|
|
|
|
|
|
|
|
for( block=start; block<end; ++block ) {
|
|
|
|
if( tr_cpBlockIsComplete( cp, block ) ) {
|
|
|
|
const int blockSize = tr_torBlockCountBytes( tor, block );
|
|
|
|
cp->completeHave -= blockSize;
|
|
|
|
if( !tor->info.pieces[piece].dnd )
|
|
|
|
cp->doneHave -= blockSize;
|
|
|
|
}
|
|
|
|
}
|
2007-07-24 20:49:47 +00:00
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int tr_cpBlockIsComplete( const tr_completion * cp, int block )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
return tr_bitfieldHas( cp->blockBitfield, block ) ? 1 : 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cpBlockAdd( tr_completion * cp, int 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 ) )
|
|
|
|
{
|
2007-07-28 15:43:34 +00:00
|
|
|
const int piece = tr_torBlockPiece( tor, block );
|
|
|
|
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
|
|
|
|
|
|
|
cp->completeHave += blockSize;
|
|
|
|
|
|
|
|
if( !tor->info.pieces[piece].dnd )
|
|
|
|
cp->doneHave += blockSize;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_bitfield * tr_cpBlockBitfield( const tr_completion * cp )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
assert( cp != NULL );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
return cp->blockBitfield;
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * bitfield )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
int i;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
assert( cp != NULL );
|
|
|
|
assert( bitfield != NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_cpReset( cp );
|
|
|
|
|
|
|
|
for( i=0; i < cp->tor->blockCount; ++i )
|
|
|
|
if( tr_bitfieldHas( bitfield, i ) )
|
|
|
|
tr_cpBlockAdd( cp, i );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
float tr_cpPercentBlocksInPiece( const tr_completion * cp, int piece )
|
2006-10-12 23:34:20 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
assert( cp != NULL );
|
2006-10-13 00:23:20 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
return (double)cp->completeBlocks[piece] / tr_torPieceCountBlocks(cp->tor,piece);
|
2006-10-12 23:34:20 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
float
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cpPercentComplete ( const tr_completion * cp )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-07-28 15:43:34 +00:00
|
|
|
return (double)cp->completeHave / cp->tor->info.totalSize;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cpLeftUntilComplete ( const tr_completion * cp )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-07-28 15:43:34 +00:00
|
|
|
return cp->tor->info.totalSize - cp->completeHave;
|
|
|
|
}
|
2007-06-30 21:11:17 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
float
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_cpPercentDone( const tr_completion * cp )
|
2007-07-28 15:43:34 +00:00
|
|
|
{
|
|
|
|
tr_cpEnsureDoneValid( cp );
|
2007-06-30 21:11:17 +00:00
|
|
|
|
2007-08-21 16:26:01 +00:00
|
|
|
return cp->doneTotal ? (double)cp->doneHave / cp->doneTotal : 1.0;
|
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
|
|
|
{
|
2007-07-28 15:43:34 +00:00
|
|
|
tr_cpEnsureDoneValid( cp );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
return cp->doneTotal - cp->doneHave;
|
|
|
|
}
|
2007-06-18 03:40:41 +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
|
|
|
{
|
|
|
|
if( cp->completeHave >= cp->tor->info.totalSize )
|
|
|
|
return TR_CP_COMPLETE;
|
2007-06-30 21:11:17 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
tr_cpEnsureDoneValid( cp );
|
2007-06-30 21:11:17 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
if( cp->doneHave >= cp->doneTotal )
|
|
|
|
return TR_CP_DONE;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
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;
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_torrent * tor = cp->tor;
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_info * info = &tor->info;
|
2007-07-26 19:15:36 +00:00
|
|
|
int i;
|
2007-06-30 21:11:17 +00:00
|
|
|
|
2007-07-26 19:15:36 +00:00
|
|
|
for( i=0; i<info->pieceCount; ++i )
|
|
|
|
if( tr_cpPieceIsComplete( cp, i ) )
|
|
|
|
++b;
|
|
|
|
|
2007-07-27 11:56:34 +00:00
|
|
|
b *= info->pieceSize;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-07-27 11:56:34 +00:00
|
|
|
if( tr_cpPieceIsComplete( cp, info->pieceCount-1 ) )
|
|
|
|
b -= (info->pieceSize - (info->totalSize % info->pieceSize));
|
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 )
|
|
|
|
{
|
2007-11-12 22:26:10 +00:00
|
|
|
return cp->completeHave;
|
2007-09-26 01:55:04 +00:00
|
|
|
}
|