1 | /****************************************************************************** |
---|
2 | * $Id: torrent.c 5606 2008-04-13 14:29:11Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include <assert.h> |
---|
26 | #include <string.h> /* memcmp */ |
---|
27 | |
---|
28 | #include "transmission.h" |
---|
29 | #include "bencode.h" |
---|
30 | #include "completion.h" |
---|
31 | #include "crypto.h" /* for tr_sha1 */ |
---|
32 | #include "resume.h" |
---|
33 | #include "fdlimit.h" /* tr_fdFileClose */ |
---|
34 | #include "metainfo.h" |
---|
35 | #include "peer-mgr.h" |
---|
36 | #include "ratecontrol.h" |
---|
37 | #include "torrent.h" |
---|
38 | #include "tracker.h" |
---|
39 | #include "trcompat.h" /* for strlcpy */ |
---|
40 | #include "trevent.h" |
---|
41 | #include "utils.h" |
---|
42 | #include "verify.h" |
---|
43 | |
---|
44 | #define MAX_BLOCK_SIZE (1024*16) |
---|
45 | |
---|
46 | /*** |
---|
47 | **** |
---|
48 | ***/ |
---|
49 | |
---|
50 | int |
---|
51 | tr_torrentExists( tr_handle * handle, |
---|
52 | const uint8_t * torrentHash ) |
---|
53 | { |
---|
54 | return tr_torrentFindFromHash( handle, torrentHash ) != NULL; |
---|
55 | } |
---|
56 | |
---|
57 | tr_torrent* |
---|
58 | tr_torrentFindFromHash( tr_handle * handle, |
---|
59 | const uint8_t * torrentHash ) |
---|
60 | { |
---|
61 | tr_torrent * tor; |
---|
62 | |
---|
63 | for( tor = handle->torrentList; tor; tor = tor->next ) |
---|
64 | if( !memcmp( tor->info.hash, torrentHash, SHA_DIGEST_LENGTH ) ) |
---|
65 | return tor; |
---|
66 | |
---|
67 | return NULL; |
---|
68 | } |
---|
69 | |
---|
70 | tr_torrent* |
---|
71 | tr_torrentFindFromObfuscatedHash( tr_handle * handle, |
---|
72 | const uint8_t * obfuscatedTorrentHash ) |
---|
73 | { |
---|
74 | tr_torrent * tor; |
---|
75 | |
---|
76 | for( tor = handle->torrentList; tor; tor = tor->next ) |
---|
77 | if( !memcmp( tor->obfuscatedHash, obfuscatedTorrentHash, SHA_DIGEST_LENGTH ) ) |
---|
78 | return tor; |
---|
79 | |
---|
80 | return NULL; |
---|
81 | } |
---|
82 | |
---|
83 | /*** |
---|
84 | **** LOCKS |
---|
85 | ***/ |
---|
86 | |
---|
87 | void |
---|
88 | tr_torrentLock( const tr_torrent * tor ) |
---|
89 | { |
---|
90 | tr_globalLock( tor->handle ); |
---|
91 | } |
---|
92 | |
---|
93 | void |
---|
94 | tr_torrentUnlock( const tr_torrent * tor ) |
---|
95 | { |
---|
96 | tr_globalUnlock( tor->handle ); |
---|
97 | } |
---|
98 | |
---|
99 | /*** |
---|
100 | **** PER-TORRENT UL / DL SPEEDS |
---|
101 | ***/ |
---|
102 | |
---|
103 | void |
---|
104 | tr_torrentSetSpeedMode( tr_torrent * tor, |
---|
105 | int up_or_down, |
---|
106 | tr_speedlimit mode ) |
---|
107 | { |
---|
108 | tr_speedlimit * limit = up_or_down==TR_UP |
---|
109 | ? &tor->uploadLimitMode |
---|
110 | : &tor->downloadLimitMode; |
---|
111 | *limit = mode; |
---|
112 | } |
---|
113 | |
---|
114 | tr_speedlimit |
---|
115 | tr_torrentGetSpeedMode( const tr_torrent * tor, |
---|
116 | int up_or_down) |
---|
117 | { |
---|
118 | return up_or_down==TR_UP ? tor->uploadLimitMode |
---|
119 | : tor->downloadLimitMode; |
---|
120 | } |
---|
121 | |
---|
122 | void |
---|
123 | tr_torrentSetSpeedLimit( tr_torrent * tor, |
---|
124 | int up_or_down, |
---|
125 | int single_KiB_sec ) |
---|
126 | { |
---|
127 | tr_ratecontrol * rc = up_or_down==TR_UP ? tor->upload : tor->download; |
---|
128 | tr_rcSetLimit( rc, single_KiB_sec ); |
---|
129 | } |
---|
130 | |
---|
131 | int |
---|
132 | tr_torrentGetSpeedLimit( const tr_torrent * tor, |
---|
133 | int up_or_down ) |
---|
134 | { |
---|
135 | tr_ratecontrol * rc = up_or_down==TR_UP ? tor->upload : tor->download; |
---|
136 | return tr_rcGetLimit( rc ); |
---|
137 | } |
---|
138 | |
---|
139 | /*** |
---|
140 | **** |
---|
141 | ***/ |
---|
142 | |
---|
143 | static void |
---|
144 | onTrackerResponse( void * tracker UNUSED, void * vevent, void * user_data ) |
---|
145 | { |
---|
146 | tr_torrent * tor = user_data; |
---|
147 | tr_tracker_event * event = vevent; |
---|
148 | |
---|
149 | switch( event->messageType ) |
---|
150 | { |
---|
151 | case TR_TRACKER_PEERS: |
---|
152 | tr_peerMgrAddPeers( tor->handle->peerMgr, |
---|
153 | tor->info.hash, |
---|
154 | TR_PEER_FROM_TRACKER, |
---|
155 | event->peerCompact, |
---|
156 | event->peerCount ); |
---|
157 | break; |
---|
158 | |
---|
159 | case TR_TRACKER_WARNING: |
---|
160 | tr_torerr( tor, _( "Tracker warning: \"%s\"" ), event->text ); |
---|
161 | tor->error = TR_ERROR_TC_WARNING; |
---|
162 | strlcpy( tor->errorString, event->text, sizeof(tor->errorString) ); |
---|
163 | break; |
---|
164 | |
---|
165 | case TR_TRACKER_ERROR: |
---|
166 | tr_torerr( tor, _( "Tracker error: \"%s\"" ), event->text ); |
---|
167 | tor->error = TR_ERROR_TC_ERROR; |
---|
168 | strlcpy( tor->errorString, event->text, sizeof(tor->errorString) ); |
---|
169 | break; |
---|
170 | |
---|
171 | case TR_TRACKER_ERROR_CLEAR: |
---|
172 | tor->error = 0; |
---|
173 | tor->errorString[0] = '\0'; |
---|
174 | break; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | /*** |
---|
179 | **** |
---|
180 | **** TORRENT INSTANTIATION |
---|
181 | **** |
---|
182 | ***/ |
---|
183 | |
---|
184 | static int |
---|
185 | getBytePiece( const tr_info * info, uint64_t byteOffset ) |
---|
186 | { |
---|
187 | assert( info != NULL ); |
---|
188 | assert( info->pieceSize != 0 ); |
---|
189 | |
---|
190 | return byteOffset / info->pieceSize; |
---|
191 | } |
---|
192 | |
---|
193 | static void |
---|
194 | initFilePieces ( tr_info * info, tr_file_index_t fileIndex ) |
---|
195 | { |
---|
196 | tr_file * file = &info->files[fileIndex]; |
---|
197 | uint64_t firstByte, lastByte; |
---|
198 | |
---|
199 | assert( info != NULL ); |
---|
200 | assert( fileIndex < info->fileCount ); |
---|
201 | |
---|
202 | file = &info->files[fileIndex]; |
---|
203 | firstByte = file->offset; |
---|
204 | lastByte = firstByte + (file->length ? file->length-1 : 0); |
---|
205 | file->firstPiece = getBytePiece( info, firstByte ); |
---|
206 | file->lastPiece = getBytePiece( info, lastByte ); |
---|
207 | } |
---|
208 | |
---|
209 | static int |
---|
210 | pieceHasFile( tr_piece_index_t piece, const tr_file * file ) |
---|
211 | { |
---|
212 | return ( file->firstPiece <= piece ) && ( piece <= file->lastPiece ); |
---|
213 | } |
---|
214 | |
---|
215 | static tr_priority_t |
---|
216 | calculatePiecePriority ( const tr_torrent * tor, |
---|
217 | tr_piece_index_t piece, |
---|
218 | int fileHint ) |
---|
219 | { |
---|
220 | tr_file_index_t i; |
---|
221 | int priority = TR_PRI_LOW; |
---|
222 | |
---|
223 | /* find the first file that has data in this piece */ |
---|
224 | if( fileHint >= 0 ) { |
---|
225 | i = fileHint; |
---|
226 | while( i>0 && pieceHasFile( piece, &tor->info.files[i-1] ) ) |
---|
227 | --i; |
---|
228 | } else { |
---|
229 | for( i=0; i<tor->info.fileCount; ++i ) |
---|
230 | if( pieceHasFile( piece, &tor->info.files[i] ) ) |
---|
231 | break; |
---|
232 | } |
---|
233 | |
---|
234 | /* the piece's priority is the max of the priorities |
---|
235 | * of all the files in that piece */ |
---|
236 | for( ; i<tor->info.fileCount; ++i ) |
---|
237 | { |
---|
238 | const tr_file * file = &tor->info.files[i]; |
---|
239 | |
---|
240 | if( !pieceHasFile( piece, file ) ) |
---|
241 | break; |
---|
242 | |
---|
243 | priority = MAX( priority, file->priority ); |
---|
244 | |
---|
245 | /* when dealing with multimedia files, getting the first and |
---|
246 | last pieces can sometimes allow you to preview it a bit |
---|
247 | before it's fully downloaded... */ |
---|
248 | if ( file->priority >= TR_PRI_NORMAL ) |
---|
249 | if ( file->firstPiece == piece || file->lastPiece == piece ) |
---|
250 | priority = TR_PRI_HIGH; |
---|
251 | } |
---|
252 | |
---|
253 | return priority; |
---|
254 | } |
---|
255 | |
---|
256 | static void |
---|
257 | tr_torrentInitFilePieces( tr_torrent * tor ) |
---|
258 | { |
---|
259 | tr_file_index_t ff; |
---|
260 | tr_piece_index_t pp; |
---|
261 | uint64_t offset = 0; |
---|
262 | |
---|
263 | assert( tor != NULL ); |
---|
264 | |
---|
265 | for( ff=0; ff<tor->info.fileCount; ++ff ) { |
---|
266 | tor->info.files[ff].offset = offset; |
---|
267 | offset += tor->info.files[ff].length; |
---|
268 | initFilePieces( &tor->info, ff ); |
---|
269 | } |
---|
270 | |
---|
271 | for( pp=0; pp<tor->info.pieceCount; ++pp ) |
---|
272 | tor->info.pieces[pp].priority = calculatePiecePriority( tor, pp, -1 ); |
---|
273 | } |
---|
274 | |
---|
275 | static void |
---|
276 | torrentRealInit( tr_handle * h, |
---|
277 | tr_torrent * tor, |
---|
278 | const tr_ctor * ctor ) |
---|
279 | { |
---|
280 | int doStart; |
---|
281 | uint64_t loaded; |
---|
282 | uint64_t t; |
---|
283 | tr_info * info = &tor->info; |
---|
284 | |
---|
285 | tr_globalLock( h ); |
---|
286 | |
---|
287 | tor->handle = h; |
---|
288 | |
---|
289 | /** |
---|
290 | * Decide on a block size. constraints: |
---|
291 | * (1) most clients decline requests over 16 KiB |
---|
292 | * (2) pieceSize must be a multiple of block size |
---|
293 | */ |
---|
294 | tor->blockSize = info->pieceSize; |
---|
295 | while( tor->blockSize > MAX_BLOCK_SIZE ) |
---|
296 | tor->blockSize /= 2; |
---|
297 | |
---|
298 | tor->lastPieceSize = info->totalSize % info->pieceSize; |
---|
299 | |
---|
300 | if( !tor->lastPieceSize ) |
---|
301 | tor->lastPieceSize = info->pieceSize; |
---|
302 | |
---|
303 | tor->lastBlockSize = info->totalSize % tor->blockSize; |
---|
304 | |
---|
305 | if( !tor->lastBlockSize ) |
---|
306 | tor->lastBlockSize = tor->blockSize; |
---|
307 | |
---|
308 | tor->blockCount = |
---|
309 | ( info->totalSize + tor->blockSize - 1 ) / tor->blockSize; |
---|
310 | |
---|
311 | tor->blockCountInPiece = |
---|
312 | info->pieceSize / tor->blockSize; |
---|
313 | |
---|
314 | tor->blockCountInLastPiece = |
---|
315 | ( tor->lastPieceSize + tor->blockSize - 1 ) / tor->blockSize; |
---|
316 | |
---|
317 | /* check our work */ |
---|
318 | assert( ( info->pieceSize % tor->blockSize ) == 0 ); |
---|
319 | t = info->pieceCount - 1; |
---|
320 | t *= info->pieceSize; |
---|
321 | t += tor->lastPieceSize; |
---|
322 | assert( t == info->totalSize ); |
---|
323 | t = tor->blockCount - 1; |
---|
324 | t *= tor->blockSize; |
---|
325 | t += tor->lastBlockSize; |
---|
326 | assert( t == info->totalSize ); |
---|
327 | t = info->pieceCount - 1; |
---|
328 | t *= tor->blockCountInPiece; |
---|
329 | t += tor->blockCountInLastPiece; |
---|
330 | assert( t == (uint64_t)tor->blockCount ); |
---|
331 | |
---|
332 | tor->completion = tr_cpInit( tor ); |
---|
333 | |
---|
334 | tr_torrentInitFilePieces( tor ); |
---|
335 | |
---|
336 | tor->upload = tr_rcInit(); |
---|
337 | tor->download = tr_rcInit(); |
---|
338 | tor->swarmspeed = tr_rcInit(); |
---|
339 | |
---|
340 | tr_sha1( tor->obfuscatedHash, "req2", 4, |
---|
341 | info->hash, SHA_DIGEST_LENGTH, |
---|
342 | NULL ); |
---|
343 | |
---|
344 | tr_peerMgrAddTorrent( h->peerMgr, tor ); |
---|
345 | |
---|
346 | if( !h->isPortSet ) |
---|
347 | tr_setBindPort( h, TR_DEFAULT_PORT ); |
---|
348 | |
---|
349 | assert( !tor->downloadedCur ); |
---|
350 | assert( !tor->uploadedCur ); |
---|
351 | |
---|
352 | tor->error = TR_OK; |
---|
353 | |
---|
354 | tor->checkedPieces = tr_bitfieldNew( tor->info.pieceCount ); |
---|
355 | tr_torrentUncheck( tor ); |
---|
356 | loaded = tr_torrentLoadResume( tor, ~0, ctor ); |
---|
357 | |
---|
358 | doStart = tor->isRunning; |
---|
359 | tor->isRunning = 0; |
---|
360 | |
---|
361 | if( !(loaded & TR_FR_SPEEDLIMIT ) ) { |
---|
362 | int limit, enabled; |
---|
363 | tr_getGlobalSpeedLimit( tor->handle, TR_UP, &enabled, &limit ); |
---|
364 | tr_torrentSetSpeedLimit( tor, TR_UP, limit ); |
---|
365 | tr_getGlobalSpeedLimit( tor->handle, TR_DOWN, &enabled, &limit ); |
---|
366 | tr_torrentSetSpeedLimit( tor, TR_DOWN, limit ); |
---|
367 | } |
---|
368 | |
---|
369 | tor->cpStatus = tr_cpGetStatus( tor->completion ); |
---|
370 | |
---|
371 | tor->tracker = tr_trackerNew( tor ); |
---|
372 | tor->trackerSubscription = tr_trackerSubscribe( tor->tracker, onTrackerResponse, tor ); |
---|
373 | |
---|
374 | tor->next = h->torrentList; |
---|
375 | h->torrentList = tor; |
---|
376 | h->torrentCount++; |
---|
377 | |
---|
378 | tr_globalUnlock( h ); |
---|
379 | |
---|
380 | /* maybe save our own copy of the metainfo */ |
---|
381 | if( tr_ctorGetSave( ctor ) ) { |
---|
382 | const tr_benc * val; |
---|
383 | if( !tr_ctorGetMetainfo( ctor, &val ) ) { |
---|
384 | int len; |
---|
385 | uint8_t * text = (uint8_t*) tr_bencSave( val, &len ); |
---|
386 | tr_metainfoSave( tor->handle, |
---|
387 | tor->info.hashString, |
---|
388 | text, len ); |
---|
389 | tr_free( text ); |
---|
390 | } |
---|
391 | } |
---|
392 | |
---|
393 | if( doStart ) |
---|
394 | tr_torrentStart( tor ); |
---|
395 | } |
---|
396 | |
---|
397 | static int |
---|
398 | hashExists( const tr_handle * h, |
---|
399 | const uint8_t * hash ) |
---|
400 | { |
---|
401 | const tr_torrent * tor; |
---|
402 | |
---|
403 | for( tor=h->torrentList; tor; tor=tor->next ) |
---|
404 | if( !memcmp( hash, tor->info.hash, SHA_DIGEST_LENGTH ) ) |
---|
405 | return TRUE; |
---|
406 | |
---|
407 | return FALSE; |
---|
408 | } |
---|
409 | |
---|
410 | int |
---|
411 | tr_torrentParse( const tr_handle * handle, |
---|
412 | const tr_ctor * ctor, |
---|
413 | tr_info * setmeInfo ) |
---|
414 | { |
---|
415 | int err = 0; |
---|
416 | int doFree; |
---|
417 | tr_info tmp; |
---|
418 | const tr_benc * metainfo; |
---|
419 | |
---|
420 | if( setmeInfo == NULL ) |
---|
421 | setmeInfo = &tmp; |
---|
422 | memset( setmeInfo, 0, sizeof( tr_info ) ); |
---|
423 | |
---|
424 | if( !err && tr_ctorGetMetainfo( ctor, &metainfo ) ) |
---|
425 | return TR_EINVALID; |
---|
426 | |
---|
427 | err = tr_metainfoParse( handle, setmeInfo, metainfo ); |
---|
428 | doFree = !err && ( setmeInfo == &tmp ); |
---|
429 | |
---|
430 | if( !err && hashExists( handle, setmeInfo->hash ) ) |
---|
431 | err = TR_EDUPLICATE; |
---|
432 | |
---|
433 | if( doFree ) |
---|
434 | tr_metainfoFree( setmeInfo ); |
---|
435 | |
---|
436 | return err; |
---|
437 | } |
---|
438 | |
---|
439 | tr_torrent * |
---|
440 | tr_torrentNew( tr_handle * handle, |
---|
441 | const tr_ctor * ctor, |
---|
442 | int * setmeError ) |
---|
443 | { |
---|
444 | int err; |
---|
445 | tr_info tmpInfo; |
---|
446 | tr_torrent * tor = NULL; |
---|
447 | |
---|
448 | err = tr_torrentParse( handle, ctor, &tmpInfo ); |
---|
449 | if( !err ) { |
---|
450 | tor = tr_new0( tr_torrent, 1 ); |
---|
451 | tor->info = tmpInfo; |
---|
452 | torrentRealInit( handle, tor, ctor ); |
---|
453 | } else if( setmeError ) { |
---|
454 | *setmeError = err; |
---|
455 | } |
---|
456 | |
---|
457 | return tor; |
---|
458 | } |
---|
459 | |
---|
460 | /** |
---|
461 | *** |
---|
462 | **/ |
---|
463 | |
---|
464 | void |
---|
465 | tr_torrentSetFolder( tr_torrent * tor, const char * path ) |
---|
466 | { |
---|
467 | tr_free( tor->destination ); |
---|
468 | tor->destination = tr_strdup( path ); |
---|
469 | tr_torrentSaveResume( tor ); |
---|
470 | } |
---|
471 | |
---|
472 | const char* |
---|
473 | tr_torrentGetFolder( const tr_torrent * tor ) |
---|
474 | { |
---|
475 | return tor->destination; |
---|
476 | } |
---|
477 | |
---|
478 | void |
---|
479 | tr_torrentChangeMyPort( tr_torrent * tor ) |
---|
480 | { |
---|
481 | if( tor->tracker ) |
---|
482 | tr_trackerChangeMyPort( tor->tracker ); |
---|
483 | } |
---|
484 | |
---|
485 | int |
---|
486 | tr_torrentIsPrivate( const tr_torrent * tor ) |
---|
487 | { |
---|
488 | return tor |
---|
489 | && tor->info.isPrivate; |
---|
490 | } |
---|
491 | |
---|
492 | int |
---|
493 | tr_torrentAllowsPex( const tr_torrent * tor ) |
---|
494 | { |
---|
495 | return tor |
---|
496 | && tor->handle->isPexEnabled |
---|
497 | && !tr_torrentIsPrivate( tor ); |
---|
498 | } |
---|
499 | |
---|
500 | static void |
---|
501 | tr_manualUpdateImpl( void * vtor ) |
---|
502 | { |
---|
503 | tr_torrent * tor = vtor; |
---|
504 | if( tor->isRunning ) |
---|
505 | tr_trackerReannounce( tor->tracker ); |
---|
506 | } |
---|
507 | void |
---|
508 | tr_manualUpdate( tr_torrent * tor ) |
---|
509 | { |
---|
510 | tr_runInEventThread( tor->handle, tr_manualUpdateImpl, tor ); |
---|
511 | } |
---|
512 | int |
---|
513 | tr_torrentCanManualUpdate( const tr_torrent * tor ) |
---|
514 | { |
---|
515 | return ( tor != NULL ) |
---|
516 | && ( tor->isRunning ) |
---|
517 | && ( tr_trackerCanManualAnnounce( tor->tracker ) ); |
---|
518 | } |
---|
519 | |
---|
520 | /* rcRate's averaging code can make it appear that we're |
---|
521 | * still sending bytes after a torrent stops or all the |
---|
522 | * peers disconnect, so short-circuit that appearance here */ |
---|
523 | void |
---|
524 | tr_torrentGetRates( const tr_torrent * tor, |
---|
525 | float * toClient, |
---|
526 | float * toPeer) |
---|
527 | { |
---|
528 | const int showSpeed = tor->isRunning |
---|
529 | && tr_peerMgrHasConnections( tor->handle->peerMgr, tor->info.hash ); |
---|
530 | |
---|
531 | if( toClient ) |
---|
532 | *toClient = showSpeed ? tr_rcRate( tor->download ) : 0.0; |
---|
533 | if( toPeer ) |
---|
534 | *toPeer = showSpeed ? tr_rcRate( tor->upload ) : 0.0; |
---|
535 | } |
---|
536 | const tr_info * |
---|
537 | tr_torrentInfo( const tr_torrent * tor ) |
---|
538 | { |
---|
539 | return tor ? &tor->info : NULL; |
---|
540 | } |
---|
541 | |
---|
542 | const tr_stat * |
---|
543 | tr_torrentStatCached( tr_torrent * tor ) |
---|
544 | { |
---|
545 | const time_t now = time( NULL ); |
---|
546 | |
---|
547 | return tor && ( now == tor->lastStatTime ) ? &tor->stats |
---|
548 | : tr_torrentStat( tor ); |
---|
549 | } |
---|
550 | |
---|
551 | tr_torrent_status |
---|
552 | tr_torrentGetStatus( tr_torrent * tor ) |
---|
553 | { |
---|
554 | tr_torrentRecheckCompleteness( tor ); |
---|
555 | |
---|
556 | if( tor->verifyState == TR_VERIFY_NOW ) |
---|
557 | return TR_STATUS_CHECK; |
---|
558 | if( tor->verifyState == TR_VERIFY_WAIT ) |
---|
559 | return TR_STATUS_CHECK_WAIT; |
---|
560 | if( !tor->isRunning ) |
---|
561 | return TR_STATUS_STOPPED; |
---|
562 | if( tor->cpStatus == TR_CP_INCOMPLETE ) |
---|
563 | return TR_STATUS_DOWNLOAD; |
---|
564 | |
---|
565 | return TR_STATUS_SEED; |
---|
566 | } |
---|
567 | |
---|
568 | const tr_stat * |
---|
569 | tr_torrentStat( tr_torrent * tor ) |
---|
570 | { |
---|
571 | tr_stat * s; |
---|
572 | struct tr_tracker * tc; |
---|
573 | |
---|
574 | if( !tor ) |
---|
575 | return NULL; |
---|
576 | |
---|
577 | tr_torrentLock( tor ); |
---|
578 | |
---|
579 | tor->lastStatTime = time( NULL ); |
---|
580 | |
---|
581 | s = &tor->stats; |
---|
582 | s->status = tr_torrentGetStatus( tor ); |
---|
583 | s->error = tor->error; |
---|
584 | memcpy( s->errorString, tor->errorString, |
---|
585 | sizeof( s->errorString ) ); |
---|
586 | |
---|
587 | tc = tor->tracker; |
---|
588 | s->tracker = tr_trackerGetAddress( tor->tracker ); |
---|
589 | |
---|
590 | tr_trackerStat( tor->tracker, &s->tracker_stat ); |
---|
591 | |
---|
592 | tr_peerMgrTorrentStats( tor->handle->peerMgr, |
---|
593 | tor->info.hash, |
---|
594 | &s->peersKnown, |
---|
595 | &s->peersConnected, |
---|
596 | &s->peersSendingToUs, |
---|
597 | &s->peersGettingFromUs, |
---|
598 | s->peersFrom ); |
---|
599 | |
---|
600 | s->manualAnnounceTime = tr_trackerGetManualAnnounceTime( tor->tracker ); |
---|
601 | |
---|
602 | s->percentComplete = tr_cpPercentComplete ( tor->completion ); |
---|
603 | |
---|
604 | s->percentDone = tr_cpPercentDone( tor->completion ); |
---|
605 | s->leftUntilDone = tr_cpLeftUntilDone( tor->completion ); |
---|
606 | s->sizeWhenDone = tr_cpSizeWhenDone( tor->completion ); |
---|
607 | |
---|
608 | s->recheckProgress = |
---|
609 | 1.0 - (tr_torrentCountUncheckedPieces( tor ) / (double) tor->info.pieceCount); |
---|
610 | |
---|
611 | tr_torrentGetRates( tor, &s->rateDownload, &s->rateUpload ); |
---|
612 | |
---|
613 | tr_trackerGetCounts( tc, |
---|
614 | &s->completedFromTracker, |
---|
615 | &s->leechers, |
---|
616 | &s->seeders ); |
---|
617 | |
---|
618 | s->swarmspeed = tr_rcRate( tor->swarmspeed ); |
---|
619 | |
---|
620 | s->startDate = tor->startDate; |
---|
621 | s->activityDate = tor->activityDate; |
---|
622 | |
---|
623 | s->corruptEver = tor->corruptCur + tor->corruptPrev; |
---|
624 | s->downloadedEver = tor->downloadedCur + tor->downloadedPrev; |
---|
625 | s->uploadedEver = tor->uploadedCur + tor->uploadedPrev; |
---|
626 | s->haveValid = tr_cpHaveValid( tor->completion ); |
---|
627 | s->haveUnchecked = tr_cpHaveTotal( tor->completion ) - s->haveValid; |
---|
628 | |
---|
629 | |
---|
630 | { |
---|
631 | tr_piece_index_t i; |
---|
632 | tr_bitfield * peerPieces = tr_peerMgrGetAvailable( tor->handle->peerMgr, |
---|
633 | tor->info.hash ); |
---|
634 | s->desiredAvailable = 0; |
---|
635 | for( i=0; i<tor->info.pieceCount; ++i ) |
---|
636 | if( !tor->info.pieces[i].dnd && tr_bitfieldHas( peerPieces, i ) ) |
---|
637 | s->desiredAvailable += tr_cpMissingBlocksInPiece( tor->completion, i ); |
---|
638 | s->desiredAvailable *= tor->blockSize; |
---|
639 | tr_bitfieldFree( peerPieces ); |
---|
640 | } |
---|
641 | |
---|
642 | if( s->leftUntilDone > s->desiredAvailable ) |
---|
643 | s->eta = TR_ETA_NOT_AVAIL; |
---|
644 | else if( s->rateDownload < 0.1 ) |
---|
645 | s->eta = TR_ETA_UNKNOWN; |
---|
646 | else |
---|
647 | s->eta = s->leftUntilDone / s->rateDownload / 1024.0; |
---|
648 | |
---|
649 | s->ratio = tr_getRatio( s->uploadedEver, |
---|
650 | s->downloadedEver ? s->downloadedEver : s->haveValid ); |
---|
651 | |
---|
652 | tr_torrentUnlock( tor ); |
---|
653 | |
---|
654 | return s; |
---|
655 | } |
---|
656 | |
---|
657 | /*** |
---|
658 | **** |
---|
659 | ***/ |
---|
660 | |
---|
661 | static uint64_t |
---|
662 | fileBytesCompleted ( const tr_torrent * tor, tr_file_index_t fileIndex ) |
---|
663 | { |
---|
664 | const tr_file * file = &tor->info.files[fileIndex]; |
---|
665 | const tr_block_index_t firstBlock = file->offset / tor->blockSize; |
---|
666 | const uint64_t firstBlockOffset = file->offset % tor->blockSize; |
---|
667 | const uint64_t lastOffset = file->length ? (file->length-1) : 0; |
---|
668 | const tr_block_index_t lastBlock = (file->offset + lastOffset) / tor->blockSize; |
---|
669 | const uint64_t lastBlockOffset = (file->offset + lastOffset) % tor->blockSize; |
---|
670 | uint64_t haveBytes = 0; |
---|
671 | |
---|
672 | assert( tor != NULL ); |
---|
673 | assert( fileIndex < tor->info.fileCount ); |
---|
674 | assert( file->offset + file->length <= tor->info.totalSize ); |
---|
675 | assert( ( firstBlock < tor->blockCount ) || (!file->length && file->offset==tor->info.totalSize) ); |
---|
676 | assert( ( lastBlock < tor->blockCount ) || (!file->length && file->offset==tor->info.totalSize) ); |
---|
677 | assert( firstBlock <= lastBlock ); |
---|
678 | assert( tr_torBlockPiece( tor, firstBlock ) == file->firstPiece ); |
---|
679 | assert( tr_torBlockPiece( tor, lastBlock ) == file->lastPiece ); |
---|
680 | |
---|
681 | if( firstBlock == lastBlock ) |
---|
682 | { |
---|
683 | if( tr_cpBlockIsComplete( tor->completion, firstBlock ) ) |
---|
684 | haveBytes += lastBlockOffset + 1 - firstBlockOffset; |
---|
685 | } |
---|
686 | else |
---|
687 | { |
---|
688 | tr_block_index_t i; |
---|
689 | |
---|
690 | if( tr_cpBlockIsComplete( tor->completion, firstBlock ) ) |
---|
691 | haveBytes += tor->blockSize - firstBlockOffset; |
---|
692 | |
---|
693 | for( i=firstBlock+1; i<lastBlock; ++i ) |
---|
694 | if( tr_cpBlockIsComplete( tor->completion, i ) ) |
---|
695 | haveBytes += tor->blockSize; |
---|
696 | |
---|
697 | if( tr_cpBlockIsComplete( tor->completion, lastBlock ) ) |
---|
698 | haveBytes += lastBlockOffset + 1; |
---|
699 | } |
---|
700 | |
---|
701 | return haveBytes; |
---|
702 | } |
---|
703 | |
---|
704 | tr_file_stat * |
---|
705 | tr_torrentFiles( const tr_torrent * tor, tr_file_index_t * fileCount ) |
---|
706 | { |
---|
707 | tr_file_index_t i; |
---|
708 | const tr_file_index_t n = tor->info.fileCount; |
---|
709 | tr_file_stat * files = tr_new0( tr_file_stat, n ); |
---|
710 | tr_file_stat * walk = files; |
---|
711 | |
---|
712 | for( i=0; i<n; ++i, ++walk ) |
---|
713 | { |
---|
714 | const tr_file * file = tor->info.files + i; |
---|
715 | |
---|
716 | walk->bytesCompleted = fileBytesCompleted( tor, i ); |
---|
717 | |
---|
718 | walk->progress = file->length |
---|
719 | ? walk->bytesCompleted / (float)file->length |
---|
720 | : 1.0; |
---|
721 | } |
---|
722 | |
---|
723 | if( fileCount ) |
---|
724 | *fileCount = n; |
---|
725 | |
---|
726 | return files; |
---|
727 | } |
---|
728 | |
---|
729 | void |
---|
730 | tr_torrentFilesFree( tr_file_stat * files, tr_file_index_t fileCount UNUSED ) |
---|
731 | { |
---|
732 | tr_free( files ); |
---|
733 | } |
---|
734 | |
---|
735 | /*** |
---|
736 | **** |
---|
737 | ***/ |
---|
738 | |
---|
739 | tr_peer_stat * |
---|
740 | tr_torrentPeers( const tr_torrent * tor, int * peerCount ) |
---|
741 | { |
---|
742 | tr_peer_stat * ret = NULL; |
---|
743 | |
---|
744 | if( tor != NULL ) |
---|
745 | ret = tr_peerMgrPeerStats( tor->handle->peerMgr, |
---|
746 | tor->info.hash, peerCount ); |
---|
747 | |
---|
748 | return ret; |
---|
749 | } |
---|
750 | |
---|
751 | void |
---|
752 | tr_torrentPeersFree( tr_peer_stat * peers, int peerCount UNUSED ) |
---|
753 | { |
---|
754 | tr_free( peers ); |
---|
755 | } |
---|
756 | |
---|
757 | void tr_torrentAvailability( const tr_torrent * tor, int8_t * tab, int size ) |
---|
758 | { |
---|
759 | return tr_peerMgrTorrentAvailability( tor->handle->peerMgr, |
---|
760 | tor->info.hash, |
---|
761 | tab, size ); |
---|
762 | } |
---|
763 | |
---|
764 | void tr_torrentAmountFinished( const tr_torrent * tor, float * tab, int size ) |
---|
765 | { |
---|
766 | int i; |
---|
767 | float interval; |
---|
768 | tr_torrentLock( tor ); |
---|
769 | |
---|
770 | interval = (float)tor->info.pieceCount / (float)size; |
---|
771 | for( i = 0; i < size; i++ ) |
---|
772 | { |
---|
773 | int piece = i * interval; |
---|
774 | tab[i] = tr_cpPercentBlocksInPiece( tor->completion, piece ); |
---|
775 | } |
---|
776 | |
---|
777 | tr_torrentUnlock( tor ); |
---|
778 | } |
---|
779 | |
---|
780 | void |
---|
781 | tr_torrentResetTransferStats( tr_torrent * tor ) |
---|
782 | { |
---|
783 | tr_torrentLock( tor ); |
---|
784 | |
---|
785 | tor->downloadedPrev += tor->downloadedCur; |
---|
786 | tor->downloadedCur = 0; |
---|
787 | tor->uploadedPrev += tor->uploadedCur; |
---|
788 | tor->uploadedCur = 0; |
---|
789 | tor->corruptPrev += tor->corruptCur; |
---|
790 | tor->corruptCur = 0; |
---|
791 | |
---|
792 | tr_torrentUnlock( tor ); |
---|
793 | } |
---|
794 | |
---|
795 | |
---|
796 | void |
---|
797 | tr_torrentSetHasPiece( tr_torrent * tor, tr_piece_index_t pieceIndex, int has ) |
---|
798 | { |
---|
799 | tr_torrentLock( tor ); |
---|
800 | |
---|
801 | assert( tor != NULL ); |
---|
802 | assert( pieceIndex < tor->info.pieceCount ); |
---|
803 | |
---|
804 | if( has ) |
---|
805 | tr_cpPieceAdd( tor->completion, pieceIndex ); |
---|
806 | else |
---|
807 | tr_cpPieceRem( tor->completion, pieceIndex ); |
---|
808 | |
---|
809 | tr_torrentUnlock( tor ); |
---|
810 | } |
---|
811 | |
---|
812 | void |
---|
813 | tr_torrentRemoveSaved( tr_torrent * tor ) |
---|
814 | { |
---|
815 | tr_metainfoRemoveSaved( tor->handle, tor->info.hashString ); |
---|
816 | |
---|
817 | tr_torrentRemoveResume( tor ); |
---|
818 | } |
---|
819 | |
---|
820 | /*** |
---|
821 | **** |
---|
822 | ***/ |
---|
823 | |
---|
824 | static void |
---|
825 | freeTorrent( tr_torrent * tor ) |
---|
826 | { |
---|
827 | tr_torrent * t; |
---|
828 | tr_handle * h = tor->handle; |
---|
829 | tr_info * inf = &tor->info; |
---|
830 | |
---|
831 | assert( tor != NULL ); |
---|
832 | assert( !tor->isRunning ); |
---|
833 | |
---|
834 | tr_globalLock( h ); |
---|
835 | |
---|
836 | tr_peerMgrRemoveTorrent( h->peerMgr, tor->info.hash ); |
---|
837 | |
---|
838 | tr_cpClose( tor->completion ); |
---|
839 | |
---|
840 | tr_rcClose( tor->upload ); |
---|
841 | tr_rcClose( tor->download ); |
---|
842 | tr_rcClose( tor->swarmspeed ); |
---|
843 | |
---|
844 | tr_trackerUnsubscribe( tor->tracker, tor->trackerSubscription ); |
---|
845 | tr_trackerFree( tor->tracker ); |
---|
846 | tor->tracker = NULL; |
---|
847 | |
---|
848 | tr_bitfieldFree( tor->checkedPieces ); |
---|
849 | |
---|
850 | tr_free( tor->destination ); |
---|
851 | |
---|
852 | if( tor == h->torrentList ) |
---|
853 | h->torrentList = tor->next; |
---|
854 | else for( t=h->torrentList; t!=NULL; t=t->next ) { |
---|
855 | if( t->next == tor ) { |
---|
856 | t->next = tor->next; |
---|
857 | break; |
---|
858 | } |
---|
859 | } |
---|
860 | |
---|
861 | assert( h->torrentCount >= 1 ); |
---|
862 | h->torrentCount--; |
---|
863 | |
---|
864 | tr_torinf( tor, _( "Closing torrent; %d left" ), h->torrentCount ); |
---|
865 | |
---|
866 | tr_metainfoFree( inf ); |
---|
867 | tr_free( tor ); |
---|
868 | |
---|
869 | tr_globalUnlock( h ); |
---|
870 | } |
---|
871 | |
---|
872 | /** |
---|
873 | *** Start/Stop Callback |
---|
874 | **/ |
---|
875 | |
---|
876 | static void |
---|
877 | fireActiveChange( tr_torrent * tor, int isRunning ) |
---|
878 | { |
---|
879 | assert( tor != NULL ); |
---|
880 | |
---|
881 | if( tor->active_func != NULL ) |
---|
882 | (tor->active_func)( tor, isRunning, tor->active_func_user_data ); |
---|
883 | } |
---|
884 | |
---|
885 | void |
---|
886 | tr_torrentSetActiveCallback( tr_torrent * tor, |
---|
887 | tr_torrent_active_func func, |
---|
888 | void * user_data ) |
---|
889 | { |
---|
890 | assert( tor != NULL ); |
---|
891 | tor->active_func = func; |
---|
892 | tor->active_func_user_data = user_data; |
---|
893 | } |
---|
894 | |
---|
895 | void |
---|
896 | tr_torrentClearActiveCallback( tr_torrent * torrent ) |
---|
897 | { |
---|
898 | tr_torrentSetActiveCallback( torrent, NULL, NULL ); |
---|
899 | } |
---|
900 | |
---|
901 | |
---|
902 | static void |
---|
903 | checkAndStartImpl( void * vtor ) |
---|
904 | { |
---|
905 | tr_torrent * tor = vtor; |
---|
906 | |
---|
907 | tr_globalLock( tor->handle ); |
---|
908 | |
---|
909 | tor->isRunning = 1; |
---|
910 | fireActiveChange( tor, tor->isRunning ); |
---|
911 | *tor->errorString = '\0'; |
---|
912 | tr_torrentResetTransferStats( tor ); |
---|
913 | tor->cpStatus = tr_cpGetStatus( tor->completion ); |
---|
914 | tr_torrentSaveResume( tor ); |
---|
915 | tor->startDate = tr_date( ); |
---|
916 | tr_trackerStart( tor->tracker ); |
---|
917 | tr_peerMgrStartTorrent( tor->handle->peerMgr, tor->info.hash ); |
---|
918 | |
---|
919 | tr_globalUnlock( tor->handle ); |
---|
920 | } |
---|
921 | |
---|
922 | static void |
---|
923 | checkAndStartCB( tr_torrent * tor ) |
---|
924 | { |
---|
925 | tr_runInEventThread( tor->handle, checkAndStartImpl, tor ); |
---|
926 | } |
---|
927 | |
---|
928 | void |
---|
929 | tr_torrentStart( tr_torrent * tor ) |
---|
930 | { |
---|
931 | tr_globalLock( tor->handle ); |
---|
932 | |
---|
933 | if( !tor->isRunning ) |
---|
934 | { |
---|
935 | tr_torrentLoadResume( tor, TR_FR_PROGRESS, NULL ); |
---|
936 | tor->isRunning = 1; |
---|
937 | tr_verifyAdd( tor, checkAndStartCB ); |
---|
938 | } |
---|
939 | |
---|
940 | tr_globalUnlock( tor->handle ); |
---|
941 | } |
---|
942 | |
---|
943 | static void |
---|
944 | torrentRecheckDoneImpl( void * vtor ) |
---|
945 | { |
---|
946 | tr_torrentRecheckCompleteness( vtor ); |
---|
947 | } |
---|
948 | static void |
---|
949 | torrentRecheckDoneCB( tr_torrent * tor ) |
---|
950 | { |
---|
951 | tr_runInEventThread( tor->handle, torrentRecheckDoneImpl, tor ); |
---|
952 | } |
---|
953 | void |
---|
954 | tr_torrentVerify( tr_torrent * tor ) |
---|
955 | { |
---|
956 | tr_globalLock( tor->handle ); |
---|
957 | |
---|
958 | tr_verifyRemove( tor ); |
---|
959 | tr_torrentUncheck( tor ); |
---|
960 | tr_verifyAdd( tor, torrentRecheckDoneCB ); |
---|
961 | |
---|
962 | tr_globalUnlock( tor->handle ); |
---|
963 | } |
---|
964 | |
---|
965 | |
---|
966 | static void |
---|
967 | stopTorrent( void * vtor ) |
---|
968 | { |
---|
969 | tr_file_index_t i; |
---|
970 | |
---|
971 | tr_torrent * tor = vtor; |
---|
972 | tr_verifyRemove( tor ); |
---|
973 | tr_peerMgrStopTorrent( tor->handle->peerMgr, tor->info.hash ); |
---|
974 | tr_trackerStop( tor->tracker ); |
---|
975 | fireActiveChange( tor, 0 ); |
---|
976 | |
---|
977 | for( i=0; i<tor->info.fileCount; ++i ) |
---|
978 | { |
---|
979 | char path[MAX_PATH_LENGTH]; |
---|
980 | const tr_file * file = &tor->info.files[i]; |
---|
981 | tr_buildPath( path, sizeof(path), tor->destination, file->name, NULL ); |
---|
982 | tr_fdFileClose( path ); |
---|
983 | } |
---|
984 | } |
---|
985 | |
---|
986 | void |
---|
987 | tr_torrentStop( tr_torrent * tor ) |
---|
988 | { |
---|
989 | tr_globalLock( tor->handle ); |
---|
990 | |
---|
991 | if( !tor->isDeleting ) |
---|
992 | tr_torrentSaveResume( tor ); |
---|
993 | tor->isRunning = 0; |
---|
994 | tr_runInEventThread( tor->handle, stopTorrent, tor ); |
---|
995 | |
---|
996 | tr_globalUnlock( tor->handle ); |
---|
997 | } |
---|
998 | |
---|
999 | static void |
---|
1000 | closeTorrent( void * vtor ) |
---|
1001 | { |
---|
1002 | tr_torrent * tor = vtor; |
---|
1003 | tr_torrentSaveResume( tor ); |
---|
1004 | tor->isRunning = 0; |
---|
1005 | stopTorrent( tor ); |
---|
1006 | if( tor->isDeleting ) |
---|
1007 | tr_torrentRemoveSaved( tor ); |
---|
1008 | freeTorrent( tor ); |
---|
1009 | } |
---|
1010 | |
---|
1011 | void |
---|
1012 | tr_torrentClose( tr_torrent * tor ) |
---|
1013 | { |
---|
1014 | if( tor != NULL ) |
---|
1015 | { |
---|
1016 | tr_handle * handle = tor->handle; |
---|
1017 | tr_globalLock( handle ); |
---|
1018 | |
---|
1019 | tr_torrentClearStatusCallback( tor ); |
---|
1020 | tr_runInEventThread( handle, closeTorrent, tor ); |
---|
1021 | |
---|
1022 | tr_globalUnlock( handle ); |
---|
1023 | } |
---|
1024 | } |
---|
1025 | |
---|
1026 | void |
---|
1027 | tr_torrentDelete( tr_torrent * tor ) |
---|
1028 | { |
---|
1029 | tor->isDeleting = 1; |
---|
1030 | tr_torrentClose( tor ); |
---|
1031 | } |
---|
1032 | |
---|
1033 | |
---|
1034 | /** |
---|
1035 | *** Completeness |
---|
1036 | **/ |
---|
1037 | |
---|
1038 | static const char * |
---|
1039 | getCompletionString( int type ) |
---|
1040 | { |
---|
1041 | switch( type ) |
---|
1042 | { |
---|
1043 | /* Translators: this is a minor point that's safe to skip over, but FYI: |
---|
1044 | "Complete" and "Done" are specific, different terms in Transmission: |
---|
1045 | "Complete" means we've downloaded every file in the torrent. |
---|
1046 | "Done" means we're done downloading the files we wanted, but NOT all that exist */ |
---|
1047 | case TR_CP_DONE: return _( "Done" ); |
---|
1048 | case TR_CP_COMPLETE: return _( "Complete" ); |
---|
1049 | default: return _( "Incomplete" ); |
---|
1050 | } |
---|
1051 | } |
---|
1052 | |
---|
1053 | static void |
---|
1054 | fireStatusChange( tr_torrent * tor, cp_status_t status ) |
---|
1055 | { |
---|
1056 | assert( tor != NULL ); |
---|
1057 | assert( status==TR_CP_INCOMPLETE || status==TR_CP_DONE || status==TR_CP_COMPLETE ); |
---|
1058 | |
---|
1059 | if( tor->status_func != NULL ) |
---|
1060 | (tor->status_func)( tor, status, tor->status_func_user_data ); |
---|
1061 | } |
---|
1062 | |
---|
1063 | void |
---|
1064 | tr_torrentSetStatusCallback( tr_torrent * tor, |
---|
1065 | tr_torrent_status_func func, |
---|
1066 | void * user_data ) |
---|
1067 | { |
---|
1068 | assert( tor != NULL ); |
---|
1069 | tor->status_func = func; |
---|
1070 | tor->status_func_user_data = user_data; |
---|
1071 | } |
---|
1072 | |
---|
1073 | void |
---|
1074 | tr_torrentClearStatusCallback( tr_torrent * torrent ) |
---|
1075 | { |
---|
1076 | tr_torrentSetStatusCallback( torrent, NULL, NULL ); |
---|
1077 | } |
---|
1078 | |
---|
1079 | void |
---|
1080 | tr_torrentRecheckCompleteness( tr_torrent * tor ) |
---|
1081 | { |
---|
1082 | cp_status_t cpStatus; |
---|
1083 | |
---|
1084 | tr_torrentLock( tor ); |
---|
1085 | |
---|
1086 | cpStatus = tr_cpGetStatus( tor->completion ); |
---|
1087 | |
---|
1088 | if( cpStatus != tor->cpStatus ) |
---|
1089 | { |
---|
1090 | const int recentChange = tor->downloadedCur != 0; |
---|
1091 | |
---|
1092 | if( recentChange ) |
---|
1093 | { |
---|
1094 | tr_torinf( tor, _( "State changed from \"%s\" to \"%s\"" ), |
---|
1095 | getCompletionString( tor->cpStatus ), |
---|
1096 | getCompletionString( cpStatus ) ); |
---|
1097 | } |
---|
1098 | |
---|
1099 | tor->cpStatus = cpStatus; |
---|
1100 | fireStatusChange( tor, cpStatus ); |
---|
1101 | |
---|
1102 | if( recentChange && ( cpStatus == TR_CP_COMPLETE ) ) |
---|
1103 | tr_trackerCompleted( tor->tracker ); |
---|
1104 | |
---|
1105 | tr_torrentSaveResume( tor ); |
---|
1106 | } |
---|
1107 | |
---|
1108 | tr_torrentUnlock( tor ); |
---|
1109 | } |
---|
1110 | |
---|
1111 | int |
---|
1112 | tr_torrentIsSeed( const tr_torrent * tor ) |
---|
1113 | { |
---|
1114 | return tor->cpStatus==TR_CP_COMPLETE || tor->cpStatus==TR_CP_DONE; |
---|
1115 | } |
---|
1116 | |
---|
1117 | /** |
---|
1118 | *** File priorities |
---|
1119 | **/ |
---|
1120 | |
---|
1121 | void |
---|
1122 | tr_torrentInitFilePriority( tr_torrent * tor, |
---|
1123 | tr_file_index_t fileIndex, |
---|
1124 | tr_priority_t priority ) |
---|
1125 | { |
---|
1126 | tr_piece_index_t i; |
---|
1127 | tr_file * file; |
---|
1128 | |
---|
1129 | assert( tor != NULL ); |
---|
1130 | assert( fileIndex < tor->info.fileCount ); |
---|
1131 | assert( priority==TR_PRI_LOW || priority==TR_PRI_NORMAL || priority==TR_PRI_HIGH ); |
---|
1132 | |
---|
1133 | file = &tor->info.files[fileIndex]; |
---|
1134 | file->priority = priority; |
---|
1135 | for( i=file->firstPiece; i<=file->lastPiece; ++i ) |
---|
1136 | tor->info.pieces[i].priority = calculatePiecePriority( tor, i, fileIndex ); |
---|
1137 | } |
---|
1138 | |
---|
1139 | void |
---|
1140 | tr_torrentSetFilePriorities( tr_torrent * tor, |
---|
1141 | tr_file_index_t * files, |
---|
1142 | tr_file_index_t fileCount, |
---|
1143 | tr_priority_t priority ) |
---|
1144 | { |
---|
1145 | tr_file_index_t i; |
---|
1146 | tr_torrentLock( tor ); |
---|
1147 | |
---|
1148 | for( i=0; i<fileCount; ++i ) |
---|
1149 | tr_torrentInitFilePriority( tor, files[i], priority ); |
---|
1150 | |
---|
1151 | tr_torrentSaveResume( tor ); |
---|
1152 | tr_torrentUnlock( tor ); |
---|
1153 | } |
---|
1154 | |
---|
1155 | tr_priority_t |
---|
1156 | tr_torrentGetFilePriority( const tr_torrent * tor, tr_file_index_t file ) |
---|
1157 | { |
---|
1158 | tr_priority_t ret; |
---|
1159 | |
---|
1160 | tr_torrentLock( tor ); |
---|
1161 | assert( tor != NULL ); |
---|
1162 | assert( file < tor->info.fileCount ); |
---|
1163 | ret = tor->info.files[file].priority; |
---|
1164 | tr_torrentUnlock( tor ); |
---|
1165 | |
---|
1166 | return ret; |
---|
1167 | } |
---|
1168 | |
---|
1169 | tr_priority_t* |
---|
1170 | tr_torrentGetFilePriorities( const tr_torrent * tor ) |
---|
1171 | { |
---|
1172 | tr_file_index_t i; |
---|
1173 | tr_priority_t * p; |
---|
1174 | |
---|
1175 | tr_torrentLock( tor ); |
---|
1176 | p = tr_new0( tr_priority_t, tor->info.fileCount ); |
---|
1177 | for( i=0; i<tor->info.fileCount; ++i ) |
---|
1178 | p[i] = tor->info.files[i].priority; |
---|
1179 | tr_torrentUnlock( tor ); |
---|
1180 | |
---|
1181 | return p; |
---|
1182 | } |
---|
1183 | |
---|
1184 | /** |
---|
1185 | *** File DND |
---|
1186 | **/ |
---|
1187 | |
---|
1188 | int |
---|
1189 | tr_torrentGetFileDL( const tr_torrent * tor, |
---|
1190 | tr_file_index_t file ) |
---|
1191 | { |
---|
1192 | int doDownload; |
---|
1193 | tr_torrentLock( tor ); |
---|
1194 | |
---|
1195 | assert( file < tor->info.fileCount ); |
---|
1196 | doDownload = !tor->info.files[file].dnd; |
---|
1197 | |
---|
1198 | tr_torrentUnlock( tor ); |
---|
1199 | return doDownload != 0; |
---|
1200 | } |
---|
1201 | |
---|
1202 | static void |
---|
1203 | setFileDND( tr_torrent * tor, |
---|
1204 | tr_file_index_t fileIndex, |
---|
1205 | int doDownload ) |
---|
1206 | { |
---|
1207 | tr_file * file; |
---|
1208 | const int dnd = !doDownload; |
---|
1209 | tr_piece_index_t firstPiece, firstPieceDND; |
---|
1210 | tr_piece_index_t lastPiece, lastPieceDND; |
---|
1211 | tr_file_index_t i; |
---|
1212 | |
---|
1213 | file = &tor->info.files[fileIndex]; |
---|
1214 | file->dnd = dnd; |
---|
1215 | firstPiece = file->firstPiece; |
---|
1216 | lastPiece = file->lastPiece; |
---|
1217 | |
---|
1218 | /* can't set the first piece to DND unless |
---|
1219 | every file using that piece is DND */ |
---|
1220 | firstPieceDND = dnd; |
---|
1221 | if( fileIndex > 0 ) { |
---|
1222 | for( i=fileIndex-1; firstPieceDND; --i ) { |
---|
1223 | if( tor->info.files[i].lastPiece != firstPiece ) |
---|
1224 | break; |
---|
1225 | firstPieceDND = tor->info.files[i].dnd; |
---|
1226 | if( !i ) |
---|
1227 | break; |
---|
1228 | } |
---|
1229 | } |
---|
1230 | |
---|
1231 | /* can't set the last piece to DND unless |
---|
1232 | every file using that piece is DND */ |
---|
1233 | lastPieceDND = dnd; |
---|
1234 | for( i=fileIndex+1; lastPieceDND && i<tor->info.fileCount; ++i ) { |
---|
1235 | if( tor->info.files[i].firstPiece != lastPiece ) |
---|
1236 | break; |
---|
1237 | lastPieceDND = tor->info.files[i].dnd; |
---|
1238 | } |
---|
1239 | |
---|
1240 | if( firstPiece == lastPiece ) |
---|
1241 | { |
---|
1242 | tor->info.pieces[firstPiece].dnd = firstPieceDND && lastPieceDND; |
---|
1243 | } |
---|
1244 | else |
---|
1245 | { |
---|
1246 | tr_piece_index_t pp; |
---|
1247 | tor->info.pieces[firstPiece].dnd = firstPieceDND; |
---|
1248 | tor->info.pieces[lastPiece].dnd = lastPieceDND; |
---|
1249 | for( pp=firstPiece+1; pp<lastPiece; ++pp ) |
---|
1250 | tor->info.pieces[pp].dnd = dnd; |
---|
1251 | } |
---|
1252 | } |
---|
1253 | |
---|
1254 | void |
---|
1255 | tr_torrentInitFileDLs ( tr_torrent * tor, |
---|
1256 | tr_file_index_t * files, |
---|
1257 | tr_file_index_t fileCount, |
---|
1258 | int doDownload ) |
---|
1259 | { |
---|
1260 | tr_file_index_t i; |
---|
1261 | tr_torrentLock( tor ); |
---|
1262 | |
---|
1263 | for( i=0; i<fileCount; ++i ) |
---|
1264 | setFileDND( tor, files[i], doDownload ); |
---|
1265 | tr_cpInvalidateDND ( tor->completion ); |
---|
1266 | |
---|
1267 | tr_torrentUnlock( tor ); |
---|
1268 | } |
---|
1269 | |
---|
1270 | void |
---|
1271 | tr_torrentSetFileDLs ( tr_torrent * tor, |
---|
1272 | tr_file_index_t * files, |
---|
1273 | tr_file_index_t fileCount, |
---|
1274 | int doDownload ) |
---|
1275 | { |
---|
1276 | tr_torrentLock( tor ); |
---|
1277 | tr_torrentInitFileDLs( tor, files, fileCount, doDownload ); |
---|
1278 | tr_torrentSaveResume( tor ); |
---|
1279 | tr_torrentUnlock( tor ); |
---|
1280 | } |
---|
1281 | |
---|
1282 | /*** |
---|
1283 | **** |
---|
1284 | ***/ |
---|
1285 | |
---|
1286 | void |
---|
1287 | tr_torrentSetMaxConnectedPeers( tr_torrent * tor, |
---|
1288 | uint16_t maxConnectedPeers ) |
---|
1289 | { |
---|
1290 | tor->maxConnectedPeers = maxConnectedPeers; |
---|
1291 | } |
---|
1292 | |
---|
1293 | uint16_t |
---|
1294 | tr_torrentGetMaxConnectedPeers( const tr_torrent * tor ) |
---|
1295 | { |
---|
1296 | return tor->maxConnectedPeers; |
---|
1297 | } |
---|
1298 | |
---|
1299 | /*** |
---|
1300 | **** |
---|
1301 | ***/ |
---|
1302 | |
---|
1303 | tr_block_index_t |
---|
1304 | _tr_block( const tr_torrent * tor, |
---|
1305 | tr_piece_index_t index, |
---|
1306 | uint32_t offset ) |
---|
1307 | { |
---|
1308 | const tr_info * inf = &tor->info; |
---|
1309 | tr_block_index_t ret; |
---|
1310 | ret = index; |
---|
1311 | ret *= ( inf->pieceSize / tor->blockSize ); |
---|
1312 | ret += offset / tor->blockSize; |
---|
1313 | return ret; |
---|
1314 | } |
---|
1315 | |
---|
1316 | int |
---|
1317 | tr_torrentReqIsValid( const tr_torrent * tor, |
---|
1318 | tr_piece_index_t index, |
---|
1319 | uint32_t offset, |
---|
1320 | uint32_t length ) |
---|
1321 | { |
---|
1322 | int err = 0; |
---|
1323 | |
---|
1324 | if( index >= tor->info.pieceCount ) |
---|
1325 | err = 1; |
---|
1326 | else if ( offset >= tr_torPieceCountBytes( tor, index ) ) |
---|
1327 | err = 2; |
---|
1328 | else if( length > MAX_BLOCK_SIZE ) |
---|
1329 | err = 3; |
---|
1330 | else if( tr_pieceOffset( tor, index, offset, length ) > tor->info.totalSize ) |
---|
1331 | err = 4; |
---|
1332 | |
---|
1333 | if( err ) |
---|
1334 | { |
---|
1335 | fprintf( stderr, "(ticket #751) err is %d\n", err ); |
---|
1336 | fprintf( stderr, "(ticket #751) req.index is %"PRIu32"\n", index ); |
---|
1337 | fprintf( stderr, "(ticket #751) req.offset is %"PRIu32"\n", offset ); |
---|
1338 | fprintf( stderr, "(ticket #751) req.length is %"PRIu32"\n", length ); |
---|
1339 | fprintf( stderr, "(ticket #751) tor->info.totalSize is %"PRIu64"\n", tor->info.totalSize ); |
---|
1340 | fprintf( stderr, "(ticket #751) tor->info.pieceCount is %d\n", tor->info.pieceCount ); |
---|
1341 | fprintf( stderr, "(ticket #751) tr_torPieceCountBytes is %d\n", tr_torPieceCountBytes( tor, index ) ); |
---|
1342 | fprintf( stderr, "(ticket #751) tr_pieceOffset is %"PRIu64"\n", tr_pieceOffset( tor, index, offset, length ) ); |
---|
1343 | } |
---|
1344 | |
---|
1345 | return !err; |
---|
1346 | } |
---|
1347 | |
---|
1348 | |
---|
1349 | uint64_t |
---|
1350 | tr_pieceOffset( const tr_torrent * tor, |
---|
1351 | tr_piece_index_t index, |
---|
1352 | uint32_t offset, |
---|
1353 | uint32_t length ) |
---|
1354 | { |
---|
1355 | uint64_t ret; |
---|
1356 | ret = tor->info.pieceSize; |
---|
1357 | ret *= index; |
---|
1358 | ret += offset; |
---|
1359 | ret += length; |
---|
1360 | return ret; |
---|
1361 | } |
---|
1362 | |
---|
1363 | /*** |
---|
1364 | **** |
---|
1365 | ***/ |
---|
1366 | |
---|
1367 | int |
---|
1368 | tr_torrentIsPieceChecked( const tr_torrent * tor, tr_piece_index_t piece ) |
---|
1369 | { |
---|
1370 | return tr_bitfieldHas( tor->checkedPieces, piece ); |
---|
1371 | } |
---|
1372 | |
---|
1373 | void |
---|
1374 | tr_torrentSetPieceChecked( tr_torrent * tor, tr_piece_index_t piece, int isChecked ) |
---|
1375 | { |
---|
1376 | if( isChecked ) |
---|
1377 | tr_bitfieldAdd( tor->checkedPieces, piece ); |
---|
1378 | else |
---|
1379 | tr_bitfieldRem( tor->checkedPieces, piece ); |
---|
1380 | } |
---|
1381 | |
---|
1382 | void |
---|
1383 | tr_torrentSetFileChecked( tr_torrent * tor, tr_file_index_t fileIndex, int isChecked ) |
---|
1384 | { |
---|
1385 | const tr_file * file = &tor->info.files[fileIndex]; |
---|
1386 | const tr_piece_index_t begin = file->firstPiece; |
---|
1387 | const tr_piece_index_t end = file->lastPiece + 1; |
---|
1388 | |
---|
1389 | if( isChecked ) |
---|
1390 | tr_bitfieldAddRange ( tor->checkedPieces, begin, end ); |
---|
1391 | else |
---|
1392 | tr_bitfieldRemRange ( tor->checkedPieces, begin, end ); |
---|
1393 | } |
---|
1394 | |
---|
1395 | int |
---|
1396 | tr_torrentIsFileChecked( const tr_torrent * tor, tr_file_index_t fileIndex ) |
---|
1397 | { |
---|
1398 | const tr_file * file = &tor->info.files[fileIndex]; |
---|
1399 | const tr_piece_index_t begin = file->firstPiece; |
---|
1400 | const tr_piece_index_t end = file->lastPiece + 1; |
---|
1401 | tr_piece_index_t i; |
---|
1402 | int isChecked = TRUE; |
---|
1403 | |
---|
1404 | for( i=begin; isChecked && i<end; ++i ) |
---|
1405 | if( !tr_torrentIsPieceChecked( tor, i ) ) |
---|
1406 | isChecked = FALSE; |
---|
1407 | |
---|
1408 | return isChecked; |
---|
1409 | } |
---|
1410 | |
---|
1411 | void |
---|
1412 | tr_torrentUncheck( tr_torrent * tor ) |
---|
1413 | { |
---|
1414 | tr_bitfieldRemRange ( tor->checkedPieces, 0, tor->info.pieceCount ); |
---|
1415 | } |
---|
1416 | |
---|
1417 | int |
---|
1418 | tr_torrentCountUncheckedPieces( const tr_torrent * tor ) |
---|
1419 | { |
---|
1420 | return tor->info.pieceCount - tr_bitfieldCountTrueBits( tor->checkedPieces ); |
---|
1421 | } |
---|