From 2d360434ebb15258571265bfa000017a8c1bd8c5 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 18 Apr 2008 12:47:13 +0000 Subject: [PATCH] handle manditory/fallback ctor settings in the bencoded resume filfes --- libtransmission/fastresume.c | 54 ++--------------------------- libtransmission/fastresume.h | 3 +- libtransmission/resume.c | 66 +++++++++++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 59 deletions(-) diff --git a/libtransmission/fastresume.c b/libtransmission/fastresume.c index cb12dd33f..446757f02 100644 --- a/libtransmission/fastresume.c +++ b/libtransmission/fastresume.c @@ -661,61 +661,11 @@ fastResumeLoadImpl ( tr_torrent * tor, return ret; } -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 ) - if( !tr_ctorGetMaxConnectedPeers( ctor, mode, &tor->maxConnectedPeers ) ) - 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_fastResumeLoad( tr_torrent * tor, - uint64_t fieldsToLoad, - const tr_ctor * ctor ) + uint64_t fieldsToLoad ) { - uint64_t ret = 0; - - ret |= useManditoryFields( tor, fieldsToLoad, ctor ); - fieldsToLoad &= ~ret; - ret |= fastResumeLoadImpl( tor, fieldsToLoad ); - fieldsToLoad &= ~ret; - ret |= useFallbackFields( tor, fieldsToLoad, ctor ); - - return ret; + return fastResumeLoadImpl( tor, fieldsToLoad ); } void diff --git a/libtransmission/fastresume.h b/libtransmission/fastresume.h index ce2c1e31b..965280c85 100644 --- a/libtransmission/fastresume.h +++ b/libtransmission/fastresume.h @@ -29,8 +29,7 @@ * Returns a bitwise-or'ed set of the data loaded from fastresume */ uint64_t tr_fastResumeLoad( tr_torrent * tor, - uint64_t fieldsToLoad, - const tr_ctor * ctor ); + uint64_t fieldsToLoad ); void tr_fastResumeRemove( const tr_torrent * tor ); diff --git a/libtransmission/resume.c b/libtransmission/resume.c index 492b7cc67..70f40a202 100644 --- a/libtransmission/resume.c +++ b/libtransmission/resume.c @@ -347,7 +347,7 @@ tr_torrentSaveResume( const tr_torrent * tor ) tr_benc top; char filename[MAX_PATH_LENGTH]; - tr_bencInitDict( &top, 10 ); + tr_bencInitDict( &top, 12 ); tr_bencDictAddInt( &top, KEY_CORRUPT, tor->corruptPrev + tor->corruptCur ); tr_bencDictAddStr( &top, KEY_DESTINATION, @@ -373,9 +373,8 @@ tr_torrentSaveResume( const tr_torrent * tor ) } uint64_t -tr_torrentLoadResume( tr_torrent * tor, - uint64_t fieldsToLoad, - const tr_ctor * ctor ) +loadFromFile( tr_torrent * tor, + uint64_t fieldsToLoad ) { int64_t i; const char * str; @@ -388,7 +387,7 @@ tr_torrentLoadResume( tr_torrent * tor, if( tr_bencLoadFile( filename, &top ) ) { tr_tordbg( tor, "Couldn't read \"%s\"; trying old format.", filename ); - fieldsLoaded = tr_fastResumeLoad( tor, fieldsToLoad, ctor ); + fieldsLoaded = tr_fastResumeLoad( tor, fieldsToLoad ); if( ( fieldsLoaded != 0 ) && ( fieldsToLoad == ~(uint64_t)0 ) ) { @@ -458,6 +457,63 @@ tr_torrentLoadResume( tr_torrent * tor, return fieldsLoaded; } +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 ) + if( !tr_ctorGetMaxConnectedPeers( ctor, mode, &tor->maxConnectedPeers ) ) + 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; +} + void tr_torrentRemoveResume( const tr_torrent * tor ) {