From b483b30889024eedc25196ced6fc2b611dc4a565 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 18 Apr 2008 23:17:40 +0000 Subject: [PATCH] minor code cleanup: remove dead code, const-correctness, etc --- libtransmission/inout.c | 49 +++++++++++++++++++++----------------- libtransmission/inout.h | 20 ++++++++-------- libtransmission/platform.c | 26 ++------------------ libtransmission/platform.h | 1 - libtransmission/resume.c | 7 +++--- 5 files changed, 42 insertions(+), 61 deletions(-) diff --git a/libtransmission/inout.c b/libtransmission/inout.c index ab6acb7b4..749dbc8ad 100644 --- a/libtransmission/inout.c +++ b/libtransmission/inout.c @@ -68,7 +68,9 @@ readOrWriteBytes( const tr_torrent * tor, if ((ioMode==TR_IO_READ) && !fileExists ) /* does file exist? */ err = tr_ioErrorFromErrno( errno ); - else if ((fd = tr_fdFileCheckout ( tor->destination, file->name, ioMode==TR_IO_WRITE )) < 0) + else if ((fd = tr_fdFileCheckout ( tor->destination, + file->name, + ioMode==TR_IO_WRITE )) < 0) err = fd; else if( lseek( fd, (off_t)fileOffset, SEEK_SET ) == ((off_t)-1) ) err = tr_ioErrorFromErrno( errno ); @@ -86,7 +88,7 @@ readOrWriteBytes( const tr_torrent * tor, return err; } -static tr_errno +static void findFileLocation( const tr_torrent * tor, tr_piece_index_t pieceIndex, uint32_t pieceOffset, @@ -109,7 +111,8 @@ findFileLocation( const tr_torrent * tor, while( len > 0 ) { size_t half = len / 2; size_t middle = first + half; - if( inf->files[middle].offset + inf->files[middle].length < offset ) { + if( inf->files[middle].offset + inf->files[middle].length < offset ) + { first = middle; ++first; len = len - half - 1; @@ -124,7 +127,6 @@ findFileLocation( const tr_torrent * tor, assert( offset < inf->files[first].offset + inf->files[first].length ); assert( *fileIndex < inf->fileCount ); assert( *fileOffset < inf->files[first].length ); - return 0; } #ifdef WIN32 @@ -161,23 +163,25 @@ ensureMinimumFileSize( const tr_torrent * tor, #endif static tr_errno -readOrWritePiece( tr_torrent * tor, - int ioMode, - tr_piece_index_t pieceIndex, - uint32_t pieceOffset, - uint8_t * buf, - size_t buflen ) +readOrWritePiece( const tr_torrent * tor, + int ioMode, + tr_piece_index_t pieceIndex, + uint32_t pieceOffset, + uint8_t * buf, + size_t buflen ) { tr_errno err = 0; tr_file_index_t fileIndex; uint64_t fileOffset; const tr_info * info = &tor->info; - assert( pieceIndex < tor->info.pieceCount ); - assert( pieceOffset + buflen <= tr_torPieceCountBytes( tor, pieceIndex ) ); + if( pieceIndex >= tor->info.pieceCount ) + return TR_ERROR_ASSERT; + if( pieceOffset + buflen > tr_torPieceCountBytes( tor, pieceIndex ) ) + return TR_ERROR_ASSERT; - if( !err ) - err = findFileLocation ( tor, pieceIndex, pieceOffset, &fileIndex, &fileOffset ); + findFileLocation ( tor, pieceIndex, pieceOffset, + &fileIndex, &fileOffset ); while( buflen && !err ) { @@ -194,7 +198,7 @@ readOrWritePiece( tr_torrent * tor, fileIndex, fileOffset, buf, bytesThisPass ); buf += bytesThisPass; buflen -= bytesThisPass; - fileIndex++; + ++fileIndex; fileOffset = 0; } @@ -208,11 +212,11 @@ tr_ioRead( const tr_torrent * tor, uint32_t len, uint8_t * buf ) { - return readOrWritePiece( (tr_torrent*)tor, TR_IO_READ, pieceIndex, begin, buf, len ); + return readOrWritePiece( tor, TR_IO_READ, pieceIndex, begin, buf, len ); } tr_errno -tr_ioWrite( tr_torrent * tor, +tr_ioWrite( const tr_torrent * tor, tr_piece_index_t pieceIndex, uint32_t begin, uint32_t len, @@ -226,9 +230,9 @@ tr_ioWrite( tr_torrent * tor, ****/ static tr_errno -tr_ioRecalculateHash( const tr_torrent * tor, - tr_piece_index_t pieceIndex, - uint8_t * setme ) +recalculateHash( const tr_torrent * tor, + tr_piece_index_t pieceIndex, + uint8_t * setme ) { static uint8_t * buf = NULL; static int buflen = 0; @@ -271,9 +275,10 @@ tr_ioTestPiece( const tr_torrent * tor, int pieceIndex ) int err; uint8_t hash[SHA_DIGEST_LENGTH]; - err = tr_ioRecalculateHash( tor, pieceIndex, hash ); + err = recalculateHash( tor, pieceIndex, hash ); - if( !err && memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH ) ) + if( !err && memcmp( hash, tor->info.pieces[pieceIndex].hash, + SHA_DIGEST_LENGTH ) ) err = TR_ERROR_IO_CHECKSUM; tr_tordbg (tor, "piece %d hash check: %s", diff --git a/libtransmission/inout.h b/libtransmission/inout.h index 39b30dc14..c938e3255 100644 --- a/libtransmission/inout.h +++ b/libtransmission/inout.h @@ -32,22 +32,22 @@ struct tr_torrent; * @return 0 on success, TR_ERROR_ASSERT if the arguments are incorrect, * or TR_ERROR_IO_* otherwise. */ -int tr_ioRead ( const struct tr_torrent * tor, - tr_piece_index_t pieceIndex, - uint32_t offset, - uint32_t len, - uint8_t * setme ); +tr_errno tr_ioRead( const struct tr_torrent * tor, + tr_piece_index_t pieceIndex, + uint32_t offset, + uint32_t len, + uint8_t * setme ); /** * Writes the block specified by the piece index, offset, and length. * @return 0 on success, TR_ERROR_ASSERT if the arguments are incorrect, * or TR_ERROR_IO_* otherwise. */ -tr_errno tr_ioWrite ( struct tr_torrent * tor, - tr_piece_index_t pieceIndex, - uint32_t offset, - uint32_t len, - const uint8_t * writeme ); +tr_errno tr_ioWrite( const struct tr_torrent * tor, + tr_piece_index_t pieceIndex, + uint32_t offset, + uint32_t len, + const uint8_t * writeme ); /** * returns 0 if the piece matches its metainfo's SHA1 checksum, diff --git a/libtransmission/platform.c b/libtransmission/platform.c index ee772072f..453924e37 100644 --- a/libtransmission/platform.c +++ b/libtransmission/platform.c @@ -161,28 +161,6 @@ tr_threadNew( void (*func)(void *), return t; } -void -tr_threadJoin( tr_thread * t ) -{ - if( t != NULL ) - { -#ifdef __BEOS__ - long exit; - wait_for_thread( t->thread, &exit ); -#elif defined(WIN32) - WaitForSingleObject( t->thread_handle, INFINITE ); - CloseHandle( t->thread_handle ); -#else - pthread_join( t->thread, NULL ); -#endif - - tr_dbg( "Thread '%s' joined", t->name ); - t->name = NULL; - t->func = NULL; - tr_free( t ); - } -} - /*** **** LOCKS ***/ @@ -210,7 +188,7 @@ tr_lockNew( void ) #ifdef __BEOS__ l->lock = create_sem( 1, "" ); #elif defined(WIN32) - InitializeCriticalSection( &l->lock ); /* critical sections support recursion */ + InitializeCriticalSection( &l->lock ); /* supports recursion */ #else pthread_mutexattr_t attr; pthread_mutexattr_init( &attr ); @@ -300,9 +278,9 @@ getHomeDir( void ) home = tr_strdup( "" ); #else struct passwd * pw = getpwuid( getuid() ); - endpwent( ); if( pw ) home = tr_strdup( pw->pw_dir ); + endpwent( ); #endif } diff --git a/libtransmission/platform.h b/libtransmission/platform.h index 6c99bd8ad..25ea5fc86 100644 --- a/libtransmission/platform.h +++ b/libtransmission/platform.h @@ -45,7 +45,6 @@ const char * tr_getResumeDir ( const struct tr_handle * ); const char * tr_getTorrentDir ( const struct tr_handle * ); tr_thread* tr_threadNew ( void (*func)(void *), void * arg, const char * name ); -void tr_threadJoin ( tr_thread * ); int tr_amInThread ( const tr_thread * ); tr_lock * tr_lockNew ( void ); diff --git a/libtransmission/resume.c b/libtransmission/resume.c index fd98bfdd8..4ef18ecbe 100644 --- a/libtransmission/resume.c +++ b/libtransmission/resume.c @@ -66,7 +66,7 @@ savePeers( tr_benc * dict, const tr_torrent * tor ) const int count = tr_peerMgrGetPeers( tor->handle->peerMgr, tor->info.hash, &pex ); if( count > 0 ) - tr_bencInitRaw( tr_bencDictAdd( dict, KEY_PEERS ), pex, sizeof(tr_pex)*count ); + tr_bencDictAddRaw( dict, KEY_PEERS, pex, sizeof(tr_pex)*count ); tr_free( pex ); } @@ -249,7 +249,6 @@ saveProgress( tr_benc * dict, const tr_torrent * tor ) time_t * mtimes; tr_benc * p; tr_benc * m; - tr_benc * b; const tr_bitfield * bitfield; p = tr_bencDictAdd( dict, KEY_PROGRESS ); @@ -266,8 +265,8 @@ saveProgress( tr_benc * dict, const tr_torrent * tor ) /* add the bitfield */ bitfield = tr_cpBlockBitfield( tor->completion ); - b = tr_bencDictAdd( p, KEY_PROGRESS_BITFIELD ); - tr_bencInitRaw( b, bitfield->bits, bitfield->len ); + tr_bencDictAddRaw( p, KEY_PROGRESS_BITFIELD, + bitfield->bits, bitfield->len ); /* cleanup */ tr_free( mtimes );