diff --git a/libtransmission/metainfo.c b/libtransmission/metainfo.c index 1e5c983c4..90cb85f7c 100644 --- a/libtransmission/metainfo.c +++ b/libtransmission/metainfo.c @@ -74,6 +74,7 @@ getOldTorrentFilename( const tr_session * session, return ret; } +/* this is for really old versions of T and will probably be removed someday */ void tr_metainfoMigrate( tr_session * session, tr_info * inf ) diff --git a/libtransmission/session.c b/libtransmission/session.c index 72dd5def6..149aaac71 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -529,7 +529,6 @@ tr_sessionSaveSettings( tr_session * session, tr_bencFree( &settings ); } -static void metainfoLookupRescan( tr_session * ); static void tr_sessionInitImpl( void * ); static void onAltTimer( int, short, void* ); static void setAltTimer( tr_session * session ); @@ -834,7 +833,6 @@ tr_sessionInitImpl( void * vdata ) tr_statsInit( session ); session->web = tr_webInit( session ); - metainfoLookupRescan( session ); session->isWaiting = FALSE; dbgmsg( "returning session %p; session->tracker is %p", session, session->tracker ); @@ -1781,19 +1779,6 @@ compareHashStringToLookupEntry( const void * va, const void * vb ) return strcmp( a, b->hashString ); } -const char* -tr_sessionFindTorrentFile( const tr_session * session, - const char * hashStr ) -{ - struct tr_metainfo_lookup * l = bsearch( hashStr, - session->metainfoLookup, - session->metainfoLookupCount, - sizeof( struct tr_metainfo_lookup ), - compareHashStringToLookupEntry ); - - return l ? l->filename : NULL; -} - static void metainfoLookupRescan( tr_session * session ) { @@ -1852,17 +1837,46 @@ metainfoLookupRescan( tr_session * session ) tr_dbg( "Found %d torrents in \"%s\"", n, dirname ); } +static struct tr_metainfo_lookup * +metainfoLookup( const tr_session * session, const char * hashString ) +{ + /* because only the mac client uses metainfoLookup, and because building + * the lookup is expensive, we hold off on building it until the client + * actually asks to look up a hash... */ + if( !session->metainfoLookupCount ) + metainfoLookupRescan( (tr_session*)session ); + + return bsearch( hashString, + session->metainfoLookup, + session->metainfoLookupCount, + sizeof( struct tr_metainfo_lookup ), + compareHashStringToLookupEntry ); +} + +const char* +tr_sessionFindTorrentFile( const tr_session * session, + const char * hashString ) +{ + const struct tr_metainfo_lookup * l = metainfoLookup( session, hashString ); + + return l ? l->filename : NULL; +} + void tr_sessionSetTorrentFile( tr_session * session, const char * hashString, const char * filename ) { - struct tr_metainfo_lookup * l = bsearch( hashString, - session->metainfoLookup, - session->metainfoLookupCount, - sizeof( struct tr_metainfo_lookup ), - compareHashStringToLookupEntry ); + struct tr_metainfo_lookup * l; + /* since we walk session->configDir/torrents/ to build the lookup table, + * and tr_sessionSetTorrentFile() is just to tell us there's a new file + * in that same directory, we don't need to do anything here if the + * lookup table hasn't been built yet */ + if( session->metainfoLookup == NULL ) + return; + + l = metainfoLookup( session, hashString ); if( l ) { if( l->filename != filename )