(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().

This commit is contained in:
Charles Kerr 2009-04-02 17:30:29 +00:00
parent e649e80fe5
commit c3b24bc471
15 changed files with 55 additions and 41 deletions

View File

@ -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 )
{

View File

@ -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 );

View File

@ -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 );

View File

@ -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 )
{

View File

@ -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 )
{

View File

@ -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 );

View File

@ -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;
}

View File

@ -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 );

View File

@ -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 ) );

View File

@ -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 );
}

View File

@ -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 )
{

View File

@ -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 );
/** @} */

View File

@ -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];

View File

@ -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;

View File

@ -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);
}