From f8a98a8c35e8a81cbb6c498bd2beb6e36fe682b9 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 27 Feb 2008 20:06:53 +0000 Subject: [PATCH] eliminate duplicate code between readfile() and tr_loadFile(). shrink down the daemon torrent code a bit. --- daemon/misc.c | 50 ++------------------------- daemon/misc.h | 13 ++----- daemon/torrents.c | 88 +++++++++++++++++++---------------------------- 3 files changed, 41 insertions(+), 110 deletions(-) diff --git a/daemon/misc.c b/daemon/misc.c index 03372086f..d4a14dc58 100644 --- a/daemon/misc.c +++ b/daemon/misc.c @@ -34,6 +34,7 @@ #include #include +#include /* tr_loadFile() */ #include "errors.h" #include "misc.h" @@ -154,54 +155,9 @@ writefile( const char * name, uint8_t * buf, ssize_t len ) } uint8_t * -readfile( const char * name, size_t * len ) +readfile( const char * filename, size_t * len ) { - struct stat sb; - int fd; - uint8_t * buf; - ssize_t res; - - fd = open( name, O_RDONLY ); - if( 0 > fd ) - { - if( ENOENT != errno ) - { - errnomsg( "failed to open %s for reading", name ); - } - return NULL; - } - - if( 0 > fstat( fd, &sb ) ) - { - errnomsg( "failed to stat %s", name ); - return NULL; - } - - buf = malloc( sb.st_size ); - if( NULL == buf ) - { - mallocmsg( sb.st_size ); - return NULL; - } - - res = read( fd, buf, sb.st_size ); - if( 0 > res ) - { - errnomsg( "failed to read from %s", name ); - free( buf ); - return NULL; - } - if( res < sb.st_size ) - { - errmsg( "failed to read all data from %s", name ); - free( buf ); - return NULL; - } - - close( fd ); - *len = res; - - return buf; + return tr_loadFile( filename, len ); } #ifndef HAVE_DAEMON diff --git a/daemon/misc.h b/daemon/misc.h index 2a2a92cdb..9833c77e6 100644 --- a/daemon/misc.h +++ b/daemon/misc.h @@ -107,21 +107,14 @@ struct bufferevent; while( 0 ) #define INTCMP_FUNC( name, type, id ) \ -int \ +static int \ name( struct type * _icf_first, struct type * _icf_second ) \ { \ if( _icf_first->id < _icf_second->id ) \ - { \ return -1; \ - } \ - else if( _icf_first->id > _icf_second->id ) \ - { \ + if( _icf_first->id > _icf_second->id ) \ return 1; \ - } \ - else \ - { \ - return 0; \ - } \ + return 0; \ } struct stritem diff --git a/daemon/torrents.c b/daemon/torrents.c index cc80d2b49..8fe71ab2c 100644 --- a/daemon/torrents.c +++ b/daemon/torrents.c @@ -71,10 +71,6 @@ static void starttimer ( int ); static void timerfunc ( int, short, void * ); static int loadstate ( void ); static int savestate ( void ); -static int toridcmp ( struct tor *, struct tor * ); -static int torhashcmp ( struct tor *, struct tor * ); -static struct tor * hashlookup ( const uint8_t * ); -static struct tor * iterate ( struct tor * ); static struct event_base * gl_base = NULL; static tr_handle * gl_handle = NULL; @@ -96,9 +92,16 @@ static int gl_downlimit = -1; static char gl_dir[MAXPATHLEN]; static tr_encryption_mode gl_crypto = TR_ENCRYPTION_PREFERRED; -RB_GENERATE_STATIC( tortree, tor, idlinks, toridcmp ) +static int +torhashcmp( struct tor * left, struct tor * right ) +{ + return memcmp( left->hash, right->hash, sizeof left->hash ); +} + RB_GENERATE_STATIC( hashtree, tor, hashlinks, torhashcmp ) + INTCMP_FUNC( toridcmp, tor, id ) +RB_GENERATE_STATIC( tortree, tor, idlinks, toridcmp ) void torrent_init( struct event_base * base ) @@ -241,6 +244,18 @@ torrent_stat( int id ) return tor ? tr_torrentStat( tor->tor ) : NULL; } +static struct tor * +hashlookup( const uint8_t * hash ) +{ + struct tor key, * found; + + memset( &key, 0, sizeof key ); + memcpy( key.hash, hash, sizeof key.hash ); + found = RB_FIND( hashtree, &gl_hashes, &key ); + + return found; +} + int torrent_lookup( const uint8_t * hashstr ) { @@ -272,22 +287,25 @@ torrent_lookup( const uint8_t * hashstr ) return tor->id; } -void * -torrent_iter( void * iter, int * id ) +static struct tor * +iterate( struct tor * tor ) { - struct tor * tor = iter; + struct tor * next = NULL; - assert( NULL != gl_handle ); - assert( !gl_exiting ); + if( gl_handle && !gl_exiting ) + next = tor ? RB_NEXT( tortree, &gl_tree, tor ) + : RB_MIN( tortree, &gl_tree ); - tor = iterate( tor ); + return next; +} - if( NULL != tor ) - { - *id = tor->id; - } - - return tor; +void * +torrent_iter( void * cur, int * id ) +{ + struct tor * next = iterate( cur ); + if( next ) + *id = next->id; + return next; } void @@ -848,39 +866,3 @@ savestate( void ) return 0; } - -int -torhashcmp( struct tor * left, struct tor * right ) -{ - return memcmp( left->hash, right->hash, sizeof left->hash ); -} - -struct tor * -hashlookup( const uint8_t * hash ) -{ - struct tor key, * found; - - memset( &key, 0, sizeof key ); - memcpy( key.hash, hash, sizeof key.hash ); - found = RB_FIND( hashtree, &gl_hashes, &key ); - - return found; -} - -struct tor * -iterate( struct tor * tor ) -{ - assert( NULL != gl_handle ); - assert( !gl_exiting ); - - if( NULL == tor ) - { - tor = RB_MIN( tortree, &gl_tree ); - } - else - { - tor = RB_NEXT( tortree, &gl_tree, tor ); - } - - return tor; -}