source: trunk/libtransmission/torrent.c @ 3233

Last change on this file since 3233 was 3233, checked in by charles, 15 years ago

test to see if the new locking code can fix the old `stop' bug

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