eliminate duplicate code between readfile() and tr_loadFile(). shrink down the daemon torrent code a bit.

This commit is contained in:
Charles Kerr 2008-02-27 20:06:53 +00:00
parent 4f11506522
commit f8a98a8c35
3 changed files with 41 additions and 110 deletions

View File

@ -34,6 +34,7 @@
#include <libtransmission/transmission.h>
#include <libtransmission/trcompat.h>
#include <libtransmission/utils.h> /* 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

View File

@ -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

View File

@ -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;
}