From b04be5fb2e2e92c2c7d51339e125af58a191ddaf Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 14 Oct 2008 03:39:16 +0000 Subject: [PATCH] make MAX_PATH_LENGTH private to libtransmission. add tr_dirname() and tr_basename() utility / portability wrappers --- cli/cli.c | 7 +---- daemon/daemon.c | 6 ++-- daemon/remote.c | 4 +-- gtk/makemeta-ui.c | 54 +++++++++++++++++----------------- libtransmission/blocklist.c | 20 ++++--------- libtransmission/fdlimit.c | 5 ++-- libtransmission/makemeta.c | 25 ++++++---------- libtransmission/peer-msgs.c | 7 ++--- libtransmission/platform.h | 7 +++++ libtransmission/torrent-ctor.c | 7 ++--- libtransmission/transmission.h | 7 ----- libtransmission/utils.c | 39 +++++++++++++++++------- libtransmission/utils.h | 13 ++++---- 13 files changed, 101 insertions(+), 100 deletions(-) diff --git a/cli/cli.c b/cli/cli.c index 7f25eaa57..560f68229 100644 --- a/cli/cli.c +++ b/cli/cli.c @@ -308,7 +308,6 @@ main( int argc, tr_handle * h; tr_ctor * ctor; tr_torrent * tor = NULL; - char cwd[MAX_PATH_LENGTH]; printf( "Transmission %s - http://www.transmissionbt.com/\n", LONG_VERSION_STRING ); @@ -352,11 +351,7 @@ main( int argc, /* if no download directory specified, use cwd instead */ if( !downloadDir ) - { - tr_getcwd( cwd, sizeof( cwd ) ); - downloadDir = cwd; - } - + downloadDir = tr_strdup( tr_getcwd( ) ); /* Initialize libtransmission */ h = tr_sessionInitFull( diff --git a/daemon/daemon.c b/daemon/daemon.c index 48b624890..b0409f78d 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -167,7 +167,7 @@ session_init( const char * configDir, const char * password, int blocklistEnabled ) { - char mycwd[MAX_PATH_LENGTH]; + char * mycwd; tr_benc state, *dict = NULL; int peerPort = -1, peers = -1; int whitelistEnabled = -1; @@ -190,7 +190,7 @@ session_init( const char * configDir, **** If neither of those can be found, the TR_DEFAULT fields are used . ***/ - tr_getcwd( mycwd, sizeof( mycwd ) ); + mycwd = tr_getcwd( ); getConfigStr( dict, KEY_DOWNLOAD_DIR, &downloadDir, mycwd ); getConfigInt( dict, KEY_PEX_ENABLED, &pexEnabled, TR_DEFAULT_PEX_ENABLED ); @@ -258,6 +258,8 @@ session_init( const char * configDir, if( dict ) tr_bencFree( &state ); + + tr_free( mycwd ); } static const char * diff --git a/daemon/remote.c b/daemon/remote.c index a34af5c9c..c9ef2e3a6 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -151,9 +151,9 @@ absolutify( const char * path ) if( *path == '/' ) buf = tr_strdup( path ); else { - char cwd[MAX_PATH_LENGTH]; - tr_getcwd( cwd, sizeof( cwd ) ); + char * cwd = tr_getcwd( ); buf = tr_buildPath( cwd, path, NULL ); + tr_free( cwd ); } return buf; diff --git a/gtk/makemeta-ui.c b/gtk/makemeta-ui.c index b6f7912a6..e0d9d4254 100644 --- a/gtk/makemeta-ui.c +++ b/gtk/makemeta-ui.c @@ -248,50 +248,50 @@ static void refreshFromBuilder( MakeMetaUI * ui ) { char sizeStr[128]; - char buf[MAX_PATH_LENGTH]; + char * buf; tr_metainfo_builder * builder = ui->builder; const char * filename = builder ? builder->top : NULL; + + /* update the progressbar */ if( !filename ) - g_snprintf( buf, sizeof( buf ), _( "No source selected" ) ); + buf = g_strdup( _( "No source selected" ) ); else - g_snprintf( buf, sizeof( buf ), "%s.torrent (%d%%)", filename, 0 ); + buf = g_strdup_printf( "%s.torrent (%d%%)", filename, 0 ); gtk_progress_bar_set_text( GTK_PROGRESS_BAR( ui->progressbar ), buf ); refreshButtons( ui ); + g_free( buf ); + /* update the size label */ if( !filename ) - g_snprintf( buf, sizeof( buf ), _( "No source selected" ) ); - else - { + buf = g_strdup( "No source selected" ); + else { tr_strlsize( sizeStr, builder->totalSize, sizeof( sizeStr ) ); - g_snprintf( buf, sizeof( buf ), - /* %1$s is the torrent size - %2$'d is its number of files */ - ngettext( "%1$s; %2$'d File", - "%1$s; %2$'d Files", - builder->fileCount ), - sizeStr, builder->fileCount ); + buf = g_strdup_printf( /* %1$s is the torrent size + %2$'d is its number of files */ + ngettext( "%1$s; %2$'d File", + "%1$s; %2$'d Files", + builder->fileCount ), + sizeStr, builder->fileCount ); } gtk_label_set_markup ( GTK_LABEL( ui->size_lb ), buf ); + g_free( buf ); + /* update the pieces label */ if( !filename ) - *buf = '\0'; - else - { - char countStr[512]; - g_snprintf( countStr, sizeof( countStr ), - ngettext( "%'d Piece", "%'d Pieces", - builder->pieceCount ), - builder->pieceCount ); + buf = g_strdup( "" ); + else { + char * countStr = g_strdup_printf( ngettext( "%'d Piece", "%'d Pieces", + builder->pieceCount ), + builder->pieceCount ); tr_strlsize( sizeStr, builder->pieceSize, sizeof( sizeStr ) ); - g_snprintf( buf, sizeof( buf ), - /* %1$s is number of pieces; - %2$s is how big each piece is */ - _( "%1$s @ %2$s" ), - countStr, - sizeStr ); + buf = g_strdup_printf( /* %1$s is number of pieces; + %2$s is how big each piece is */ + _( "%1$s @ %2$s" ), countStr, sizeStr ); + g_free( countStr ); } gtk_label_set_markup ( GTK_LABEL( ui->pieces_lb ), buf ); + g_free( buf ); } static void diff --git a/libtransmission/blocklist.c b/libtransmission/blocklist.c index 1259e0ceb..842d38cb6 100644 --- a/libtransmission/blocklist.c +++ b/libtransmission/blocklist.c @@ -18,7 +18,6 @@ #include #endif -#include /* basename */ #ifndef WIN32 #include #endif @@ -105,12 +104,9 @@ blocklistLoad( tr_blocklist * b ) b->fd = fd; { - char * name; - char buf[MAX_PATH_LENGTH]; - tr_strlcpy( buf, b->filename, sizeof( buf ) ); - name = basename( buf ); - tr_inf( _( "Blocklist \"%s\" contains %'u entries" ), name, - (unsigned int)b->ruleCount ); + char * base = tr_basename( b->filename ); + tr_inf( _( "Blocklist \"%s\" contains %'zu entries" ), base, b->ruleCount ); + tr_free( base ); } } @@ -296,13 +292,9 @@ _tr_blocklistSetContent( tr_blocklist * b, } { - char * name; - char buf[MAX_PATH_LENGTH]; - tr_strlcpy( buf, b->filename, sizeof( buf ) ); - name = basename( buf ); - tr_inf( _( - "Blocklist \"%1$s\" updated with %2$'d entries" ), name, - lineCount ); + char * base = tr_basename( b->filename ); + tr_inf( _( "Blocklist \"%1$s\" updated with %2$'d entries" ), base, lineCount ); + tr_free( base ); } diff --git a/libtransmission/fdlimit.c b/libtransmission/fdlimit.c index 5d35eb42a..97f0c289e 100644 --- a/libtransmission/fdlimit.c +++ b/libtransmission/fdlimit.c @@ -40,7 +40,6 @@ #include /* getrlimit */ #endif #include -#include /* dirname */ #include /* O_LARGEFILE */ #include @@ -124,8 +123,8 @@ TrOpenFile( int i, filename = tr_buildPath( folder, torrentFile, NULL ); if( write ) { - char * tmp = tr_strdup( filename ); - const int err = tr_mkdirp( dirname( tmp ), 0777 ) ? errno : 0; + char * tmp = tr_dirname( filename ); + const int err = tr_mkdirp( tmp, 0777 ) ? errno : 0; tr_free( tmp ); tr_free( filename ); if( err ) diff --git a/libtransmission/makemeta.c b/libtransmission/makemeta.c index f7afeafe9..1b9efc823 100644 --- a/libtransmission/makemeta.c +++ b/libtransmission/makemeta.c @@ -19,7 +19,6 @@ #include #include #include -#include /* dirname, basename */ #include #include "crypto.h" /* tr_sha1 */ @@ -133,14 +132,11 @@ tr_metaInfoBuilderCreate( tr_handle * handle, /* build a list of files containing topFile and, if it's a directory, all of its children */ { - char *dir, *base; - char dirbuf[MAX_PATH_LENGTH]; - char basebuf[MAX_PATH_LENGTH]; - tr_strlcpy( dirbuf, topFile, sizeof( dirbuf ) ); - tr_strlcpy( basebuf, topFile, sizeof( basebuf ) ); - dir = dirname( dirbuf ); - base = basename( basebuf ); + char * dir = tr_dirname( topFile ); + char * base = tr_basename( topFile ); files = getFiles( dir, base, NULL ); + tr_free( base ); + tr_free( dir ); } for( walk = files; walk != NULL; walk = walk->next ) @@ -333,7 +329,7 @@ makeInfoDict( tr_benc * dict, tr_metainfo_builder * builder ) { uint8_t * pch; - char base[MAX_PATH_LENGTH]; + char * base; tr_bencDictReserve( dict, 5 ); @@ -353,8 +349,9 @@ makeInfoDict( tr_benc * dict, } } - tr_strlcpy( base, builder->top, sizeof( base ) ); - tr_bencDictAddStr( dict, "name", basename( base ) ); + base = tr_basename( builder->top ); + tr_bencDictAddStr( dict, "name", base ); + tr_free( base ); tr_bencDictAddInt( dict, "piece length", builder->pieceSize ); @@ -519,11 +516,7 @@ tr_makeMetaInfo( tr_metainfo_builder * builder, if( outputFile && *outputFile ) builder->outputFile = tr_strdup( outputFile ); else - { - char out[MAX_PATH_LENGTH]; - tr_snprintf( out, sizeof( out ), "%s.torrent", builder->top ); - builder->outputFile = tr_strdup( out ); - } + builder->outputFile = tr_strdup_printf( "%s.torrent", builder->top ); /* enqueue the builder */ lock = getQueueLock ( builder->handle ); diff --git a/libtransmission/peer-msgs.c b/libtransmission/peer-msgs.c index 7c6ced456..ca29e5d22 100644 --- a/libtransmission/peer-msgs.c +++ b/libtransmission/peer-msgs.c @@ -16,7 +16,6 @@ #include #include #include -#include /* basename */ #include @@ -327,7 +326,7 @@ myDebug( const char * file, va_list args; char timestr[64]; struct evbuffer * buf = evbuffer_new( ); - char * myfile = tr_strdup( file ); + char * base = tr_basename( file ); evbuffer_add_printf( buf, "[%s] %s - %s [%s]: ", tr_getLogTimeStr( timestr, sizeof( timestr ) ), @@ -337,10 +336,10 @@ myDebug( const char * file, va_start( args, fmt ); evbuffer_add_vprintf( buf, fmt, args ); va_end( args ); - evbuffer_add_printf( buf, " (%s:%d)\n", basename( myfile ), line ); + evbuffer_add_printf( buf, " (%s:%d)\n", base, line ); fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp ); - tr_free( myfile ); + tr_free( base ); evbuffer_free( buf ); } } diff --git a/libtransmission/platform.h b/libtransmission/platform.h index 82c5e9603..71522f436 100644 --- a/libtransmission/platform.h +++ b/libtransmission/platform.h @@ -32,6 +32,13 @@ #define TR_PATH_DELIMITER_STR "/" #endif +#ifdef __BEOS__ + #include + #define MAX_PATH_LENGTH B_FILE_NAME_LENGTH +#else + #define MAX_PATH_LENGTH 1024 +#endif + typedef struct tr_lock tr_lock; typedef struct tr_thread tr_thread; diff --git a/libtransmission/torrent-ctor.c b/libtransmission/torrent-ctor.c index fbb7e73ab..d3e9da668 100644 --- a/libtransmission/torrent-ctor.c +++ b/libtransmission/torrent-ctor.c @@ -11,7 +11,6 @@ */ #include -#include /* basename */ #include "transmission.h" #include "bencode.h" #include "platform.h" @@ -121,9 +120,9 @@ tr_ctorSetMetainfoFromFile( tr_ctor * ctor, name = NULL; if( !name || !*name ) { - char * tmp = tr_strdup( filename ); - tr_bencDictAddStr( info, "name", basename( tmp ) ); - tr_free( tmp ); + char * base = tr_basename( filename ); + tr_bencDictAddStr( info, "name", base ); + tr_free( base ); } } } diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 958b97ef7..5e4f5a71e 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -49,13 +49,6 @@ extern "C" { #define SHA_DIGEST_LENGTH 20 -#ifdef __BEOS__ - #include - #define MAX_PATH_LENGTH B_FILE_NAME_LENGTH -#else - #define MAX_PATH_LENGTH 1024 -#endif - typedef uint32_t tr_file_index_t; typedef uint32_t tr_piece_index_t; typedef uint64_t tr_block_index_t; diff --git a/libtransmission/utils.c b/libtransmission/utils.c index a50e0a4c5..c93e87829 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -195,7 +195,7 @@ tr_deepLog( const char * file, va_list args; char timestr[64]; struct evbuffer * buf = evbuffer_new( ); - char * myfile = tr_strdup( file ); + char * base = tr_basename( file ); evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof( timestr ) ) ); @@ -204,10 +204,10 @@ tr_deepLog( const char * file, va_start( args, fmt ); evbuffer_add_vprintf( buf, fmt, args ); va_end( args ); - evbuffer_add_printf( buf, " (%s:%d)\n", basename( myfile ), line ); + evbuffer_add_printf( buf, " (%s:%d)\n", base, line ); (void) fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp ); - tr_free( myfile ); + tr_free( base ); evbuffer_free( buf ); } } @@ -456,14 +456,34 @@ tr_loadFile( const char * path, } char* -tr_getcwd( char * buffer, - int maxlen ) +tr_getcwd( void ) { + char buf[2048]; + *buf = '\0'; #ifdef WIN32 - return _getcwd( buffer, maxlen ); + _getcwd( buf, sizeof( buf ) ); #else - return getcwd( buffer, maxlen ); + getcwd( buf, sizeof( buf ) ); #endif + return tr_strdup( buf ); +} + +char* +tr_basename( const char * path ) +{ + char * tmp = tr_strdup( path ); + char * ret = tr_strdup( basename( tmp ) ); + tr_free( tmp ); + return ret; +} + +char* +tr_dirname( const char * path ) +{ + char * tmp = tr_strdup( path ); + char * ret = tr_strdup( dirname( tmp ) ); + tr_free( tmp ); + return ret; } int @@ -524,10 +544,9 @@ tr_mkdirp( const char * path_in, else if( ( sb.st_mode & S_IFMT ) != S_IFDIR ) { /* Node exists but isn't a folder */ - char buf[MAX_PATH_LENGTH]; - tr_snprintf( buf, sizeof( buf ), _( - "File \"%s\" is in the way" ), path ); + char * buf = tr_strdup_printf( _( "File \"%s\" is in the way" ), path ); tr_err( _( "Couldn't create \"%1$s\": %2$s" ), path_in, buf ); + tr_free( buf ); tr_free( path ); errno = ENOTDIR; return -1; diff --git a/libtransmission/utils.h b/libtransmission/utils.h index b0c695dda..ef51b8202 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -140,11 +140,14 @@ void tr_deepLog( const char * file, char* tr_getLogTimeStr( char * buf, int buflen ); -/** - * a portability wrapper around getcwd(). - */ -char* tr_getcwd( char * buffer, - int maxlen ); +/** a portability wrapper for getcwd(). */ +char* tr_getcwd( void ) TR_GNUC_MALLOC; + +/** a portability wrapper for basename(). */ +char* tr_basename( const char * path ) TR_GNUC_MALLOC; + +/** a portability wrapper for dirname(). */ +char* tr_dirname( const char * path ) TR_GNUC_MALLOC; /** * a portability wrapper around mkdir().