2009-01-07 06:53:29 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2009-2014 Mnemosyne LLC
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
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-07-02 13:20:17 +00:00
|
|
|
#include <assert.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
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-11-12 22:26:10 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpReset (tr_completion * cp)
|
2007-11-12 22:26:10 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
cp->sizeNow = 0;
|
|
|
|
cp->sizeWhenDoneIsDirty = true;
|
|
|
|
cp->haveValidIsDirty = true;
|
|
|
|
tr_bitfieldSetHasNone (&cp->blockBitfield);
|
2007-11-12 22:26:10 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpConstruct (tr_completion * cp, tr_torrent * tor)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
cp->tor = tor;
|
|
|
|
tr_bitfieldConstruct (&cp->blockBitfield, tor->blockCount);
|
|
|
|
tr_cpReset (cp);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 04:14:57 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpBlockInit (tr_completion * cp, const tr_bitfield * b)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
tr_cpReset (cp);
|
|
|
|
|
|
|
|
/* set blockBitfield */
|
|
|
|
tr_bitfieldSetFromBitfield (&cp->blockBitfield, b);
|
|
|
|
|
|
|
|
/* set sizeNow */
|
|
|
|
cp->sizeNow = tr_bitfieldCountTrueBits (&cp->blockBitfield);
|
|
|
|
assert (cp->sizeNow <= cp->tor->blockCount);
|
|
|
|
cp->sizeNow *= cp->tor->blockSize;
|
|
|
|
if (tr_bitfieldHas (b, cp->tor->blockCount-1))
|
|
|
|
cp->sizeNow -= (cp->tor->blockSize - cp->tor->lastBlockSize);
|
|
|
|
|
|
|
|
assert (cp->sizeNow <= cp->tor->info.totalSize);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
tr_completeness
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpGetStatus (const tr_completion * cp)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
if (tr_cpHasAll (cp))
|
|
|
|
return TR_SEED;
|
|
|
|
|
|
|
|
if (!tr_torrentHasMetadata (cp->tor))
|
2011-02-23 03:54:04 +00:00
|
|
|
return TR_LEECH;
|
2012-12-15 04:28:19 +00:00
|
|
|
|
|
|
|
if (cp->sizeNow == tr_cpSizeWhenDone (cp))
|
|
|
|
return TR_PARTIAL_SEED;
|
|
|
|
|
|
|
|
return TR_LEECH;
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpPieceRem (tr_completion * cp, tr_piece_index_t piece)
|
2007-07-28 15:43:34 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
tr_block_index_t i, f, l;
|
|
|
|
const tr_torrent * tor = cp->tor;
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
tr_torGetPieceBlockRange (cp->tor, piece, &f, &l);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
for (i=f; i<=l; ++i)
|
|
|
|
if (tr_cpBlockIsComplete (cp, i))
|
|
|
|
cp->sizeNow -= tr_torBlockCountBytes (tor, i);
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
cp->haveValidIsDirty = true;
|
|
|
|
cp->sizeWhenDoneIsDirty = true;
|
|
|
|
tr_bitfieldRemRange (&cp->blockBitfield, f, l+1);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-27 16:08:20 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpPieceAdd (tr_completion * cp, tr_piece_index_t piece)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
tr_block_index_t i, f, l;
|
|
|
|
tr_torGetPieceBlockRange (cp->tor, piece, &f, &l);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
for (i=f; i<=l; ++i)
|
|
|
|
tr_cpBlockAdd (cp, i);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpBlockAdd (tr_completion * cp, tr_block_index_t block)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
const tr_torrent * tor = cp->tor;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
if (!tr_cpBlockIsComplete (cp, block))
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
const tr_piece_index_t piece = tr_torBlockPiece (cp->tor, block);
|
2012-12-15 03:47:11 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
tr_bitfieldAdd (&cp->blockBitfield, block);
|
|
|
|
cp->sizeNow += tr_torBlockCountBytes (tor, block);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
cp->haveValidIsDirty = true;
|
|
|
|
cp->sizeWhenDoneIsDirty |= tor->info.pieces[piece].dnd;
|
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
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpHaveValid (const tr_completion * ccp)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
if (ccp->haveValidIsDirty)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +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 (tr_cpPieceIsComplete (ccp, i))
|
|
|
|
size += tr_torPieceCountBytes (tor, i);
|
|
|
|
|
|
|
|
cp->haveValidLazy = size;
|
|
|
|
cp->haveValidIsDirty = false;
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2012-12-15 04:28:19 +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
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpSizeWhenDone (const tr_completion * ccp)
|
2008-11-05 04:50:03 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
if (ccp->sizeWhenDoneIsDirty)
|
2008-11-05 04:50:03 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
uint64_t size = 0;
|
|
|
|
const tr_torrent * tor = ccp->tor;
|
|
|
|
const tr_info * inf = tr_torrentInfo (tor);
|
|
|
|
tr_completion * cp = (tr_completion *) ccp; /* mutable */
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
if (tr_cpHasAll (ccp))
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
size = inf->totalSize;
|
2011-03-29 01:17:18 +00:00
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
else
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
tr_piece_index_t p;
|
2011-03-29 01:17:18 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
for (p=0; p<inf->pieceCount; ++p)
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
uint64_t n = 0;
|
|
|
|
const uint64_t pieceSize = tr_torPieceCountBytes (tor, p);
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
if (!inf->pieces[p].dnd)
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
n = pieceSize;
|
2011-03-29 01:17:18 +00:00
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
else
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
tr_block_index_t f, l;
|
|
|
|
tr_torGetPieceBlockRange (cp->tor, p, &f, &l);
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
n = tr_bitfieldCountRange (&cp->blockBitfield, f, l+1);
|
|
|
|
n *= cp->tor->blockSize;
|
|
|
|
if (l == (cp->tor->blockCount-1) && tr_bitfieldHas (&cp->blockBitfield, l))
|
|
|
|
n -= (cp->tor->blockSize - cp->tor->lastBlockSize);
|
2011-03-29 01:17:18 +00:00
|
|
|
}
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
assert (n <= tr_torPieceCountBytes (tor, p));
|
|
|
|
size += n;
|
2011-03-29 01:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
assert (size <= inf->totalSize);
|
|
|
|
assert (size >= cp->sizeNow);
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
cp->sizeWhenDoneLazy = size;
|
|
|
|
cp->sizeWhenDoneIsDirty = false;
|
2008-11-05 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
return ccp->sizeWhenDoneLazy;
|
2008-11-05 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 06:18:48 +00:00
|
|
|
uint64_t
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpLeftUntilDone (const tr_completion * cp)
|
2011-09-26 06:18:48 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
const uint64_t sizeWhenDone = tr_cpSizeWhenDone (cp);
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
assert (sizeWhenDone >= cp->sizeNow);
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
return sizeWhenDone - cp->sizeNow;
|
2011-09-26 06:18:48 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpGetAmountDone (const tr_completion * cp, float * tab, int tabCount)
|
2008-04-24 15:25:01 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
int i;
|
|
|
|
const bool seed = tr_cpHasAll (cp);
|
|
|
|
const float interval = cp->tor->info.pieceCount / (float)tabCount;
|
|
|
|
|
|
|
|
for (i=0; i<tabCount; ++i)
|
|
|
|
{
|
|
|
|
if (seed)
|
|
|
|
{
|
|
|
|
tab[i] = 1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_block_index_t f, l;
|
|
|
|
const tr_piece_index_t piece = (tr_piece_index_t)i * interval;
|
|
|
|
tr_torGetPieceBlockRange (cp->tor, piece, &f, &l);
|
|
|
|
tab[i] = tr_bitfieldCountRange (&cp->blockBitfield, f, l+1) / (float)(l+1-f);
|
2011-03-05 03:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-26 01:55:04 +00:00
|
|
|
}
|
2009-01-02 17:01:55 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
size_t
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpMissingBlocksInPiece (const tr_completion * cp, tr_piece_index_t piece)
|
2009-01-02 17:01:55 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
if (tr_cpHasAll (cp))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_block_index_t f, l;
|
|
|
|
tr_torGetPieceBlockRange (cp->tor, piece, &f, &l);
|
|
|
|
return (l+1-f) - tr_bitfieldCountRange (&cp->blockBitfield, f, l+1);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-02 17:01:55 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
size_t
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpMissingBytesInPiece (const tr_completion * cp, tr_piece_index_t piece)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
if (tr_cpHasAll (cp))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t haveBytes = 0;
|
|
|
|
tr_block_index_t f, l;
|
|
|
|
const size_t pieceByteSize = tr_torPieceCountBytes (cp->tor, piece);
|
|
|
|
tr_torGetPieceBlockRange (cp->tor, piece, &f, &l);
|
|
|
|
if (f != l)
|
|
|
|
{
|
|
|
|
/* nb: we don't pass the usual l+1 here to tr_bitfieldCountRange ().
|
|
|
|
It's faster to handle the last block separately because its size
|
|
|
|
needs to be checked separately. */
|
|
|
|
haveBytes = tr_bitfieldCountRange (&cp->blockBitfield, f, l);
|
|
|
|
haveBytes *= cp->tor->blockSize;
|
2011-07-02 13:20:17 +00:00
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
|
|
|
if (tr_bitfieldHas (&cp->blockBitfield, l)) /* handle the last block */
|
|
|
|
haveBytes += tr_torBlockCountBytes (cp->tor, l);
|
|
|
|
|
|
|
|
assert (haveBytes <= pieceByteSize);
|
|
|
|
return pieceByteSize - haveBytes;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2009-01-02 17:01:55 +00:00
|
|
|
}
|
2009-01-12 21:59:53 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpFileIsComplete (const tr_completion * cp, tr_file_index_t i)
|
2009-01-12 21:59:53 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
if (cp->tor->info.files[i].length == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_block_index_t f, l;
|
|
|
|
tr_torGetFileBlockRange (cp->tor, i, &f, &l);
|
|
|
|
return tr_bitfieldCountRange (&cp->blockBitfield, f, l+1) == (l+1-f);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
void *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpCreatePieceBitfield (const tr_completion * cp, size_t * byte_count)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
void * ret;
|
|
|
|
tr_piece_index_t n;
|
|
|
|
tr_bitfield pieces;
|
|
|
|
|
|
|
|
assert (tr_torrentHasMetadata (cp->tor));
|
|
|
|
|
|
|
|
n = cp->tor->info.pieceCount;
|
|
|
|
tr_bitfieldConstruct (&pieces, n);
|
|
|
|
|
|
|
|
if (tr_cpHasAll (cp))
|
|
|
|
{
|
|
|
|
tr_bitfieldSetHasAll (&pieces);
|
|
|
|
}
|
|
|
|
else if (!tr_cpHasNone (cp))
|
|
|
|
{
|
|
|
|
tr_piece_index_t i;
|
|
|
|
bool * flags = tr_new (bool, n);
|
|
|
|
for (i=0; i<n; ++i)
|
|
|
|
flags[i] = tr_cpPieceIsComplete (cp, i);
|
|
|
|
tr_bitfieldSetFromFlags (&pieces, flags, n);
|
|
|
|
tr_free (flags);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2009-01-12 21:59:53 +00:00
|
|
|
|
2012-12-15 04:28:19 +00:00
|
|
|
ret = tr_bitfieldGetRaw (&pieces, byte_count);
|
|
|
|
tr_bitfieldDestruct (&pieces);
|
|
|
|
return ret;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2011-01-31 16:43:37 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
double
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpPercentComplete (const tr_completion * cp)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
const double ratio = tr_getRatio (cp->sizeNow, cp->tor->info.totalSize);
|
|
|
|
|
|
|
|
if ((int)ratio == TR_RATIO_NA)
|
|
|
|
return 0.0;
|
|
|
|
else if ((int)ratio == TR_RATIO_INF)
|
|
|
|
return 1.0;
|
|
|
|
else
|
|
|
|
return ratio;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cpPercentDone (const tr_completion * cp)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2012-12-15 04:28:19 +00:00
|
|
|
const double ratio = tr_getRatio (cp->sizeNow, tr_cpSizeWhenDone (cp));
|
|
|
|
const int iratio = (int)ratio;
|
|
|
|
return ((iratio == TR_RATIO_NA) || (iratio == TR_RATIO_INF)) ? 0.0 : ratio;
|
2009-01-12 21:59:53 +00:00
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|