transmission/libtransmission/torrent.c

3020 lines
80 KiB
C
Raw Normal View History

/*
2010-01-04 21:00:47 +00:00
* This file Copyright (C) 2009-2010 Mnemosyne LLC
2007-01-19 08:36:49 +00:00
*
* This file is licensed by the GPL version 2. Works owned by the
* Transmission project are granted a special exemption to clause 2(b)
* so that the bulk of its code can remain under the MIT license.
* This exemption does not extend to derived works not owned by
* the Transmission project.
2007-01-19 08:36:49 +00:00
*
* $Id$
*/
2007-01-19 08:36:49 +00:00
2008-04-14 11:52:50 +00:00
#include <sys/types.h> /* stat */
#include <sys/stat.h> /* stat */
#include <sys/wait.h> /* wait() */
2008-04-14 11:52:50 +00:00
#include <unistd.h> /* stat */
#include <dirent.h>
2008-04-14 11:52:50 +00:00
2007-11-09 20:07:52 +00:00
#include <assert.h>
#include <limits.h> /* INT_MAX */
#include <math.h>
#include <stdarg.h>
2008-02-19 18:39:49 +00:00
#include <string.h> /* memcmp */
#include <stdlib.h> /* qsort */
#include <stdarg.h> /* some 1.4.x versions of evutil.h need this */
#include <evutil.h> /* evutil_vsnprintf() */
2007-01-19 08:36:49 +00:00
#include "transmission.h"
#include "announcer.h"
#include "bandwidth.h"
2007-12-21 22:18:40 +00:00
#include "bencode.h"
#include "cache.h"
#include "completion.h"
#include "crypto.h" /* for tr_sha1 */
#include "resume.h"
#include "fdlimit.h" /* tr_fdTorrentClose */
#include "inout.h" /* tr_ioTestPiece() */
2009-11-24 02:16:31 +00:00
#include "magnet.h"
2007-07-09 20:10:42 +00:00
#include "metainfo.h"
#include "peer-common.h" /* MAX_BLOCK_SIZE */
#include "peer-mgr.h"
#include "platform.h" /* TR_PATH_DELIMITER_STR */
#include "ptrarray.h"
#include "session.h"
#include "torrent.h"
2009-11-24 17:10:40 +00:00
#include "torrent-magnet.h"
#include "trevent.h" /* tr_runInEventThread() */
#include "utils.h"
#include "verify.h"
#include "version.h"
2007-01-19 08:36:49 +00:00
/***
****
***/
2008-05-18 16:44:30 +00:00
int
tr_torrentId( const tr_torrent * tor )
{
return tor->uniqueId;
}
tr_torrent*
tr_torrentFindFromId( tr_session * session, int id )
{
tr_torrent * tor = NULL;
while(( tor = tr_torrentNext( session, tor )))
if( tor->uniqueId == id )
return tor;
return NULL;
}
tr_torrent*
tr_torrentFindFromHashString( tr_session * session, const char * str )
{
tr_torrent * tor = NULL;
while(( tor = tr_torrentNext( session, tor )))
if( !strcasecmp( str, tor->info.hashString ) )
return tor;
return NULL;
}
tr_torrent*
tr_torrentFindFromHash( tr_session * session, const uint8_t * torrentHash )
{
tr_torrent * tor = NULL;
while(( tor = tr_torrentNext( session, tor )))
if( *tor->info.hash == *torrentHash )
if( !memcmp( tor->info.hash, torrentHash, SHA_DIGEST_LENGTH ) )
return tor;
return NULL;
}
tr_torrent*
tr_torrentFindFromMagnetLink( tr_session * session, const char * magnet )
{
tr_magnet_info * info;
tr_torrent * tor = NULL;
if(( info = tr_magnetParse( magnet )))
{
tor = tr_torrentFindFromHash( session, info->hash );
tr_magnetFree( info );
}
return tor;
}
tr_torrent*
tr_torrentFindFromObfuscatedHash( tr_session * session,
const uint8_t * obfuscatedTorrentHash )
{
tr_torrent * tor = NULL;
while(( tor = tr_torrentNext( session, tor )))
if( !memcmp( tor->obfuscatedHash, obfuscatedTorrentHash,
SHA_DIGEST_LENGTH ) )
return tor;
return NULL;
}
tr_bool
tr_torrentIsPieceTransferAllowed( const tr_torrent * tor,
tr_direction direction )
{
int limit;
tr_bool allowed = TRUE;
if( tr_torrentUsesSpeedLimit( tor, direction ) )
if( tr_torrentGetSpeedLimit_Bps( tor, direction ) <= 0 )
allowed = FALSE;
if( tr_torrentUsesSessionLimits( tor ) )
if( tr_sessionGetActiveSpeedLimit_Bps( tor->session, direction, &limit ) )
if( limit <= 0 )
allowed = FALSE;
return allowed;
}
/***
**** PER-TORRENT UL / DL SPEEDS
***/
2007-01-19 08:36:49 +00:00
void
tr_torrentSetSpeedLimit_Bps( tr_torrent * tor, tr_direction dir, int Bps )
2007-01-19 08:36:49 +00:00
{
assert( tr_isTorrent( tor ) );
assert( tr_isDirection( dir ) );
assert( Bps >= 0 );
if( tr_bandwidthSetDesiredSpeed_Bps( tor->bandwidth, dir, Bps ) )
tr_torrentSetDirty( tor );
}
void
tr_torrentSetSpeedLimit_KBps( tr_torrent * tor, tr_direction dir, int KBps )
{
tr_torrentSetSpeedLimit_Bps( tor, dir, toSpeedBytes( KBps ) );
}
int
tr_torrentGetSpeedLimit_Bps( const tr_torrent * tor, tr_direction dir )
{
assert( tr_isTorrent( tor ) );
assert( tr_isDirection( dir ) );
return tr_bandwidthGetDesiredSpeed_Bps( tor->bandwidth, dir );
}
int
tr_torrentGetSpeedLimit_KBps( const tr_torrent * tor, tr_direction dir )
{
return toSpeedKBps( tr_torrentGetSpeedLimit_Bps( tor, dir ) );
}
void
tr_torrentUseSpeedLimit( tr_torrent * tor, tr_direction dir, tr_bool do_use )
{
assert( tr_isTorrent( tor ) );
assert( tr_isDirection( dir ) );
if( tr_bandwidthSetLimited( tor->bandwidth, dir, do_use ) )
tr_torrentSetDirty( tor );
}
tr_bool
2009-03-28 16:47:01 +00:00
tr_torrentUsesSpeedLimit( const tr_torrent * tor, tr_direction dir )
2007-01-19 08:36:49 +00:00
{
assert( tr_isTorrent( tor ) );
assert( tr_isDirection( dir ) );
return tr_bandwidthIsLimited( tor->bandwidth, dir );
2007-01-19 08:36:49 +00:00
}
void
2009-03-28 16:47:01 +00:00
tr_torrentUseSessionLimits( tr_torrent * tor, tr_bool doUse )
{
tr_bool changed;
assert( tr_isTorrent( tor ) );
changed = tr_bandwidthHonorParentLimits( tor->bandwidth, TR_UP, doUse );
changed |= tr_bandwidthHonorParentLimits( tor->bandwidth, TR_DOWN, doUse );
if( changed )
tr_torrentSetDirty( tor );
}
tr_bool
2009-03-28 16:47:01 +00:00
tr_torrentUsesSessionLimits( const tr_torrent * tor )
2007-01-19 08:36:49 +00:00
{
assert( tr_isTorrent( tor ) );
return tr_bandwidthAreParentLimitsHonored( tor->bandwidth, TR_UP );
2007-01-19 08:36:49 +00:00
}
/***
****
***/
void
tr_torrentSetRatioMode( tr_torrent * tor, tr_ratiolimit mode )
{
assert( tr_isTorrent( tor ) );
assert( mode==TR_RATIOLIMIT_GLOBAL || mode==TR_RATIOLIMIT_SINGLE || mode==TR_RATIOLIMIT_UNLIMITED );
if( mode != tor->ratioLimitMode )
{
tor->ratioLimitMode = mode;
tr_torrentSetDirty( tor );
}
}
tr_ratiolimit
tr_torrentGetRatioMode( const tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
return tor->ratioLimitMode;
}
void
tr_torrentSetRatioLimit( tr_torrent * tor, double desiredRatio )
{
assert( tr_isTorrent( tor ) );
if( (int)(desiredRatio*100.0) != (int)(tor->desiredRatio*100.0) )
{
tor->desiredRatio = desiredRatio;
tr_torrentSetDirty( tor );
}
}
double
tr_torrentGetRatioLimit( const tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
return tor->desiredRatio;
}
tr_bool
tr_torrentGetSeedRatio( const tr_torrent * tor, double * ratio )
{
tr_bool isLimited;
switch( tr_torrentGetRatioMode( tor ) )
{
case TR_RATIOLIMIT_SINGLE:
isLimited = TRUE;
if( ratio )
*ratio = tr_torrentGetRatioLimit( tor );
break;
case TR_RATIOLIMIT_GLOBAL:
2009-02-17 04:00:53 +00:00
isLimited = tr_sessionIsRatioLimited( tor->session );
if( isLimited && ratio )
*ratio = tr_sessionGetRatioLimit( tor->session );
break;
default: /* TR_RATIOLIMIT_UNLIMITED */
isLimited = FALSE;
break;
}
return isLimited;
}
/* returns true if the seed ratio applies --
* it applies if the torrent's a seed AND it has a seed ratio set */
static tr_bool
tr_torrentGetSeedRatioBytes( tr_torrent * tor,
uint64_t * setmeLeft,
uint64_t * setmeGoal )
{
double seedRatio;
tr_bool seedRatioApplies = FALSE;
if( tr_torrentGetSeedRatio( tor, &seedRatio ) )
{
const uint64_t u = tor->uploadedCur + tor->uploadedPrev;
const uint64_t d = tor->downloadedCur + tor->downloadedPrev;
const uint64_t baseline = d ? d : tr_cpSizeWhenDone( &tor->completion );
const uint64_t goal = baseline * seedRatio;
if( setmeLeft ) *setmeLeft = goal > u ? goal - u : 0;
if( setmeGoal ) *setmeGoal = goal;
seedRatioApplies = tr_torrentIsSeed( tor );
}
return seedRatioApplies;
}
static tr_bool
tr_torrentIsSeedRatioDone( tr_torrent * tor )
{
uint64_t bytesLeft;
return tr_torrentGetSeedRatioBytes( tor, &bytesLeft, NULL ) && !bytesLeft;
}
/***
****
***/
void
tr_torrentSetIdleMode( tr_torrent * tor, tr_idlelimit mode )
{
assert( tr_isTorrent( tor ) );
assert( mode==TR_IDLELIMIT_GLOBAL || mode==TR_IDLELIMIT_SINGLE || mode==TR_IDLELIMIT_UNLIMITED );
if( mode != tor->idleLimitMode )
{
tor->idleLimitMode = mode;
tr_torrentSetDirty( tor );
}
}
tr_idlelimit
tr_torrentGetIdleMode( const tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
return tor->idleLimitMode;
}
void
tr_torrentSetIdleLimit( tr_torrent * tor, uint16_t idleMinutes )
{
assert( tr_isTorrent( tor ) );
if( idleMinutes > 0 )
{
tor->idleLimitMinutes = idleMinutes;
tr_torrentSetDirty( tor );
}
}
uint16_t
tr_torrentGetIdleLimit( const tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
return tor->idleLimitMinutes;
}
tr_bool
tr_torrentGetSeedIdle( const tr_torrent * tor, uint16_t * idleMinutes )
{
tr_bool isLimited;
switch( tr_torrentGetIdleMode( tor ) )
{
case TR_IDLELIMIT_SINGLE:
isLimited = TRUE;
if( idleMinutes )
*idleMinutes = tr_torrentGetIdleLimit( tor );
break;
case TR_IDLELIMIT_GLOBAL:
isLimited = tr_sessionIsIdleLimited( tor->session );
if( isLimited && idleMinutes )
*idleMinutes = tr_sessionGetIdleLimit( tor->session );
break;
default: /* TR_IDLELIMIT_UNLIMITED */
isLimited = FALSE;
break;
}
return isLimited;
}
static tr_bool
tr_torrentIsSeedIdleLimitDone( tr_torrent * tor )
{
uint16_t idleMinutes;
return tr_torrentGetSeedIdle( tor, &idleMinutes )
&& difftime(tr_time(), MAX(tor->startDate, tor->activityDate)) >= idleMinutes * 60u;
}
/***
****
***/
void
tr_torrentCheckSeedLimit( tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
if( !tor->isRunning || !tr_torrentIsSeed( tor ) )
return;
/* if we're seeding and reach our seed ratio limit, stop the torrent */
if( tr_torrentIsSeedRatioDone( tor ) )
{
tr_torinf( tor, "Seed ratio reached; pausing torrent" );
tor->isStopping = TRUE;
/* maybe notify the client */
if( tor->ratio_limit_hit_func != NULL )
tor->ratio_limit_hit_func( tor, tor->ratio_limit_hit_func_user_data );
}
/* if we're seeding and reach our inactiviy limit, stop the torrent */
else if( tr_torrentIsSeedIdleLimitDone( tor ) )
{
tr_torinf( tor, "Seeding idle limit reached; pausing torrent" );
tor->isStopping = TRUE;
tor->finishedSeedingByIdle = TRUE;
/* maybe notify the client */
if( tor->idle_limit_hit_func != NULL )
tor->idle_limit_hit_func( tor, tor->idle_limit_hit_func_user_data );
}
}
/***
****
***/
void
tr_torrentSetLocalError( tr_torrent * tor, const char * fmt, ... )
{
va_list ap;
assert( tr_isTorrent( tor ) );
va_start( ap, fmt );
tor->error = TR_STAT_LOCAL_ERROR;
tor->errorTracker[0] = '\0';
evutil_vsnprintf( tor->errorString, sizeof( tor->errorString ), fmt, ap );
va_end( ap );
tr_torerr( tor, "%s", tor->errorString );
if( tor->isRunning )
tor->isStopping = TRUE;
}
static void
tr_torrentClearError( tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
tor->error = TR_STAT_OK;
tor->errorString[0] = '\0';
tor->errorTracker[0] = '\0';
}
static void
onTrackerResponse( tr_torrent * tor, const tr_tracker_event * event, void * unused UNUSED )
{
switch( event->messageType )
{
case TR_TRACKER_PEERS:
{
size_t i, n;
const int8_t seedProbability = event->seedProbability;
const tr_bool allAreSeeds = seedProbability == 100;
tr_pex * pex = tr_peerMgrArrayToPex( event->compact,
event->compactLen, &n );
if( allAreSeeds )
tr_tordbg( tor, "Got %d seeds from tracker", (int)n );
else
tr_tordbg( tor, "Got %d peers from tracker", (int)n );
for( i = 0; i < n; ++i )
tr_peerMgrAddPex( tor, TR_PEER_FROM_TRACKER, pex+i, seedProbability );
if( allAreSeeds && tr_torrentIsPrivate( tor ) )
tr_peerMgrMarkAllAsSeeds( tor );
tr_free( pex );
break;
}
case TR_TRACKER_WARNING:
tr_torerr( tor, _( "Tracker warning: \"%s\"" ), event->text );
tor->error = TR_STAT_TRACKER_WARNING;
tr_strlcpy( tor->errorTracker, event->tracker, sizeof( tor->errorTracker ) );
tr_strlcpy( tor->errorString, event->text, sizeof( tor->errorString ) );
break;
case TR_TRACKER_ERROR:
tr_torerr( tor, _( "Tracker error: \"%s\"" ), event->text );
tor->error = TR_STAT_TRACKER_ERROR;
tr_strlcpy( tor->errorTracker, event->tracker, sizeof( tor->errorTracker ) );
tr_strlcpy( tor->errorString, event->text, sizeof( tor->errorString ) );
break;
case TR_TRACKER_ERROR_CLEAR:
if( tor->error != TR_STAT_LOCAL_ERROR )
tr_torrentClearError( tor );
break;
}
}
/***
****
**** TORRENT INSTANTIATION
****
***/
2007-04-02 20:38:23 +00:00
static tr_piece_index_t
getBytePiece( const tr_info * info, uint64_t byteOffset )
{
2008-08-01 16:43:22 +00:00
assert( info );
assert( info->pieceSize != 0 );
2007-04-02 20:38:23 +00:00
return byteOffset / info->pieceSize;
2007-04-02 20:38:23 +00:00
}
static void
initFilePieces( tr_info * info,
tr_file_index_t fileIndex )
2007-04-02 20:38:23 +00:00
{
tr_file * file;
uint64_t firstByte, lastByte;
2007-04-02 20:38:23 +00:00
2008-08-01 16:43:22 +00:00
assert( info );
assert( fileIndex < info->fileCount );
2007-01-19 08:36:49 +00:00
file = &info->files[fileIndex];
firstByte = file->offset;
lastByte = firstByte + ( file->length ? file->length - 1 : 0 );
file->firstPiece = getBytePiece( info, firstByte );
file->lastPiece = getBytePiece( info, lastByte );
}
2007-12-25 06:37:21 +00:00
static int
pieceHasFile( tr_piece_index_t piece,
const tr_file * file )
2007-12-25 06:37:21 +00:00
{
return ( file->firstPiece <= piece ) && ( piece <= file->lastPiece );
}
static tr_priority_t
calculatePiecePriority( const tr_torrent * tor,
tr_piece_index_t piece,
int fileHint )
{
tr_file_index_t i;
tr_priority_t priority = TR_PRI_LOW;
/* find the first file that has data in this piece */
if( fileHint >= 0 ) {
i = fileHint;
while( i > 0 && pieceHasFile( piece, &tor->info.files[i - 1] ) )
--i;
} else {
for( i = 0; i < tor->info.fileCount; ++i )
if( pieceHasFile( piece, &tor->info.files[i] ) )
break;
}
/* the piece's priority is the max of the priorities
* of all the files in that piece */
for( ; i < tor->info.fileCount; ++i )
2007-01-19 08:36:49 +00:00
{
const tr_file * file = &tor->info.files[i];
2007-12-25 06:37:21 +00:00
if( !pieceHasFile( piece, file ) )
break;
priority = MAX( priority, file->priority );
/* when dealing with multimedia files, getting the first and
last pieces can sometimes allow you to preview it a bit
before it's fully downloaded... */
if( file->priority >= TR_PRI_NORMAL )
if( file->firstPiece == piece || file->lastPiece == piece )
priority = TR_PRI_HIGH;
2007-01-19 08:36:49 +00:00
}
return priority;
2007-01-19 08:36:49 +00:00
}
static void
tr_torrentInitFilePieces( tr_torrent * tor )
2007-01-19 08:36:49 +00:00
{
int * firstFiles;
tr_file_index_t f;
tr_piece_index_t p;
uint64_t offset = 0;
tr_info * inf = &tor->info;
/* assign the file offsets */
for( f=0; f<inf->fileCount; ++f ) {
inf->files[f].offset = offset;
offset += inf->files[f].length;
initFilePieces( inf, f );
}
2007-04-02 20:38:23 +00:00
/* build the array of first-file hints to give calculatePiecePriority */
firstFiles = tr_new( int, inf->pieceCount );
for( p=f=0; p<inf->pieceCount; ++p ) {
while( inf->files[f].lastPiece < p )
++f;
firstFiles[p] = f;
}
2007-01-19 08:36:49 +00:00
#if 0
/* test to confirm the first-file hints are correct */
for( p=0; p<inf->pieceCount; ++p ) {
f = firstFiles[p];
assert( inf->files[f].firstPiece <= p );
assert( inf->files[f].lastPiece >= p );
if( f > 0 )
assert( inf->files[f-1].lastPiece < p );
for( f=0; f<inf->fileCount; ++f )
if( pieceHasFile( p, &inf->files[f] ) )
break;
assert( (int)f == firstFiles[p] );
2007-01-19 08:36:49 +00:00
}
#endif
for( p=0; p<inf->pieceCount; ++p )
inf->pieces[p].priority = calculatePiecePriority( tor, p, firstFiles[p] );
2007-01-19 08:36:49 +00:00
tr_free( firstFiles );
2007-01-19 08:36:49 +00:00
}
static void torrentStart( tr_torrent * tor );
/**
* Decide on a block size. constraints:
* (1) most clients decline requests over 16 KiB
* (2) pieceSize must be a multiple of block size
*/
uint32_t
tr_getBlockSize( uint32_t pieceSize )
{
uint32_t b = pieceSize;
while( b > MAX_BLOCK_SIZE )
b /= 2u;
#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero#1143: crash in 1.30 when adding torrents: div by zero
2008-08-06 11:39:30 +00:00
if( !b || ( pieceSize % b ) ) /* not cleanly divisible */
return 0;
return b;
}
2009-11-24 02:16:31 +00:00
static void refreshCurrentDir( tr_torrent * tor );
static void
2009-11-24 02:16:31 +00:00
torrentInitFromInfo( tr_torrent * tor )
2007-01-19 08:36:49 +00:00
{
2009-11-24 02:16:31 +00:00
uint64_t t;
tr_info * info = &tor->info;
tor->blockSize = tr_getBlockSize( info->pieceSize );
2009-11-24 02:16:31 +00:00
if( info->pieceSize )
tor->lastPieceSize = (uint32_t)(info->totalSize % info->pieceSize);
2007-07-28 16:31:08 +00:00
if( !tor->lastPieceSize )
tor->lastPieceSize = info->pieceSize;
2007-07-28 16:31:08 +00:00
2009-11-24 02:16:31 +00:00
if( tor->blockSize )
tor->lastBlockSize = info->totalSize % tor->blockSize;
2007-07-28 16:31:08 +00:00
if( !tor->lastBlockSize )
tor->lastBlockSize = tor->blockSize;
2007-07-28 16:31:08 +00:00
2009-11-24 02:16:31 +00:00
tor->blockCount = tor->blockSize
? ( info->totalSize + tor->blockSize - 1 ) / tor->blockSize
: 0;
2009-11-24 02:16:31 +00:00
tor->blockCountInPiece = tor->blockSize
? info->pieceSize / tor->blockSize
: 0;
2009-11-24 02:16:31 +00:00
tor->blockCountInLastPiece = tor->blockSize
? ( tor->lastPieceSize + tor->blockSize - 1 ) / tor->blockSize
: 0;
/* check our work */
2009-11-24 02:16:31 +00:00
if( tor->blockSize != 0 )
assert( ( info->pieceSize % tor->blockSize ) == 0 );
t = info->pieceCount - 1;
t *= info->pieceSize;
t += tor->lastPieceSize;
assert( t == info->totalSize );
t = tor->blockCount - 1;
t *= tor->blockSize;
t += tor->lastBlockSize;
assert( t == info->totalSize );
t = info->pieceCount - 1;
t *= tor->blockCountInPiece;
t += tor->blockCountInLastPiece;
assert( t == (uint64_t)tor->blockCount );
tr_cpConstruct( &tor->completion, tor );
2007-01-19 08:36:49 +00:00
tr_torrentInitFilePieces( tor );
2009-11-24 02:16:31 +00:00
tor->completeness = tr_cpGetStatus( &tor->completion );
}
static void tr_torrentFireMetadataCompleted( tr_torrent * tor );
2009-11-24 02:16:31 +00:00
void
tr_torrentGotNewInfoDict( tr_torrent * tor )
{
torrentInitFromInfo( tor );
tr_torrentFireMetadataCompleted( tor );
2009-11-24 02:16:31 +00:00
}
static tr_bool
setLocalErrorIfFilesDisappeared( tr_torrent * tor )
{
const tr_bool disappeared = ( tr_cpHaveTotal( &tor->completion ) > 0 ) && ( tr_torrentGetCurrentSizeOnDisk( tor ) == 0 );
if( disappeared )
{
tr_tordbg( tor, "%s", "[LAZY] uh oh, the files disappeared" );
tr_torrentSetLocalError( tor, "%s", _( "No data found! Ensure your drives are connected or use \"Set Location\". To re-download, remove the torrent and re-add it." ) );
}
return disappeared;
}
2009-11-24 02:16:31 +00:00
static void
torrentInit( tr_torrent * tor, const tr_ctor * ctor )
{
int doStart;
uint64_t loaded;
const char * dir;
tr_bool isNewTorrent;
struct stat st;
2009-11-24 02:16:31 +00:00
static int nextUniqueId = 1;
tr_session * session = tr_ctorGetSession( ctor );
assert( session != NULL );
tr_sessionLock( session );
2009-11-24 02:16:31 +00:00
tor->session = session;
tor->uniqueId = nextUniqueId++;
tor->magicNumber = TORRENT_MAGIC_NUMBER;
tr_sha1( tor->obfuscatedHash, "req2", 4,
2009-11-24 02:16:31 +00:00
tor->info.hash, SHA_DIGEST_LENGTH,
NULL );
2007-01-19 08:36:49 +00:00
2009-11-24 02:16:31 +00:00
if( !tr_ctorGetDownloadDir( ctor, TR_FORCE, &dir ) ||
!tr_ctorGetDownloadDir( ctor, TR_FALLBACK, &dir ) )
tor->downloadDir = tr_strdup( dir );
if( tr_ctorGetIncompleteDir( ctor, &dir ) )
dir = tr_sessionGetIncompleteDir( session );
if( tr_sessionIsIncompleteDirEnabled( session ) )
tor->incompleteDir = tr_strdup( dir );
tor->bandwidth = tr_bandwidthNew( session, session->bandwidth );
tor->bandwidth->priority = tr_ctorGetBandwidthPriority( ctor );
2009-11-24 02:16:31 +00:00
tor->error = TR_STAT_OK;
tor->finishedSeedingByIdle = FALSE;
2009-11-24 02:16:31 +00:00
tr_peerMgrAddTorrent( session->peerMgr, tor );
2007-01-19 08:36:49 +00:00
assert( !tor->downloadedCur );
assert( !tor->uploadedCur );
tr_torrentSetAddedDate( tor, tr_time( ) ); /* this is a default value to be
overwritten by the resume file */
torrentInitFromInfo( tor );
loaded = tr_torrentLoadResume( tor, ~0, ctor );
tor->completeness = tr_cpGetStatus( &tor->completion );
setLocalErrorIfFilesDisappeared( tor );
tr_ctorInitTorrentPriorities( ctor, tor );
tr_ctorInitTorrentWanted( ctor, tor );
refreshCurrentDir( tor );
2007-12-21 22:18:40 +00:00
doStart = tor->isRunning;
tor->isRunning = 0;
if( !( loaded & TR_FR_SPEEDLIMIT ) )
{
2009-03-28 16:47:01 +00:00
tr_torrentUseSpeedLimit( tor, TR_UP, FALSE );
tr_torrentSetSpeedLimit_Bps( tor, TR_UP, tr_sessionGetSpeedLimit_Bps( tor->session, TR_UP ) );
2009-03-28 16:47:01 +00:00
tr_torrentUseSpeedLimit( tor, TR_DOWN, FALSE );
tr_torrentSetSpeedLimit_Bps( tor, TR_DOWN, tr_sessionGetSpeedLimit_Bps( tor->session, TR_DOWN ) );
2009-03-28 16:47:01 +00:00
tr_torrentUseSessionLimits( tor, TRUE );
}
if( !( loaded & TR_FR_RATIOLIMIT ) )
{
tr_torrentSetRatioMode( tor, TR_RATIOLIMIT_GLOBAL );
tr_torrentSetRatioLimit( tor, tr_sessionGetRatioLimit( tor->session ) );
}
if( !( loaded & TR_FR_IDLELIMIT ) )
{
tr_torrentSetIdleMode( tor, TR_IDLELIMIT_GLOBAL );
tr_torrentSetIdleLimit( tor, tr_sessionGetIdleLimit( tor->session ) );
}
2008-05-18 16:44:30 +00:00
{
tr_torrent * it = NULL;
tr_torrent * last = NULL;
while( ( it = tr_torrentNext( session, it ) ) )
2008-05-18 16:44:30 +00:00
last = it;
2008-05-18 16:44:30 +00:00
if( !last )
session->torrentList = tor;
2008-05-18 16:44:30 +00:00
else
last->next = tor;
++session->torrentCount;
2008-05-18 16:44:30 +00:00
}
/* if we don't have a local .torrent file already, assume the torrent is new */
isNewTorrent = stat( tor->info.torrent, &st );
/* maybe save our own copy of the metainfo */
if( tr_ctorGetSave( ctor ) )
{
const tr_benc * val;
if( !tr_ctorGetMetainfo( ctor, &val ) )
{
2009-11-24 02:16:31 +00:00
const char * path = tor->info.torrent;
const int err = tr_bencToFile( val, TR_FMT_BENC, path );
if( err )
tr_torrentSetLocalError( tor, "Unable to save torrent file: %s", tr_strerror( err ) );
2009-11-24 02:16:31 +00:00
tr_sessionSetTorrentFile( tor->session, tor->info.hashString, path );
}
}
tor->tiers = tr_announcerAddTorrent( tor->session->announcer, tor, onTrackerResponse, NULL );
if( isNewTorrent )
{
tor->startAfterVerify = doStart;
tr_torrentVerify( tor );
}
else if( doStart )
{
torrentStart( tor );
}
2009-11-24 02:16:31 +00:00
tr_sessionUnlock( session );
}
static tr_parse_result
torrentParseImpl( const tr_ctor * ctor, tr_info * setmeInfo,
tr_bool * setmeHasInfo, int * dictLength )
{
int doFree;
tr_bool didParse;
tr_bool hasInfo = FALSE;
tr_info tmp;
const tr_benc * metainfo;
tr_session * session = tr_ctorGetSession( ctor );
2009-08-05 02:07:51 +00:00
tr_parse_result result = TR_PARSE_OK;
2007-12-21 22:18:40 +00:00
if( setmeInfo == NULL )
setmeInfo = &tmp;
memset( setmeInfo, 0, sizeof( tr_info ) );
if( tr_ctorGetMetainfo( ctor, &metainfo ) )
return TR_PARSE_ERR;
didParse = tr_metainfoParse( session, metainfo, setmeInfo,
&hasInfo, dictLength );
doFree = didParse && ( setmeInfo == &tmp );
if( !didParse )
result = TR_PARSE_ERR;
if( didParse && hasInfo && !tr_getBlockSize( setmeInfo->pieceSize ) )
result = TR_PARSE_ERR;
if( didParse && session && tr_torrentExists( session, setmeInfo->hash ) )
result = TR_PARSE_DUPLICATE;
2007-08-19 04:03:06 +00:00
if( doFree )
2007-12-21 22:18:40 +00:00
tr_metainfoFree( setmeInfo );
if( setmeHasInfo != NULL )
*setmeHasInfo = hasInfo;
return result;
}
2007-12-21 22:18:40 +00:00
tr_parse_result
tr_torrentParse( const tr_ctor * ctor, tr_info * setmeInfo )
{
return torrentParseImpl( ctor, setmeInfo, NULL, NULL );
}
2007-12-21 22:18:40 +00:00
tr_torrent *
tr_torrentNew( const tr_ctor * ctor, int * setmeError )
2007-01-19 08:36:49 +00:00
{
int len;
tr_bool hasInfo;
2009-11-24 02:16:31 +00:00
tr_info tmpInfo;
tr_parse_result r;
tr_torrent * tor = NULL;
assert( ctor != NULL );
assert( tr_isSession( tr_ctorGetSession( ctor ) ) );
r = torrentParseImpl( ctor, &tmpInfo, &hasInfo, &len );
if( r == TR_PARSE_OK )
{
tor = tr_new0( tr_torrent, 1 );
tor->info = tmpInfo;
if( hasInfo )
tor->infoDictLength = len;
torrentInit( tor, ctor );
}
else
{
if( r == TR_PARSE_DUPLICATE )
tr_metainfoFree( &tmpInfo );
if( setmeError )
*setmeError = r;
}
return tor;
}
/**
***
**/
void
tr_torrentSetDownloadDir( tr_torrent * tor, const char * path )
2007-01-19 08:36:49 +00:00
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
2008-05-18 16:44:30 +00:00
if( !path || !tor->downloadDir || strcmp( path, tor->downloadDir ) )
{
2008-05-18 16:44:30 +00:00
tr_free( tor->downloadDir );
tor->downloadDir = tr_strdup( path );
tr_torrentSetDirty( tor );
}
refreshCurrentDir( tor );
}
const char*
2008-05-18 16:44:30 +00:00
tr_torrentGetDownloadDir( const tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
2008-05-18 16:44:30 +00:00
return tor->downloadDir;
2007-01-19 08:36:49 +00:00
}
const char *
tr_torrentGetCurrentDir( const tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
return tor->currentDir;
}
void
tr_torrentChangeMyPort( tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
if( tor->isRunning )
tr_announcerChangeMyPort( tor );
}
static inline void
tr_torrentManualUpdateImpl( void * vtor )
2007-01-19 08:36:49 +00:00
{
tr_torrent * tor = vtor;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
if( tor->isRunning )
tr_announcerManualAnnounce( tor );
}
void
tr_torrentManualUpdate( tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_runInEventThread( tor->session, tr_torrentManualUpdateImpl, tor );
}
tr_bool
tr_torrentCanManualUpdate( const tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
return ( tr_isTorrent( tor ) )
&& ( tor->isRunning )
&& ( tr_announcerCanManualAnnounce( tor ) );
2007-01-19 08:36:49 +00:00
}
2007-12-21 22:18:40 +00:00
const tr_info *
tr_torrentInfo( const tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
return tr_isTorrent( tor ) ? &tor->info : NULL;
2007-12-21 22:18:40 +00:00
}
const tr_stat *
tr_torrentStatCached( tr_torrent * tor )
{
const time_t now = tr_time( );
2009-01-30 00:41:08 +00:00
return tr_isTorrent( tor ) && ( now == tor->lastStatTime )
? &tor->stats
: tr_torrentStat( tor );
2007-12-21 22:18:40 +00:00
}
void
tr_torrentSetVerifyState( tr_torrent * tor, tr_verify_state state )
{
assert( tr_isTorrent( tor ) );
assert( state==TR_VERIFY_NONE || state==TR_VERIFY_WAIT || state==TR_VERIFY_NOW );
tor->verifyState = state;
tor->anyDate = tr_time( );
}
tr_torrent_activity
tr_torrentGetActivity( tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_torrentRecheckCompleteness( tor );
if( tor->verifyState == TR_VERIFY_NOW )
return TR_STATUS_CHECK;
if( tor->verifyState == TR_VERIFY_WAIT )
return TR_STATUS_CHECK_WAIT;
if( !tor->isRunning )
return TR_STATUS_STOPPED;
if( tor->completeness == TR_LEECH )
return TR_STATUS_DOWNLOAD;
return TR_STATUS_SEED;
}
static double
getVerifyProgress( const tr_torrent * tor )
{
tr_piece_index_t i, n;
tr_piece_index_t checked = 0;
assert( tr_isTorrent( tor ) );
for( i=0, n=tor->info.pieceCount; i!=n; ++i )
if( tor->info.pieces[i].timeChecked )
++checked;
return checked / (double)tor->info.pieceCount;
}
const tr_stat *
tr_torrentStat( tr_torrent * tor )
2007-01-19 08:36:49 +00:00
{
tr_stat * s;
int usableSeeds;
uint64_t now;
double d;
uint64_t seedRatioBytesLeft;
uint64_t seedRatioBytesGoal;
tr_bool seedRatioApplies;
uint16_t seedIdleMinutes;
2007-01-19 08:36:49 +00:00
2008-04-06 17:12:15 +00:00
if( !tor )
return NULL;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_torrentLock( tor );
tor->lastStatTime = tr_time( );
s = &tor->stats;
s->id = tor->uniqueId;
s->activity = tr_torrentGetActivity( tor );
s->error = tor->error;
memcpy( s->errorString, tor->errorString, sizeof( s->errorString ) );
2007-01-19 08:36:49 +00:00
s->manualAnnounceTime = tr_announcerNextManualAnnounce( tor );
tr_peerMgrTorrentStats( tor,
&s->peersKnown,
&s->peersConnected,
&usableSeeds,
2008-06-10 02:36:52 +00:00
&s->webseedsSendingToUs,
&s->peersSendingToUs,
&s->peersGettingFromUs,
s->peersFrom );
now = tr_time_msec( );
d = tr_peerMgrGetWebseedSpeed_Bps( tor, now );
s->rawUploadSpeed_KBps = toSpeedKBps( tr_bandwidthGetRawSpeed_Bps ( tor->bandwidth, now, TR_UP ) );
s->pieceUploadSpeed_KBps = toSpeedKBps( tr_bandwidthGetPieceSpeed_Bps( tor->bandwidth, now, TR_UP ) );
s->rawDownloadSpeed_KBps = toSpeedKBps( d + tr_bandwidthGetRawSpeed_Bps ( tor->bandwidth, now, TR_DOWN ) );
s->pieceDownloadSpeed_KBps = toSpeedKBps( d + tr_bandwidthGetPieceSpeed_Bps( tor->bandwidth, now, TR_DOWN ) );
2007-01-19 08:36:49 +00:00
usableSeeds += tor->info.webseedCount;
s->percentComplete = tr_cpPercentComplete ( &tor->completion );
2009-11-24 17:10:40 +00:00
s->metadataPercentComplete = tr_torrentGetMetadataPercent( tor );
s->percentDone = tr_cpPercentDone ( &tor->completion );
s->leftUntilDone = tr_cpLeftUntilDone( &tor->completion );
s->sizeWhenDone = tr_cpSizeWhenDone ( &tor->completion );
s->recheckProgress = s->activity == TR_STATUS_CHECK ? getVerifyProgress( tor ) : 0;
s->activityDate = tor->activityDate;
s->addedDate = tor->addedDate;
s->doneDate = tor->doneDate;
s->startDate = tor->startDate;
2007-01-19 08:36:49 +00:00
if ((s->activity == TR_STATUS_DOWNLOAD || s->activity == TR_STATUS_SEED) && s->startDate != 0)
s->idleSecs = difftime(tr_time(), MAX(s->startDate, s->activityDate));
else
s->idleSecs = -1;
s->corruptEver = tor->corruptCur + tor->corruptPrev;
s->downloadedEver = tor->downloadedCur + tor->downloadedPrev;
s->uploadedEver = tor->uploadedCur + tor->uploadedPrev;
s->haveValid = tr_cpHaveValid( &tor->completion );
s->haveUnchecked = tr_cpHaveTotal( &tor->completion ) - s->haveValid;
if( usableSeeds > 0 )
{
s->desiredAvailable = s->leftUntilDone;
}
else if( !s->leftUntilDone || !s->peersConnected )
{
s->desiredAvailable = 0;
}
else
{
tr_piece_index_t i;
tr_bitfield * peerPieces = tr_peerMgrGetAvailable( tor );
2007-09-27 03:30:22 +00:00
s->desiredAvailable = 0;
for( i = 0; i < tor->info.pieceCount; ++i )
if( !tor->info.pieces[i].dnd && tr_bitfieldHasFast( peerPieces, i ) )
s->desiredAvailable += tr_cpMissingBlocksInPiece( &tor->completion, i );
s->desiredAvailable *= tor->blockSize;
tr_bitfieldFree( peerPieces );
}
s->ratio = tr_getRatio( s->uploadedEver,
s->downloadedEver ? s->downloadedEver : s->haveValid );
seedRatioApplies = tr_torrentGetSeedRatioBytes( tor, &seedRatioBytesLeft,
&seedRatioBytesGoal );
switch( s->activity )
{
/* etaXLSpeed exists because if we use the piece speed directly,
* brief fluctuations cause the ETA to jump all over the place.
* so, etaXLSpeed is a smoothed-out version of the piece speed
* to dampen the effect of fluctuations */
case TR_STATUS_DOWNLOAD:
if( ( tor->etaDLSpeedCalculatedAt + 800 ) < now ) {
tor->etaDLSpeed_KBps = ( ( tor->etaDLSpeedCalculatedAt + 4000 ) < now )
? s->pieceDownloadSpeed_KBps /* if no recent previous speed, no need to smooth */
: ((tor->etaDLSpeed_KBps*4.0) + s->pieceDownloadSpeed_KBps)/5.0; /* smooth across 5 readings */
tor->etaDLSpeedCalculatedAt = now;
}
2009-11-24 02:16:31 +00:00
if( s->leftUntilDone > s->desiredAvailable )
s->eta = TR_ETA_NOT_AVAIL;
else if( tor->etaDLSpeed_KBps < 1 )
s->eta = TR_ETA_UNKNOWN;
else
s->eta = s->leftUntilDone / toSpeedBytes(tor->etaDLSpeed_KBps);
s->etaIdle = TR_ETA_NOT_AVAIL;
break;
case TR_STATUS_SEED: {
if( !seedRatioApplies )
s->eta = TR_ETA_NOT_AVAIL;
else {
if( ( tor->etaULSpeedCalculatedAt + 800 ) < now ) {
tor->etaULSpeed_KBps = ( ( tor->etaULSpeedCalculatedAt + 4000 ) < now )
? s->pieceUploadSpeed_KBps /* if no recent previous speed, no need to smooth */
: ((tor->etaULSpeed_KBps*4.0) + s->pieceUploadSpeed_KBps)/5.0; /* smooth across 5 readings */
tor->etaULSpeedCalculatedAt = now;
}
if( tor->etaULSpeed_KBps < 1 )
s->eta = TR_ETA_UNKNOWN;
else
s->eta = seedRatioBytesLeft / toSpeedBytes(tor->etaULSpeed_KBps);
}
if( tor->etaULSpeed_KBps < 1 && tr_torrentGetSeedIdle( tor, &seedIdleMinutes ) )
s->etaIdle = seedIdleMinutes * 60 - s->idleSecs;
else
s->etaIdle = TR_ETA_NOT_AVAIL;
break;
}
default:
s->eta = TR_ETA_NOT_AVAIL;
s->etaIdle = TR_ETA_NOT_AVAIL;
break;
}
2009-11-24 02:16:31 +00:00
/* s->haveValid is here to make sure a torrent isn't marked 'finished'
* when the user hits "uncheck all" prior to starting the torrent... */
s->finished = tor->finishedSeedingByIdle || (seedRatioApplies && !seedRatioBytesLeft && s->haveValid);
if( !seedRatioApplies || s->finished )
s->seedRatioPercentDone = 1;
else if( !seedRatioBytesGoal ) /* impossible? safeguard for div by zero */
s->seedRatioPercentDone = 0;
2009-11-24 02:16:31 +00:00
else
s->seedRatioPercentDone = (double)(seedRatioBytesGoal - seedRatioBytesLeft) / seedRatioBytesGoal;
tr_torrentUnlock( tor );
2007-01-19 08:36:49 +00:00
return s;
}
/***
****
***/
static uint64_t
fileBytesCompleted( const tr_torrent * tor, tr_file_index_t index )
{
uint64_t total = 0;
const tr_file * f = &tor->info.files[index];
if( f->length )
{
const tr_block_index_t firstBlock = f->offset / tor->blockSize;
const uint64_t lastByte = f->offset + f->length - 1;
const tr_block_index_t lastBlock = lastByte / tor->blockSize;
if( firstBlock == lastBlock )
{
if( tr_cpBlockIsCompleteFast( &tor->completion, firstBlock ) )
total = f->length;
}
else
{
tr_block_index_t i;
/* the first block */
if( tr_cpBlockIsCompleteFast( &tor->completion, firstBlock ) )
total += tor->blockSize - ( f->offset % tor->blockSize );
/* the middle blocks */
if( f->firstPiece == f->lastPiece )
{
for( i=firstBlock+1; i<lastBlock; ++i )
if( tr_cpBlockIsCompleteFast( &tor->completion, i ) )
total += tor->blockSize;
}
else
{
2009-10-30 17:17:53 +00:00
uint64_t b = 0;
const tr_block_index_t firstBlockOfLastPiece
= tr_torPieceFirstBlock( tor, f->lastPiece );
const tr_block_index_t lastBlockOfFirstPiece
= tr_torPieceFirstBlock( tor, f->firstPiece )
+ tr_torPieceCountBlocks( tor, f->firstPiece ) - 1;
/* the rest of the first piece */
for( i=firstBlock+1; i<lastBlock && i<=lastBlockOfFirstPiece; ++i )
if( tr_cpBlockIsCompleteFast( &tor->completion, i ) )
++b;
/* the middle pieces */
if( f->firstPiece + 1 < f->lastPiece )
for( i=f->firstPiece+1; i<f->lastPiece; ++i )
b += tor->blockCountInPiece - tr_cpMissingBlocksInPiece( &tor->completion, i );
/* the rest of the last piece */
for( i=firstBlockOfLastPiece; i<lastBlock; ++i )
if( tr_cpBlockIsCompleteFast( &tor->completion, i ) )
++b;
b *= tor->blockSize;
total += b;
}
/* the last block */
if( tr_cpBlockIsCompleteFast( &tor->completion, lastBlock ) )
total += ( f->offset + f->length ) - ( (uint64_t)tor->blockSize * lastBlock );
}
}
return total;
}
tr_file_stat *
tr_torrentFiles( const tr_torrent * tor,
tr_file_index_t * fileCount )
{
tr_file_index_t i;
const tr_file_index_t n = tor->info.fileCount;
tr_file_stat * files = tr_new0( tr_file_stat, n );
tr_file_stat * walk = files;
const tr_bool isSeed = tor->completeness == TR_SEED;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
for( i=0; i<n; ++i, ++walk ) {
const uint64_t b = isSeed ? tor->info.files[i].length : fileBytesCompleted( tor, i );
walk->bytesCompleted = b;
walk->progress = tor->info.files[i].length > 0 ? ( (float)b / tor->info.files[i].length ) : 1.0f;
}
if( fileCount )
*fileCount = n;
return files;
}
void
tr_torrentFilesFree( tr_file_stat * files,
tr_file_index_t fileCount UNUSED )
{
tr_free( files );
}
/***
****
***/
double*
tr_torrentWebSpeeds_KBps( const tr_torrent * tor )
{
return tr_isTorrent( tor ) ? tr_peerMgrWebSpeeds_KBps( tor ) : NULL;
}
tr_peer_stat *
tr_torrentPeers( const tr_torrent * tor,
int * peerCount )
2007-01-19 08:36:49 +00:00
{
2007-11-23 02:14:21 +00:00
tr_peer_stat * ret = NULL;
2009-01-30 00:41:08 +00:00
if( tr_isTorrent( tor ) )
ret = tr_peerMgrPeerStats( tor, peerCount );
2007-11-23 02:14:21 +00:00
return ret;
2007-01-19 08:36:49 +00:00
}
void
tr_torrentPeersFree( tr_peer_stat * peers,
int peerCount UNUSED )
2007-01-19 08:36:49 +00:00
{
tr_free( peers );
2007-01-19 08:36:49 +00:00
}
tr_tracker_stat *
tr_torrentTrackers( const tr_torrent * torrent,
int * setmeTrackerCount )
{
assert( tr_isTorrent( torrent ) );
return tr_announcerStats( torrent, setmeTrackerCount );
}
void
tr_torrentTrackersFree( tr_tracker_stat * trackers,
int trackerCount )
{
tr_announcerStatsFree( trackers, trackerCount );
}
void
tr_torrentAvailability( const tr_torrent * tor,
int8_t * tab,
int size )
2007-01-19 08:36:49 +00:00
{
tr_peerMgrTorrentAvailability( tor, tab, size );
2007-01-19 08:36:49 +00:00
}
void
tr_torrentAmountFinished( const tr_torrent * tor,
float * tab,
int size )
2007-01-19 08:36:49 +00:00
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_torrentLock( tor );
tr_cpGetAmountDone( &tor->completion, tab, size );
tr_torrentUnlock( tor );
2007-04-28 01:34:39 +00:00
}
static void
tr_torrentResetTransferStats( tr_torrent * tor )
2007-01-19 08:36:49 +00:00
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_torrentLock( tor );
2007-01-19 08:36:49 +00:00
tor->downloadedPrev += tor->downloadedCur;
tor->downloadedCur = 0;
tor->uploadedPrev += tor->uploadedCur;
tor->uploadedCur = 0;
tor->corruptPrev += tor->corruptCur;
tor->corruptCur = 0;
2007-01-19 08:36:49 +00:00
tr_torrentSetDirty( tor );
tr_torrentUnlock( tor );
}
2007-01-19 08:36:49 +00:00
void
tr_torrentSetHasPiece( tr_torrent * tor,
tr_piece_index_t pieceIndex,
tr_bool has )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
assert( pieceIndex < tor->info.pieceCount );
if( has )
tr_cpPieceAdd( &tor->completion, pieceIndex );
else
tr_cpPieceRem( &tor->completion, pieceIndex );
}
2007-01-19 08:36:49 +00:00
/***
****
***/
static void
freeTorrent( tr_torrent * tor )
{
tr_torrent * t;
tr_session * session = tor->session;
tr_info * inf = &tor->info;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
assert( !tor->isRunning );
tr_sessionLock( session );
tr_peerMgrRemoveTorrent( tor );
2007-09-23 19:24:06 +00:00
tr_cpDestruct( &tor->completion );
tr_announcerRemoveTorrent( session->announcer, tor );
2008-05-18 16:44:30 +00:00
tr_free( tor->downloadDir );
tr_free( tor->incompleteDir );
2008-12-06 02:21:50 +00:00
tr_free( tor->peer_id );
if( tor == session->torrentList )
session->torrentList = tor->next;
else for( t = session->torrentList; t != NULL; t = t->next ) {
if( t->next == tor ) {
t->next = tor->next;
break;
}
}
assert( session->torrentCount >= 1 );
session->torrentCount--;
tr_bandwidthFree( tor->bandwidth );
2008-11-06 02:56:51 +00:00
tr_metainfoFree( inf );
tr_free( tor );
tr_sessionUnlock( session );
}
/**
*** Start/Stop Callback
**/
static void
torrentStartImpl( void * vtor )
{
time_t now;
2007-11-16 05:43:47 +00:00
tr_torrent * tor = vtor;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_sessionLock( tor->session );
tr_torrentRecheckCompleteness( tor );
now = tr_time( );
tor->isRunning = TRUE;
tor->completeness = tr_cpGetStatus( &tor->completion );
tor->startDate = tor->anyDate = now;
tr_torrentClearError( tor );
tor->finishedSeedingByIdle = FALSE;
tr_torrentResetTransferStats( tor );
tr_announcerTorrentStarted( tor );
tor->dhtAnnounceAt = now + tr_cryptoWeakRandInt( 20 );
tor->dhtAnnounce6At = now + tr_cryptoWeakRandInt( 20 );
tor->lpdAnnounceAt = now;
tr_peerMgrStartTorrent( tor );
tr_sessionUnlock( tor->session );
}
2007-11-16 05:43:47 +00:00
uint64_t
tr_torrentGetCurrentSizeOnDisk( const tr_torrent * tor )
2007-11-16 05:43:47 +00:00
{
tr_file_index_t i;
uint64_t byte_count = 0;
const tr_file_index_t n = tor->info.fileCount;
for( i=0; i<n; ++i )
{
struct stat sb;
char * filename = tr_torrentFindFile( tor, i );
sb.st_size = 0;
if( filename && !stat( filename, &sb ) )
byte_count += sb.st_size;
tr_free( filename );
}
return byte_count;
2007-11-16 05:43:47 +00:00
}
static void
torrentStart( tr_torrent * tor )
2007-07-19 03:48:27 +00:00
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
/* already running... */
if( tor->isRunning )
return;
/* don't allow the torrent to be started if the files disappeared */
if( setLocalErrorIfFilesDisappeared( tor ) )
return;
/* otherwise, start it now... */
tr_sessionLock( tor->session );
/* allow finished torrents to be resumed */
if( tr_torrentIsSeedRatioDone( tor ) ) {
tr_torinf( tor, _( "Restarted manually -- disabling its seed ratio" ) );
tr_torrentSetRatioMode( tor, TR_RATIOLIMIT_UNLIMITED );
}
2007-07-19 03:48:27 +00:00
tr_verifyRemove( tor );
/* corresponds to the peer_id sent as a tracker request parameter.
* one tracker admin says: "When the same torrent is opened and
* closed and opened again without quitting Transmission ...
* change the peerid. It would help sometimes if a stopped event
* was missed to ensure that we didn't think someone was cheating. */
tr_free( tor->peer_id );
tor->peer_id = tr_peerIdNew( );
tor->isRunning = 1;
tr_torrentSetDirty( tor );
tr_runInEventThread( tor->session, torrentStartImpl, tor );
tr_sessionUnlock( tor->session );
2007-07-19 03:48:27 +00:00
}
void
tr_torrentStart( tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
if( tr_isTorrent( tor ) )
torrentStart( tor );
}
static void
torrentRecheckDoneImpl( void * vtor )
{
2009-01-30 00:41:08 +00:00
tr_torrent * tor = vtor;
assert( tr_isTorrent( tor ) );
2009-01-30 00:41:08 +00:00
tr_torrentRecheckCompleteness( tor );
if( tor->startAfterVerify ) {
tor->startAfterVerify = FALSE;
torrentStart( tor );
}
}
static void
torrentRecheckDoneCB( tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_runInEventThread( tor->session, torrentRecheckDoneImpl, tor );
}
static void
verifyTorrent( void * vtor )
{
tr_torrent * tor = vtor;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_sessionLock( tor->session );
2009-01-30 00:41:08 +00:00
/* if the torrent's already being verified, stop it */
tr_verifyRemove( tor );
/* if the torrent's running, stop it & set the restart-after-verify flag */
if( tor->startAfterVerify || tor->isRunning ) {
/* don't clobber isStopping */
const tr_bool startAfter = tor->isStopping ? FALSE : TRUE;
tr_torrentStop( tor );
tor->startAfterVerify = startAfter;
}
if( setLocalErrorIfFilesDisappeared( tor ) )
tor->startAfterVerify = FALSE;
else
tr_verifyAdd( tor, torrentRecheckDoneCB );
tr_sessionUnlock( tor->session );
}
void
tr_torrentVerify( tr_torrent * tor )
{
if( tr_isTorrent( tor ) )
tr_runInEventThread( tor->session, verifyTorrent, tor );
}
void
tr_torrentSave( tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
if( tor->isDirty )
2009-11-24 17:10:40 +00:00
{
tor->isDirty = FALSE;
tr_torrentSaveResume( tor );
}
}
static void
stopTorrent( void * vtor )
{
tr_torrent * tor = vtor;
tr_torinf( tor, "Pausing" );
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_verifyRemove( tor );
tr_peerMgrStopTorrent( tor );
tr_announcerTorrentStopped( tor );
tr_cacheFlushTorrent( tor->session->cache, tor );
tr_fdTorrentClose( tor->session, tor->uniqueId );
if( !tor->isDeleting )
tr_torrentSave( tor );
}
void
tr_torrentStop( tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
2009-01-30 00:41:08 +00:00
if( tr_isTorrent( tor ) )
{
tr_sessionLock( tor->session );
tor->isRunning = 0;
tor->isStopping = 0;
tr_torrentSetDirty( tor );
tr_runInEventThread( tor->session, stopTorrent, tor );
tr_sessionUnlock( tor->session );
}
}
static void
closeTorrent( void * vtor )
{
tr_benc * d;
tr_torrent * tor = vtor;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
d = tr_bencListAddDict( &tor->session->removedTorrents, 2 );
tr_bencDictAddInt( d, "id", tor->uniqueId );
tr_bencDictAddInt( d, "date", tr_time( ) );
tr_torinf( tor, "%s", _( "Removing torrent" ) );
stopTorrent( tor );
if( tor->isDeleting )
{
tr_metainfoRemoveSaved( tor->session, &tor->info );
tr_torrentRemoveResume( tor );
}
tor->isRunning = 0;
freeTorrent( tor );
}
void
tr_torrentFree( tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
if( tr_isTorrent( tor ) )
{
tr_session * session = tor->session;
2009-01-30 00:41:08 +00:00
assert( tr_isSession( session ) );
tr_sessionLock( session );
2007-10-02 02:59:07 +00:00
tr_torrentClearCompletenessCallback( tor );
tr_runInEventThread( session, closeTorrent, tor );
2007-10-02 02:59:07 +00:00
tr_sessionUnlock( session );
}
}
void
tr_torrentRemove( tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tor->isDeleting = 1;
tr_torrentFree( tor );
}
/**
*** Completeness
**/
static const char *
getCompletionString( int type )
{
switch( type )
{
/* Translators: this is a minor point that's safe to skip over, but FYI:
"Complete" and "Done" are specific, different terms in Transmission:
"Complete" means we've downloaded every file in the torrent.
"Done" means we're done downloading the files we wanted, but NOT all
that exist */
case TR_PARTIAL_SEED:
return _( "Done" );
case TR_SEED:
return _( "Complete" );
default:
return _( "Incomplete" );
}
}
static void
fireCompletenessChange( tr_torrent * tor,
tr_completeness status,
tr_bool wasRunning )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
assert( ( status == TR_LEECH )
|| ( status == TR_SEED )
|| ( status == TR_PARTIAL_SEED ) );
if( tor->completeness_func )
tor->completeness_func( tor, status, wasRunning,
tor->completeness_func_user_data );
}
void
tr_torrentSetCompletenessCallback( tr_torrent * tor,
tr_torrent_completeness_func func,
void * user_data )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tor->completeness_func = func;
tor->completeness_func_user_data = user_data;
}
void
tr_torrentClearCompletenessCallback( tr_torrent * torrent )
{
tr_torrentSetCompletenessCallback( torrent, NULL, NULL );
}
void
tr_torrentSetRatioLimitHitCallback( tr_torrent * tor,
tr_torrent_ratio_limit_hit_func func,
void * user_data )
{
assert( tr_isTorrent( tor ) );
tor->ratio_limit_hit_func = func;
tor->ratio_limit_hit_func_user_data = user_data;
}
void
tr_torrentClearRatioLimitHitCallback( tr_torrent * torrent )
{
tr_torrentSetRatioLimitHitCallback( torrent, NULL, NULL );
}
void
tr_torrentSetIdleLimitHitCallback( tr_torrent * tor,
tr_torrent_idle_limit_hit_func func,
void * user_data )
{
assert( tr_isTorrent( tor ) );
tor->idle_limit_hit_func = func;
tor->idle_limit_hit_func_user_data = user_data;
}
void
tr_torrentClearIdleLimitHitCallback( tr_torrent * torrent )
{
tr_torrentSetIdleLimitHitCallback( torrent, NULL, NULL );
}
static void
onSigCHLD( int i UNUSED )
{
waitpid( -1, 0, WNOHANG );
}
static void
torrentCallScript( const tr_torrent * tor, const char * script )
{
char timeStr[128];
const time_t now = tr_time( );
assert( tr_isTorrent( tor ) );
tr_strlcpy( timeStr, ctime( &now ), sizeof( timeStr ) );
*strchr( timeStr,'\n' ) = '\0';
if( script && *script )
{
char * cmd[] = { tr_strdup( script ), NULL };
char * env[] = {
tr_strdup_printf( "TR_APP_VERSION=%s", SHORT_VERSION_STRING ),
tr_strdup_printf( "TR_TIME_LOCALTIME=%s", timeStr ),
tr_strdup_printf( "TR_TORRENT_DIR=%s", tor->currentDir ),
tr_strdup_printf( "TR_TORRENT_ID=%d", tr_torrentId( tor ) ),
tr_strdup_printf( "TR_TORRENT_HASH=%s", tor->info.hashString ),
tr_strdup_printf( "TR_TORRENT_NAME=%s", tr_torrentName( tor ) ),
NULL };
tr_torinf( tor, "Calling script \"%s\"", script );
signal( SIGCHLD, onSigCHLD );
if( !fork( ) )
{
execve( script, cmd, env );
_exit( 0 );
}
}
}
void
tr_torrentRecheckCompleteness( tr_torrent * tor )
{
tr_completeness completeness;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_torrentLock( tor );
completeness = tr_cpGetStatus( &tor->completion );
if( completeness != tor->completeness )
{
const int recentChange = tor->downloadedCur != 0;
const tr_bool wasLeeching = !tr_torrentIsSeed( tor );
const tr_bool wasRunning = tor->isRunning;
if( recentChange )
{
2008-04-21 15:22:54 +00:00
tr_torinf( tor, _( "State changed from \"%1$s\" to \"%2$s\"" ),
getCompletionString( tor->completeness ),
getCompletionString( completeness ) );
}
tor->completeness = completeness;
tr_fdTorrentClose( tor->session, tor->uniqueId );
if( tr_torrentIsSeed( tor ) )
{
if( recentChange )
{
tr_announcerTorrentCompleted( tor );
tor->doneDate = tor->anyDate = tr_time( );
}
if( wasLeeching && wasRunning )
{
/* clear interested flag on all peers */
tr_peerMgrClearInterest( tor );
/* if completeness was TR_LEECH then the seed limit check will have been skipped in bandwidthPulse */
tr_torrentCheckSeedLimit( tor );
}
if( tor->currentDir == tor->incompleteDir )
tr_torrentSetLocation( tor, tor->downloadDir, TRUE, NULL, NULL );
if( tr_sessionIsTorrentDoneScriptEnabled( tor->session ) )
torrentCallScript( tor, tr_sessionGetTorrentDoneScript( tor->session ) );
}
fireCompletenessChange( tor, wasRunning, completeness );
tr_torrentSetDirty( tor );
}
tr_torrentUnlock( tor );
2007-01-19 08:36:49 +00:00
}
/***
****
***/
static void
tr_torrentFireMetadataCompleted( tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
if( tor->metadata_func )
tor->metadata_func( tor, tor->metadata_func_user_data );
}
void
tr_torrentSetMetadataCallback( tr_torrent * tor,
tr_torrent_metadata_func func,
void * user_data )
{
assert( tr_isTorrent( tor ) );
tor->metadata_func = func;
tor->metadata_func_user_data = user_data;
}
/**
*** File priorities
**/
2007-12-25 06:37:21 +00:00
void
tr_torrentInitFilePriority( tr_torrent * tor,
tr_file_index_t fileIndex,
tr_priority_t priority )
{
tr_piece_index_t i;
tr_file * file;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
assert( fileIndex < tor->info.fileCount );
assert( tr_isPriority( priority ) );
file = &tor->info.files[fileIndex];
file->priority = priority;
for( i = file->firstPiece; i <= file->lastPiece; ++i )
tor->info.pieces[i].priority = calculatePiecePriority( tor, i, fileIndex );
}
void
tr_torrentSetFilePriorities( tr_torrent * tor,
const tr_file_index_t * files,
tr_file_index_t fileCount,
tr_priority_t priority )
{
tr_file_index_t i;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_torrentLock( tor );
for( i = 0; i < fileCount; ++i )
if( files[i] < tor->info.fileCount )
tr_torrentInitFilePriority( tor, files[i], priority );
tr_torrentSetDirty( tor );
tr_peerMgrRebuildRequests( tor );
tr_torrentUnlock( tor );
}
tr_priority_t*
tr_torrentGetFilePriorities( const tr_torrent * tor )
{
tr_file_index_t i;
tr_priority_t * p;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_torrentLock( tor );
p = tr_new0( tr_priority_t, tor->info.fileCount );
for( i = 0; i < tor->info.fileCount; ++i )
p[i] = tor->info.files[i].priority;
tr_torrentUnlock( tor );
return p;
}
/**
*** File DND
**/
static void
setFileDND( tr_torrent * tor, tr_file_index_t fileIndex, int doDownload )
{
tr_file * file;
const int8_t dnd = !doDownload;
tr_piece_index_t firstPiece;
int8_t firstPieceDND;
tr_piece_index_t lastPiece;
int8_t lastPieceDND;
tr_file_index_t i;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
file = &tor->info.files[fileIndex];
2007-07-16 23:52:05 +00:00
file->dnd = dnd;
firstPiece = file->firstPiece;
lastPiece = file->lastPiece;
/* can't set the first piece to DND unless
every file using that piece is DND */
firstPieceDND = dnd;
if( fileIndex > 0 )
{
for( i = fileIndex - 1; firstPieceDND; --i )
{
2008-03-24 16:10:04 +00:00
if( tor->info.files[i].lastPiece != firstPiece )
break;
firstPieceDND = tor->info.files[i].dnd;
if( !i )
break;
}
}
/* can't set the last piece to DND unless
every file using that piece is DND */
lastPieceDND = dnd;
for( i = fileIndex + 1; lastPieceDND && i < tor->info.fileCount; ++i )
{
if( tor->info.files[i].firstPiece != lastPiece )
break;
lastPieceDND = tor->info.files[i].dnd;
}
if( firstPiece == lastPiece )
{
tor->info.pieces[firstPiece].dnd = firstPieceDND && lastPieceDND;
}
else
{
tr_piece_index_t pp;
tor->info.pieces[firstPiece].dnd = firstPieceDND;
tor->info.pieces[lastPiece].dnd = lastPieceDND;
for( pp = firstPiece + 1; pp < lastPiece; ++pp )
tor->info.pieces[pp].dnd = dnd;
}
}
void
tr_torrentInitFileDLs( tr_torrent * tor,
const tr_file_index_t * files,
tr_file_index_t fileCount,
tr_bool doDownload )
{
tr_file_index_t i;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_torrentLock( tor );
for( i=0; i<fileCount; ++i )
if( files[i] < tor->info.fileCount )
setFileDND( tor, files[i], doDownload );
tr_cpInvalidateDND( &tor->completion );
tr_torrentUnlock( tor );
}
void
tr_torrentSetFileDLs( tr_torrent * tor,
const tr_file_index_t * files,
tr_file_index_t fileCount,
tr_bool doDownload )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tr_torrentLock( tor );
tr_torrentInitFileDLs( tor, files, fileCount, doDownload );
tr_torrentSetDirty( tor );
tr_peerMgrRebuildRequests( tor );
tr_torrentUnlock( tor );
}
/***
****
***/
tr_priority_t
tr_torrentGetPriority( const tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
return tor->bandwidth->priority;
}
void
tr_torrentSetPriority( tr_torrent * tor, tr_priority_t priority )
{
assert( tr_isTorrent( tor ) );
assert( tr_isPriority( priority ) );
if( tor->bandwidth->priority != priority )
{
tor->bandwidth->priority = priority;
tr_torrentSetDirty( tor );
}
}
/***
****
***/
void
tr_torrentSetPeerLimit( tr_torrent * tor,
uint16_t maxConnectedPeers )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
if ( tor->maxConnectedPeers != maxConnectedPeers )
{
tor->maxConnectedPeers = maxConnectedPeers;
tr_torrentSetDirty( tor );
}
}
uint16_t
tr_torrentGetPeerLimit( const tr_torrent * tor )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
return tor->maxConnectedPeers;
}
/***
****
***/
tr_block_index_t
_tr_block( const tr_torrent * tor,
tr_piece_index_t index,
uint32_t offset )
{
tr_block_index_t ret;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
ret = index;
2009-01-30 00:41:08 +00:00
ret *= ( tor->info.pieceSize / tor->blockSize );
ret += offset / tor->blockSize;
return ret;
}
tr_bool
tr_torrentReqIsValid( const tr_torrent * tor,
tr_piece_index_t index,
uint32_t offset,
uint32_t length )
{
int err = 0;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
if( index >= tor->info.pieceCount )
err = 1;
2008-10-27 17:35:22 +00:00
else if( length < 1 )
err = 2;
2008-10-27 04:39:30 +00:00
else if( ( offset + length ) > tr_torPieceCountBytes( tor, index ) )
err = 3;
2008-10-27 04:39:30 +00:00
else if( length > MAX_BLOCK_SIZE )
err = 4;
2008-10-27 04:39:30 +00:00
else if( tr_pieceOffset( tor, index, offset, length ) > tor->info.totalSize )
err = 5;
if( err ) tr_tordbg( tor, "index %lu offset %lu length %lu err %d\n",
(unsigned long)index,
(unsigned long)offset,
(unsigned long)length,
err );
return !err;
}
uint64_t
tr_pieceOffset( const tr_torrent * tor,
tr_piece_index_t index,
uint32_t offset,
uint32_t length )
{
uint64_t ret;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
ret = tor->info.pieceSize;
ret *= index;
ret += offset;
ret += length;
return ret;
}
2008-01-02 18:05:05 +00:00
/***
****
***/
void
tr_torrentSetPieceChecked( tr_torrent * tor, tr_piece_index_t pieceIndex )
2008-01-02 18:05:05 +00:00
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
assert( pieceIndex < tor->info.pieceCount );
2009-01-30 00:41:08 +00:00
tr_tordbg( tor, "[LAZY] setting piece %zu timeChecked to now", (size_t)pieceIndex );
tor->info.pieces[pieceIndex].timeChecked = tr_time( );
2008-01-02 18:05:05 +00:00
}
void
tr_torrentSetChecked( tr_torrent * tor, time_t when )
2008-01-02 18:05:05 +00:00
{
tr_piece_index_t i, n;
2008-01-02 18:05:05 +00:00
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
for( i=0, n=tor->info.pieceCount; i!=n; ++i )
tor->info.pieces[i].timeChecked = when;
2008-01-02 18:05:05 +00:00
}
tr_bool
tr_torrentCheckPiece( tr_torrent * tor, tr_piece_index_t pieceIndex )
{
const tr_bool pass = tr_ioTestPiece( tor, pieceIndex );
2009-01-30 00:41:08 +00:00
tr_tordbg( tor, "[LAZY] tr_torrentCheckPiece tested piece %zu, pass==%d", (size_t)pieceIndex, (int)pass );
tr_torrentSetHasPiece( tor, pieceIndex, pass );
tr_torrentSetPieceChecked( tor, pieceIndex );
tor->anyDate = tr_time( );
tr_torrentSetDirty( tor );
2009-01-30 00:41:08 +00:00
return pass;
2008-01-02 18:05:05 +00:00
}
2008-04-14 11:52:50 +00:00
static time_t
getFileMTime( const tr_torrent * tor, tr_file_index_t i )
2008-04-14 11:52:50 +00:00
{
struct stat sb;
time_t mtime = 0;
char * path = tr_torrentFindFile( tor, i );
2009-01-30 00:41:08 +00:00
if( ( path != NULL ) && !stat( path, &sb ) && S_ISREG( sb.st_mode ) )
{
2008-04-14 11:52:50 +00:00
#ifdef SYS_DARWIN
mtime = sb.st_mtimespec.tv_sec;
2008-04-14 11:52:50 +00:00
#else
mtime = sb.st_mtime;
2008-04-14 11:52:50 +00:00
#endif
}
tr_free( path );
return mtime;
}
tr_bool
tr_torrentPieceNeedsCheck( const tr_torrent * tor, tr_piece_index_t p )
{
uint64_t unused;
tr_file_index_t f;
const tr_info * inf = tr_torrentInfo( tor );
/* if we've never checked this piece, then it needs to be checked */
if( !inf->pieces[p].timeChecked ) {
tr_tordbg( tor, "[LAZY] piece %zu needs to be tested because it's never been tested", (size_t)p );
return TRUE;
}
/* If we think we've completed one of the files in this piece,
* but it's been modified since we last checked it,
* then it needs to be rechecked */
tr_ioFindFileLocation( tor, p, 0, &f, &unused );
for( ; f < inf->fileCount && pieceHasFile( p, &inf->files[f] ); ++f ) {
if( tr_cpFileIsComplete( &tor->completion, f ) ) {
if( getFileMTime( tor, f ) > inf->pieces[p].timeChecked ) {
tr_tordbg( tor, "[LAZY] piece %zu needs to be tested because file %zu mtime is newer than check time %zu", (size_t)p, (size_t)f, (size_t)inf->pieces[p].timeChecked );
return TRUE;
}
2008-04-14 11:52:50 +00:00
}
}
tr_tordbg( tor, "[LAZY] piece %zu does not need to be tested", (size_t)p );
return FALSE;
2008-04-14 11:52:50 +00:00
}
/***
****
***/
static int
compareTrackerByTier( const void * va, const void * vb )
{
const tr_tracker_info * a = va;
const tr_tracker_info * b = vb;
/* sort by tier */
if( a->tier != b->tier )
return a->tier - b->tier;
/* get the effects of a stable sort by comparing the two elements' addresses */
return a - b;
}
tr_bool
tr_torrentSetAnnounceList( tr_torrent * tor,
const tr_tracker_info * trackers_in,
int trackerCount )
{
int i;
tr_benc metainfo;
tr_bool ok = TRUE;
tr_tracker_info * trackers;
tr_torrentLock( tor );
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
/* ensure the trackers' tiers are in ascending order */
trackers = tr_memdup( trackers_in, sizeof( tr_tracker_info ) * trackerCount );
qsort( trackers, trackerCount, sizeof( tr_tracker_info ), compareTrackerByTier );
/* look for bad URLs */
for( i=0; ok && i<trackerCount; ++i )
if( !tr_urlIsValidTracker( trackers[i].announce ) )
ok = FALSE;
/* save to the .torrent file */
if( ok && !tr_bencLoadFile( &metainfo, TR_FMT_BENC, tor->info.torrent ) )
{
tr_bool hasInfo;
tr_info tmpInfo;
/* remove the old fields */
tr_bencDictRemove( &metainfo, "announce" );
tr_bencDictRemove( &metainfo, "announce-list" );
/* add the new fields */
if( trackerCount > 0 )
{
tr_bencDictAddStr( &metainfo, "announce", trackers[0].announce );
}
if( trackerCount > 1 )
{
int i;
int prevTier = -1;
tr_benc * tier = NULL;
tr_benc * announceList = tr_bencDictAddList( &metainfo, "announce-list", 0 );
for( i=0; i<trackerCount; ++i ) {
if( prevTier != trackers[i].tier ) {
prevTier = trackers[i].tier;
tier = tr_bencListAddList( announceList, 0 );
}
tr_bencListAddStr( tier, trackers[i].announce );
}
}
/* try to parse it back again, to make sure it's good */
memset( &tmpInfo, 0, sizeof( tr_info ) );
if( tr_metainfoParse( tor->session, &metainfo, &tmpInfo,
&hasInfo, &tor->infoDictLength ) )
{
/* it's good, so keep these new trackers and free the old ones */
tr_info swap;
swap.trackers = tor->info.trackers;
swap.trackerCount = tor->info.trackerCount;
tor->info.trackers = tmpInfo.trackers;
tor->info.trackerCount = tmpInfo.trackerCount;
tmpInfo.trackers = swap.trackers;
tmpInfo.trackerCount = swap.trackerCount;
tr_metainfoFree( &tmpInfo );
tr_bencToFile( &metainfo, TR_FMT_BENC, tor->info.torrent );
}
/* cleanup */
tr_bencFree( &metainfo );
/* if we had a tracker-related error on this torrent,
* and that tracker's been removed,
* then clear the error */
if( ( tor->error == TR_STAT_TRACKER_WARNING )
|| ( tor->error == TR_STAT_TRACKER_ERROR ) )
{
tr_bool clear = TRUE;
for( i=0; clear && i<trackerCount; ++i )
if( !strcmp( trackers[i].announce, tor->errorTracker ) )
clear = FALSE;
if( clear )
tr_torrentClearError( tor );
}
/* tell the announcer to reload this torrent's tracker list */
tr_announcerResetTorrent( tor->session->announcer, tor );
}
tr_torrentUnlock( tor );
tr_free( trackers );
return ok;
}
/**
***
**/
void
tr_torrentSetAddedDate( tr_torrent * tor,
time_t t )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tor->addedDate = t;
tor->anyDate = MAX( tor->anyDate, tor->addedDate );
}
void
tr_torrentSetActivityDate( tr_torrent * tor, time_t t )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
tor->activityDate = t;
tor->anyDate = MAX( tor->anyDate, tor->activityDate );
}
2008-06-03 19:42:54 +00:00
void
tr_torrentSetDoneDate( tr_torrent * tor,
time_t t )
2008-06-03 19:42:54 +00:00
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
2008-06-03 19:42:54 +00:00
tor->doneDate = t;
tor->anyDate = MAX( tor->anyDate, tor->doneDate );
2008-06-03 19:42:54 +00:00
}
/**
***
**/
uint64_t
tr_torrentGetBytesLeftToAllocate( const tr_torrent * tor )
{
tr_file_index_t i;
uint64_t bytesLeft = 0;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
for( i=0; i<tor->info.fileCount; ++i )
{
if( !tor->info.files[i].dnd )
{
struct stat sb;
const uint64_t length = tor->info.files[i].length;
char * path = tr_torrentFindFile( tor, i );
bytesLeft += length;
if( ( path != NULL ) && !stat( path, &sb )
&& S_ISREG( sb.st_mode )
&& ( (uint64_t)sb.st_size <= length ) )
bytesLeft -= sb.st_size;
tr_free( path );
}
}
return bytesLeft;
}
/****
***** Removing the torrent's local data
****/
static int
vstrcmp( const void * a, const void * b )
{
return strcmp( a, b );
}
static int
compareLongestFirst( const void * a, const void * b )
{
const size_t alen = strlen( a );
const size_t blen = strlen( b );
if( alen != blen )
return alen > blen ? -1 : 1;
return vstrcmp( a, b );
}
static void
addDirtyFile( const char * root,
const char * filename,
tr_ptrArray * dirtyFolders )
{
char * dir = tr_dirname( filename );
/* add the parent folders to dirtyFolders until we reach the root or a known-dirty */
while ( ( dir != NULL )
&& ( strlen( root ) <= strlen( dir ) )
&& ( tr_ptrArrayFindSorted( dirtyFolders, dir, vstrcmp ) == NULL ) )
{
char * tmp;
tr_ptrArrayInsertSorted( dirtyFolders, tr_strdup( dir ), vstrcmp );
tmp = tr_dirname( dir );
tr_free( dir );
dir = tmp;
}
tr_free( dir );
}
static void
walkLocalData( const tr_torrent * tor,
const char * root,
const char * dir,
const char * base,
tr_ptrArray * torrentFiles,
tr_ptrArray * folders,
tr_ptrArray * dirtyFolders )
{
int i;
struct stat sb;
char * buf;
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
buf = tr_buildPath( dir, base, NULL );
i = stat( buf, &sb );
if( !i )
{
DIR * odir = NULL;
if( S_ISDIR( sb.st_mode ) && ( ( odir = opendir ( buf ) ) ) )
{
struct dirent *d;
tr_ptrArrayInsertSorted( folders, tr_strdup( buf ), vstrcmp );
for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
if( d->d_name && strcmp( d->d_name, "." ) && strcmp( d->d_name, ".." ) )
walkLocalData( tor, root, buf, d->d_name, torrentFiles, folders, dirtyFolders );
closedir( odir );
}
else if( S_ISREG( sb.st_mode ) && ( sb.st_size > 0 ) )
{
const char * sub = buf + strlen( tor->currentDir ) + strlen( TR_PATH_DELIMITER_STR );
const tr_bool isTorrentFile = tr_ptrArrayFindSorted( torrentFiles, sub, vstrcmp ) != NULL;
if( !isTorrentFile )
addDirtyFile( root, buf, dirtyFolders );
}
}
tr_free( buf );
}
static void
deleteLocalFile( const char * filename, tr_fileFunc fileFunc )
{
struct stat sb;
if( !stat( filename, &sb ) ) /* if file exists... */
fileFunc( filename );
}
static void
deleteLocalData( tr_torrent * tor, tr_fileFunc fileFunc )
{
int i, n;
char ** s;
tr_file_index_t f;
tr_ptrArray torrentFiles = TR_PTR_ARRAY_INIT;
tr_ptrArray folders = TR_PTR_ARRAY_INIT;
tr_ptrArray dirtyFolders = TR_PTR_ARRAY_INIT; /* dirty == contains non-torrent files */
const char * firstFile = tor->info.files[0].name;
const char * cpch = strchr( firstFile, TR_PATH_DELIMITER );
char * tmp = cpch ? tr_strndup( firstFile, cpch - firstFile ) : NULL;
char * root = tr_buildPath( tor->currentDir, tmp, NULL );
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
for( f=0; f<tor->info.fileCount; ++f ) {
tr_ptrArrayInsertSorted( &torrentFiles, tr_strdup( tor->info.files[f].name ), vstrcmp );
tr_ptrArrayInsertSorted( &torrentFiles, tr_torrentBuildPartial( tor, f ), vstrcmp );
}
/* build the set of folders and dirtyFolders */
walkLocalData( tor, root, root, NULL, &torrentFiles, &folders, &dirtyFolders );
/* try to remove entire folders first, so that the recycle bin will be tidy */
s = (char**) tr_ptrArrayPeek( &folders, &n );
for( i=0; i<n; ++i )
if( tr_ptrArrayFindSorted( &dirtyFolders, s[i], vstrcmp ) == NULL )
deleteLocalFile( s[i], fileFunc );
/* now blow away any remaining torrent files, such as torrent files in dirty folders */
for( i=0, n=tr_ptrArraySize( &torrentFiles ); i<n; ++i ) {
char * path = tr_buildPath( tor->currentDir, tr_ptrArrayNth( &torrentFiles, i ), NULL );
deleteLocalFile( path, fileFunc );
tr_free( path );
}
/* Now clean out the directories left empty from the previous step.
* Work from deepest to shallowest s.t. lower folders
* won't prevent the upper folders from being deleted */
{
tr_ptrArray cleanFolders = TR_PTR_ARRAY_INIT;
s = (char**) tr_ptrArrayPeek( &folders, &n );
for( i=0; i<n; ++i )
if( tr_ptrArrayFindSorted( &dirtyFolders, s[i], vstrcmp ) == NULL )
tr_ptrArrayInsertSorted( &cleanFolders, s[i], compareLongestFirst );
s = (char**) tr_ptrArrayPeek( &cleanFolders, &n );
for( i=0; i<n; ++i ) {
#ifdef SYS_DARWIN
char * dsStore = tr_buildPath( s[i], ".DS_Store", NULL );
deleteLocalFile( dsStore, fileFunc );
tr_free( dsStore );
#endif
deleteLocalFile( s[i], fileFunc );
}
tr_ptrArrayDestruct( &cleanFolders, NULL );
}
/* cleanup */
tr_ptrArrayDestruct( &dirtyFolders, tr_free );
tr_ptrArrayDestruct( &folders, tr_free );
tr_ptrArrayDestruct( &torrentFiles, tr_free );
tr_free( root );
tr_free( tmp );
}
void
tr_torrentDeleteLocalData( tr_torrent * tor, tr_fileFunc fileFunc )
{
2009-01-30 00:41:08 +00:00
assert( tr_isTorrent( tor ) );
if( fileFunc == NULL )
fileFunc = remove;
/* close all the files because we're about to delete them */
tr_cacheFlushTorrent( tor->session->cache, tor );
tr_fdTorrentClose( tor->session, tor->uniqueId );
if( tor->info.fileCount > 1 )
{
deleteLocalData( tor, fileFunc );
}
else if( tor->info.fileCount == 1 )
{
char * tmp;
/* torrent only has one file */
char * path = tr_buildPath( tor->currentDir, tor->info.files[0].name, NULL );
deleteLocalFile( path, fileFunc );
tr_free( path );
tmp = tr_torrentBuildPartial( tor, 0 );
path = tr_buildPath( tor->currentDir, tmp, NULL );
deleteLocalFile( path, fileFunc );
tr_free( path );
tr_free( tmp );
}
}
/***
****
***/
struct LocationData
{
tr_bool move_from_old_location;
int * setme_state;
double * setme_progress;
char * location;
tr_torrent * tor;
};
static tr_bool
isSameLocation( const char * path1, const char * path2 )
{
struct stat s1, s2;
const int err1 = stat( path1, &s1 );
const int err2 = stat( path2, &s2 );
if( !err1 && !err2 ) {
tr_dbg( "path1 dev:inode is %"PRIu64":%"PRIu64"; "
"path2 dev:inode is %"PRIu64":%"PRIu64,
(uint64_t)s1.st_dev, (uint64_t)s1.st_ino,
(uint64_t)s2.st_dev, (uint64_t)s2.st_ino );
return ( s1.st_dev == s2.st_dev )
&& ( s1.st_ino == s2.st_ino );
}
/* either one, or the other, or both don't exist... */
tr_dbg( "stat(%s) returned %d\n", path1, err1 );
tr_dbg( "stat(%s) returned %d\n", path2, err2 );
return FALSE;
}
static void
setLocation( void * vdata )
{
tr_bool err = FALSE;
struct LocationData * data = vdata;
tr_torrent * tor = data->tor;
const tr_bool do_move = data->move_from_old_location;
const char * location = data->location;
double bytesHandled = 0;
assert( tr_isTorrent( tor ) );
tr_dbg( "Moving \"%s\" location from currentDir \"%s\" to \"%s\"",
tr_torrentName(tor), tor->currentDir, location );
tr_mkdirp( location, 0777 );
if( !isSameLocation( location, tor->currentDir ) )
{
tr_file_index_t i;
/* bad idea to move files while they're being verified... */
tr_verifyRemove( tor );
/* try to move the files.
* FIXME: there are still all kinds of nasty cases, like what
* if the target directory runs out of space halfway through... */
for( i=0; !err && i<tor->info.fileCount; ++i )
{
const tr_file * f = &tor->info.files[i];
const char * oldbase;
char * sub;
if( tr_torrentFindFile2( tor, i, &oldbase, &sub ) )
{
char * oldpath = tr_buildPath( oldbase, sub, NULL );
char * newpath = tr_buildPath( location, sub, NULL );
tr_dbg( "Found file #%d: %s", (int)i, oldpath );
if( do_move )
{
tr_bool renamed = FALSE;
errno = 0;
tr_torinf( tor, "moving \"%s\" to \"%s\"", oldpath, newpath );
if( tr_moveFile( oldpath, newpath, &renamed ) )
{
err = TRUE;
tr_torerr( tor, "error moving \"%s\" to \"%s\": %s",
oldpath, newpath, tr_strerror( errno ) );
}
}
tr_free( newpath );
tr_free( oldpath );
tr_free( sub );
}
if( data->setme_progress )
{
bytesHandled += f->length;
*data->setme_progress = bytesHandled / tor->info.totalSize;
}
}
if( !err )
{
/* blow away the leftover subdirectories in the old location */
if( do_move )
tr_torrentDeleteLocalData( tor, remove );
/* set the new location and reverify */
tr_torrentSetDownloadDir( tor, location );
}
}
if( !err && do_move )
{
tr_free( tor->incompleteDir );
tor->incompleteDir = NULL;
tor->currentDir = tor->downloadDir;
}
if( data->setme_state )
*data->setme_state = err ? TR_LOC_ERROR : TR_LOC_DONE;
/* cleanup */
tr_free( data->location );
tr_free( data );
}
void
tr_torrentSetLocation( tr_torrent * tor,
const char * location,
tr_bool move_from_old_location,
double * setme_progress,
int * setme_state )
{
struct LocationData * data;
assert( tr_isTorrent( tor ) );
if( setme_state )
*setme_state = TR_LOC_MOVING;
if( setme_progress )
*setme_progress = 0;
/* run this in the libtransmission thread */
data = tr_new( struct LocationData, 1 );
data->tor = tor;
data->location = tr_strdup( location );
data->move_from_old_location = move_from_old_location;
data->setme_state = setme_state;
data->setme_progress = setme_progress;
tr_runInEventThread( tor->session, setLocation, data );
}
/***
****
***/
void
tr_torrentFileCompleted( tr_torrent * tor, tr_file_index_t fileNum )
{
char * sub;
const char * base;
/* close the file so that we can reopen in read-only mode as needed */
tr_fdFileClose( tor->session, tor, fileNum );
/* if the torrent's current filename isn't the same as the one in the
* metadata -- for example, if it had the ".part" suffix appended to
* it until now -- then rename it to match the one in the metadata */
if( tr_torrentFindFile2( tor, fileNum, &base, &sub ) )
{
const tr_file * file = &tor->info.files[fileNum];
if( strcmp( sub, file->name ) )
{
char * oldpath = tr_buildPath( base, sub, NULL );
char * newpath = tr_buildPath( base, file->name, NULL );
if( rename( oldpath, newpath ) )
tr_torerr( tor, "Error moving \"%s\" to \"%s\": %s", oldpath, newpath, tr_strerror( errno ) );
tr_free( newpath );
tr_free( oldpath );
}
tr_free( sub );
}
}
/***
****
***/
static tr_bool
fileExists( const char * filename )
{
struct stat sb;
const tr_bool ok = !stat( filename, &sb );
return ok;
}
tr_bool
tr_torrentFindFile2( const tr_torrent * tor, tr_file_index_t fileNum,
const char ** base, char ** subpath )
{
char * part;
const tr_file * file;
const char * b = NULL;
const char * s = NULL;
assert( tr_isTorrent( tor ) );
assert( fileNum < tor->info.fileCount );
file = &tor->info.files[fileNum];
part = tr_torrentBuildPartial( tor, fileNum );
if( b == NULL ) {
char * filename = tr_buildPath( tor->downloadDir, file->name, NULL );
if( fileExists( filename ) ) {
b = tor->downloadDir;
s = file->name;
}
tr_free( filename );
}
if( ( b == NULL ) && ( tor->incompleteDir != NULL ) ) {
char * filename = tr_buildPath( tor->incompleteDir, file->name, NULL );
if( fileExists( filename ) ) {
b = tor->incompleteDir;
s = file->name;
}
tr_free( filename );
}
if( ( b == NULL ) && ( tor->incompleteDir != NULL ) ) {
char * filename = tr_buildPath( tor->incompleteDir, part, NULL );
if( fileExists( filename ) ) {
b = tor->incompleteDir;
s = part;
}
tr_free( filename );
}
if( b == NULL) {
char * filename = tr_buildPath( tor->downloadDir, part, NULL );
if( fileExists( filename ) ) {
b = tor->downloadDir;
s = part;
}
tr_free( filename );
}
if( base != NULL )
*base = b;
if( subpath != NULL )
*subpath = tr_strdup( s );
tr_free( part );
return b != NULL;
}
char*
tr_torrentFindFile( const tr_torrent * tor, tr_file_index_t fileNum )
{
char * subpath;
char * ret = NULL;
const char * base;
if( tr_torrentFindFile2( tor, fileNum, &base, &subpath ) )
{
ret = tr_buildPath( base, subpath, NULL );
tr_free( subpath );
}
return ret;
}
/* Decide whether we should be looking for files in downloadDir or incompleteDir. */
static void
refreshCurrentDir( tr_torrent * tor )
{
const char * dir = NULL;
if( tor->incompleteDir == NULL )
dir = tor->downloadDir;
else if( !tr_torrentHasMetadata( tor ) ) /* no files to find */
dir = tor->incompleteDir;
else if( !tr_torrentFindFile2( tor, 0, &dir, NULL ) )
dir = tor->incompleteDir;
assert( dir != NULL );
assert( ( dir == tor->downloadDir ) || ( dir == tor->incompleteDir ) );
tor->currentDir = dir;
}
char*
tr_torrentBuildPartial( const tr_torrent * tor, tr_file_index_t fileNum )
{
return tr_strdup_printf( "%s.part", tor->info.files[fileNum].name );
}