2008-04-13 14:29:11 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
|
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
|
|
|
* $Id:$
|
|
|
|
*/
|
|
|
|
|
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"
|
2008-04-13 14:29:11 +00:00
|
|
|
#include "fastresume.h"
|
2008-04-13 22:31:07 +00:00
|
|
|
#include "peer-mgr.h" /* pex */
|
2008-04-13 14:29:11 +00:00
|
|
|
#include "platform.h" /* tr_getResumeDir */
|
|
|
|
#include "resume.h"
|
|
|
|
#include "torrent.h"
|
|
|
|
#include "utils.h" /* tr_buildPath */
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
#define KEY_CORRUPT "corrupt"
|
|
|
|
#define KEY_DESTINATION "destination"
|
2008-04-14 15:17:16 +00:00
|
|
|
#define KEY_DND "dnd"
|
2008-04-13 22:31:07 +00:00
|
|
|
#define KEY_DOWNLOADED "downloaded"
|
|
|
|
#define KEY_MAX_PEERS "max-peers"
|
|
|
|
#define KEY_PAUSED "paused"
|
|
|
|
#define KEY_PEERS "peers"
|
|
|
|
#define KEY_PRIORITY "priority"
|
|
|
|
#define KEY_PROGRESS "progress"
|
|
|
|
#define KEY_SPEEDLIMIT "speed-limit"
|
|
|
|
#define KEY_UPLOADED "uploaded"
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2008-04-14 14:39:13 +00:00
|
|
|
#define KEY_PROGRESS_MTIMES "mtimes"
|
2008-04-13 22:31:07 +00:00
|
|
|
#define KEY_PROGRESS_BITFIELD "bitfield"
|
|
|
|
|
2008-04-13 14:29:11 +00:00
|
|
|
static void
|
|
|
|
getResumeFilename( char * buf, size_t buflen, const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
const char * dir = tr_getResumeDir( tor->handle );
|
2008-04-22 20:51:15 +00:00
|
|
|
char base[MAX_PATH_LENGTH];
|
2008-04-14 11:52:50 +00:00
|
|
|
snprintf( base, sizeof( base ), "%s.%16.16s.resume",
|
|
|
|
tor->info.name,
|
|
|
|
tor->info.hashString );
|
2008-04-13 14:29:11 +00:00
|
|
|
tr_buildPath( buf, buflen, dir, base, NULL );
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
savePeers( tr_benc * dict, const tr_torrent * tor )
|
|
|
|
{
|
2008-04-14 14:39:13 +00:00
|
|
|
tr_pex * pex = NULL;
|
2008-04-14 11:52:50 +00:00
|
|
|
const int count = tr_peerMgrGetPeers( tor->handle->peerMgr,
|
|
|
|
tor->info.hash, &pex );
|
2008-04-14 14:39:13 +00:00
|
|
|
if( count > 0 )
|
2008-04-18 23:17:40 +00:00
|
|
|
tr_bencDictAddRaw( dict, KEY_PEERS, pex, sizeof(tr_pex)*count );
|
2008-04-14 14:39:13 +00:00
|
|
|
tr_free( pex );
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
loadPeers( tr_benc * dict, tr_torrent * tor )
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
tr_benc * p;
|
|
|
|
|
|
|
|
if(( p = tr_bencDictFindType( dict, KEY_PEERS, TYPE_STR )))
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const char * str = p->val.s.s;
|
|
|
|
const size_t len = p->val.s.i;
|
|
|
|
const int count = len / sizeof( tr_pex );
|
|
|
|
for( i=0; i<count; ++i ) {
|
|
|
|
tr_pex pex;
|
|
|
|
memcpy( &pex, str + (i*sizeof(tr_pex)), sizeof(tr_pex) );
|
2008-04-14 11:52:50 +00:00
|
|
|
tr_peerMgrAddPex( tor->handle->peerMgr,
|
|
|
|
tor->info.hash, TR_PEER_FROM_CACHE, &pex );
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
tr_tordbg( tor, "Loaded %d peers from resume file", count );
|
|
|
|
ret = TR_FR_PEERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-14 15:17:16 +00:00
|
|
|
static void
|
|
|
|
saveDND( tr_benc * dict, const tr_torrent * tor )
|
|
|
|
{
|
2008-04-22 20:51:15 +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;
|
|
|
|
tr_file_index_t i;
|
|
|
|
tr_benc * list;
|
|
|
|
|
|
|
|
list = tr_bencDictAddList( dict, KEY_DND, n );
|
|
|
|
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
|
|
|
|
loadDND( tr_benc * dict, tr_torrent * tor )
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
tr_info * inf = &tor->info;
|
|
|
|
const tr_file_index_t n = inf->fileCount;
|
2008-04-14 19:52:51 +00:00
|
|
|
tr_benc * list = NULL;
|
2008-04-14 15:17:16 +00:00
|
|
|
|
|
|
|
if( tr_bencDictFindList( dict, KEY_DND, &list )
|
|
|
|
&& ( list->val.l.count == (int)n ) )
|
|
|
|
{
|
|
|
|
int64_t tmp;
|
|
|
|
tr_file_index_t * dl = tr_new( tr_file_index_t, n );
|
|
|
|
tr_file_index_t * dnd = tr_new( tr_file_index_t, n );
|
|
|
|
tr_file_index_t i, dlCount=0, dndCount=0;
|
|
|
|
|
|
|
|
for( i=0; i<n; ++i ) {
|
|
|
|
if( tr_bencGetInt( &list->val.l.vals[i], &tmp ) && tmp )
|
|
|
|
dnd[dndCount++] = i;
|
|
|
|
else
|
|
|
|
dl[dlCount++] = i;
|
|
|
|
}
|
|
|
|
|
2008-04-14 19:52:51 +00:00
|
|
|
if( dndCount ) {
|
2008-04-14 15:17:16 +00:00
|
|
|
tr_torrentInitFileDLs ( tor, dnd, dndCount, FALSE );
|
2008-04-14 19:52:51 +00:00
|
|
|
tr_tordbg( tor, "Resume file found %d files listed as dnd", dndCount );
|
|
|
|
}
|
|
|
|
if( dlCount ) {
|
2008-04-14 15:17:16 +00:00
|
|
|
tr_torrentInitFileDLs ( tor, dl, dlCount, TRUE );
|
2008-04-14 19:52:51 +00:00
|
|
|
tr_tordbg( tor, "Resume file found %d files marked for download", dlCount );
|
|
|
|
}
|
2008-04-14 15:17:16 +00:00
|
|
|
|
|
|
|
tr_free( dnd );
|
|
|
|
tr_free( dl );
|
|
|
|
ret = TR_FR_PRIORITY;
|
|
|
|
}
|
2008-04-14 19:52:51 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_tordbg( tor, "Couldn't load DND flags. dnd list (%p) has %d children; torrent has %d files",
|
2008-04-15 13:27:28 +00:00
|
|
|
list, ( list ? list->val.l.count : -1 ), (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
|
|
|
|
savePriorities( tr_benc * dict, const tr_torrent * tor )
|
|
|
|
{
|
2008-04-22 20:51:15 +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;
|
|
|
|
tr_file_index_t i;
|
|
|
|
tr_benc * list;
|
|
|
|
|
2008-04-14 15:17:16 +00:00
|
|
|
list = tr_bencDictAddList( dict, KEY_PRIORITY, n );
|
2008-04-13 22:31:07 +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
|
|
|
|
loadPriorities( tr_benc * dict, tr_torrent * tor )
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
tr_info * inf = &tor->info;
|
|
|
|
const tr_file_index_t n = inf->fileCount;
|
|
|
|
tr_benc * list;
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( tr_bencDictFindList( dict, KEY_PRIORITY, &list )
|
|
|
|
&& ( list->val.l.count == (int)n ) )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
|
|
|
int64_t tmp;
|
|
|
|
tr_file_index_t i;
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
if( tr_bencGetInt( &list->val.l.vals[i], &tmp ) )
|
|
|
|
inf->files[i].priority = tmp;
|
|
|
|
ret = TR_FR_PRIORITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static void
|
|
|
|
saveSpeedLimits( tr_benc * dict, const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
tr_benc * d = tr_bencDictAddDict( dict, KEY_SPEEDLIMIT, 4 );
|
|
|
|
tr_bencDictAddInt( d, KEY_SPEEDLIMIT_DOWN_SPEED,
|
|
|
|
tr_torrentGetSpeedLimit( tor, TR_DOWN ) );
|
|
|
|
tr_bencDictAddInt( d, KEY_SPEEDLIMIT_DOWN_MODE,
|
|
|
|
tr_torrentGetSpeedMode( tor, TR_DOWN ) );
|
|
|
|
tr_bencDictAddInt( d, KEY_SPEEDLIMIT_UP_SPEED,
|
|
|
|
tr_torrentGetSpeedLimit( tor, TR_UP ) );
|
|
|
|
tr_bencDictAddInt( d, KEY_SPEEDLIMIT_UP_MODE,
|
|
|
|
tr_torrentGetSpeedMode( tor, TR_UP ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
loadSpeedLimits( tr_benc * dict, tr_torrent * tor )
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
tr_benc * d;
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( dict, KEY_SPEEDLIMIT, &d ) )
|
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_DOWN_SPEED, &i ) )
|
|
|
|
tr_torrentSetSpeedLimit( tor, TR_DOWN, i );
|
|
|
|
if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_DOWN_MODE, &i ) )
|
|
|
|
tr_torrentSetSpeedMode( tor, TR_DOWN, i );
|
|
|
|
if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_UP_SPEED, &i ) )
|
|
|
|
tr_torrentSetSpeedLimit( tor, TR_UP, i );
|
|
|
|
if( tr_bencDictFindInt( d, KEY_SPEEDLIMIT_UP_MODE, &i ) )
|
|
|
|
tr_torrentSetSpeedMode( tor, TR_UP, i );
|
|
|
|
ret = TR_FR_SPEEDLIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
static void
|
|
|
|
saveProgress( tr_benc * dict, const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
time_t * mtimes;
|
|
|
|
tr_benc * p;
|
|
|
|
tr_benc * m;
|
|
|
|
const tr_bitfield * bitfield;
|
|
|
|
|
|
|
|
p = tr_bencDictAdd( dict, KEY_PROGRESS );
|
|
|
|
tr_bencInitDict( p, 2 );
|
|
|
|
|
|
|
|
/* add the mtimes */
|
2008-04-14 11:52:50 +00:00
|
|
|
mtimes = tr_torrentGetMTimes( tor, &n );
|
|
|
|
m = tr_bencDictAddList( p, KEY_PROGRESS_MTIMES, n );
|
2008-04-14 20:30:43 +00:00
|
|
|
for( i=0; i<n; ++i ) {
|
2008-04-15 13:27:28 +00:00
|
|
|
if( !tr_torrentIsFileChecked( tor, i ) )
|
2008-04-14 20:30:43 +00:00
|
|
|
mtimes[i] = ~(time_t)0; /* force a recheck */
|
|
|
|
tr_bencListAddInt( m, mtimes[i] );
|
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
|
|
|
|
/* add the bitfield */
|
|
|
|
bitfield = tr_cpBlockBitfield( tor->completion );
|
2008-04-18 23:17:40 +00:00
|
|
|
tr_bencDictAddRaw( p, KEY_PROGRESS_BITFIELD,
|
|
|
|
bitfield->bits, bitfield->len );
|
2008-04-13 22:31:07 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free( mtimes );
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
loadProgress( tr_benc * dict, tr_torrent * tor )
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
tr_benc * p;
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( dict, KEY_PROGRESS, &p ) )
|
|
|
|
{
|
|
|
|
tr_benc * m;
|
|
|
|
tr_benc * b;
|
|
|
|
int n;
|
2008-04-14 11:52:50 +00:00
|
|
|
time_t * curMTimes = tr_torrentGetMTimes( tor, &n );
|
2008-04-13 22:31:07 +00:00
|
|
|
|
|
|
|
if( tr_bencDictFindList( p, KEY_PROGRESS_MTIMES, &m )
|
|
|
|
&& ( m->val.l.count == (int64_t)tor->info.fileCount )
|
|
|
|
&& ( m->val.l.count == n ) )
|
|
|
|
{
|
|
|
|
int i;
|
2008-04-14 20:30:43 +00:00
|
|
|
for( i=0; i<n; ++i )
|
2008-04-13 22:31:07 +00:00
|
|
|
{
|
2008-04-14 20:30:43 +00:00
|
|
|
int64_t tmp;
|
|
|
|
if( !tr_bencGetInt( &m->val.l.vals[i], &tmp ) ) {
|
|
|
|
tr_tordbg( tor, "File #%d needs to be verified - couldn't find benc entry", i );
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_torrentSetFileChecked( tor, i, FALSE );
|
2008-04-14 20:30:43 +00:00
|
|
|
} else {
|
|
|
|
const time_t t = (time_t) tmp;
|
|
|
|
if( t == curMTimes[i] )
|
|
|
|
tr_torrentSetFileChecked( tor, i, TRUE );
|
|
|
|
else {
|
2008-04-15 13:27:28 +00:00
|
|
|
tr_tordbg( tor, "File #%d needs to be verified - times %lu and %lu don't match", i, t, curMTimes[i] );
|
2008-04-14 20:30:43 +00:00
|
|
|
tr_torrentSetFileChecked( tor, i, FALSE );
|
|
|
|
}
|
2008-04-13 22:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_torrentUncheck( tor );
|
|
|
|
tr_tordbg( tor, "Torrent needs to be verified - unable to find mtimes" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if(( b = tr_bencDictFindType( p, KEY_PROGRESS_BITFIELD, TYPE_STR )))
|
|
|
|
{
|
|
|
|
tr_bitfield tmp;
|
|
|
|
tmp.len = b->val.s.i;
|
|
|
|
tmp.bits = (uint8_t*) b->val.s.s;
|
|
|
|
if( tr_cpBlockBitfieldSet( tor->completion, &tmp ) ) {
|
|
|
|
tr_torrentUncheck( tor );
|
|
|
|
tr_tordbg( tor, "Torrent needs to be verified - error loading bitfield" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_torrentUncheck( tor );
|
|
|
|
tr_tordbg( tor, "Torrent needs to be verified - unable to find bitfield" );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( curMTimes );
|
|
|
|
ret = TR_FR_PROGRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
void
|
|
|
|
tr_torrentSaveResume( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
2008-04-14 11:52:50 +00:00
|
|
|
char filename[MAX_PATH_LENGTH];
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2008-04-18 12:47:13 +00:00
|
|
|
tr_bencInitDict( &top, 12 );
|
2008-04-14 14:39:13 +00:00
|
|
|
tr_bencDictAddInt( &top, KEY_CORRUPT,
|
|
|
|
tor->corruptPrev + tor->corruptCur );
|
|
|
|
tr_bencDictAddStr( &top, KEY_DESTINATION,
|
|
|
|
tor->destination );
|
2008-04-14 11:52:50 +00:00
|
|
|
tr_bencDictAddInt( &top, KEY_DOWNLOADED,
|
2008-04-14 14:39:13 +00:00
|
|
|
tor->downloadedPrev + tor->downloadedCur );
|
2008-04-14 11:52:50 +00:00
|
|
|
tr_bencDictAddInt( &top, KEY_UPLOADED,
|
|
|
|
tor->uploadedPrev + tor->uploadedCur );
|
2008-04-14 14:39:13 +00:00
|
|
|
tr_bencDictAddInt( &top, KEY_MAX_PEERS,
|
|
|
|
tor->maxConnectedPeers );
|
|
|
|
tr_bencDictAddInt( &top, KEY_PAUSED,
|
|
|
|
tor->isRunning ? 0 : 1 );
|
2008-04-13 22:31:07 +00:00
|
|
|
savePeers( &top, tor );
|
|
|
|
savePriorities( &top, tor );
|
2008-04-14 15:17:16 +00:00
|
|
|
saveDND( &top, tor );
|
2008-04-13 22:31:07 +00:00
|
|
|
saveProgress( &top, tor );
|
|
|
|
saveSpeedLimits( &top, tor );
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
getResumeFilename( filename, sizeof( filename ), tor );
|
|
|
|
tr_bencSaveFile( filename, &top );
|
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-04-18 12:47:13 +00:00
|
|
|
loadFromFile( tr_torrent * tor,
|
|
|
|
uint64_t fieldsToLoad )
|
2008-04-13 14:29:11 +00:00
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
int64_t i;
|
|
|
|
const char * str;
|
2008-04-13 14:29:11 +00:00
|
|
|
uint64_t fieldsLoaded = 0;
|
|
|
|
char filename[MAX_PATH_LENGTH];
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_benc top;
|
2008-04-13 14:29:11 +00:00
|
|
|
|
|
|
|
getResumeFilename( filename, sizeof( filename ), tor );
|
2008-04-13 22:31:07 +00:00
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( tr_bencLoadFile( filename, &top ) )
|
2008-04-14 03:12:24 +00:00
|
|
|
{
|
2008-04-14 11:52:50 +00:00
|
|
|
tr_tordbg( tor, "Couldn't read \"%s\"; trying old format.", filename );
|
2008-04-18 12:47:13 +00:00
|
|
|
fieldsLoaded = tr_fastResumeLoad( tor, fieldsToLoad );
|
2008-04-14 03:12:24 +00:00
|
|
|
|
|
|
|
if( ( fieldsLoaded != 0 ) && ( fieldsToLoad == ~(uint64_t)0 ) )
|
|
|
|
{
|
|
|
|
tr_torrentSaveResume( tor );
|
|
|
|
tr_fastResumeRemove( tor );
|
|
|
|
tr_tordbg( tor, "Migrated resume file to \"%s\"", filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 )
|
|
|
|
&& 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-04-14 20:41:45 +00:00
|
|
|
if( ( fieldsToLoad & ( TR_FR_PROGRESS | TR_FR_DESTINATION ) )
|
2008-04-14 11:52:50 +00:00
|
|
|
&& tr_bencDictFindStr( &top, KEY_DESTINATION, &str ) ) {
|
2008-04-13 22:31:07 +00:00
|
|
|
tr_free( tor->destination );
|
|
|
|
tor->destination = tr_strdup( str );
|
|
|
|
fieldsLoaded |= TR_FR_DESTINATION;
|
|
|
|
}
|
2008-04-13 14:29:11 +00:00
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( ( fieldsToLoad & TR_FR_DOWNLOADED )
|
|
|
|
&& 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 )
|
|
|
|
&& 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 )
|
|
|
|
&& 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 )
|
|
|
|
&& tr_bencDictFindInt( &top, KEY_PAUSED, &i ) ) {
|
2008-04-13 22:31:07 +00:00
|
|
|
tor->isRunning = i ? 0 : 1;
|
|
|
|
fieldsLoaded |= TR_FR_RUN;
|
|
|
|
}
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
if( fieldsToLoad & TR_FR_PEERS )
|
|
|
|
fieldsLoaded |= loadPeers( &top, tor );
|
|
|
|
|
|
|
|
if( fieldsToLoad & TR_FR_PRIORITY )
|
|
|
|
fieldsLoaded |= loadPriorities( &top, tor );
|
|
|
|
|
|
|
|
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 );
|
2008-04-13 22:31:07 +00:00
|
|
|
|
|
|
|
tr_bencFree( &top );
|
|
|
|
return fieldsLoaded;
|
2008-04-13 14:29:11 +00:00
|
|
|
}
|
|
|
|
|
2008-04-18 12:47:13 +00:00
|
|
|
static uint64_t
|
|
|
|
setFromCtor( tr_torrent * tor, uint64_t fields, const tr_ctor * ctor, int mode )
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
|
|
|
|
if( fields & TR_FR_RUN ) {
|
|
|
|
uint8_t isPaused;
|
|
|
|
if( !tr_ctorGetPaused( ctor, mode, &isPaused ) ) {
|
|
|
|
tor->isRunning = !isPaused;
|
|
|
|
ret |= TR_FR_RUN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if( fields & TR_FR_DESTINATION ) {
|
|
|
|
const char * destination;
|
|
|
|
if( !tr_ctorGetDestination( ctor, mode, &destination ) ) {
|
|
|
|
ret |= TR_FR_DESTINATION;
|
|
|
|
tr_free( tor->destination );
|
|
|
|
tor->destination = tr_strdup( destination );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
useManditoryFields( tr_torrent * tor, uint64_t fields, const tr_ctor * ctor )
|
|
|
|
{
|
|
|
|
return setFromCtor( tor, fields, ctor, TR_FORCE );
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
useFallbackFields( tr_torrent * tor, uint64_t fields, const tr_ctor * ctor )
|
|
|
|
{
|
|
|
|
return setFromCtor( tor, fields, ctor, TR_FALLBACK );
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
tr_torrentLoadResume( tr_torrent * tor,
|
|
|
|
uint64_t fieldsToLoad,
|
|
|
|
const tr_ctor * ctor )
|
|
|
|
{
|
|
|
|
uint64_t ret = 0;
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
char filename[MAX_PATH_LENGTH];
|
|
|
|
getResumeFilename( filename, sizeof( filename ), tor );
|
|
|
|
unlink( filename );
|
|
|
|
tr_fastResumeRemove( tor );
|
|
|
|
}
|