source: trunk/libtransmission/torrent.c @ 5081

Last change on this file since 5081 was 5081, checked in by charles, 14 years ago

remove unnecessary #includes

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