source: trunk/libtransmission/torrent.c @ 7064

Last change on this file since 7064 was 7064, checked in by charles, 13 years ago

#1429 (libT) cleaner handling of the special case where the upload or download speed limit is zero

  • Property svn:keywords set to Date Rev Author Id
File size: 45.1 KB
Line 
1/******************************************************************************
2 * $Id: torrent.c 7064 2008-11-07 04:10:27Z 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 <limits.h> /* INT_MAX */
31#include <string.h> /* memcmp */
32#include <stdlib.h> /* qsort */
33
34#include "transmission.h"
35#include "bencode.h"
36#include "completion.h"
37#include "crypto.h" /* for tr_sha1 */
38#include "resume.h"
39#include "fdlimit.h" /* tr_fdFileClose */
40#include "metainfo.h"
41#include "peer-mgr.h"
42#include "ratecontrol.h"
43#include "torrent.h"
44#include "tracker.h"
45#include "trevent.h"
46#include "utils.h"
47#include "verify.h"
48
49#define MAX_BLOCK_SIZE ( 1024 * 16 )
50
51/***
52****
53***/
54
55int
56tr_torrentId( const tr_torrent * tor )
57{
58    return tor->uniqueId;
59}
60
61tr_torrent*
62tr_torrentFindFromId( tr_handle * handle,
63                      int         id )
64{
65    tr_torrent * tor = NULL;
66
67    while( ( tor = tr_torrentNext( handle, tor ) ) )
68        if( tor->uniqueId == id )
69            return tor;
70
71    return NULL;
72}
73
74tr_torrent*
75tr_torrentFindFromHashString( tr_handle *  handle,
76                              const char * str )
77{
78    tr_torrent * tor = NULL;
79
80    while( ( tor = tr_torrentNext( handle, tor ) ) )
81        if( !strcmp( str, tor->info.hashString ) )
82            return tor;
83
84    return NULL;
85}
86
87int
88tr_torrentExists( const tr_handle * handle,
89                  const uint8_t *   torrentHash )
90{
91    return tr_torrentFindFromHash( (tr_handle*)handle,
92                                  torrentHash ) != NULL;
93}
94
95tr_torrent*
96tr_torrentFindFromHash( tr_handle *     handle,
97                        const uint8_t * torrentHash )
98{
99    tr_torrent * tor = NULL;
100
101    while( ( tor = tr_torrentNext( handle, tor ) ) )
102        if( *tor->info.hash == *torrentHash )
103            if( !memcmp( tor->info.hash, torrentHash, SHA_DIGEST_LENGTH ) )
104                return tor;
105
106    return NULL;
107}
108
109tr_torrent*
110tr_torrentFindFromObfuscatedHash( tr_handle *     handle,
111                                  const uint8_t * obfuscatedTorrentHash )
112{
113    tr_torrent * tor = NULL;
114
115    while( ( tor = tr_torrentNext( handle, tor ) ) )
116        if( !memcmp( tor->obfuscatedHash, obfuscatedTorrentHash,
117                     SHA_DIGEST_LENGTH ) )
118            return tor;
119
120    return NULL;
121}
122
123/***
124****  LOCKS
125***/
126
127void
128tr_torrentLock( const tr_torrent * tor )
129{
130    tr_globalLock( tor->session );
131}
132
133void
134tr_torrentUnlock( const tr_torrent * tor )
135{
136    tr_globalUnlock( tor->session );
137}
138
139/***
140****  PER-TORRENT UL / DL SPEEDS
141***/
142
143void
144tr_torrentSetSpeedMode( tr_torrent *  tor,
145                        tr_direction  direction,
146                        tr_speedlimit mode )
147{
148    tr_speedlimit * limit = direction == TR_UP ? &tor->uploadLimitMode
149                                               : &tor->downloadLimitMode;
150
151    *limit = mode;
152}
153
154tr_speedlimit
155tr_torrentGetSpeedMode( const tr_torrent * tor,
156                        tr_direction       direction )
157{
158    return direction == TR_UP ? tor->uploadLimitMode
159                              : tor->downloadLimitMode;
160}
161
162void
163tr_torrentSetSpeedLimit( tr_torrent * tor,
164                         tr_direction direction,
165                         int          single_KiB_sec )
166{
167    switch( direction )
168    {
169        case TR_UP:
170            tor->uploadLimit = single_KiB_sec; break;
171
172        case TR_DOWN:
173            tor->downloadLimit = single_KiB_sec; break;
174
175        default:
176            assert( 0 );
177    }
178}
179
180int
181tr_torrentGetSpeedLimit( const tr_torrent * tor,
182                         tr_direction       direction )
183{
184    switch( direction )
185    {
186        case TR_UP:
187            return tor->uploadLimit;
188
189        case TR_DOWN:
190            return tor->downloadLimit;
191
192        default:
193            assert( 0 );
194    }
195}
196
197int
198tr_torrentPieceTransferIsAllowed( const tr_torrent  * tor,
199                                  tr_direction        direction )
200{
201    int isEnabled = FALSE;
202
203    switch( tr_torrentGetSpeedMode( tor, direction ) )
204    {
205        case TR_SPEEDLIMIT_GLOBAL:
206            isEnabled = tr_sessionGetSpeedLimit( tor->session, direction ) > 0;
207            break;
208
209        case TR_SPEEDLIMIT_SINGLE:
210            isEnabled = tr_torrentGetSpeedLimit( tor, direction ) > 0;
211            break;
212
213        case TR_SPEEDLIMIT_UNLIMITED:
214            isEnabled = TRUE;
215            break;
216
217        default:
218            assert( 0 && "unhandled speed mode" );
219            break;
220    }
221
222    return isEnabled;
223}
224
225/***
226****
227***/
228
229static void
230onTrackerResponse( void * tracker UNUSED,
231                   void *         vevent,
232                   void *         user_data )
233{
234    tr_torrent *       tor = user_data;
235    tr_tracker_event * event = vevent;
236
237    switch( event->messageType )
238    {
239        case TR_TRACKER_PEERS:
240        {
241            size_t   i, n;
242            tr_pex * pex = tr_peerMgrCompactToPex( event->compact,
243                                                   event->compactLen,
244                                                   NULL, 0, &n );
245            if( event->allAreSeeds )
246                tr_tordbg( tor, "Got %d seeds from tracker", (int)n );
247            else
248                tr_torinf( tor, _( "Got %d peers from tracker" ), (int)n );
249
250            for( i = 0; i < n; ++i )
251            {
252                if( event->allAreSeeds )
253                    pex[i].flags |= ADDED_F_SEED_FLAG;
254                tr_peerMgrAddPex( tor->session->peerMgr, tor->info.hash,
255                                  TR_PEER_FROM_TRACKER, pex + i );
256            }
257
258            tr_free( pex );
259            break;
260        }
261
262        case TR_TRACKER_WARNING:
263            tr_torerr( tor, _( "Tracker warning: \"%s\"" ), event->text );
264            tor->error = -1;
265            tr_strlcpy( tor->errorString, event->text,
266                       sizeof( tor->errorString ) );
267            break;
268
269        case TR_TRACKER_ERROR:
270            tr_torerr( tor, _( "Tracker error: \"%s\"" ), event->text );
271            tor->error = -2;
272            tr_strlcpy( tor->errorString, event->text,
273                       sizeof( tor->errorString ) );
274            break;
275
276        case TR_TRACKER_ERROR_CLEAR:
277            tor->error = 0;
278            tor->errorString[0] = '\0';
279            break;
280    }
281}
282
283/***
284****
285****  TORRENT INSTANTIATION
286****
287***/
288
289static int
290getBytePiece( const tr_info * info,
291              uint64_t        byteOffset )
292{
293    assert( info );
294    assert( info->pieceSize != 0 );
295
296    return byteOffset / info->pieceSize;
297}
298
299static void
300initFilePieces( tr_info *       info,
301                tr_file_index_t fileIndex )
302{
303    tr_file * file;
304    uint64_t  firstByte, lastByte;
305
306    assert( info );
307    assert( fileIndex < info->fileCount );
308
309    file = &info->files[fileIndex];
310    firstByte = file->offset;
311    lastByte = firstByte + ( file->length ? file->length - 1 : 0 );
312    file->firstPiece = getBytePiece( info, firstByte );
313    file->lastPiece = getBytePiece( info, lastByte );
314}
315
316static int
317pieceHasFile( tr_piece_index_t piece,
318              const tr_file *  file )
319{
320    return ( file->firstPiece <= piece ) && ( piece <= file->lastPiece );
321}
322
323static tr_priority_t
324calculatePiecePriority( const tr_torrent * tor,
325                        tr_piece_index_t   piece,
326                        int                fileHint )
327{
328    tr_file_index_t i;
329    int             priority = TR_PRI_LOW;
330
331    /* find the first file that has data in this piece */
332    if( fileHint >= 0 )
333    {
334        i = fileHint;
335        while( i > 0 && pieceHasFile( piece, &tor->info.files[i - 1] ) )
336            --i;
337    }
338    else
339    {
340        for( i = 0; i < tor->info.fileCount; ++i )
341            if( pieceHasFile( piece, &tor->info.files[i] ) )
342                break;
343    }
344
345    /* the piece's priority is the max of the priorities
346     * of all the files in that piece */
347    for( ; i < tor->info.fileCount; ++i )
348    {
349        const tr_file * file = &tor->info.files[i];
350
351        if( !pieceHasFile( piece, file ) )
352            break;
353
354        priority = MAX( priority, file->priority );
355
356        /* when dealing with multimedia files, getting the first and
357           last pieces can sometimes allow you to preview it a bit
358           before it's fully downloaded... */
359        if( file->priority >= TR_PRI_NORMAL )
360            if( file->firstPiece == piece || file->lastPiece == piece )
361                priority = TR_PRI_HIGH;
362    }
363
364    return priority;
365}
366
367static void
368tr_torrentInitFilePieces( tr_torrent * tor )
369{
370    tr_file_index_t  ff;
371    tr_piece_index_t pp;
372    uint64_t         offset = 0;
373    tr_info *        inf = &tor->info;
374
375    assert( inf );
376
377    for( ff = 0; ff < inf->fileCount; ++ff )
378    {
379        inf->files[ff].offset = offset;
380        offset += inf->files[ff].length;
381        initFilePieces( inf, ff );
382    }
383
384    for( pp = 0; pp < inf->pieceCount; ++pp )
385        inf->pieces[pp].priority = calculatePiecePriority( tor, pp, -1 );
386}
387
388int
389tr_torrentPromoteTracker( tr_torrent * tor,
390                          int          pos )
391{
392    int i;
393    int tier;
394
395    assert( tor );
396    assert( ( 0 <= pos ) && ( pos < tor->info.trackerCount ) );
397
398    /* the tier of the tracker we're promoting */
399    tier = tor->info.trackers[pos].tier;
400
401    /* find the index of that tier's first tracker */
402    for( i = 0; i < tor->info.trackerCount; ++i )
403        if( tor->info.trackers[i].tier == tier )
404            break;
405
406    assert( i < tor->info.trackerCount );
407
408    /* promote the tracker at `pos' to the front of the tier */
409    if( i != pos )
410    {
411        const tr_tracker_info tmp = tor->info.trackers[i];
412        tor->info.trackers[i] = tor->info.trackers[pos];
413        tor->info.trackers[pos] = tmp;
414    }
415
416    /* return the new position of the tracker that started out at [pos] */
417    return i;
418}
419
420struct RandomTracker
421{
422    tr_tracker_info    tracker;
423    int                random_value;
424};
425
426/* the tiers will be sorted from lowest to highest,
427 * and trackers are randomized within the tiers */
428static int
429compareRandomTracker( const void * va,
430                      const void * vb )
431{
432    const struct RandomTracker * a = va;
433    const struct RandomTracker * b = vb;
434
435    if( a->tracker.tier != b->tracker.tier )
436        return a->tracker.tier - b->tracker.tier;
437
438    return a->random_value - b->random_value;
439}
440
441static void
442randomizeTiers( tr_info * info )
443{
444    int                    i;
445    const int              n = info->trackerCount;
446    struct RandomTracker * r = tr_new0( struct RandomTracker, n );
447
448    for( i = 0; i < n; ++i )
449    {
450        r[i].tracker = info->trackers[i];
451        r[i].random_value = tr_cryptoRandInt( INT_MAX );
452    }
453    qsort( r, n, sizeof( struct RandomTracker ), compareRandomTracker );
454    for( i = 0; i < n; ++i )
455        info->trackers[i] = r[i].tracker;
456    tr_free( r );
457}
458
459static void torrentStart( tr_torrent * tor,
460                          int          reloadProgress );
461
462/**
463 * Decide on a block size.  constraints:
464 * (1) most clients decline requests over 16 KiB
465 * (2) pieceSize must be a multiple of block size
466 */
467static uint32_t
468getBlockSize( uint32_t pieceSize )
469{
470    uint32_t b = pieceSize;
471
472    while( b > MAX_BLOCK_SIZE )
473        b /= 2u;
474
475    if( !b || ( pieceSize % b ) ) /* not cleanly divisible */
476        return 0;
477    return b;
478}
479
480static void
481torrentRealInit( tr_handle *     h,
482                 tr_torrent *    tor,
483                 const tr_ctor * ctor )
484{
485    int        doStart;
486    uint64_t   loaded;
487    uint64_t   t;
488    tr_info *  info = &tor->info;
489    static int nextUniqueId = 1;
490
491    tr_globalLock( h );
492
493    tor->session   = h;
494    tor->uniqueId = nextUniqueId++;
495
496    randomizeTiers( info );
497
498    tor->pieceSpeed[TR_CLIENT_TO_PEER] = tr_rcInit( );
499    tor->pieceSpeed[TR_PEER_TO_CLIENT] = tr_rcInit( );
500
501    tor->blockSize = getBlockSize( info->pieceSize );
502
503    tor->lastPieceSize = info->totalSize % info->pieceSize;
504
505    if( !tor->lastPieceSize )
506        tor->lastPieceSize = info->pieceSize;
507
508    tor->lastBlockSize = info->totalSize % tor->blockSize;
509
510    if( !tor->lastBlockSize )
511        tor->lastBlockSize = tor->blockSize;
512
513    tor->blockCount =
514        ( info->totalSize + tor->blockSize - 1 ) / tor->blockSize;
515
516    tor->blockCountInPiece =
517        info->pieceSize / tor->blockSize;
518
519    tor->blockCountInLastPiece =
520        ( tor->lastPieceSize + tor->blockSize - 1 ) / tor->blockSize;
521
522    /* check our work */
523    assert( ( info->pieceSize % tor->blockSize ) == 0 );
524    t = info->pieceCount - 1;
525    t *= info->pieceSize;
526    t += tor->lastPieceSize;
527    assert( t == info->totalSize );
528    t = tor->blockCount - 1;
529    t *= tor->blockSize;
530    t += tor->lastBlockSize;
531    assert( t == info->totalSize );
532    t = info->pieceCount - 1;
533    t *= tor->blockCountInPiece;
534    t += tor->blockCountInLastPiece;
535    assert( t == (uint64_t)tor->blockCount );
536
537    tor->completion = tr_cpInit( tor );
538
539    tr_torrentInitFilePieces( tor );
540
541    tor->uploadLimit = 0;
542    tor->downloadLimit = 0;
543    tor->swarmSpeed = tr_rcInit( );
544
545    tr_sha1( tor->obfuscatedHash, "req2", 4,
546             info->hash, SHA_DIGEST_LENGTH,
547             NULL );
548
549    tr_peerMgrAddTorrent( h->peerMgr, tor );
550
551    if( !h->isPortSet )
552        tr_sessionSetPeerPort( h, TR_DEFAULT_PORT );
553
554    assert( !tor->downloadedCur );
555    assert( !tor->uploadedCur );
556
557    tor->error   = 0;
558
559    tor->checkedPieces = tr_bitfieldNew( tor->info.pieceCount );
560    tr_torrentUncheck( tor );
561
562    tor->addedDate = time( NULL ); /* this is a default value to be
563                                      overwritten by the resume file */
564    loaded = tr_torrentLoadResume( tor, ~0, ctor );
565
566    doStart = tor->isRunning;
567    tor->isRunning = 0;
568
569    if( !( loaded & TR_FR_SPEEDLIMIT ) )
570    {
571        tr_torrentSetSpeedLimit( tor, TR_UP,
572                                tr_sessionGetSpeedLimit( tor->session, TR_UP ) );
573        tr_torrentSetSpeedLimit( tor, TR_DOWN,
574                                tr_sessionGetSpeedLimit( tor->session,
575                                                         TR_DOWN ) );
576    }
577
578    tor->completeness = tr_cpGetStatus( tor->completion );
579
580    tor->tracker = tr_trackerNew( tor );
581    tor->trackerSubscription =
582        tr_trackerSubscribe( tor->tracker, onTrackerResponse,
583                             tor );
584
585    {
586        tr_torrent * it = NULL;
587        tr_torrent * last = NULL;
588        while( ( it = tr_torrentNext( h, it ) ) )
589            last = it;
590
591        if( !last )
592            h->torrentList = tor;
593        else
594            last->next = tor;
595        ++h->torrentCount;
596    }
597
598    tr_globalUnlock( h );
599
600    /* maybe save our own copy of the metainfo */
601    if( tr_ctorGetSave( ctor ) )
602    {
603        const tr_benc * val;
604        if( !tr_ctorGetMetainfo( ctor, &val ) )
605        {
606            const char * filename = tor->info.torrent;
607            tr_bencSaveFile( filename, val );
608            tr_sessionSetTorrentFile( tor->session, tor->info.hashString,
609                                      filename );
610        }
611    }
612
613    tr_metainfoMigrate( h, &tor->info );
614
615    if( doStart )
616        torrentStart( tor, FALSE );
617}
618
619int
620tr_torrentParse( const tr_handle * handle,
621                 const tr_ctor *   ctor,
622                 tr_info *         setmeInfo )
623{
624    int             err = 0;
625    int             doFree;
626    tr_info         tmp;
627    const tr_benc * metainfo;
628
629    if( setmeInfo == NULL )
630        setmeInfo = &tmp;
631    memset( setmeInfo, 0, sizeof( tr_info ) );
632
633    if( !err && tr_ctorGetMetainfo( ctor, &metainfo ) )
634        return TR_EINVALID;
635
636    err = tr_metainfoParse( handle, setmeInfo, metainfo );
637    doFree = !err && ( setmeInfo == &tmp );
638
639    if( !err && !getBlockSize( setmeInfo->pieceSize ) )
640        err = TR_EINVALID;
641
642    if( !err && tr_torrentExists( handle, setmeInfo->hash ) )
643        err = TR_EDUPLICATE;
644
645    if( doFree )
646        tr_metainfoFree( setmeInfo );
647
648    return err;
649}
650
651tr_torrent *
652tr_torrentNew( tr_handle *     handle,
653               const tr_ctor * ctor,
654               int *           setmeError )
655{
656    int          err;
657    tr_info      tmpInfo;
658    tr_torrent * tor = NULL;
659
660    err = tr_torrentParse( handle, ctor, &tmpInfo );
661    if( !err )
662    {
663        tor = tr_new0( tr_torrent, 1 );
664        tor->info = tmpInfo;
665        torrentRealInit( handle, tor, ctor );
666    }
667    else if( setmeError )
668    {
669        *setmeError = err;
670    }
671
672    return tor;
673}
674
675/**
676***
677**/
678
679void
680tr_torrentSetDownloadDir( tr_torrent * tor,
681                          const char * path )
682{
683    if( !path || !tor->downloadDir || strcmp( path, tor->downloadDir ) )
684    {
685        tr_free( tor->downloadDir );
686        tor->downloadDir = tr_strdup( path );
687        tr_torrentSaveResume( tor );
688    }
689}
690
691const char*
692tr_torrentGetDownloadDir( const tr_torrent * tor )
693{
694    return tor->downloadDir;
695}
696
697void
698tr_torrentChangeMyPort( tr_torrent * tor )
699{
700    if( tor->tracker )
701        tr_trackerChangeMyPort( tor->tracker );
702}
703
704int
705tr_torrentIsPrivate( const tr_torrent * tor )
706{
707    return tor
708           && tor->info.isPrivate;
709}
710
711int
712tr_torrentAllowsPex( const tr_torrent * tor )
713{
714    return tor
715           && tor->session->isPexEnabled
716           && !tr_torrentIsPrivate( tor );
717}
718
719static void
720tr_torrentManualUpdateImpl( void * vtor )
721{
722    tr_torrent * tor = vtor;
723
724    if( tor->isRunning )
725        tr_trackerReannounce( tor->tracker );
726}
727
728void
729tr_torrentManualUpdate( tr_torrent * tor )
730{
731    tr_runInEventThread( tor->session, tr_torrentManualUpdateImpl, tor );
732}
733
734int
735tr_torrentCanManualUpdate( const tr_torrent * tor )
736{
737    return ( tor )
738           && ( tor->isRunning )
739           && ( tr_trackerCanManualAnnounce( tor->tracker ) );
740}
741
742const tr_info *
743tr_torrentInfo( const tr_torrent * tor )
744{
745    return tor ? &tor->info : NULL;
746}
747
748static double
749tr_torrentGetRate( const tr_torrent * tor,
750                   tr_direction       direction )
751{
752    assert( tor != NULL );
753    assert( direction == TR_UP || direction == TR_DOWN );
754
755    return tr_rcRate( tor->pieceSpeed[direction] );
756}
757
758const tr_stat *
759tr_torrentStatCached( tr_torrent * tor )
760{
761    const time_t now = time( NULL );
762
763    return tor && ( now == tor->lastStatTime ) ? &tor->stats
764           : tr_torrentStat( tor );
765}
766
767tr_torrent_activity
768tr_torrentGetActivity( tr_torrent * tor )
769{
770    tr_torrentRecheckCompleteness( tor );
771
772    if( tor->verifyState == TR_VERIFY_NOW )
773        return TR_STATUS_CHECK;
774    if( tor->verifyState == TR_VERIFY_WAIT )
775        return TR_STATUS_CHECK_WAIT;
776    if( !tor->isRunning )
777        return TR_STATUS_STOPPED;
778    if( tor->completeness == TR_CP_INCOMPLETE )
779        return TR_STATUS_DOWNLOAD;
780
781    return TR_STATUS_SEED;
782}
783
784const tr_stat *
785tr_torrentStat( tr_torrent * tor )
786{
787    tr_stat *               s;
788    struct tr_tracker *     tc;
789    const tr_tracker_info * ti;
790    int                     usableSeeds = 0;
791
792    if( !tor )
793        return NULL;
794
795    tr_torrentLock( tor );
796
797    tor->lastStatTime = time( NULL );
798
799    s = &tor->stats;
800    s->id = tor->uniqueId;
801    s->activity = tr_torrentGetActivity( tor );
802    s->error  = tor->error;
803    memcpy( s->errorString, tor->errorString,
804           sizeof( s->errorString ) );
805
806    tc = tor->tracker;
807    ti = tr_trackerGetAddress( tor->tracker );
808    s->announceURL = ti ? ti->announce : NULL;
809    s->scrapeURL   = ti ? ti->scrape   : NULL;
810    tr_trackerStat( tc, s );
811    tr_trackerGetCounts( tc, &s->timesCompleted,
812                         &s->leechers,
813                         &s->seeders );
814    tr_peerMgrTorrentStats( tor->session->peerMgr,
815                            tor->info.hash,
816                            &s->peersKnown,
817                            &s->peersConnected,
818                            &usableSeeds,
819                            &s->webseedsSendingToUs,
820                            &s->peersSendingToUs,
821                            &s->peersGettingFromUs,
822                            s->peersFrom );
823
824    s->rateDownload = tr_torrentGetRate( tor, TR_PEER_TO_CLIENT );
825
826    s->rateUpload = tr_torrentGetRate( tor, TR_CLIENT_TO_PEER );
827
828    usableSeeds += tor->info.webseedCount;
829
830    s->percentComplete = tr_cpPercentComplete ( tor->completion );
831
832    s->percentDone = tr_cpPercentDone( tor->completion );
833    s->leftUntilDone = tr_cpLeftUntilDone( tor->completion );
834    s->sizeWhenDone = tr_cpSizeWhenDone( tor->completion );
835
836    s->recheckProgress = s->activity == TR_STATUS_CHECK
837                       ? 1.0 -
838                         ( tr_torrentCountUncheckedPieces( tor ) /
839                           (double) tor->info.pieceCount )
840                       : 0.0;
841
842    s->swarmSpeed = tr_rcRate( tor->swarmSpeed );
843
844    s->activityDate = tor->activityDate;
845    s->addedDate    = tor->addedDate;
846    s->doneDate     = tor->doneDate;
847    s->startDate    = tor->startDate;
848
849    s->corruptEver     = tor->corruptCur    + tor->corruptPrev;
850    s->downloadedEver  = tor->downloadedCur + tor->downloadedPrev;
851    s->uploadedEver    = tor->uploadedCur   + tor->uploadedPrev;
852    s->haveValid       = tr_cpHaveValid( tor->completion );
853    s->haveUnchecked   = tr_cpHaveTotal( tor->completion ) - s->haveValid;
854
855
856    if( usableSeeds > 0 )
857    {
858        s->desiredAvailable = s->leftUntilDone;
859    }
860    else if( !s->leftUntilDone || !s->peersConnected )
861    {
862        s->desiredAvailable = 0;
863    }
864    else
865    {
866        tr_piece_index_t i;
867        tr_bitfield *    peerPieces = tr_peerMgrGetAvailable(
868            tor->session->peerMgr,
869            tor->info.
870            hash );
871        s->desiredAvailable = 0;
872        for( i = 0; i < tor->info.pieceCount; ++i )
873            if( !tor->info.pieces[i].dnd && tr_bitfieldHas( peerPieces, i ) )
874                s->desiredAvailable += tr_cpMissingBlocksInPiece(
875                    tor->completion, i );
876        s->desiredAvailable *= tor->blockSize;
877        tr_bitfieldFree( peerPieces );
878    }
879
880    if( s->leftUntilDone > s->desiredAvailable )
881        s->eta = TR_ETA_NOT_AVAIL;
882    else if( s->rateDownload < 0.1 )
883        s->eta = TR_ETA_UNKNOWN;
884    else
885        s->eta = s->leftUntilDone / s->rateDownload / 1024.0;
886
887    s->ratio = tr_getRatio(
888        s->uploadedEver,
889        s->downloadedEver ? s->downloadedEver : s->
890        haveValid );
891
892    tr_torrentUnlock( tor );
893
894    return s;
895}
896
897/***
898****
899***/
900
901static uint64_t
902fileBytesCompleted( const tr_torrent * tor,
903                    tr_file_index_t    fileIndex )
904{
905    const tr_file *        file     =  &tor->info.files[fileIndex];
906    const tr_block_index_t firstBlock       =  file->offset /
907                                              tor->blockSize;
908    const uint64_t         firstBlockOffset =  file->offset %
909                                              tor->blockSize;
910    const uint64_t         lastOffset       =
911        file->length ? ( file->length - 1 ) : 0;
912    const tr_block_index_t lastBlock        =
913        ( file->offset + lastOffset ) / tor->blockSize;
914    const uint64_t         lastBlockOffset  =
915        ( file->offset + lastOffset ) % tor->blockSize;
916    uint64_t               haveBytes = 0;
917
918    assert( tor );
919    assert( fileIndex < tor->info.fileCount );
920    assert( file->offset + file->length <= tor->info.totalSize );
921    assert( ( firstBlock < tor->blockCount )
922          || ( !file->length && file->offset == tor->info.totalSize ) );
923    assert( ( lastBlock < tor->blockCount )
924          || ( !file->length && file->offset == tor->info.totalSize ) );
925    assert( firstBlock <= lastBlock );
926    assert( tr_torBlockPiece( tor, firstBlock ) == file->firstPiece );
927    assert( tr_torBlockPiece( tor, lastBlock ) == file->lastPiece );
928
929    if( firstBlock == lastBlock )
930    {
931        if( tr_cpBlockIsComplete( tor->completion, firstBlock ) )
932            haveBytes += lastBlockOffset + 1 - firstBlockOffset;
933    }
934    else
935    {
936        tr_block_index_t i;
937
938        if( tr_cpBlockIsComplete( tor->completion, firstBlock ) )
939            haveBytes += tor->blockSize - firstBlockOffset;
940
941        for( i = firstBlock + 1; i < lastBlock; ++i )
942            if( tr_cpBlockIsComplete( tor->completion, i ) )
943                haveBytes += tor->blockSize;
944
945        if( tr_cpBlockIsComplete( tor->completion, lastBlock ) )
946            haveBytes += lastBlockOffset + 1;
947    }
948
949    return haveBytes;
950}
951
952tr_file_stat *
953tr_torrentFiles( const tr_torrent * tor,
954                 tr_file_index_t *  fileCount )
955{
956    tr_file_index_t       i;
957    const tr_file_index_t n = tor->info.fileCount;
958    tr_file_stat *        files = tr_new0( tr_file_stat, n );
959    tr_file_stat *        walk = files;
960
961    for( i = 0; i < n; ++i, ++walk )
962    {
963        const uint64_t b = fileBytesCompleted( tor, i );
964        walk->bytesCompleted = b;
965        walk->progress = tr_getRatio( b, tor->info.files[i].length );
966    }
967
968    if( fileCount )
969        *fileCount = n;
970
971    return files;
972}
973
974void
975tr_torrentFilesFree( tr_file_stat *            files,
976                     tr_file_index_t fileCount UNUSED )
977{
978    tr_free( files );
979}
980
981/***
982****
983***/
984
985float*
986tr_torrentWebSpeeds( const tr_torrent * tor )
987{
988    return tor ? tr_peerMgrWebSpeeds( tor->session->peerMgr, tor->info.hash )
989           : NULL;
990}
991
992tr_peer_stat *
993tr_torrentPeers( const tr_torrent * tor,
994                 int *              peerCount )
995{
996    tr_peer_stat * ret = NULL;
997
998    if( tor )
999        ret = tr_peerMgrPeerStats( tor->session->peerMgr,
1000                                   tor->info.hash, peerCount );
1001
1002    return ret;
1003}
1004
1005void
1006tr_torrentPeersFree( tr_peer_stat * peers,
1007                     int peerCount  UNUSED )
1008{
1009    tr_free( peers );
1010}
1011
1012void
1013tr_torrentAvailability( const tr_torrent * tor,
1014                        int8_t *           tab,
1015                        int                size )
1016{
1017    tr_peerMgrTorrentAvailability( tor->session->peerMgr,
1018                                   tor->info.hash,
1019                                   tab, size );
1020}
1021
1022void
1023tr_torrentAmountFinished( const tr_torrent * tor,
1024                          float *            tab,
1025                          int                size )
1026{
1027    tr_torrentLock( tor );
1028    tr_cpGetAmountDone( tor->completion, tab, size );
1029    tr_torrentUnlock( tor );
1030}
1031
1032void
1033tr_torrentResetTransferStats( tr_torrent * tor )
1034{
1035    tr_torrentLock( tor );
1036
1037    tor->downloadedPrev += tor->downloadedCur;
1038    tor->downloadedCur   = 0;
1039    tor->uploadedPrev   += tor->uploadedCur;
1040    tor->uploadedCur     = 0;
1041    tor->corruptPrev    += tor->corruptCur;
1042    tor->corruptCur      = 0;
1043
1044    tr_torrentUnlock( tor );
1045}
1046
1047void
1048tr_torrentSetHasPiece( tr_torrent *     tor,
1049                       tr_piece_index_t pieceIndex,
1050                       int              has )
1051{
1052    tr_torrentLock( tor );
1053
1054    assert( tor );
1055    assert( pieceIndex < tor->info.pieceCount );
1056
1057    if( has )
1058        tr_cpPieceAdd( tor->completion, pieceIndex );
1059    else
1060        tr_cpPieceRem( tor->completion, pieceIndex );
1061
1062    tr_torrentUnlock( tor );
1063}
1064
1065/***
1066****
1067***/
1068
1069static void
1070freeTorrent( tr_torrent * tor )
1071{
1072    tr_torrent * t;
1073    tr_handle *  h = tor->session;
1074    tr_info *    inf = &tor->info;
1075
1076    assert( tor );
1077    assert( !tor->isRunning );
1078
1079    tr_globalLock( h );
1080
1081    tr_peerMgrRemoveTorrent( h->peerMgr, tor->info.hash );
1082
1083    tr_cpClose( tor->completion );
1084
1085    tr_rcClose( tor->swarmSpeed );
1086
1087    tr_trackerUnsubscribe( tor->tracker, tor->trackerSubscription );
1088    tr_trackerFree( tor->tracker );
1089    tor->tracker = NULL;
1090
1091    tr_bitfieldFree( tor->checkedPieces );
1092
1093    tr_free( tor->downloadDir );
1094
1095    if( tor == h->torrentList )
1096        h->torrentList = tor->next;
1097    else for( t = h->torrentList; t != NULL; t = t->next )
1098        {
1099            if( t->next == tor )
1100            {
1101                t->next = tor->next;
1102                break;
1103            }
1104        }
1105
1106    assert( h->torrentCount >= 1 );
1107    h->torrentCount--;
1108
1109    tr_rcClose( tor->pieceSpeed[TR_PEER_TO_CLIENT] );
1110    tr_rcClose( tor->pieceSpeed[TR_CLIENT_TO_PEER] );
1111
1112    tr_metainfoFree( inf );
1113    tr_free( tor );
1114
1115    tr_globalUnlock( h );
1116}
1117
1118/**
1119***  Start/Stop Callback
1120**/
1121
1122static void
1123checkAndStartImpl( void * vtor )
1124{
1125    tr_torrent * tor = vtor;
1126
1127    tr_globalLock( tor->session );
1128
1129    tor->isRunning  = 1;
1130    *tor->errorString = '\0';
1131    tr_torrentResetTransferStats( tor );
1132    tor->completeness = tr_cpGetStatus( tor->completion );
1133    tr_torrentSaveResume( tor );
1134    tor->startDate = time( NULL );
1135    tr_trackerStart( tor->tracker );
1136    tr_peerMgrStartTorrent( tor->session->peerMgr, tor->info.hash );
1137
1138    tr_globalUnlock( tor->session );
1139}
1140
1141static void
1142checkAndStartCB( tr_torrent * tor )
1143{
1144    tr_runInEventThread( tor->session, checkAndStartImpl, tor );
1145}
1146
1147static void
1148torrentStart( tr_torrent * tor,
1149              int          reloadProgress )
1150{
1151    tr_globalLock( tor->session );
1152
1153    if( !tor->isRunning )
1154    {
1155        const int isVerifying = tr_verifyInProgress( tor );
1156
1157        if( !isVerifying && reloadProgress )
1158            tr_torrentLoadResume( tor, TR_FR_PROGRESS, NULL );
1159
1160        tor->isRunning = 1;
1161
1162        if( !isVerifying )
1163            tr_verifyAdd( tor, checkAndStartCB );
1164    }
1165
1166    tr_globalUnlock( tor->session );
1167}
1168
1169void
1170tr_torrentStart( tr_torrent * tor )
1171{
1172    if( tor )
1173        torrentStart( tor, TRUE );
1174}
1175
1176static void
1177torrentRecheckDoneImpl( void * vtor )
1178{
1179    tr_torrentRecheckCompleteness( vtor );
1180}
1181
1182static void
1183torrentRecheckDoneCB( tr_torrent * tor )
1184{
1185    tr_runInEventThread( tor->session, torrentRecheckDoneImpl, tor );
1186}
1187
1188void
1189tr_torrentVerify( tr_torrent * tor )
1190{
1191    tr_verifyRemove( tor );
1192
1193    tr_globalLock( tor->session );
1194
1195    tr_torrentUncheck( tor );
1196    tr_verifyAdd( tor, torrentRecheckDoneCB );
1197
1198    tr_globalUnlock( tor->session );
1199}
1200
1201static void
1202stopTorrent( void * vtor )
1203{
1204    tr_file_index_t i;
1205
1206    tr_torrent *    tor = vtor;
1207
1208    tr_verifyRemove( tor );
1209    tr_peerMgrStopTorrent( tor->session->peerMgr, tor->info.hash );
1210    tr_trackerStop( tor->tracker );
1211
1212    for( i = 0; i < tor->info.fileCount; ++i )
1213    {
1214        const tr_file * file = &tor->info.files[i];
1215        char * path = tr_buildPath( tor->downloadDir, file->name, NULL );
1216        tr_fdFileClose( path );
1217        tr_free( path );
1218    }
1219}
1220
1221void
1222tr_torrentStop( tr_torrent * tor )
1223{
1224    if( tor )
1225    {
1226        tr_globalLock( tor->session );
1227
1228        tor->isRunning = 0;
1229        if( !tor->isDeleting )
1230            tr_torrentSaveResume( tor );
1231        tr_runInEventThread( tor->session, stopTorrent, tor );
1232
1233        tr_globalUnlock( tor->session );
1234    }
1235}
1236
1237static void
1238closeTorrent( void * vtor )
1239{
1240    tr_torrent * tor = vtor;
1241
1242    tr_torrentSaveResume( tor );
1243    tor->isRunning = 0;
1244    stopTorrent( tor );
1245    if( tor->isDeleting )
1246    {
1247        tr_metainfoRemoveSaved( tor->session, &tor->info );
1248        tr_torrentRemoveResume( tor );
1249    }
1250    freeTorrent( tor );
1251}
1252
1253void
1254tr_torrentFree( tr_torrent * tor )
1255{
1256    if( tor )
1257    {
1258        tr_handle * handle = tor->session;
1259        tr_globalLock( handle );
1260
1261        tr_torrentClearCompletenessCallback( tor );
1262        tr_runInEventThread( handle, closeTorrent, tor );
1263
1264        tr_globalUnlock( handle );
1265    }
1266}
1267
1268void
1269tr_torrentRemove( tr_torrent * tor )
1270{
1271    tor->isDeleting = 1;
1272    tr_torrentFree( tor );
1273}
1274
1275/**
1276***  Completeness
1277**/
1278
1279static const char *
1280getCompletionString( int type )
1281{
1282    switch( type )
1283    {
1284        /* Translators: this is a minor point that's safe to skip over, but FYI:
1285           "Complete" and "Done" are specific, different terms in Transmission:
1286           "Complete" means we've downloaded every file in the torrent.
1287           "Done" means we're done downloading the files we wanted, but NOT all
1288           that exist */
1289        case TR_CP_DONE:
1290            return _( "Done" );
1291
1292        case TR_CP_COMPLETE:
1293            return _( "Complete" );
1294
1295        default:
1296            return _( "Incomplete" );
1297    }
1298}
1299
1300static void
1301fireCompletenessChange( tr_torrent       * tor,
1302                        tr_completeness    status )
1303{
1304    assert( tor );
1305    assert( ( status == TR_CP_INCOMPLETE )
1306         || ( status == TR_CP_DONE )
1307         || ( status == TR_CP_COMPLETE ) );
1308
1309    if( tor->completeness_func )
1310        tor->completeness_func( tor, status, tor->completeness_func_user_data );
1311}
1312
1313void
1314tr_torrentSetCompletenessCallback( tr_torrent                    * tor,
1315                                   tr_torrent_completeness_func    func,
1316                                   void                          * user_data )
1317{
1318    assert( tor );
1319    tor->completeness_func = func;
1320    tor->completeness_func_user_data = user_data;
1321}
1322
1323void
1324tr_torrentClearCompletenessCallback( tr_torrent * torrent )
1325{
1326    tr_torrentSetCompletenessCallback( torrent, NULL, NULL );
1327}
1328
1329void
1330tr_torrentRecheckCompleteness( tr_torrent * tor )
1331{
1332    tr_completeness completeness;
1333
1334    tr_torrentLock( tor );
1335
1336    completeness = tr_cpGetStatus( tor->completion );
1337
1338    if( completeness != tor->completeness )
1339    {
1340        const int recentChange = tor->downloadedCur != 0;
1341
1342        if( recentChange )
1343        {
1344            tr_torinf( tor, _( "State changed from \"%1$s\" to \"%2$s\"" ),
1345                      getCompletionString( tor->completeness ),
1346                      getCompletionString( completeness ) );
1347        }
1348
1349        tor->completeness = completeness;
1350        fireCompletenessChange( tor, completeness );
1351
1352        if( recentChange && ( completeness == TR_CP_COMPLETE ) )
1353        {
1354            tr_trackerCompleted( tor->tracker );
1355
1356            tor->doneDate = time( NULL );
1357        }
1358
1359        tr_torrentSaveResume( tor );
1360    }
1361
1362    tr_torrentUnlock( tor );
1363}
1364
1365int
1366tr_torrentIsSeed( const tr_torrent * tor )
1367{
1368    return tor->completeness == TR_CP_COMPLETE || tor->completeness == TR_CP_DONE;
1369}
1370
1371/**
1372***  File priorities
1373**/
1374
1375void
1376tr_torrentInitFilePriority( tr_torrent *    tor,
1377                            tr_file_index_t fileIndex,
1378                            tr_priority_t   priority )
1379{
1380    tr_piece_index_t i;
1381    tr_file *        file;
1382
1383    assert( tor );
1384    assert( fileIndex < tor->info.fileCount );
1385    assert(
1386        priority == TR_PRI_LOW || priority == TR_PRI_NORMAL || priority ==
1387        TR_PRI_HIGH );
1388
1389    file = &tor->info.files[fileIndex];
1390    file->priority = priority;
1391    for( i = file->firstPiece; i <= file->lastPiece; ++i )
1392        tor->info.pieces[i].priority = calculatePiecePriority( tor, i,
1393                                                               fileIndex );
1394}
1395
1396void
1397tr_torrentSetFilePriorities( tr_torrent *      tor,
1398                             tr_file_index_t * files,
1399                             tr_file_index_t   fileCount,
1400                             tr_priority_t     priority )
1401{
1402    tr_file_index_t i;
1403
1404    tr_torrentLock( tor );
1405
1406    for( i = 0; i < fileCount; ++i )
1407        tr_torrentInitFilePriority( tor, files[i], priority );
1408
1409    tr_torrentSaveResume( tor );
1410    tr_torrentUnlock( tor );
1411}
1412
1413tr_priority_t
1414tr_torrentGetFilePriority( const tr_torrent * tor,
1415                           tr_file_index_t    file )
1416{
1417    tr_priority_t ret;
1418
1419    tr_torrentLock( tor );
1420    assert( tor );
1421    assert( file < tor->info.fileCount );
1422    ret = tor->info.files[file].priority;
1423    tr_torrentUnlock( tor );
1424
1425    return ret;
1426}
1427
1428tr_priority_t*
1429tr_torrentGetFilePriorities( const tr_torrent * tor )
1430{
1431    tr_file_index_t i;
1432    tr_priority_t * p;
1433
1434    tr_torrentLock( tor );
1435    p = tr_new0( tr_priority_t, tor->info.fileCount );
1436    for( i = 0; i < tor->info.fileCount; ++i )
1437        p[i] = tor->info.files[i].priority;
1438    tr_torrentUnlock( tor );
1439
1440    return p;
1441}
1442
1443/**
1444***  File DND
1445**/
1446
1447int
1448tr_torrentGetFileDL( const tr_torrent * tor,
1449                     tr_file_index_t    file )
1450{
1451    int doDownload;
1452
1453    tr_torrentLock( tor );
1454
1455    assert( file < tor->info.fileCount );
1456    doDownload = !tor->info.files[file].dnd;
1457
1458    tr_torrentUnlock( tor );
1459    return doDownload != 0;
1460}
1461
1462static void
1463setFileDND( tr_torrent *    tor,
1464            tr_file_index_t fileIndex,
1465            int             doDownload )
1466{
1467    tr_file *        file;
1468    const int        dnd = !doDownload;
1469    tr_piece_index_t firstPiece, firstPieceDND;
1470    tr_piece_index_t lastPiece, lastPieceDND;
1471    tr_file_index_t  i;
1472
1473    file = &tor->info.files[fileIndex];
1474    file->dnd = dnd;
1475    firstPiece = file->firstPiece;
1476    lastPiece = file->lastPiece;
1477
1478    /* can't set the first piece to DND unless
1479       every file using that piece is DND */
1480    firstPieceDND = dnd;
1481    if( fileIndex > 0 )
1482    {
1483        for( i = fileIndex - 1; firstPieceDND; --i )
1484        {
1485            if( tor->info.files[i].lastPiece != firstPiece )
1486                break;
1487            firstPieceDND = tor->info.files[i].dnd;
1488            if( !i )
1489                break;
1490        }
1491    }
1492
1493    /* can't set the last piece to DND unless
1494       every file using that piece is DND */
1495    lastPieceDND = dnd;
1496    for( i = fileIndex + 1; lastPieceDND && i < tor->info.fileCount; ++i )
1497    {
1498        if( tor->info.files[i].firstPiece != lastPiece )
1499            break;
1500        lastPieceDND = tor->info.files[i].dnd;
1501    }
1502
1503    if( firstPiece == lastPiece )
1504    {
1505        tor->info.pieces[firstPiece].dnd = firstPieceDND && lastPieceDND;
1506    }
1507    else
1508    {
1509        tr_piece_index_t pp;
1510        tor->info.pieces[firstPiece].dnd = firstPieceDND;
1511        tor->info.pieces[lastPiece].dnd = lastPieceDND;
1512        for( pp = firstPiece + 1; pp < lastPiece; ++pp )
1513            tor->info.pieces[pp].dnd = dnd;
1514    }
1515}
1516
1517void
1518tr_torrentInitFileDLs( tr_torrent *      tor,
1519                       tr_file_index_t * files,
1520                       tr_file_index_t   fileCount,
1521                       int               doDownload )
1522{
1523    tr_file_index_t i;
1524
1525    tr_torrentLock( tor );
1526
1527    for( i = 0; i < fileCount; ++i )
1528        setFileDND( tor, files[i], doDownload );
1529    tr_cpInvalidateDND ( tor->completion );
1530
1531    tr_torrentUnlock( tor );
1532}
1533
1534void
1535tr_torrentSetFileDLs( tr_torrent *      tor,
1536                      tr_file_index_t * files,
1537                      tr_file_index_t   fileCount,
1538                      int               doDownload )
1539{
1540    tr_torrentLock( tor );
1541    tr_torrentInitFileDLs( tor, files, fileCount, doDownload );
1542    tr_torrentSaveResume( tor );
1543    tr_torrentUnlock( tor );
1544}
1545
1546/***
1547****
1548***/
1549
1550void
1551tr_torrentSetPeerLimit( tr_torrent * tor,
1552                        uint16_t     maxConnectedPeers )
1553{
1554    tor->maxConnectedPeers = maxConnectedPeers;
1555}
1556
1557uint16_t
1558tr_torrentGetPeerLimit( const tr_torrent * tor )
1559{
1560    return tor->maxConnectedPeers;
1561}
1562
1563/***
1564****
1565***/
1566
1567tr_block_index_t
1568_tr_block( const tr_torrent * tor,
1569           tr_piece_index_t   index,
1570           uint32_t           offset )
1571{
1572    const tr_info *  inf = &tor->info;
1573    tr_block_index_t ret;
1574
1575    ret = index;
1576    ret *= ( inf->pieceSize / tor->blockSize );
1577    ret += offset / tor->blockSize;
1578    return ret;
1579}
1580
1581int
1582tr_torrentReqIsValid( const tr_torrent * tor,
1583                      tr_piece_index_t   index,
1584                      uint32_t           offset,
1585                      uint32_t           length )
1586{
1587    int err = 0;
1588
1589    if( index >= tor->info.pieceCount )
1590        err = 1;
1591    else if( length < 1 )
1592        err = 2;
1593    else if( ( offset + length ) > tr_torPieceCountBytes( tor, index ) )
1594        err = 3;
1595    else if( length > MAX_BLOCK_SIZE )
1596        err = 4;
1597    else if( tr_pieceOffset( tor, index, offset, length ) > tor->info.totalSize )
1598        err = 5;
1599
1600    if( err ) fprintf( stderr, "index %lu offset %lu length %lu err %d\n",
1601                       (unsigned long)index, (unsigned long)offset,
1602                       (unsigned long)length,
1603                       err );
1604
1605    return !err;
1606}
1607
1608uint64_t
1609tr_pieceOffset( const tr_torrent * tor,
1610                tr_piece_index_t   index,
1611                uint32_t           offset,
1612                uint32_t           length )
1613{
1614    uint64_t ret;
1615
1616    ret = tor->info.pieceSize;
1617    ret *= index;
1618    ret += offset;
1619    ret += length;
1620    return ret;
1621}
1622
1623/***
1624****
1625***/
1626
1627int
1628tr_torrentIsPieceChecked( const tr_torrent * tor,
1629                          tr_piece_index_t   piece )
1630{
1631    return tr_bitfieldHas( tor->checkedPieces, piece );
1632}
1633
1634void
1635tr_torrentSetPieceChecked( tr_torrent *     tor,
1636                           tr_piece_index_t piece,
1637                           int              isChecked )
1638{
1639    if( isChecked )
1640        tr_bitfieldAdd( tor->checkedPieces, piece );
1641    else
1642        tr_bitfieldRem( tor->checkedPieces, piece );
1643}
1644
1645void
1646tr_torrentSetFileChecked( tr_torrent *    tor,
1647                          tr_file_index_t fileIndex,
1648                          int             isChecked )
1649{
1650    const tr_file *        file = &tor->info.files[fileIndex];
1651    const tr_piece_index_t begin = file->firstPiece;
1652    const tr_piece_index_t end = file->lastPiece + 1;
1653
1654    if( isChecked )
1655        tr_bitfieldAddRange ( tor->checkedPieces, begin, end );
1656    else
1657        tr_bitfieldRemRange ( tor->checkedPieces, begin, end );
1658}
1659
1660int
1661tr_torrentIsFileChecked( const tr_torrent * tor,
1662                         tr_file_index_t    fileIndex )
1663{
1664    const tr_file *        file = &tor->info.files[fileIndex];
1665    const tr_piece_index_t begin = file->firstPiece;
1666    const tr_piece_index_t end = file->lastPiece + 1;
1667    tr_piece_index_t       i;
1668    int                    isChecked = TRUE;
1669
1670    for( i = begin; isChecked && i < end; ++i )
1671        if( !tr_torrentIsPieceChecked( tor, i ) )
1672            isChecked = FALSE;
1673
1674    return isChecked;
1675}
1676
1677void
1678tr_torrentUncheck( tr_torrent * tor )
1679{
1680    tr_bitfieldRemRange ( tor->checkedPieces, 0, tor->info.pieceCount );
1681}
1682
1683int
1684tr_torrentCountUncheckedPieces( const tr_torrent * tor )
1685{
1686    return tor->info.pieceCount - tr_bitfieldCountTrueBits(
1687               tor->checkedPieces );
1688}
1689
1690time_t*
1691tr_torrentGetMTimes( const tr_torrent * tor,
1692                     size_t *           setme_n )
1693{
1694    size_t       i;
1695    const size_t n = tor->info.fileCount;
1696    time_t *     m = tr_new0( time_t, n );
1697
1698    for( i = 0; i < n; ++i )
1699    {
1700        struct stat sb;
1701        char * path = tr_buildPath( tor->downloadDir, tor->info.files[i].name, NULL );
1702        if( !stat( path, &sb ) )
1703        {
1704#ifdef SYS_DARWIN
1705            m[i] = sb.st_mtimespec.tv_sec;
1706#else
1707            m[i] = sb.st_mtime;
1708#endif
1709        }
1710        tr_free( path );
1711    }
1712
1713    *setme_n = n;
1714    return m;
1715}
1716
1717/***
1718****
1719***/
1720
1721void
1722tr_torrentSetAnnounceList( tr_torrent *            tor,
1723                           const tr_tracker_info * trackers,
1724                           int                     trackerCount )
1725{
1726    tr_benc metainfo;
1727
1728    /* save to the .torrent file */
1729    if( !tr_bencLoadFile( tor->info.torrent, &metainfo ) )
1730    {
1731        int       i;
1732        int       prevTier = -1;
1733        tr_benc * tier = NULL;
1734        tr_benc * announceList;
1735        tr_info   tmpInfo;
1736
1737        /* remove the old fields */
1738        tr_bencDictRemove( &metainfo, "announce" );
1739        tr_bencDictRemove( &metainfo, "announce-list" );
1740
1741        /* add the new fields */
1742        tr_bencDictAddStr( &metainfo, "announce", trackers[0].announce );
1743        announceList = tr_bencDictAddList( &metainfo, "announce-list", 0 );
1744        for( i = 0; i < trackerCount; ++i )
1745        {
1746            if( prevTier != trackers[i].tier )
1747            {
1748                prevTier = trackers[i].tier;
1749                tier = tr_bencListAddList( announceList, 0 );
1750            }
1751            tr_bencListAddStr( tier, trackers[i].announce );
1752        }
1753
1754        /* try to parse it back again, to make sure it's good */
1755        memset( &tmpInfo, 0, sizeof( tr_info ) );
1756        if( !tr_metainfoParse( tor->session, &tmpInfo, &metainfo ) )
1757        {
1758            /* if it's good, save it and use it */
1759            tr_metainfoFree( &tor->info );
1760            tor->info = tmpInfo;
1761            tr_torrentInitFilePieces( tor );
1762            tr_bencSaveFile( tor->info.torrent, &metainfo );
1763        }
1764
1765        /* cleanup */
1766        tr_bencFree( &metainfo );
1767    }
1768}
1769
1770/**
1771***
1772**/
1773
1774/** @deprecated this method will be removed in 1.40 */
1775void
1776tr_torrentSetAddedDate( tr_torrent * tor,
1777                        time_t       t )
1778{
1779    tor->addedDate = t;
1780}
1781
1782/** @deprecated this method will be removed in 1.40 */
1783void
1784tr_torrentSetActivityDate( tr_torrent * tor,
1785                           time_t       t )
1786{
1787    tor->activityDate = t;
1788}
1789
1790/** @deprecated this method will be removed in 1.40 */
1791void
1792tr_torrentSetDoneDate( tr_torrent * tor,
1793                       time_t       t )
1794{
1795    tor->doneDate = t;
1796}
1797
1798/**
1799***
1800**/
1801
1802uint64_t
1803tr_torrentGetBytesLeftToAllocate( const tr_torrent * tor )
1804{
1805    const tr_file * it;
1806    const tr_file * end;
1807    struct stat sb;
1808    uint64_t bytesLeft = 0;
1809
1810    for( it=tor->info.files, end=it+tor->info.fileCount; it!=end; ++it )
1811    {
1812        if( !it->dnd )
1813        {
1814            char * path = tr_buildPath( tor->downloadDir, it->name, NULL );
1815
1816            bytesLeft += it->length;
1817
1818            if( !stat( path, &sb )
1819                    && S_ISREG( sb.st_mode )
1820                    && ( (uint64_t)sb.st_size <= it->length ) )
1821                bytesLeft -= sb.st_size;
1822
1823            tr_free( path );
1824        }
1825    }
1826
1827    return bytesLeft;
1828}
Note: See TracBrowser for help on using the repository browser.