(trunk libT) fix #2162: .resume file doesn't get saved often enough when its contents change

This commit is contained in:
Charles Kerr 2009-08-13 14:47:56 +00:00
parent 96c6012790
commit 784464450a
4 changed files with 56 additions and 5 deletions

View File

@ -47,6 +47,12 @@
#include "version.h"
#include "web.h"
enum
{
SAVE_INTERVAL_SECS = 120
};
#define dbgmsg( ... ) \
do { \
if( tr_deepLoggingIsActive( ) ) \
@ -530,6 +536,31 @@ tr_sessionSaveSettings( tr_session * session,
tr_bencFree( &settings );
}
/***
****
***/
/**
* Periodically save the .resume files of any torrents whose
* status has recently changed. This prevents loss of metadata
* in the case of a crash, unclean shutdown, clumsy user, etc.
*/
static void
onSaveTimer( int foo UNUSED, short bar UNUSED, void * vsession )
{
tr_torrent * tor = NULL;
tr_session * session = vsession;
while(( tor = tr_torrentNext( session, tor )))
tr_torrentSave( tor );
tr_timerAdd( session->saveTimer, SAVE_INTERVAL_SECS, 0 );
}
/***
****
***/
static void tr_sessionInitImpl( void * );
static void onAltTimer( int, short, void* );
static void setAltTimer( tr_session * session );
@ -834,6 +865,10 @@ tr_sessionInitImpl( void * vdata )
evtimer_set( session->altTimer, onAltTimer, session );
setAltTimer( session );
session->saveTimer = tr_new0( struct event, 1 );
evtimer_set( session->saveTimer, onSaveTimer, session );
tr_timerAdd( session->saveTimer, SAVE_INTERVAL_SECS, 0 );
/* first %s is the application name
second %s is the version number */
tr_inf( _( "%s %s started" ), TR_NAME, LONG_VERSION_STRING );
@ -1423,6 +1458,10 @@ sessionCloseImpl( void * vsession )
if( session->isDHTEnabled )
tr_dhtUninit( session );
evtimer_del( session->saveTimer );
tr_free( session->saveTimer );
session->saveTimer = NULL;
evtimer_del( session->altTimer );
tr_free( session->altTimer );
session->altTimer = NULL;

View File

@ -124,6 +124,7 @@ struct tr_session
tr_benc * metainfoLookup;
struct event * altTimer;
struct event * saveTimer;
/* the size of the output buffer for peer connections */
int so_sndbuf;

View File

@ -1354,6 +1354,17 @@ tr_torrentVerify( tr_torrent * tor )
tr_runInEventThread( tor->session, verifyTorrent, tor );
}
void
tr_torrentSave( tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
if( tor->isDirty ) {
tor->isDirty = FALSE;
tr_torrentSaveResume( tor );
}
}
static void
stopTorrent( void * vtor )
{
@ -1367,11 +1378,8 @@ stopTorrent( void * vtor )
tr_fdTorrentClose( tor->uniqueId );
if( tor->isDirty ) {
tor->isDirty = 0;
if( !tor->isDeleting )
tr_torrentSaveResume( tor );
}
if( !tor->isDeleting )
tr_torrentSave( tor );
}
void

View File

@ -118,6 +118,9 @@ tr_torrent* tr_torrentNext( tr_session * session,
void tr_torrentCheckSeedRatio( tr_torrent * tor );
/** save a torrent's .resume file if it's changed since the last time it was saved */
void tr_torrentSave( tr_torrent * tor );
typedef enum