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
|
|
|
*/
|
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"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_cpReset(tr_completion* cp)
|
2007-11-12 22:26:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
cp->sizeNow = 0;
|
|
|
|
cp->sizeWhenDoneIsDirty = true;
|
|
|
|
cp->haveValidIsDirty = true;
|
2021-10-11 17:29:14 +00:00
|
|
|
cp->blockBitfield->setHasNone();
|
2007-11-12 22:26:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_cpConstruct(tr_completion* cp, tr_torrent* tor)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
cp->tor = tor;
|
2021-10-11 17:29:14 +00:00
|
|
|
cp->blockBitfield = new Bitfield(tor->blockCount);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cpReset(cp);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
void tr_cpBlockInit(tr_completion* cp, Bitfield const& b)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cpReset(cp);
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
// set blockBitfield
|
|
|
|
cp->blockBitfield->setFromBitfield(b);
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
// set sizeNow
|
|
|
|
cp->sizeNow = cp->blockBitfield->countBits();
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(cp->sizeNow <= cp->tor->blockCount);
|
2017-04-19 12:04:45 +00:00
|
|
|
cp->sizeNow *= cp->tor->blockSize;
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
if (b.readBit(cp->tor->blockCount - 1))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
cp->sizeNow -= (cp->tor->blockSize - cp->tor->lastBlockSize);
|
|
|
|
}
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(cp->sizeNow <= cp->tor->info.totalSize);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_completeness tr_cpGetStatus(tr_completion const* cp)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_cpHasAll(cp))
|
|
|
|
{
|
|
|
|
return TR_SEED;
|
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_torrentHasMetadata(cp->tor))
|
|
|
|
{
|
|
|
|
return TR_LEECH;
|
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (cp->sizeNow == tr_cpSizeWhenDone(cp))
|
|
|
|
{
|
|
|
|
return TR_PARTIAL_SEED;
|
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return TR_LEECH;
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_cpPieceRem(tr_completion* cp, tr_piece_index_t piece)
|
2007-07-28 15:43:34 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_block_index_t f;
|
|
|
|
tr_block_index_t l;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_torrent const* tor = cp->tor;
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torGetPieceBlockRange(cp->tor, piece, &f, &l);
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_block_index_t i = f; i <= l; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (tr_cpBlockIsComplete(cp, i))
|
|
|
|
{
|
|
|
|
cp->sizeNow -= tr_torBlockCountBytes(tor, i);
|
|
|
|
}
|
|
|
|
}
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
cp->haveValidIsDirty = true;
|
|
|
|
cp->sizeWhenDoneIsDirty = true;
|
2021-10-11 17:29:14 +00:00
|
|
|
cp->blockBitfield->clearBitRange(f, l + 1);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_cpPieceAdd(tr_completion* cp, tr_piece_index_t piece)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_block_index_t f;
|
|
|
|
tr_block_index_t l;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torGetPieceBlockRange(cp->tor, piece, &f, &l);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_block_index_t i = f; i <= l; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_cpBlockAdd(cp, i);
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_cpBlockAdd(tr_completion* cp, tr_block_index_t block)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_torrent const* tor = cp->tor;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_cpBlockIsComplete(cp, block))
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_piece_index_t const piece = tr_torBlockPiece(cp->tor, block);
|
2012-12-15 03:47:11 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
cp->blockBitfield->setBit(block);
|
2017-04-19 12:04:45 +00:00
|
|
|
cp->sizeNow += tr_torBlockCountBytes(tor, block);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
cp->haveValidIsDirty = true;
|
2019-07-14 12:40:41 +00:00
|
|
|
cp->sizeWhenDoneIsDirty = cp->sizeWhenDoneIsDirty || tor->info.pieces[piece].dnd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t tr_cpHaveValid(tr_completion const* ccp)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ccp->haveValidIsDirty)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t size = 0;
|
|
|
|
tr_completion* cp = (tr_completion*)ccp; /* mutable */
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_torrent const* tor = ccp->tor;
|
|
|
|
tr_info const* info = &tor->info;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_piece_index_t i = 0; i < info->pieceCount; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ccp->haveValidLazy;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2007-09-26 01:55:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t tr_cpSizeWhenDone(tr_completion const* ccp)
|
2008-11-05 04:50:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ccp->sizeWhenDoneIsDirty)
|
2008-11-05 04:50:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t size = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_torrent const* tor = ccp->tor;
|
|
|
|
tr_info const* inf = tr_torrentInfo(tor);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_completion* cp = (tr_completion*)ccp; /* mutable */
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_cpHasAll(ccp))
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size = inf->totalSize;
|
2011-03-29 01:17:18 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_piece_index_t p = 0; p < inf->pieceCount; ++p)
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t n = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t const pieceSize = tr_torPieceCountBytes(tor, p);
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!inf->pieces[p].dnd)
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
n = pieceSize;
|
2011-03-29 01:17:18 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2011-03-29 01:17:18 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_block_index_t f;
|
|
|
|
tr_block_index_t l;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torGetPieceBlockRange(cp->tor, p, &f, &l);
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
n = cp->blockBitfield->countRange(f, l + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
n *= cp->tor->blockSize;
|
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
if (l == cp->tor->blockCount - 1 && cp->blockBitfield->readBit(l))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:30:03 +00:00
|
|
|
n -= cp->tor->blockSize - cp->tor->lastBlockSize;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-29 01:17:18 +00:00
|
|
|
}
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(n <= tr_torPieceCountBytes(tor, p));
|
2017-04-19 12:04:45 +00:00
|
|
|
size += n;
|
2011-03-29 01:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-23 03:54:04 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(size <= inf->totalSize);
|
|
|
|
TR_ASSERT(size >= cp->sizeNow);
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
cp->sizeWhenDoneLazy = size;
|
|
|
|
cp->sizeWhenDoneIsDirty = false;
|
2008-11-05 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ccp->sizeWhenDoneLazy;
|
2008-11-05 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t tr_cpLeftUntilDone(tr_completion const* cp)
|
2011-09-26 06:18:48 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
uint64_t const sizeWhenDone = tr_cpSizeWhenDone(cp);
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(sizeWhenDone >= cp->sizeNow);
|
2011-09-26 06:18:48 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return sizeWhenDone - cp->sizeNow;
|
2011-09-26 06:18:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_cpGetAmountDone(tr_completion const* cp, float* tab, int tabCount)
|
2008-04-24 15:25:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const seed = tr_cpHasAll(cp);
|
|
|
|
float const interval = cp->tor->info.pieceCount / (float)tabCount;
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < tabCount; ++i)
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (seed)
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2019-03-17 04:07:48 +00:00
|
|
|
tab[i] = 1.0F;
|
2012-12-15 04:28:19 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_block_index_t f;
|
|
|
|
tr_block_index_t l;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_piece_index_t const piece = (tr_piece_index_t)i * interval;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torGetPieceBlockRange(cp->tor, piece, &f, &l);
|
2021-10-11 17:29:14 +00:00
|
|
|
tab[i] = cp->blockBitfield->countRange(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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t tr_cpMissingBlocksInPiece(tr_completion const* cp, tr_piece_index_t piece)
|
2009-01-02 17:01:55 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_cpHasAll(cp))
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2012-12-15 04:28:19 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_block_index_t f;
|
|
|
|
tr_block_index_t l;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torGetPieceBlockRange(cp->tor, piece, &f, &l);
|
2021-10-11 17:29:14 +00:00
|
|
|
return (l + 1 - f) - cp->blockBitfield->countRange(f, l + 1);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-02 17:01:55 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t tr_cpMissingBytesInPiece(tr_completion const* cp, tr_piece_index_t piece)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_cpHasAll(cp))
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2012-12-15 04:28:19 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t haveBytes = 0;
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_block_index_t f;
|
|
|
|
tr_block_index_t l;
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const pieceByteSize = tr_torPieceCountBytes(cp->tor, piece);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torGetPieceBlockRange(cp->tor, piece, &f, &l);
|
|
|
|
|
|
|
|
if (f != l)
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2021-10-11 17:29:14 +00:00
|
|
|
/* nb: we don't pass the usual l+1 here to Bitfield::countRange().
|
2017-04-19 12:04:45 +00:00
|
|
|
It's faster to handle the last block separately because its size
|
|
|
|
needs to be checked separately. */
|
2021-10-11 17:29:14 +00:00
|
|
|
haveBytes = cp->blockBitfield->countRange(f, l);
|
2017-04-19 12:04:45 +00:00
|
|
|
haveBytes *= cp->tor->blockSize;
|
2011-07-02 13:20:17 +00:00
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
if (cp->blockBitfield->readBit(l)) /* handle the last block */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
haveBytes += tr_torBlockCountBytes(cp->tor, l);
|
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(haveBytes <= pieceByteSize);
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_cpFileIsComplete(tr_completion const* cp, tr_file_index_t i)
|
2009-01-12 21:59:53 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (cp->tor->info.files[i].length == 0)
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2012-12-15 04:28:19 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
tr_block_index_t f;
|
|
|
|
tr_block_index_t l;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torGetFileBlockRange(cp->tor, i, &f, &l);
|
2021-10-11 17:29:14 +00:00
|
|
|
return cp->blockBitfield->countRange(f, l + 1) == (l + 1 - f);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void* tr_cpCreatePieceBitfield(tr_completion const* cp, size_t* byte_count)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(tr_torrentHasMetadata(cp->tor));
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void* ret;
|
|
|
|
tr_piece_index_t n;
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
n = cp->tor->info.pieceCount;
|
2021-10-11 17:29:14 +00:00
|
|
|
|
|
|
|
Bitfield pieces(n);
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_cpHasAll(cp))
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2021-10-11 17:29:14 +00:00
|
|
|
pieces.setHasAll();
|
2012-12-15 04:28:19 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (!tr_cpHasNone(cp))
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool* flags = tr_new(bool, n);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_piece_index_t i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
flags[i] = tr_cpPieceIsComplete(cp, i);
|
|
|
|
}
|
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
pieces.setFromFlags(flags, n);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(flags);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2009-01-12 21:59:53 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
ret = pieces.getRaw(byte_count);
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2011-01-31 16:43:37 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
double tr_cpPercentComplete(tr_completion const* cp)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
double const ratio = tr_getRatio(cp->sizeNow, cp->tor->info.totalSize);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
double tr_cpPercentDone(tr_completion const* cp)
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
double const ratio = tr_getRatio(cp->sizeNow, tr_cpSizeWhenDone(cp));
|
|
|
|
int const iratio = (int)ratio;
|
2017-04-30 16:25:26 +00:00
|
|
|
return (iratio == TR_RATIO_NA || iratio == TR_RATIO_INF) ? 0.0 : ratio;
|
2009-01-12 21:59:53 +00:00
|
|
|
}
|