2007-01-19 08:36:49 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2007 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2007-01-19 08:36:49 +00:00
|
|
|
#include "transmission.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 */
|
2007-07-10 03:12:46 +00:00
|
|
|
#include "fastresume.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "handshake.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "inout.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "metainfo.h"
|
|
|
|
#include "net.h" /* tr_netNtop */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "peer-mgr.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "platform.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "ratecontrol.h"
|
2007-01-19 08:40:06 +00:00
|
|
|
#include "shared.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "tracker.h"
|
|
|
|
#include "trcompat.h" /* for strlcpy */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "trevent.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2007-01-19 08:36:49 +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
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int
|
|
|
|
tr_torrentExists( tr_handle * handle,
|
|
|
|
const uint8_t * torrentHash )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
return tr_torrentFindFromHash( handle, torrentHash ) != NULL;
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent*
|
|
|
|
tr_torrentFindFromHash( tr_handle * handle,
|
|
|
|
const uint8_t * torrentHash )
|
|
|
|
{
|
|
|
|
tr_torrent * tor;
|
|
|
|
|
|
|
|
for( tor = handle->torrentList; tor; tor = tor->next )
|
|
|
|
if( !memcmp( tor->info.hash, torrentHash, SHA_DIGEST_LENGTH ) )
|
|
|
|
return tor;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent*
|
|
|
|
tr_torrentFindFromObfuscatedHash( tr_handle * handle,
|
|
|
|
const uint8_t * obfuscatedTorrentHash )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor;
|
|
|
|
|
|
|
|
for( tor = handle->torrentList; tor; tor = tor->next )
|
|
|
|
if( !memcmp( tor->obfuscatedHash, obfuscatedTorrentHash, SHA_DIGEST_LENGTH ) )
|
|
|
|
return tor;
|
|
|
|
|
|
|
|
return NULL;
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/***
|
|
|
|
**** LOCKS
|
|
|
|
***/
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( const tr_torrent * tor )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalLock( tor->handle );
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentUnlock( const tr_torrent * tor )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalUnlock( tor->handle );
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-05-28 15:23:28 +00:00
|
|
|
|
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
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentSetSpeedMode( tr_torrent * tor,
|
|
|
|
int up_or_down,
|
|
|
|
tr_speedlimit mode )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_speedlimit * limit = up_or_down==TR_UP
|
2007-07-20 03:24:04 +00:00
|
|
|
? &tor->uploadLimitMode
|
|
|
|
: &tor->downloadLimitMode;
|
|
|
|
*limit = mode;
|
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
|
|
|
|
tr_torrentGetSpeedMode( const tr_torrent * tor,
|
|
|
|
int up_or_down)
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-07-20 03:24:04 +00:00
|
|
|
return up_or_down==TR_UP ? tor->uploadLimitMode
|
|
|
|
: tor->downloadLimitMode;
|
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
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentSetSpeedLimit( tr_torrent * tor,
|
|
|
|
int up_or_down,
|
|
|
|
int single_KiB_sec )
|
2007-07-18 05:27:45 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ratecontrol * rc = up_or_down==TR_UP ? tor->upload : tor->download;
|
2007-07-20 03:24:04 +00:00
|
|
|
tr_rcSetLimit( rc, single_KiB_sec );
|
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
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentGetSpeedLimit( const tr_torrent * tor,
|
|
|
|
int up_or_down )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ratecontrol * rc = up_or_down==TR_UP ? tor->upload : tor->download;
|
2007-07-20 03:24:04 +00:00
|
|
|
return tr_rcGetLimit( rc );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
onTrackerResponse( void * tracker UNUSED, void * vevent, void * user_data )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = (tr_torrent *) user_data;
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_tracker_event_t * event = (tr_tracker_event_t *) vevent;
|
|
|
|
|
|
|
|
switch( event->messageType )
|
|
|
|
{
|
|
|
|
case TR_TRACKER_PEERS:
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerMgrAddPeers( tor->handle->peerMgr,
|
|
|
|
tor->info.hash,
|
|
|
|
TR_PEER_FROM_TRACKER,
|
|
|
|
event->peerCompact,
|
|
|
|
event->peerCount );
|
2007-08-16 20:00:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_WARNING:
|
|
|
|
tr_err( "Tracker: Warning - %s", event->text );
|
|
|
|
tor->error = TR_ERROR_TC_WARNING;
|
|
|
|
strlcpy( tor->errorString, event->text, sizeof(tor->errorString) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_TRACKER_ERROR:
|
|
|
|
tr_err( "Tracker: Error - %s", event->text );
|
|
|
|
tor->error = TR_ERROR_TC_ERROR;
|
|
|
|
strlcpy( tor->errorString, event->text, sizeof(tor->errorString) );
|
|
|
|
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
|
2007-09-20 16:32:01 +00:00
|
|
|
getBytePiece( const tr_info * info, uint64_t byteOffset )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
|
|
|
assert( info != NULL );
|
|
|
|
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
|
2007-09-20 16:32:01 +00:00
|
|
|
initFilePieces ( tr_info * info, int fileIndex )
|
2007-04-02 20:38:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_file * file = &info->files[fileIndex];
|
2007-06-26 18:45:03 +00:00
|
|
|
uint64_t firstByte, lastByte;
|
2007-04-02 20:38:23 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
assert( info != NULL );
|
|
|
|
assert( 0<=fileIndex && 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;
|
|
|
|
lastByte = firstByte + (file->length ? file->length-1 : 0);
|
|
|
|
file->firstPiece = getBytePiece( info, firstByte );
|
|
|
|
file->lastPiece = getBytePiece( info, lastByte );
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_priority_t
|
2007-09-20 16:32:01 +00:00
|
|
|
calculatePiecePriority ( const tr_torrent * tor,
|
|
|
|
int piece )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
|
|
|
int i;
|
2007-07-10 03:12:46 +00:00
|
|
|
tr_priority_t priority = TR_PRI_NORMAL;
|
2007-06-26 18:45:03 +00:00
|
|
|
|
|
|
|
for( i=0; 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-06-26 18:45:03 +00:00
|
|
|
if ( file->firstPiece <= piece
|
|
|
|
&& file->lastPiece >= piece
|
|
|
|
&& file->priority > priority)
|
|
|
|
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... */
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
int i;
|
|
|
|
uint64_t offset = 0;
|
2007-04-02 20:38:23 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
assert( tor != NULL );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
for( i=0; i<tor->info.fileCount; ++i ) {
|
|
|
|
tor->info.files[i].offset = offset;
|
|
|
|
offset += tor->info.files[i].length;
|
|
|
|
initFilePieces( &tor->info, i );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
for( i=0; i<tor->info.pieceCount; ++i )
|
2007-07-28 15:43:34 +00:00
|
|
|
tor->info.pieces[i].priority = calculatePiecePriority( tor, i );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
static void
|
2007-09-20 20:14:13 +00:00
|
|
|
torrentRealInit( tr_handle * h,
|
|
|
|
tr_torrent * tor,
|
|
|
|
const char * destination,
|
|
|
|
int flags )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-10-04 19:58:30 +00:00
|
|
|
int doStart;
|
2007-07-23 03:00:20 +00:00
|
|
|
uint64_t loaded;
|
2007-07-28 15:43:34 +00:00
|
|
|
uint64_t t;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_bitfield * uncheckedPieces;
|
|
|
|
tr_info * info = &tor->info;
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-30 21:29:49 +00:00
|
|
|
tor->info.flags |= flags;
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalLock( h );
|
2007-01-29 08:24:09 +00:00
|
|
|
|
2007-10-13 15:29:15 +00:00
|
|
|
tor->destination = tr_strdup( destination );
|
|
|
|
|
2007-01-27 21:17:10 +00:00
|
|
|
tor->handle = h;
|
2007-03-31 19:19:09 +00:00
|
|
|
tor->pexDisabled = 0;
|
|
|
|
|
2007-07-28 15:43:34 +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
|
|
|
|
*/
|
|
|
|
tor->blockSize = info->pieceSize;
|
|
|
|
while( tor->blockSize > (1024*16) )
|
|
|
|
tor->blockSize /= 2;
|
|
|
|
|
|
|
|
tor->lastPieceSize = info->totalSize % info->pieceSize;
|
|
|
|
|
2007-07-28 16:31:08 +00:00
|
|
|
if( !tor->lastPieceSize )
|
|
|
|
tor->lastPieceSize = info->pieceSize;
|
|
|
|
|
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 )
|
|
|
|
tor->lastBlockSize = tor->blockSize;
|
|
|
|
|
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 );
|
|
|
|
|
2007-01-19 08:36:49 +00:00
|
|
|
tor->completion = tr_cpInit( tor );
|
|
|
|
|
2007-07-25 01:59:46 +00:00
|
|
|
tr_torrentInitFilePieces( tor );
|
|
|
|
|
2007-01-19 08:36:49 +00:00
|
|
|
tor->upload = tr_rcInit();
|
|
|
|
tor->download = tr_rcInit();
|
|
|
|
tor->swarmspeed = tr_rcInit();
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
tr_sha1( tor->obfuscatedHash, "req2", 4,
|
|
|
|
info->hash, SHA_DIGEST_LENGTH,
|
|
|
|
NULL );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerMgrAddTorrent( h->peerMgr, tor );
|
|
|
|
|
2007-01-27 21:17:10 +00:00
|
|
|
if( !h->isPortSet )
|
2007-01-19 08:36:49 +00:00
|
|
|
tr_setBindPort( h, TR_DEFAULT_PORT );
|
|
|
|
|
2007-07-09 23:09:00 +00:00
|
|
|
assert( !tor->downloadedCur );
|
|
|
|
assert( !tor->uploadedCur );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tor->error = TR_OK;
|
2007-07-23 03:00:20 +00:00
|
|
|
|
|
|
|
uncheckedPieces = tr_bitfieldNew( tor->info.pieceCount );
|
2007-08-13 16:43:33 +00:00
|
|
|
|
2007-10-13 15:29:15 +00:00
|
|
|
loaded = tr_fastResumeLoad( tor, uncheckedPieces );
|
2007-10-13 13:54:05 +00:00
|
|
|
assert( tor->destination != NULL );
|
|
|
|
|
2007-08-13 16:43:33 +00:00
|
|
|
/* the `paused' flag has highest precedence...
|
|
|
|
after that, the fastresume setting is used...
|
|
|
|
if that's not found, default to RUNNING */
|
|
|
|
if( flags & TR_FLAG_PAUSED )
|
2007-10-04 19:58:30 +00:00
|
|
|
doStart = 0;
|
|
|
|
else if( loaded & TR_FR_RUN )
|
|
|
|
doStart = tor->isRunning;
|
|
|
|
else
|
|
|
|
doStart = 1;
|
|
|
|
tor->isRunning = 0;
|
2007-08-13 16:43:33 +00:00
|
|
|
|
2007-07-23 03:00:20 +00:00
|
|
|
if( tr_bitfieldIsEmpty( uncheckedPieces ) )
|
|
|
|
tr_bitfieldFree( uncheckedPieces );
|
2007-07-24 00:09:23 +00:00
|
|
|
else
|
2007-07-23 03:00:20 +00:00
|
|
|
tor->uncheckedPieces = uncheckedPieces;
|
|
|
|
|
|
|
|
|
|
|
|
if( !(loaded & TR_FR_SPEEDLIMIT ) ) {
|
|
|
|
int limit, enabled;
|
|
|
|
tr_getGlobalSpeedLimit( tor->handle, TR_UP, &enabled, &limit );
|
|
|
|
tr_torrentSetSpeedLimit( tor, TR_UP, limit );
|
|
|
|
tr_getGlobalSpeedLimit( tor->handle, TR_DOWN, &enabled, &limit );
|
|
|
|
tr_torrentSetSpeedLimit( tor, TR_DOWN, limit );
|
|
|
|
}
|
|
|
|
|
2007-07-02 01:29:05 +00:00
|
|
|
tor->cpStatus = 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 );
|
|
|
|
tor->trackerSubscription = tr_trackerSubscribe( tor->tracker, onTrackerResponse, tor );
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tor->next = h->torrentList;
|
|
|
|
h->torrentList = tor;
|
|
|
|
h->torrentCount++;
|
2007-10-01 15:17:15 +00:00
|
|
|
|
|
|
|
tr_globalUnlock( h );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
if( doStart )
|
|
|
|
tr_torrentStart( tor );
|
2007-06-27 05:14:38 +00:00
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
static int
|
2007-09-20 20:14:13 +00:00
|
|
|
pathIsInUse ( const tr_handle * h,
|
|
|
|
const char * destination,
|
|
|
|
const char * name )
|
2007-06-27 05:14:38 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_torrent * tor;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
|
|
|
for( tor=h->torrentList; tor; tor=tor->next )
|
|
|
|
if( !strcmp( destination, tor->destination )
|
|
|
|
&& !strcmp( name, tor->info.name ) )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-09-20 20:14:13 +00:00
|
|
|
hashExists( const tr_handle * h,
|
|
|
|
const uint8_t * hash )
|
2007-06-27 05:14:38 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_torrent * tor;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
|
|
|
for( tor=h->torrentList; tor; tor=tor->next )
|
|
|
|
if( !memcmp( hash, tor->info.hash, SHA_DIGEST_LENGTH ) )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-09-20 20:14:13 +00:00
|
|
|
infoCanAdd( const tr_handle * h,
|
|
|
|
const char * destination,
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_info * info )
|
2007-06-27 05:14:38 +00:00
|
|
|
{
|
|
|
|
if( hashExists( h, info->hash ) )
|
|
|
|
return TR_EDUPLICATE;
|
|
|
|
|
2007-06-27 23:22:09 +00:00
|
|
|
if( destination && pathIsInUse( h, destination, info->name ) )
|
|
|
|
return TR_EDUPLICATE;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
|
|
|
return TR_OK;
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
int
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrentParse( const tr_handle * h,
|
|
|
|
const char * path,
|
|
|
|
const char * destination,
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_info * setme_info )
|
2007-06-27 05:14:38 +00:00
|
|
|
{
|
2007-08-19 04:03:06 +00:00
|
|
|
int ret, doFree;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_info tmp;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
if( setme_info == NULL )
|
|
|
|
setme_info = &tmp;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
memset( setme_info, 0, sizeof( tr_info ) );
|
2007-06-27 14:54:31 +00:00
|
|
|
ret = tr_metainfoParseFile( setme_info, h->tag, path, FALSE );
|
2007-08-19 04:03:06 +00:00
|
|
|
doFree = !ret && (setme_info == &tmp);
|
2007-06-27 14:54:31 +00:00
|
|
|
|
2007-06-27 23:22:09 +00:00
|
|
|
if( ret == TR_OK )
|
|
|
|
ret = infoCanAdd( h, destination, setme_info );
|
2007-06-27 14:54:31 +00:00
|
|
|
|
2007-08-19 04:03:06 +00:00
|
|
|
if( doFree )
|
2007-06-28 01:12:16 +00:00
|
|
|
tr_metainfoFree( &tmp );
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return ret;
|
2007-06-27 05:14:38 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent *
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrentInit( tr_handle * h,
|
|
|
|
const char * path,
|
|
|
|
const char * destination,
|
|
|
|
int flags,
|
|
|
|
int * error )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-27 05:14:38 +00:00
|
|
|
int val;
|
2007-07-25 18:43:21 +00:00
|
|
|
int tmpError;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2007-05-24 15:57:04 +00:00
|
|
|
|
2007-07-25 18:43:21 +00:00
|
|
|
if( !error )
|
|
|
|
error = &tmpError;
|
|
|
|
|
2007-06-28 00:04:42 +00:00
|
|
|
if(( val = tr_torrentParse( h, path, destination, NULL )))
|
2007-06-27 05:14:38 +00:00
|
|
|
*error = val;
|
2007-09-20 16:32:01 +00:00
|
|
|
else if(!(( tor = tr_new0( tr_torrent, 1 ))))
|
2007-06-27 05:14:38 +00:00
|
|
|
*error = TR_EOTHER;
|
|
|
|
else {
|
|
|
|
tr_metainfoParseFile( &tor->info, h->tag, path, TR_FLAG_SAVE & flags );
|
|
|
|
torrentRealInit( h, tor, destination, flags );
|
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-06-28 23:35:02 +00:00
|
|
|
int
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrentParseHash( const tr_handle * h,
|
|
|
|
const char * hashStr,
|
|
|
|
const char * destination,
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_info * setme_info )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-08-19 04:03:06 +00:00
|
|
|
int ret, doFree;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_info tmp;
|
2007-06-27 14:54:31 +00:00
|
|
|
|
|
|
|
if( setme_info == NULL )
|
|
|
|
setme_info = &tmp;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
memset( setme_info, 0, sizeof( tr_info ) );
|
2007-06-27 14:54:31 +00:00
|
|
|
ret = tr_metainfoParseHash( setme_info, h->tag, hashStr );
|
2007-08-19 04:03:06 +00:00
|
|
|
doFree = !ret && (setme_info == &tmp);
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-27 23:22:09 +00:00
|
|
|
if( ret == TR_OK )
|
|
|
|
ret = infoCanAdd( h, destination, setme_info );
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2007-08-19 04:03:06 +00:00
|
|
|
if( doFree )
|
2007-06-28 01:12:16 +00:00
|
|
|
tr_metainfoFree( &tmp );
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return ret;
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent *
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrentInitSaved( tr_handle * h,
|
|
|
|
const char * hashStr,
|
|
|
|
const char * destination,
|
|
|
|
int flags,
|
|
|
|
int * error )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2007-06-27 05:14:38 +00:00
|
|
|
int val;
|
2007-07-25 18:43:21 +00:00
|
|
|
int tmpError;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2007-07-25 18:43:21 +00:00
|
|
|
if( !error )
|
|
|
|
error = &tmpError;
|
|
|
|
|
2007-06-27 23:22:09 +00:00
|
|
|
if(( val = tr_torrentParseHash( h, hashStr, destination, NULL )))
|
2007-06-27 05:14:38 +00:00
|
|
|
*error = val;
|
2007-09-20 16:32:01 +00:00
|
|
|
else if(!(( tor = tr_new0( tr_torrent, 1 ))))
|
2007-06-26 18:45:03 +00:00
|
|
|
*error = TR_EOTHER;
|
2007-06-27 05:14:38 +00:00
|
|
|
else {
|
|
|
|
tr_metainfoParseHash( &tor->info, h->tag, hashStr );
|
|
|
|
torrentRealInit( h, tor, destination, (TR_FLAG_SAVE|flags) );
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
return tor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrentParseData( const tr_handle * h,
|
|
|
|
const uint8_t * data,
|
|
|
|
size_t size,
|
|
|
|
const char * destination,
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_info * setme_info )
|
2007-06-27 05:14:38 +00:00
|
|
|
{
|
2007-08-19 04:03:06 +00:00
|
|
|
int ret, doFree;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_info tmp;
|
2007-06-27 14:54:31 +00:00
|
|
|
|
|
|
|
if( setme_info == NULL )
|
|
|
|
setme_info = &tmp;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
memset( setme_info, 0, sizeof( tr_info ) );
|
2007-06-27 14:54:31 +00:00
|
|
|
ret = tr_metainfoParseData( setme_info, h->tag, data, size, FALSE );
|
2007-08-19 04:03:06 +00:00
|
|
|
doFree = !ret && (setme_info == &tmp);
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2007-06-27 23:22:09 +00:00
|
|
|
if( ret == TR_OK )
|
|
|
|
ret = infoCanAdd( h, destination, setme_info );
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2007-08-19 04:03:06 +00:00
|
|
|
if( doFree )
|
2007-06-28 01:12:16 +00:00
|
|
|
tr_metainfoFree( &tmp );
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return ret;
|
2007-06-27 05:14:38 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent *
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrentInitData( tr_handle * h,
|
2007-06-27 14:54:31 +00:00
|
|
|
const uint8_t * data,
|
|
|
|
size_t size,
|
|
|
|
const char * destination,
|
|
|
|
int flags,
|
|
|
|
int * error )
|
2007-06-27 05:14:38 +00:00
|
|
|
{
|
|
|
|
int val;
|
2007-07-25 18:43:21 +00:00
|
|
|
int tmpError;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
2007-07-25 18:43:21 +00:00
|
|
|
if( !error )
|
|
|
|
error = &tmpError;
|
|
|
|
|
2007-06-27 23:22:09 +00:00
|
|
|
if(( val = tr_torrentParseData( h, data, size, destination, NULL )))
|
2007-06-27 05:14:38 +00:00
|
|
|
*error = val;
|
2007-09-20 16:32:01 +00:00
|
|
|
else if(!(( tor = tr_new0( tr_torrent, 1 ))))
|
2007-06-27 05:14:38 +00:00
|
|
|
*error = TR_EOTHER;
|
|
|
|
else {
|
|
|
|
tr_metainfoParseData( &tor->info, h->tag, data, size, TR_FLAG_SAVE & flags );
|
|
|
|
torrentRealInit( h, tor, destination, flags );
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
return tor;
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_info *
|
|
|
|
tr_torrentInfo( const tr_torrent * tor )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
|
|
|
return &tor->info;
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-07-15 17:36:56 +00:00
|
|
|
#if 0
|
2007-09-20 16:32:01 +00:00
|
|
|
int tr_torrentScrape( tr_torrent * tor, int * s, int * l, int * d )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
return tr_trackerScrape( tor, s, l, d );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
2007-07-15 17:36:56 +00:00
|
|
|
#endif
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static int
|
|
|
|
saveFastResumeNow( void * vtor )
|
2007-07-23 03:00:20 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = (tr_torrent *) vtor;
|
|
|
|
|
2007-07-23 03:00:20 +00:00
|
|
|
tr_fastResumeSave( tor );
|
2007-09-23 13:53:44 +00:00
|
|
|
tr_torrentRecheckCompleteness( tor );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
tr_timerFree( &tor->saveTimer );
|
|
|
|
return FALSE;
|
2007-07-23 03:00:20 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
|
|
|
saveFastResumeSoon( void * vtor )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = (tr_torrent *) vtor;
|
|
|
|
|
|
|
|
if( tor->saveTimer == NULL )
|
2007-09-23 14:17:39 +00:00
|
|
|
tor->saveTimer = tr_timerNew( tor->handle, saveFastResumeNow, tor, 100 );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-07-23 03:00:20 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentSetFolder( tr_torrent * tor, const char * path )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_free( tor->destination );
|
|
|
|
tor->destination = tr_strdup( path );
|
2007-09-20 16:32:01 +00:00
|
|
|
saveFastResumeSoon( 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*
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentGetFolder( const tr_torrent * tor )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
|
|
|
return tor->destination;
|
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
|
|
|
{
|
|
|
|
if( tor->tracker )
|
|
|
|
tr_trackerChangeMyPort( tor->tracker );
|
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-10-14 17:21:24 +00:00
|
|
|
int
|
|
|
|
tr_torrentIsPexEnabled( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
if( tor->info.flags & TR_FLAG_PRIVATE )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if( tor->pexDisabled )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
|
|
|
tr_torrentDisablePex( tr_torrent * tor, int disable )
|
2007-03-31 19:19:09 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( tor != NULL );
|
|
|
|
assert( disable==0 || disable==1 );
|
2007-03-31 19:19:09 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/* pex is ALWAYS disabled for private torrents */
|
|
|
|
if( tor->info.flags & TR_FLAG_PRIVATE )
|
|
|
|
disable = TRUE;
|
2007-03-31 19:19:09 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tor->pexDisabled = disable;
|
2007-03-31 19:19:09 +00:00
|
|
|
}
|
|
|
|
|
2007-07-30 17:11:00 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_manualUpdate( tr_torrent * tor )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-10-04 19:58:30 +00:00
|
|
|
if( tor->isRunning )
|
|
|
|
tr_trackerReannounce( tor->tracker );
|
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
|
|
|
{
|
|
|
|
return ( tor != NULL )
|
2007-10-04 19:58:30 +00:00
|
|
|
&& ( tor->isRunning )
|
2007-07-30 17:11:00 +00:00
|
|
|
&& ( tr_trackerCanManualAnnounce( tor->tracker ) );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_stat *
|
|
|
|
tr_torrentStat( tr_torrent * tor )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_stat * s;
|
|
|
|
struct tr_tracker * tc;
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-09-21 14:20:55 +00:00
|
|
|
tor->statCur = !tor->statCur;
|
2007-01-19 08:36:49 +00:00
|
|
|
s = &tor->stats[tor->statCur];
|
|
|
|
|
|
|
|
s->error = tor->error;
|
|
|
|
memcpy( s->errorString, tor->errorString,
|
|
|
|
sizeof( s->errorString ) );
|
|
|
|
|
|
|
|
tc = tor->tracker;
|
2007-08-16 20:00:06 +00:00
|
|
|
s->tracker = tr_trackerGetAddress( tor->tracker );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerMgrTorrentStats( tor->handle->peerMgr,
|
|
|
|
tor->info.hash,
|
2007-10-01 15:51:54 +00:00
|
|
|
&s->peersKnown,
|
2007-09-20 16:32:01 +00:00
|
|
|
&s->peersConnected,
|
|
|
|
&s->peersSendingToUs,
|
|
|
|
&s->peersGettingFromUs,
|
|
|
|
s->peersFrom );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-07-15 17:19:07 +00:00
|
|
|
s->percentComplete = tr_cpPercentComplete ( tor->completion );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
s->percentDone = tr_cpPercentDone( tor->completion );
|
2007-09-26 01:55:04 +00:00
|
|
|
s->leftUntilDone = tr_cpLeftUntilDone( tor->completion );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
if( tor->recheckState == TR_RECHECK_NOW )
|
|
|
|
s->status = TR_STATUS_CHECK;
|
|
|
|
else if( tor->recheckState == TR_RECHECK_WAIT )
|
|
|
|
s->status = TR_STATUS_CHECK_WAIT;
|
|
|
|
else if( !tor->isRunning )
|
|
|
|
s->status = TR_STATUS_STOPPED;
|
|
|
|
else if( tor->cpStatus == TR_CP_INCOMPLETE )
|
|
|
|
s->status = TR_STATUS_DOWNLOAD;
|
|
|
|
else if( tor->cpStatus == TR_CP_DONE )
|
|
|
|
s->status = TR_STATUS_DONE;
|
|
|
|
else
|
|
|
|
s->status = TR_STATUS_SEED;
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-07-24 01:33:59 +00:00
|
|
|
s->recheckProgress = (tor->uncheckedPieces == NULL)
|
|
|
|
? 0.0
|
|
|
|
: 1.0 - ((double)tr_bitfieldCountTrueBits(tor->uncheckedPieces) / tor->info.pieceCount);
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
s->cpStatus = tor->cpStatus;
|
|
|
|
|
2007-10-13 23:57:13 +00:00
|
|
|
/* rcRate's averaging code can make it appear that we're
|
|
|
|
* still sending bytes after a torrent stops or all the
|
|
|
|
* peers disconnect, so short-circuit that appearance here */
|
|
|
|
if( tor->isRunning && s->peersConnected ) {
|
|
|
|
s->rateDownload = tr_rcRate( tor->download );
|
|
|
|
s->rateUpload = tr_rcRate( tor->upload );
|
|
|
|
} else {
|
|
|
|
s->rateDownload = 0.0;
|
|
|
|
s->rateUpload = 0.0;
|
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
tr_trackerGetCounts( tc,
|
|
|
|
&s->completedFromTracker,
|
|
|
|
&s->leechers,
|
|
|
|
&s->seeders );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
|
|
|
s->swarmspeed = tr_rcRate( tor->swarmspeed );
|
2007-05-25 19:14:42 +00:00
|
|
|
|
|
|
|
s->startDate = tor->startDate;
|
|
|
|
s->activityDate = tor->activityDate;
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
s->eta = s->rateDownload < 0.1
|
|
|
|
? -1.0f
|
2007-09-26 01:55:04 +00:00
|
|
|
: (s->leftUntilDone / s->rateDownload / 1024.0);
|
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;
|
|
|
|
s->haveValid = tr_cpHaveValid( tor->completion );
|
|
|
|
s->haveUnchecked = tr_cpHaveTotal( tor->completion ) - s->haveValid;
|
2007-09-27 03:03:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
tr_bitfield * available = tr_peerMgrGetAvailable( tor->handle->peerMgr,
|
|
|
|
tor->info.hash );
|
2007-09-27 03:30:22 +00:00
|
|
|
s->desiredSize = 0;
|
|
|
|
s->desiredAvailable = 0;
|
2007-09-27 03:03:38 +00:00
|
|
|
|
|
|
|
for( i=0; i<tor->info.pieceCount; ++i ) {
|
|
|
|
if( !tor->info.pieces[i].dnd ) {
|
2007-09-27 03:30:22 +00:00
|
|
|
s->desiredSize += tor->info.pieceSize;
|
2007-09-27 03:03:38 +00:00
|
|
|
if( tr_bitfieldHas( available, i ) )
|
2007-09-27 03:30:22 +00:00
|
|
|
s->desiredAvailable += tor->info.pieceSize;
|
2007-09-27 03:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bitfieldFree( available );
|
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-09-26 01:55:04 +00:00
|
|
|
s->ratio = s->downloadedEver ? s->uploadedEver / (float)s->downloadedEver
|
|
|
|
: TR_RATIO_NA;
|
2007-01-19 08:36:49 +00:00
|
|
|
|
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
|
2007-09-20 16:32:01 +00:00
|
|
|
fileBytesCompleted ( const tr_torrent * tor, int fileIndex )
|
2007-07-15 20:29:57 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_file * file = &tor->info.files[fileIndex];
|
2007-07-15 20:29:57 +00:00
|
|
|
const uint64_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 uint64_t lastBlock = (file->offset + lastOffset) / tor->blockSize;
|
|
|
|
const uint64_t lastBlockOffset = (file->offset + lastOffset) % tor->blockSize;
|
|
|
|
uint64_t haveBytes = 0;
|
|
|
|
|
|
|
|
assert( tor != NULL );
|
|
|
|
assert( 0<=fileIndex && fileIndex<tor->info.fileCount );
|
|
|
|
assert( file->offset + file->length <= tor->info.totalSize );
|
2007-08-07 14:17:37 +00:00
|
|
|
assert( ( (int)firstBlock < tor->blockCount ) || (!file->length && file->offset==tor->info.totalSize) );
|
|
|
|
assert( ( (int)lastBlock < tor->blockCount ) || (!file->length && file->offset==tor->info.totalSize) );
|
2007-07-15 20:29:57 +00:00
|
|
|
assert( firstBlock <= lastBlock );
|
2007-07-28 15:43:34 +00:00
|
|
|
assert( (int)tr_torBlockPiece( tor, firstBlock ) == file->firstPiece );
|
|
|
|
assert( (int)tr_torBlockPiece( tor, lastBlock ) == file->lastPiece );
|
2007-07-15 20:29:57 +00:00
|
|
|
|
|
|
|
if( firstBlock == lastBlock )
|
|
|
|
{
|
|
|
|
if( tr_cpBlockIsComplete( tor->completion, firstBlock ) )
|
|
|
|
haveBytes += lastBlockOffset + 1 - firstBlockOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint64_t i;
|
|
|
|
|
|
|
|
if( tr_cpBlockIsComplete( tor->completion, firstBlock ) )
|
|
|
|
haveBytes += tor->blockSize - firstBlockOffset;
|
|
|
|
|
|
|
|
for( i=firstBlock+1; i<lastBlock; ++i )
|
|
|
|
if( tr_cpBlockIsComplete( tor->completion, i ) )
|
|
|
|
haveBytes += tor->blockSize;
|
|
|
|
|
|
|
|
if( tr_cpBlockIsComplete( tor->completion, lastBlock ) )
|
|
|
|
haveBytes += lastBlockOffset + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return haveBytes;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_file_stat *
|
|
|
|
tr_torrentFiles( const tr_torrent * tor, int * fileCount )
|
2007-07-15 20:05:32 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const int n = tor->info.fileCount;
|
2007-09-20 16:32:01 +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
|
|
|
|
|
|
|
for( i=0; i<n; ++i, ++walk )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_file * file = tor->info.files + i;
|
2007-07-15 20:05:32 +00:00
|
|
|
cp_status_t cp;
|
|
|
|
|
2007-07-15 20:29:57 +00:00
|
|
|
walk->bytesCompleted = fileBytesCompleted( tor, i );
|
2007-07-15 20:05:32 +00:00
|
|
|
|
2007-07-16 11:19:44 +00:00
|
|
|
walk->progress = file->length
|
|
|
|
? walk->bytesCompleted / (float)file->length
|
2007-07-15 20:29:57 +00:00
|
|
|
: 1.0;
|
2007-07-15 20:05:32 +00:00
|
|
|
|
2007-07-16 11:19:44 +00:00
|
|
|
if( walk->bytesCompleted >= file->length )
|
2007-07-15 20:05:32 +00:00
|
|
|
cp = TR_CP_COMPLETE;
|
|
|
|
else if( tor->info.files[i].dnd )
|
|
|
|
cp = TR_CP_DONE;
|
|
|
|
else
|
|
|
|
cp = TR_CP_INCOMPLETE;
|
|
|
|
|
|
|
|
walk->completionStatus = cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
*fileCount = n;
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentFilesFree( tr_file_stat * files, int fileCount UNUSED )
|
2007-07-15 20:05:32 +00:00
|
|
|
{
|
|
|
|
tr_free( files );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peer_stat *
|
|
|
|
tr_torrentPeers( const tr_torrent * tor, int * peerCount )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
return tr_peerMgrPeerStats( tor->handle->peerMgr,
|
|
|
|
tor->info.hash, peerCount );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-10-02 03:33:17 +00:00
|
|
|
void
|
|
|
|
tr_torrentPeersFree( tr_peer_stat * peers, int peerCount )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-10-02 03:33:17 +00:00
|
|
|
int i;
|
|
|
|
for( i=0; i<peerCount; ++i )
|
|
|
|
tr_free( (char*) peers[i].client );
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_free( peers );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_torrentAvailability( const tr_torrent * tor, int8_t * tab, int size )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
return tr_peerMgrTorrentAvailability( tor->handle->peerMgr,
|
|
|
|
tor->info.hash,
|
|
|
|
tab, size );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void tr_torrentAmountFinished( const tr_torrent * tor, float * tab, int size )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
int i;
|
2007-01-31 16:23:07 +00:00
|
|
|
float interval;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-01-31 16:23:07 +00:00
|
|
|
interval = (float)tor->info.pieceCount / (float)size;
|
2007-01-19 08:36:49 +00:00
|
|
|
for( i = 0; i < size; i++ )
|
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
int piece = i * interval;
|
2007-01-19 08:36:49 +00:00
|
|
|
tab[i] = tr_cpPercentBlocksInPiece( tor->completion, piece );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentSetHasPiece( tr_torrent * tor, int pieceIndex, int has )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
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
|
|
|
if( has )
|
|
|
|
tr_cpPieceAdd( tor->completion, pieceIndex );
|
|
|
|
else
|
|
|
|
tr_cpPieceRem( tor->completion, pieceIndex );
|
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-09-20 16:32:01 +00:00
|
|
|
void tr_torrentRemoveSaved( tr_torrent * tor )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
|
|
|
tr_metainfoRemoveSaved( tor->info.hashString, tor->handle->tag );
|
|
|
|
}
|
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;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_handle * h = tor->handle;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_info * inf = &tor->info;
|
|
|
|
|
|
|
|
assert( tor != NULL );
|
2007-10-04 19:58:30 +00:00
|
|
|
assert( !tor->isRunning );
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalLock( h );
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-09-23 19:24:06 +00:00
|
|
|
tr_peerMgrRemoveTorrent( h->peerMgr, tor->info.hash );
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_cpClose( tor->completion );
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_rcClose( tor->upload );
|
|
|
|
tr_rcClose( tor->download );
|
|
|
|
tr_rcClose( 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;
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_free( tor->destination );
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
if( tor == h->torrentList )
|
|
|
|
h->torrentList = tor->next;
|
|
|
|
else for( t=h->torrentList; t!=NULL; t=t->next ) {
|
|
|
|
if( t->next == tor ) {
|
|
|
|
t->next = tor->next;
|
|
|
|
break;
|
2007-05-28 16:40:56 +00:00
|
|
|
}
|
2007-05-28 15:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( h->torrentCount >= 1 );
|
2007-06-30 13:19:57 +00:00
|
|
|
h->torrentCount--;
|
|
|
|
|
2007-07-21 18:31:08 +00:00
|
|
|
tr_inf( "closing torrent %s... %d torrents left",
|
2007-07-21 17:35:47 +00:00
|
|
|
tor->info.name, h->torrentCount );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_metainfoFree( inf );
|
2007-07-21 17:35:47 +00:00
|
|
|
tr_free( tor );
|
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalUnlock( h );
|
2007-05-28 15:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
AFTER_RECHECK_NONE,
|
|
|
|
AFTER_RECHECK_START,
|
|
|
|
AFTER_RECHECK_STOP,
|
|
|
|
AFTER_RECHECK_CLOSE
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
checkAndStartCB( tr_torrent * tor )
|
|
|
|
{
|
|
|
|
tr_globalLock( tor->handle );
|
|
|
|
|
|
|
|
tor->isRunning = 1;
|
|
|
|
*tor->errorString = '\0';
|
|
|
|
tr_torrentResetTransferStats( tor );
|
|
|
|
tr_torrentRecheckCompleteness( tor );
|
|
|
|
tor->startDate = tr_date( );
|
|
|
|
tr_trackerStart( tor->tracker );
|
|
|
|
tr_peerMgrStartTorrent( tor->handle->peerMgr, tor->info.hash );
|
|
|
|
|
|
|
|
tr_globalUnlock( tor->handle );
|
|
|
|
}
|
2007-10-04 23:47:49 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentStart( tr_torrent * tor )
|
2007-07-19 03:48:27 +00:00
|
|
|
{
|
2007-10-04 19:58:30 +00:00
|
|
|
tr_globalLock( tor->handle );
|
2007-07-19 03:48:27 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
if( !tor->isRunning )
|
|
|
|
{
|
|
|
|
tor->isRunning = 1;
|
|
|
|
tr_ioRecheckAdd( tor, checkAndStartCB );
|
|
|
|
}
|
2007-07-19 03:48:27 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
tr_globalUnlock( tor->handle );
|
2007-07-19 03:48:27 +00:00
|
|
|
}
|
|
|
|
|
2007-10-02 02:59:07 +00:00
|
|
|
void
|
2007-10-04 19:58:30 +00:00
|
|
|
tr_torrentRecheck( tr_torrent * tor )
|
|
|
|
{
|
2007-10-04 23:47:49 +00:00
|
|
|
tr_globalLock( tor->handle );
|
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
if( !tor->uncheckedPieces )
|
|
|
|
tor->uncheckedPieces = tr_bitfieldNew( tor->info.pieceCount );
|
|
|
|
tr_bitfieldAddRange( tor->uncheckedPieces, 0, tor->info.pieceCount );
|
|
|
|
|
2007-10-07 04:32:37 +00:00
|
|
|
tr_ioRecheckAdd( tor, tr_torrentRecheckCompleteness );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
tr_globalUnlock( tor->handle );
|
|
|
|
}
|
2007-07-12 20:48:13 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
stopTorrent( void * vtor )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = vtor;
|
|
|
|
tr_ioRecheckRemove( tor );
|
|
|
|
tr_peerMgrStopTorrent( tor->handle->peerMgr, tor->info.hash );
|
|
|
|
tr_trackerStop( tor->tracker );
|
|
|
|
tr_ioClose( tor );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentStop( tr_torrent * tor )
|
|
|
|
{
|
|
|
|
tr_globalLock( tor->handle );
|
|
|
|
|
|
|
|
saveFastResumeNow( tor );
|
|
|
|
tor->isRunning = 0;
|
|
|
|
tr_runInEventThread( tor->handle, stopTorrent, tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-10-02 02:59:07 +00:00
|
|
|
tr_globalUnlock( tor->handle );
|
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;
|
|
|
|
saveFastResumeNow( tor );
|
|
|
|
tor->isRunning = 0;
|
|
|
|
stopTorrent( tor );
|
|
|
|
freeTorrent( tor );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
|
|
|
tr_torrentClose( tr_torrent * tor )
|
|
|
|
{
|
2007-10-02 02:59:07 +00:00
|
|
|
tr_globalLock( tor->handle );
|
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
tr_runInEventThread( tor->handle, closeTorrent, tor );
|
2007-10-02 02:59:07 +00:00
|
|
|
|
|
|
|
tr_globalUnlock( tor->handle );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-09-28 14:51:34 +00:00
|
|
|
/**
|
|
|
|
*** Completeness
|
|
|
|
**/
|
|
|
|
|
|
|
|
static void
|
|
|
|
fireStatusChange( tr_torrent * tor, cp_status_t status )
|
|
|
|
{
|
|
|
|
assert( tor != NULL );
|
|
|
|
assert( status==TR_CP_INCOMPLETE || status==TR_CP_DONE || status==TR_CP_COMPLETE );
|
|
|
|
|
|
|
|
if( tor->status_func != NULL )
|
|
|
|
(tor->status_func)( tor, status, tor->status_func_user_data );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentSetStatusCallback( tr_torrent * tor,
|
|
|
|
tr_torrent_status_func func,
|
|
|
|
void * user_data )
|
|
|
|
{
|
|
|
|
assert( tor != NULL );
|
|
|
|
tor->status_func = func;
|
|
|
|
tor->status_func_user_data = user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentClearStatusCallback( tr_torrent * torrent )
|
|
|
|
{
|
|
|
|
tr_torrentSetStatusCallback( torrent, NULL, NULL );
|
|
|
|
}
|
|
|
|
|
2007-09-23 13:53:44 +00:00
|
|
|
void
|
|
|
|
tr_torrentRecheckCompleteness( tr_torrent * tor )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
cp_status_t cpStatus;
|
2007-07-15 04:19:39 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-01-21 06:42:05 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
cpStatus = tr_cpGetStatus( tor->completion );
|
|
|
|
if( cpStatus != tor->cpStatus ) {
|
|
|
|
tor->cpStatus = cpStatus;
|
2007-09-28 14:51:34 +00:00
|
|
|
fireStatusChange( tor, cpStatus );
|
2007-09-20 16:32:01 +00:00
|
|
|
if( (cpStatus == TR_CP_COMPLETE) /* ...and if we're complete */
|
|
|
|
&& tor->downloadedCur ) { /* and it just happened */
|
|
|
|
tr_trackerCompleted( tor->tracker ); /* tell the tracker */
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ioClose( tor );
|
|
|
|
saveFastResumeSoon( tor );
|
2007-01-19 15:24:20 +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-07-16 19:57:34 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentSetFilePriority( tr_torrent * tor,
|
|
|
|
int fileIndex,
|
|
|
|
tr_priority_t priority )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
int i;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_file * file;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
assert( tor != NULL );
|
|
|
|
assert( 0<=fileIndex && fileIndex<tor->info.fileCount );
|
2007-07-10 03:12:46 +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;
|
|
|
|
for( i=file->firstPiece; i<=file->lastPiece; ++i )
|
|
|
|
tor->info.pieces[i].priority = calculatePiecePriority( tor, i );
|
|
|
|
|
|
|
|
tr_dbg ( "Setting file #%d (pieces %d-%d) priority to %d (%s)",
|
|
|
|
fileIndex, file->firstPiece, file->lastPiece,
|
|
|
|
priority, tor->info.files[fileIndex].name );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
saveFastResumeSoon( tor );
|
2007-07-10 03:12:46 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentUnlock( tor );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-07-10 03:12:46 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentSetFilePriorities( tr_torrent * tor,
|
|
|
|
int * files,
|
|
|
|
int fileCount,
|
|
|
|
tr_priority_t priority )
|
2007-07-10 03:12:46 +00:00
|
|
|
{
|
|
|
|
int i;
|
2007-07-16 19:57:34 +00:00
|
|
|
for( i=0; i<fileCount; ++i )
|
|
|
|
tr_torrentSetFilePriority( tor, files[i], priority );
|
2007-07-10 03:12:46 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_priority_t
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentGetFilePriority( const tr_torrent * tor, int file )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_priority_t ret;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-06-18 03:40:41 +00:00
|
|
|
assert( tor != NULL );
|
|
|
|
assert( 0<=file && 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
|
|
|
{
|
|
|
|
int i;
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_priority_t * p;
|
|
|
|
|
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 );
|
2007-06-18 03:40:41 +00:00
|
|
|
for( i=0; i<tor->info.fileCount; ++i )
|
|
|
|
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,
|
|
|
|
int file )
|
2007-07-10 03:12:46 +00:00
|
|
|
{
|
2007-07-16 19:57:34 +00:00
|
|
|
int doDownload;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-07-10 03:12:46 +00:00
|
|
|
|
|
|
|
assert( 0<=file && 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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentSetFileDL( tr_torrent * tor,
|
|
|
|
int fileIndex,
|
|
|
|
int doDownload )
|
2007-07-10 03:12:46 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_file * file;
|
2007-07-16 19:57:34 +00:00
|
|
|
const int dnd = !doDownload;
|
|
|
|
int firstPiece, firstPieceDND;
|
|
|
|
int lastPiece, lastPieceDND;
|
2007-07-10 03:41:16 +00:00
|
|
|
int i;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentLock( tor );
|
2007-07-10 03:12:46 +00:00
|
|
|
|
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;
|
|
|
|
for( i=fileIndex-1; firstPieceDND && i>=0; --i ) {
|
|
|
|
if( tor->info.files[i].lastPiece != firstPiece )
|
|
|
|
break;
|
|
|
|
firstPieceDND = tor->info.files[i].dnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
{
|
|
|
|
tor->info.pieces[firstPiece].dnd = firstPieceDND;
|
|
|
|
tor->info.pieces[lastPiece].dnd = lastPieceDND;
|
2007-07-16 23:52:05 +00:00
|
|
|
for( i=firstPiece+1; i<lastPiece; ++i )
|
2007-07-16 19:57:34 +00:00
|
|
|
tor->info.pieces[i].dnd = dnd;
|
|
|
|
}
|
|
|
|
|
2007-07-28 15:43:34 +00:00
|
|
|
tr_cpInvalidateDND ( tor->completion );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
saveFastResumeSoon( tor );
|
2007-07-10 03:41:16 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentUnlock( tor );
|
2007-07-10 03:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrentSetFileDLs ( tr_torrent * tor,
|
|
|
|
int * files,
|
|
|
|
int fileCount,
|
|
|
|
int doDownload )
|
2007-07-10 03:41:16 +00:00
|
|
|
{
|
2007-07-16 19:57:34 +00:00
|
|
|
int i;
|
|
|
|
for( i=0; i<fileCount; ++i )
|
|
|
|
tr_torrentSetFileDL( tor, files[i], doDownload );
|
2007-07-10 03:12:46 +00:00
|
|
|
}
|
2007-07-19 11:54:37 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int _tr_block( const tr_torrent * tor, int index, int begin )
|
2007-07-19 11:54:37 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_info * inf = &tor->info;
|
2007-07-19 11:54:37 +00:00
|
|
|
return index * ( inf->pieceSize / tor->blockSize ) +
|
|
|
|
begin / tor->blockSize;
|
|
|
|
}
|
2007-09-23 23:38:39 +00:00
|
|
|
|
|
|
|
uint64_t
|
|
|
|
tr_pieceOffset( const tr_torrent * tor, int index, int begin, int length )
|
|
|
|
{
|
|
|
|
uint64_t ret;
|
|
|
|
ret = tor->info.pieceSize;
|
|
|
|
ret *= index;
|
|
|
|
ret += begin;
|
|
|
|
ret += length;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|