From 9f347f8df1fc090d8ac78f31f9af382df923fb86 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 1 Dec 2009 00:07:18 +0000 Subject: [PATCH] (trunk) #2119 "daemon should reload settings.json on SIGHUP" -- reload the blocklists, too. Suggested by nano- in irc. --- daemon/daemon.c | 1 + libtransmission/session.c | 185 ++++++++++++++++++--------------- libtransmission/transmission.h | 3 + 3 files changed, 105 insertions(+), 84 deletions(-) diff --git a/daemon/daemon.c b/daemon/daemon.c index b1ef33e81..74fc847b8 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -115,6 +115,7 @@ gotsig( int sig ) tr_sessionLoadSettings( &settings, configDir, MY_NAME ); tr_sessionSet( mySession, &settings ); tr_bencFree( &settings ); + tr_sessionReloadBlocklists( mySession ); break; } diff --git a/libtransmission/session.c b/libtransmission/session.c index b69fff6c8..e456dabe5 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -225,88 +225,6 @@ tr_sessionGetPublicAddress( const tr_session * session, int tr_af_type ) **** ***/ -static int -tr_stringEndsWith( const char * str, const char * end ) -{ - const size_t slen = strlen( str ); - const size_t elen = strlen( end ); - - return slen >= elen && !memcmp( &str[slen - elen], end, elen ); -} - -static void -loadBlocklists( tr_session * session ) -{ - int binCount = 0; - int newCount = 0; - struct stat sb; - char * dirname; - DIR * odir = NULL; - tr_list * list = NULL; - const tr_bool isEnabled = session->isBlocklistEnabled; - - /* walk through the directory and find blocklists */ - dirname = tr_buildPath( session->configDir, "blocklists", NULL ); - if( !stat( dirname, - &sb ) && S_ISDIR( sb.st_mode ) - && ( ( odir = opendir( dirname ) ) ) ) - { - struct dirent *d; - for( d = readdir( odir ); d; d = readdir( odir ) ) - { - char * filename; - - if( !d->d_name || d->d_name[0] == '.' ) /* skip dotfiles, ., and .. - */ - continue; - - filename = tr_buildPath( dirname, d->d_name, NULL ); - - if( tr_stringEndsWith( filename, ".bin" ) ) - { - /* if we don't already have this blocklist, add it */ - if( !tr_list_find( list, filename, - (TrListCompareFunc)strcmp ) ) - { - tr_list_append( &list, - _tr_blocklistNew( filename, isEnabled ) ); - ++binCount; - } - } - else - { - /* strip out the file suffix, if there is one, and add ".bin" - instead */ - tr_blocklist * b; - const char * dot = strrchr( d->d_name, '.' ); - const int len = dot ? dot - d->d_name - : (int)strlen( d->d_name ); - char * tmp = tr_strdup_printf( - "%s" TR_PATH_DELIMITER_STR "%*.*s.bin", - dirname, len, len, d->d_name ); - b = _tr_blocklistNew( tmp, isEnabled ); - _tr_blocklistSetContent( b, filename ); - tr_list_append( &list, b ); - ++newCount; - tr_free( tmp ); - } - - tr_free( filename ); - } - - closedir( odir ); - } - - session->blocklists = list; - - if( binCount ) - tr_dbg( "Found %d blocklists in \"%s\"", binCount, dirname ); - if( newCount ) - tr_dbg( "Found %d new blocklists in \"%s\"", newCount, dirname ); - - tr_free( dirname ); -} - static tr_bool isAltTime( const tr_session * s ) { @@ -652,6 +570,8 @@ onNowTimer( int foo UNUSED, short bar UNUSED, void * vsession ) /* fprintf( stderr, "time %zu sec, %zu microsec\n", (size_t)tr_time(), (size_t)tv.tv_usec ); */ } +static void loadBlocklists( tr_session * session ); + static void tr_sessionInitImpl( void * vdata ) { @@ -1525,6 +1445,8 @@ compareTorrentByCur( const void * va, const void * vb ) return 0; } +static void closeBlocklists( tr_session * ); + static void sessionCloseImpl( void * vsession ) { @@ -1573,8 +1495,7 @@ sessionCloseImpl( void * vsession ) tr_statsClose( session ); tr_peerMgrFree( session->peerMgr ); - tr_list_free( &session->blocklists, - (TrListForeachFunc)_tr_blocklistFree ); + closeBlocklists( session ); tr_webClose( &session->web ); session->isClosed = TRUE; @@ -1817,6 +1738,102 @@ tr_sessionIsPortForwardingEnabled( const tr_session * session ) **** ***/ +static int +tr_stringEndsWith( const char * str, const char * end ) +{ + const size_t slen = strlen( str ); + const size_t elen = strlen( end ); + + return slen >= elen && !memcmp( &str[slen - elen], end, elen ); +} + +static void +loadBlocklists( tr_session * session ) +{ + int binCount = 0; + int newCount = 0; + struct stat sb; + char * dirname; + DIR * odir = NULL; + tr_list * list = NULL; + const tr_bool isEnabled = session->isBlocklistEnabled; + + /* walk through the directory and find blocklists */ + dirname = tr_buildPath( session->configDir, "blocklists", NULL ); + if( !stat( dirname, + &sb ) && S_ISDIR( sb.st_mode ) + && ( ( odir = opendir( dirname ) ) ) ) + { + struct dirent *d; + for( d = readdir( odir ); d; d = readdir( odir ) ) + { + char * filename; + + if( !d->d_name || d->d_name[0] == '.' ) /* skip dotfiles, ., and .. + */ + continue; + + filename = tr_buildPath( dirname, d->d_name, NULL ); + + if( tr_stringEndsWith( filename, ".bin" ) ) + { + /* if we don't already have this blocklist, add it */ + if( !tr_list_find( list, filename, + (TrListCompareFunc)strcmp ) ) + { + tr_list_append( &list, + _tr_blocklistNew( filename, isEnabled ) ); + ++binCount; + } + } + else + { + /* strip out the file suffix, if there is one, and add ".bin" + instead */ + tr_blocklist * b; + const char * dot = strrchr( d->d_name, '.' ); + const int len = dot ? dot - d->d_name + : (int)strlen( d->d_name ); + char * tmp = tr_strdup_printf( + "%s" TR_PATH_DELIMITER_STR "%*.*s.bin", + dirname, len, len, d->d_name ); + b = _tr_blocklistNew( tmp, isEnabled ); + _tr_blocklistSetContent( b, filename ); + tr_list_append( &list, b ); + ++newCount; + tr_free( tmp ); + } + + tr_free( filename ); + } + + closedir( odir ); + } + + session->blocklists = list; + + if( binCount ) + tr_dbg( "Found %d blocklists in \"%s\"", binCount, dirname ); + if( newCount ) + tr_dbg( "Found %d new blocklists in \"%s\"", newCount, dirname ); + + tr_free( dirname ); +} + +static void +closeBlocklists( tr_session * session ) +{ + tr_list_free( &session->blocklists, + (TrListForeachFunc)_tr_blocklistFree ); +} + +void +tr_sessionReloadBlocklists( tr_session * session ) +{ + closeBlocklists( session ); + loadBlocklists( session ); +} + int tr_blocklistGetRuleCount( const tr_session * session ) { diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index ed4115da5..9909cb6a0 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -306,6 +306,9 @@ tr_session * tr_sessionInit( const char * tag, void tr_sessionSet( tr_session * session, struct tr_benc * settings ); +void tr_sessionReloadBlocklists( tr_session * session ); + + /** @brief End a libtransmission session @see tr_sessionInit() */ void tr_sessionClose( tr_session * );