2009-01-07 06:53:29 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
|
2007-01-19 08:36:49 +00:00
|
|
|
*
|
2009-01-07 06:53:29 +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
|
|
|
*
|
2009-01-07 06:53:29 +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 <unistd.h> /* stat */
|
2008-12-23 16:04:11 +00:00
|
|
|
#include <dirent.h>
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2008-05-05 19:33:29 +00:00
|
|
|
#include <limits.h> /* INT_MAX */
|
2008-02-19 18:39:49 +00:00
|
|
|
#include <string.h> /* memcmp */
|
2008-05-05 18:54:19 +00:00
|
|
|
#include <stdlib.h> /* qsort */
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2009-01-12 19:58:16 +00:00
|
|
|
#include <event.h> /* evbuffer */
|
|
|
|
|
2007-01-19 08:36:49 +00:00
|
|
|
#include "transmission.h"
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.h"
|
2008-11-24 04:21:23 +00:00
|
|
|
#include "bandwidth.h"
|
2007-12-21 22:18:40 +00:00
|
|
|
#include "bencode.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "completion.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "crypto.h" /* for tr_sha1 */
|
2008-04-13 14:29:11 +00:00
|
|
|
#include "resume.h"
|
2007-11-21 16:16:59 +00:00
|
|
|
#include "fdlimit.h" /* tr_fdFileClose */
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "metainfo.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-mgr.h"
|
2008-12-23 16:04:11 +00:00
|
|
|
#include "platform.h" /* TR_PATH_DELIMITER_STR */
|
|
|
|
#include "ptrarray.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "ratecontrol.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "tracker.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "trevent.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2008-02-15 16:00:46 +00:00
|
|
|
#include "verify.h"
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
#define MAX_BLOCK_SIZE ( 1024 * 16 )
|
2008-03-13 00:38:16 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/***
|
2007-09-20 16:32:01 +00:00
|
|
|
****
|
2007-06-26 18:45:03 +00:00
|
|
|
***/
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
int
|
|
|
|
tr_torrentId( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
return tor->uniqueId;
|
|
|
|
}
|
|
|
|
|
2008-05-12 00:41:55 +00:00
|
|
|
tr_torrent*
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrentFindFromId( tr_session * session, int id )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
|
|
|
tr_torrent * tor = NULL;
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
while( ( tor = tr_torrentNext( session, tor ) ) )
|
2008-05-12 00:41:55 +00:00
|
|
|
if( tor->uniqueId == id )
|
|
|
|
return tor;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent*
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrentFindFromHashString( tr_session * session, const char * str )
|
2008-05-12 00:41:55 +00:00
|
|
|
{
|
|
|
|
tr_torrent * tor = NULL;
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
while( ( tor = tr_torrentNext( session, tor ) ) )
|
2008-05-12 00:41:55 +00:00
|
|
|
if( !strcmp( str, tor->info.hashString ) )
|
|
|
|
return tor;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent*
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrentFindFromHash( tr_session * session, const uint8_t * torrentHash )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-05-10 00:19:00 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
while( ( tor = tr_torrentNext( session, tor ) ) )
|
2008-11-03 17:01:08 +00:00
|
|
|
if( *tor->info.hash == *torrentHash )
|
|
|
|
if( !memcmp( tor->info.hash, torrentHash, SHA_DIGEST_LENGTH ) )
|
|
|
|
return tor;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent*
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrentFindFromObfuscatedHash( tr_session * session,
|
2008-09-23 19:11:04 +00:00
|
|
|
const uint8_t * obfuscatedTorrentHash )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2008-05-10 00:19:00 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
while( ( tor = tr_torrentNext( session, tor ) ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !memcmp( tor->obfuscatedHash, obfuscatedTorrentHash,
|
|
|
|
SHA_DIGEST_LENGTH ) )
|
2007-09-20 16:32:01 +00:00
|
|
|
return tor;
|
|
|
|
|
|
|
|
return NULL;
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** PER-TORRENT UL / DL SPEEDS
|
|
|
|
***/
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentSetSpeedMode( tr_torrent * tor,
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_direction dir,
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_speedlimit mode )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
assert( tor != NULL );
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
assert( mode==TR_SPEEDLIMIT_GLOBAL || mode==TR_SPEEDLIMIT_SINGLE || mode==TR_SPEEDLIMIT_UNLIMITED );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
tor->speedLimitMode[dir] = mode;
|
|
|
|
|
|
|
|
tr_bandwidthSetLimited( tor->bandwidth, dir, mode==TR_SPEEDLIMIT_SINGLE );
|
2008-12-09 15:09:26 +00:00
|
|
|
tr_bandwidthHonorParentLimits( tor->bandwidth, dir, mode==TR_SPEEDLIMIT_GLOBAL );
|
2007-02-02 17:33:32 +00:00
|
|
|
}
|
2007-07-20 03:24:04 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_speedlimit
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentGetSpeedMode( const tr_torrent * tor,
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_direction dir )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
assert( tor != NULL );
|
2008-12-21 18:15:00 +00:00
|
|
|
assert( tr_isDirection( dir ) );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
|
|
|
return tor->speedLimitMode[dir];
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
2007-07-20 03:24:04 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentSetSpeedLimit( tr_torrent * tor,
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_direction dir,
|
|
|
|
int desiredSpeed )
|
2007-07-18 05:27:45 +00:00
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_bandwidthSetDesiredSpeed( tor->bandwidth, dir, desiredSpeed );
|
2007-07-18 05:27:45 +00:00
|
|
|
}
|
2007-07-20 03:24:04 +00:00
|
|
|
|
2007-07-18 05:27:45 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentGetSpeedLimit( const tr_torrent * tor,
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_direction dir )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2008-11-25 21:35:17 +00:00
|
|
|
return tr_bandwidthGetDesiredSpeed( tor->bandwidth, dir );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool
|
2008-11-07 05:37:59 +00:00
|
|
|
tr_torrentIsPieceTransferAllowed( const tr_torrent * tor,
|
2008-11-07 04:10:27 +00:00
|
|
|
tr_direction direction )
|
|
|
|
{
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isEnabled = FALSE;
|
2008-11-07 04:10:27 +00:00
|
|
|
|
|
|
|
switch( tr_torrentGetSpeedMode( tor, direction ) )
|
|
|
|
{
|
|
|
|
case TR_SPEEDLIMIT_GLOBAL:
|
2008-11-07 05:37:59 +00:00
|
|
|
isEnabled = !tr_sessionIsSpeedLimitEnabled( tor->session, direction )
|
|
|
|
|| tr_sessionGetSpeedLimit( tor->session, direction ) > 0;
|
2008-11-07 04:10:27 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_SPEEDLIMIT_SINGLE:
|
|
|
|
isEnabled = tr_torrentGetSpeedLimit( tor, direction ) > 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_SPEEDLIMIT_UNLIMITED:
|
|
|
|
isEnabled = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( 0 && "unhandled speed mode" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isEnabled;
|
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
onTrackerResponse( void * tracker UNUSED,
|
|
|
|
void * vevent,
|
|
|
|
void * user_data )
|
2007-08-16 20:00:06 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent * tor = user_data;
|
2007-10-17 18:59:58 +00:00
|
|
|
tr_tracker_event * event = vevent;
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
switch( event->messageType )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
case TR_TRACKER_PEERS:
|
|
|
|
{
|
|
|
|
size_t i, n;
|
2008-12-15 00:17:08 +00:00
|
|
|
tr_pex * pex = tr_peerMgrArrayToPex( event->compact,
|
|
|
|
event->compactLen, &n );
|
|
|
|
if( event->allAreSeeds )
|
2008-04-19 15:07:59 +00:00
|
|
|
tr_tordbg( tor, "Got %d seeds from tracker", (int)n );
|
|
|
|
else
|
|
|
|
tr_torinf( tor, _( "Got %d peers from tracker" ), (int)n );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
|
|
|
{
|
2008-04-19 15:07:59 +00:00
|
|
|
if( event->allAreSeeds )
|
|
|
|
pex[i].flags |= ADDED_F_SEED_FLAG;
|
2009-01-13 21:00:05 +00:00
|
|
|
tr_peerMgrAddPex( tor, TR_PEER_FROM_TRACKER, pex + i );
|
2008-04-19 15:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( pex );
|
2007-08-16 20:00:06 +00:00
|
|
|
break;
|
2008-04-19 15:07:59 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
case TR_TRACKER_WARNING:
|
2008-03-19 20:07:27 +00:00
|
|
|
tr_torerr( tor, _( "Tracker warning: \"%s\"" ), event->text );
|
2008-10-03 04:49:06 +00:00
|
|
|
tor->error = -1;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_strlcpy( tor->errorString, event->text,
|
|
|
|
sizeof( tor->errorString ) );
|
2007-08-16 20:00:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_ERROR:
|
2008-03-19 20:07:27 +00:00
|
|
|
tr_torerr( tor, _( "Tracker error: \"%s\"" ), event->text );
|
2008-10-03 04:49:06 +00:00
|
|
|
tor->error = -2;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_strlcpy( tor->errorString, event->text,
|
|
|
|
sizeof( tor->errorString ) );
|
2007-08-16 20:00:06 +00:00
|
|
|
break;
|
|
|
|
|
2007-09-25 18:39:58 +00:00
|
|
|
case TR_TRACKER_ERROR_CLEAR:
|
|
|
|
tor->error = 0;
|
|
|
|
tor->errorString[0] = '\0';
|
|
|
|
break;
|
2007-08-16 20:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** TORRENT INSTANTIATION
|
|
|
|
****
|
|
|
|
***/
|
2007-04-02 20:38:23 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
getBytePiece( const tr_info * info,
|
|
|
|
uint64_t byteOffset )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( info );
|
2007-06-26 18:45:03 +00:00
|
|
|
assert( info->pieceSize != 0 );
|
2007-04-02 20:38:23 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
return byteOffset / info->pieceSize;
|
2007-04-02 20:38:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
initFilePieces( tr_info * info,
|
|
|
|
tr_file_index_t fileIndex )
|
2007-04-02 20:38:23 +00:00
|
|
|
{
|
2008-07-17 20:33:34 +00:00
|
|
|
tr_file * file;
|
2008-09-23 19:11:04 +00:00
|
|
|
uint64_t firstByte, lastByte;
|
2007-04-02 20:38:23 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( info );
|
2008-03-22 18:10:59 +00:00
|
|
|
assert( fileIndex < info->fileCount );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
file = &info->files[fileIndex];
|
|
|
|
firstByte = file->offset;
|
2008-09-23 19:11:04 +00:00
|
|
|
lastByte = firstByte + ( file->length ? file->length - 1 : 0 );
|
2007-06-26 18:45:03 +00:00
|
|
|
file->firstPiece = getBytePiece( info, firstByte );
|
|
|
|
file->lastPiece = getBytePiece( info, lastByte );
|
|
|
|
}
|
|
|
|
|
2007-12-25 06:37:21 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
static tr_priority_t
|
2008-09-23 19:11:04 +00:00
|
|
|
calculatePiecePriority( const tr_torrent * tor,
|
|
|
|
tr_piece_index_t piece,
|
|
|
|
int fileHint )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_file_index_t i;
|
2008-09-23 19:11:04 +00:00
|
|
|
int priority = TR_PRI_LOW;
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2008-01-02 20:37:22 +00:00
|
|
|
/* find the first file that has data in this piece */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( fileHint >= 0 )
|
|
|
|
{
|
2008-01-02 20:37:22 +00:00
|
|
|
i = fileHint;
|
2008-09-23 19:11:04 +00:00
|
|
|
while( i > 0 && pieceHasFile( piece, &tor->info.files[i - 1] ) )
|
2008-01-02 20:37:22 +00:00
|
|
|
--i;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( i = 0; i < tor->info.fileCount; ++i )
|
2008-01-02 20:37:22 +00:00
|
|
|
if( pieceHasFile( piece, &tor->info.files[i] ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-12-25 05:42:33 +00:00
|
|
|
/* the piece's priority is the max of the priorities
|
|
|
|
* of all the files in that piece */
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ; i < tor->info.fileCount; ++i )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_file * file = &tor->info.files[i];
|
2007-12-25 05:42:33 +00:00
|
|
|
|
2007-12-25 06:37:21 +00:00
|
|
|
if( !pieceHasFile( piece, file ) )
|
2008-01-02 20:37:22 +00:00
|
|
|
break;
|
2007-12-25 05:42:33 +00:00
|
|
|
|
|
|
|
priority = MAX( priority, file->priority );
|
2007-08-08 15:07:06 +00:00
|
|
|
|
|
|
|
/* 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... */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( file->priority >= TR_PRI_NORMAL )
|
|
|
|
if( file->firstPiece == piece || file->lastPiece == piece )
|
2007-08-08 15:07:06 +00:00
|
|
|
priority = TR_PRI_HIGH;
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
return priority;
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
static void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentInitFilePieces( tr_torrent * tor )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file_index_t ff;
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_piece_index_t pp;
|
2008-09-23 19:11:04 +00:00
|
|
|
uint64_t offset = 0;
|
|
|
|
tr_info * inf = &tor->info;
|
2007-04-02 20:38:23 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( inf );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ff = 0; ff < inf->fileCount; ++ff )
|
|
|
|
{
|
|
|
|
inf->files[ff].offset = offset;
|
|
|
|
offset += inf->files[ff].length;
|
|
|
|
initFilePieces( inf, ff );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( pp = 0; pp < inf->pieceCount; ++pp )
|
2008-06-01 01:40:32 +00:00
|
|
|
inf->pieces[pp].priority = calculatePiecePriority( tor, pp, -1 );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2008-05-05 19:51:53 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentPromoteTracker( tr_torrent * tor,
|
|
|
|
int pos )
|
2008-05-05 19:51:53 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int tier;
|
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( tor );
|
2008-05-05 20:11:03 +00:00
|
|
|
assert( ( 0 <= pos ) && ( pos < tor->info.trackerCount ) );
|
2008-05-05 19:51:53 +00:00
|
|
|
|
2008-05-05 20:11:03 +00:00
|
|
|
/* the tier of the tracker we're promoting */
|
2008-05-05 19:51:53 +00:00
|
|
|
tier = tor->info.trackers[pos].tier;
|
|
|
|
|
2008-05-05 20:11:03 +00:00
|
|
|
/* find the index of that tier's first tracker */
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < tor->info.trackerCount; ++i )
|
2008-05-05 19:51:53 +00:00
|
|
|
if( tor->info.trackers[i].tier == tier )
|
|
|
|
break;
|
|
|
|
|
|
|
|
assert( i < tor->info.trackerCount );
|
|
|
|
|
2008-05-05 20:11:03 +00:00
|
|
|
/* promote the tracker at `pos' to the front of the tier */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( i != pos )
|
|
|
|
{
|
2008-05-05 20:11:03 +00:00
|
|
|
const tr_tracker_info tmp = tor->info.trackers[i];
|
2008-05-05 19:51:53 +00:00
|
|
|
tor->info.trackers[i] = tor->info.trackers[pos];
|
|
|
|
tor->info.trackers[pos] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return the new position of the tracker that started out at [pos] */
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2008-05-05 18:54:19 +00:00
|
|
|
struct RandomTracker
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_tracker_info tracker;
|
|
|
|
int random_value;
|
2008-05-05 18:54:19 +00:00
|
|
|
};
|
|
|
|
|
2008-05-05 20:11:03 +00:00
|
|
|
/* the tiers will be sorted from lowest to highest,
|
|
|
|
* and trackers are randomized within the tiers */
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE int
|
2008-09-23 19:11:04 +00:00
|
|
|
compareRandomTracker( const void * va,
|
|
|
|
const void * vb )
|
2008-05-05 18:54:19 +00:00
|
|
|
{
|
|
|
|
const struct RandomTracker * a = va;
|
|
|
|
const struct RandomTracker * b = vb;
|
|
|
|
|
|
|
|
if( a->tracker.tier != b->tracker.tier )
|
|
|
|
return a->tracker.tier - b->tracker.tier;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
return a->random_value - b->random_value;
|
2008-05-05 18:54:19 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-05 18:54:19 +00:00
|
|
|
static void
|
|
|
|
randomizeTiers( tr_info * info )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
const int n = info->trackerCount;
|
2008-05-05 20:11:03 +00:00
|
|
|
struct RandomTracker * r = tr_new0( struct RandomTracker, n );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
for( i = 0; i < n; ++i )
|
|
|
|
{
|
2008-05-05 18:54:19 +00:00
|
|
|
r[i].tracker = info->trackers[i];
|
2008-08-14 11:11:25 +00:00
|
|
|
r[i].random_value = tr_cryptoRandInt( INT_MAX );
|
2008-05-05 18:54:19 +00:00
|
|
|
}
|
|
|
|
qsort( r, n, sizeof( struct RandomTracker ), compareRandomTracker );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
2008-05-05 18:54:19 +00:00
|
|
|
info->trackers[i] = r[i].tracker;
|
|
|
|
tr_free( r );
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static void torrentStart( tr_torrent * tor,
|
|
|
|
int reloadProgress );
|
2008-05-06 01:43:24 +00:00
|
|
|
|
2008-08-05 20:27:40 +00:00
|
|
|
/**
|
|
|
|
* Decide on a block size. constraints:
|
|
|
|
* (1) most clients decline requests over 16 KiB
|
|
|
|
* (2) pieceSize must be a multiple of block size
|
|
|
|
*/
|
|
|
|
static uint32_t
|
|
|
|
getBlockSize( uint32_t pieceSize )
|
|
|
|
{
|
|
|
|
uint32_t b = pieceSize;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-05 20:27:40 +00:00
|
|
|
while( b > MAX_BLOCK_SIZE )
|
|
|
|
b /= 2u;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-06 11:39:30 +00:00
|
|
|
if( !b || ( pieceSize % b ) ) /* not cleanly divisible */
|
2008-08-05 20:27:40 +00:00
|
|
|
return 0;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
static void
|
2008-12-14 11:21:11 +00:00
|
|
|
torrentRealInit( tr_session * session,
|
|
|
|
tr_torrent * tor,
|
|
|
|
const tr_ctor * ctor )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2009-01-09 18:39:30 +00:00
|
|
|
int doStart;
|
|
|
|
uint64_t loaded;
|
|
|
|
uint64_t t;
|
|
|
|
const char * dir;
|
|
|
|
static int nextUniqueId = 1;
|
|
|
|
tr_info * info = &tor->info;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_globalLock( session );
|
2007-01-29 08:24:09 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tor->session = session;
|
2008-05-12 00:41:55 +00:00
|
|
|
tor->uniqueId = nextUniqueId++;
|
2009-01-30 00:41:08 +00:00
|
|
|
tor->magicNumber = TORRENT_MAGIC_NUMBER;
|
2007-03-31 19:19:09 +00:00
|
|
|
|
2008-05-05 18:54:19 +00:00
|
|
|
randomizeTiers( info );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tor->bandwidth = tr_bandwidthNew( session, session->bandwidth );
|
2008-11-25 21:35:17 +00:00
|
|
|
|
2008-08-05 20:27:40 +00:00
|
|
|
tor->blockSize = getBlockSize( info->pieceSize );
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2009-01-09 18:39:30 +00:00
|
|
|
if( !tr_ctorGetDownloadDir( ctor, TR_FORCE, &dir ) ||
|
|
|
|
!tr_ctorGetDownloadDir( ctor, TR_FALLBACK, &dir ) )
|
|
|
|
tor->downloadDir = tr_strdup( dir );
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
tor->lastPieceSize = info->totalSize % info->pieceSize;
|
|
|
|
|
2007-07-28 16:31:08 +00:00
|
|
|
if( !tor->lastPieceSize )
|
2008-09-23 19:11:04 +00:00
|
|
|
tor->lastPieceSize = info->pieceSize;
|
2007-07-28 16:31:08 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
tor->lastBlockSize = info->totalSize % tor->blockSize;
|
|
|
|
|
2007-07-28 16:31:08 +00:00
|
|
|
if( !tor->lastBlockSize )
|
2008-09-23 19:11:04 +00:00
|
|
|
tor->lastBlockSize = tor->blockSize;
|
2007-07-28 16:31:08 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
tor->blockCount =
|
|
|
|
( info->totalSize + tor->blockSize - 1 ) / tor->blockSize;
|
|
|
|
|
|
|
|
tor->blockCountInPiece =
|
|
|
|
info->pieceSize / tor->blockSize;
|
|
|
|
|
|
|
|
tor->blockCountInLastPiece =
|
|
|
|
( tor->lastPieceSize + tor->blockSize - 1 ) / tor->blockSize;
|
|
|
|
|
|
|
|
/* check our work */
|
|
|
|
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 );
|
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
tr_cpConstruct( &tor->completion, tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-07-25 01:59:46 +00:00
|
|
|
tr_torrentInitFilePieces( tor );
|
|
|
|
|
2009-01-02 20:42:35 +00:00
|
|
|
tr_rcConstruct( &tor->swarmSpeed );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
tr_sha1( tor->obfuscatedHash, "req2", 4,
|
2008-09-23 19:11:04 +00:00
|
|
|
info->hash, SHA_DIGEST_LENGTH,
|
|
|
|
NULL );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_peerMgrAddTorrent( session->peerMgr, tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
assert( session->isPortSet );
|
2007-07-09 23:09:00 +00:00
|
|
|
assert( !tor->downloadedCur );
|
|
|
|
assert( !tor->uploadedCur );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
tor->error = 0;
|
2007-07-23 03:00:20 +00:00
|
|
|
|
2008-12-29 09:51:54 +00:00
|
|
|
tr_bitfieldConstruct( &tor->checkedPieces, tor->info.pieceCount );
|
2008-01-02 18:05:05 +00:00
|
|
|
tr_torrentUncheck( tor );
|
2008-06-02 04:41:55 +00:00
|
|
|
|
|
|
|
tor->addedDate = time( NULL ); /* this is a default value to be
|
|
|
|
overwritten by the resume file */
|
2008-04-13 14:29:11 +00:00
|
|
|
loaded = tr_torrentLoadResume( tor, ~0, ctor );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
doStart = tor->isRunning;
|
2007-10-04 19:58:30 +00:00
|
|
|
tor->isRunning = 0;
|
2007-08-13 16:43:33 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !( loaded & TR_FR_SPEEDLIMIT ) )
|
|
|
|
{
|
2008-05-12 16:33:17 +00:00
|
|
|
tr_torrentSetSpeedLimit( tor, TR_UP,
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_sessionGetSpeedLimit( tor->session, TR_UP ) );
|
2008-05-12 16:33:17 +00:00
|
|
|
tr_torrentSetSpeedLimit( tor, TR_DOWN,
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_sessionGetSpeedLimit( tor->session,
|
2008-09-23 19:11:04 +00:00
|
|
|
TR_DOWN ) );
|
2007-07-23 03:00:20 +00:00
|
|
|
}
|
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
tor->completeness = tr_cpGetStatus( &tor->completion );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
tor->tracker = tr_trackerNew( tor );
|
2008-09-23 19:11:04 +00:00
|
|
|
tor->trackerSubscription =
|
|
|
|
tr_trackerSubscribe( tor->tracker, onTrackerResponse,
|
|
|
|
tor );
|
2007-08-16 20:00:06 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
|
|
|
tr_torrent * it = NULL;
|
|
|
|
tr_torrent * last = NULL;
|
2008-12-14 11:21:11 +00:00
|
|
|
while( ( it = tr_torrentNext( session, it ) ) )
|
2008-05-18 16:44:30 +00:00
|
|
|
last = it;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( !last )
|
2008-12-14 11:21:11 +00:00
|
|
|
session->torrentList = tor;
|
2008-05-18 16:44:30 +00:00
|
|
|
else
|
|
|
|
last->next = tor;
|
2008-12-14 11:21:11 +00:00
|
|
|
++session->torrentCount;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2007-10-01 15:17:15 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_globalUnlock( session );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-12-22 18:00:47 +00:00
|
|
|
/* maybe save our own copy of the metainfo */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_ctorGetSave( ctor ) )
|
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
const tr_benc * val;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !tr_ctorGetMetainfo( ctor, &val ) )
|
|
|
|
{
|
2008-04-14 11:52:50 +00:00
|
|
|
const char * filename = tor->info.torrent;
|
|
|
|
tr_bencSaveFile( filename, val );
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_sessionSetTorrentFile( tor->session, tor->info.hashString,
|
2008-09-23 19:11:04 +00:00
|
|
|
filename );
|
2007-12-22 18:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_metainfoMigrate( session, &tor->info );
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
if( doStart )
|
2008-05-06 01:43:24 +00:00
|
|
|
torrentStart( tor, FALSE );
|
2007-06-27 05:14:38 +00:00
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
int
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrentParse( const tr_session * session,
|
|
|
|
const tr_ctor * ctor,
|
|
|
|
tr_info * setmeInfo )
|
2007-06-27 05:14:38 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err = 0;
|
|
|
|
int doFree;
|
|
|
|
tr_info tmp;
|
2008-02-26 21:58:58 +00:00
|
|
|
const tr_benc * metainfo;
|
2007-12-21 22:18:40 +00:00
|
|
|
|
|
|
|
if( setmeInfo == NULL )
|
|
|
|
setmeInfo = &tmp;
|
|
|
|
memset( setmeInfo, 0, sizeof( tr_info ) );
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2008-03-02 19:55:04 +00:00
|
|
|
if( !err && tr_ctorGetMetainfo( ctor, &metainfo ) )
|
2007-12-21 22:18:40 +00:00
|
|
|
return TR_EINVALID;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
err = tr_metainfoParse( session, setmeInfo, metainfo );
|
2008-02-27 20:44:46 +00:00
|
|
|
doFree = !err && ( setmeInfo == &tmp );
|
2007-06-27 14:54:31 +00:00
|
|
|
|
2008-08-06 11:39:30 +00:00
|
|
|
if( !err && !getBlockSize( setmeInfo->pieceSize ) )
|
2008-08-05 20:27:40 +00:00
|
|
|
err = TR_EINVALID;
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
if( !err && tr_torrentExists( session, setmeInfo->hash ) )
|
2007-12-21 22:18:40 +00:00
|
|
|
err = TR_EDUPLICATE;
|
2008-02-27 20:16:01 +00:00
|
|
|
|
2007-08-19 04:03:06 +00:00
|
|
|
if( doFree )
|
2007-12-21 22:18:40 +00:00
|
|
|
tr_metainfoFree( setmeInfo );
|
2007-06-28 01:12:16 +00:00
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
return err;
|
2007-06-27 05:14:38 +00:00
|
|
|
}
|
2007-12-21 22:18:40 +00:00
|
|
|
|
|
|
|
tr_torrent *
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_torrentNew( tr_session * session,
|
|
|
|
const tr_ctor * ctor,
|
|
|
|
int * setmeError )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err;
|
|
|
|
tr_info tmpInfo;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2007-05-24 15:57:04 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
err = tr_torrentParse( session, ctor, &tmpInfo );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !err )
|
|
|
|
{
|
2007-12-21 22:18:40 +00:00
|
|
|
tor = tr_new0( tr_torrent, 1 );
|
|
|
|
tor->info = tmpInfo;
|
2008-12-14 11:21:11 +00:00
|
|
|
torrentRealInit( session, tor, ctor );
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else if( setmeError )
|
|
|
|
{
|
2007-12-21 22:18:40 +00:00
|
|
|
*setmeError = err;
|
2007-05-11 18:56:59 +00:00
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
return tor;
|
2007-05-11 18:56:59 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-07-23 03:00:20 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
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-06 01:43:24 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_free( tor->downloadDir );
|
|
|
|
tor->downloadDir = tr_strdup( path );
|
2008-05-06 01:43:24 +00:00
|
|
|
tr_torrentSaveResume( tor );
|
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-07-23 03:00:20 +00:00
|
|
|
const char*
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_torrentGetDownloadDir( const tr_torrent * tor )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
void
|
2007-09-21 05:31:29 +00:00
|
|
|
tr_torrentChangeMyPort( tr_torrent * tor )
|
2007-08-16 20:00:06 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if( tor->tracker )
|
|
|
|
tr_trackerChangeMyPort( tor->tracker );
|
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2009-01-11 17:02:04 +00:00
|
|
|
static TR_INLINE void
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_torrentManualUpdateImpl( void * vtor )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-11-20 16:50:48 +00:00
|
|
|
tr_torrent * tor = vtor;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
if( tor->isRunning )
|
|
|
|
tr_trackerReannounce( tor->tracker );
|
2007-07-30 17:11:00 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-20 16:50:48 +00:00
|
|
|
void
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_torrentManualUpdate( tr_torrent * tor )
|
2007-11-20 16:50:48 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_runInEventThread( tor->session, tr_torrentManualUpdateImpl, tor );
|
2007-11-20 16:50:48 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-07-30 17:11:00 +00:00
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentCanManualUpdate( const tr_torrent * tor )
|
2007-07-30 17:11:00 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
return ( tr_isTorrent( tor ) )
|
|
|
|
&& ( tor->isRunning )
|
|
|
|
&& ( tr_trackerCanManualAnnounce( tor->tracker ) );
|
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 = time( NULL );
|
2007-10-21 15:47:26 +00:00
|
|
|
|
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
|
|
|
}
|
2007-10-21 15:47:26 +00:00
|
|
|
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_torrent_activity
|
|
|
|
tr_torrentGetActivity( tr_torrent * tor )
|
2008-04-01 02:35:04 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-04-01 02:35:04 +00:00
|
|
|
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;
|
2008-11-30 01:52:45 +00:00
|
|
|
if( tor->completeness == TR_LEECH )
|
2008-04-01 02:35:04 +00:00
|
|
|
return TR_STATUS_DOWNLOAD;
|
|
|
|
|
|
|
|
return TR_STATUS_SEED;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_stat *
|
|
|
|
tr_torrentStat( tr_torrent * tor )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_stat * s;
|
|
|
|
struct tr_tracker * tc;
|
2008-05-22 16:59:51 +00:00
|
|
|
const tr_tracker_info * ti;
|
2008-09-23 19:11:04 +00:00
|
|
|
int usableSeeds = 0;
|
2009-01-05 04:27:54 +00:00
|
|
|
uint64_t now;
|
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 ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
tor->lastStatTime = time( NULL );
|
2007-10-20 21:07:11 +00:00
|
|
|
|
2008-03-31 19:34:20 +00:00
|
|
|
s = &tor->stats;
|
2008-05-22 16:59:51 +00:00
|
|
|
s->id = tor->uniqueId;
|
2008-10-20 17:54:56 +00:00
|
|
|
s->activity = tr_torrentGetActivity( tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
s->error = tor->error;
|
|
|
|
memcpy( s->errorString, tor->errorString,
|
2008-09-23 19:11:04 +00:00
|
|
|
sizeof( s->errorString ) );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
|
|
|
tc = tor->tracker;
|
2009-01-03 23:11:29 +00:00
|
|
|
ti = tr_trackerGetAddress( tor->tracker, tor );
|
2008-05-22 16:59:51 +00:00
|
|
|
s->announceURL = ti ? ti->announce : NULL;
|
|
|
|
s->scrapeURL = ti ? ti->scrape : NULL;
|
2008-05-22 23:11:21 +00:00
|
|
|
tr_trackerStat( tc, s );
|
2008-12-02 19:46:51 +00:00
|
|
|
|
2008-06-16 03:47:50 +00:00
|
|
|
tr_trackerGetCounts( tc, &s->timesCompleted,
|
2008-12-02 19:46:51 +00:00
|
|
|
&s->leechers,
|
|
|
|
&s->seeders,
|
|
|
|
&s->downloaders );
|
|
|
|
|
2009-01-13 21:00:05 +00:00
|
|
|
tr_peerMgrTorrentStats( tor,
|
2007-10-01 15:51:54 +00:00
|
|
|
&s->peersKnown,
|
2007-09-20 16:32:01 +00:00
|
|
|
&s->peersConnected,
|
2008-09-23 19:11:04 +00:00
|
|
|
&usableSeeds,
|
2008-06-10 02:36:52 +00:00
|
|
|
&s->webseedsSendingToUs,
|
2007-09-20 16:32:01 +00:00
|
|
|
&s->peersSendingToUs,
|
|
|
|
&s->peersGettingFromUs,
|
2008-10-20 03:47:48 +00:00
|
|
|
s->peersFrom );
|
|
|
|
|
2009-01-05 04:27:54 +00:00
|
|
|
now = tr_date( );
|
|
|
|
s->swarmSpeed = tr_rcRate( &tor->swarmSpeed, now );
|
|
|
|
s->rawUploadSpeed = tr_bandwidthGetRawSpeed ( tor->bandwidth, now, TR_UP );
|
|
|
|
s->rawDownloadSpeed = tr_bandwidthGetRawSpeed ( tor->bandwidth, now, TR_DOWN );
|
|
|
|
s->pieceUploadSpeed = tr_bandwidthGetPieceSpeed( tor->bandwidth, now, TR_UP );
|
|
|
|
s->pieceDownloadSpeed = tr_bandwidthGetPieceSpeed( tor->bandwidth, now, TR_DOWN );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2008-06-09 02:38:27 +00:00
|
|
|
usableSeeds += tor->info.webseedCount;
|
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
s->percentComplete = tr_cpPercentComplete ( &tor->completion );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
s->percentDone = tr_cpPercentDone ( &tor->completion );
|
|
|
|
s->leftUntilDone = tr_cpLeftUntilDone( &tor->completion );
|
|
|
|
s->sizeWhenDone = tr_cpSizeWhenDone ( &tor->completion );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2008-10-20 17:54:56 +00:00
|
|
|
s->recheckProgress = s->activity == TR_STATUS_CHECK
|
|
|
|
? 1.0 -
|
2008-09-23 19:11:04 +00:00
|
|
|
( tr_torrentCountUncheckedPieces( tor ) /
|
|
|
|
(double) tor->info.pieceCount )
|
2008-10-20 17:54:56 +00:00
|
|
|
: 0.0;
|
2007-07-24 01:33:59 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-05-25 19:14:42 +00:00
|
|
|
s->activityDate = tor->activityDate;
|
2008-06-03 19:16:12 +00:00
|
|
|
s->addedDate = tor->addedDate;
|
|
|
|
s->doneDate = tor->doneDate;
|
|
|
|
s->startDate = tor->startDate;
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-09-26 01:55:04 +00:00
|
|
|
s->corruptEver = tor->corruptCur + tor->corruptPrev;
|
|
|
|
s->downloadedEver = tor->downloadedCur + tor->downloadedPrev;
|
|
|
|
s->uploadedEver = tor->uploadedCur + tor->uploadedPrev;
|
2009-01-02 17:01:55 +00:00
|
|
|
s->haveValid = tr_cpHaveValid( &tor->completion );
|
|
|
|
s->haveUnchecked = tr_cpHaveTotal( &tor->completion ) - s->haveValid;
|
2007-09-27 03:03:38 +00:00
|
|
|
|
|
|
|
|
2008-06-09 02:38:27 +00:00
|
|
|
if( usableSeeds > 0 )
|
2008-05-31 05:22:10 +00:00
|
|
|
{
|
|
|
|
s->desiredAvailable = s->leftUntilDone;
|
|
|
|
}
|
2008-05-31 15:15:54 +00:00
|
|
|
else if( !s->leftUntilDone || !s->peersConnected )
|
2008-05-31 05:22:10 +00:00
|
|
|
{
|
2008-05-30 15:40:46 +00:00
|
|
|
s->desiredAvailable = 0;
|
2008-05-31 05:22:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_piece_index_t i;
|
2009-01-13 21:00:05 +00:00
|
|
|
tr_bitfield * peerPieces = tr_peerMgrGetAvailable( tor );
|
2007-09-27 03:30:22 +00:00
|
|
|
s->desiredAvailable = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < tor->info.pieceCount; ++i )
|
2008-04-12 15:56:21 +00:00
|
|
|
if( !tor->info.pieces[i].dnd && tr_bitfieldHas( peerPieces, i ) )
|
2009-01-02 17:01:55 +00:00
|
|
|
s->desiredAvailable += tr_cpMissingBlocksInPiece( &tor->completion, i );
|
2008-04-11 23:05:14 +00:00
|
|
|
s->desiredAvailable *= tor->blockSize;
|
|
|
|
tr_bitfieldFree( peerPieces );
|
2007-09-27 03:03:38 +00:00
|
|
|
}
|
2008-01-04 19:13:30 +00:00
|
|
|
|
2008-04-11 23:05:14 +00:00
|
|
|
if( s->leftUntilDone > s->desiredAvailable )
|
2008-04-06 14:42:47 +00:00
|
|
|
s->eta = TR_ETA_NOT_AVAIL;
|
2008-11-08 02:49:04 +00:00
|
|
|
else if( s->pieceDownloadSpeed < 0.1 )
|
2008-04-06 14:42:47 +00:00
|
|
|
s->eta = TR_ETA_UNKNOWN;
|
|
|
|
else
|
2008-11-08 02:49:04 +00:00
|
|
|
s->eta = s->leftUntilDone / s->pieceDownloadSpeed / 1024.0;
|
2008-04-06 14:42:47 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
s->ratio = tr_getRatio(
|
|
|
|
s->uploadedEver,
|
|
|
|
s->downloadedEver ? s->downloadedEver : s->
|
|
|
|
haveValid );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentUnlock( tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-07-15 20:05:32 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-07-15 20:29:57 +00:00
|
|
|
static uint64_t
|
2008-09-23 19:11:04 +00:00
|
|
|
fileBytesCompleted( const tr_torrent * tor,
|
|
|
|
tr_file_index_t fileIndex )
|
|
|
|
{
|
|
|
|
const tr_file * file = &tor->info.files[fileIndex];
|
|
|
|
const tr_block_index_t firstBlock = file->offset /
|
|
|
|
tor->blockSize;
|
|
|
|
const uint64_t firstBlockOffset = file->offset %
|
|
|
|
tor->blockSize;
|
|
|
|
const uint64_t lastOffset =
|
|
|
|
file->length ? ( file->length - 1 ) : 0;
|
|
|
|
const tr_block_index_t lastBlock =
|
|
|
|
( file->offset + lastOffset ) / tor->blockSize;
|
|
|
|
const uint64_t lastBlockOffset =
|
|
|
|
( file->offset + lastOffset ) % tor->blockSize;
|
|
|
|
uint64_t haveBytes = 0;
|
2007-07-15 20:29:57 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
2008-03-22 18:10:59 +00:00
|
|
|
assert( fileIndex < tor->info.fileCount );
|
2007-07-15 20:29:57 +00:00
|
|
|
assert( file->offset + file->length <= tor->info.totalSize );
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( ( firstBlock < tor->blockCount )
|
|
|
|
|| ( !file->length && file->offset == tor->info.totalSize ) );
|
|
|
|
assert( ( lastBlock < tor->blockCount )
|
|
|
|
|| ( !file->length && file->offset == tor->info.totalSize ) );
|
2007-07-15 20:29:57 +00:00
|
|
|
assert( firstBlock <= lastBlock );
|
2008-03-22 18:10:59 +00:00
|
|
|
assert( tr_torBlockPiece( tor, firstBlock ) == file->firstPiece );
|
|
|
|
assert( tr_torBlockPiece( tor, lastBlock ) == file->lastPiece );
|
2007-07-15 20:29:57 +00:00
|
|
|
|
|
|
|
if( firstBlock == lastBlock )
|
|
|
|
{
|
2009-01-02 17:01:55 +00:00
|
|
|
if( tr_cpBlockIsComplete( &tor->completion, firstBlock ) )
|
2007-07-15 20:29:57 +00:00
|
|
|
haveBytes += lastBlockOffset + 1 - firstBlockOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_block_index_t i;
|
2007-07-15 20:29:57 +00:00
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
if( tr_cpBlockIsComplete( &tor->completion, firstBlock ) )
|
2007-07-15 20:29:57 +00:00
|
|
|
haveBytes += tor->blockSize - firstBlockOffset;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = firstBlock + 1; i < lastBlock; ++i )
|
2009-01-02 17:01:55 +00:00
|
|
|
if( tr_cpBlockIsComplete( &tor->completion, i ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
haveBytes += tor->blockSize;
|
2007-07-15 20:29:57 +00:00
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
if( tr_cpBlockIsComplete( &tor->completion, lastBlock ) )
|
2007-07-15 20:29:57 +00:00
|
|
|
haveBytes += lastBlockOffset + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return haveBytes;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_file_stat *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentFiles( const tr_torrent * tor,
|
|
|
|
tr_file_index_t * fileCount )
|
2007-07-15 20:05:32 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file_index_t i;
|
2008-03-22 18:10:59 +00:00
|
|
|
const tr_file_index_t n = tor->info.fileCount;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file_stat * files = tr_new0( tr_file_stat, n );
|
|
|
|
tr_file_stat * walk = files;
|
2007-07-15 20:05:32 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i, ++walk )
|
|
|
|
{
|
2008-04-24 15:25:01 +00:00
|
|
|
const uint64_t b = fileBytesCompleted( tor, i );
|
|
|
|
walk->bytesCompleted = b;
|
|
|
|
walk->progress = tr_getRatio( b, tor->info.files[i].length );
|
2007-07-15 20:05:32 +00:00
|
|
|
}
|
|
|
|
|
2008-03-22 18:27:29 +00:00
|
|
|
if( fileCount )
|
|
|
|
*fileCount = n;
|
2007-07-15 20:05:32 +00:00
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentFilesFree( tr_file_stat * files,
|
|
|
|
tr_file_index_t fileCount UNUSED )
|
2007-07-15 20:05:32 +00:00
|
|
|
{
|
|
|
|
tr_free( files );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-06-10 01:38:12 +00:00
|
|
|
float*
|
|
|
|
tr_torrentWebSpeeds( const tr_torrent * tor )
|
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
return tr_isTorrent( tor )
|
|
|
|
? tr_peerMgrWebSpeeds( tor )
|
|
|
|
: NULL;
|
2008-06-10 01:38:12 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peer_stat *
|
2008-09-23 19:11:04 +00:00
|
|
|
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 ) )
|
2009-01-13 21:00:05 +00:00
|
|
|
ret = tr_peerMgrPeerStats( tor, peerCount );
|
2007-11-23 02:14:21 +00:00
|
|
|
|
|
|
|
return ret;
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-10-02 03:33:17 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentPeersFree( tr_peer_stat * peers,
|
|
|
|
int peerCount UNUSED )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_free( peers );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2008-05-28 13:03:34 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentAvailability( const tr_torrent * tor,
|
|
|
|
int8_t * tab,
|
|
|
|
int size )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2009-01-13 21:00:05 +00:00
|
|
|
tr_peerMgrTorrentAvailability( tor, tab, size );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 15:25:01 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
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 ) );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2009-01-02 17:01:55 +00:00
|
|
|
tr_cpGetAmountDone( &tor->completion, tab, size );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentUnlock( tor );
|
2007-04-28 01:34:39 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
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 ) );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tor->downloadedPrev += tor->downloadedCur;
|
|
|
|
tor->downloadedCur = 0;
|
|
|
|
tor->uploadedPrev += tor->uploadedCur;
|
|
|
|
tor->uploadedCur = 0;
|
2007-08-21 15:17:02 +00:00
|
|
|
tor->corruptPrev += tor->corruptCur;
|
|
|
|
tor->corruptCur = 0;
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentUnlock( tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentSetHasPiece( tr_torrent * tor,
|
|
|
|
tr_piece_index_t pieceIndex,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool has )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
2008-01-08 03:08:36 +00:00
|
|
|
assert( pieceIndex < tor->info.pieceCount );
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
if( has )
|
2009-01-02 17:01:55 +00:00
|
|
|
tr_cpPieceAdd( &tor->completion, pieceIndex );
|
2007-06-26 18:45:03 +00:00
|
|
|
else
|
2009-01-02 17:01:55 +00:00
|
|
|
tr_cpPieceRem( &tor->completion, pieceIndex );
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
static void
|
2007-10-04 19:58:30 +00:00
|
|
|
freeTorrent( tr_torrent * tor )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * t;
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_session * session = tor->session;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_info * inf = &tor->info;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
2007-10-04 19:58:30 +00:00
|
|
|
assert( !tor->isRunning );
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_globalLock( session );
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2009-01-13 21:00:05 +00:00
|
|
|
tr_peerMgrRemoveTorrent( tor );
|
2007-09-23 19:24:06 +00:00
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
tr_cpDestruct( &tor->completion );
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2009-01-02 20:42:35 +00:00
|
|
|
tr_rcDestruct( &tor->swarmSpeed );
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_trackerUnsubscribe( tor->tracker, tor->trackerSubscription );
|
|
|
|
tr_trackerFree( tor->tracker );
|
|
|
|
tor->tracker = NULL;
|
|
|
|
|
2008-12-29 09:51:54 +00:00
|
|
|
tr_bitfieldDestruct( &tor->checkedPieces );
|
2008-01-02 18:05:05 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_free( tor->downloadDir );
|
2008-12-06 02:21:50 +00:00
|
|
|
tr_free( tor->peer_id );
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
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;
|
2007-05-28 16:40:56 +00:00
|
|
|
}
|
2008-12-14 11:21:11 +00:00
|
|
|
}
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
assert( session->torrentCount >= 1 );
|
|
|
|
session->torrentCount--;
|
2007-06-30 13:19:57 +00:00
|
|
|
|
2008-11-25 21:35:17 +00:00
|
|
|
tr_bandwidthFree( tor->bandwidth );
|
2008-11-06 02:56:51 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_metainfoFree( inf );
|
2007-07-21 17:35:47 +00:00
|
|
|
tr_free( tor );
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_globalUnlock( session );
|
2007-05-28 15:23:28 +00:00
|
|
|
}
|
|
|
|
|
2008-01-18 19:13:32 +00:00
|
|
|
/**
|
|
|
|
*** Start/Stop Callback
|
|
|
|
**/
|
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
static void
|
2007-11-16 05:43:47 +00:00
|
|
|
checkAndStartImpl( void * vtor )
|
2007-10-04 19:58:30 +00:00
|
|
|
{
|
2007-11-16 05:43:47 +00:00
|
|
|
tr_torrent * tor = vtor;
|
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_globalLock( tor->session );
|
2007-10-04 19:58:30 +00:00
|
|
|
|
2008-12-02 18:24:26 +00:00
|
|
|
tor->isRunning = 1;
|
2007-10-04 19:58:30 +00:00
|
|
|
*tor->errorString = '\0';
|
|
|
|
tr_torrentResetTransferStats( tor );
|
2009-01-02 17:01:55 +00:00
|
|
|
tor->completeness = tr_cpGetStatus( &tor->completion );
|
2008-04-13 14:29:11 +00:00
|
|
|
tr_torrentSaveResume( tor );
|
2008-05-23 20:04:41 +00:00
|
|
|
tor->startDate = time( NULL );
|
2007-10-04 19:58:30 +00:00
|
|
|
tr_trackerStart( tor->tracker );
|
2009-01-13 21:00:05 +00:00
|
|
|
tr_peerMgrStartTorrent( tor );
|
2007-10-04 19:58:30 +00:00
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_globalUnlock( tor->session );
|
2007-10-04 19:58:30 +00:00
|
|
|
}
|
2007-11-16 05:43:47 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
checkAndStartCB( tr_torrent * tor )
|
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
2009-01-29 16:56:43 +00:00
|
|
|
assert( tr_isSession( tor->session ) );
|
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_runInEventThread( tor->session, checkAndStartImpl, tor );
|
2007-11-16 05:43:47 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 01:43:24 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
torrentStart( tr_torrent * tor,
|
|
|
|
int reloadProgress )
|
2007-07-19 03:48:27 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_globalLock( tor->session );
|
2007-07-19 03:48:27 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
if( !tor->isRunning )
|
|
|
|
{
|
2008-10-28 15:13:07 +00:00
|
|
|
const int isVerifying = tr_verifyInProgress( tor );
|
|
|
|
|
|
|
|
if( !isVerifying && reloadProgress )
|
2008-05-06 01:43:24 +00:00
|
|
|
tr_torrentLoadResume( tor, TR_FR_PROGRESS, NULL );
|
2008-10-28 15:13:07 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
tor->isRunning = 1;
|
2008-10-28 15:13:07 +00:00
|
|
|
|
|
|
|
if( !isVerifying )
|
|
|
|
tr_verifyAdd( tor, checkAndStartCB );
|
2007-10-04 19:58:30 +00:00
|
|
|
}
|
2007-07-19 03:48:27 +00:00
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_globalUnlock( tor->session );
|
2007-07-19 03:48:27 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 01:43:24 +00:00
|
|
|
void
|
|
|
|
tr_torrentStart( tr_torrent * tor )
|
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
if( tr_isTorrent( tor ) )
|
2008-06-10 19:25:18 +00:00
|
|
|
torrentStart( tor, TRUE );
|
2008-05-06 01:43:24 +00:00
|
|
|
}
|
|
|
|
|
2007-11-16 20:41:41 +00:00
|
|
|
static void
|
|
|
|
torrentRecheckDoneImpl( void * vtor )
|
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
tr_torrent * tor = vtor;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
tr_torrentRecheckCompleteness( tor );
|
2007-11-16 20:41:41 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-11-16 20:41:41 +00:00
|
|
|
static void
|
|
|
|
torrentRecheckDoneCB( tr_torrent * tor )
|
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_runInEventThread( tor->session, torrentRecheckDoneImpl, tor );
|
2007-11-16 20:41:41 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-10-02 02:59:07 +00:00
|
|
|
void
|
2008-02-27 17:38:39 +00:00
|
|
|
tr_torrentVerify( tr_torrent * tor )
|
2007-10-04 19:58:30 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-04-27 06:34:34 +00:00
|
|
|
tr_verifyRemove( tor );
|
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_globalLock( tor->session );
|
2007-10-04 23:47:49 +00:00
|
|
|
|
2008-01-02 18:05:05 +00:00
|
|
|
tr_torrentUncheck( tor );
|
2008-02-15 16:00:46 +00:00
|
|
|
tr_verifyAdd( tor, torrentRecheckDoneCB );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_globalUnlock( tor->session );
|
2007-10-04 19:58:30 +00:00
|
|
|
}
|
2007-07-12 20:48:13 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
static void
|
2009-01-12 19:58:16 +00:00
|
|
|
tr_torrentCloseLocalFiles( const tr_torrent * tor )
|
2007-10-04 19:58:30 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_file_index_t i;
|
2009-01-12 19:58:16 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2007-11-21 16:16:59 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2009-01-12 19:58:16 +00:00
|
|
|
for( i=0; i<tor->info.fileCount; ++i )
|
|
|
|
{
|
|
|
|
const tr_file * file = &tor->info.files[i];
|
|
|
|
evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) );
|
|
|
|
evbuffer_add_printf( buf, "%s%s%s", tor->downloadDir, TR_PATH_DELIMITER_STR, file->name );
|
|
|
|
tr_fdFileClose( (const char*) EVBUFFER_DATA( buf ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
evbuffer_free( buf );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
stopTorrent( void * vtor )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = vtor;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-02-15 16:00:46 +00:00
|
|
|
tr_verifyRemove( tor );
|
2009-01-13 21:00:05 +00:00
|
|
|
tr_peerMgrStopTorrent( tor );
|
2007-10-04 19:58:30 +00:00
|
|
|
tr_trackerStop( tor->tracker );
|
2007-11-21 16:16:59 +00:00
|
|
|
|
2009-01-12 19:58:16 +00:00
|
|
|
tr_torrentCloseLocalFiles( tor );
|
2007-10-04 19:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentStop( tr_torrent * tor )
|
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
if( tr_isTorrent( tor ) )
|
2008-06-10 19:25:18 +00:00
|
|
|
{
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_globalLock( tor->session );
|
2007-10-04 19:58:30 +00:00
|
|
|
|
2008-06-10 19:25:18 +00:00
|
|
|
tor->isRunning = 0;
|
|
|
|
if( !tor->isDeleting )
|
|
|
|
tr_torrentSaveResume( tor );
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_runInEventThread( tor->session, stopTorrent, tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_globalUnlock( tor->session );
|
2008-06-10 19:25:18 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
static void
|
|
|
|
closeTorrent( void * vtor )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = vtor;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-04-13 14:29:11 +00:00
|
|
|
tr_torrentSaveResume( tor );
|
2007-10-04 19:58:30 +00:00
|
|
|
tor->isRunning = 0;
|
|
|
|
stopTorrent( tor );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tor->isDeleting )
|
|
|
|
{
|
2008-10-02 15:53:33 +00:00
|
|
|
tr_metainfoRemoveSaved( tor->session, &tor->info );
|
2008-05-22 23:11:21 +00:00
|
|
|
tr_torrentRemoveResume( tor );
|
|
|
|
}
|
2007-10-04 19:58:30 +00:00
|
|
|
freeTorrent( tor );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
2008-05-21 17:14:58 +00:00
|
|
|
tr_torrentFree( tr_torrent * tor )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
if( tr_isTorrent( tor ) )
|
2008-01-16 17:57:13 +00:00
|
|
|
{
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_session * session = tor->session;
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isSession( session ) );
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_globalLock( session );
|
2007-10-02 02:59:07 +00:00
|
|
|
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_torrentClearCompletenessCallback( tor );
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_runInEventThread( session, closeTorrent, tor );
|
2007-10-02 02:59:07 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_globalUnlock( session );
|
2008-01-16 17:57:13 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2008-02-19 05:03:56 +00:00
|
|
|
void
|
2008-05-20 23:58:59 +00:00
|
|
|
tr_torrentRemove( tr_torrent * tor )
|
2008-02-19 05:03:56 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-02-19 05:03:56 +00:00
|
|
|
tor->isDeleting = 1;
|
2008-05-21 17:14:58 +00:00
|
|
|
tr_torrentFree( tor );
|
2008-02-19 05:03:56 +00:00
|
|
|
}
|
|
|
|
|
2007-09-28 14:51:34 +00:00
|
|
|
/**
|
|
|
|
*** Completeness
|
|
|
|
**/
|
|
|
|
|
2008-03-18 16:56:01 +00:00
|
|
|
static const char *
|
|
|
|
getCompletionString( int type )
|
|
|
|
{
|
|
|
|
switch( type )
|
|
|
|
{
|
2008-03-19 20:07:27 +00:00
|
|
|
/* 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.
|
2008-09-23 19:11:04 +00:00
|
|
|
"Done" means we're done downloading the files we wanted, but NOT all
|
|
|
|
that exist */
|
2008-11-30 01:52:45 +00:00
|
|
|
case TR_PARTIAL_SEED:
|
2008-09-23 19:11:04 +00:00
|
|
|
return _( "Done" );
|
|
|
|
|
2008-11-30 01:52:45 +00:00
|
|
|
case TR_SEED:
|
2008-09-23 19:11:04 +00:00
|
|
|
return _( "Complete" );
|
|
|
|
|
|
|
|
default:
|
|
|
|
return _( "Incomplete" );
|
2008-03-18 16:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-28 14:51:34 +00:00
|
|
|
static void
|
2008-10-20 17:54:56 +00:00
|
|
|
fireCompletenessChange( tr_torrent * tor,
|
|
|
|
tr_completeness status )
|
2007-09-28 14:51:34 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
2008-11-30 01:52:45 +00:00
|
|
|
assert( ( status == TR_LEECH )
|
|
|
|
|| ( status == TR_SEED )
|
|
|
|
|| ( status == TR_PARTIAL_SEED ) );
|
2007-09-28 14:51:34 +00:00
|
|
|
|
2008-10-20 17:54:56 +00:00
|
|
|
if( tor->completeness_func )
|
|
|
|
tor->completeness_func( tor, status, tor->completeness_func_user_data );
|
2007-09-28 14:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_torrentSetCompletenessCallback( tr_torrent * tor,
|
|
|
|
tr_torrent_completeness_func func,
|
|
|
|
void * user_data )
|
2007-09-28 14:51:34 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-10-20 17:54:56 +00:00
|
|
|
tor->completeness_func = func;
|
|
|
|
tor->completeness_func_user_data = user_data;
|
2007-09-28 14:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_torrentClearCompletenessCallback( tr_torrent * torrent )
|
2007-09-28 14:51:34 +00:00
|
|
|
{
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_torrentSetCompletenessCallback( torrent, NULL, NULL );
|
2007-09-28 14:51:34 +00:00
|
|
|
}
|
|
|
|
|
2007-09-23 13:53:44 +00:00
|
|
|
void
|
|
|
|
tr_torrentRecheckCompleteness( tr_torrent * tor )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-10-20 17:54:56 +00:00
|
|
|
tr_completeness completeness;
|
2007-07-15 04:19:39 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-01-21 06:42:05 +00:00
|
|
|
|
2009-01-02 17:01:55 +00:00
|
|
|
completeness = tr_cpGetStatus( &tor->completion );
|
2008-03-18 16:56:01 +00:00
|
|
|
|
2008-10-20 17:54:56 +00:00
|
|
|
if( completeness != tor->completeness )
|
2008-03-18 16:56:01 +00:00
|
|
|
{
|
|
|
|
const int recentChange = tor->downloadedCur != 0;
|
|
|
|
|
|
|
|
if( recentChange )
|
|
|
|
{
|
2008-04-21 15:22:54 +00:00
|
|
|
tr_torinf( tor, _( "State changed from \"%1$s\" to \"%2$s\"" ),
|
2008-10-20 17:54:56 +00:00
|
|
|
getCompletionString( tor->completeness ),
|
|
|
|
getCompletionString( completeness ) );
|
2008-03-18 16:56:01 +00:00
|
|
|
}
|
|
|
|
|
2008-10-20 17:54:56 +00:00
|
|
|
tor->completeness = completeness;
|
2009-01-12 19:58:16 +00:00
|
|
|
tr_torrentCloseLocalFiles( tor );
|
2008-10-20 17:54:56 +00:00
|
|
|
fireCompletenessChange( tor, completeness );
|
2008-03-18 16:56:01 +00:00
|
|
|
|
2008-11-30 01:52:45 +00:00
|
|
|
if( recentChange && ( completeness == TR_SEED ) )
|
2008-06-03 19:16:12 +00:00
|
|
|
{
|
2008-03-18 16:56:01 +00:00
|
|
|
tr_trackerCompleted( tor->tracker );
|
|
|
|
|
2008-06-03 19:16:12 +00:00
|
|
|
tor->doneDate = time( NULL );
|
|
|
|
}
|
|
|
|
|
2008-04-13 14:29:11 +00:00
|
|
|
tr_torrentSaveResume( tor );
|
2007-01-19 15:24:20 +00:00
|
|
|
}
|
2008-03-18 16:56:01 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentUnlock( tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 19:57:34 +00:00
|
|
|
/**
|
|
|
|
*** File priorities
|
|
|
|
**/
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-12-25 06:37:21 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentInitFilePriority( tr_torrent * tor,
|
|
|
|
tr_file_index_t fileIndex,
|
|
|
|
tr_priority_t priority )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_piece_index_t i;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file * file;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
2008-03-22 18:10:59 +00:00
|
|
|
assert( fileIndex < tor->info.fileCount );
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( priority == TR_PRI_LOW || priority == TR_PRI_NORMAL || priority == TR_PRI_HIGH );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
file = &tor->info.files[fileIndex];
|
|
|
|
file->priority = priority;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = file->firstPiece; i <= file->lastPiece; ++i )
|
|
|
|
tor->info.pieces[i].priority = calculatePiecePriority( tor, i,
|
|
|
|
fileIndex );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-07-10 03:12:46 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentSetFilePriorities( tr_torrent * tor,
|
|
|
|
tr_file_index_t * files,
|
|
|
|
tr_file_index_t fileCount,
|
|
|
|
tr_priority_t priority )
|
2007-07-10 03:12:46 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_file_index_t i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-10-19 23:44:01 +00:00
|
|
|
tr_torrentLock( tor );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < fileCount; ++i )
|
2007-12-25 06:37:21 +00:00
|
|
|
tr_torrentInitFilePriority( tor, files[i], priority );
|
2007-10-19 23:44:01 +00:00
|
|
|
|
2008-04-13 14:29:11 +00:00
|
|
|
tr_torrentSaveResume( tor );
|
2007-10-19 23:44:01 +00:00
|
|
|
tr_torrentUnlock( tor );
|
2007-07-10 03:12:46 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_priority_t
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentGetFilePriority( const tr_torrent * tor,
|
|
|
|
tr_file_index_t file )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_priority_t ret;
|
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( tor );
|
2008-03-22 18:10:59 +00:00
|
|
|
assert( file < tor->info.fileCount );
|
2007-06-26 18:45:03 +00:00
|
|
|
ret = tor->info.files[file].priority;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentUnlock( tor );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
return ret;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_priority_t*
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentGetFilePriorities( const tr_torrent * tor )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_file_index_t i;
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_priority_t * p;
|
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-06-27 05:14:38 +00:00
|
|
|
p = tr_new0( tr_priority_t, tor->info.fileCount );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < tor->info.fileCount; ++i )
|
2007-06-18 03:40:41 +00:00
|
|
|
p[i] = tor->info.files[i].priority;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentUnlock( tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
return p;
|
|
|
|
}
|
2007-07-10 03:12:46 +00:00
|
|
|
|
2007-07-16 19:57:34 +00:00
|
|
|
/**
|
|
|
|
*** File DND
|
|
|
|
**/
|
|
|
|
|
2007-07-10 03:12:46 +00:00
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentGetFileDL( const tr_torrent * tor,
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_file_index_t file )
|
2007-07-10 03:12:46 +00:00
|
|
|
{
|
2007-07-16 19:57:34 +00:00
|
|
|
int doDownload;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-07-10 03:12:46 +00:00
|
|
|
|
2008-03-22 18:10:59 +00:00
|
|
|
assert( file < tor->info.fileCount );
|
2007-07-16 19:57:34 +00:00
|
|
|
doDownload = !tor->info.files[file].dnd;
|
2007-07-10 03:12:46 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentUnlock( tor );
|
2007-07-16 19:57:34 +00:00
|
|
|
return doDownload != 0;
|
2007-07-10 03:12:46 +00:00
|
|
|
}
|
|
|
|
|
2007-10-19 23:23:21 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
setFileDND( tr_torrent * tor,
|
|
|
|
tr_file_index_t fileIndex,
|
|
|
|
int doDownload )
|
2007-07-10 03:12:46 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file * file;
|
|
|
|
const int dnd = !doDownload;
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_piece_index_t firstPiece, firstPieceDND;
|
|
|
|
tr_piece_index_t lastPiece, lastPieceDND;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file_index_t i;
|
2007-07-10 03:41:16 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-07-10 03:41:16 +00:00
|
|
|
file = &tor->info.files[fileIndex];
|
2007-07-16 23:52:05 +00:00
|
|
|
file->dnd = dnd;
|
2007-07-16 19:57:34 +00:00
|
|
|
firstPiece = file->firstPiece;
|
|
|
|
lastPiece = file->lastPiece;
|
|
|
|
|
|
|
|
/* can't set the first piece to DND unless
|
|
|
|
every file using that piece is DND */
|
|
|
|
firstPieceDND = dnd;
|
2008-09-23 19:11:04 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-07-16 19:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* can't set the last piece to DND unless
|
|
|
|
every file using that piece is DND */
|
|
|
|
lastPieceDND = dnd;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = fileIndex + 1; lastPieceDND && i < tor->info.fileCount; ++i )
|
|
|
|
{
|
2007-07-16 19:57:34 +00:00
|
|
|
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
|
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_piece_index_t pp;
|
2007-07-16 19:57:34 +00:00
|
|
|
tor->info.pieces[firstPiece].dnd = firstPieceDND;
|
|
|
|
tor->info.pieces[lastPiece].dnd = lastPieceDND;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( pp = firstPiece + 1; pp < lastPiece; ++pp )
|
2008-03-22 18:10:59 +00:00
|
|
|
tor->info.pieces[pp].dnd = dnd;
|
2007-07-16 19:57:34 +00:00
|
|
|
}
|
2007-07-10 03:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentInitFileDLs( tr_torrent * tor,
|
|
|
|
tr_file_index_t * files,
|
|
|
|
tr_file_index_t fileCount,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool doDownload )
|
2007-07-10 03:41:16 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_file_index_t i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-10-19 23:23:21 +00:00
|
|
|
tr_torrentLock( tor );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < fileCount; ++i )
|
2007-10-19 23:23:21 +00:00
|
|
|
setFileDND( tor, files[i], doDownload );
|
2009-01-02 17:01:55 +00:00
|
|
|
tr_cpInvalidateDND ( &tor->completion );
|
2007-10-19 23:23:21 +00:00
|
|
|
|
|
|
|
tr_torrentUnlock( tor );
|
2007-07-10 03:12:46 +00:00
|
|
|
}
|
2007-07-19 11:54:37 +00:00
|
|
|
|
2007-12-22 17:30:31 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentSetFileDLs( tr_torrent * tor,
|
|
|
|
tr_file_index_t * files,
|
|
|
|
tr_file_index_t fileCount,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool doDownload )
|
2007-12-22 17:30:31 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-12-22 17:30:31 +00:00
|
|
|
tr_torrentLock( tor );
|
|
|
|
tr_torrentInitFileDLs( tor, files, fileCount, doDownload );
|
2008-04-13 14:29:11 +00:00
|
|
|
tr_torrentSaveResume( tor );
|
2007-12-22 17:30:31 +00:00
|
|
|
tr_torrentUnlock( tor );
|
|
|
|
}
|
|
|
|
|
2007-07-19 11:54:37 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-12-20 21:44:16 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentSetPeerLimit( tr_torrent * tor,
|
|
|
|
uint16_t maxConnectedPeers )
|
2007-12-20 21:44:16 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-12-22 04:00:19 +00:00
|
|
|
tor->maxConnectedPeers = maxConnectedPeers;
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
2007-12-22 04:11:17 +00:00
|
|
|
uint16_t
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentGetPeerLimit( const tr_torrent * tor )
|
2007-12-22 04:00:19 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-12-22 04:00:19 +00:00
|
|
|
return tor->maxConnectedPeers;
|
|
|
|
}
|
|
|
|
|
2007-12-20 21:44:16 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_block_index_t
|
2008-09-23 19:11:04 +00:00
|
|
|
_tr_block( const tr_torrent * tor,
|
|
|
|
tr_piece_index_t index,
|
|
|
|
uint32_t offset )
|
2007-07-19 11:54:37 +00:00
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_block_index_t ret;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-03-22 18:10:59 +00:00
|
|
|
ret = index;
|
2009-01-30 00:41:08 +00:00
|
|
|
ret *= ( tor->info.pieceSize / tor->blockSize );
|
2008-03-22 18:10:59 +00:00
|
|
|
ret += offset / tor->blockSize;
|
|
|
|
return ret;
|
2007-07-19 11:54:37 +00:00
|
|
|
}
|
2007-09-23 23:38:39 +00:00
|
|
|
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool
|
2008-03-06 13:24:44 +00:00
|
|
|
tr_torrentReqIsValid( const tr_torrent * tor,
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_piece_index_t index,
|
2008-03-06 13:24:44 +00:00
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length )
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-03-22 18:10:59 +00:00
|
|
|
if( index >= tor->info.pieceCount )
|
2008-03-06 13:24:44 +00:00
|
|
|
err = 1;
|
2008-10-27 17:35:22 +00:00
|
|
|
else if( length < 1 )
|
2008-03-06 13:24:44 +00:00
|
|
|
err = 2;
|
2008-10-27 04:39:30 +00:00
|
|
|
else if( ( offset + length ) > tr_torPieceCountBytes( tor, index ) )
|
2008-03-06 13:24:44 +00:00
|
|
|
err = 3;
|
2008-10-27 04:39:30 +00:00
|
|
|
else if( length > MAX_BLOCK_SIZE )
|
2008-03-06 13:24:44 +00:00
|
|
|
err = 4;
|
2008-10-27 04:39:30 +00:00
|
|
|
else if( tr_pieceOffset( tor, index, offset, length ) > tor->info.totalSize )
|
|
|
|
err = 5;
|
2008-03-06 13:24:44 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( err ) fprintf( stderr, "index %lu offset %lu length %lu err %d\n",
|
|
|
|
(unsigned long)index, (unsigned long)offset,
|
|
|
|
(unsigned long)length,
|
|
|
|
err );
|
2008-06-09 22:53:45 +00:00
|
|
|
|
2008-03-06 13:24:44 +00:00
|
|
|
return !err;
|
|
|
|
}
|
|
|
|
|
2007-09-23 23:38:39 +00:00
|
|
|
uint64_t
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_pieceOffset( const tr_torrent * tor,
|
|
|
|
tr_piece_index_t index,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length )
|
2007-09-23 23:38:39 +00:00
|
|
|
{
|
|
|
|
uint64_t ret;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2007-09-23 23:38:39 +00:00
|
|
|
ret = tor->info.pieceSize;
|
|
|
|
ret *= index;
|
2008-03-22 18:10:59 +00:00
|
|
|
ret += offset;
|
2007-09-23 23:38:39 +00:00
|
|
|
ret += length;
|
|
|
|
return ret;
|
|
|
|
}
|
2008-01-02 18:05:05 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_torrentSetPieceChecked( tr_torrent * tor,
|
|
|
|
tr_piece_index_t piece,
|
|
|
|
tr_bool isChecked )
|
2008-01-02 18:05:05 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-01-02 18:05:05 +00:00
|
|
|
if( isChecked )
|
2008-12-29 09:51:54 +00:00
|
|
|
tr_bitfieldAdd( &tor->checkedPieces, piece );
|
2008-01-02 18:05:05 +00:00
|
|
|
else
|
2008-12-29 09:51:54 +00:00
|
|
|
tr_bitfieldRem( &tor->checkedPieces, piece );
|
2008-01-02 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentSetFileChecked( tr_torrent * tor,
|
|
|
|
tr_file_index_t fileIndex,
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isChecked )
|
2008-01-02 18:05:05 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const tr_file * file = &tor->info.files[fileIndex];
|
2008-03-22 18:10:59 +00:00
|
|
|
const tr_piece_index_t begin = file->firstPiece;
|
|
|
|
const tr_piece_index_t end = file->lastPiece + 1;
|
2008-01-02 18:05:05 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-01-02 18:05:05 +00:00
|
|
|
if( isChecked )
|
2008-12-29 09:51:54 +00:00
|
|
|
tr_bitfieldAddRange ( &tor->checkedPieces, begin, end );
|
2008-01-02 18:05:05 +00:00
|
|
|
else
|
2008-12-29 09:51:54 +00:00
|
|
|
tr_bitfieldRemRange ( &tor->checkedPieces, begin, end );
|
2008-01-02 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentIsFileChecked( const tr_torrent * tor,
|
|
|
|
tr_file_index_t fileIndex )
|
2008-02-01 01:38:55 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const tr_file * file = &tor->info.files[fileIndex];
|
2008-03-22 18:10:59 +00:00
|
|
|
const tr_piece_index_t begin = file->firstPiece;
|
|
|
|
const tr_piece_index_t end = file->lastPiece + 1;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_piece_index_t i;
|
2008-12-13 23:39:12 +00:00
|
|
|
tr_bool isChecked = TRUE;
|
2008-02-01 01:38:55 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = begin; isChecked && i < end; ++i )
|
2008-02-01 01:38:55 +00:00
|
|
|
if( !tr_torrentIsPieceChecked( tor, i ) )
|
|
|
|
isChecked = FALSE;
|
|
|
|
|
|
|
|
return isChecked;
|
|
|
|
}
|
|
|
|
|
2008-01-02 18:05:05 +00:00
|
|
|
void
|
|
|
|
tr_torrentUncheck( tr_torrent * tor )
|
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-12-29 09:51:54 +00:00
|
|
|
tr_bitfieldRemRange ( &tor->checkedPieces, 0, tor->info.pieceCount );
|
2008-01-02 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_torrentCountUncheckedPieces( const tr_torrent * tor )
|
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-12-29 09:51:54 +00:00
|
|
|
return tor->info.pieceCount - tr_bitfieldCountTrueBits( &tor->checkedPieces );
|
2008-01-02 18:05:05 +00:00
|
|
|
}
|
2008-04-14 11:52:50 +00:00
|
|
|
|
|
|
|
time_t*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentGetMTimes( const tr_torrent * tor,
|
|
|
|
size_t * setme_n )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t i;
|
2008-09-04 14:42:32 +00:00
|
|
|
const size_t n = tor->info.fileCount;
|
2008-09-23 19:11:04 +00:00
|
|
|
time_t * m = tr_new0( time_t, n );
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
|
|
|
{
|
2008-04-14 11:52:50 +00:00
|
|
|
struct stat sb;
|
2008-10-14 03:03:29 +00:00
|
|
|
char * path = tr_buildPath( tor->downloadDir, tor->info.files[i].name, NULL );
|
|
|
|
if( !stat( path, &sb ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-04-14 11:52:50 +00:00
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
m[i] = sb.st_mtimespec.tv_sec;
|
|
|
|
#else
|
|
|
|
m[i] = sb.st_mtime;
|
|
|
|
#endif
|
|
|
|
}
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( path );
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*setme_n = n;
|
|
|
|
return m;
|
|
|
|
}
|
2008-06-01 01:40:32 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentSetAnnounceList( tr_torrent * tor,
|
|
|
|
const tr_tracker_info * trackers,
|
|
|
|
int trackerCount )
|
2008-06-01 01:40:32 +00:00
|
|
|
{
|
2008-06-12 16:25:36 +00:00
|
|
|
tr_benc metainfo;
|
2008-06-01 01:40:32 +00:00
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-06-01 01:40:32 +00:00
|
|
|
/* save to the .torrent file */
|
|
|
|
if( !tr_bencLoadFile( tor->info.torrent, &metainfo ) )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
int prevTier = -1;
|
2008-06-01 01:40:32 +00:00
|
|
|
tr_benc * tier = NULL;
|
|
|
|
tr_benc * announceList;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_info tmpInfo;
|
2008-06-01 01:40:32 +00:00
|
|
|
|
|
|
|
/* remove the old fields */
|
|
|
|
tr_bencDictRemove( &metainfo, "announce" );
|
|
|
|
tr_bencDictRemove( &metainfo, "announce-list" );
|
|
|
|
|
|
|
|
/* add the new fields */
|
|
|
|
tr_bencDictAddStr( &metainfo, "announce", trackers[0].announce );
|
|
|
|
announceList = tr_bencDictAddList( &metainfo, "announce-list", 0 );
|
2008-12-02 23:27:45 +00:00
|
|
|
for( i = 0; i < trackerCount; ++i ) {
|
|
|
|
if( prevTier != trackers[i].tier ) {
|
2008-06-01 01:40:32 +00:00
|
|
|
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 ) );
|
2008-10-02 15:53:33 +00:00
|
|
|
if( !tr_metainfoParse( tor->session, &tmpInfo, &metainfo ) )
|
2008-06-01 01:40:32 +00:00
|
|
|
{
|
2008-12-02 23:27:45 +00:00
|
|
|
/* 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 );
|
2008-06-01 01:40:32 +00:00
|
|
|
tr_bencSaveFile( tor->info.torrent, &metainfo );
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
/* cleanup */
|
2008-06-01 01:40:32 +00:00
|
|
|
tr_bencFree( &metainfo );
|
|
|
|
}
|
|
|
|
}
|
2008-06-03 19:22:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
/** @deprecated this method will be removed in 1.40 */
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentSetAddedDate( tr_torrent * tor,
|
|
|
|
time_t t )
|
2008-06-03 19:22:22 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-06-03 19:22:22 +00:00
|
|
|
tor->addedDate = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated this method will be removed in 1.40 */
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentSetActivityDate( tr_torrent * tor,
|
|
|
|
time_t t )
|
2008-06-03 19:22:22 +00:00
|
|
|
{
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-06-03 19:22:22 +00:00
|
|
|
tor->activityDate = t;
|
|
|
|
}
|
2008-06-03 19:42:54 +00:00
|
|
|
|
|
|
|
/** @deprecated this method will be removed in 1.40 */
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
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;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-11-05 05:56:06 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
tr_torrentGetBytesLeftToAllocate( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
const tr_file * it;
|
|
|
|
const tr_file * end;
|
|
|
|
struct stat sb;
|
|
|
|
uint64_t bytesLeft = 0;
|
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-11-05 05:56:06 +00:00
|
|
|
for( it=tor->info.files, end=it+tor->info.fileCount; it!=end; ++it )
|
|
|
|
{
|
|
|
|
if( !it->dnd )
|
|
|
|
{
|
|
|
|
char * path = tr_buildPath( tor->downloadDir, it->name, NULL );
|
|
|
|
|
|
|
|
bytesLeft += it->length;
|
|
|
|
|
|
|
|
if( !stat( path, &sb )
|
|
|
|
&& S_ISREG( sb.st_mode )
|
2008-11-06 02:56:51 +00:00
|
|
|
&& ( (uint64_t)sb.st_size <= it->length ) )
|
2008-11-05 05:56:06 +00:00
|
|
|
bytesLeft -= sb.st_size;
|
|
|
|
|
|
|
|
tr_free( path );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytesLeft;
|
|
|
|
}
|
2008-12-23 16:04:11 +00:00
|
|
|
|
|
|
|
/****
|
|
|
|
***** 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 );
|
2008-12-29 08:54:36 +00:00
|
|
|
|
2008-12-23 16:04:11 +00:00
|
|
|
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 ) );
|
|
|
|
|
2008-12-23 16:04:11 +00:00
|
|
|
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 && d->d_name[0] != '.' ) /* skip dotfiles */
|
|
|
|
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->downloadDir ) + 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
|
|
|
|
deleteLocalData( tr_torrent * tor, tr_fileFunc fileFunc )
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
char ** s;
|
|
|
|
tr_file_index_t f;
|
2008-12-29 08:54:36 +00:00
|
|
|
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 */
|
2008-12-23 16:04:11 +00:00
|
|
|
|
|
|
|
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->downloadDir, tmp, NULL );
|
|
|
|
|
2009-01-30 00:41:08 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-12-23 16:04:11 +00:00
|
|
|
for( f=0; f<tor->info.fileCount; ++f )
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayInsertSorted( &torrentFiles, tor->info.files[f].name, vstrcmp );
|
2008-12-23 16:04:11 +00:00
|
|
|
|
|
|
|
/* build the set of folders and dirtyFolders */
|
2008-12-29 08:54:36 +00:00
|
|
|
walkLocalData( tor, root, root, NULL, &torrentFiles, &folders, &dirtyFolders );
|
2008-12-23 16:04:11 +00:00
|
|
|
|
|
|
|
/* try to remove entire folders first, so that the recycle bin will be tidy */
|
2008-12-29 08:54:36 +00:00
|
|
|
s = (char**) tr_ptrArrayPeek( &folders, &n );
|
2008-12-23 16:04:11 +00:00
|
|
|
for( i=0; i<n; ++i )
|
2008-12-29 08:54:36 +00:00
|
|
|
if( tr_ptrArrayFindSorted( &dirtyFolders, s[i], vstrcmp ) == NULL )
|
2008-12-23 16:04:11 +00:00
|
|
|
fileFunc( s[i] );
|
|
|
|
|
2008-12-29 08:54:36 +00:00
|
|
|
/* now blow away any remaining torrent files, such as torrent files in dirty folders */
|
2008-12-23 16:04:11 +00:00
|
|
|
for( f=0; f<tor->info.fileCount; ++f ) {
|
|
|
|
char * path = tr_buildPath( tor->downloadDir, tor->info.files[f].name, NULL );
|
|
|
|
fileFunc( path );
|
|
|
|
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 */
|
|
|
|
{
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArray cleanFolders = TR_PTR_ARRAY_INIT;
|
|
|
|
s = (char**) tr_ptrArrayPeek( &folders, &n );
|
2008-12-23 16:04:11 +00:00
|
|
|
for( i=0; i<n; ++i )
|
2008-12-29 08:54:36 +00:00
|
|
|
if( tr_ptrArrayFindSorted( &dirtyFolders, s[i], vstrcmp ) == NULL )
|
|
|
|
tr_ptrArrayInsertSorted( &cleanFolders, s[i], compareLongestFirst );
|
|
|
|
s = (char**) tr_ptrArrayPeek( &cleanFolders, &n );
|
2008-12-23 16:04:11 +00:00
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
fileFunc( s[i] );
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayDestruct( &cleanFolders, NULL );
|
2008-12-23 16:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayDestruct( &dirtyFolders, tr_free );
|
|
|
|
tr_ptrArrayDestruct( &folders, tr_free );
|
|
|
|
tr_ptrArrayDestruct( &torrentFiles, NULL );
|
2008-12-23 16:04:11 +00:00
|
|
|
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 ) );
|
|
|
|
|
2008-12-23 16:04:11 +00:00
|
|
|
if( fileFunc == NULL )
|
2009-01-22 21:43:46 +00:00
|
|
|
fileFunc = remove;
|
2008-12-23 16:04:11 +00:00
|
|
|
|
2009-01-12 19:58:16 +00:00
|
|
|
/* close all the files because we're about to delete them */
|
|
|
|
tr_torrentCloseLocalFiles( tor );
|
|
|
|
|
2008-12-23 16:04:11 +00:00
|
|
|
if( tor->info.fileCount > 1 )
|
|
|
|
deleteLocalData( tor, fileFunc );
|
|
|
|
else {
|
2008-12-29 08:54:36 +00:00
|
|
|
/* torrent only has one file */
|
2008-12-23 16:04:11 +00:00
|
|
|
char * path = tr_buildPath( tor->downloadDir, tor->info.files[0].name, NULL );
|
|
|
|
fileFunc( path );
|
|
|
|
tr_free( path );
|
|
|
|
}
|
|
|
|
}
|