2 * This file Copyright (C) Mnemosyne LLC
4 * This file is licensed by the GPL version 2. Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
10 * $Id: completion.h 12035 2011-02-24 15:58:47Z jordan $
13 #ifndef __TRANSMISSION__
14 #error only libtransmission should #include this header.
17 #ifndef TR_COMPLETION_H
18 #define TR_COMPLETION_H
22 #include "transmission.h"
25 typedef struct tr_completion
27 tr_bool sizeWhenDoneIsDirty
;
28 tr_bool blocksWantedIsDirty
;
29 tr_bool haveValidIsDirty
;
33 /* do we have this block? */
34 tr_bitfield blockBitfield
;
36 /* do we have this piece? */
37 tr_bitfield pieceBitfield
;
39 /* a block is complete if and only if we have it */
40 uint16_t * completeBlocks
;
42 /* total number of blocks that we want downloaded
43 DON'T access this directly; it's a lazy field.
44 Used by tr_cpBlocksMissing(). */
45 tr_block_index_t blocksWantedLazy
;
47 /* total number of completed blocks that we want downloaded
48 DON'T access this directly; it's a lazy field.
49 Used by tr_cpBlocksMissing(). */
50 tr_block_index_t blocksWantedCompleteLazy
;
52 /* number of bytes we'll have when done downloading. [0..info.totalSize]
53 DON'T access this directly; it's a lazy field.
54 use tr_cpSizeWhenDone() instead! */
55 uint64_t sizeWhenDoneLazy
;
57 /* number of bytes we'll have when done downloading. [0..info.totalSize]
58 DON'T access this directly; it's a lazy field.
59 use tr_cpHaveValid() instead! */
60 uint64_t haveValidLazy
;
62 /* number of bytes we want or have now. [0..sizeWhenDone] */
71 tr_completion
* tr_cpConstruct( tr_completion
*, tr_torrent
* );
73 tr_completion
* tr_cpDestruct( tr_completion
* );
79 tr_completeness
tr_cpGetStatus( const tr_completion
* );
81 uint64_t tr_cpHaveValid( const tr_completion
* );
83 tr_block_index_t
tr_cpBlocksMissing( const tr_completion
* );
85 uint64_t tr_cpSizeWhenDone( const tr_completion
* );
87 void tr_cpInvalidateDND( tr_completion
* );
89 void tr_cpGetAmountDone( const tr_completion
* completion
,
93 static inline uint64_t tr_cpHaveTotal( const tr_completion
* cp
)
98 static inline uint64_t tr_cpLeftUntilComplete( const tr_completion
* cp
)
100 return tr_torrentInfo(cp
->tor
)->totalSize
- cp
->sizeNow
;
103 static inline uint64_t tr_cpLeftUntilDone( const tr_completion
* cp
)
105 return tr_cpSizeWhenDone( cp
) - cp
->sizeNow
;
108 static inline double tr_cpPercentComplete( const tr_completion
* cp
)
110 const double ratio
= tr_getRatio( cp
->sizeNow
, tr_torrentInfo(cp
->tor
)->totalSize
);
111 if( (int)ratio
== TR_RATIO_NA
)
113 else if( (int)ratio
== TR_RATIO_INF
)
119 static inline double tr_cpPercentDone( const tr_completion
* cp
)
121 const double ratio
= tr_getRatio( cp
->sizeNow
, tr_cpSizeWhenDone( cp
) );
122 const int iratio
= (int)ratio
;
123 return ((iratio
== TR_RATIO_NA
) || (iratio
== TR_RATIO_INF
)) ? 0.0 : ratio
;
130 int tr_cpMissingBlocksInPiece( const tr_completion
* cp
,
131 tr_piece_index_t piece
);
133 tr_bool
tr_cpPieceIsComplete( const tr_completion
* cp
,
134 tr_piece_index_t piece
);
136 void tr_cpPieceAdd( tr_completion
* completion
,
137 tr_piece_index_t piece
);
139 void tr_cpPieceRem( tr_completion
* completion
,
140 tr_piece_index_t piece
);
142 tr_bool
tr_cpFileIsComplete( const tr_completion
* cp
, tr_file_index_t
);
148 static inline tr_bool
tr_cpBlockIsCompleteFast( const tr_completion
* cp
, tr_block_index_t block
)
150 return tr_bitfieldHasFast( &cp
->blockBitfield
, block
);
153 static inline tr_bool
tr_cpBlockIsComplete( const tr_completion
* cp
, tr_block_index_t block
)
155 return tr_bitfieldHas( &cp
->blockBitfield
, block
);
158 void tr_cpBlockAdd( tr_completion
* completion
,
159 tr_block_index_t block
);
161 tr_bool
tr_cpBlockBitfieldSet( tr_completion
* completion
,
162 struct tr_bitfield
* blocks
);
164 void tr_cpSetHaveAll( tr_completion
* completion
);
170 static inline const struct tr_bitfield
* tr_cpPieceBitfield( const tr_completion
* cp
) {
171 return &cp
->pieceBitfield
;
174 static inline const struct tr_bitfield
* tr_cpBlockBitfield( const tr_completion
* cp
) {
176 assert( cp
->blockBitfield
.bits
);
177 assert( cp
->blockBitfield
.bitCount
);
178 return &cp
->blockBitfield
;