source: trunk/libtransmission/torrent.c @ 9340

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

(trunk libT) #2520: tor->isRunning is not always saved to .resume file

  • Property svn:keywords set to Date Rev Author Id
File size: 68.6 KB
Line 
1/*
2 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: torrent.c 9340 2009-10-21 02:45:49Z charles $
11 */
12
13#include <sys/types.h> /* stat */
14#include <sys/stat.h> /* stat */
15#include <unistd.h> /* stat */
16#include <dirent.h>
17
18#include <assert.h>
19#include <limits.h> /* INT_MAX */
20#include <math.h> /* fabs */
21#include <stdarg.h>
22#include <string.h> /* memcmp */
23#include <stdlib.h> /* qsort */
24
25#include <event.h> /* evbuffer */
26
27#include "transmission.h"
28#include "announcer.h"
29#include "bandwidth.h"
30#include "bencode.h"
31#include "completion.h"
32#include "crypto.h" /* for tr_sha1 */
33#include "resume.h"
34#include "fdlimit.h" /* tr_fdTorrentClose */
35#include "metainfo.h"
36#include "peer-mgr.h"
37#include "platform.h" /* TR_PATH_DELIMITER_STR */
38#include "ptrarray.h"
39#include "ratecontrol.h"
40#include "session.h"
41#include "torrent.h"
42#include "trevent.h"
43#include "utils.h"
44#include "verify.h"
45
46#define MAX_BLOCK_SIZE ( 1024 * 16 )
47
48/***
49****
50***/
51
52int
53tr_torrentId( const tr_torrent * tor )
54{
55    return tor->uniqueId;
56}
57
58tr_torrent*
59tr_torrentFindFromId( tr_session * session, int id )
60{
61    tr_torrent * tor = NULL;
62
63    while(( tor = tr_torrentNext( session, tor )))
64        if( tor->uniqueId == id )
65            return tor;
66
67    return NULL;
68}
69
70tr_torrent*
71tr_torrentFindFromHashString( tr_session *  session, const char * str )
72{
73    tr_torrent * tor = NULL;
74
75    while(( tor = tr_torrentNext( session, tor )))
76        if( !strcmp( str, tor->info.hashString ) )
77            return tor;
78
79    return NULL;
80}
81
82tr_torrent*
83tr_torrentFindFromHash( tr_session * session, const uint8_t * torrentHash )
84{
85    tr_torrent * tor = NULL;
86
87    while(( tor = tr_torrentNext( session, tor )))
88        if( *tor->info.hash == *torrentHash )
89            if( !memcmp( tor->info.hash, torrentHash, SHA_DIGEST_LENGTH ) )
90                return tor;
91
92    return NULL;
93}
94
95tr_torrent*
96tr_torrentFindFromObfuscatedHash( tr_session * session,
97                                  const uint8_t * obfuscatedTorrentHash )
98{
99    tr_torrent * tor = NULL;
100
101    while(( tor = tr_torrentNext( session, tor )))
102        if( !memcmp( tor->obfuscatedHash, obfuscatedTorrentHash,
103                     SHA_DIGEST_LENGTH ) )
104            return tor;
105
106    return NULL;
107}
108
109/***
110****  PER-TORRENT UL / DL SPEEDS
111***/
112
113void
114tr_torrentSetSpeedLimit( tr_torrent * tor, tr_direction dir, int KiB_sec )
115{
116    assert( tr_isTorrent( tor ) );
117    assert( tr_isDirection( dir ) );
118
119    if( tr_bandwidthSetDesiredSpeed( tor->bandwidth, dir, KiB_sec ) )
120        tr_torrentSetDirty( tor );
121}
122
123int
124tr_torrentGetSpeedLimit( const tr_torrent * tor, tr_direction dir )
125{
126    assert( tr_isTorrent( tor ) );
127    assert( tr_isDirection( dir ) );
128
129    return tr_bandwidthGetDesiredSpeed( tor->bandwidth, dir );
130}
131
132void
133tr_torrentUseSpeedLimit( tr_torrent * tor, tr_direction dir, tr_bool do_use )
134{
135    assert( tr_isTorrent( tor ) );
136    assert( tr_isDirection( dir ) );
137
138    if( tr_bandwidthSetLimited( tor->bandwidth, dir, do_use ) )
139        tr_torrentSetDirty( tor );
140}
141
142tr_bool
143tr_torrentUsesSpeedLimit( const tr_torrent * tor, tr_direction dir )
144{
145    assert( tr_isTorrent( tor ) );
146    assert( tr_isDirection( dir ) );
147
148    return tr_bandwidthIsLimited( tor->bandwidth, dir );
149}
150
151void
152tr_torrentUseSessionLimits( tr_torrent * tor, tr_bool doUse )
153{
154    tr_bool changed;
155
156    assert( tr_isTorrent( tor ) );
157
158    changed = tr_bandwidthHonorParentLimits( tor->bandwidth, TR_UP, doUse );
159    changed |= tr_bandwidthHonorParentLimits( tor->bandwidth, TR_DOWN, doUse );
160
161    if( changed )
162        tr_torrentSetDirty( tor );
163}
164
165tr_bool
166tr_torrentUsesSessionLimits( const tr_torrent * tor )
167{
168    assert( tr_isTorrent( tor ) );
169
170    return tr_bandwidthAreParentLimitsHonored( tor->bandwidth, TR_UP );
171}
172
173/***
174****
175***/
176
177void
178tr_torrentSetRatioMode( tr_torrent *  tor, tr_ratiolimit mode )
179{
180    assert( tr_isTorrent( tor ) );
181    assert( mode==TR_RATIOLIMIT_GLOBAL || mode==TR_RATIOLIMIT_SINGLE || mode==TR_RATIOLIMIT_UNLIMITED  );
182
183    if( mode != tor->ratioLimitMode )
184    {
185        tor->ratioLimitMode = mode;
186        tor->needsSeedRatioCheck = TRUE;
187
188        tr_torrentSetDirty( tor );
189    }
190}
191
192tr_ratiolimit
193tr_torrentGetRatioMode( const tr_torrent * tor )
194{
195    assert( tr_isTorrent( tor ) );
196
197    return tor->ratioLimitMode;
198}
199
200void
201tr_torrentSetRatioLimit( tr_torrent * tor, double desiredRatio )
202{
203    assert( tr_isTorrent( tor ) );
204
205    if( (int)(desiredRatio*100.0) != (int)(tor->desiredRatio*100.0) )
206    {
207        tor->desiredRatio = desiredRatio;
208
209        tor->needsSeedRatioCheck = TRUE;
210
211        tr_torrentSetDirty( tor );
212    }
213}
214
215double
216tr_torrentGetRatioLimit( const tr_torrent * tor )
217{
218    assert( tr_isTorrent( tor ) );
219
220    return tor->desiredRatio;
221}
222
223tr_bool
224tr_torrentIsPieceTransferAllowed( const tr_torrent  * tor,
225                                  tr_direction        direction )
226{
227    int limit;
228    tr_bool allowed = TRUE;
229
230    if( tr_torrentUsesSpeedLimit( tor, direction ) )
231        if( tr_torrentGetSpeedLimit( tor, direction ) <= 0 )
232            allowed = FALSE;
233
234    if( tr_torrentUsesSessionLimits( tor ) )
235        if( tr_sessionGetActiveSpeedLimit( tor->session, direction, &limit ) )
236            if( limit <= 0 )
237                allowed = FALSE;
238
239    return allowed;
240}
241
242tr_bool
243tr_torrentGetSeedRatio( const tr_torrent * tor, double * ratio )
244{
245    tr_bool isLimited;
246
247    switch( tr_torrentGetRatioMode( tor ) )
248    {
249        case TR_RATIOLIMIT_SINGLE:
250            isLimited = TRUE;
251            if( ratio )
252                *ratio = tr_torrentGetRatioLimit( tor );
253            break;
254
255        case TR_RATIOLIMIT_GLOBAL:
256            isLimited = tr_sessionIsRatioLimited( tor->session );
257            if( isLimited && ratio )
258                *ratio = tr_sessionGetRatioLimit( tor->session );
259            break;
260
261        default: /* TR_RATIOLIMIT_UNLIMITED */
262            isLimited = FALSE;
263            break;
264    }
265
266    return isLimited;
267}
268
269/***
270****
271***/
272
273void
274tr_torrentSetLocalError( tr_torrent * tor, const char * fmt, ... )
275{
276    va_list ap;
277
278    assert( tr_isTorrent( tor ) );
279
280    va_start( ap, fmt );
281    tor->error = TR_STAT_LOCAL_ERROR;
282    evutil_vsnprintf( tor->errorString, sizeof( tor->errorString ), fmt, ap );
283    va_end( ap );
284}
285
286static void
287onTrackerResponse( void * tracker UNUSED,
288                   void * vevent,
289                   void * user_data )
290{
291    tr_torrent *       tor = user_data;
292    tr_tracker_event * event = vevent;
293
294    switch( event->messageType )
295    {
296        case TR_TRACKER_PEERS:
297        {
298            size_t   i, n;
299            tr_pex * pex = tr_peerMgrArrayToPex( event->compact,
300                                                 event->compactLen, &n );
301             if( event->allAreSeeds )
302                tr_tordbg( tor, "Got %d seeds from tracker", (int)n );
303            else
304                tr_torinf( tor, _( "Got %d peers from tracker" ), (int)n );
305
306            for( i = 0; i < n; ++i )
307            {
308                if( event->allAreSeeds )
309                    pex[i].flags |= ADDED_F_SEED_FLAG;
310                tr_peerMgrAddPex( tor, TR_PEER_FROM_TRACKER, pex + i );
311            }
312
313            tr_free( pex );
314            break;
315        }
316
317        case TR_TRACKER_WARNING:
318            tr_torerr( tor, _( "Tracker warning: \"%s\"" ), event->text );
319            tor->error = TR_STAT_TRACKER_WARNING;
320            tr_strlcpy( tor->errorString, event->text, sizeof( tor->errorString ) );
321            break;
322
323        case TR_TRACKER_ERROR:
324            tr_torerr( tor, _( "Tracker error: \"%s\"" ), event->text );
325            tor->error = TR_STAT_TRACKER_ERROR;
326            tr_strlcpy( tor->errorString, event->text, sizeof( tor->errorString ) );
327            break;
328
329        case TR_TRACKER_ERROR_CLEAR:
330            tor->error = TR_STAT_OK;
331            tor->errorString[0] = '\0';
332            break;
333    }
334}
335
336/***
337****
338****  TORRENT INSTANTIATION
339****
340***/
341
342static int
343getBytePiece( const tr_info * info,
344              uint64_t        byteOffset )
345{
346    assert( info );
347    assert( info->pieceSize != 0 );
348
349    return byteOffset / info->pieceSize;
350}
351
352static void
353initFilePieces( tr_info *       info,
354                tr_file_index_t fileIndex )
355{
356    tr_file * file;
357    uint64_t  firstByte, lastByte;
358
359    assert( info );
360    assert( fileIndex < info->fileCount );
361
362    file = &info->files[fileIndex];
363    firstByte = file->offset;
364    lastByte = firstByte + ( file->length ? file->length - 1 : 0 );
365    file->firstPiece = getBytePiece( info, firstByte );
366    file->lastPiece = getBytePiece( info, lastByte );
367}
368
369static int
370pieceHasFile( tr_piece_index_t piece,
371              const tr_file *  file )
372{
373    return ( file->firstPiece <= piece ) && ( piece <= file->lastPiece );
374}
375
376static tr_priority_t
377calculatePiecePriority( const tr_torrent * tor,
378                        tr_piece_index_t   piece,
379                        int                fileHint )
380{
381    tr_file_index_t i;
382    int             priority = TR_PRI_LOW;
383
384    /* find the first file that has data in this piece */
385    if( fileHint >= 0 ) {
386        i = fileHint;
387        while( i > 0 && pieceHasFile( piece, &tor->info.files[i - 1] ) )
388            --i;
389    } else {
390        for( i = 0; i < tor->info.fileCount; ++i )
391            if( pieceHasFile( piece, &tor->info.files[i] ) )
392                break;
393    }
394
395    /* the piece's priority is the max of the priorities
396     * of all the files in that piece */
397    for( ; i < tor->info.fileCount; ++i )
398    {
399        const tr_file * file = &tor->info.files[i];
400
401        if( !pieceHasFile( piece, file ) )
402            break;
403
404        priority = MAX( priority, file->priority );
405
406        /* when dealing with multimedia files, getting the first and
407           last pieces can sometimes allow you to preview it a bit
408           before it's fully downloaded... */
409        if( file->priority >= TR_PRI_NORMAL )
410            if( file->firstPiece == piece || file->lastPiece == piece )
411                priority = TR_PRI_HIGH;
412    }
413
414    return priority;
415}
416
417static void
418tr_torrentInitFilePieces( tr_torrent * tor )
419{
420    tr_file_index_t  f;
421    tr_piece_index_t p;
422    uint64_t offset = 0;
423    tr_info * inf = &tor->info;
424    int * firstFiles;
425
426    /* assign the file offsets */
427    for( f=0; f<inf->fileCount; ++f ) {
428        inf->files[f].offset = offset;
429        offset += inf->files[f].length;
430        initFilePieces( inf, f );
431    }
432
433    /* build the array of first-file hints to give calculatePiecePriority */
434    firstFiles = tr_new( int, inf->pieceCount );
435    for( p=f=0; p<inf->pieceCount; ++p ) {
436        while( inf->files[f].lastPiece < p )
437            ++f;
438        firstFiles[p] = f;
439    }
440
441#if 0
442    /* test to confirm the first-file hints are correct */
443    for( p=0; p<inf->pieceCount; ++p ) {
444        f = firstFiles[p];
445        assert( inf->files[f].firstPiece <= p );
446        assert( inf->files[f].lastPiece >= p );
447        if( f > 0 )
448            assert( inf->files[f-1].lastPiece < p );
449        for( f=0; f<inf->fileCount; ++f )
450            if( pieceHasFile( p, &inf->files[f] ) )
451                break;
452        assert( (int)f == firstFiles[p] );
453    }
454#endif
455
456    for( p=0; p<inf->pieceCount; ++p )
457        inf->pieces[p].priority = calculatePiecePriority( tor, p, firstFiles[p] );
458
459    tr_free( firstFiles );
460}
461
462int
463tr_torrentPromoteTracker( tr_torrent * tor,
464                          int          pos )
465{
466    int i;
467    int tier;
468
469    assert( tor );
470    assert( ( 0 <= pos ) && ( pos < tor->info.trackerCount ) );
471
472    /* the tier of the tracker we're promoting */
473    tier = tor->info.trackers[pos].tier;
474
475    /* find the index of that tier's first tracker */
476    for( i = 0; i < tor->info.trackerCount; ++i )
477        if( tor->info.trackers[i].tier == tier )
478            break;
479
480    assert( i < tor->info.trackerCount );
481
482    /* promote the tracker at `pos' to the front of the tier */
483    if( i != pos )
484    {
485        const tr_tracker_info tmp = tor->info.trackers[i];
486        tor->info.trackers[i] = tor->info.trackers[pos];
487        tor->info.trackers[pos] = tmp;
488    }
489
490    /* return the new position of the tracker that started out at [pos] */
491    return i;
492}
493
494struct RandomTracker
495{
496    tr_tracker_info    tracker;
497    int                random_value;
498};
499
500/* the tiers will be sorted from lowest to highest,
501 * and trackers are randomized within the tiers */
502static TR_INLINE int
503compareRandomTracker( const void * va,
504                      const void * vb )
505{
506    const struct RandomTracker * a = va;
507    const struct RandomTracker * b = vb;
508
509    if( a->tracker.tier != b->tracker.tier )
510        return a->tracker.tier - b->tracker.tier;
511
512    return a->random_value - b->random_value;
513}
514
515static void
516randomizeTiers( tr_info * info )
517{
518    int                    i;
519    const int              n = info->trackerCount;
520    struct RandomTracker * r = tr_new0( struct RandomTracker, n );
521
522    for( i = 0; i < n; ++i )
523    {
524        r[i].tracker = info->trackers[i];
525        r[i].random_value = tr_cryptoRandInt( INT_MAX );
526    }
527    qsort( r, n, sizeof( struct RandomTracker ), compareRandomTracker );
528    for( i = 0; i < n; ++i )
529        info->trackers[i] = r[i].tracker;
530    tr_free( r );
531}
532
533static void torrentStart( tr_torrent * tor );
534
535/**
536 * Decide on a block size.  constraints:
537 * (1) most clients decline requests over 16 KiB
538 * (2) pieceSize must be a multiple of block size
539 */
540static uint32_t
541getBlockSize( uint32_t pieceSize )
542{
543    uint32_t b = pieceSize;
544
545    while( b > MAX_BLOCK_SIZE )
546        b /= 2u;
547
548    if( !b || ( pieceSize % b ) ) /* not cleanly divisible */
549        return 0;
550    return b;
551}
552
553static void refreshCurrentDir( tr_torrent * tor );;
554
555static void
556torrentRealInit( tr_torrent * tor, const tr_ctor * ctor )
557{
558    int          doStart;
559    uint64_t     loaded;
560    uint64_t     t;
561    const char * dir;
562    static int   nextUniqueId = 1;
563    tr_info    * info = &tor->info;
564    tr_session * session = tr_ctorGetSession( ctor );
565
566    assert( session != NULL );
567
568    tr_globalLock( session );
569
570    tor->session   = session;
571    tor->uniqueId = nextUniqueId++;
572    tor->magicNumber = TORRENT_MAGIC_NUMBER;
573
574    randomizeTiers( info );
575
576    tor->bandwidth = tr_bandwidthNew( session, session->bandwidth );
577
578    tor->blockSize = getBlockSize( info->pieceSize );
579
580    if( !tr_ctorGetDownloadDir( ctor, TR_FORCE, &dir ) ||
581        !tr_ctorGetDownloadDir( ctor, TR_FALLBACK, &dir ) )
582            tor->downloadDir = tr_strdup( dir );
583
584    if( tr_sessionIsIncompleteDirEnabled( session ) )
585        tor->incompleteDir = tr_strdup( tr_sessionGetIncompleteDir( session ) );
586
587    tor->lastPieceSize = info->totalSize % info->pieceSize;
588
589    if( !tor->lastPieceSize )
590        tor->lastPieceSize = info->pieceSize;
591
592    tor->lastBlockSize = info->totalSize % tor->blockSize;
593
594    if( !tor->lastBlockSize )
595        tor->lastBlockSize = tor->blockSize;
596
597    tor->blockCount =
598        ( info->totalSize + tor->blockSize - 1 ) / tor->blockSize;
599
600    tor->blockCountInPiece =
601        info->pieceSize / tor->blockSize;
602
603    tor->blockCountInLastPiece =
604        ( tor->lastPieceSize + tor->blockSize - 1 ) / tor->blockSize;
605
606    /* check our work */
607    assert( ( info->pieceSize % tor->blockSize ) == 0 );
608    t = info->pieceCount - 1;
609    t *= info->pieceSize;
610    t += tor->lastPieceSize;
611    assert( t == info->totalSize );
612    t = tor->blockCount - 1;
613    t *= tor->blockSize;
614    t += tor->lastBlockSize;
615    assert( t == info->totalSize );
616    t = info->pieceCount - 1;
617    t *= tor->blockCountInPiece;
618    t += tor->blockCountInLastPiece;
619    assert( t == (uint64_t)tor->blockCount );
620
621    tr_cpConstruct( &tor->completion, tor );
622
623    tr_torrentInitFilePieces( tor );
624
625    tr_rcConstruct( &tor->swarmSpeed );
626
627    tr_sha1( tor->obfuscatedHash, "req2", 4,
628             info->hash, SHA_DIGEST_LENGTH,
629             NULL );
630
631    tr_peerMgrAddTorrent( session->peerMgr, tor );
632
633    assert( !tor->downloadedCur );
634    assert( !tor->uploadedCur );
635
636    tr_ctorInitTorrentPriorities( ctor, tor );
637
638    tr_ctorInitTorrentWanted( ctor, tor );
639
640    tor->error = TR_STAT_OK;
641
642    tr_bitfieldConstruct( &tor->checkedPieces, tor->info.pieceCount );
643    tr_torrentUncheck( tor );
644
645    tr_torrentSetAddedDate( tor, time( NULL ) ); /* this is a default value to be
646                                                    overwritten by the resume file */
647
648    loaded = tr_torrentLoadResume( tor, ~0, ctor );
649
650    refreshCurrentDir( tor );
651
652    doStart = tor->isRunning;
653    tor->isRunning = 0;
654
655    if( !( loaded & TR_FR_SPEEDLIMIT ) )
656    {
657        tr_torrentUseSpeedLimit( tor, TR_UP, FALSE );
658        tr_torrentSetSpeedLimit( tor, TR_UP, tr_sessionGetSpeedLimit( tor->session, TR_UP ) );
659        tr_torrentUseSpeedLimit( tor, TR_DOWN, FALSE );
660        tr_torrentSetSpeedLimit( tor, TR_DOWN, tr_sessionGetSpeedLimit( tor->session, TR_DOWN ) );
661        tr_torrentUseSessionLimits( tor, TRUE );
662    }
663
664    if( !( loaded & TR_FR_RATIOLIMIT ) )
665    {
666        tr_torrentSetRatioMode( tor, TR_RATIOLIMIT_GLOBAL );
667        tr_torrentSetRatioLimit( tor, tr_sessionGetRatioLimit( tor->session ) );
668    }
669
670    tor->completeness = tr_cpGetStatus( &tor->completion );
671
672    {
673        tr_torrent * it = NULL;
674        tr_torrent * last = NULL;
675        while( ( it = tr_torrentNext( session, it ) ) )
676            last = it;
677
678        if( !last )
679            session->torrentList = tor;
680        else
681            last->next = tor;
682        ++session->torrentCount;
683    }
684
685    tr_globalUnlock( session );
686
687    /* maybe save our own copy of the metainfo */
688    if( tr_ctorGetSave( ctor ) )
689    {
690        const tr_benc * val;
691        if( !tr_ctorGetMetainfo( ctor, &val ) )
692        {
693            const char * filename = tor->info.torrent;
694            tr_bencToFile( val, TR_FMT_BENC, filename );
695            tr_sessionSetTorrentFile( tor->session, tor->info.hashString, filename );
696        }
697    }
698
699    tor->tiers = tr_announcerAddTorrent( session->announcer, tor );
700    tor->tiersSubscription = tr_announcerSubscribe( tor->tiers, onTrackerResponse, tor );
701
702    tr_metainfoMigrate( session, &tor->info );
703
704    if( doStart )
705        torrentStart( tor );
706}
707
708tr_parse_result
709tr_torrentParse( const tr_ctor * ctor, tr_info * setmeInfo )
710{
711    int             doFree;
712    tr_bool         didParse;
713    tr_info         tmp;
714    const tr_benc * metainfo;
715    tr_session    * session = tr_ctorGetSession( ctor );
716    tr_parse_result result = TR_PARSE_OK;
717
718    if( setmeInfo == NULL )
719        setmeInfo = &tmp;
720    memset( setmeInfo, 0, sizeof( tr_info ) );
721
722    if( tr_ctorGetMetainfo( ctor, &metainfo ) )
723        return TR_PARSE_ERR;
724
725    didParse = tr_metainfoParse( session, setmeInfo, metainfo );
726    doFree = didParse && ( setmeInfo == &tmp );
727
728    if( !didParse )
729        result = TR_PARSE_ERR;
730
731    if( didParse && !getBlockSize( setmeInfo->pieceSize ) )
732        result = TR_PARSE_ERR;
733
734    if( didParse && session && tr_torrentExists( session, setmeInfo->hash ) )
735        result = TR_PARSE_DUPLICATE;
736
737    if( doFree )
738        tr_metainfoFree( setmeInfo );
739
740    return result;
741}
742
743tr_torrent *
744tr_torrentNew( const tr_ctor  * ctor,
745               int            * setmeError )
746{
747    int          err;
748    tr_info      tmpInfo;
749    tr_torrent * tor = NULL;
750
751    assert( ctor != NULL );
752    assert( tr_isSession( tr_ctorGetSession( ctor ) ) );
753
754    err = tr_torrentParse( ctor, &tmpInfo );
755    if( !err )
756    {
757        tor = tr_new0( tr_torrent, 1 );
758        tor->info = tmpInfo;
759        torrentRealInit( tor, ctor );
760    }
761    else if( setmeError )
762    {
763        *setmeError = err;
764    }
765
766    return tor;
767}
768
769/**
770***
771**/
772
773void
774tr_torrentSetDownloadDir( tr_torrent * tor, const char * path )
775{
776    assert( tr_isTorrent( tor  ) );
777
778    if( !path || !tor->downloadDir || strcmp( path, tor->downloadDir ) )
779    {
780        tr_free( tor->downloadDir );
781        tor->downloadDir = tr_strdup( path );
782        tr_torrentSetDirty( tor );
783    }
784
785    refreshCurrentDir( tor );
786}
787
788const char*
789tr_torrentGetDownloadDir( const tr_torrent * tor )
790{
791    assert( tr_isTorrent( tor  ) );
792
793    return tor->downloadDir;
794}
795
796void
797tr_torrentChangeMyPort( tr_torrent * tor )
798{
799    assert( tr_isTorrent( tor  ) );
800
801    tr_announcerChangeMyPort( tor );
802}
803
804static TR_INLINE void
805tr_torrentManualUpdateImpl( void * vtor )
806{
807    tr_torrent * tor = vtor;
808
809    assert( tr_isTorrent( tor  ) );
810
811    if( tor->isRunning )
812        tr_announcerManualAnnounce( tor );
813}
814
815void
816tr_torrentManualUpdate( tr_torrent * tor )
817{
818    assert( tr_isTorrent( tor  ) );
819
820    tr_runInEventThread( tor->session, tr_torrentManualUpdateImpl, tor );
821}
822
823tr_bool
824tr_torrentCanManualUpdate( const tr_torrent * tor )
825{
826    return ( tr_isTorrent( tor  ) )
827        && ( tor->isRunning )
828        && ( tr_announcerCanManualAnnounce( tor ) );
829}
830
831const tr_info *
832tr_torrentInfo( const tr_torrent * tor )
833{
834    return tr_isTorrent( tor ) ? &tor->info : NULL;
835}
836
837const tr_stat *
838tr_torrentStatCached( tr_torrent * tor )
839{
840    const time_t now = time( NULL );
841
842    return tr_isTorrent( tor ) && ( now == tor->lastStatTime )
843         ? &tor->stats
844         : tr_torrentStat( tor );
845}
846
847void
848tr_torrentSetVerifyState( tr_torrent * tor, tr_verify_state state )
849{
850    assert( tr_isTorrent( tor ) );
851    assert( state==TR_VERIFY_NONE || state==TR_VERIFY_WAIT || state==TR_VERIFY_NOW );
852
853    tor->verifyState = state;
854    tor->anyDate = time( NULL );
855}
856
857tr_torrent_activity
858tr_torrentGetActivity( tr_torrent * tor )
859{
860    assert( tr_isTorrent( tor ) );
861
862    tr_torrentRecheckCompleteness( tor );
863
864    if( tor->verifyState == TR_VERIFY_NOW )
865        return TR_STATUS_CHECK;
866    if( tor->verifyState == TR_VERIFY_WAIT )
867        return TR_STATUS_CHECK_WAIT;
868    if( !tor->isRunning )
869        return TR_STATUS_STOPPED;
870    if( tor->completeness == TR_LEECH )
871        return TR_STATUS_DOWNLOAD;
872
873    return TR_STATUS_SEED;
874}
875
876const tr_stat *
877tr_torrentStat( tr_torrent * tor )
878{
879    tr_stat *               s;
880    int                     usableSeeds = 0;
881    uint64_t                now;
882    double                  downloadedForRatio, seedRatio=0;
883    double                  d;
884    tr_bool                 checkSeedRatio;
885
886    if( !tor )
887        return NULL;
888
889    assert( tr_isTorrent( tor ) );
890    tr_torrentLock( tor );
891
892    tor->lastStatTime = time( NULL );
893
894    s = &tor->stats;
895    s->id = tor->uniqueId;
896    s->activity = tr_torrentGetActivity( tor );
897    s->error = tor->error;
898    memcpy( s->errorString, tor->errorString, sizeof( s->errorString ) );
899
900    s->manualAnnounceTime = tr_announcerNextManualAnnounce( tor );
901
902    tr_peerMgrTorrentStats( tor,
903                            &s->peersKnown,
904                            &s->peersConnected,
905                            &usableSeeds,
906                            &s->webseedsSendingToUs,
907                            &s->peersSendingToUs,
908                            &s->peersGettingFromUs,
909                            s->peersFrom );
910
911    now = tr_date( );
912    d = tr_peerMgrGetWebseedSpeed( tor, now );
913    s->swarmSpeed         = tr_rcRate( &tor->swarmSpeed, now );
914    s->rawUploadSpeed     = tr_bandwidthGetRawSpeed  ( tor->bandwidth, now, TR_UP );
915    s->pieceUploadSpeed   = tr_bandwidthGetPieceSpeed( tor->bandwidth, now, TR_UP );
916    s->rawDownloadSpeed   = d + tr_bandwidthGetRawSpeed  ( tor->bandwidth, now, TR_DOWN );
917    s->pieceDownloadSpeed = d + tr_bandwidthGetPieceSpeed( tor->bandwidth, now, TR_DOWN );
918
919    usableSeeds += tor->info.webseedCount;
920
921    s->percentComplete = tr_cpPercentComplete ( &tor->completion );
922
923    s->percentDone   = tr_cpPercentDone  ( &tor->completion );
924    s->leftUntilDone = tr_cpLeftUntilDone( &tor->completion );
925    s->sizeWhenDone  = tr_cpSizeWhenDone ( &tor->completion );
926
927    s->recheckProgress = s->activity == TR_STATUS_CHECK
928                       ? 1.0 -
929                         ( tr_torrentCountUncheckedPieces( tor ) /
930                           (double) tor->info.pieceCount )
931                       : 0.0;
932
933    s->activityDate = tor->activityDate;
934    s->addedDate    = tor->addedDate;
935    s->doneDate     = tor->doneDate;
936    s->startDate    = tor->startDate;
937
938    s->corruptEver     = tor->corruptCur    + tor->corruptPrev;
939    s->downloadedEver  = tor->downloadedCur + tor->downloadedPrev;
940    s->uploadedEver    = tor->uploadedCur   + tor->uploadedPrev;
941    s->haveValid       = tr_cpHaveValid( &tor->completion );
942    s->haveUnchecked   = tr_cpHaveTotal( &tor->completion ) - s->haveValid;
943
944    if( usableSeeds > 0 )
945    {
946        s->desiredAvailable = s->leftUntilDone;
947    }
948    else if( !s->leftUntilDone || !s->peersConnected )
949    {
950        s->desiredAvailable = 0;
951    }
952    else
953    {
954        tr_piece_index_t i;
955        tr_bitfield *    peerPieces = tr_peerMgrGetAvailable( tor );
956        s->desiredAvailable = 0;
957        for( i = 0; i < tor->info.pieceCount; ++i )
958            if( !tor->info.pieces[i].dnd && tr_bitfieldHasFast( peerPieces, i ) )
959                s->desiredAvailable += tr_cpMissingBlocksInPiece( &tor->completion, i );
960        s->desiredAvailable *= tor->blockSize;
961        tr_bitfieldFree( peerPieces );
962    }
963
964    downloadedForRatio = s->downloadedEver ? s->downloadedEver : s->haveValid;
965    s->ratio = tr_getRatio( s->uploadedEver, downloadedForRatio );
966
967    checkSeedRatio = tr_torrentGetSeedRatio( tor, &seedRatio );
968
969    switch( s->activity )
970    {
971        case TR_STATUS_DOWNLOAD:
972            if( s->leftUntilDone > s->desiredAvailable )
973                s->eta = TR_ETA_NOT_AVAIL;
974            else if( s->pieceDownloadSpeed < 0.1 )
975                s->eta = TR_ETA_UNKNOWN;
976            else {
977                /* etaSpeed exists because if we pieceDownloadSpeed directly,
978                 * brief fluctuations cause the ETA to jump all over the place.
979                 * so, etaSpeed is a smoothed-out version of pieceDownloadSpeed
980                 * to dampen the effect of fluctuations */
981                if( ( tor->etaSpeedCalculatedAt + 800 ) < now ) {
982                    tor->etaSpeedCalculatedAt = now;
983                    tor->etaSpeed = (fabs(tor->etaSpeed)<0.0001)
984                        ? s->pieceDownloadSpeed /* if no previous speed, no need to smooth */
985                        : 0.8*tor->etaSpeed + 0.2*s->pieceDownloadSpeed; /* smooth across 5 readings */
986                }
987                s->eta = s->leftUntilDone / tor->etaSpeed / 1024.0;
988            }
989            break;
990
991        case TR_STATUS_SEED:
992            if( checkSeedRatio )
993            {
994                if( s->pieceUploadSpeed < 0.1 )
995                    s->eta = TR_ETA_UNKNOWN;
996                else
997                    s->eta = (downloadedForRatio * (seedRatio - s->ratio)) / s->pieceUploadSpeed / 1024.0;
998            }
999            else
1000                s->eta = TR_ETA_NOT_AVAIL;
1001            break;
1002
1003        default:
1004            s->eta = TR_ETA_NOT_AVAIL;
1005            break;
1006    }
1007
1008    if( !checkSeedRatio || s->ratio >= seedRatio || s->ratio == TR_RATIO_INF )
1009        s->percentRatio = 1.0;
1010    else if( s->ratio == TR_RATIO_NA )
1011        s->percentRatio = 0.0;
1012    else
1013        s->percentRatio = s->ratio / seedRatio;
1014
1015    tr_torrentUnlock( tor );
1016
1017    return s;
1018}
1019
1020/***
1021****
1022***/
1023
1024static uint64_t
1025fileBytesCompleted( const tr_torrent * tor, tr_file_index_t index )
1026{
1027    uint64_t total = 0;
1028    const tr_file * f = &tor->info.files[index];
1029
1030    if( f->length )
1031    {
1032        const tr_block_index_t firstBlock = f->offset / tor->blockSize;
1033        const uint64_t lastByte = f->offset + f->length - 1;
1034        const tr_block_index_t lastBlock = lastByte / tor->blockSize;
1035
1036        if( firstBlock == lastBlock )
1037        {
1038            if( tr_cpBlockIsCompleteFast( &tor->completion, firstBlock ) )
1039                total = f->length;
1040        }
1041        else
1042        {
1043            uint32_t i;
1044
1045            /* the first block */
1046            if( tr_cpBlockIsCompleteFast( &tor->completion, firstBlock ) )
1047                total += tor->blockSize - ( f->offset % tor->blockSize );
1048
1049            /* the middle blocks */
1050            if( f->firstPiece == f->lastPiece )
1051            {
1052                for( i=firstBlock+1; i<lastBlock; ++i )
1053                    if( tr_cpBlockIsCompleteFast( &tor->completion, i ) )
1054                        total += tor->blockSize;
1055            }
1056            else
1057            {
1058                int64_t b = 0;
1059                const tr_block_index_t firstBlockOfLastPiece
1060                           = tr_torPieceFirstBlock( tor, f->lastPiece );
1061                const tr_block_index_t lastBlockOfFirstPiece
1062                           = tr_torPieceFirstBlock( tor, f->firstPiece )
1063                             + tr_torPieceCountBlocks( tor, f->firstPiece ) - 1;
1064
1065                /* the rest of the first piece */
1066                for( i=firstBlock+1; i<lastBlock && i<=lastBlockOfFirstPiece; ++i )
1067                    if( tr_cpBlockIsCompleteFast( &tor->completion, i ) )
1068                        ++b;
1069
1070                /* the middle pieces */
1071                if( f->firstPiece + 1 < f->lastPiece )
1072                    for( i=f->firstPiece+1; i<f->lastPiece; ++i )
1073                        b += tor->blockCountInPiece - tr_cpMissingBlocksInPiece( &tor->completion, i );
1074
1075                /* the rest of the last piece */
1076                for( i=firstBlockOfLastPiece; i<lastBlock; ++i )
1077                    if( tr_cpBlockIsCompleteFast( &tor->completion, i ) )
1078                        ++b;
1079
1080                b *= tor->blockSize;
1081                total += b;
1082            }
1083
1084            /* the last block */
1085            if( tr_cpBlockIsCompleteFast( &tor->completion, lastBlock ) )
1086                total += ( f->offset + f->length ) - ( tor->blockSize * lastBlock );
1087        }
1088    }
1089
1090    return total;
1091}
1092
1093tr_file_stat *
1094tr_torrentFiles( const tr_torrent * tor,
1095                 tr_file_index_t *  fileCount )
1096{
1097    tr_file_index_t       i;
1098    const tr_file_index_t n = tor->info.fileCount;
1099    tr_file_stat *        files = tr_new0( tr_file_stat, n );
1100    tr_file_stat *        walk = files;
1101    const tr_bool         isSeed = tor->completeness == TR_SEED;
1102
1103    assert( tr_isTorrent( tor ) );
1104
1105    for( i = 0; i < n; ++i, ++walk )
1106    {
1107        const uint64_t b = isSeed ? tor->info.files[i].length : fileBytesCompleted( tor, i );
1108        walk->bytesCompleted = b;
1109        walk->progress = tr_getRatio( b, tor->info.files[i].length );
1110    }
1111
1112    if( fileCount )
1113        *fileCount = n;
1114
1115    return files;
1116}
1117
1118void
1119tr_torrentFilesFree( tr_file_stat *            files,
1120                     tr_file_index_t fileCount UNUSED )
1121{
1122    tr_free( files );
1123}
1124
1125/***
1126****
1127***/
1128
1129float*
1130tr_torrentWebSpeeds( const tr_torrent * tor )
1131{
1132    return tr_isTorrent( tor )
1133         ? tr_peerMgrWebSpeeds( tor )
1134         : NULL;
1135}
1136
1137tr_peer_stat *
1138tr_torrentPeers( const tr_torrent * tor,
1139                 int *              peerCount )
1140{
1141    tr_peer_stat * ret = NULL;
1142
1143    if( tr_isTorrent( tor ) )
1144        ret = tr_peerMgrPeerStats( tor, peerCount );
1145
1146    return ret;
1147}
1148
1149void
1150tr_torrentPeersFree( tr_peer_stat * peers,
1151                     int peerCount  UNUSED )
1152{
1153    tr_free( peers );
1154}
1155
1156tr_tracker_stat *
1157tr_torrentTrackers( const tr_torrent * torrent,
1158                    int              * setmeTrackerCount )
1159{
1160    assert( tr_isTorrent( torrent ) );
1161
1162    return tr_announcerStats( torrent, setmeTrackerCount );
1163}
1164
1165void
1166tr_torrentTrackersFree( tr_tracker_stat * trackers,
1167                        int trackerCount )
1168{
1169    tr_announcerStatsFree( trackers, trackerCount );
1170}
1171
1172void
1173tr_torrentAvailability( const tr_torrent * tor,
1174                        int8_t *           tab,
1175                        int                size )
1176{
1177    tr_peerMgrTorrentAvailability( tor, tab, size );
1178}
1179
1180void
1181tr_torrentAmountFinished( const tr_torrent * tor,
1182                          float *            tab,
1183                          int                size )
1184{
1185    assert( tr_isTorrent( tor ) );
1186
1187    tr_torrentLock( tor );
1188    tr_cpGetAmountDone( &tor->completion, tab, size );
1189    tr_torrentUnlock( tor );
1190}
1191
1192static void
1193tr_torrentResetTransferStats( tr_torrent * tor )
1194{
1195    assert( tr_isTorrent( tor ) );
1196
1197    tr_torrentLock( tor );
1198
1199    tor->downloadedPrev += tor->downloadedCur;
1200    tor->downloadedCur   = 0;
1201    tor->uploadedPrev   += tor->uploadedCur;
1202    tor->uploadedCur     = 0;
1203    tor->corruptPrev    += tor->corruptCur;
1204    tor->corruptCur      = 0;
1205
1206    tr_torrentSetDirty( tor );
1207
1208    tr_torrentUnlock( tor );
1209}
1210
1211void
1212tr_torrentSetHasPiece( tr_torrent *     tor,
1213                       tr_piece_index_t pieceIndex,
1214                       tr_bool          has )
1215{
1216    assert( tr_isTorrent( tor ) );
1217    assert( pieceIndex < tor->info.pieceCount );
1218
1219    if( has )
1220        tr_cpPieceAdd( &tor->completion, pieceIndex );
1221    else
1222        tr_cpPieceRem( &tor->completion, pieceIndex );
1223}
1224
1225/***
1226****
1227***/
1228
1229static void
1230freeTorrent( tr_torrent * tor )
1231{
1232    tr_torrent * t;
1233    tr_session *  session = tor->session;
1234    tr_info *    inf = &tor->info;
1235
1236    assert( tr_isTorrent( tor ) );
1237    assert( !tor->isRunning );
1238
1239    tr_globalLock( session );
1240
1241    tr_peerMgrRemoveTorrent( tor );
1242
1243    tr_cpDestruct( &tor->completion );
1244
1245    tr_rcDestruct( &tor->swarmSpeed );
1246
1247    tr_announcerUnsubscribe( tor->tiers, tor->tiersSubscription );
1248    tr_announcerRemoveTorrent( session->announcer, tor );
1249
1250    tr_bitfieldDestruct( &tor->checkedPieces );
1251
1252    tr_free( tor->downloadDir );
1253    tr_free( tor->incompleteDir );
1254    tr_free( tor->peer_id );
1255
1256    if( tor == session->torrentList )
1257        session->torrentList = tor->next;
1258    else for( t = session->torrentList; t != NULL; t = t->next ) {
1259        if( t->next == tor ) {
1260            t->next = tor->next;
1261            break;
1262        }
1263    }
1264
1265    assert( session->torrentCount >= 1 );
1266    session->torrentCount--;
1267
1268    tr_bandwidthFree( tor->bandwidth );
1269
1270    tr_metainfoFree( inf );
1271    tr_free( tor );
1272
1273    tr_globalUnlock( session );
1274}
1275
1276/**
1277***  Start/Stop Callback
1278**/
1279
1280static void
1281checkAndStartImpl( void * vtor )
1282{
1283    tr_torrent * tor = vtor;
1284
1285    assert( tr_isTorrent( tor ) );
1286
1287    tr_globalLock( tor->session );
1288
1289    /** If we had local data before, but it's disappeared,
1290        stop the torrent and log an error. */
1291    if( tor->preVerifyTotal && !tr_cpHaveTotal( &tor->completion ) )
1292    {
1293        tr_torrentSetLocalError( tor, _( "Can't find local data.  Try \"Set Location\" to find it, or restart the torrent to re-download." ) );
1294        tr_torrentStop( tor );
1295    }
1296    else
1297    {
1298        const time_t now = time( NULL );
1299        tor->isRunning = TRUE;
1300        tor->needsSeedRatioCheck = TRUE;
1301        tor->error = TR_STAT_OK;
1302        tor->errorString[0] = '\0';
1303        tor->completeness = tr_cpGetStatus( &tor->completion );
1304        tor->startDate = tor->anyDate = now;
1305
1306        tr_torrentResetTransferStats( tor );
1307        tr_announcerTorrentStarted( tor );
1308        tor->dhtAnnounceAt = now + tr_cryptoWeakRandInt( 20 );
1309        tr_peerMgrStartTorrent( tor );
1310    }
1311
1312    tr_globalUnlock( tor->session );
1313}
1314
1315static void
1316checkAndStartCB( tr_torrent * tor )
1317{
1318    assert( tr_isTorrent( tor ) );
1319    assert( tr_isSession( tor->session ) );
1320
1321    tr_runInEventThread( tor->session, checkAndStartImpl, tor );
1322}
1323
1324static void
1325torrentStart( tr_torrent * tor )
1326{
1327    assert( tr_isTorrent( tor ) );
1328
1329    tr_globalLock( tor->session );
1330
1331    if( !tor->isRunning )
1332    {
1333        tr_verifyRemove( tor );
1334
1335        /* corresponds to the peer_id sent as a tracker request parameter.
1336         * one tracker admin says: "When the same torrent is opened and
1337         * closed and opened again without quitting Transmission ...
1338         * change the peerid. It would help sometimes if a stopped event
1339         * was missed to ensure that we didn't think someone was cheating. */
1340        tr_free( tor->peer_id );
1341        tor->peer_id = tr_peerIdNew( );
1342
1343        tor->isRunning = 1;
1344        tr_torrentSetDirty( tor );
1345        tor->preVerifyTotal = tr_cpHaveTotal( &tor->completion );
1346        tr_verifyAdd( tor, checkAndStartCB );
1347    }
1348
1349    tr_globalUnlock( tor->session );
1350}
1351
1352void
1353tr_torrentStart( tr_torrent * tor )
1354{
1355    if( tr_isTorrent( tor ) )
1356        torrentStart( tor );
1357}
1358
1359static void
1360torrentRecheckDoneImpl( void * vtor )
1361{
1362    tr_torrent * tor = vtor;
1363
1364    assert( tr_isTorrent( tor ) );
1365    tr_torrentRecheckCompleteness( tor );
1366
1367    if( tor->preVerifyTotal && !tr_cpHaveTotal( &tor->completion ) )
1368    {
1369        tr_torrentSetLocalError( tor, _( "Can't find local data.  Try \"Set Location\" to find it, or restart the torrent to re-download." ) );
1370        tr_torrentStop( tor );
1371    }
1372    else if( tor->startAfterVerify )
1373    {
1374        tor->startAfterVerify = FALSE;
1375
1376        tr_torrentStart( tor );
1377    }
1378}
1379
1380static void
1381torrentRecheckDoneCB( tr_torrent * tor )
1382{
1383    assert( tr_isTorrent( tor ) );
1384
1385    tr_runInEventThread( tor->session, torrentRecheckDoneImpl, tor );
1386}
1387
1388static void
1389verifyTorrent( void * vtor )
1390{
1391    tr_torrent * tor = vtor;
1392
1393    assert( tr_isTorrent( tor ) );
1394    tr_globalLock( tor->session );
1395
1396    /* if the torrent's already being verified, stop it */
1397    tr_verifyRemove( tor );
1398
1399    /* if the torrent's running, stop it & set the restart-after-verify flag */
1400    if( tor->startAfterVerify || tor->isRunning ) {
1401        tr_torrentStop( tor );
1402        tor->startAfterVerify = TRUE;
1403    }
1404
1405    /* add the torrent to the recheck queue */
1406    tor->preVerifyTotal = tr_cpHaveTotal( &tor->completion );
1407    tr_torrentUncheck( tor );
1408    tr_verifyAdd( tor, torrentRecheckDoneCB );
1409
1410    tr_globalUnlock( tor->session );
1411}
1412
1413void
1414tr_torrentVerify( tr_torrent * tor )
1415{
1416    if( tr_isTorrent( tor ) )
1417        tr_runInEventThread( tor->session, verifyTorrent, tor );
1418}
1419
1420void
1421tr_torrentSave( tr_torrent * tor )
1422{
1423    assert( tr_isTorrent( tor ) );
1424
1425    if( tor->isDirty ) {
1426        tor->isDirty = FALSE;
1427        tr_torrentSaveResume( tor );
1428    }
1429}
1430
1431static void
1432stopTorrent( void * vtor )
1433{
1434    tr_torrent * tor = vtor;
1435
1436    assert( tr_isTorrent( tor ) );
1437
1438    tr_verifyRemove( tor );
1439    tr_peerMgrStopTorrent( tor );
1440    tr_announcerTorrentStopped( tor );
1441
1442    tr_fdTorrentClose( tor->uniqueId );
1443
1444    if( !tor->isDeleting )
1445        tr_torrentSave( tor );
1446}
1447
1448void
1449tr_torrentStop( tr_torrent * tor )
1450{
1451    assert( tr_isTorrent( tor ) );
1452
1453    if( tr_isTorrent( tor ) )
1454    {
1455        tr_globalLock( tor->session );
1456
1457        tor->isRunning = 0;
1458        tr_torrentSetDirty( tor );
1459        tr_runInEventThread( tor->session, stopTorrent, tor );
1460
1461        tr_globalUnlock( tor->session );
1462    }
1463}
1464
1465static void
1466closeTorrent( void * vtor )
1467{
1468    tr_benc * d;
1469    tr_torrent * tor = vtor;
1470
1471    assert( tr_isTorrent( tor ) );
1472
1473    d = tr_bencListAddDict( &tor->session->removedTorrents, 2 );
1474    tr_bencDictAddInt( d, "id", tor->uniqueId );
1475    tr_bencDictAddInt( d, "date", time( NULL ) );
1476
1477    stopTorrent( tor );
1478
1479    if( tor->isDeleting )
1480    {
1481        tr_metainfoRemoveSaved( tor->session, &tor->info );
1482        tr_torrentRemoveResume( tor );
1483    }
1484
1485    tor->isRunning = 0;
1486    freeTorrent( tor );
1487}
1488
1489void
1490tr_torrentFree( tr_torrent * tor )
1491{
1492    if( tr_isTorrent( tor ) )
1493    {
1494        tr_session * session = tor->session;
1495        assert( tr_isSession( session ) );
1496        tr_globalLock( session );
1497
1498        tr_torrentClearCompletenessCallback( tor );
1499        tr_runInEventThread( session, closeTorrent, tor );
1500
1501        tr_globalUnlock( session );
1502    }
1503}
1504
1505void
1506tr_torrentRemove( tr_torrent * tor )
1507{
1508    assert( tr_isTorrent( tor ) );
1509
1510    tor->isDeleting = 1;
1511    tr_torrentFree( tor );
1512}
1513
1514/**
1515***  Completeness
1516**/
1517
1518static const char *
1519getCompletionString( int type )
1520{
1521    switch( type )
1522    {
1523        /* Translators: this is a minor point that's safe to skip over, but FYI:
1524           "Complete" and "Done" are specific, different terms in Transmission:
1525           "Complete" means we've downloaded every file in the torrent.
1526           "Done" means we're done downloading the files we wanted, but NOT all
1527           that exist */
1528        case TR_PARTIAL_SEED:
1529            return _( "Done" );
1530
1531        case TR_SEED:
1532            return _( "Complete" );
1533
1534        default:
1535            return _( "Incomplete" );
1536    }
1537}
1538
1539static void
1540fireCompletenessChange( tr_torrent       * tor,
1541                        tr_completeness    status )
1542{
1543    assert( tr_isTorrent( tor ) );
1544    assert( ( status == TR_LEECH )
1545         || ( status == TR_SEED )
1546         || ( status == TR_PARTIAL_SEED ) );
1547
1548    if( tor->completeness_func )
1549        tor->completeness_func( tor, status, tor->completeness_func_user_data );
1550}
1551
1552void
1553tr_torrentSetCompletenessCallback( tr_torrent                    * tor,
1554                                   tr_torrent_completeness_func    func,
1555                                   void                          * user_data )
1556{
1557    assert( tr_isTorrent( tor ) );
1558
1559    tor->completeness_func = func;
1560    tor->completeness_func_user_data = user_data;
1561}
1562
1563void
1564tr_torrentSetRatioLimitHitCallback( tr_torrent                     * tor,
1565                                    tr_torrent_ratio_limit_hit_func  func,
1566                                    void                           * user_data )
1567{
1568    assert( tr_isTorrent( tor ) );
1569
1570    tor->ratio_limit_hit_func = func;
1571    tor->ratio_limit_hit_func_user_data = user_data;
1572}
1573
1574void
1575tr_torrentClearCompletenessCallback( tr_torrent * torrent )
1576{
1577    tr_torrentSetCompletenessCallback( torrent, NULL, NULL );
1578}
1579
1580void
1581tr_torrentClearRatioLimitHitCallback( tr_torrent * torrent )
1582{
1583    tr_torrentSetRatioLimitHitCallback( torrent, NULL, NULL );
1584}
1585
1586void
1587tr_torrentRecheckCompleteness( tr_torrent * tor )
1588{
1589    tr_completeness completeness;
1590
1591    assert( tr_isTorrent( tor ) );
1592
1593    tr_torrentLock( tor );
1594
1595    completeness = tr_cpGetStatus( &tor->completion );
1596
1597    if( completeness != tor->completeness )
1598    {
1599        const int recentChange = tor->downloadedCur != 0;
1600
1601        if( recentChange )
1602        {
1603            tr_torinf( tor, _( "State changed from \"%1$s\" to \"%2$s\"" ),
1604                      getCompletionString( tor->completeness ),
1605                      getCompletionString( completeness ) );
1606        }
1607
1608        tor->completeness = completeness;
1609        tor->needsSeedRatioCheck = TRUE;
1610        tr_fdTorrentClose( tor->uniqueId );
1611
1612        /* if the torrent is a seed now,
1613         * and the files used to be in the incompleteDir,
1614         * then move them to the destination directory */
1615        if( tr_torrentIsSeed( tor ) && ( tor->currentDir == tor->incompleteDir ) )
1616            tr_torrentSetLocation( tor, tor->downloadDir, TRUE, NULL, NULL );
1617
1618        fireCompletenessChange( tor, completeness );
1619
1620        if( recentChange && ( completeness == TR_SEED ) )
1621        {
1622            tr_announcerTorrentCompleted( tor );
1623
1624            tor->doneDate = tor->anyDate = time( NULL );
1625        }
1626
1627        tr_torrentSetDirty( tor );
1628    }
1629
1630    tr_torrentUnlock( tor );
1631}
1632
1633/**
1634***  File priorities
1635**/
1636
1637void
1638tr_torrentInitFilePriority( tr_torrent *    tor,
1639                            tr_file_index_t fileIndex,
1640                            tr_priority_t   priority )
1641{
1642    tr_piece_index_t i;
1643    tr_file *        file;
1644
1645    assert( tr_isTorrent( tor ) );
1646    assert( fileIndex < tor->info.fileCount );
1647    assert( tr_isPriority( priority ) );
1648
1649    file = &tor->info.files[fileIndex];
1650    file->priority = priority;
1651    for( i = file->firstPiece; i <= file->lastPiece; ++i )
1652        tor->info.pieces[i].priority = calculatePiecePriority( tor, i, fileIndex );
1653}
1654
1655void
1656tr_torrentSetFilePriorities( tr_torrent *      tor,
1657                             tr_file_index_t * files,
1658                             tr_file_index_t   fileCount,
1659                             tr_priority_t     priority )
1660{
1661    tr_file_index_t i;
1662
1663    assert( tr_isTorrent( tor ) );
1664
1665    tr_torrentLock( tor );
1666
1667    for( i = 0; i < fileCount; ++i )
1668        tr_torrentInitFilePriority( tor, files[i], priority );
1669
1670    tr_torrentSetDirty( tor );
1671    tr_torrentUnlock( tor );
1672}
1673
1674tr_priority_t
1675tr_torrentGetFilePriority( const tr_torrent * tor,
1676                           tr_file_index_t    file )
1677{
1678    tr_priority_t ret;
1679
1680    assert( tr_isTorrent( tor ) );
1681
1682    tr_torrentLock( tor );
1683    assert( tor );
1684    assert( file < tor->info.fileCount );
1685    ret = tor->info.files[file].priority;
1686    tr_torrentUnlock( tor );
1687
1688    return ret;
1689}
1690
1691tr_priority_t*
1692tr_torrentGetFilePriorities( const tr_torrent * tor )
1693{
1694    tr_file_index_t i;
1695    tr_priority_t * p;
1696
1697    assert( tr_isTorrent( tor ) );
1698
1699    tr_torrentLock( tor );
1700    p = tr_new0( tr_priority_t, tor->info.fileCount );
1701    for( i = 0; i < tor->info.fileCount; ++i )
1702        p[i] = tor->info.files[i].priority;
1703    tr_torrentUnlock( tor );
1704
1705    return p;
1706}
1707
1708/**
1709***  File DND
1710**/
1711
1712int
1713tr_torrentGetFileDL( const tr_torrent * tor,
1714                     tr_file_index_t    file )
1715{
1716    int doDownload;
1717
1718    assert( tr_isTorrent( tor ) );
1719
1720    tr_torrentLock( tor );
1721
1722    assert( file < tor->info.fileCount );
1723    doDownload = !tor->info.files[file].dnd;
1724
1725    tr_torrentUnlock( tor );
1726    return doDownload != 0;
1727}
1728
1729static void
1730setFileDND( tr_torrent * tor, tr_file_index_t fileIndex, int doDownload )
1731{
1732    tr_file *        file;
1733    const int        dnd = !doDownload;
1734    tr_piece_index_t firstPiece, firstPieceDND;
1735    tr_piece_index_t lastPiece, lastPieceDND;
1736    tr_file_index_t  i;
1737
1738    assert( tr_isTorrent( tor ) );
1739
1740    file = &tor->info.files[fileIndex];
1741    file->dnd = dnd;
1742    firstPiece = file->firstPiece;
1743    lastPiece = file->lastPiece;
1744
1745    /* can't set the first piece to DND unless
1746       every file using that piece is DND */
1747    firstPieceDND = dnd;
1748    if( fileIndex > 0 )
1749    {
1750        for( i = fileIndex - 1; firstPieceDND; --i )
1751        {
1752            if( tor->info.files[i].lastPiece != firstPiece )
1753                break;
1754            firstPieceDND = tor->info.files[i].dnd;
1755            if( !i )
1756                break;
1757        }
1758    }
1759
1760    /* can't set the last piece to DND unless
1761       every file using that piece is DND */
1762    lastPieceDND = dnd;
1763    for( i = fileIndex + 1; lastPieceDND && i < tor->info.fileCount; ++i )
1764    {
1765        if( tor->info.files[i].firstPiece != lastPiece )
1766            break;
1767        lastPieceDND = tor->info.files[i].dnd;
1768    }
1769
1770    if( firstPiece == lastPiece )
1771    {
1772        tor->info.pieces[firstPiece].dnd = firstPieceDND && lastPieceDND;
1773    }
1774    else
1775    {
1776        tr_piece_index_t pp;
1777        tor->info.pieces[firstPiece].dnd = firstPieceDND;
1778        tor->info.pieces[lastPiece].dnd = lastPieceDND;
1779        for( pp = firstPiece + 1; pp < lastPiece; ++pp )
1780            tor->info.pieces[pp].dnd = dnd;
1781    }
1782}
1783
1784void
1785tr_torrentInitFileDLs( tr_torrent      * tor,
1786                       tr_file_index_t * files,
1787                       tr_file_index_t   fileCount,
1788                       tr_bool           doDownload )
1789{
1790    tr_file_index_t i;
1791
1792    assert( tr_isTorrent( tor ) );
1793
1794    tr_torrentLock( tor );
1795
1796    for( i=0; i<fileCount; ++i )
1797        setFileDND( tor, files[i], doDownload );
1798    tr_cpInvalidateDND( &tor->completion );
1799    tor->needsSeedRatioCheck = TRUE;
1800
1801    tr_torrentUnlock( tor );
1802}
1803
1804void
1805tr_torrentSetFileDLs( tr_torrent *      tor,
1806                      tr_file_index_t * files,
1807                      tr_file_index_t   fileCount,
1808                      tr_bool           doDownload )
1809{
1810    assert( tr_isTorrent( tor ) );
1811
1812    tr_torrentLock( tor );
1813    tr_torrentInitFileDLs( tor, files, fileCount, doDownload );
1814    tr_torrentSetDirty( tor );
1815    tr_torrentUnlock( tor );
1816}
1817
1818/***
1819****
1820***/
1821
1822tr_priority_t
1823tr_torrentGetPriority( const tr_torrent * tor )
1824{
1825    assert( tr_isTorrent( tor ) );
1826
1827    return tor->bandwidth->priority;
1828}
1829
1830void
1831tr_torrentSetPriority( tr_torrent * tor, tr_priority_t priority )
1832{
1833    assert( tr_isTorrent( tor ) );
1834    assert( tr_isPriority( priority ) );
1835
1836    if( tor->bandwidth->priority != priority )
1837    {
1838        tor->bandwidth->priority = priority;
1839
1840        tr_torrentSetDirty( tor );
1841    }
1842}
1843
1844/***
1845****
1846***/
1847
1848void
1849tr_torrentSetPeerLimit( tr_torrent * tor,
1850                        uint16_t     maxConnectedPeers )
1851{
1852    assert( tr_isTorrent( tor ) );
1853
1854    tor->maxConnectedPeers = maxConnectedPeers;
1855}
1856
1857uint16_t
1858tr_torrentGetPeerLimit( const tr_torrent * tor )
1859{
1860    assert( tr_isTorrent( tor ) );
1861
1862    return tor->maxConnectedPeers;
1863}
1864
1865/***
1866****
1867***/
1868
1869tr_block_index_t
1870_tr_block( const tr_torrent * tor,
1871           tr_piece_index_t   index,
1872           uint32_t           offset )
1873{
1874    tr_block_index_t ret;
1875
1876    assert( tr_isTorrent( tor ) );
1877
1878    ret = index;
1879    ret *= ( tor->info.pieceSize / tor->blockSize );
1880    ret += offset / tor->blockSize;
1881    return ret;
1882}
1883
1884tr_bool
1885tr_torrentReqIsValid( const tr_torrent * tor,
1886                      tr_piece_index_t   index,
1887                      uint32_t           offset,
1888                      uint32_t           length )
1889{
1890    int err = 0;
1891
1892    assert( tr_isTorrent( tor ) );
1893
1894    if( index >= tor->info.pieceCount )
1895        err = 1;
1896    else if( length < 1 )
1897        err = 2;
1898    else if( ( offset + length ) > tr_torPieceCountBytes( tor, index ) )
1899        err = 3;
1900    else if( length > MAX_BLOCK_SIZE )
1901        err = 4;
1902    else if( tr_pieceOffset( tor, index, offset, length ) > tor->info.totalSize )
1903        err = 5;
1904
1905    if( err ) tr_tordbg( tor, "index %lu offset %lu length %lu err %d\n",
1906                              (unsigned long)index,
1907                              (unsigned long)offset,
1908                              (unsigned long)length,
1909                              err );
1910
1911    return !err;
1912}
1913
1914uint64_t
1915tr_pieceOffset( const tr_torrent * tor,
1916                tr_piece_index_t   index,
1917                uint32_t           offset,
1918                uint32_t           length )
1919{
1920    uint64_t ret;
1921
1922    assert( tr_isTorrent( tor ) );
1923
1924    ret = tor->info.pieceSize;
1925    ret *= index;
1926    ret += offset;
1927    ret += length;
1928    return ret;
1929}
1930
1931/***
1932****
1933***/
1934
1935void
1936tr_torrentSetPieceChecked( tr_torrent        * tor,
1937                           tr_piece_index_t    piece,
1938                           tr_bool             isChecked )
1939{
1940    assert( tr_isTorrent( tor ) );
1941
1942    if( isChecked )
1943        tr_bitfieldAdd( &tor->checkedPieces, piece );
1944    else
1945        tr_bitfieldRem( &tor->checkedPieces, piece );
1946}
1947
1948void
1949tr_torrentSetFileChecked( tr_torrent *    tor,
1950                          tr_file_index_t fileIndex,
1951                          tr_bool         isChecked )
1952{
1953    const tr_file *        file = &tor->info.files[fileIndex];
1954    const tr_piece_index_t begin = file->firstPiece;
1955    const tr_piece_index_t end = file->lastPiece + 1;
1956
1957    assert( tr_isTorrent( tor ) );
1958
1959    if( isChecked )
1960        tr_bitfieldAddRange( &tor->checkedPieces, begin, end );
1961    else
1962        tr_bitfieldRemRange( &tor->checkedPieces, begin, end );
1963}
1964
1965tr_bool
1966tr_torrentIsFileChecked( const tr_torrent * tor,
1967                         tr_file_index_t    fileIndex )
1968{
1969    const tr_file *        file = &tor->info.files[fileIndex];
1970    const tr_piece_index_t begin = file->firstPiece;
1971    const tr_piece_index_t end = file->lastPiece + 1;
1972    tr_piece_index_t       i;
1973    tr_bool                isChecked = TRUE;
1974
1975    assert( tr_isTorrent( tor ) );
1976
1977    for( i = begin; isChecked && i < end; ++i )
1978        if( !tr_torrentIsPieceChecked( tor, i ) )
1979            isChecked = FALSE;
1980
1981    return isChecked;
1982}
1983
1984void
1985tr_torrentUncheck( tr_torrent * tor )
1986{
1987    assert( tr_isTorrent( tor ) );
1988
1989    tr_bitfieldRemRange( &tor->checkedPieces, 0, tor->info.pieceCount );
1990}
1991
1992int
1993tr_torrentCountUncheckedPieces( const tr_torrent * tor )
1994{
1995    assert( tr_isTorrent( tor ) );
1996
1997    return tor->info.pieceCount - tr_bitfieldCountTrueBits( &tor->checkedPieces );
1998}
1999
2000time_t*
2001tr_torrentGetMTimes( const tr_torrent * tor, size_t * setme_n )
2002{
2003    size_t       i;
2004    const size_t n = tor->info.fileCount;
2005    time_t *     m = tr_new0( time_t, n );
2006
2007    assert( tr_isTorrent( tor ) );
2008
2009    for( i = 0; i < n; ++i )
2010    {
2011        struct stat sb;
2012        char * path = tr_torrentFindFile( tor, i );
2013        if( ( path != NULL ) && !stat( path, &sb ) && S_ISREG( sb.st_mode ) )
2014        {
2015#ifdef SYS_DARWIN
2016            m[i] = sb.st_mtimespec.tv_sec;
2017#else
2018            m[i] = sb.st_mtime;
2019#endif
2020        }
2021        tr_free( path );
2022    }
2023
2024    *setme_n = n;
2025    return m;
2026}
2027
2028/***
2029****
2030***/
2031
2032tr_announce_list_err
2033tr_torrentSetAnnounceList( tr_torrent *            tor,
2034                           const tr_tracker_info * trackers,
2035                           int                     trackerCount )
2036{
2037    int i, j;
2038    tr_benc metainfo;
2039
2040    assert( tr_isTorrent( tor ) );
2041
2042    /* look for bad URLs */
2043    for( i=0; i<trackerCount; ++i )
2044        if( !tr_httpIsValidURL( trackers[i].announce ) )
2045            return TR_ANNOUNCE_LIST_HAS_BAD;
2046
2047    /* look for duplicates */
2048    for( i=0; i<trackerCount; ++i )
2049        for( j=0; j<trackerCount; ++j )
2050            if( ( i != j ) && ( !strcmp( trackers[i].announce, trackers[j].announce ) ) )
2051                return TR_ANNOUNCE_LIST_HAS_DUPLICATES;
2052
2053    /* save to the .torrent file */
2054    if( !tr_bencLoadFile( &metainfo, TR_FMT_BENC, tor->info.torrent ) )
2055    {
2056        int       i;
2057        int       prevTier = -1;
2058        tr_benc * tier = NULL;
2059        tr_benc * announceList;
2060        tr_info   tmpInfo;
2061
2062        /* remove the old fields */
2063        tr_bencDictRemove( &metainfo, "announce" );
2064        tr_bencDictRemove( &metainfo, "announce-list" );
2065
2066        /* add the new fields */
2067        tr_bencDictAddStr( &metainfo, "announce", trackers[0].announce );
2068        announceList = tr_bencDictAddList( &metainfo, "announce-list", 0 );
2069        for( i = 0; i < trackerCount; ++i ) {
2070            if( prevTier != trackers[i].tier ) {
2071                prevTier = trackers[i].tier;
2072                tier = tr_bencListAddList( announceList, 0 );
2073            }
2074            tr_bencListAddStr( tier, trackers[i].announce );
2075        }
2076
2077        /* try to parse it back again, to make sure it's good */
2078        memset( &tmpInfo, 0, sizeof( tr_info ) );
2079        if( tr_metainfoParse( tor->session, &tmpInfo, &metainfo ) )
2080        {
2081            /* it's good, so keep these new trackers and free the old ones */
2082
2083            tr_info swap;
2084            swap.trackers = tor->info.trackers;
2085            swap.trackerCount = tor->info.trackerCount;
2086            tor->info.trackers = tmpInfo.trackers;
2087            tor->info.trackerCount = tmpInfo.trackerCount;
2088            tmpInfo.trackers = swap.trackers;
2089            tmpInfo.trackerCount = swap.trackerCount;
2090
2091            tr_metainfoFree( &tmpInfo );
2092            tr_bencToFile( &metainfo, TR_FMT_BENC, tor->info.torrent );
2093        }
2094
2095        /* cleanup */
2096        tr_bencFree( &metainfo );
2097
2098        /* tell the announcer to reload this torrent's tracker list */
2099        tr_announcerResetTorrent( tor->session->announcer, tor );
2100    }
2101
2102    return TR_ANNOUNCE_LIST_OK;
2103}
2104
2105/**
2106***
2107**/
2108
2109void
2110tr_torrentSetAddedDate( tr_torrent * tor,
2111                        time_t       t )
2112{
2113    assert( tr_isTorrent( tor ) );
2114
2115    tor->addedDate = t;
2116    tor->anyDate = MAX( tor->anyDate, tor->addedDate );
2117}
2118
2119void
2120tr_torrentSetActivityDate( tr_torrent * tor, time_t t )
2121{
2122    assert( tr_isTorrent( tor ) );
2123
2124    tor->activityDate = t;
2125    tor->anyDate = MAX( tor->anyDate, tor->activityDate );
2126}
2127
2128void
2129tr_torrentSetDoneDate( tr_torrent * tor,
2130                       time_t       t )
2131{
2132    assert( tr_isTorrent( tor ) );
2133
2134    tor->doneDate = t;
2135    tor->anyDate = MAX( tor->anyDate, tor->doneDate );
2136}
2137
2138/**
2139***
2140**/
2141
2142uint64_t
2143tr_torrentGetBytesLeftToAllocate( const tr_torrent * tor )
2144{
2145    tr_file_index_t i;
2146    uint64_t bytesLeft = 0;
2147
2148    assert( tr_isTorrent( tor ) );
2149
2150    for( i=0; i<tor->info.fileCount; ++i )
2151    {
2152        if( !tor->info.files[i].dnd )
2153        {
2154            struct stat sb;
2155            const uint64_t length = tor->info.files[i].length;
2156            char * path = tr_torrentFindFile( tor, i );
2157
2158            bytesLeft += length;
2159
2160            if( ( path != NULL ) && !stat( path, &sb )
2161                                 && S_ISREG( sb.st_mode )
2162                                 && ( (uint64_t)sb.st_size <= length ) )
2163                bytesLeft -= sb.st_size;
2164
2165            tr_free( path );
2166        }
2167    }
2168
2169    return bytesLeft;
2170}
2171
2172/****
2173*****  Removing the torrent's local data
2174****/
2175
2176static int
2177vstrcmp( const void * a, const void * b )
2178{
2179    return strcmp( a, b );
2180}
2181
2182static int
2183compareLongestFirst( const void * a, const void * b )
2184{
2185    const size_t alen = strlen( a );
2186    const size_t blen = strlen( b );
2187
2188    if( alen != blen )
2189        return alen > blen ? -1 : 1;
2190
2191    return vstrcmp( a, b );
2192}
2193
2194static void
2195addDirtyFile( const char  * root,
2196              const char  * filename,
2197              tr_ptrArray * dirtyFolders )
2198{
2199    char * dir = tr_dirname( filename );
2200
2201    /* add the parent folders to dirtyFolders until we reach the root or a known-dirty */
2202    while (     ( dir != NULL )
2203             && ( strlen( root ) <= strlen( dir ) )
2204             && ( tr_ptrArrayFindSorted( dirtyFolders, dir, vstrcmp ) == NULL ) )
2205    {
2206        char * tmp;
2207        tr_ptrArrayInsertSorted( dirtyFolders, tr_strdup( dir ), vstrcmp );
2208
2209        tmp = tr_dirname( dir );
2210        tr_free( dir );
2211        dir = tmp;
2212    }
2213
2214    tr_free( dir );
2215}
2216
2217static void
2218walkLocalData( const tr_torrent * tor,
2219               const char       * root,
2220               const char       * dir,
2221               const char       * base,
2222               tr_ptrArray      * torrentFiles,
2223               tr_ptrArray      * folders,
2224               tr_ptrArray      * dirtyFolders )
2225{
2226    int i;
2227    struct stat sb;
2228    char * buf;
2229
2230    assert( tr_isTorrent( tor ) );
2231
2232    buf = tr_buildPath( dir, base, NULL );
2233    i = stat( buf, &sb );
2234    if( !i )
2235    {
2236        DIR * odir = NULL;
2237
2238        if( S_ISDIR( sb.st_mode ) && ( ( odir = opendir ( buf ) ) ) )
2239        {
2240            struct dirent *d;
2241            tr_ptrArrayInsertSorted( folders, tr_strdup( buf ), vstrcmp );
2242            for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
2243                if( d->d_name && d->d_name[0] != '.' ) /* skip dotfiles */
2244                    walkLocalData( tor, root, buf, d->d_name, torrentFiles, folders, dirtyFolders );
2245            closedir( odir );
2246        }
2247        else if( S_ISREG( sb.st_mode ) && ( sb.st_size > 0 ) )
2248        {
2249            const char * sub = buf + strlen( tor->currentDir ) + strlen( TR_PATH_DELIMITER_STR );
2250            const tr_bool isTorrentFile = tr_ptrArrayFindSorted( torrentFiles, sub, vstrcmp ) != NULL;
2251            if( !isTorrentFile )
2252                addDirtyFile( root, buf, dirtyFolders );
2253        }
2254    }
2255
2256    tr_free( buf );
2257}
2258
2259static void
2260deleteLocalData( tr_torrent * tor, tr_fileFunc fileFunc )
2261{
2262    int i, n;
2263    char ** s;
2264    tr_file_index_t f;
2265    tr_ptrArray torrentFiles = TR_PTR_ARRAY_INIT;
2266    tr_ptrArray folders      = TR_PTR_ARRAY_INIT;
2267    tr_ptrArray dirtyFolders = TR_PTR_ARRAY_INIT; /* dirty == contains non-torrent files */
2268
2269    const char * firstFile = tor->info.files[0].name;
2270    const char * cpch = strchr( firstFile, TR_PATH_DELIMITER );
2271    char * tmp = cpch ? tr_strndup( firstFile, cpch - firstFile ) : NULL;
2272    char * root = tr_buildPath( tor->currentDir, tmp, NULL );
2273
2274    assert( tr_isTorrent( tor ) );
2275
2276    for( f=0; f<tor->info.fileCount; ++f ) {
2277        tr_ptrArrayInsertSorted( &torrentFiles, tr_strdup( tor->info.files[f].name ), vstrcmp );
2278        tr_ptrArrayInsertSorted( &torrentFiles, tr_torrentBuildPartial( tor, f ), vstrcmp );
2279    }
2280
2281    /* build the set of folders and dirtyFolders */
2282    walkLocalData( tor, root, root, NULL, &torrentFiles, &folders, &dirtyFolders );
2283
2284    /* try to remove entire folders first, so that the recycle bin will be tidy */
2285    s = (char**) tr_ptrArrayPeek( &folders, &n );
2286    for( i=0; i<n; ++i )
2287        if( tr_ptrArrayFindSorted( &dirtyFolders, s[i], vstrcmp ) == NULL )
2288            fileFunc( s[i] );
2289
2290    /* now blow away any remaining torrent files, such as torrent files in dirty folders */
2291    for( f=0; f<tor->info.fileCount; ++f ) {
2292        char * path = tr_buildPath( tor->currentDir, tor->info.files[f].name, NULL );
2293        fileFunc( path );
2294        tr_free( path );
2295    }
2296
2297    /* Now clean out the directories left empty from the previous step.
2298     * Work from deepest to shallowest s.t. lower folders
2299     * won't prevent the upper folders from being deleted */
2300    {
2301        tr_ptrArray cleanFolders = TR_PTR_ARRAY_INIT;
2302        s = (char**) tr_ptrArrayPeek( &folders, &n );
2303        for( i=0; i<n; ++i )
2304            if( tr_ptrArrayFindSorted( &dirtyFolders, s[i], vstrcmp ) == NULL )
2305                tr_ptrArrayInsertSorted( &cleanFolders, s[i], compareLongestFirst );
2306        s = (char**) tr_ptrArrayPeek( &cleanFolders, &n );
2307        for( i=0; i<n; ++i )
2308            fileFunc( s[i] );
2309        tr_ptrArrayDestruct( &cleanFolders, NULL );
2310    }
2311
2312    /* cleanup */
2313    tr_ptrArrayDestruct( &dirtyFolders, tr_free );
2314    tr_ptrArrayDestruct( &folders, tr_free );
2315    tr_ptrArrayDestruct( &torrentFiles, tr_free );
2316    tr_free( root );
2317    tr_free( tmp );
2318}
2319
2320void
2321tr_torrentDeleteLocalData( tr_torrent * tor, tr_fileFunc fileFunc )
2322{
2323    assert( tr_isTorrent( tor ) );
2324
2325    if( fileFunc == NULL )
2326        fileFunc = remove;
2327
2328    /* close all the files because we're about to delete them */
2329    tr_fdTorrentClose( tor->uniqueId );
2330
2331    if( tor->info.fileCount > 1 )
2332        deleteLocalData( tor, fileFunc );
2333    else {
2334        /* torrent only has one file */
2335        char * path = tr_torrentFindFile( tor, 0 );
2336        fileFunc( path );
2337        tr_free( path );
2338    }
2339}
2340
2341/***
2342****
2343***/
2344
2345struct LocationData
2346{
2347    tr_bool move_from_old_location;
2348    int * setme_state;
2349    double * setme_progress;
2350    char * location;
2351    tr_torrent * tor;
2352};
2353
2354static void
2355setLocation( void * vdata )
2356{
2357    tr_bool err = FALSE;
2358    tr_bool verify_needed = FALSE;
2359    struct LocationData * data = vdata;
2360    tr_torrent * tor = data->tor;
2361    const tr_bool do_move = data->move_from_old_location;
2362    const char * location = data->location;
2363    double bytesHandled = 0;
2364
2365    assert( tr_isTorrent( tor ) );
2366
2367    if( strcmp( location, tor->currentDir ) )
2368    {
2369        tr_file_index_t i;
2370
2371        /* bad idea to move files while they're being verified... */
2372        tr_verifyRemove( tor );
2373
2374        /* if the torrent is running, stop it and set a flag to
2375         * restart after we're done */
2376        if( tor->isRunning )
2377        {
2378            tr_torrentStop( tor );
2379            tor->startAfterVerify = TRUE;
2380        }
2381
2382        /* try to move the files.
2383         * FIXME: there are still all kinds of nasty cases, like what
2384         * if the target directory runs out of space halfway through... */
2385        for( i=0; !err && i<tor->info.fileCount; ++i )
2386        {
2387            const tr_file * f = &tor->info.files[i];
2388            const char * oldbase;
2389            char * sub;
2390            if( tr_torrentFindFile2( tor, i, &oldbase, &sub ) )
2391            {
2392                char * oldpath = tr_buildPath( oldbase, sub, NULL );
2393                char * newpath = tr_buildPath( location, sub, NULL );
2394
2395                if( do_move )
2396                {
2397                    errno = 0;
2398                    tr_torinf( tor, "moving \"%s\" to \"%s\"", oldpath, newpath );
2399                    if( rename( oldpath, newpath ) )
2400                    {
2401                        verify_needed = TRUE;
2402                        if( tr_moveFile( oldpath, newpath ) )
2403                        {
2404                            err = TRUE;
2405                            tr_torerr( tor, "error moving \"%s\" to \"%s\": %s",
2406                                            oldpath, newpath, tr_strerror( errno ) );
2407                        }
2408                    }
2409                }
2410
2411                tr_free( newpath );
2412                tr_free( oldpath );
2413                tr_free( sub );
2414            }
2415
2416            if( data->setme_progress )
2417            {
2418                bytesHandled += f->length;
2419                *data->setme_progress = bytesHandled / tor->info.totalSize;
2420            }
2421        }
2422
2423        if( !err )
2424        {
2425            /* blow away the leftover subdirectories in the old location */
2426            if( do_move && verify_needed )
2427                tr_torrentDeleteLocalData( tor, remove );
2428
2429            /* set the new location and reverify */
2430            tr_torrentSetDownloadDir( tor, location );
2431            if( verify_needed )
2432                tr_torrentVerify( tor );
2433        }
2434    }
2435
2436    if( data->setme_state )
2437        *data->setme_state = err ? TR_LOC_ERROR : TR_LOC_DONE;
2438
2439    /* cleanup */
2440    tr_free( data->location );
2441    tr_free( data );
2442}
2443
2444void
2445tr_torrentSetLocation( tr_torrent  * tor,
2446                       const char  * location,
2447                       tr_bool       move_from_old_location,
2448                       double      * setme_progress,
2449                       int         * setme_state )
2450{
2451    struct LocationData * data;
2452
2453    assert( tr_isTorrent( tor ) );
2454
2455    if( setme_state )
2456        *setme_state = TR_LOC_MOVING;
2457    if( setme_progress )
2458        *setme_progress = 0;
2459
2460    /* run this in the libtransmission thread */
2461    data = tr_new( struct LocationData, 1 );
2462    data->tor = tor;
2463    data->location = tr_strdup( location );
2464    data->move_from_old_location = move_from_old_location;
2465    data->setme_state = setme_state;
2466    data->setme_progress = setme_progress;
2467    tr_runInEventThread( tor->session, setLocation, data );
2468}
2469
2470/***
2471****
2472***/
2473
2474void
2475tr_torrentCheckSeedRatio( tr_torrent * tor )
2476{
2477    double seedRatio;
2478
2479    assert( tr_isTorrent( tor ) );
2480
2481    /* if we're seeding and we've reached our seed ratio limit, stop the torrent */
2482    if( tor->isRunning && tr_torrentIsSeed( tor ) && tr_torrentGetSeedRatio( tor, &seedRatio ) )
2483    {
2484        const uint64_t up = tor->uploadedCur + tor->uploadedPrev;
2485        uint64_t down = tor->downloadedCur + tor->downloadedPrev;
2486        double ratio;
2487
2488        /* maybe we're the initial seeder and never downloaded anything... */
2489        if( down == 0 )
2490            down = tr_cpHaveValid( &tor->completion );
2491
2492        ratio = tr_getRatio( up, down );
2493
2494        if( ratio >= seedRatio || ratio == TR_RATIO_INF )
2495        {
2496            tr_torrentStop( tor );
2497
2498            /* set to no ratio limit to allow easy restarting */
2499            tr_torrentSetRatioMode( tor, TR_RATIOLIMIT_UNLIMITED );
2500
2501            /* maybe notify the client */
2502            if( tor->ratio_limit_hit_func != NULL )
2503                tor->ratio_limit_hit_func( tor, tor->ratio_limit_hit_func_user_data );
2504        }
2505    }
2506}
2507
2508/***
2509****
2510***/
2511
2512void
2513tr_torrentFileCompleted( tr_torrent * tor, tr_file_index_t fileNum )
2514{
2515    char * sub;
2516    const char * base;
2517
2518    /* close the file so that we can reopen in read-only mode as needed */
2519    tr_fdFileClose( tor, fileNum );
2520
2521    /* if the torrent's filename on disk isn't the same as the one in the metadata,
2522     * then it's been modified to denote that it was a partial file.
2523     * Now that it's complete, use the proper filename. */
2524    if( tr_torrentFindFile2( tor, fileNum, &base, &sub ) )
2525    {
2526        const tr_file * file = &tor->info.files[fileNum];
2527
2528        if( strcmp( sub, file->name ) )
2529        {
2530            char * oldpath = tr_buildPath( base, sub, NULL );
2531            char * newpath = tr_buildPath( base, file->name, NULL );
2532
2533            if( rename( oldpath, newpath ) )
2534                tr_torerr( tor, "Error moving \"%s\" to \"%s\": %s", oldpath, newpath, tr_strerror( errno ) );
2535
2536            tr_free( newpath );
2537            tr_free( oldpath );
2538        }
2539
2540        tr_free( sub );
2541    }
2542}
2543
2544/***
2545****
2546***/
2547
2548static tr_bool
2549fileExists( const char * filename )
2550{
2551    struct stat sb;
2552    const tr_bool ok = !stat( filename, &sb );
2553    return ok;
2554}
2555
2556tr_bool
2557tr_torrentFindFile2( const tr_torrent * tor, tr_file_index_t fileNum,
2558                     const char ** base, char ** subpath )
2559{
2560    char * part;
2561    const tr_file * file;
2562    const char * b = NULL;
2563    const char * s = NULL;
2564
2565    assert( tr_isTorrent( tor ) );
2566    assert( fileNum < tor->info.fileCount );
2567
2568    file = &tor->info.files[fileNum];
2569    part = tr_torrentBuildPartial( tor, fileNum );
2570
2571    if( b == NULL ) {
2572        char * filename = tr_buildPath( tor->downloadDir, file->name, NULL );
2573        if( fileExists( filename ) ) {
2574            b = tor->downloadDir;
2575            s = file->name;
2576        }
2577        tr_free( filename );
2578    }
2579
2580    if( ( b == NULL ) && ( tor->incompleteDir != NULL ) ) {
2581        char * filename = tr_buildPath( tor->incompleteDir, file->name, NULL );
2582        if( fileExists( filename ) ) {
2583            b = tor->incompleteDir;
2584            s = file->name;
2585        }
2586        tr_free( filename );
2587    }
2588
2589    if( ( b == NULL ) && ( tor->incompleteDir != NULL ) ) {
2590        char * filename = tr_buildPath( tor->incompleteDir, part, NULL );
2591        if( fileExists( filename ) ) {
2592            b = tor->incompleteDir;
2593            s = part;
2594        }
2595        tr_free( filename );
2596    }
2597
2598    if( b == NULL) {
2599        char * filename = tr_buildPath( tor->downloadDir, part, NULL );
2600        if( fileExists( filename ) ) {
2601            b = tor->downloadDir;
2602            s = part;
2603        }
2604        tr_free( filename );
2605    }
2606
2607    if( base != NULL )
2608        *base = b;
2609    if( subpath != NULL )
2610        *subpath = tr_strdup( s );
2611
2612    tr_free( part );
2613    return b != NULL;
2614}
2615
2616
2617char*
2618tr_torrentFindFile( const tr_torrent * tor, tr_file_index_t fileNum )
2619{
2620    char * subpath;
2621    char * ret = NULL;
2622    const char * base;
2623
2624    if( tr_torrentFindFile2( tor, fileNum, &base, &subpath ) )
2625    {
2626        ret = tr_buildPath( base, subpath, NULL );
2627        tr_free( subpath );
2628    }
2629
2630    return ret;
2631}
2632
2633/* Decide whether we should be looking for files in downloadDir or incompleteDir. */
2634static void
2635refreshCurrentDir( tr_torrent * tor )
2636{
2637    const char * dir = NULL;
2638    char * sub;
2639
2640    if( tor->incompleteDir == NULL )
2641        dir = tor->downloadDir;
2642    else if( !tr_torrentFindFile2( tor, 0, &dir, &sub ) )
2643        dir = tor->incompleteDir;
2644
2645    assert( dir != NULL );
2646    assert( ( dir == tor->downloadDir ) || ( dir == tor->incompleteDir ) );
2647    tor->currentDir = dir;
2648}
2649
2650char*
2651tr_torrentBuildPartial( const tr_torrent * tor, tr_file_index_t fileNum )
2652{
2653    return tr_strdup_printf( "%s.part", tor->info.files[fileNum].name );
2654}
Note: See TracBrowser for help on using the repository browser.