From 0ad06c50e873824e8dff41987becc03ed590f074 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 7 Jun 2010 14:25:31 +0000 Subject: [PATCH] (trunk) #3262 "problems with '.' as the first character in a .torrent's filename" -- fixed in trunk for 2.00 --- daemon/watch.c | 18 ++---------------- libtransmission/platform.c | 8 ++++---- libtransmission/session.c | 5 ++--- libtransmission/torrent.c | 2 +- libtransmission/utils.c | 19 +++++++++++++++++++ libtransmission/utils.h | 4 ++++ 6 files changed, 32 insertions(+), 24 deletions(-) diff --git a/daemon/watch.c b/daemon/watch.c index e53ddc1ef..de2f03998 100644 --- a/daemon/watch.c +++ b/daemon/watch.c @@ -41,18 +41,6 @@ struct dtr_watchdir #endif }; -static tr_bool -str_has_suffix( const char *str, const char *suffix ) -{ - const size_t str_len = strlen( str ); - const size_t suffix_len = strlen( suffix ); - - if( str_len < suffix_len ) - return FALSE; - - return !strncasecmp( str + str_len - suffix_len, suffix, suffix_len ); -} - /*** **** INOTIFY IMPLEMENTATION ***/ @@ -97,9 +85,7 @@ watchdir_new_impl( dtr_watchdir * w ) { const char * name = d->d_name; - if( !name || *name=='.' ) /* skip dotfiles */ - continue; - if( !str_has_suffix( name, ".torrent" ) ) /* skip non-torrents */ + if( !tr_str_has_suffix( name, ".torrent" ) ) /* skip non-torrents */ continue; tr_inf( "Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir ); @@ -149,7 +135,7 @@ watchdir_update_impl( dtr_watchdir * w ) while (i < len) { struct inotify_event * event = (struct inotify_event *) &buf[i]; const char * name = event->name; - if( str_has_suffix( name, ".torrent" ) ) + if( tr_str_has_suffix( name, ".torrent" ) ) { tr_inf( "Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir ); w->callback( w->session, w->dir, name ); diff --git a/libtransmission/platform.c b/libtransmission/platform.c index 70e3776eb..09f1b057d 100644 --- a/libtransmission/platform.c +++ b/libtransmission/platform.c @@ -339,11 +339,11 @@ moveFiles( const char * oldDir, struct dirent * dirp; while( ( dirp = readdir( dirh ) ) ) { - if( strcmp( dirp->d_name, - "." ) && strcmp( dirp->d_name, ".." ) ) + const char * name = dirp->d_name; + if( name && strcmp( name, "." ) && strcmp( name, ".." ) ) { - char * o = tr_buildPath( oldDir, dirp->d_name, NULL ); - char * n = tr_buildPath( newDir, dirp->d_name, NULL ); + char * o = tr_buildPath( oldDir, name, NULL ); + char * n = tr_buildPath( newDir, name, NULL ); rename( o, n ); ++count; tr_free( n ); diff --git a/libtransmission/session.c b/libtransmission/session.c index b24ac7a2d..afb586726 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -1720,8 +1720,7 @@ tr_sessionLoadTorrents( tr_session * session, struct dirent *d; for( d = readdir( odir ); d != NULL; d = readdir( odir ) ) { - if( d->d_name && d->d_name[0] != '.' ) /* skip dotfiles, ., and .. - */ + if( tr_str_has_suffix( d->d_name, ".torrent" ) ) { tr_torrent * tor; char * path = tr_buildPath( dirname, d->d_name, NULL ); @@ -2103,7 +2102,7 @@ metainfoLookupInit( tr_session * session ) struct dirent *d; while(( d = readdir( odir ))) { - if( d->d_name && d->d_name[0] != '.' ) + if( tr_str_has_suffix( d->d_name, ".torrent" ) ) { tr_info inf; char * path = tr_buildPath( dirname, d->d_name, NULL ); diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 87aa068b9..83fd73ac0 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -2376,7 +2376,7 @@ walkLocalData( const tr_torrent * tor, struct dirent *d; tr_ptrArrayInsertSorted( folders, tr_strdup( buf ), vstrcmp ); for( d = readdir( odir ); d != NULL; d = readdir( odir ) ) - if( d->d_name && d->d_name[0] != '.' ) /* skip dotfiles */ + if( d->d_name && strcmp( d->d_name, "." ) && strcmp( d->d_name, ".." ) ) walkLocalData( tor, root, buf, d->d_name, torrentFiles, folders, dirtyFolders ); closedir( odir ); } diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 128cb222d..d28eea5c0 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -773,6 +773,25 @@ tr_strstrip( char * str ) return str; } +tr_bool +tr_str_has_suffix( const char *str, const char *suffix ) +{ + size_t str_len; + size_t suffix_len; + + if( !str ) + return FALSE; + if( !suffix ) + return TRUE; + + str_len = strlen( str ); + suffix_len = strlen( suffix ); + if( str_len < suffix_len ) + return FALSE; + + return !strncasecmp( str + str_len - suffix_len, suffix, suffix_len ); +} + /**** ***** ****/ diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 6c1939ea0..85df9b058 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -400,6 +400,10 @@ const char* tr_strerror( int ); @return the stripped string */ char* tr_strstrip( char * str ); +/** @brief Returns true if the string ends with the specified case-insensitive suffix */ +tr_bool tr_str_has_suffix( const char *str, const char *suffix ); + + /** @brief Portability wrapper for memmem() that uses the system implementation if available */ const char* tr_memmem( const char * haystack, size_t haystack_len, const char * needle, size_t needle_len ); -- 2.40.0