source: trunk/libtransmission/completion.h @ 7692

Last change on this file since 7692 was 7692, checked in by charles, 13 years ago

(trunk libT) close open files immediately after they're done downloading

  • Property svn:keywords set to Date Rev Author Id
File size: 4.3 KB
Line 
1/*
2 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
3 *
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.
9 *
10 * $Id: completion.h 7692 2009-01-12 21:59:53Z charles $
11 */
12
13#ifndef TR_COMPLETION_H
14#define TR_COMPLETION_H
15
16#ifndef __TRANSMISSION__
17 #error only libtransmission should #include this header.
18#endif
19
20#include <assert.h>
21
22#include "transmission.h"
23#include "utils.h" /* tr_bitfield */
24
25typedef struct tr_completion
26{
27    tr_bool  sizeWhenDoneIsDirty;
28    tr_bool  haveValidIsDirty;
29
30    tr_torrent *    tor;
31
32    /* do we have this block? */
33    tr_bitfield    blockBitfield;
34
35    /* do we have this piece? */
36    tr_bitfield    pieceBitfield;
37
38    /* a block is complete if and only if we have it */
39    uint16_t *  completeBlocks;
40
41    /* number of bytes we'll have when done downloading. [0..info.totalSize]
42       DON'T access this directly; it's a lazy field.
43       use tr_cpSizeWhenDone() instead! */
44    uint64_t    sizeWhenDoneLazy;
45
46    /* number of bytes we'll have when done downloading. [0..info.totalSize]
47       DON'T access this directly; it's a lazy field.
48       use tr_cpHaveValid() instead! */
49    uint64_t    haveValidLazy;
50
51    /* number of bytes we want or have now. [0..sizeWhenDone] */
52    uint64_t    sizeNow;
53}
54tr_completion;
55
56/**
57*** Life Cycle
58**/
59
60tr_completion * tr_cpConstruct( tr_completion *, tr_torrent * );
61
62tr_completion * tr_cpDestruct( tr_completion * );
63
64static TR_INLINE tr_completion* tr_cpNew( tr_torrent * tor )
65{
66    return tr_cpConstruct( tr_new0( tr_completion, 1 ), tor );
67}
68
69static TR_INLINE void tr_cpFree( tr_completion * cp )
70{
71    tr_free( tr_cpDestruct( cp ) );
72}
73
74/**
75*** General
76**/
77
78tr_completeness            tr_cpGetStatus( const tr_completion * );
79
80uint64_t                   tr_cpHaveValid( const tr_completion * );
81
82uint64_t                   tr_cpSizeWhenDone( const tr_completion * );
83
84void                       tr_cpInvalidateDND( tr_completion * );
85
86void                       tr_cpGetAmountDone( const   tr_completion * completion,
87                                               float                 * tab,
88                                               int                     tabCount );
89
90static TR_INLINE uint64_t tr_cpHaveTotal( const tr_completion * cp )
91{
92    return cp->sizeNow;
93}
94
95static TR_INLINE uint64_t tr_cpLeftUntilComplete( const tr_completion * cp )
96{
97    return tr_torrentInfo(cp->tor)->totalSize - cp->sizeNow;
98}
99
100static TR_INLINE uint64_t tr_cpLeftUntilDone( const tr_completion * cp )
101{
102    return tr_cpSizeWhenDone( cp ) - cp->sizeNow;
103}
104
105static TR_INLINE float tr_cpPercentComplete( const tr_completion * cp )
106{
107    return tr_getRatio( cp->sizeNow, tr_torrentInfo(cp->tor)->totalSize );
108}
109
110static TR_INLINE float tr_cpPercentDone( const tr_completion * cp )
111{
112    return tr_getRatio( cp->sizeNow, tr_cpSizeWhenDone( cp ) );
113}
114
115/**
116*** Pieces
117**/
118
119int tr_cpMissingBlocksInPiece( const tr_completion  * cp,
120                               tr_piece_index_t       piece );
121
122tr_bool  tr_cpPieceIsComplete( const tr_completion * cp,
123                               tr_piece_index_t      piece );
124
125void   tr_cpPieceAdd( tr_completion    * completion,
126                      tr_piece_index_t   piece );
127
128void   tr_cpPieceRem( tr_completion     * completion,
129                      tr_piece_index_t   piece );
130
131tr_bool tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t );
132
133/**
134*** Blocks
135**/
136
137static TR_INLINE tr_bool tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t block ) {
138    return tr_bitfieldHas( &cp->blockBitfield, block );
139}
140
141void      tr_cpBlockAdd( tr_completion * completion,
142                         tr_block_index_t block );
143
144tr_bool   tr_cpBlockBitfieldSet( tr_completion      * completion,
145                                 struct tr_bitfield * blocks );
146
147/***
148****
149***/
150
151static TR_INLINE const struct tr_bitfield * tr_cpPieceBitfield( const tr_completion * cp ) {
152    return &cp->pieceBitfield;
153}
154
155static TR_INLINE const struct tr_bitfield * tr_cpBlockBitfield( const tr_completion * cp ) {
156    assert( cp );
157    assert( cp->blockBitfield.bits );
158    assert( cp->blockBitfield.bitCount );
159    return &cp->blockBitfield;
160}
161
162#endif
Note: See TracBrowser for help on using the repository browser.