2008-04-13 14:29:11 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2008-04-13 14:29:11 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2008-04-13 14:29:11 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-04-13 14:29:11 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-05-28 17:17:12 +00:00
|
|
|
* $Id$
|
2008-04-13 14:29:11 +00:00
|
|
|
*/
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
#include <unistd.h> /* unlink */
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2008-04-13 14:29:11 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2008-04-13 22:31:07 +00:00
|
|
|
#include "bencode.h"
|
|
|
|
#include "completion.h"
|
2010-04-20 23:14:00 +00:00
|
|
|
#include "metainfo.h" /* tr_metainfoGetBasename() */
|
2008-04-13 22:31:07 +00:00
|
|
|
#include "peer-mgr.h" /* pex */
|
2010-04-20 23:14:00 +00:00
|
|
|
#include "platform.h" /* tr_getResumeDir() */
|
2008-04-13 14:29:11 +00:00
|
|
|
#include "resume.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "session.h"
|
2008-04-13 14:29:11 +00:00
|
|
|
#include "torrent.h"
|
|
|
|
#include "utils.h" /* tr_buildPath */
|
|
|
|
|
2009-04-18 23:17:30 +00:00
|
|
|
#define KEY_ACTIVITY_DATE "activity-date"
|
|
|
|
#define KEY_ADDED_DATE "added-date"
|
|
|
|
#define KEY_CORRUPT "corrupt"
|
|
|
|
#define KEY_DONE_DATE "done-date"
|
|
|
|
#define KEY_DOWNLOAD_DIR "destination"
|
|
|
|
#define KEY_DND "dnd"
|
|
|
|
#define KEY_DOWNLOADED "downloaded"
|
2009-10-19 05:05:00 +00:00
|
|
|
#define KEY_INCOMPLETE_DIR "incomplete-dir"
|
2009-04-18 23:17:30 +00:00
|
|
|
#define KEY_MAX_PEERS "max-peers"
|
|
|
|
#define KEY_PAUSED "paused"
|
2009-12-16 00:45:18 +00:00
|
|
|
#define KEY_PEERS "peers2"
|
|
|
|
#define KEY_PEERS6 "peers2-6"
|
2009-04-18 23:17:30 +00:00
|
|
|
#define KEY_FILE_PRIORITIES "priority"
|
|
|
|
#define KEY_BANDWIDTH_PRIORITY "bandwidth-priority"
|
|
|
|
#define KEY_PROGRESS "progress"
|
|
|
|
#define KEY_SPEEDLIMIT_OLD "speed-limit"
|
|
|
|
#define KEY_SPEEDLIMIT_UP "speed-limit-up"
|
|
|
|
#define KEY_SPEEDLIMIT_DOWN "speed-limit-down"
|
|
|
|
#define KEY_RATIOLIMIT "ratio-limit"
|
2010-07-24 02:57:39 +00:00
|
|
|
#define KEY_IDLELIMIT "idle-limit"
|
2009-04-18 23:17:30 +00:00
|
|
|
#define KEY_UPLOADED "uploaded"
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
#define KEY_SPEED_KiBps "speed"
|
|
|
|
#define KEY_SPEED_Bps "speed-Bps"
|
2009-03-04 19:52:57 +00:00
|
|
|
#define KEY_USE_GLOBAL_SPEED_LIMIT "use-global-speed-limit"
|
|
|
|
#define KEY_USE_SPEED_LIMIT "use-speed-limit"
|
2010-12-23 19:32:59 +00:00
|
|
|
#define KEY_TIME_SEEDING "seeding-time-seconds"
|
|
|
|
#define KEY_TIME_DOWNLOADING "downloading-time-seconds"
|
2009-03-04 19:52:57 +00:00
|
|
|
#define KEY_SPEEDLIMIT_DOWN_SPEED "down-speed"
|
|
|
|
#define KEY_SPEEDLIMIT_DOWN_MODE "down-mode"
|
|
|
|
#define KEY_SPEEDLIMIT_UP_SPEED "up-speed"
|
|
|
|
#define KEY_SPEEDLIMIT_UP_MODE "up-mode"
|
|
|
|
#define KEY_RATIOLIMIT_RATIO "ratio-limit"
|
|
|
|
#define KEY_RATIOLIMIT_MODE "ratio-mode"
|
2010-07-24 02:57:39 +00:00
|
|
|
#define KEY_IDLELIMIT_MINS "idle-limit"
|
|
|
|
#define KEY_IDLELIMIT_MODE "idle-mode"
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2010-12-09 20:43:23 +00:00
|
|
|
#define KEY_PROGRESS_CHECKTIME "time-checked"
|
2011-01-24 05:11:16 +00:00
|
|
|
#define KEY_PROGRESS_MTIMES "mtimes"
|
2010-03-15 23:29:56 +00:00
|
|
|
#define KEY_PROGRESS_BITFIELD "bitfield"
|
|
|
|
#define KEY_PROGRESS_HAVE "have"
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2009-09-10 02:20:35 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
MAX_REMEMBERED_PEERS = 200
|
|
|
|
};
|
|
|
|
|
2008-08-20 19:00:52 +00:00
|
|
|
static char*
|
|
|
|
getResumeFilename( const tr_torrent * tor )
|
2008-04-13 14:29:11 +00:00
|
|
|
{
|
2010-04-20 23:14:00 +00:00
|
|
|
char * base = tr_metainfoGetBasename( tr_torrentInfo( tor ) );
|
|
|
|
char * filename = tr_strdup_printf( "%s" TR_PATH_DELIMITER_STR "%s.resume",
|
|
|
|
tr_getResumeDir( tor->session ), base );
|
|
|
|
tr_free( base );
|
|
|
|
return filename;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2009-09-10 02:20:35 +00:00
|
|
|
savePeers( tr_benc * dict, const tr_torrent * tor )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-08-07 05:29:37 +00:00
|
|
|
int count;
|
|
|
|
tr_pex * pex;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-11-20 07:47:31 +00:00
|
|
|
count = tr_peerMgrGetPeers( (tr_torrent*) tor, &pex, TR_AF_INET, TR_PEERS_ALL, MAX_REMEMBERED_PEERS );
|
2008-04-14 14:39:13 +00:00
|
|
|
if( count > 0 )
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddRaw( dict, KEY_PEERS, pex, sizeof( tr_pex ) * count );
|
2009-01-05 18:18:13 +00:00
|
|
|
tr_free( pex );
|
2009-08-07 05:29:37 +00:00
|
|
|
|
2009-11-20 07:47:31 +00:00
|
|
|
count = tr_peerMgrGetPeers( (tr_torrent*) tor, &pex, TR_AF_INET6, TR_PEERS_ALL, MAX_REMEMBERED_PEERS );
|
2008-12-15 00:17:08 +00:00
|
|
|
if( count > 0 )
|
|
|
|
tr_bencDictAddRaw( dict, KEY_PEERS6, pex, sizeof( tr_pex ) * count );
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2008-04-14 14:39:13 +00:00
|
|
|
tr_free( pex );
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2009-09-10 02:20:35 +00:00
|
|
|
static int
|
|
|
|
addPeers( tr_torrent * tor, const uint8_t * buf, int buflen )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int numAdded = 0;
|
|
|
|
const int count = buflen / sizeof( tr_pex );
|
|
|
|
|
|
|
|
for( i=0; i<count && numAdded<MAX_REMEMBERED_PEERS; ++i )
|
|
|
|
{
|
|
|
|
tr_pex pex;
|
|
|
|
memcpy( &pex, buf + ( i * sizeof( tr_pex ) ), sizeof( tr_pex ) );
|
|
|
|
if( tr_isPex( &pex ) )
|
|
|
|
{
|
2010-04-20 21:54:03 +00:00
|
|
|
tr_peerMgrAddPex( tor, TR_PEER_FROM_RESUME, &pex, -1 );
|
2009-09-10 02:20:35 +00:00
|
|
|
++numAdded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return numAdded;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static uint64_t
|
2009-09-10 02:20:35 +00:00
|
|
|
loadPeers( tr_benc * dict, tr_torrent * tor )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint64_t ret = 0;
|
2008-08-20 18:42:45 +00:00
|
|
|
const uint8_t * str;
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t len;
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2008-08-20 18:42:45 +00:00
|
|
|
if( tr_bencDictFindRaw( dict, KEY_PEERS, &str, &len ) )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-09-10 02:20:35 +00:00
|
|
|
const int numAdded = addPeers( tor, str, len );
|
|
|
|
tr_tordbg( tor, "Loaded %d IPv4 peers from resume file", numAdded );
|
2008-12-15 00:17:08 +00:00
|
|
|
ret = TR_FR_PEERS;
|
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2008-12-15 00:17:08 +00:00
|
|
|
if( tr_bencDictFindRaw( dict, KEY_PEERS6, &str, &len ) )
|
|
|
|
{
|
2009-09-10 02:20:35 +00:00
|
|
|
const int numAdded = addPeers( tor, str, len );
|
|
|
|
tr_tordbg( tor, "Loaded %d IPv6 peers from resume file", numAdded );
|
2008-04-13 22:31:07 +00:00
|
|
|
ret = TR_FR_PEERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-14 15:17:16 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
saveDND( tr_benc * dict,
|
|
|
|
const tr_torrent * tor )
|
2008-04-14 15:17:16 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
2008-04-14 15:17:16 +00:00
|
|
|
const tr_file_index_t n = inf->fileCount;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file_index_t i;
|
|
|
|
tr_benc * list;
|
2008-04-14 15:17:16 +00:00
|
|
|
|
|
|
|
list = tr_bencDictAddList( dict, KEY_DND, n );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencListAddInt( list, inf->files[i].dnd ? 1 : 0 );
|
2008-04-14 15:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2008-09-23 19:11:04 +00:00
|
|
|
loadDND( tr_benc * dict,
|
|
|
|
tr_torrent * tor )
|
2008-04-14 15:17:16 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint64_t ret = 0;
|
|
|
|
tr_info * inf = &tor->info;
|
2008-04-14 15:17:16 +00:00
|
|
|
const tr_file_index_t n = inf->fileCount;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * list = NULL;
|
2008-04-14 15:17:16 +00:00
|
|
|
|
|
|
|
if( tr_bencDictFindList( dict, KEY_DND, &list )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& ( tr_bencListSize( list ) == n ) )
|
2008-04-14 15:17:16 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t tmp;
|
2008-04-14 15:17:16 +00:00
|
|
|
tr_file_index_t * dl = tr_new( tr_file_index_t, n );
|
|
|
|
tr_file_index_t * dnd = tr_new( tr_file_index_t, n );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file_index_t i, dlCount = 0, dndCount = 0;
|
2008-04-14 15:17:16 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
|
|
|
{
|
2008-09-04 14:42:32 +00:00
|
|
|
if( tr_bencGetInt( tr_bencListChild( list, i ), &tmp ) && tmp )
|
2008-04-14 15:17:16 +00:00
|
|
|
dnd[dndCount++] = i;
|
|
|
|
else
|
|
|
|
dl[dlCount++] = i;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( dndCount )
|
|
|
|
{
|
2008-04-14 15:17:16 +00:00
|
|
|
tr_torrentInitFileDLs ( tor, dnd, dndCount, FALSE );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_tordbg( tor, "Resume file found %d files listed as dnd",
|
|
|
|
dndCount );
|
2008-04-14 19:52:51 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
if( dlCount )
|
|
|
|
{
|
2008-04-14 15:17:16 +00:00
|
|
|
tr_torrentInitFileDLs ( tor, dl, dlCount, TRUE );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_tordbg( tor,
|
|
|
|
"Resume file found %d files marked for download",
|
|
|
|
dlCount );
|
2008-04-14 19:52:51 +00:00
|
|
|
}
|
2008-04-14 15:17:16 +00:00
|
|
|
|
|
|
|
tr_free( dnd );
|
|
|
|
tr_free( dl );
|
2009-04-18 23:17:30 +00:00
|
|
|
ret = TR_FR_DND;
|
2008-04-14 15:17:16 +00:00
|
|
|
}
|
2008-04-14 19:52:51 +00:00
|
|
|
else
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_tordbg(
|
|
|
|
tor,
|
2010-12-27 19:18:17 +00:00
|
|
|
"Couldn't load DND flags. DND list (%p) has %zu children; torrent has %d files",
|
2008-09-23 19:11:04 +00:00
|
|
|
list, tr_bencListSize( list ), (int)n );
|
2008-04-14 19:52:51 +00:00
|
|
|
}
|
2008-04-14 15:17:16 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static void
|
2009-04-18 23:17:30 +00:00
|
|
|
saveFilePriorities( tr_benc * dict, const tr_torrent * tor )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const tr_info * inf = tr_torrentInfo( tor );
|
2008-04-13 22:31:07 +00:00
|
|
|
const tr_file_index_t n = inf->fileCount;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_file_index_t i;
|
|
|
|
tr_benc * list;
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2009-04-18 23:17:30 +00:00
|
|
|
list = tr_bencDictAddList( dict, KEY_FILE_PRIORITIES, n );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencListAddInt( list, inf->files[i].priority );
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2009-04-18 23:17:30 +00:00
|
|
|
loadFilePriorities( tr_benc * dict, tr_torrent * tor )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint64_t ret = 0;
|
|
|
|
tr_info * inf = &tor->info;
|
2008-04-13 22:31:07 +00:00
|
|
|
const tr_file_index_t n = inf->fileCount;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * list;
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2009-04-18 23:17:30 +00:00
|
|
|
if( tr_bencDictFindList( dict, KEY_FILE_PRIORITIES, &list )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& ( tr_bencListSize( list ) == n ) )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-01-12 18:02:28 +00:00
|
|
|
int64_t priority;
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_file_index_t i;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
2009-01-12 18:02:28 +00:00
|
|
|
if( tr_bencGetInt( tr_bencListChild( list, i ), &priority ) )
|
|
|
|
tr_torrentInitFilePriority( tor, i, priority );
|
2009-04-18 23:17:30 +00:00
|
|
|
ret = TR_FR_FILE_PRIORITIES;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static void
|
2009-03-04 19:52:57 +00:00
|
|
|
saveSingleSpeedLimit( tr_benc * d, const tr_torrent * tor, tr_direction dir )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-03-04 19:52:57 +00:00
|
|
|
tr_bencDictReserve( d, 3 );
|
2010-07-03 00:25:22 +00:00
|
|
|
tr_bencDictAddInt( d, KEY_SPEED_Bps, tr_torrentGetSpeedLimit_Bps( tor, dir ) );
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bencDictAddBool( d, KEY_USE_GLOBAL_SPEED_LIMIT, tr_torrentUsesSessionLimits( tor ) );
|
|
|
|
tr_bencDictAddBool( d, KEY_USE_SPEED_LIMIT, tr_torrentUsesSpeedLimit( tor, dir ) );
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
static void
|
2009-03-04 19:52:57 +00:00
|
|
|
saveSpeedLimits( tr_benc * dict, const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
saveSingleSpeedLimit( tr_bencDictAddDict( dict, KEY_SPEEDLIMIT_DOWN, 0 ), tor, TR_DOWN );
|
|
|
|
saveSingleSpeedLimit( tr_bencDictAddDict( dict, KEY_SPEEDLIMIT_UP, 0 ), tor, TR_UP );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
saveRatioLimits( tr_benc * dict, const tr_torrent * tor )
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_benc * d = tr_bencDictAddDict( dict, KEY_RATIOLIMIT, 2 );
|
|
|
|
tr_bencDictAddReal( d, KEY_RATIOLIMIT_RATIO, tr_torrentGetRatioLimit( tor ) );
|
|
|
|
tr_bencDictAddInt( d, KEY_RATIOLIMIT_MODE, tr_torrentGetRatioMode( tor ) );
|
2009-02-13 18:23:56 +00:00
|
|
|
}
|
|
|
|
|
2010-07-16 03:12:57 +00:00
|
|
|
static void
|
2010-07-24 02:57:39 +00:00
|
|
|
saveIdleLimits( tr_benc * dict, const tr_torrent * tor )
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
2010-07-24 02:57:39 +00:00
|
|
|
tr_benc * d = tr_bencDictAddDict( dict, KEY_IDLELIMIT, 2 );
|
|
|
|
tr_bencDictAddInt( d, KEY_IDLELIMIT_MINS, tr_torrentGetIdleLimit( tor ) );
|
|
|
|
tr_bencDictAddInt( d, KEY_IDLELIMIT_MODE, tr_torrentGetIdleMode( tor ) );
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 19:52:57 +00:00
|
|
|
static void
|
|
|
|
loadSingleSpeedLimit( tr_benc * d, tr_direction dir, tr_torrent * tor )
|
|
|
|
{
|
|
|
|
int64_t i;
|
2009-03-29 23:05:32 +00:00
|
|
|
tr_bool boolVal;
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
if( tr_bencDictFindInt( d, KEY_SPEED_Bps, &i ) )
|
|
|
|
tr_torrentSetSpeedLimit_Bps( tor, dir, i );
|
|
|
|
else if( tr_bencDictFindInt( d, KEY_SPEED_KiBps, &i ) )
|
|
|
|
tr_torrentSetSpeedLimit_Bps( tor, dir, i*1024 );
|
2009-03-29 23:05:32 +00:00
|
|
|
|
|
|
|
if( tr_bencDictFindBool( d, KEY_USE_SPEED_LIMIT, &boolVal ) )
|
|
|
|
tr_torrentUseSpeedLimit( tor, dir, boolVal );
|
|
|
|
|
|
|
|
if( tr_bencDictFindBool( d, KEY_USE_GLOBAL_SPEED_LIMIT, &boolVal ) )
|
|
|
|
tr_torrentUseSessionLimits( tor, boolVal );
|
2009-03-04 19:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum old_speed_modes
|
|
|
|
{
|
|
|
|
TR_SPEEDLIMIT_GLOBAL, /* only follow the overall speed limit */
|
|
|
|
TR_SPEEDLIMIT_SINGLE /* only follow the per-torrent limit */
|
|
|
|
};
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static uint64_t
|
2009-03-04 19:52:57 +00:00
|
|
|
loadSpeedLimits( tr_benc * dict, tr_torrent * tor )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
uint64_t ret = 0;
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_benc * d;
|
|
|
|
|
2009-03-04 19:52:57 +00:00
|
|
|
if( tr_bencDictFindDict( dict, KEY_SPEEDLIMIT_UP, &d ) )
|
|
|
|
{
|
|
|
|
loadSingleSpeedLimit( d, TR_UP, tor );
|
|
|
|
ret = TR_FR_SPEEDLIMIT;
|
|
|
|
}
|
|
|
|
if( tr_bencDictFindDict( dict, KEY_SPEEDLIMIT_DOWN, &d ) )
|
|
|
|
{
|
|
|
|
loadSingleSpeedLimit( d, TR_DOWN, tor );
|
|
|
|
ret = TR_FR_SPEEDLIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* older speedlimit structure */
|
|
|
|
if( !ret && tr_bencDictFindDict( dict, KEY_SPEEDLIMIT_OLD, &d ) )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2009-03-04 19:52:57 +00:00
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
int64_t i;
|
|
|
|
if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_DOWN_SPEED, &i ) )
|
2010-07-03 00:25:22 +00:00
|
|
|
tr_torrentSetSpeedLimit_Bps( tor, TR_DOWN, i*1024 );
|
2009-03-04 19:52:57 +00:00
|
|
|
if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_DOWN_MODE, &i ) ) {
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_torrentUseSpeedLimit( tor, TR_DOWN, i==TR_SPEEDLIMIT_SINGLE );
|
|
|
|
tr_torrentUseSessionLimits( tor, i==TR_SPEEDLIMIT_GLOBAL );
|
2009-03-04 19:52:57 +00:00
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_UP_SPEED, &i ) )
|
2010-07-03 00:25:22 +00:00
|
|
|
tr_torrentSetSpeedLimit_Bps( tor, TR_UP, i*1024 );
|
2009-03-04 19:52:57 +00:00
|
|
|
if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_UP_MODE, &i ) ) {
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_torrentUseSpeedLimit( tor, TR_UP, i==TR_SPEEDLIMIT_SINGLE );
|
|
|
|
tr_torrentUseSessionLimits( tor, i==TR_SPEEDLIMIT_GLOBAL );
|
2009-03-04 19:52:57 +00:00
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
ret = TR_FR_SPEEDLIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
static uint64_t
|
|
|
|
loadRatioLimits( tr_benc * dict,
|
|
|
|
tr_torrent * tor )
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
tr_benc * d;
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( dict, KEY_RATIOLIMIT, &d ) )
|
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
double dratio;
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindReal( d, KEY_RATIOLIMIT_RATIO, &dratio ) )
|
2009-02-13 18:23:56 +00:00
|
|
|
tr_torrentSetRatioLimit( tor, dratio );
|
|
|
|
if( tr_bencDictFindInt( d, KEY_RATIOLIMIT_MODE, &i ) )
|
|
|
|
tr_torrentSetRatioMode( tor, i );
|
|
|
|
ret = TR_FR_RATIOLIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-07-16 03:12:57 +00:00
|
|
|
|
|
|
|
static uint64_t
|
2010-07-24 02:57:39 +00:00
|
|
|
loadIdleLimits( tr_benc * dict,
|
2010-07-16 03:12:57 +00:00
|
|
|
tr_torrent * tor )
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
tr_benc * d;
|
|
|
|
|
2010-07-24 02:57:39 +00:00
|
|
|
if( tr_bencDictFindDict( dict, KEY_IDLELIMIT, &d ) )
|
2010-07-16 03:12:57 +00:00
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
int64_t imin;
|
2010-07-24 02:57:39 +00:00
|
|
|
if( tr_bencDictFindInt( d, KEY_IDLELIMIT_MINS, &imin ) )
|
|
|
|
tr_torrentSetIdleLimit( tor, imin );
|
|
|
|
if( tr_bencDictFindInt( d, KEY_IDLELIMIT_MODE, &i ) )
|
|
|
|
tr_torrentSetIdleMode( tor, i );
|
|
|
|
ret = TR_FR_IDLELIMIT;
|
2010-07-16 03:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
saveProgress( tr_benc * dict,
|
|
|
|
const tr_torrent * tor )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t i, n;
|
|
|
|
tr_benc * p;
|
|
|
|
tr_benc * m;
|
2008-04-13 22:31:07 +00:00
|
|
|
const tr_bitfield * bitfield;
|
|
|
|
|
|
|
|
p = tr_bencDictAdd( dict, KEY_PROGRESS );
|
|
|
|
tr_bencInitDict( p, 2 );
|
|
|
|
|
2010-12-09 20:43:23 +00:00
|
|
|
/* add each piece's timeChecked */
|
|
|
|
n = tor->info.pieceCount;
|
|
|
|
m = tr_bencDictAddList( p, KEY_PROGRESS_CHECKTIME, n );
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
tr_bencListAddInt( m, tor->info.pieces[i].timeChecked );
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2010-03-15 23:29:56 +00:00
|
|
|
/* add the progress */
|
|
|
|
if( tor->completeness == TR_SEED )
|
|
|
|
tr_bencDictAddStr( p, KEY_PROGRESS_HAVE, "all" );
|
2009-01-02 17:01:55 +00:00
|
|
|
bitfield = tr_cpBlockBitfield( &tor->completion );
|
2008-04-18 23:17:40 +00:00
|
|
|
tr_bencDictAddRaw( p, KEY_PROGRESS_BITFIELD,
|
2008-06-07 01:44:54 +00:00
|
|
|
bitfield->bits, bitfield->byteCount );
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2008-09-23 19:11:04 +00:00
|
|
|
loadProgress( tr_benc * dict,
|
|
|
|
tr_torrent * tor )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2010-12-09 20:43:23 +00:00
|
|
|
size_t i, n;
|
2008-09-23 19:11:04 +00:00
|
|
|
uint64_t ret = 0;
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_benc * p;
|
|
|
|
|
2010-12-09 20:43:23 +00:00
|
|
|
for( i=0, n=tor->info.pieceCount; i<n; ++i )
|
|
|
|
tor->info.pieces[i].timeChecked = 0;
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
if( tr_bencDictFindDict( dict, KEY_PROGRESS, &p ) )
|
|
|
|
{
|
2010-03-15 23:29:56 +00:00
|
|
|
const char * err;
|
|
|
|
const char * str;
|
2008-09-23 19:11:04 +00:00
|
|
|
const uint8_t * raw;
|
|
|
|
size_t rawlen;
|
|
|
|
tr_benc * m;
|
2010-12-09 20:43:23 +00:00
|
|
|
int64_t timeChecked;
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2010-12-09 20:43:23 +00:00
|
|
|
if( tr_bencDictFindList( p, KEY_PROGRESS_CHECKTIME, &m ) )
|
2011-01-24 05:11:16 +00:00
|
|
|
{
|
|
|
|
/* This key was added in 2.20.
|
|
|
|
Load in the timestamp of when we last checked each piece */
|
2010-12-09 20:43:23 +00:00
|
|
|
for( i=0, n=tor->info.pieceCount; i<n; ++i )
|
|
|
|
if( tr_bencGetInt( tr_bencListChild( m, i ), &timeChecked ) )
|
|
|
|
tor->info.pieces[i].timeChecked = (time_t)timeChecked;
|
2011-01-24 05:11:16 +00:00
|
|
|
}
|
|
|
|
else if( tr_bencDictFindList( p, KEY_PROGRESS_MTIMES, &m ) )
|
|
|
|
{
|
|
|
|
/* This is how it was done pre-2.20... per file. */
|
|
|
|
for( i=0, n=tr_bencListSize(m); i<n; ++i )
|
|
|
|
{
|
|
|
|
/* get the timestamp of file #i */
|
|
|
|
if( tr_bencGetInt( tr_bencListChild( m, i ), &timeChecked ) )
|
|
|
|
{
|
|
|
|
/* walk through all the pieces that are in that file... */
|
|
|
|
tr_piece_index_t j;
|
|
|
|
tr_file * file = &tor->info.files[i];
|
|
|
|
for( j=file->firstPiece; j<=file->lastPiece; ++j )
|
|
|
|
{
|
|
|
|
tr_piece * piece = &tor->info.pieces[j];
|
|
|
|
|
|
|
|
/* If the piece's timestamp is unset from earlier,
|
|
|
|
* set it here. */
|
|
|
|
if( piece->timeChecked == 0 )
|
|
|
|
piece->timeChecked = timeChecked;
|
|
|
|
|
|
|
|
/* If the piece's timestamp is *newer* timeChecked,
|
|
|
|
* the piece probably spans more than one file.
|
|
|
|
* To be safe, let's use the older timestamp. */
|
|
|
|
if( piece->timeChecked > timeChecked )
|
|
|
|
piece->timeChecked = timeChecked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2010-03-15 23:29:56 +00:00
|
|
|
err = NULL;
|
|
|
|
if( tr_bencDictFindStr( p, KEY_PROGRESS_HAVE, &str ) )
|
|
|
|
{
|
|
|
|
if( !strcmp( str, "all" ) )
|
|
|
|
tr_cpSetHaveAll( &tor->completion );
|
|
|
|
else
|
|
|
|
err = "Invalid value for HAVE";
|
|
|
|
}
|
|
|
|
else if( tr_bencDictFindRaw( p, KEY_PROGRESS_BITFIELD, &raw, &rawlen ) )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
|
|
|
tr_bitfield tmp;
|
2008-08-20 18:42:45 +00:00
|
|
|
tmp.byteCount = rawlen;
|
2008-06-07 14:41:31 +00:00
|
|
|
tmp.bitCount = tmp.byteCount * 8;
|
2008-08-20 18:42:45 +00:00
|
|
|
tmp.bits = (uint8_t*) raw;
|
2009-01-02 17:01:55 +00:00
|
|
|
if( !tr_cpBlockBitfieldSet( &tor->completion, &tmp ) )
|
2010-03-15 23:29:56 +00:00
|
|
|
err = "Error loading bitfield";
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
2010-03-15 23:29:56 +00:00
|
|
|
else err = "Couldn't find 'have' or 'bitfield'";
|
2010-12-09 20:43:23 +00:00
|
|
|
|
2010-03-15 23:29:56 +00:00
|
|
|
if( err != NULL )
|
|
|
|
tr_tordbg( tor, "Torrent needs to be verified - %s", err );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
ret = TR_FR_PROGRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
void
|
2010-03-22 00:55:43 +00:00
|
|
|
tr_torrentSaveResume( tr_torrent * tor )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2010-03-22 00:55:43 +00:00
|
|
|
int err;
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_benc top;
|
2010-03-22 00:55:43 +00:00
|
|
|
char * filename;
|
|
|
|
|
2009-08-07 05:29:37 +00:00
|
|
|
if( !tr_isTorrent( tor ) )
|
2008-07-02 01:46:10 +00:00
|
|
|
return;
|
|
|
|
|
2010-12-04 14:53:53 +00:00
|
|
|
tr_bencInitDict( &top, 50 ); /* arbitrary "big enough" number */
|
2010-12-23 19:32:59 +00:00
|
|
|
tr_bencDictAddInt( &top, KEY_TIME_SEEDING, tor->secondsSeeding );
|
|
|
|
tr_bencDictAddInt( &top, KEY_TIME_DOWNLOADING, tor->secondsDownloading );
|
2009-10-19 05:05:00 +00:00
|
|
|
tr_bencDictAddInt( &top, KEY_ACTIVITY_DATE, tor->activityDate );
|
|
|
|
tr_bencDictAddInt( &top, KEY_ADDED_DATE, tor->addedDate );
|
|
|
|
tr_bencDictAddInt( &top, KEY_CORRUPT, tor->corruptPrev + tor->corruptCur );
|
|
|
|
tr_bencDictAddInt( &top, KEY_DONE_DATE, tor->doneDate );
|
|
|
|
tr_bencDictAddStr( &top, KEY_DOWNLOAD_DIR, tor->downloadDir );
|
|
|
|
if( tor->incompleteDir != NULL )
|
|
|
|
tr_bencDictAddStr( &top, KEY_INCOMPLETE_DIR, tor->incompleteDir );
|
|
|
|
tr_bencDictAddInt( &top, KEY_DOWNLOADED, tor->downloadedPrev + tor->downloadedCur );
|
|
|
|
tr_bencDictAddInt( &top, KEY_UPLOADED, tor->uploadedPrev + tor->uploadedCur );
|
|
|
|
tr_bencDictAddInt( &top, KEY_MAX_PEERS, tor->maxConnectedPeers );
|
|
|
|
tr_bencDictAddInt( &top, KEY_BANDWIDTH_PRIORITY, tr_torrentGetPriority( tor ) );
|
2009-03-29 23:13:46 +00:00
|
|
|
tr_bencDictAddBool( &top, KEY_PAUSED, !tor->isRunning );
|
2008-04-13 22:31:07 +00:00
|
|
|
savePeers( &top, tor );
|
2010-02-02 22:45:22 +00:00
|
|
|
if( tr_torrentHasMetadata( tor ) )
|
|
|
|
{
|
|
|
|
saveFilePriorities( &top, tor );
|
|
|
|
saveDND( &top, tor );
|
|
|
|
saveProgress( &top, tor );
|
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
saveSpeedLimits( &top, tor );
|
2009-02-13 18:23:56 +00:00
|
|
|
saveRatioLimits( &top, tor );
|
2010-07-24 02:57:39 +00:00
|
|
|
saveIdleLimits( &top, tor );
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2008-08-20 19:00:52 +00:00
|
|
|
filename = getResumeFilename( tor );
|
2010-03-22 00:55:43 +00:00
|
|
|
if(( err = tr_bencToFile( &top, TR_FMT_BENC, filename )))
|
|
|
|
tr_torrentSetLocalError( tor, "Unable to save resume file: %s", tr_strerror( err ) );
|
2008-08-20 19:00:52 +00:00
|
|
|
tr_free( filename );
|
2008-04-13 22:31:07 +00:00
|
|
|
|
|
|
|
tr_bencFree( &top );
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2008-04-22 20:51:15 +00:00
|
|
|
static uint64_t
|
2008-09-23 19:11:04 +00:00
|
|
|
loadFromFile( tr_torrent * tor,
|
|
|
|
uint64_t fieldsToLoad )
|
2008-04-13 14:29:11 +00:00
|
|
|
{
|
2009-08-14 14:41:59 +00:00
|
|
|
int64_t i;
|
2008-04-13 22:31:07 +00:00
|
|
|
const char * str;
|
2009-08-14 14:41:59 +00:00
|
|
|
uint64_t fieldsLoaded = 0;
|
|
|
|
char * filename;
|
|
|
|
tr_benc top;
|
|
|
|
tr_bool boolVal;
|
|
|
|
const tr_bool wasDirty = tor->isDirty;
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2009-09-28 18:22:57 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-08-20 19:00:52 +00:00
|
|
|
filename = getResumeFilename( tor );
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2009-06-02 01:48:48 +00:00
|
|
|
if( tr_bencLoadFile( &top, TR_FMT_BENC, filename ) )
|
2008-04-14 03:12:24 +00:00
|
|
|
{
|
2009-10-19 05:05:00 +00:00
|
|
|
tr_tordbg( tor, "Couldn't read \"%s\"", filename );
|
2008-04-14 03:12:24 +00:00
|
|
|
|
2008-08-20 19:00:52 +00:00
|
|
|
tr_free( filename );
|
2008-04-14 03:12:24 +00:00
|
|
|
return fieldsLoaded;
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
|
|
|
|
tr_tordbg( tor, "Read resume file \"%s\"", filename );
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( ( fieldsToLoad & TR_FR_CORRUPT )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& tr_bencDictFindInt( &top, KEY_CORRUPT, &i ) )
|
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
tor->corruptPrev = i;
|
|
|
|
fieldsLoaded |= TR_FR_CORRUPT;
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( ( fieldsToLoad & ( TR_FR_PROGRESS | TR_FR_DOWNLOAD_DIR ) )
|
2008-12-03 01:25:45 +00:00
|
|
|
&& ( tr_bencDictFindStr( &top, KEY_DOWNLOAD_DIR, &str ) )
|
|
|
|
&& ( str && *str ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_free( tor->downloadDir );
|
|
|
|
tor->downloadDir = tr_strdup( str );
|
|
|
|
fieldsLoaded |= TR_FR_DOWNLOAD_DIR;
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2009-10-19 05:05:00 +00:00
|
|
|
if( ( fieldsToLoad & ( TR_FR_PROGRESS | TR_FR_INCOMPLETE_DIR ) )
|
|
|
|
&& ( tr_bencDictFindStr( &top, KEY_INCOMPLETE_DIR, &str ) )
|
|
|
|
&& ( str && *str ) )
|
|
|
|
{
|
|
|
|
tr_free( tor->incompleteDir );
|
|
|
|
tor->incompleteDir = tr_strdup( str );
|
|
|
|
fieldsLoaded |= TR_FR_INCOMPLETE_DIR;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( ( fieldsToLoad & TR_FR_DOWNLOADED )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& tr_bencDictFindInt( &top, KEY_DOWNLOADED, &i ) )
|
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
tor->downloadedPrev = i;
|
|
|
|
fieldsLoaded |= TR_FR_DOWNLOADED;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( ( fieldsToLoad & TR_FR_UPLOADED )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& tr_bencDictFindInt( &top, KEY_UPLOADED, &i ) )
|
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
tor->uploadedPrev = i;
|
|
|
|
fieldsLoaded |= TR_FR_UPLOADED;
|
|
|
|
}
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( ( fieldsToLoad & TR_FR_MAX_PEERS )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& tr_bencDictFindInt( &top, KEY_MAX_PEERS, &i ) )
|
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
tor->maxConnectedPeers = i;
|
|
|
|
fieldsLoaded |= TR_FR_MAX_PEERS;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( ( fieldsToLoad & TR_FR_RUN )
|
2009-03-29 23:05:32 +00:00
|
|
|
&& tr_bencDictFindBool( &top, KEY_PAUSED, &boolVal ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-03-29 23:05:32 +00:00
|
|
|
tor->isRunning = !boolVal;
|
2008-04-13 22:31:07 +00:00
|
|
|
fieldsLoaded |= TR_FR_RUN;
|
|
|
|
}
|
|
|
|
|
2008-06-02 04:41:55 +00:00
|
|
|
if( ( fieldsToLoad & TR_FR_ADDED_DATE )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& tr_bencDictFindInt( &top, KEY_ADDED_DATE, &i ) )
|
|
|
|
{
|
2008-06-02 04:41:55 +00:00
|
|
|
tor->addedDate = i;
|
|
|
|
fieldsLoaded |= TR_FR_ADDED_DATE;
|
|
|
|
}
|
|
|
|
|
2008-06-03 19:16:12 +00:00
|
|
|
if( ( fieldsToLoad & TR_FR_DONE_DATE )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& tr_bencDictFindInt( &top, KEY_DONE_DATE, &i ) )
|
|
|
|
{
|
2008-06-03 19:16:12 +00:00
|
|
|
tor->doneDate = i;
|
|
|
|
fieldsLoaded |= TR_FR_DONE_DATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( fieldsToLoad & TR_FR_ACTIVITY_DATE )
|
2008-09-23 19:11:04 +00:00
|
|
|
&& tr_bencDictFindInt( &top, KEY_ACTIVITY_DATE, &i ) )
|
|
|
|
{
|
2009-03-28 16:47:01 +00:00
|
|
|
tr_torrentSetActivityDate( tor, i );
|
2008-06-03 19:16:12 +00:00
|
|
|
fieldsLoaded |= TR_FR_ACTIVITY_DATE;
|
|
|
|
}
|
|
|
|
|
2010-12-23 19:32:59 +00:00
|
|
|
if( ( fieldsToLoad & TR_FR_TIME_SEEDING )
|
|
|
|
&& tr_bencDictFindInt( &top, KEY_TIME_SEEDING, &i ) )
|
|
|
|
{
|
|
|
|
tor->secondsSeeding = i;
|
|
|
|
fieldsLoaded |= TR_FR_TIME_SEEDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( fieldsToLoad & TR_FR_TIME_DOWNLOADING )
|
|
|
|
&& tr_bencDictFindInt( &top, KEY_TIME_DOWNLOADING, &i ) )
|
|
|
|
{
|
|
|
|
tor->secondsDownloading = i;
|
|
|
|
fieldsLoaded |= TR_FR_TIME_DOWNLOADING;
|
|
|
|
}
|
|
|
|
|
2009-04-18 23:17:30 +00:00
|
|
|
if( ( fieldsToLoad & TR_FR_BANDWIDTH_PRIORITY )
|
|
|
|
&& tr_bencDictFindInt( &top, KEY_BANDWIDTH_PRIORITY, &i )
|
|
|
|
&& tr_isPriority( i ) )
|
|
|
|
{
|
|
|
|
tr_torrentSetPriority( tor, i );
|
|
|
|
fieldsLoaded |= TR_FR_BANDWIDTH_PRIORITY;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( fieldsToLoad & TR_FR_PEERS )
|
|
|
|
fieldsLoaded |= loadPeers( &top, tor );
|
|
|
|
|
2009-04-18 23:17:30 +00:00
|
|
|
if( fieldsToLoad & TR_FR_FILE_PRIORITIES )
|
|
|
|
fieldsLoaded |= loadFilePriorities( &top, tor );
|
2008-04-14 11:52:50 +00:00
|
|
|
|
|
|
|
if( fieldsToLoad & TR_FR_PROGRESS )
|
|
|
|
fieldsLoaded |= loadProgress( &top, tor );
|
|
|
|
|
2008-04-14 15:17:16 +00:00
|
|
|
if( fieldsToLoad & TR_FR_DND )
|
|
|
|
fieldsLoaded |= loadDND( &top, tor );
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( fieldsToLoad & TR_FR_SPEEDLIMIT )
|
|
|
|
fieldsLoaded |= loadSpeedLimits( &top, tor );
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
if( fieldsToLoad & TR_FR_RATIOLIMIT )
|
|
|
|
fieldsLoaded |= loadRatioLimits( &top, tor );
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2010-07-24 02:57:39 +00:00
|
|
|
if( fieldsToLoad & TR_FR_IDLELIMIT )
|
|
|
|
fieldsLoaded |= loadIdleLimits( &top, tor );
|
2010-07-16 03:12:57 +00:00
|
|
|
|
2009-08-14 14:41:59 +00:00
|
|
|
/* loading the resume file triggers of a lot of changes,
|
|
|
|
* but none of them needs to trigger a re-saving of the
|
|
|
|
* same resume information... */
|
|
|
|
tor->isDirty = wasDirty;
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_bencFree( &top );
|
2008-08-20 19:00:52 +00:00
|
|
|
tr_free( filename );
|
2008-04-13 22:31:07 +00:00
|
|
|
return fieldsLoaded;
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2008-04-18 12:47:13 +00:00
|
|
|
static uint64_t
|
2008-09-23 19:11:04 +00:00
|
|
|
setFromCtor( tr_torrent * tor,
|
|
|
|
uint64_t fields,
|
|
|
|
const tr_ctor * ctor,
|
|
|
|
int mode )
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( fields & TR_FR_RUN )
|
|
|
|
{
|
2008-04-18 12:47:13 +00:00
|
|
|
uint8_t isPaused;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !tr_ctorGetPaused( ctor, mode, &isPaused ) )
|
|
|
|
{
|
2008-04-18 12:47:13 +00:00
|
|
|
tor->isRunning = !isPaused;
|
|
|
|
ret |= TR_FR_RUN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( fields & TR_FR_MAX_PEERS )
|
2008-05-12 16:33:17 +00:00
|
|
|
if( !tr_ctorGetPeerLimit( ctor, mode, &tor->maxConnectedPeers ) )
|
2008-04-18 12:47:13 +00:00
|
|
|
ret |= TR_FR_MAX_PEERS;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( fields & TR_FR_DOWNLOAD_DIR )
|
|
|
|
{
|
2008-12-03 01:25:45 +00:00
|
|
|
const char * path;
|
|
|
|
if( !tr_ctorGetDownloadDir( ctor, mode, &path ) && path && *path )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
ret |= TR_FR_DOWNLOAD_DIR;
|
|
|
|
tr_free( tor->downloadDir );
|
2008-12-03 01:25:45 +00:00
|
|
|
tor->downloadDir = tr_strdup( path );
|
2008-04-18 12:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2008-09-23 19:11:04 +00:00
|
|
|
useManditoryFields( tr_torrent * tor,
|
|
|
|
uint64_t fields,
|
|
|
|
const tr_ctor * ctor )
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
|
|
|
return setFromCtor( tor, fields, ctor, TR_FORCE );
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2008-09-23 19:11:04 +00:00
|
|
|
useFallbackFields( tr_torrent * tor,
|
|
|
|
uint64_t fields,
|
|
|
|
const tr_ctor * ctor )
|
2008-04-18 12:47:13 +00:00
|
|
|
{
|
|
|
|
return setFromCtor( tor, fields, ctor, TR_FALLBACK );
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrentLoadResume( tr_torrent * tor,
|
2008-04-18 12:47:13 +00:00
|
|
|
uint64_t fieldsToLoad,
|
|
|
|
const tr_ctor * ctor )
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
|
2009-09-28 18:22:57 +00:00
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
2008-04-18 12:47:13 +00:00
|
|
|
ret |= useManditoryFields( tor, fieldsToLoad, ctor );
|
|
|
|
fieldsToLoad &= ~ret;
|
|
|
|
ret |= loadFromFile( tor, fieldsToLoad );
|
|
|
|
fieldsToLoad &= ~ret;
|
|
|
|
ret |= useFallbackFields( tor, fieldsToLoad, ctor );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-13 14:29:11 +00:00
|
|
|
void
|
|
|
|
tr_torrentRemoveResume( const tr_torrent * tor )
|
|
|
|
{
|
2008-08-20 19:00:52 +00:00
|
|
|
char * filename = getResumeFilename( tor );
|
2008-04-13 14:29:11 +00:00
|
|
|
unlink( filename );
|
2008-08-20 19:00:52 +00:00
|
|
|
tr_free( filename );
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|