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