diff --git a/libtransmission/choking.c b/libtransmission/choking.c index 0852c322c..b80bf54da 100644 --- a/libtransmission/choking.c +++ b/libtransmission/choking.c @@ -56,7 +56,7 @@ tr_choking_t * tr_chokingInit( tr_handle_t * h ) { tr_choking_t * c; - c = calloc( sizeof( tr_choking_t ), 1 ); + c = calloc( 1, sizeof( tr_choking_t ) ); c->h = h; c->slots = 4242; tr_lockInit( &c->lock ); diff --git a/libtransmission/completion.c b/libtransmission/completion.c index af3317871..3936b6858 100644 --- a/libtransmission/completion.c +++ b/libtransmission/completion.c @@ -162,7 +162,7 @@ void tr_cpDownloaderRem( tr_completion_t * cp, int block ) } } -int tr_cpBlockIsComplete( tr_completion_t * cp, int block ) +int tr_cpBlockIsComplete( const tr_completion_t * cp, int block ) { return tr_bitfieldHas( cp->blockBitfield, block ); } @@ -257,9 +257,9 @@ float tr_cpPercentBlocksInPiece( tr_completion_t * cp, int piece ) return (float)complete / (float)blockCount; } -int tr_cpMissingBlockInPiece( tr_completion_t * cp, int piece ) +int tr_cpMissingBlockInPiece( const tr_completion_t * cp, int piece ) { - tr_torrent_t * tor = cp->tor; + const tr_torrent_t * tor = cp->tor; int start, count, end, i; start = tr_pieceStartBlock( piece ); diff --git a/libtransmission/completion.h b/libtransmission/completion.h index 44dca6a7d..68846e54d 100644 --- a/libtransmission/completion.h +++ b/libtransmission/completion.h @@ -54,17 +54,17 @@ void tr_cpPieceRem( tr_completion_t *, int piece ); /* Blocks */ void tr_cpDownloaderAdd( tr_completion_t *, int block ); void tr_cpDownloaderRem( tr_completion_t *, int block ); -int tr_cpBlockIsComplete( tr_completion_t *, int block ); +int tr_cpBlockIsComplete( const tr_completion_t *, int block ); void tr_cpBlockAdd( tr_completion_t *, int block ); void tr_cpBlockRem( tr_completion_t *, int block ); tr_bitfield_t * tr_cpBlockBitfield( tr_completion_t * ); void tr_cpBlockBitfieldSet( tr_completion_t *, tr_bitfield_t * ); float tr_cpPercentBlocksInPiece( tr_completion_t * cp, int piece ); /* Missing = we don't have it and we are not getting it from any peer yet */ -static inline int tr_cpMissingBlocksForPiece( tr_completion_t * cp, int piece ) +static inline int tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece ) { return cp->missingBlocks[piece]; } -int tr_cpMissingBlockInPiece( tr_completion_t *, int piece ); +int tr_cpMissingBlockInPiece( const tr_completion_t *, int piece ); int tr_cpMostMissingBlockInPiece( tr_completion_t *, int piece, int * downloaders ); diff --git a/libtransmission/fdlimit.c b/libtransmission/fdlimit.c index 1231341e5..c5aeff284 100644 --- a/libtransmission/fdlimit.c +++ b/libtransmission/fdlimit.c @@ -67,7 +67,7 @@ static tr_fd_t * gFd = NULL; /*********************************************************************** * Local prototypes **********************************************************************/ -static int OpenFile( int i, char * folder, char * name, int write ); +static int OpenFile( int i, const char * folder, const char * name, int write ); static void CloseFile( int i ); @@ -84,7 +84,7 @@ void tr_fdInit() return; } - gFd = calloc( sizeof( tr_fd_t ), 1 ); + gFd = calloc( 1, sizeof( tr_fd_t ) ); /* Init lock and cond */ tr_lockInit( &gFd->lock ); @@ -125,7 +125,7 @@ void tr_fdInit() /*********************************************************************** * tr_fdFileOpen **********************************************************************/ -int tr_fdFileOpen( char * folder, char * name, int write ) +int tr_fdFileOpen( const char * folder, const char * name, int write ) { int i, winner, ret; uint64_t date; @@ -241,7 +241,7 @@ void tr_fdFileRelease( int file ) /*********************************************************************** * tr_fdFileClose **********************************************************************/ -void tr_fdFileClose( char * folder, char * name ) +void tr_fdFileClose( const char * folder, const char * name ) { int i; @@ -410,11 +410,11 @@ void tr_fdClose() *********************************************************************** * **********************************************************************/ -static int OpenFile( int i, char * folder, char * name, int write ) +static int OpenFile( int i, const char * folder, const char * name, int write ) { tr_openFile_t * file = &gFd->open[i]; struct stat sb; - char * path; + char path[MAX_PATH_LENGTH]; int ret; tr_dbg( "Opening %s in %s (%d)", name, folder, write ); @@ -425,7 +425,7 @@ static int OpenFile( int i, char * folder, char * name, int write ) return TR_ERROR_IO_PARENT; } - asprintf( &path, "%s/%s", folder, name ); + snprintf( path, sizeof(path), "%s/%s", folder, name ); /* Create subfolders, if any */ if( write ) @@ -442,7 +442,6 @@ static int OpenFile( int i, char * folder, char * name, int write ) { ret = tr_ioErrorFromErrno(); tr_err( "Could not create folder '%s'", path ); - free( path ); return ret; } } @@ -451,7 +450,6 @@ static int OpenFile( int i, char * folder, char * name, int write ) if( !S_ISDIR( sb.st_mode ) ) { tr_err( "Is not a folder: '%s'", path ); - free( path ); return TR_ERROR_IO_OTHER; } } @@ -465,11 +463,9 @@ static int OpenFile( int i, char * folder, char * name, int write ) if( file->file < 0 ) { ret = tr_ioErrorFromErrno(); - free( path ); tr_err( "Could not open %s in %s (%d, %d)", name, folder, write, ret ); return ret; } - free( path ); return TR_OK; } diff --git a/libtransmission/fdlimit.h b/libtransmission/fdlimit.h index a30b422c0..13334651a 100644 --- a/libtransmission/fdlimit.h +++ b/libtransmission/fdlimit.h @@ -39,7 +39,7 @@ void tr_fdInit(); * Returns the file descriptor if successful, otherwise returns * one of the TR_ERROR_IO_*. **********************************************************************/ -int tr_fdFileOpen( char * folder, char * name, int write ); +int tr_fdFileOpen( const char * folder, const char * name, int write ); /*********************************************************************** * tr_fdFileRelease @@ -55,7 +55,7 @@ void tr_fdFileRelease( int file ); * If the file 'name' in directory 'folder' was open, closes it, * flushing data on disk. **********************************************************************/ -void tr_fdFileClose( char * folder, char * name ); +void tr_fdFileClose( const char * folder, const char * name ); /*********************************************************************** * Sockets diff --git a/libtransmission/peer.c b/libtransmission/peer.c index d012fa963..020486eb7 100644 --- a/libtransmission/peer.c +++ b/libtransmission/peer.c @@ -735,7 +735,7 @@ void tr_peerBlame( tr_peer_t * peer, int piece, int success ) tr_bitfieldRem( peer->blamefield, piece ); } -int tr_peerGetConnectable( tr_torrent_t * tor, uint8_t ** _buf ) +int tr_peerGetConnectable( const tr_torrent_t * tor, uint8_t ** _buf ) { int count = 0; uint8_t * buf; diff --git a/libtransmission/peer.h b/libtransmission/peer.h index 1dc8e1f26..6d88ecf15 100644 --- a/libtransmission/peer.h +++ b/libtransmission/peer.h @@ -57,6 +57,6 @@ void tr_peerSetOptimistic ( tr_peer_t *, int ); int tr_peerIsOptimistic ( tr_peer_t * ); void tr_peerBlame ( tr_peer_t *, int piece, int success ); struct in_addr * tr_peerAddress ( tr_peer_t * ); -int tr_peerGetConnectable( tr_torrent_t *, uint8_t ** ); +int tr_peerGetConnectable( const tr_torrent_t *, uint8_t ** ); #endif diff --git a/libtransmission/ratecontrol.c b/libtransmission/ratecontrol.c index e395a8fee..3db5f72d5 100644 --- a/libtransmission/ratecontrol.c +++ b/libtransmission/ratecontrol.c @@ -75,7 +75,7 @@ tr_ratecontrol_t * tr_rcInit() { tr_ratecontrol_t * r; - r = calloc( sizeof( tr_ratecontrol_t ), 1 ); + r = calloc( 1, sizeof( tr_ratecontrol_t ) ); r->limit = -1; tr_lockInit( &r->lock ); diff --git a/libtransmission/tracker.c b/libtransmission/tracker.c index c141c87a7..ca100f6e1 100644 --- a/libtransmission/tracker.c +++ b/libtransmission/tracker.c @@ -629,7 +629,7 @@ static void readAnswer( tr_tracker_t * tc, const char * data, int len, tr_httpParse( data, len, hdr ); - address = calloc( sizeof( char ), hdr->len+1 ); + address = calloc( hdr->len+1, sizeof( char ) ); snprintf( address, hdr->len+1, "%s", hdr->data ); tc->shouldChangeAnnounce = TC_CHANGE_REDIRECT; @@ -871,7 +871,7 @@ static void readScrapeAnswer( tr_tracker_t * tc, const char * data, int len ) tr_httpParse( data, len, hdr ); - address = calloc( sizeof( char ), hdr->len+1 ); + address = calloc( hdr->len+1, sizeof( char ) ); snprintf( address, hdr->len+1, "%s", hdr->data ); /* Needs a new scrape */ diff --git a/libtransmission/transmission.c b/libtransmission/transmission.c index 0f310ca3b..cee133023 100644 --- a/libtransmission/transmission.c +++ b/libtransmission/transmission.c @@ -38,7 +38,7 @@ tr_handle_t * tr_init( const char * tag ) tr_msgInit(); tr_netResolveThreadInit(); - h = calloc( sizeof( tr_handle_t ), 1 ); + h = calloc( 1, sizeof( tr_handle_t ) ); if( NULL == h ) { return NULL; @@ -180,4 +180,3 @@ void tr_close( tr_handle_t * h ) tr_netResolveThreadClose(); } - diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 4bd2d494d..50b13b331 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -169,16 +169,16 @@ static inline void tr_bitfieldRem( tr_bitfield_t * bitfield, int piece ) } #define tr_blockPiece(a) _tr_blockPiece(tor,a) -static inline int _tr_blockPiece( tr_torrent_t * tor, int block ) +static inline int _tr_blockPiece( const tr_torrent_t * tor, int block ) { - tr_info_t * inf = &tor->info; + const tr_info_t * inf = &tor->info; return block / ( inf->pieceSize / tor->blockSize ); } #define tr_blockSize(a) _tr_blockSize(tor,a) -static inline int _tr_blockSize( tr_torrent_t * tor, int block ) +static inline int _tr_blockSize( const tr_torrent_t * tor, int block ) { - tr_info_t * inf = &tor->info; + const tr_info_t * inf = &tor->info; int dummy; if( block != tor->blockCount - 1 || @@ -191,17 +191,17 @@ static inline int _tr_blockSize( tr_torrent_t * tor, int block ) } #define tr_blockPosInPiece(a) _tr_blockPosInPiece(tor,a) -static inline int _tr_blockPosInPiece( tr_torrent_t * tor, int block ) +static inline int _tr_blockPosInPiece( const tr_torrent_t * tor, int block ) { - tr_info_t * inf = &tor->info; + const tr_info_t * inf = &tor->info; return tor->blockSize * ( block % ( inf->pieceSize / tor->blockSize ) ); } #define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a) -static inline int _tr_pieceCountBlocks( tr_torrent_t * tor, int piece ) +static inline int _tr_pieceCountBlocks( const tr_torrent_t * tor, int piece ) { - tr_info_t * inf = &tor->info; + const tr_info_t * inf = &tor->info; if( piece < inf->pieceCount - 1 || !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) ) { @@ -211,16 +211,16 @@ static inline int _tr_pieceCountBlocks( tr_torrent_t * tor, int piece ) } #define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a) -static inline int _tr_pieceStartBlock( tr_torrent_t * tor, int piece ) +static inline int _tr_pieceStartBlock( const tr_torrent_t * tor, int piece ) { - tr_info_t * inf = &tor->info; + const tr_info_t * inf = &tor->info; return piece * ( inf->pieceSize / tor->blockSize ); } #define tr_pieceSize(a) _tr_pieceSize(tor,a) -static inline int _tr_pieceSize( tr_torrent_t * tor, int piece ) +static inline int _tr_pieceSize( const tr_torrent_t * tor, int piece ) { - tr_info_t * inf = &tor->info; + const tr_info_t * inf = &tor->info; if( piece < inf->pieceCount - 1 || !( inf->totalSize % inf->pieceSize ) ) { @@ -230,9 +230,9 @@ static inline int _tr_pieceSize( tr_torrent_t * tor, int piece ) } #define tr_block(a,b) _tr_block(tor,a,b) -static inline int _tr_block( tr_torrent_t * tor, int index, int begin ) +static inline int _tr_block( const tr_torrent_t * tor, int index, int begin ) { - tr_info_t * inf = &tor->info; + const tr_info_t * inf = &tor->info; return index * ( inf->pieceSize / tor->blockSize ) + begin / tor->blockSize; }