]> granicus.if.org Git - transmission/commitdiff
new utility for iterating through torrents: tr_torrentNext()
authorCharles Kerr <charles@transmissionbt.com>
Sat, 10 May 2008 00:19:00 +0000 (00:19 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Sat, 10 May 2008 00:19:00 +0000 (00:19 +0000)
libtransmission/port-forwarding.c
libtransmission/session.c
libtransmission/torrent.c
libtransmission/torrent.h
libtransmission/transmission.h

index 44d371333acc5d80c537f651b29fcb0b5da6df7b..5e4f6e3a1fd3ff64da6f73c5a310dc19c4921fea 100644 (file)
@@ -205,11 +205,11 @@ tr_sharedShuttingDown( tr_shared * s )
 void
 tr_sharedSetPort( tr_shared * s, int port )
 {
-    tr_torrent * tor;
+    tr_torrent * tor = NULL;
 
     s->publicPort = port;
 
-    for( tor = s->h->torrentList; tor; tor = tor->next )
+    while(( tor = tr_torrentNext( s->h, tor )))
         tr_torrentChangeMyPort( tor );
 }
 
index 5135248e1424ba9a072ccfd5c98b2eb1acc93a6a..cf99990811dd151e2b9f445792b5570c12014c33 100644 (file)
@@ -388,16 +388,13 @@ static void
 tr_closeAllConnections( void * vh )
 {
     tr_handle * h = vh;
-    tr_torrent * t;
+    tr_torrent * tor;
 
     tr_sharedShuttingDown( h->shared );
     tr_trackerShuttingDown( h );
 
-    for( t=h->torrentList; t!=NULL; ) {
-        tr_torrent * tmp = t;
-        t = t->next;
-        tr_torrentClose( tmp );
-    }
+    while(( tor = tr_torrentNext( h, NULL )))
+        tr_torrentClose( tor );
 
     tr_peerMgrFree( h->peerMgr );
 
@@ -682,3 +679,9 @@ tr_sessionSetTorrentFile( tr_handle    * h,
         metainfoLookupResort( h );
     }
 }
+
+tr_torrent*
+tr_torrentNext( tr_handle * session, tr_torrent * tor )
+{
+    return tor ? tor->next : session->torrentList;
+}
index 4568f2ed5435043a00800b887b888aa364ab33fa..060cccf51b252a416bbee517c1f0e571a23a950a 100644 (file)
 ***/
 
 int
-tr_torrentExists( tr_handle       * handle,
+tr_torrentExists( const tr_handle * handle,
                   const uint8_t   * torrentHash )
 {
-    return tr_torrentFindFromHash( handle, torrentHash ) != NULL;
+    return tr_torrentFindFromHash( (tr_handle*)handle, torrentHash ) != NULL;
 }
 
 tr_torrent*
 tr_torrentFindFromHash( tr_handle      * handle,
                         const uint8_t  * torrentHash )
 {
-    tr_torrent * tor;
+    tr_torrent * tor = NULL;
 
-    for( tor = handle->torrentList; tor; tor = tor->next )
+    while(( tor = tr_torrentNext( handle, tor )))
         if( !memcmp( tor->info.hash, torrentHash, SHA_DIGEST_LENGTH ) )
             return tor;
 
@@ -77,9 +77,9 @@ tr_torrent*
 tr_torrentFindFromObfuscatedHash( tr_handle      * handle,
                                   const uint8_t  * obfuscatedTorrentHash )
 {
-    tr_torrent * tor;
+    tr_torrent * tor = NULL;
 
-    for( tor = handle->torrentList; tor; tor = tor->next )
+    while(( tor = tr_torrentNext( handle, tor )))
         if( !memcmp( tor->obfuscatedHash, obfuscatedTorrentHash, SHA_DIGEST_LENGTH ) )
             return tor;
 
@@ -482,19 +482,6 @@ torrentRealInit( tr_handle     * h,
         torrentStart( tor, FALSE );
 }
 
-static int
-hashExists( const tr_handle   * h,
-            const uint8_t     * hash )
-{
-    const tr_torrent * tor;
-
-    for( tor=h->torrentList; tor; tor=tor->next )
-        if( !memcmp( hash, tor->info.hash, SHA_DIGEST_LENGTH ) )
-            return TRUE;
-
-    return FALSE;
-}
-
 int
 tr_torrentParse( const tr_handle  * handle,
                  const tr_ctor    * ctor,
@@ -515,7 +502,7 @@ tr_torrentParse( const tr_handle  * handle,
     err = tr_metainfoParse( handle, setmeInfo, metainfo );
     doFree = !err && ( setmeInfo == &tmp );
 
-    if( !err && hashExists( handle, setmeInfo->hash ) )
+    if( !err && tr_torrentExists( handle, setmeInfo->hash ) )
         err = TR_EDUPLICATE;
 
     if( doFree )
index 367b8596ae899c8972e3c8d919a003e3e9bd29fa..7c60ecdc4c2937d0146528de0973ff90f962248c 100644 (file)
@@ -48,7 +48,7 @@ int  tr_torrentIsSeed  ( const tr_torrent * );
 
 void tr_torrentChangeMyPort  ( tr_torrent * );
 
-int tr_torrentExists( tr_handle *, const uint8_t * );
+int tr_torrentExists( const tr_handle *, const uint8_t * );
 tr_torrent* tr_torrentFindFromHash( tr_handle *, const uint8_t * );
 tr_torrent* tr_torrentFindFromObfuscatedHash( tr_handle *, const uint8_t* );
 
index 9727cc9cdd335038d93dc6f48d3ab6b9f7314bb7..2b33ed253b76edf9ed988755009e0b01d552a4c4 100644 (file)
@@ -248,6 +248,12 @@ typedef struct tr_torrent tr_torrent;
 
 int tr_torrentCount( const tr_handle * h );
 
+/**
+ * Iterate through the torrents.
+ * Pass in in a NULL pointer to get the first torrent.
+ */
+tr_torrent* tr_torrentNext( tr_handle *, tr_torrent * );
+
 /***********************************************************************
 *** Speed Limits
 **/