diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 46e3677e3..d52acf747 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -1025,8 +1025,10 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt ) tr_torrentSetActivityDate( tor, now ); - if( e->wasPieceData ) + if( e->wasPieceData ) { tor->uploadedCur += e->length; + tr_torrentSetDirty( tor ); + } /* update the stats */ if( e->wasPieceData ) @@ -1064,8 +1066,10 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt ) * content distributor, not the peers, it is the tracker's job * to manage the swarms, not the web server and does not fit * into the jurisdiction of the tracker." */ - if( peer && e->wasPieceData ) + if( peer && e->wasPieceData ) { tor->downloadedCur += e->length; + tr_torrentSetDirty( tor ); + } /* update the stats */ if( e->wasPieceData ) @@ -1102,6 +1106,7 @@ peerCallbackFunc( void * vpeer, void * vevent, void * vt ) tr_block_index_t block = _tr_block( tor, e->pieceIndex, e->offset ); tr_cpBlockAdd( &tor->completion, block ); + tr_torrentSetDirty( tor ); decrementPieceRequests( t, e->pieceIndex ); broadcastGotBlock( t, e->pieceIndex, e->offset, e->length ); diff --git a/libtransmission/resume.c b/libtransmission/resume.c index 7db9b4bb7..e59aa43c2 100644 --- a/libtransmission/resume.c +++ b/libtransmission/resume.c @@ -76,15 +76,14 @@ static void savePeers( tr_benc * dict, const tr_torrent * tor ) { - tr_pex * pex = NULL; - int count = tr_peerMgrGetPeers( (tr_torrent*) tor, &pex, TR_AF_INET ); + int count; + tr_pex * pex; + count = tr_peerMgrGetPeers( (tr_torrent*) tor, &pex, TR_AF_INET ); if( count > 0 ) tr_bencDictAddRaw( dict, KEY_PEERS, pex, sizeof( tr_pex ) * count ); - tr_free( pex ); - pex = NULL; - + count = tr_peerMgrGetPeers( (tr_torrent*) tor, &pex, TR_AF_INET6 ); if( count > 0 ) tr_bencDictAddRaw( dict, KEY_PEERS6, pex, sizeof( tr_pex ) * count ); @@ -481,9 +480,11 @@ tr_torrentSaveResume( const tr_torrent * tor ) tr_benc top; char * filename; - if( !tor ) + if( !tr_isTorrent( tor ) ) return; + tr_tordbg( tor, "Saving .resume file for \"%s\"", tor->info.name ); + tr_bencInitDict( &top, 32 ); /* arbitrary "big enough" number */ tr_bencDictAddInt( &top, KEY_ACTIVITY_DATE, tor->activityDate ); diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index a5a5b490b..72c0039ad 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -116,6 +116,8 @@ tr_torrentSetSpeedLimit( tr_torrent * tor, tr_direction dir, int KiB_sec ) assert( tr_isDirection( dir ) ); tr_bandwidthSetDesiredSpeed( tor->bandwidth, dir, KiB_sec ); + + tr_torrentSetDirty( tor ); } int @@ -134,6 +136,8 @@ tr_torrentUseSpeedLimit( tr_torrent * tor, tr_direction dir, tr_bool do_use ) assert( tr_isDirection( dir ) ); tr_bandwidthSetLimited( tor->bandwidth, dir, do_use ); + + tr_torrentSetDirty( tor ); } tr_bool @@ -152,6 +156,8 @@ tr_torrentUseSessionLimits( tr_torrent * tor, tr_bool doUse ) tr_bandwidthHonorParentLimits( tor->bandwidth, TR_UP, doUse ); tr_bandwidthHonorParentLimits( tor->bandwidth, TR_DOWN, doUse ); + + tr_torrentSetDirty( tor ); } tr_bool @@ -174,6 +180,8 @@ tr_torrentSetRatioMode( tr_torrent * tor, tr_ratiolimit mode ) tor->ratioLimitMode = mode; tor->needsSeedRatioCheck = TRUE; + + tr_torrentSetDirty( tor ); } tr_ratiolimit @@ -192,6 +200,8 @@ tr_torrentSetRatioLimit( tr_torrent * tor, double desiredRatio ) tor->desiredRatio = desiredRatio; tor->needsSeedRatioCheck = TRUE; + + tr_torrentSetDirty( tor ); } double @@ -741,7 +751,7 @@ tr_torrentSetDownloadDir( tr_torrent * tor, const char * path ) { tr_free( tor->downloadDir ); tor->downloadDir = tr_strdup( path ); - tr_torrentSaveResume( tor ); + tr_torrentSetDirty( tor ); } } @@ -1159,6 +1169,8 @@ tr_torrentResetTransferStats( tr_torrent * tor ) tor->corruptPrev += tor->corruptCur; tor->corruptCur = 0; + tr_torrentSetDirty( tor ); + tr_torrentUnlock( tor ); } @@ -1246,10 +1258,9 @@ checkAndStartImpl( void * vtor ) tor->needsSeedRatioCheck = TRUE; tor->error = TR_STAT_OK; tor->errorString[0] = '\0'; - tr_torrentResetTransferStats( tor ); tor->completeness = tr_cpGetStatus( &tor->completion ); - tr_torrentSaveResume( tor ); tor->startDate = tor->anyDate = now; + tr_torrentResetTransferStats( tor ); tr_trackerStart( tor->tracker ); tor->dhtAnnounceAt = now + tr_cryptoWeakRandInt( 20 ); tr_peerMgrStartTorrent( tor ); @@ -1351,6 +1362,12 @@ stopTorrent( void * vtor ) tr_trackerStop( tor->tracker ); tr_fdTorrentClose( tor->uniqueId ); + + if( tor->isDirty ) { + tor->isDirty = 0; + if( !tor->isDeleting ) + tr_torrentSaveResume( tor ); + } } void @@ -1363,8 +1380,6 @@ tr_torrentStop( tr_torrent * tor ) tr_globalLock( tor->session ); tor->isRunning = 0; - if( !tor->isDeleting ) - tr_torrentSaveResume( tor ); tr_runInEventThread( tor->session, stopTorrent, tor ); tr_globalUnlock( tor->session ); @@ -1383,7 +1398,6 @@ closeTorrent( void * vtor ) tr_bencDictAddInt( d, "id", tor->uniqueId ); tr_bencDictAddInt( d, "date", time( NULL ) ); - tr_torrentSaveResume( tor ); tor->isRunning = 0; stopTorrent( tor ); if( tor->isDeleting ) @@ -1525,7 +1539,7 @@ tr_torrentRecheckCompleteness( tr_torrent * tor ) tor->doneDate = tor->anyDate = time( NULL ); } - tr_torrentSaveResume( tor ); + tr_torrentSetDirty( tor ); } tr_torrentUnlock( tor ); @@ -1568,7 +1582,7 @@ tr_torrentSetFilePriorities( tr_torrent * tor, for( i = 0; i < fileCount; ++i ) tr_torrentInitFilePriority( tor, files[i], priority ); - tr_torrentSaveResume( tor ); + tr_torrentSetDirty( tor ); tr_torrentUnlock( tor ); } @@ -1712,7 +1726,7 @@ tr_torrentSetFileDLs( tr_torrent * tor, tr_torrentLock( tor ); tr_torrentInitFileDLs( tor, files, fileCount, doDownload ); - tr_torrentSaveResume( tor ); + tr_torrentSetDirty( tor ); tr_torrentUnlock( tor ); } @@ -1735,6 +1749,8 @@ tr_torrentSetPriority( tr_torrent * tor, tr_priority_t priority ) assert( tr_isPriority( priority ) ); tor->bandwidth->priority = priority; + + tr_torrentSetDirty( tor ); } /*** @@ -2003,6 +2019,7 @@ tr_torrentSetActivityDate( tr_torrent * tor, tor->activityDate = t; tor->anyDate = MAX( tor->anyDate, tor->activityDate ); + tr_torrentSetDirty( tor ); } void diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 8d75a385d..aaef8df65 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -204,6 +204,7 @@ struct tr_torrent tr_bool isDeleting; tr_bool needsSeedRatioCheck; tr_bool startAfterVerify; + tr_bool isDirty; uint16_t maxConnectedPeers; @@ -325,4 +326,13 @@ static inline tr_bool tr_isTorrent( const tr_torrent * tor ) && ( tr_isSession( tor->session ) ); } +/* set a flag indicating that the torrent's .resume file + * needs to be saved when the torrent is closed */ +static inline void tr_torrentSetDirty( tr_torrent * tor ) +{ + assert( tr_isTorrent( tor ) ); + + tor->isDirty = TRUE; +} + #endif diff --git a/libtransmission/tracker.c b/libtransmission/tracker.c index ceacf8d41..3d04acc54 100644 --- a/libtransmission/tracker.c +++ b/libtransmission/tracker.c @@ -553,10 +553,6 @@ onTrackerResponse( tr_session * session, t->reannounceAt = now + interval; t->manualAnnounceAllowedAt = now + t->announceMinIntervalSec; - - /* #319: save the .resume file after an announce so that, in case - * of a crash, our stats still match up with the tracker's stats */ - tr_torrentSaveResume( tr_torrentFindFromHash( t->session, t->hash ) ); } else if( 300 <= responseCode && responseCode <= 399 ) { diff --git a/libtransmission/verify.c b/libtransmission/verify.c index 0396d5b8d..496850733 100644 --- a/libtransmission/verify.c +++ b/libtransmission/verify.c @@ -23,7 +23,6 @@ #include "transmission.h" #include "completion.h" #include "fdlimit.h" -#include "resume.h" /* tr_torrentSaveResume() */ #include "inout.h" #include "list.h" #include "platform.h" @@ -219,7 +218,7 @@ verifyThreadFunc( void * unused UNUSED ) if( !stopCurrent ) { if( changed ) - tr_torrentSaveResume( tor ); + tr_torrentSetDirty( tor ); fireCheckDone( tor, currentNode.verify_done_cb ); } }