From: Charles Kerr Date: Thu, 2 Apr 2009 17:30:29 +0000 (+0000) Subject: (trunk) modify tr_torrentParse() and tr_torrentNew() arguments s.t. they use the... X-Git-Tag: 1.60~184 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c3b24bc4713de6e12cd7c395d7bcd5da79113211;p=transmission (trunk) modify tr_torrentParse() and tr_torrentNew() arguments s.t. they use the ctor's session instead of passing it in again. Allow a NULL session pointer for some cases of tr_torrentParse(). --- diff --git a/cli/cli.c b/cli/cli.c index e77861e06..ac8c31343 100644 --- a/cli/cli.c +++ b/cli/cli.c @@ -355,7 +355,7 @@ main( int argc, { tr_info info; - if( !tr_torrentParse( h, ctor, &info ) ) + if( !tr_torrentParse( ctor, &info ) ) { int i; const time_t start = time( NULL ); @@ -391,7 +391,7 @@ main( int argc, { tr_info info; - if( !tr_torrentParse( h, ctor, &info ) ) + if( !tr_torrentParse( ctor, &info ) ) { dumpInfo( stdout, &info ); tr_metainfoFree( &info ); @@ -401,7 +401,7 @@ main( int argc, goto cleanup; } - tor = tr_torrentNew( h, ctor, &error ); + tor = tr_torrentNew( ctor, &error ); tr_ctorFree( ctor ); if( !tor ) { diff --git a/daemon/daemon.c b/daemon/daemon.c index 1d3cda465..da8024ead 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -190,7 +190,7 @@ onFileAdded( tr_session * session, const char * dir, const char * file ) int err = tr_ctorSetMetainfoFromFile( ctor, filename ); if( !err ) - tr_torrentNew( session, ctor, &err ); + tr_torrentNew( ctor, &err ); tr_ctorFree( ctor ); tr_free( filename ); diff --git a/gtk/add-dialog.c b/gtk/add-dialog.c index 2849ca2a3..11cbd06f5 100644 --- a/gtk/add-dialog.c +++ b/gtk/add-dialog.c @@ -171,7 +171,6 @@ sourceChanged( GtkFileChooserButton * b, int err = 0; int new_file = 0; tr_torrent * torrent; - tr_session * session = tr_core_session( data->core ); if( filename && ( !data->filename || strcmp( filename, data->filename ) ) ) @@ -186,7 +185,7 @@ sourceChanged( GtkFileChooserButton * b, tr_ctorSetPaused( data->ctor, TR_FORCE, TRUE ); tr_ctorSetDeleteSource( data->ctor, FALSE ); - if( ( torrent = tr_torrentNew( session, data->ctor, &err ) ) ) + if( ( torrent = tr_torrentNew( data->ctor, &err ) ) ) { removeOldTorrent( data ); data->gtor = tr_torrent_new_preexisting( torrent ); diff --git a/gtk/tr-core.c b/gtk/tr-core.c index e23f15541..2f50c3260 100644 --- a/gtk/tr-core.c +++ b/gtk/tr-core.c @@ -927,7 +927,7 @@ add_filename( TrCore * core, else { tr_info inf; - int err = tr_torrentParse( session, ctor, &inf ); + int err = tr_torrentParse( ctor, &inf ); switch( err ) { diff --git a/gtk/tr-torrent.c b/gtk/tr-torrent.c index c6b2d9ca8..183d7b10c 100644 --- a/gtk/tr-torrent.c +++ b/gtk/tr-torrent.c @@ -198,7 +198,7 @@ tr_torrent_new_ctor( tr_session * session, * doesn't have any concept of the glib trash API */ tr_ctorGetDeleteSource( ctor, &doTrash ); tr_ctorSetDeleteSource( ctor, FALSE ); - tor = tr_torrentNew( session, ctor, errcode ); + tor = tr_torrentNew( ctor, errcode ); if( tor && doTrash ) { diff --git a/libtransmission/fdlimit.c b/libtransmission/fdlimit.c index bfb36e7a6..13a0daebe 100644 --- a/libtransmission/fdlimit.c +++ b/libtransmission/fdlimit.c @@ -443,7 +443,7 @@ tr_fdFileReturn( int fd ) TrCloseFile( i ); else if( o->syncAt <= time( NULL ) ) { dbgmsg( "fsync()ing file '%s' in slot #%d", o->filename, i ); - fsync( o->fd ); + //fsync( o->fd ); #ifdef HAVE_POSIX_FADVISE /* TODO: test performance with and without this */ posix_fadvise( o->fd, 0, 0, POSIX_FADV_DONTNEED ); diff --git a/libtransmission/metainfo.c b/libtransmission/metainfo.c index c52cc134e..1e5c983c4 100644 --- a/libtransmission/metainfo.c +++ b/libtransmission/metainfo.c @@ -440,7 +440,7 @@ tr_metainfoParseImpl( const tr_session * session, /* filename of Transmission's copy */ tr_free( inf->torrent ); - inf->torrent = getTorrentFilename( session, inf ); + inf->torrent = session ? getTorrentFilename( session, inf ) : NULL; return NULL; } diff --git a/libtransmission/rpcimpl.c b/libtransmission/rpcimpl.c index 41e10db5f..6c926baa9 100644 --- a/libtransmission/rpcimpl.c +++ b/libtransmission/rpcimpl.c @@ -745,7 +745,7 @@ addTorrentImpl( struct tr_rpc_idle_data * data, tr_ctor * ctor ) { int err = 0; const char * result = NULL; - tr_torrent * tor = tr_torrentNew( data->session, ctor, &err ); + tr_torrent * tor = tr_torrentNew( ctor, &err ); tr_ctorFree( ctor ); diff --git a/libtransmission/session.c b/libtransmission/session.c index 79f650d8a..48a488d8e 100644 --- a/libtransmission/session.c +++ b/libtransmission/session.c @@ -1368,7 +1368,7 @@ tr_sessionLoadTorrents( tr_session * session, tr_torrent * tor; char * path = tr_buildPath( dirname, d->d_name, NULL ); tr_ctorSetMetainfoFromFile( ctor, path ); - if(( tor = tr_torrentNew( session, ctor, NULL ))) + if(( tor = tr_torrentNew( ctor, NULL ))) { tr_list_append( &list, tor ); ++n; @@ -1619,7 +1619,7 @@ metainfoLookupRescan( tr_session * session ) tr_info inf; char * path = tr_buildPath( dirname, d->d_name, NULL ); tr_ctorSetMetainfoFromFile( ctor, path ); - if( !tr_torrentParse( session, ctor, &inf ) ) + if( !tr_torrentParse( ctor, &inf ) ) { tr_list_append( &list, tr_strdup( inf.hashString ) ); tr_list_append( &list, tr_strdup( path ) ); diff --git a/libtransmission/torrent-ctor.c b/libtransmission/torrent-ctor.c index 123d6953a..02c2c6f73 100644 --- a/libtransmission/torrent-ctor.c +++ b/libtransmission/torrent-ctor.c @@ -291,6 +291,12 @@ tr_ctorGetMetainfo( const tr_ctor * ctor, return err; } +tr_session* +tr_ctorGetSession( const tr_ctor * ctor ) +{ + return (tr_session*) ctor->session; +} + /*** **** ***/ @@ -301,9 +307,11 @@ tr_ctorNew( const tr_session * session ) tr_ctor * ctor = tr_new0( struct tr_ctor, 1 ); ctor->session = session; - tr_ctorSetPeerLimit( ctor, TR_FALLBACK, session->peerLimitPerTorrent ); tr_ctorSetPaused( ctor, TR_FALLBACK, FALSE ); - tr_ctorSetDownloadDir( ctor, TR_FALLBACK, session->downloadDir ); + if( session != NULL ) { + tr_ctorSetPeerLimit( ctor, TR_FALLBACK, session->peerLimitPerTorrent ); + tr_ctorSetDownloadDir( ctor, TR_FALLBACK, session->downloadDir ); + } tr_ctorSetSave( ctor, TRUE ); return ctor; } @@ -316,4 +324,3 @@ tr_ctorFree( tr_ctor * ctor ) tr_free( ctor->optionalArgs[0].downloadDir ); tr_free( ctor ); } - diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 5b548f135..02106cac1 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -503,9 +503,7 @@ getBlockSize( uint32_t pieceSize ) } static void -torrentRealInit( tr_session * session, - tr_torrent * tor, - const tr_ctor * ctor ) +torrentRealInit( tr_torrent * tor, const tr_ctor * ctor ) { int doStart; uint64_t loaded; @@ -513,6 +511,9 @@ torrentRealInit( tr_session * session, const char * dir; static int nextUniqueId = 1; tr_info * info = &tor->info; + tr_session * session = tr_ctorGetSession( ctor ); + + assert( session != NULL ); tr_globalLock( session ); @@ -647,14 +648,14 @@ torrentRealInit( tr_session * session, } int -tr_torrentParse( const tr_session * session, - const tr_ctor * ctor, +tr_torrentParse( const tr_ctor * ctor, tr_info * setmeInfo ) { int err = 0; int doFree; tr_info tmp; const tr_benc * metainfo; + tr_session * session = tr_ctorGetSession( ctor ); if( setmeInfo == NULL ) setmeInfo = &tmp; @@ -669,7 +670,7 @@ tr_torrentParse( const tr_session * session, if( !err && !getBlockSize( setmeInfo->pieceSize ) ) err = TR_EINVALID; - if( !err && tr_torrentExists( session, setmeInfo->hash ) ) + if( !err && session && tr_torrentExists( session, setmeInfo->hash ) ) err = TR_EDUPLICATE; if( doFree ) @@ -679,20 +680,22 @@ tr_torrentParse( const tr_session * session, } tr_torrent * -tr_torrentNew( tr_session * session, - const tr_ctor * ctor, +tr_torrentNew( const tr_ctor * ctor, int * setmeError ) { int err; tr_info tmpInfo; tr_torrent * tor = NULL; - err = tr_torrentParse( session, ctor, &tmpInfo ); + assert( ctor != NULL ); + assert( tr_isSession( tr_ctorGetSession( ctor ) ) ); + + err = tr_torrentParse( ctor, &tmpInfo ); if( !err ) { tor = tr_new0( tr_torrent, 1 ); tor->info = tmpInfo; - torrentRealInit( session, tor, ctor ); + torrentRealInit( tor, ctor ); } else if( setmeError ) { diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index dc768f95e..12f8edc53 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -754,7 +754,9 @@ tr_ctorMode; struct tr_benc; -tr_ctor* tr_ctorNew( const tr_session * session ); +/* it's okay to use NULL here if you're only parsing the torrent. + * @see tr_torrentParse() */ +tr_ctor* tr_ctorNew( const tr_session * session_or_NULL ); void tr_ctorFree( tr_ctor * ctor ); @@ -808,6 +810,8 @@ int tr_ctorGetMetainfo( const tr_ctor * ctor, int tr_ctorGetDeleteSource( const tr_ctor * ctor, tr_bool * setmeDoDelete ); +tr_session* tr_ctorGetSession( const tr_ctor * ctor ); + /* returns NULL if tr_ctorSetMetainfoFromFile() wasn't used */ const char* tr_ctorGetSourceFile( const tr_ctor * ctor ); @@ -825,10 +829,12 @@ const char* tr_ctorGetSourceFile( const tr_ctor * ctor ); * (that is, if TR_EINVALID is not returned), then the parsed * metainfo is stored in setme_info and should be freed by the * caller via tr_metainfoFree(). + * + * If the constructor's session variable is NULL, + * info.torrent will be NULL and the duplicate check will not be performed. */ -int tr_torrentParse( const tr_session * session, - const tr_ctor * ctor, - tr_info * setme_info_or_NULL ); +int tr_torrentParse( const tr_ctor * ctor, + tr_info * setme_info_or_NULL ); /** @brief free a metainfo @see tr_torrentParse */ @@ -839,8 +845,7 @@ void tr_metainfoFree( tr_info * inf ); @return 0 on success, TR_EINVALID if the torrent couldn't be parsed, or TR_EDUPLICATE if there's already a matching torrent object. */ -tr_torrent * tr_torrentNew( tr_session * session, - const tr_ctor * ctor, +tr_torrent * tr_torrentNew( const tr_ctor * ctor, int * setmeError ); /** @} */ diff --git a/macosx/Controller.m b/macosx/Controller.m index c1c930190..bf2427481 100644 --- a/macosx/Controller.m +++ b/macosx/Controller.m @@ -793,7 +793,7 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy //ensure torrent doesn't already exist tr_ctor * ctor = tr_ctorNew(fLib); tr_ctorSetMetainfoFromFile(ctor, [torrentPath UTF8String]); - int result = tr_torrentParse(fLib, ctor, &info); + int result = tr_torrentParse(ctor, &info); if (result != TR_OK) { if (result == TR_EDUPLICATE) @@ -2457,7 +2457,7 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy tr_ctor * ctor = tr_ctorNew(fLib); tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]); - switch (tr_torrentParse(fLib, ctor, NULL)) + switch (tr_torrentParse(ctor, NULL)) { case TR_OK: [self openFiles: [NSArray arrayWithObject: file] addType: ADD_AUTO forcePath: nil]; @@ -2697,7 +2697,7 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy { tr_ctor * ctor = tr_ctorNew(fLib); tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]); - switch (tr_torrentParse(fLib, ctor, NULL)) + switch (tr_torrentParse(ctor, NULL)) { case TR_OK: if (!fOverlayWindow) @@ -2761,7 +2761,7 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy { tr_ctor * ctor = tr_ctorNew(fLib); tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]); - switch (tr_torrentParse(fLib, ctor, NULL)) + switch (tr_torrentParse(ctor, NULL)) { case TR_OK: [filesToOpen addObject: file]; diff --git a/macosx/DragOverlayWindow.m b/macosx/DragOverlayWindow.m index c0a444598..b49a16c18 100644 --- a/macosx/DragOverlayWindow.m +++ b/macosx/DragOverlayWindow.m @@ -88,7 +88,7 @@ tr_ctor * ctor = tr_ctorNew(fLib); tr_ctorSetMetainfoFromFile(ctor, [file UTF8String]); tr_info info; - if (tr_torrentParse(fLib, ctor, &info) == TR_OK) + if (tr_torrentParse(ctor, &info) == TR_OK) { count++; size += info.totalSize; diff --git a/macosx/Torrent.m b/macosx/Torrent.m index b304bb3a0..8bf4e82ab 100644 --- a/macosx/Torrent.m +++ b/macosx/Torrent.m @@ -1623,26 +1623,26 @@ int trashDataFile(const char * filename) if (hashString) { tr_ctorSetMetainfoFromHash(ctor, [hashString UTF8String]); - if (tr_torrentParse(lib, ctor, &info) == TR_OK) + if (tr_torrentParse(ctor, &info) == TR_OK) { NSString * currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]] ? fIncompleteFolder : fDownloadFolder; tr_ctorSetDownloadDir(ctor, TR_FORCE, [currentDownloadFolder UTF8String]); - fHandle = tr_torrentNew(lib, ctor, NULL); + fHandle = tr_torrentNew(ctor, NULL); } tr_metainfoFree(&info); } if (!fHandle && path) { tr_ctorSetMetainfoFromFile(ctor, [path UTF8String]); - if (tr_torrentParse(lib, ctor, &info) == TR_OK) + if (tr_torrentParse(ctor, &info) == TR_OK) { NSString * currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]] ? fIncompleteFolder : fDownloadFolder; tr_ctorSetDownloadDir(ctor, TR_FORCE, [currentDownloadFolder UTF8String]); - fHandle = tr_torrentNew(lib, ctor, NULL); + fHandle = tr_torrentNew(ctor, NULL); } tr_metainfoFree(&info); }