1 /*
2  * This file Copyright (C) 2009-2014 Mnemosyne LLC
3  *
4  * It may be used under the GNU GPL versions 2 or 3
5  * or any future license endorsed by Mnemosyne LLC.
6  *
7  */
8 
9 #pragma once
10 
11 #ifndef __TRANSMISSION__
12 #error only libtransmission should #include this header.
13 #endif
14 
15 #include "transmission.h"
16 #include "bitfield.h"
17 #include "utils.h" /* tr_getRatio() */
18 
19 typedef struct tr_completion
20 {
21     tr_torrent* tor;
22 
23     tr_bitfield blockBitfield;
24 
25     /* number of bytes we'll have when done downloading. [0..info.totalSize]
26        DON'T access this directly; it's a lazy field.
27        use tr_cpSizeWhenDone() instead! */
28     uint64_t sizeWhenDoneLazy;
29 
30     /* whether or not sizeWhenDone needs to be recalculated */
31     bool sizeWhenDoneIsDirty;
32 
33     /* number of bytes we'll have when done downloading. [0..info.totalSize]
34        DON'T access this directly; it's a lazy field.
35        use tr_cpHaveValid() instead! */
36     uint64_t haveValidLazy;
37 
38     /* whether or not haveValidLazy needs to be recalculated */
39     bool haveValidIsDirty;
40 
41     /* number of bytes we want or have now. [0..sizeWhenDone] */
42     uint64_t sizeNow;
43 }
44 tr_completion;
45 
46 /**
47 *** Life Cycle
48 **/
49 
50 void tr_cpConstruct(tr_completion*, tr_torrent*);
51 
52 void tr_cpBlockInit(tr_completion* cp, tr_bitfield const* blocks);
53 
tr_cpDestruct(tr_completion * cp)54 static inline void tr_cpDestruct(tr_completion* cp)
55 {
56     tr_bitfieldDestruct(&cp->blockBitfield);
57 }
58 
59 /**
60 *** General
61 **/
62 
63 double tr_cpPercentComplete(tr_completion const* cp);
64 
65 double tr_cpPercentDone(tr_completion const* cp);
66 
67 tr_completeness tr_cpGetStatus(tr_completion const*);
68 
69 uint64_t tr_cpHaveValid(tr_completion const*);
70 
71 uint64_t tr_cpSizeWhenDone(tr_completion const*);
72 
73 uint64_t tr_cpLeftUntilDone(tr_completion const*);
74 
75 void tr_cpGetAmountDone(tr_completion const* completion, float* tab, int tabCount);
76 
tr_cpHaveTotal(tr_completion const * cp)77 static inline uint64_t tr_cpHaveTotal(tr_completion const* cp)
78 {
79     return cp->sizeNow;
80 }
81 
tr_cpHasAll(tr_completion const * cp)82 static inline bool tr_cpHasAll(tr_completion const* cp)
83 {
84     return tr_torrentHasMetadata(cp->tor) && tr_bitfieldHasAll(&cp->blockBitfield);
85 }
86 
tr_cpHasNone(tr_completion const * cp)87 static inline bool tr_cpHasNone(tr_completion const* cp)
88 {
89     return !tr_torrentHasMetadata(cp->tor) || tr_bitfieldHasNone(&cp->blockBitfield);
90 }
91 
92 /**
93 ***  Pieces
94 **/
95 
96 void tr_cpPieceAdd(tr_completion* cp, tr_piece_index_t i);
97 
98 void tr_cpPieceRem(tr_completion* cp, tr_piece_index_t i);
99 
100 size_t tr_cpMissingBlocksInPiece(tr_completion const*, tr_piece_index_t);
101 
102 size_t tr_cpMissingBytesInPiece(tr_completion const*, tr_piece_index_t);
103 
tr_cpPieceIsComplete(tr_completion const * cp,tr_piece_index_t i)104 static inline bool tr_cpPieceIsComplete(tr_completion const* cp, tr_piece_index_t i)
105 {
106     return tr_cpMissingBlocksInPiece(cp, i) == 0;
107 }
108 
109 /**
110 ***  Blocks
111 **/
112 
113 void tr_cpBlockAdd(tr_completion* cp, tr_block_index_t i);
114 
tr_cpBlockIsComplete(tr_completion const * cp,tr_block_index_t i)115 static inline bool tr_cpBlockIsComplete(tr_completion const* cp, tr_block_index_t i)
116 {
117     return tr_bitfieldHas(&cp->blockBitfield, i);
118 }
119 
120 /***
121 ****  Misc
122 ***/
123 
124 bool tr_cpFileIsComplete(tr_completion const* cp, tr_file_index_t);
125 
126 void* tr_cpCreatePieceBitfield(tr_completion const* cp, size_t* byte_count);
127 
tr_cpInvalidateDND(tr_completion * cp)128 static inline void tr_cpInvalidateDND(tr_completion* cp)
129 {
130     cp->sizeWhenDoneIsDirty = true;
131 }
132