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-14 16:29:21 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2007-01-19 08:36:49 +00:00
|
|
|
#include "transmission.h"
|
2007-07-10 03:12:46 +00:00
|
|
|
#include "fastresume.h"
|
|
|
|
#include "trcompat.h" /* for strlcpy */
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "metainfo.h"
|
|
|
|
#include "net.h" /* tr_netNtop */
|
2007-01-19 08:40:06 +00:00
|
|
|
#include "shared.h"
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/***
|
|
|
|
**** LOCKS
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentReaderLock( const tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
tr_rwReaderLock ( (tr_rwlock_t*)&tor->lock );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentReaderUnlock( const tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
tr_rwReaderUnlock ( (tr_rwlock_t*)&tor->lock );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentWriterLock( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
tr_rwWriterLock ( &tor->lock );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentWriterUnlock( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
tr_rwWriterUnlock ( &tor->lock );
|
|
|
|
}
|
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-07-20 03:24:04 +00:00
|
|
|
tr_torrentSetSpeedMode( tr_torrent_t * tor,
|
|
|
|
int up_or_down,
|
|
|
|
tr_speedlimit_t mode )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-07-20 03:24:04 +00:00
|
|
|
tr_speedlimit_t * limit = up_or_down==TR_UP
|
|
|
|
? &tor->uploadLimitMode
|
|
|
|
: &tor->downloadLimitMode;
|
|
|
|
*limit = mode;
|
2007-02-02 17:33:32 +00:00
|
|
|
}
|
2007-07-20 03:24:04 +00:00
|
|
|
|
|
|
|
tr_speedlimit_t
|
|
|
|
tr_torrentGetSpeedMode( const tr_torrent_t * 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-07-20 03:24:04 +00:00
|
|
|
tr_torrentSetSpeedLimit( tr_torrent_t * tor,
|
|
|
|
int up_or_down,
|
|
|
|
int single_KiB_sec )
|
2007-07-18 05:27:45 +00:00
|
|
|
{
|
2007-07-20 03:24:04 +00:00
|
|
|
tr_ratecontrol_t * rc = up_or_down==TR_UP ? tor->upload : tor->download;
|
|
|
|
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-07-20 03:24:04 +00:00
|
|
|
tr_torrentGetSpeedLimit( const tr_torrent_t * tor,
|
|
|
|
int up_or_down )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-07-20 03:24:04 +00:00
|
|
|
tr_ratecontrol_t * rc = up_or_down==TR_UP ? tor->upload : tor->download;
|
|
|
|
return tr_rcGetLimit( rc );
|
2007-01-19 08:36:49 +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
|
|
|
|
getBytePiece( const tr_info_t * info, uint64_t byteOffset )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
initFilePieces ( tr_info_t * info, int fileIndex )
|
2007-04-02 20:38:23 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_file_t * file = &info->files[fileIndex];
|
|
|
|
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 );
|
|
|
|
tr_dbg( "file #%d is in pieces [%d...%d] (%s)", fileIndex, file->firstPiece, file->lastPiece, file->name );
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_priority_t
|
|
|
|
calculatePiecePriority ( const tr_torrent_t * tor,
|
|
|
|
int piece )
|
|
|
|
{
|
|
|
|
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-06-26 18:45:03 +00:00
|
|
|
const tr_file_t * file = &tor->info.files[i];
|
|
|
|
if ( file->firstPiece <= piece
|
|
|
|
&& file->lastPiece >= piece
|
|
|
|
&& file->priority > priority)
|
|
|
|
priority = file->priority;
|
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-06-26 18:45:03 +00:00
|
|
|
tr_torrentInitFilePieces( tr_torrent_t * 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-25 01:59:46 +00:00
|
|
|
{
|
|
|
|
tr_piece_t * piece = tor->info.pieces + i;
|
|
|
|
uint64_t lastByte;
|
|
|
|
int lastBlock;
|
|
|
|
if( i == (tor->info.pieceCount-1))
|
2007-07-25 02:28:25 +00:00
|
|
|
lastByte = tor->info.totalSize - 1;
|
2007-07-25 01:59:46 +00:00
|
|
|
else {
|
|
|
|
lastByte = i + 1;
|
|
|
|
lastByte *= tor->info.pieceSize;
|
|
|
|
--lastByte;
|
|
|
|
}
|
|
|
|
lastBlock = lastByte / tor->blockSize;
|
|
|
|
|
|
|
|
piece->priority = calculatePiecePriority( tor, i );
|
|
|
|
piece->firstBlock = i * (tor->info.pieceSize / tor->blockSize);
|
|
|
|
piece->blockCount = lastBlock + 1 - piece->firstBlock;
|
|
|
|
|
|
|
|
assert( 0 <= piece->firstBlock );
|
|
|
|
assert( piece->firstBlock <= lastBlock );
|
|
|
|
assert( lastBlock < tor->blockCount );
|
|
|
|
assert( piece->firstBlock + piece->blockCount <= tor->blockCount );
|
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
static void torrentThreadLoop( void * );
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
static void
|
2007-06-26 18:45:03 +00:00
|
|
|
torrentRealInit( tr_handle_t * h,
|
|
|
|
tr_torrent_t * tor,
|
|
|
|
const char * destination,
|
2007-06-27 05:14:38 +00:00
|
|
|
int flags )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-27 05:14:38 +00:00
|
|
|
int i;
|
2007-07-23 03:00:20 +00:00
|
|
|
uint64_t loaded;
|
2007-06-26 18:45:03 +00:00
|
|
|
char name[512];
|
2007-07-23 03:00:20 +00:00
|
|
|
tr_bitfield_t * uncheckedPieces;
|
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-01-29 08:24:09 +00:00
|
|
|
tr_sharedLock( h->shared );
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tor->destination = tr_strdup( destination );
|
|
|
|
|
2007-01-27 21:17:10 +00:00
|
|
|
tor->handle = h;
|
|
|
|
tor->key = h->key;
|
2007-03-23 08:28:01 +00:00
|
|
|
tor->azId = h->azId;
|
2007-06-18 03:40:41 +00:00
|
|
|
tor->hasChangedState = -1;
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-01-19 08:36:49 +00:00
|
|
|
/* Escaped info hash for HTTP queries */
|
|
|
|
for( i = 0; i < SHA_DIGEST_LENGTH; i++ )
|
|
|
|
{
|
2007-03-12 00:04:11 +00:00
|
|
|
snprintf( &tor->escapedHashString[3*i],
|
|
|
|
sizeof( tor->escapedHashString ) - 3 * i,
|
2007-06-27 05:14:38 +00:00
|
|
|
"%%%02x", tor->info.hash[i] );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-03-31 19:19:09 +00:00
|
|
|
tor->pexDisabled = 0;
|
|
|
|
|
2007-01-19 08:36:49 +00:00
|
|
|
/* Block size: usually 16 ko, or less if we have to */
|
2007-06-27 05:14:38 +00:00
|
|
|
tor->blockSize = MIN( tor->info.pieceSize, 1 << 14 );
|
|
|
|
tor->blockCount = ( tor->info.totalSize + tor->blockSize - 1 ) /
|
2007-01-19 08:36:49 +00:00
|
|
|
tor->blockSize;
|
|
|
|
tor->completion = tr_cpInit( tor );
|
|
|
|
|
2007-07-25 01:59:46 +00:00
|
|
|
tr_torrentInitFilePieces( tor );
|
|
|
|
|
2007-06-06 00:30:13 +00:00
|
|
|
tor->thread = THREAD_EMPTY;
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_rwInit( &tor->lock );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
|
|
|
tor->upload = tr_rcInit();
|
|
|
|
tor->download = tr_rcInit();
|
|
|
|
tor->swarmspeed = tr_rcInit();
|
|
|
|
|
|
|
|
/* We have a new torrent */
|
2007-01-27 21:17:10 +00:00
|
|
|
tor->publicPort = tr_sharedGetPublicPort( h->shared );
|
2007-01-29 08:24:09 +00:00
|
|
|
|
2007-01-19 08:36:49 +00:00
|
|
|
tr_sharedUnlock( h->shared );
|
|
|
|
|
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;
|
|
|
|
tor->runStatus = flags & TR_FLAG_PAUSED ? TR_RUN_STOPPED : TR_RUN_RUNNING;
|
2007-07-23 03:00:20 +00:00
|
|
|
|
|
|
|
uncheckedPieces = tr_bitfieldNew( tor->info.pieceCount );
|
|
|
|
loaded = tr_fastResumeLoad( tor, uncheckedPieces );
|
|
|
|
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-06-26 18:45:03 +00:00
|
|
|
tr_sharedLock( h->shared );
|
|
|
|
tor->next = h->torrentList;
|
|
|
|
h->torrentList = tor;
|
|
|
|
h->torrentCount++;
|
|
|
|
tr_sharedUnlock( h->shared );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
snprintf( name, sizeof( name ), "torrent %p (%s)", tor, tor->info.name );
|
|
|
|
tr_threadCreate( &tor->thread, torrentThreadLoop, tor, name );
|
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
|
|
|
|
pathIsInUse ( const tr_handle_t * h,
|
|
|
|
const char * destination,
|
|
|
|
const char * name )
|
|
|
|
{
|
|
|
|
const tr_torrent_t * tor;
|
|
|
|
|
|
|
|
for( tor=h->torrentList; tor; tor=tor->next )
|
|
|
|
if( !strcmp( destination, tor->destination )
|
|
|
|
&& !strcmp( name, tor->info.name ) )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hashExists( const tr_handle_t * h,
|
|
|
|
const uint8_t * hash )
|
|
|
|
{
|
|
|
|
const tr_torrent_t * tor;
|
|
|
|
|
|
|
|
for( tor=h->torrentList; tor; tor=tor->next )
|
|
|
|
if( !memcmp( hash, tor->info.hash, SHA_DIGEST_LENGTH ) )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
infoCanAdd( const tr_handle_t * h,
|
|
|
|
const char * destination,
|
|
|
|
const tr_info_t * info )
|
|
|
|
{
|
|
|
|
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-06-27 14:54:31 +00:00
|
|
|
tr_torrentParse( const tr_handle_t * h,
|
|
|
|
const char * path,
|
|
|
|
const char * destination,
|
2007-06-27 23:22:09 +00:00
|
|
|
tr_info_t * setme_info )
|
2007-06-27 05:14:38 +00:00
|
|
|
{
|
2007-06-27 14:54:31 +00:00
|
|
|
int ret;
|
|
|
|
tr_info_t 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-06-28 01:12:16 +00:00
|
|
|
memset( setme_info, 0, sizeof( tr_info_t ) );
|
2007-06-27 14:54:31 +00:00
|
|
|
ret = tr_metainfoParseFile( setme_info, h->tag, path, FALSE );
|
|
|
|
|
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-06-28 01:12:16 +00:00
|
|
|
if( setme_info == &tmp )
|
|
|
|
tr_metainfoFree( &tmp );
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return ret;
|
2007-06-27 05:14:38 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrent_t *
|
|
|
|
tr_torrentInit( tr_handle_t * 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;
|
|
|
|
tr_torrent_t * tor = NULL;
|
2007-05-24 15:57:04 +00:00
|
|
|
|
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;
|
|
|
|
else if(!(( tor = tr_new0( tr_torrent_t, 1 ))))
|
|
|
|
*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-06-27 14:54:31 +00:00
|
|
|
tr_torrentParseHash( const tr_handle_t * h,
|
|
|
|
const char * hashStr,
|
|
|
|
const char * destination,
|
2007-06-27 23:22:09 +00:00
|
|
|
tr_info_t * setme_info )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-27 14:54:31 +00:00
|
|
|
int ret;
|
|
|
|
tr_info_t tmp;
|
|
|
|
|
|
|
|
if( setme_info == NULL )
|
|
|
|
setme_info = &tmp;
|
|
|
|
|
2007-06-28 01:12:16 +00:00
|
|
|
memset( setme_info, 0, sizeof( tr_info_t ) );
|
2007-06-27 14:54:31 +00:00
|
|
|
ret = tr_metainfoParseHash( setme_info, h->tag, hashStr );
|
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-06-28 01:12:16 +00:00
|
|
|
if( setme_info == &tmp )
|
|
|
|
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-06-27 14:54:31 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrent_t *
|
2007-06-27 14:54:31 +00:00
|
|
|
tr_torrentInitSaved( tr_handle_t * 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;
|
|
|
|
tr_torrent_t * tor = NULL;
|
|
|
|
|
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;
|
|
|
|
else if(!(( tor = tr_new0( tr_torrent_t, 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-06-27 14:54:31 +00:00
|
|
|
tr_torrentParseData( const tr_handle_t * h,
|
|
|
|
const uint8_t * data,
|
|
|
|
size_t size,
|
|
|
|
const char * destination,
|
2007-06-27 23:22:09 +00:00
|
|
|
tr_info_t * setme_info )
|
2007-06-27 05:14:38 +00:00
|
|
|
{
|
2007-06-27 14:54:31 +00:00
|
|
|
int ret;
|
|
|
|
tr_info_t tmp;
|
|
|
|
|
|
|
|
if( setme_info == NULL )
|
|
|
|
setme_info = &tmp;
|
|
|
|
|
2007-06-28 01:12:16 +00:00
|
|
|
memset( setme_info, 0, sizeof( tr_info_t ) );
|
2007-06-27 14:54:31 +00:00
|
|
|
ret = tr_metainfoParseData( setme_info, h->tag, data, size, FALSE );
|
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-06-28 01:12:16 +00:00
|
|
|
if( setme_info == &tmp )
|
|
|
|
tr_metainfoFree( &tmp );
|
|
|
|
|
2007-06-27 14:54:31 +00:00
|
|
|
return ret;
|
2007-06-27 05:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent_t *
|
2007-06-27 14:54:31 +00:00
|
|
|
tr_torrentInitData( tr_handle_t * h,
|
|
|
|
const uint8_t * data,
|
|
|
|
size_t size,
|
|
|
|
const char * destination,
|
|
|
|
int flags,
|
|
|
|
int * error )
|
2007-06-27 05:14:38 +00:00
|
|
|
{
|
|
|
|
int val;
|
|
|
|
tr_torrent_t * tor = NULL;
|
|
|
|
|
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;
|
|
|
|
else if(!(( tor = tr_new0( tr_torrent_t, 1 ))))
|
|
|
|
*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-06-30 13:36:58 +00:00
|
|
|
const tr_info_t *
|
|
|
|
tr_torrentInfo( const tr_torrent_t * 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-06-26 18:45:03 +00:00
|
|
|
int tr_torrentScrape( tr_torrent_t * 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-07-23 03:00:20 +00:00
|
|
|
static void
|
|
|
|
fastResumeSave( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
tr_fastResumeSave( tor );
|
|
|
|
tor->fastResumeDirty = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentSetFolder( tr_torrent_t * 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-07-23 03:00:20 +00:00
|
|
|
fastResumeSave( 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*
|
|
|
|
tr_torrentGetFolder( const tr_torrent_t * tor )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
|
|
|
return tor->destination;
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-01-19 08:36:49 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* torrentReallyStop
|
|
|
|
***********************************************************************
|
|
|
|
* Joins the download thread and frees/closes everything related to it.
|
|
|
|
**********************************************************************/
|
|
|
|
|
2007-03-31 19:19:09 +00:00
|
|
|
void tr_torrentDisablePex( tr_torrent_t * tor, int disable )
|
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterLock( tor );
|
2007-03-31 19:19:09 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
if( ! ( TR_FLAG_PRIVATE & tor->info.flags ) )
|
2007-03-31 19:19:09 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
if( tor->pexDisabled != disable )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
tor->pexDisabled = disable;
|
|
|
|
for( i=0; i<tor->peerCount; ++i )
|
|
|
|
tr_peerSetPrivate( tor->peers[i], disable );
|
|
|
|
}
|
2007-03-31 19:19:09 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterUnlock( tor );
|
2007-03-31 19:19:09 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
static int tr_didStateChangeTo ( tr_torrent_t * tor, int status )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
tr_torrentWriterLock( tor );
|
|
|
|
if (( ret = tor->hasChangedState == status ))
|
2007-06-18 03:40:41 +00:00
|
|
|
tor->hasChangedState = -1;
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterUnlock( tor );
|
|
|
|
|
|
|
|
return ret;
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
int tr_getIncomplete( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
return tr_didStateChangeTo( tor, TR_CP_INCOMPLETE );
|
|
|
|
}
|
|
|
|
int tr_getDone( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
return tr_didStateChangeTo( tor, TR_CP_DONE );
|
|
|
|
}
|
|
|
|
int tr_getComplete( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
return tr_didStateChangeTo( tor, TR_CP_COMPLETE );
|
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void tr_manualUpdate( tr_torrent_t * tor UNUSED )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
#if 0
|
2007-03-29 00:19:09 +00:00
|
|
|
int peerCount, new;
|
2007-01-19 11:19:20 +00:00
|
|
|
uint8_t * peerCompact;
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
if( tor->status != TR_RUN_RUNNING )
|
2007-01-19 08:36:49 +00:00
|
|
|
return;
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterLock( tor );
|
2007-01-19 11:19:20 +00:00
|
|
|
tr_trackerAnnouncePulse( tor->tracker, &peerCount, &peerCompact, 1 );
|
2007-03-29 00:19:09 +00:00
|
|
|
new = 0;
|
2007-01-19 11:19:20 +00:00
|
|
|
if( peerCount > 0 )
|
|
|
|
{
|
2007-03-29 00:19:09 +00:00
|
|
|
new = tr_torrentAddCompact( tor, TR_PEER_FROM_TRACKER,
|
2007-06-26 18:45:03 +00:00
|
|
|
peerCompact, peerCount );
|
2007-01-19 11:19:20 +00:00
|
|
|
free( peerCompact );
|
|
|
|
}
|
2007-03-29 00:19:09 +00:00
|
|
|
tr_dbg( "got %i peers from manual announce, used %i", peerCount, new );
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterUnlock( tor );
|
|
|
|
#endif
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-07-15 19:12:54 +00:00
|
|
|
const tr_stat_t *
|
|
|
|
tr_torrentStat( tr_torrent_t * tor )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
|
|
|
tr_stat_t * s;
|
|
|
|
tr_tracker_t * tc;
|
|
|
|
int i;
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentReaderLock( tor );
|
|
|
|
|
2007-01-19 08:36:49 +00:00
|
|
|
tor->statCur = ( tor->statCur + 1 ) % 2;
|
|
|
|
s = &tor->stats[tor->statCur];
|
|
|
|
|
|
|
|
s->error = tor->error;
|
|
|
|
memcpy( s->errorString, tor->errorString,
|
|
|
|
sizeof( s->errorString ) );
|
|
|
|
|
|
|
|
tc = tor->tracker;
|
|
|
|
s->cannotConnect = tr_trackerCannotConnect( tc );
|
2007-06-27 05:14:38 +00:00
|
|
|
s->tracker = tc
|
|
|
|
? tr_trackerGet( tc )
|
|
|
|
: &tor->info.trackerList[0].list[0];
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/* peers... */
|
2007-03-23 08:28:01 +00:00
|
|
|
memset( s->peersFrom, 0, sizeof( s->peersFrom ) );
|
2007-07-16 00:50:43 +00:00
|
|
|
s->peersTotal = tor->peerCount;
|
|
|
|
s->peersConnected = 0;
|
|
|
|
s->peersSendingToUs = 0;
|
|
|
|
s->peersGettingFromUs = 0;
|
2007-07-15 17:19:07 +00:00
|
|
|
|
2007-07-15 05:18:18 +00:00
|
|
|
for( i=0; i<tor->peerCount; ++i )
|
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
const tr_peer_t * peer = tor->peers[i];
|
2007-07-15 17:19:07 +00:00
|
|
|
|
2007-07-15 05:18:18 +00:00
|
|
|
if( tr_peerIsConnected( peer ) )
|
|
|
|
{
|
2007-07-15 17:19:07 +00:00
|
|
|
++s->peersConnected;
|
2007-06-26 18:45:03 +00:00
|
|
|
++s->peersFrom[tr_peerIsFrom(peer)];
|
2007-07-15 05:18:18 +00:00
|
|
|
|
2007-07-15 15:43:31 +00:00
|
|
|
if( tr_peerDownloadRate( peer ) > 0.01 )
|
2007-07-16 00:50:43 +00:00
|
|
|
++s->peersSendingToUs;
|
2007-07-15 05:18:18 +00:00
|
|
|
|
2007-07-15 15:43:31 +00:00
|
|
|
if( tr_peerUploadRate( peer ) > 0.01 )
|
2007-07-16 00:50:43 +00:00
|
|
|
++s->peersGettingFromUs;
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-15 17:19:07 +00:00
|
|
|
s->percentDone = tr_cpPercentDone ( tor->completion );
|
|
|
|
s->percentComplete = tr_cpPercentComplete ( tor->completion );
|
|
|
|
s->left = tr_cpLeftUntilDone ( tor->completion );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
|
|
|
|
2007-07-23 03:00:20 +00:00
|
|
|
if( tor->uncheckedPieces )
|
2007-07-24 01:33:59 +00:00
|
|
|
s->status = tor->runStatus==TR_RUN_CHECKING
|
|
|
|
? TR_STATUS_CHECK
|
|
|
|
: TR_STATUS_CHECK_WAIT;
|
2007-06-26 18:45:03 +00:00
|
|
|
else switch( tor->runStatus ) {
|
2007-07-12 20:48:13 +00:00
|
|
|
case TR_RUN_STOPPING: /* fallthrough */
|
|
|
|
case TR_RUN_STOPPING_NET_WAIT: s->status = TR_STATUS_STOPPING; break;
|
2007-06-26 18:45:03 +00:00
|
|
|
case TR_RUN_STOPPED: s->status = TR_STATUS_STOPPED; break;
|
|
|
|
case TR_RUN_CHECKING: s->status = TR_STATUS_CHECK; break;
|
|
|
|
case TR_RUN_RUNNING: switch( tor->cpStatus ) {
|
|
|
|
case TR_CP_INCOMPLETE: s->status = TR_STATUS_DOWNLOAD; break;
|
|
|
|
case TR_CP_DONE: s->status = TR_STATUS_DONE; break;
|
|
|
|
case TR_CP_COMPLETE: s->status = TR_STATUS_SEED; break;
|
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
/* tr_rcRate() doesn't make the difference between 'piece'
|
|
|
|
messages and other messages, which causes a non-zero
|
|
|
|
download rate even tough we are not downloading. So we
|
|
|
|
force it to zero not to confuse the user. */
|
|
|
|
s->rateDownload = tor->runStatus==TR_RUN_RUNNING
|
|
|
|
? tr_rcRate( tor->download )
|
|
|
|
: 0.0;
|
2007-01-19 08:36:49 +00:00
|
|
|
s->rateUpload = tr_rcRate( tor->upload );
|
|
|
|
|
|
|
|
s->seeders = tr_trackerSeeders( tc );
|
|
|
|
s->leechers = tr_trackerLeechers( tc );
|
|
|
|
s->completedFromTracker = tr_trackerDownloaded( tc );
|
|
|
|
|
|
|
|
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
|
|
|
|
: (s->left / s->rateDownload / 1024.0);
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
s->uploaded = tor->uploadedCur + tor->uploadedPrev;
|
|
|
|
s->downloaded = tor->downloadedCur + tor->downloadedPrev;
|
|
|
|
s->downloadedValid = tr_cpDownloadedValid( tor->completion );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
|
|
|
s->ratio = s->downloaded || s->downloadedValid
|
|
|
|
? (float)s->uploaded / (float)MAX(s->downloaded, s->downloadedValid)
|
|
|
|
: TR_RATIO_NA;
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentReaderUnlock( 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
|
|
|
|
fileBytesCompleted ( const tr_torrent_t * tor, int fileIndex )
|
|
|
|
{
|
|
|
|
const tr_file_t * file = &tor->info.files[fileIndex];
|
|
|
|
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 );
|
|
|
|
assert( (int)firstBlock < tor->blockCount );
|
|
|
|
assert( (int)lastBlock < tor->blockCount );
|
|
|
|
assert( firstBlock <= lastBlock );
|
2007-07-25 01:59:46 +00:00
|
|
|
assert( TOR_BLOCK_PIECE( tor, firstBlock ) == file->firstPiece );
|
|
|
|
assert( TOR_BLOCK_PIECE( 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-07-15 20:05:32 +00:00
|
|
|
tr_file_stat_t *
|
|
|
|
tr_torrentFiles( const tr_torrent_t * tor, int * fileCount )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const int n = tor->info.fileCount;
|
|
|
|
tr_file_stat_t * files = tr_new0( tr_file_stat_t, n );
|
|
|
|
tr_file_stat_t * walk = files;
|
|
|
|
|
|
|
|
for( i=0; i<n; ++i, ++walk )
|
|
|
|
{
|
2007-07-16 11:19:44 +00:00
|
|
|
const tr_file_t * 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
|
|
|
|
tr_torrentFilesFree( tr_file_stat_t * files, int fileCount UNUSED )
|
|
|
|
{
|
|
|
|
tr_free( files );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_peer_stat_t *
|
|
|
|
tr_torrentPeers( const tr_torrent_t * tor, int * peerCount )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
|
|
|
tr_peer_stat_t * peers;
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentReaderLock( tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
|
|
|
*peerCount = tor->peerCount;
|
2007-06-27 05:14:38 +00:00
|
|
|
|
|
|
|
peers = tr_new0( tr_peer_stat_t, tor->peerCount );
|
2007-01-19 08:36:49 +00:00
|
|
|
if (peers != NULL)
|
|
|
|
{
|
|
|
|
tr_peer_t * peer;
|
|
|
|
struct in_addr * addr;
|
|
|
|
int i;
|
2007-07-13 22:22:46 +00:00
|
|
|
for( i=0; i<tor->peerCount; ++i )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
|
|
|
peer = tor->peers[i];
|
|
|
|
|
|
|
|
addr = tr_peerAddress( peer );
|
|
|
|
if( NULL != addr )
|
|
|
|
{
|
|
|
|
tr_netNtop( addr, peers[i].addr,
|
|
|
|
sizeof( peers[i].addr ) );
|
|
|
|
}
|
|
|
|
|
2007-07-13 22:22:46 +00:00
|
|
|
peers[i].client = tr_peerClient( peer );
|
|
|
|
peers[i].isConnected = tr_peerIsConnected( peer );
|
|
|
|
peers[i].from = tr_peerIsFrom( peer );
|
|
|
|
peers[i].progress = tr_peerProgress( peer );
|
|
|
|
peers[i].port = tr_peerPort( peer );
|
2007-07-15 05:18:18 +00:00
|
|
|
|
2007-07-13 22:22:46 +00:00
|
|
|
peers[i].uploadToRate = tr_peerUploadRate( peer );
|
|
|
|
peers[i].downloadFromRate = tr_peerDownloadRate( peer );
|
2007-07-15 15:45:08 +00:00
|
|
|
|
|
|
|
peers[i].isDownloading = peers[i].uploadToRate > 0.01;
|
2007-07-15 15:43:31 +00:00
|
|
|
peers[i].isUploading = peers[i].downloadFromRate > 0.01;
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentReaderUnlock( tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
|
|
|
return peers;
|
|
|
|
}
|
|
|
|
|
2007-04-03 18:22:58 +00:00
|
|
|
void tr_torrentPeersFree( tr_peer_stat_t * 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
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void tr_torrentAvailability( const tr_torrent_t * tor, int8_t * tab, int size )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
|
|
|
int i, j, piece;
|
2007-01-31 16:23:07 +00:00
|
|
|
float interval;
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentReaderLock( tor );
|
|
|
|
|
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-01-31 16:23:07 +00:00
|
|
|
piece = i * interval;
|
2007-01-19 08:36:49 +00:00
|
|
|
|
|
|
|
if( tr_cpPieceIsComplete( tor->completion, piece ) )
|
|
|
|
{
|
|
|
|
tab[i] = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tab[i] = 0;
|
|
|
|
for( j = 0; j < tor->peerCount; j++ )
|
|
|
|
{
|
2007-06-21 14:47:26 +00:00
|
|
|
if( tr_peerHasPiece( tor->peers[j], piece ) )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
|
|
|
(tab[i])++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-26 18:45:03 +00:00
|
|
|
|
|
|
|
tr_torrentReaderUnlock( tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void tr_torrentAmountFinished( const tr_torrent_t * 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-06-26 18:45:03 +00:00
|
|
|
tr_torrentReaderLock( 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-06-26 18:45:03 +00:00
|
|
|
tr_torrentReaderUnlock( tor );
|
2007-04-28 01:34:39 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void
|
|
|
|
tr_torrentResetTransferStats( tr_torrent_t * tor )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterLock( 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-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterUnlock( tor );
|
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void
|
|
|
|
tr_torrentSetHasPiece( tr_torrent_t * tor, int pieceIndex, int has )
|
|
|
|
{
|
|
|
|
tr_torrentWriterLock( 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-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterUnlock( tor );
|
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void tr_torrentRemoveSaved( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
tr_metainfoRemoveSaved( tor->info.hashString, tor->handle->tag );
|
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void tr_torrentRecheck( tr_torrent_t * tor )
|
|
|
|
{
|
2007-07-23 03:00:20 +00:00
|
|
|
if( !tor->uncheckedPieces )
|
|
|
|
tor->uncheckedPieces = tr_bitfieldNew( tor->info.pieceCount );
|
|
|
|
tr_bitfieldAddRange( tor->uncheckedPieces, 0, tor->info.pieceCount );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-03-29 00:19:09 +00:00
|
|
|
int tr_torrentAttachPeer( tr_torrent_t * tor, tr_peer_t * peer )
|
2007-01-21 06:42:05 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
tr_peer_t * otherPeer;
|
|
|
|
|
2007-06-28 17:43:53 +00:00
|
|
|
assert( tor != NULL );
|
|
|
|
assert( peer != NULL );
|
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
if( tor->peerCount >= TR_MAX_PEER_COUNT )
|
|
|
|
{
|
|
|
|
tr_peerDestroy( peer );
|
2007-03-29 00:19:09 +00:00
|
|
|
return 0;
|
2007-01-21 06:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't accept two connections from the same IP */
|
|
|
|
for( i = 0; i < tor->peerCount; i++ )
|
|
|
|
{
|
|
|
|
otherPeer = tor->peers[i];
|
|
|
|
if( !memcmp( tr_peerAddress( peer ), tr_peerAddress( otherPeer ), 4 ) )
|
|
|
|
{
|
|
|
|
tr_peerDestroy( peer );
|
2007-03-29 00:19:09 +00:00
|
|
|
return 0;
|
2007-01-21 06:42:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-31 19:19:09 +00:00
|
|
|
tr_peerSetPrivate( peer, tor->info.flags & TR_FLAG_PRIVATE ||
|
|
|
|
tor->pexDisabled );
|
2007-01-21 06:42:05 +00:00
|
|
|
tr_peerSetTorrent( peer, tor );
|
|
|
|
tor->peers[tor->peerCount++] = peer;
|
2007-03-29 00:19:09 +00:00
|
|
|
|
|
|
|
return 1;
|
2007-01-21 06:42:05 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 00:19:09 +00:00
|
|
|
int tr_torrentAddCompact( tr_torrent_t * tor, int from,
|
2007-03-23 08:28:01 +00:00
|
|
|
uint8_t * buf, int count )
|
2007-01-21 06:42:05 +00:00
|
|
|
{
|
|
|
|
struct in_addr addr;
|
|
|
|
in_port_t port;
|
2007-03-29 00:19:09 +00:00
|
|
|
int i, added;
|
2007-01-21 06:42:05 +00:00
|
|
|
tr_peer_t * peer;
|
|
|
|
|
2007-03-29 00:19:09 +00:00
|
|
|
added = 0;
|
2007-01-21 06:42:05 +00:00
|
|
|
for( i = 0; i < count; i++ )
|
|
|
|
{
|
|
|
|
memcpy( &addr, buf, 4 ); buf += 4;
|
|
|
|
memcpy( &port, buf, 2 ); buf += 2;
|
|
|
|
|
2007-07-14 16:29:21 +00:00
|
|
|
peer = tr_peerInit( &addr, port, -1, from );
|
2007-03-29 00:19:09 +00:00
|
|
|
added += tr_torrentAttachPeer( tor, peer );
|
2007-01-21 06:42:05 +00:00
|
|
|
}
|
2007-03-29 00:19:09 +00:00
|
|
|
|
|
|
|
return added;
|
2007-01-21 06:42:05 +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 setRunState( tr_torrent_t * tor, run_status_t run )
|
2007-05-28 15:23:28 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterLock( tor );
|
|
|
|
tor->runStatus = run;
|
|
|
|
tr_torrentWriterUnlock( tor );
|
|
|
|
}
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void tr_torrentStart( tr_torrent_t * tor )
|
2007-05-28 15:23:28 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
setRunState( tor, TR_RUN_RUNNING );
|
2007-05-28 15:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void tr_torrentStop( tr_torrent_t * tor )
|
2007-05-28 15:23:28 +00:00
|
|
|
{
|
2007-07-21 18:03:24 +00:00
|
|
|
if((tor->runStatus != TR_RUN_STOPPING) && (tor->runStatus != TR_RUN_STOPPED) )
|
|
|
|
setRunState( tor, TR_RUN_STOPPING );
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
void tr_torrentClose( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
tr_torrentStop( tor );
|
|
|
|
tor->dieFlag = TRUE;
|
|
|
|
}
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
static void
|
|
|
|
tr_torrentFree( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
tr_torrent_t * t;
|
|
|
|
tr_handle_t * h = tor->handle;
|
|
|
|
tr_info_t * inf = &tor->info;
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_sharedLock( h->shared );
|
2007-05-28 15:23:28 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_rwClose( &tor->lock );
|
|
|
|
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-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
|
|
|
tr_metainfoFree( inf );
|
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-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 );
|
|
|
|
|
|
|
|
tr_free( tor );
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_sharedUnlock( h->shared );
|
2007-05-28 15:23:28 +00:00
|
|
|
}
|
|
|
|
|
2007-07-19 03:48:27 +00:00
|
|
|
static void
|
|
|
|
recheckCpState( tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
cp_status_t cpStatus;
|
|
|
|
|
|
|
|
tr_torrentWriterLock( tor );
|
|
|
|
|
|
|
|
cpStatus = tr_cpGetStatus( tor->completion );
|
|
|
|
if( cpStatus != tor->cpStatus ) {
|
|
|
|
tor->cpStatus = cpStatus;
|
|
|
|
tor->hasChangedState = tor->cpStatus; /* tell the client... */
|
|
|
|
if( (cpStatus == TR_CP_COMPLETE) /* ...and if we're complete */
|
|
|
|
&& tor->tracker!=NULL /* and we have a tracker */
|
|
|
|
&& tor->downloadedCur ) { /* and it just happened */
|
|
|
|
tr_trackerCompleted( tor->tracker ); /* tell the tracker */
|
|
|
|
}
|
|
|
|
tr_ioSync( tor->io );
|
2007-07-23 03:00:20 +00:00
|
|
|
fastResumeSave( tor );
|
2007-07-19 03:48:27 +00:00
|
|
|
}
|
|
|
|
tr_torrentWriterUnlock( tor );
|
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
static void
|
|
|
|
torrentThreadLoop ( void * _tor )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
static tr_lock_t checkFilesLock;
|
|
|
|
static int checkFilesLockInited = FALSE;
|
2007-01-19 08:36:49 +00:00
|
|
|
tr_torrent_t * tor = _tor;
|
2007-06-26 18:45:03 +00:00
|
|
|
|
|
|
|
/* create the check-files mutex */
|
|
|
|
if( !checkFilesLockInited ) {
|
2007-06-27 05:14:38 +00:00
|
|
|
checkFilesLockInited = TRUE;
|
|
|
|
tr_lockInit( &checkFilesLock );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/* loop until the torrent is being deleted */
|
|
|
|
while( ! ( tor->dieFlag && (tor->runStatus == TR_RUN_STOPPED) ) )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
/* sleep a little while */
|
2007-07-10 17:07:18 +00:00
|
|
|
tr_wait( tor->runStatus == TR_RUN_STOPPED ? 1600 : 600 );
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-07-19 03:48:27 +00:00
|
|
|
if( tor->fastResumeDirty )
|
|
|
|
{
|
2007-07-16 19:57:34 +00:00
|
|
|
fastResumeSave( tor );
|
2007-07-19 03:48:27 +00:00
|
|
|
recheckCpState( tor );
|
2007-07-16 19:57:34 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/* if we're stopping... */
|
|
|
|
if( tor->runStatus == TR_RUN_STOPPING )
|
2007-01-21 06:42:05 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
int i;
|
|
|
|
int peerCount;
|
|
|
|
uint8_t * peerCompact;
|
|
|
|
tr_torrentWriterLock( tor );
|
|
|
|
|
|
|
|
/* close the IO */
|
|
|
|
tr_ioClose( tor->io );
|
|
|
|
tor->io = NULL;
|
2007-07-23 03:00:20 +00:00
|
|
|
fastResumeSave( tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
|
|
|
/* close the peers */
|
|
|
|
for( i=0; i<tor->peerCount; ++i )
|
|
|
|
tr_peerDestroy( tor->peers[i] );
|
|
|
|
tor->peerCount = 0;
|
|
|
|
|
|
|
|
/* resest the transfer rates */
|
|
|
|
tr_rcReset( tor->download );
|
|
|
|
tr_rcReset( tor->upload );
|
|
|
|
tr_rcReset( tor->swarmspeed );
|
|
|
|
|
2007-07-12 20:48:13 +00:00
|
|
|
/* tell the tracker we're stopping */
|
|
|
|
tr_trackerStopped( tor->tracker );
|
|
|
|
tr_trackerPulse( tor->tracker, &peerCount, &peerCompact );
|
|
|
|
tor->runStatus = TR_RUN_STOPPING_NET_WAIT;
|
2007-06-26 18:45:03 +00:00
|
|
|
tor->stopDate = tr_date();
|
|
|
|
tr_torrentWriterUnlock( tor );
|
2007-01-21 06:42:05 +00:00
|
|
|
}
|
|
|
|
|
2007-07-12 20:48:13 +00:00
|
|
|
if( tor->runStatus == TR_RUN_STOPPING_NET_WAIT )
|
|
|
|
{
|
2007-07-13 01:34:14 +00:00
|
|
|
uint64_t date;
|
|
|
|
int peerCount;
|
|
|
|
uint8_t * peerCompact;
|
|
|
|
tr_trackerPulse( tor->tracker, &peerCount, &peerCompact );
|
|
|
|
|
2007-07-12 20:48:13 +00:00
|
|
|
/* have we finished telling the tracker that we're stopping? */
|
2007-07-13 01:34:14 +00:00
|
|
|
date = tr_trackerLastResponseDate( tor->tracker );
|
2007-07-12 20:48:13 +00:00
|
|
|
if( date > tor->stopDate )
|
|
|
|
{
|
|
|
|
tr_torrentWriterLock( tor );
|
|
|
|
tr_trackerClose( tor->tracker );
|
|
|
|
tor->tracker = NULL;
|
|
|
|
tor->runStatus = TR_RUN_STOPPED;
|
|
|
|
tr_torrentWriterUnlock( tor );
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/* do we need to check files? */
|
2007-07-23 03:00:20 +00:00
|
|
|
if( tor->uncheckedPieces )
|
2007-01-19 15:24:20 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
if( !tr_lockTryLock( &checkFilesLock ) )
|
2007-01-19 15:24:20 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
run_status_t realStatus;
|
|
|
|
|
|
|
|
tr_torrentWriterLock( tor );
|
|
|
|
realStatus = tor->runStatus;
|
|
|
|
tor->runStatus = TR_RUN_CHECKING;
|
|
|
|
tr_torrentWriterUnlock( tor );
|
|
|
|
|
2007-07-23 03:00:20 +00:00
|
|
|
tr_ioCheckFiles( tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
setRunState( tor, realStatus );
|
|
|
|
|
2007-07-15 04:19:39 +00:00
|
|
|
tr_torrentWriterLock( tor );
|
|
|
|
tor->cpStatus = tr_cpGetStatus( tor->completion );
|
|
|
|
tr_torrentWriterUnlock( tor );
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_lockUnlock( &checkFilesLock );
|
2007-01-19 15:24:20 +00:00
|
|
|
}
|
2007-01-21 06:42:05 +00:00
|
|
|
continue;
|
2007-01-19 15:24:20 +00:00
|
|
|
}
|
2007-01-21 06:42:05 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/* if we're paused or stopped, not much to do... */
|
|
|
|
if( tor->runStatus == TR_RUN_STOPPED )
|
|
|
|
continue;
|
2007-01-21 06:42:05 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/* ping our peers if we're running... */
|
|
|
|
if( tor->runStatus == TR_RUN_RUNNING )
|
2007-01-19 08:36:49 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
int i;
|
|
|
|
int peerCount;
|
|
|
|
uint8_t * peerCompact;
|
|
|
|
|
|
|
|
/* starting to run... */
|
|
|
|
if( tor->io == NULL ) {
|
2007-06-27 05:14:38 +00:00
|
|
|
*tor->errorString = '\0';
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentResetTransferStats( tor );
|
2007-07-23 03:00:20 +00:00
|
|
|
tor->io = tr_ioNew( tor );
|
2007-07-16 11:19:44 +00:00
|
|
|
tr_peerIdNew ( tor->peer_id, sizeof(tor->peer_id) );
|
2007-06-26 18:45:03 +00:00
|
|
|
tor->tracker = tr_trackerInit( tor );
|
|
|
|
tor->startDate = tr_date();
|
|
|
|
}
|
2007-01-21 06:42:05 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/* refresh our completion state */
|
2007-07-19 03:48:27 +00:00
|
|
|
recheckCpState( tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
|
|
|
|
/* ping the tracker... */
|
|
|
|
tr_trackerPulse( tor->tracker, &peerCount, &peerCompact );
|
|
|
|
if( peerCount > 0 ) {
|
|
|
|
int used = tr_torrentAddCompact( tor, TR_PEER_FROM_TRACKER,
|
|
|
|
peerCompact, peerCount );
|
|
|
|
tr_dbg( "got %i peers from announce, used %i", peerCount, used );
|
|
|
|
free( peerCompact );
|
2007-01-21 06:42:05 +00:00
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/* Shuffle peers */
|
|
|
|
if ( tor->peerCount > 1 ) {
|
|
|
|
tr_peer_t * tmp = tor->peers[0];
|
|
|
|
memmove( tor->peers, tor->peers+1,
|
|
|
|
(tor->peerCount-1) * sizeof(void*) );
|
|
|
|
tor->peers[tor->peerCount - 1] = tmp;
|
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
/* receive/send messages */
|
2007-06-30 12:46:45 +00:00
|
|
|
tr_torrentWriterLock( tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
for( i=0; i<tor->peerCount; ) {
|
|
|
|
tr_peer_t * peer = tor->peers[i];
|
|
|
|
int ret = tr_peerPulse( peer );
|
|
|
|
if( ret & TR_ERROR_IO_MASK ) {
|
|
|
|
tr_err( "Fatal error, stopping download (%d)", ret );
|
|
|
|
tor->runStatus = TR_RUN_STOPPING;
|
|
|
|
tor->error = ret;
|
|
|
|
strlcpy( tor->errorString,
|
|
|
|
tr_errorString(ret),
|
|
|
|
sizeof(tor->errorString) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if( ret ) {
|
|
|
|
tr_peerDestroy( peer );
|
|
|
|
tor->peerCount--;
|
|
|
|
memmove( &tor->peers[i], &tor->peers[i+1],
|
|
|
|
(tor->peerCount-i)*sizeof(void*) );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2007-06-30 12:46:45 +00:00
|
|
|
tr_torrentWriterUnlock( tor );
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
2007-01-19 15:24:20 +00:00
|
|
|
}
|
2007-01-19 08:36:49 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_ioClose( tor->io );
|
|
|
|
tr_torrentFree( tor );
|
2007-01-19 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +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
|
|
|
|
tr_torrentSetFilePriority( tr_torrent_t * tor,
|
|
|
|
int fileIndex,
|
|
|
|
tr_priority_t priority )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
tr_file_t * file;
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterLock( tor );
|
|
|
|
|
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-07-16 19:57:34 +00:00
|
|
|
tor->fastResumeDirty = TRUE;
|
2007-07-10 03:12:46 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrentWriterUnlock( tor );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-07-10 03:12:46 +00:00
|
|
|
void
|
2007-07-13 00:27:47 +00:00
|
|
|
tr_torrentSetFilePriorities( tr_torrent_t * 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
|
|
|
|
tr_torrentGetFilePriority( const tr_torrent_t * tor, int file )
|
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_priority_t ret;
|
|
|
|
|
|
|
|
tr_torrentReaderLock( 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;
|
|
|
|
tr_torrentReaderUnlock( 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*
|
|
|
|
tr_torrentGetFilePriorities( const tr_torrent_t * tor )
|
|
|
|
{
|
|
|
|
int i;
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_priority_t * p;
|
|
|
|
|
|
|
|
tr_torrentReaderLock( 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-06-26 18:45:03 +00:00
|
|
|
tr_torrentReaderUnlock( tor );
|
|
|
|
|
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
|
|
|
|
tr_torrentGetFileDL( const tr_torrent_t * tor,
|
|
|
|
int file )
|
|
|
|
{
|
2007-07-16 19:57:34 +00:00
|
|
|
int doDownload;
|
2007-07-10 03:12:46 +00:00
|
|
|
tr_torrentReaderLock( tor );
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
tr_torrentReaderUnlock( tor );
|
2007-07-16 19:57:34 +00:00
|
|
|
return doDownload != 0;
|
2007-07-10 03:12:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrentSetFileDL( tr_torrent_t * tor,
|
2007-07-10 03:41:16 +00:00
|
|
|
int fileIndex,
|
2007-07-16 19:57:34 +00:00
|
|
|
int doDownload )
|
2007-07-10 03:12:46 +00:00
|
|
|
{
|
2007-07-16 23:52:05 +00:00
|
|
|
tr_file_t * 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-07-10 03:12:46 +00:00
|
|
|
tr_torrentWriterLock( 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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor->fastResumeDirty = TRUE;
|
2007-07-10 03:41:16 +00:00
|
|
|
|
|
|
|
tr_torrentWriterUnlock( tor );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-07-13 01:17:21 +00:00
|
|
|
tr_torrentSetFileDLs ( tr_torrent_t * tor,
|
|
|
|
int * files,
|
|
|
|
int fileCount,
|
2007-07-16 19:57:34 +00:00
|
|
|
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
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
int _tr_blockSize( const tr_torrent_t * tor, int block )
|
|
|
|
{
|
|
|
|
const tr_info_t * inf = &tor->info;
|
|
|
|
int dummy;
|
|
|
|
|
|
|
|
if( block != tor->blockCount - 1 ||
|
|
|
|
!( dummy = inf->totalSize % tor->blockSize ) )
|
|
|
|
{
|
|
|
|
return tor->blockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dummy;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _tr_pieceSize( const tr_torrent_t * tor, int piece )
|
|
|
|
{
|
|
|
|
const tr_info_t * inf = &tor->info;
|
|
|
|
if( piece < inf->pieceCount - 1 ||
|
|
|
|
!( inf->totalSize % inf->pieceSize ) )
|
|
|
|
{
|
|
|
|
return inf->pieceSize;
|
|
|
|
}
|
|
|
|
return inf->totalSize % inf->pieceSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _tr_block( const tr_torrent_t * tor, int index, int begin )
|
|
|
|
{
|
|
|
|
const tr_info_t * inf = &tor->info;
|
|
|
|
return index * ( inf->pieceSize / tor->blockSize ) +
|
|
|
|
begin / tor->blockSize;
|
|
|
|
}
|