source: trunk/libtransmission/torrent.h @ 9335

Last change on this file since 9335 was 9335, checked in by charles, 12 years ago

(trunk libT) make the ".part" suffix an optional feature as per BMW's request. Clean up the code a little.

  • Property svn:keywords set to Date Rev Author Id
File size: 11.8 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: torrent.h 9335 2009-10-20 03:14:44Z charles $
11 */
12
13#ifndef __TRANSMISSION__
14 #error only libtransmission should #include this header.
15#endif
16
17#ifndef TR_TORRENT_H
18#define TR_TORRENT_H 1
19
20#include "completion.h" /* tr_completion */
21#include "ratecontrol.h" /* tr_ratecontrol */
22#include "session.h" /* tr_globalLock(), tr_globalUnlock() */
23#include "utils.h" /* TR_GNUC_PRINTF */
24
25struct tr_bandwidth;
26struct tr_ratecontrol;
27struct tr_torrent_tiers;
28
29/**
30***  Package-visible ctor API
31**/
32
33void        tr_ctorSetSave( tr_ctor * ctor,
34                            tr_bool   saveMetadataInOurTorrentsDir );
35
36int         tr_ctorGetSave( const tr_ctor * ctor );
37
38void        tr_ctorInitTorrentPriorities( const tr_ctor * ctor, tr_torrent * tor );
39
40void        tr_ctorInitTorrentWanted( const tr_ctor * ctor, tr_torrent * tor );
41
42/**
43***
44**/
45
46/* just like tr_torrentSetFileDLs but doesn't trigger a fastresume save */
47void        tr_torrentInitFileDLs( tr_torrent *      tor,
48                                   tr_file_index_t * files,
49                                   tr_file_index_t   fileCount,
50                                   tr_bool           do_download );
51
52void        tr_torrentRecheckCompleteness( tr_torrent * );
53
54void        tr_torrentSetHasPiece( tr_torrent *     tor,
55                                   tr_piece_index_t pieceIndex,
56                                   tr_bool          has );
57
58void        tr_torrentChangeMyPort( tr_torrent * session );
59
60tr_torrent* tr_torrentFindFromHash( tr_session *    session,
61                                    const uint8_t * hash );
62
63tr_torrent* tr_torrentFindFromHashString( tr_session * session,
64                                          const char * hashString );
65
66tr_torrent* tr_torrentFindFromObfuscatedHash( tr_session    * session,
67                                              const uint8_t * hash );
68
69tr_bool     tr_torrentIsPieceTransferAllowed( const tr_torrent * torrent,
70                                              tr_direction       direction );
71
72
73
74#define tr_block( a, b ) _tr_block( tor, a, b )
75tr_block_index_t _tr_block( const tr_torrent * tor,
76                            tr_piece_index_t   index,
77                            uint32_t           offset );
78
79tr_bool          tr_torrentReqIsValid( const tr_torrent * tor,
80                                       tr_piece_index_t   index,
81                                       uint32_t           offset,
82                                       uint32_t           length );
83
84uint64_t         tr_pieceOffset( const tr_torrent * tor,
85                                 tr_piece_index_t   index,
86                                 uint32_t           offset,
87                                 uint32_t           length );
88
89void             tr_torrentInitFilePriority( tr_torrent       * tor,
90                                             tr_file_index_t    fileIndex,
91                                             tr_priority_t      priority );
92
93int              tr_torrentCountUncheckedPieces( const tr_torrent * );
94
95tr_bool          tr_torrentIsFileChecked( const tr_torrent  * tor,
96                                          tr_file_index_t     file );
97
98void             tr_torrentSetPieceChecked( tr_torrent       * tor,
99                                            tr_piece_index_t   piece,
100                                            tr_bool            isChecked );
101
102void             tr_torrentSetFileChecked( tr_torrent       * tor,
103                                           tr_file_index_t    file,
104                                           tr_bool            isChecked );
105
106void             tr_torrentUncheck( tr_torrent * tor );
107
108int              tr_torrentPromoteTracker( tr_torrent   * tor,
109                                           int            trackerIndex );
110
111time_t*          tr_torrentGetMTimes( const tr_torrent  * tor,
112                                      size_t            * setmeCount );
113
114tr_torrent*      tr_torrentNext( tr_session  * session,
115                                 tr_torrent  * current );
116
117void             tr_torrentCheckSeedRatio( tr_torrent * tor );
118
119/** save a torrent's .resume file if it's changed since the last time it was saved */
120void             tr_torrentSave( tr_torrent * tor );
121
122void             tr_torrentSetLocalError( tr_torrent * tor, const char * fmt, ... ) TR_GNUC_PRINTF( 2, 3 );
123
124
125
126typedef enum
127{
128    TR_VERIFY_NONE,
129    TR_VERIFY_WAIT,
130    TR_VERIFY_NOW
131}
132tr_verify_state;
133
134void             tr_torrentSetVerifyState( tr_torrent      * tor,
135                                           tr_verify_state   state );
136
137struct tr_torrent
138{
139    tr_session *             session;
140    tr_info                  info;
141
142    struct tr_ratecontrol    swarmSpeed;
143
144    int                      magicNumber;
145
146    tr_stat_errtype          error;
147    char                     errorString[128];
148
149    uint8_t                  obfuscatedHash[SHA_DIGEST_LENGTH];
150
151    /* If the initiator of the connection receives a handshake in which the
152     * peer_id does not match the expected peerid, then the initiator is
153     * expected to drop the connection. Note that the initiator presumably
154     * received the peer information from the tracker, which includes the
155     * peer_id that was registered by the peer. The peer_id from the tracker
156     * and in the handshake are expected to match.
157     */
158    uint8_t * peer_id;
159
160    /* Where the files will be when it's complete */
161    char * downloadDir;
162
163    /* Where the files are when the torrent is incomplete */
164    char * incompleteDir;
165
166    /* Where the files are now.
167     * This pointer will be equal to downloadDir or incompleteDir */
168    const char * currentDir;
169
170    /* How many bytes we ask for per request */
171    uint32_t                   blockSize;
172    tr_block_index_t           blockCount;
173
174    uint32_t                   lastBlockSize;
175    uint32_t                   lastPieceSize;
176
177    uint32_t                   blockCountInPiece;
178    uint32_t                   blockCountInLastPiece;
179
180    struct tr_completion       completion;
181
182    struct tr_bitfield         checkedPieces;
183    tr_completeness            completeness;
184
185    struct tr_torrent_tiers  * tiers;
186    struct tr_publisher_tag  * tiersSubscription;
187
188    time_t                     dhtAnnounceAt;
189    tr_bool                    dhtAnnounceInProgress;
190
191    uint64_t                   downloadedCur;
192    uint64_t                   downloadedPrev;
193    uint64_t                   uploadedCur;
194    uint64_t                   uploadedPrev;
195    uint64_t                   corruptCur;
196    uint64_t                   corruptPrev;
197
198    uint64_t                   etaSpeedCalculatedAt;
199    double                     etaSpeed;
200
201    time_t                     addedDate;
202    time_t                     activityDate;
203    time_t                     doneDate;
204    time_t                     startDate;
205    time_t                     anyDate;
206
207    tr_torrent_completeness_func *   completeness_func;
208    void *                     completeness_func_user_data;
209
210    tr_torrent_ratio_limit_hit_func * ratio_limit_hit_func;
211    void *                     ratio_limit_hit_func_user_data;
212
213    tr_bool                    isRunning;
214    tr_bool                    isDeleting;
215    tr_bool                    needsSeedRatioCheck;
216    tr_bool                    startAfterVerify;
217    tr_bool                    isDirty;
218
219    uint16_t                   maxConnectedPeers;
220
221    tr_verify_state            verifyState;
222
223    time_t                     lastStatTime;
224    tr_stat                    stats;
225
226    tr_torrent *               next;
227
228    int                        uniqueId;
229
230    struct tr_bandwidth      * bandwidth;
231
232    struct tr_torrent_peers  * torrentPeers;
233
234    double                     desiredRatio;
235    tr_ratiolimit              ratioLimitMode;
236
237    uint64_t                   preVerifyTotal;
238};
239
240/* get the index of this piece's first block */
241static TR_INLINE tr_block_index_t
242tr_torPieceFirstBlock( const tr_torrent * tor, const tr_piece_index_t piece )
243{
244    return piece * tor->blockCountInPiece;
245}
246
247/* what piece index is this block in? */
248static TR_INLINE tr_piece_index_t
249tr_torBlockPiece( const tr_torrent * tor, const tr_block_index_t block )
250{
251    return block / tor->blockCountInPiece;
252}
253
254/* how many blocks are in this piece? */
255static TR_INLINE uint32_t
256tr_torPieceCountBlocks( const tr_torrent * tor, const tr_piece_index_t piece )
257{
258    return piece == tor->info.pieceCount - 1 ? tor->blockCountInLastPiece
259                                             : tor->blockCountInPiece;
260}
261
262/* how many bytes are in this piece? */
263static TR_INLINE uint32_t
264tr_torPieceCountBytes( const tr_torrent * tor, const tr_piece_index_t piece )
265{
266    return piece == tor->info.pieceCount - 1 ? tor->lastPieceSize
267                                             : tor->info.pieceSize;
268}
269
270/* how many bytes are in this block? */
271static TR_INLINE uint32_t
272tr_torBlockCountBytes( const tr_torrent * tor, const tr_block_index_t block )
273{
274    return block == tor->blockCount - 1 ? tor->lastBlockSize
275                                        : tor->blockSize;
276}
277
278static TR_INLINE void tr_torrentLock( const tr_torrent * tor )
279{
280    tr_globalLock( tor->session );
281}
282
283static TR_INLINE void tr_torrentUnlock( const tr_torrent * tor )
284{
285    tr_globalUnlock( tor->session );
286}
287
288static TR_INLINE tr_bool
289tr_torrentExists( const tr_session * session, const uint8_t *   torrentHash )
290{
291    return tr_torrentFindFromHash( (tr_session*)session, torrentHash ) != NULL;
292}
293
294static TR_INLINE tr_bool
295tr_torrentIsSeed( const tr_torrent * tor )
296{
297    return tor->completeness != TR_LEECH;
298}
299
300static TR_INLINE tr_bool tr_torrentIsPrivate( const tr_torrent * tor )
301{
302    return ( tor != NULL ) && tor->info.isPrivate;
303}
304
305static TR_INLINE tr_bool tr_torrentAllowsPex( const tr_torrent * tor )
306{
307    return ( tor != NULL )
308        && ( tor->session->isPexEnabled )
309        && ( !tr_torrentIsPrivate( tor ) );
310}
311
312static TR_INLINE tr_bool tr_torrentAllowsDHT( const tr_torrent * tor )
313{
314    return ( tor != NULL )
315        && ( tr_sessionAllowsDHT( tor->session ) )
316        && ( !tr_torrentIsPrivate( tor ) );
317}
318
319static TR_INLINE tr_bool tr_torrentIsPieceChecked( const tr_torrent  * tor,
320                                                   tr_piece_index_t    i )
321{
322    return tr_bitfieldHasFast( &tor->checkedPieces, i );
323}
324
325/***
326****
327***/
328
329enum
330{
331    TORRENT_MAGIC_NUMBER = 95549
332};
333
334static TR_INLINE tr_bool tr_isTorrent( const tr_torrent * tor )
335{
336    return ( tor != NULL )
337        && ( tor->magicNumber == TORRENT_MAGIC_NUMBER )
338        && ( tr_isSession( tor->session ) );
339}
340
341/* set a flag indicating that the torrent's .resume file
342 * needs to be saved when the torrent is closed */
343static TR_INLINE void tr_torrentSetDirty( tr_torrent * tor )
344{
345    assert( tr_isTorrent( tor ) );
346
347    tor->isDirty = TRUE;
348}
349
350static TR_INLINE const char * tr_torrentName( const tr_torrent * tor )
351{
352    assert( tr_isTorrent( tor ) );
353
354    return tor->info.name;
355}
356
357/**
358 * Tell the tr_torrent that one of its files has become complete
359 */
360void tr_torrentFileCompleted( tr_torrent * tor, tr_file_index_t fileNo );
361
362
363/* Like tr_torrentFindFile(), but splits the filename into base
364 * (tr_torrent.incompleteDir or tr_torrent.downloadDir)  and subpath */
365tr_bool tr_torrentFindFile2( const tr_torrent *, tr_file_index_t fileNo,
366                             const char ** base, char ** subpath );
367
368
369/* Returns a newly-allocated string that's been munged to the form
370 * that denotes to humans that it's a partial file.
371 * This is like the filenames in tr_torrent.info.files --
372 * it's not a complete filename by itself, but a fragment that
373 * can be passed to tr_buildPath() */
374char* tr_torrentBuildPartial( const tr_torrent *, tr_file_index_t fileNo );
375
376
377
378
379#endif
Note: See TracBrowser for help on using the repository browser.