Changeset 2207


Ignore:
Timestamp:
Jun 27, 2007, 2:54:31 PM (16 years ago)
Author:
charles
Message:

Modified tr_torrentCanAdd() to give the new functionality BMW suggests

Location:
trunk/libtransmission
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/choking.c

    r2202 r2207  
    3030#endif
    3131
    32 /* We may try to allocate and free tables of size 0.
    33    Quick and dirty way to handle it... */
    34 #define malloc tr_malloc
    35 #define free   tr_free
    36 
    3732struct tr_choking_s
    3833{
     
    4641    tr_choking_t * c;
    4742
    48     c        = calloc( 1, sizeof( tr_choking_t ) );
     43    c        = tr_new0( tr_choking_t, 1 );
    4944    c->h     = h;
    5045    c->slots = 4242;
     
    9388    if( *zeroCount && ( shuffle = tr_rand( *zeroCount ) ) )
    9489    {
    95         tr_peer_t ** bak;
    96         bak = malloc( shuffle * sizeof( tr_peer_t * ) );
     90        tr_peer_t ** bak = tr_new( tr_peer_t*, shuffle );;
    9791        memcpy( bak, zero, shuffle * sizeof( tr_peer_t * ) );
    9892        memmove( zero, &zero[shuffle],
     
    10094        memcpy( &zero[*zeroCount - shuffle], bak,
    10195                 shuffle * sizeof( tr_peer_t * ) );
    102         free( bak );
     96        tr_free( bak );
    10397    }
    10498
     
    150144    }
    151145
    152     canChoke   = malloc( peersTotalCount * sizeof( tr_peer_t * ) );
    153     canUnchoke = malloc( peersTotalCount * sizeof( tr_peer_t * ) );
     146    canChoke   = tr_new( tr_peer_t*, peersTotalCount );
     147    canUnchoke = tr_new( tr_peer_t*, peersTotalCount );
    154148    canChokeCount   = 0;
    155149    canUnchokeCount = 0;
     
    213207    }
    214208
    215     canChokeZero      = malloc( canChokeCount * sizeof( tr_peer_t * ) );
    216     canChokeNonZero   = malloc( canChokeCount * sizeof( tr_peer_t * ) );
    217     canUnchokeZero    = malloc( canUnchokeCount * sizeof( tr_peer_t * ) );
    218     canUnchokeNonZero = malloc( canUnchokeCount * sizeof( tr_peer_t * ) );
     209    canChokeZero      = tr_new( tr_peer_t*, canChokeCount );
     210    canChokeNonZero   = tr_new( tr_peer_t*, canChokeCount );
     211    canUnchokeZero    = tr_new( tr_peer_t*, canUnchokeCount );
     212    canUnchokeNonZero = tr_new( tr_peer_t*, canUnchokeCount );
    219213
    220214    sortPeersDescending( canChoke, canChokeCount,
     
    225219                        canUnchokeNonZero, &canUnchokeNonZeroCount);
    226220
    227     free( canChoke );
    228     free( canUnchoke );
     221    tr_free( canChoke );
     222    tr_free( canUnchoke );
    229223
    230224    if( mustOptimistic )
     
    304298    }
    305299
    306     free( canChokeZero );
    307     free( canChokeNonZero );
    308     free( canUnchokeZero );
    309     free( canUnchokeNonZero );
     300    tr_free( canChokeZero );
     301    tr_free( canChokeNonZero );
     302    tr_free( canUnchokeZero );
     303    tr_free( canUnchokeNonZero );
    310304
    311305    /* Unlock all torrents */
     
    319313{
    320314    tr_lockClose( &c->lock );
    321     free( c );
    322 }
     315    tr_free( c );
     316}
  • trunk/libtransmission/completion.c

    r2187 r2207  
    5252    tr_completion_t * cp;
    5353
    54     cp                   = malloc( sizeof( tr_completion_t ) );
     54    cp                   = tr_new( tr_completion_t, 1 );
    5555    cp->tor              = tor;
    5656    cp->blockBitfield    = tr_bitfieldNew( tor->blockCount );
    57     cp->blockDownloaders = malloc( tor->blockCount );
     57    cp->blockDownloaders = tr_new( uint8_t, tor->blockCount );
    5858    cp->pieceBitfield    = tr_bitfieldNew( tor->info.pieceCount );
    59     cp->missingBlocks    = malloc( tor->info.pieceCount * sizeof( int ) );
    60     cp->completeBlocks   = malloc( tor->info.pieceCount * sizeof( int ) );
     59    cp->missingBlocks    = tr_new( int, tor->info.pieceCount );
     60    cp->completeBlocks   = tr_new( int, tor->info.pieceCount );
    6161
    6262    cp->nBlocksInLastPiece = tr_pieceCountBlocks ( tor->info.pieceCount - 1 );
     
    7171void tr_cpClose( tr_completion_t * cp )
    7272{
     73    tr_free(         cp->completeBlocks );
     74    tr_free(         cp->missingBlocks );
     75    tr_bitfieldFree( cp->pieceBitfield );
     76    tr_free(         cp->blockDownloaders );
    7377    tr_bitfieldFree( cp->blockBitfield );
    74     free(            cp->blockDownloaders );
    75     tr_bitfieldFree( cp->pieceBitfield );
    76     free(            cp->missingBlocks );
    77     free(            cp->completeBlocks );
    78     free(            cp );
     78    tr_free(         cp );
    7979}
    8080
     
    264264    end   = start + count;
    265265
    266     pool     = malloc( count * sizeof( int ) );
     266    pool     = tr_new( int, count );
    267267    poolSize = 0;
    268268    min      = 255;
     
    295295    }
    296296
    297     free( pool );
     297    tr_free( pool );
    298298    return ret;
    299299}
  • trunk/libtransmission/metainfo.c

    r2157 r2207  
    3030 * Local prototypes
    3131 **********************************************************************/
    32 static int realparse( tr_info_t * inf, uint8_t * buf, size_t len );
     32static int realparse( tr_info_t * inf, const uint8_t * buf, size_t len );
    3333static void savedname( char * name, size_t len, const char * hash,
    3434                       const char * tag );
     
    6060    if( NULL == buf )
    6161    {
    62         return 1;
     62        return TR_EINVALID;
    6363    }
    6464
     
    6666    {
    6767        free( buf );
    68         return 1;
     68        return TR_EINVALID;
    6969    }
    7070
     
    7474        {
    7575            free( buf );
    76             return 1;
     76            return TR_EINVALID;
    7777        }
    7878        savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );
     
    8585    free( buf );
    8686
    87     return 0;
     87    return TR_OK;
    8888}
    8989
    9090int
    9191tr_metainfoParseData( tr_info_t * inf, const char * tag,
    92                       uint8_t * data, size_t size, int save )
     92                      const uint8_t * data, size_t size, int save )
    9393{
    9494    if( realparse( inf, data, size ) )
    9595    {
    96         return 1;
     96        return TR_EINVALID;
    9797    }
    9898
     
    101101        if( savetorrent( inf->hashString, tag, data, size ) )
    102102        {
    103             return 1;
     103            return TR_EINVALID;
    104104        }
    105105        savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );
    106106    }
    107107
    108     return 0;
     108    return TR_OK;
    109109}
    110110
     
    133133    if( NULL == buf )
    134134    {
    135         return 1;
     135        return TR_EINVALID;
    136136    }
    137137
     
    139139    {
    140140        free( buf );
    141         return 1;
     141        return TR_EINVALID;
    142142    }
    143143
     
    148148        {
    149149            free( buf );
    150             return 1;
     150            return TR_EINVALID;
    151151        }
    152152        savedname( inf->torrent, sizeof inf->torrent, hash, tag );
     
    155155    free( buf );
    156156
    157     return 0;
     157    return TR_OK;
    158158}
    159159
    160160int
    161 realparse( tr_info_t * inf, uint8_t * buf, size_t size )
     161realparse( tr_info_t * inf, const uint8_t * buf, size_t size )
    162162{
    163163    benc_val_t   meta, * beInfo, * val, * val2;
     
    168168    {
    169169        tr_err( "Error while parsing bencoded data" );
    170         return 1;
     170        return TR_EINVALID;
    171171    }
    172172
     
    177177        tr_err( "%s \"info\" dictionary", ( beInfo ? "Invalid" : "Missing" ) );
    178178        tr_bencFree( &meta );
    179         return 1;
     179        return TR_EINVALID;
    180180    }
    181181    SHA1( (uint8_t *) beInfo->begin,
     
    274274
    275275    tr_bencFree( &meta );
    276     return 0;
     276    return TR_OK;
    277277
    278278  fail:
    279279    tr_metainfoFree( inf );
    280280    tr_bencFree( &meta );
    281     return 1;
     281    return TR_EINVALID;
    282282}
    283283
     
    311311    if( TYPE_LIST != name->type )
    312312    {
    313         return 1;
     313        return TR_EINVALID;
    314314    }
    315315
     
    317317    if( NULL == list )
    318318    {
    319         return 1;
     319        return TR_EINVALID;
    320320    }
    321321
     
    343343    if( 0 == jj )
    344344    {
    345         return 1;
     345        return TR_EINVALID;
    346346    }
    347347
     
    354354    free( list );
    355355
    356     return 0;
     356    return TR_OK;
    357357}
    358358
     
    458458        {
    459459            tr_err( "No \"announce\" entry" );
    460             return 1;
     460            return TR_EINVALID;
    461461        }
    462462
     
    465465        {
    466466            tr_err( "Invalid announce URL (%s)", val->val.s.s );
    467             return 1;
     467            return TR_EINVALID;
    468468        }
    469469
     
    479479    }
    480480
    481     return 0;
     481    return TR_OK;
    482482}
    483483
     
    615615    {
    616616        tr_err( "Could not open file (%s) (%s)", path, strerror( errno ) );
    617         return 1;
     617        return TR_EINVALID;
    618618    }
    619619    fseek( file, 0, SEEK_SET );
     
    622622        tr_err( "Could not write file (%s) (%s)", path, strerror( errno ) );
    623623        fclose( file );
    624         return 1;
     624        return TR_EINVALID;
    625625    }
    626626    fclose( file );
    627627
    628     return 0;
     628    return TR_OK;
    629629}
    630630
     
    639639    {
    640640        tr_err( "%s \"name\" string", ( name ? "Invalid" : "Missing" ) );
    641         return 1;
     641        return TR_EINVALID;
    642642    }
    643643
     
    646646    {
    647647        tr_err( "Invalid \"name\" string" );
    648         return 1;
     648        return TR_EINVALID;
    649649    }
    650650    inf->totalSize = 0;
     
    659659        if( NULL == inf->files )
    660660        {
    661             return 1;
     661            return TR_EINVALID;
    662662        }
    663663
     
    671671                tr_err( "%s \"path\" entry",
    672672                        ( path ? "Invalid" : "Missing" ) );
    673                 return 1;
     673                return TR_EINVALID;
    674674            }
    675675            length = tr_bencDictFind( item, "length" );
     
    678678                tr_err( "%s \"length\" entry",
    679679                        ( length ? "Invalid" : "Missing" ) );
    680                 return 1;
     680                return TR_EINVALID;
    681681            }
    682682            inf->files[ii].length = length->val.i;
     
    693693        if( NULL == inf->files )
    694694        {
    695             return 1;
     695            return TR_EINVALID;
    696696        }
    697697
     
    709709    }
    710710
    711     return 0;
     711    return TR_OK;
    712712}
    713713
  • trunk/libtransmission/metainfo.h

    r1636 r2207  
    3131                          const char * path, int save );
    3232int tr_metainfoParseData( tr_info_t *, const char * tag,
    33                           uint8_t * data, size_t size, int save );
     33                          const uint8_t * data, size_t size, int save );
    3434int tr_metainfoParseHash( tr_info_t *, const char * tag, const char * hash );
    3535void tr_metainfoFree( tr_info_t * inf );
  • trunk/libtransmission/torrent.c

    r2206 r2207  
    277277
    278278int
    279 tr_torrentCanAdd( const tr_handle_t  * h,
    280                   const char         * destination,
    281                   const char         * path )
    282 {
    283     tr_info_t info;
    284 
    285     if( tr_metainfoParseFile( &info, h->tag, path, FALSE ) )
    286         return TR_EINVALID;
    287 
    288     return infoCanAdd( h, destination, &info );
     279tr_torrentParse( const tr_handle_t  * h,
     280                 const char         * path,
     281                 const char         * destination,
     282                 tr_info_t          * setme_info,
     283                 int                * setme_canAdd )
     284{
     285    int ret;
     286    tr_info_t tmp;
     287
     288    if( setme_info == NULL )
     289        setme_info = &tmp;
     290
     291    ret = tr_metainfoParseFile( setme_info, h->tag, path, FALSE );
     292
     293    if( setme_canAdd )
     294       *setme_canAdd = ret ? ret : infoCanAdd( h, destination, setme_info );
     295
     296    return ret;
    289297}
    290298 
     
    297305{
    298306    int val;
     307    int err = 0;
    299308    tr_torrent_t * tor = NULL;
    300309
    301     if(( val = tr_torrentCanAdd( h, destination, path ) ))
     310    if(( val = tr_torrentParse( h, destination, path, NULL, &err )))
    302311        *error = val;
     312    else if( err )
     313        *error = err;
    303314    else if(!(( tor = tr_new0( tr_torrent_t, 1 ))))
    304315        *error = TR_EOTHER;
     
    312323
    313324static int
    314 tr_torrentCanAddHash( tr_handle_t * h,
    315                       const char  * destination,
    316                       const char  * hashStr )
    317 {
    318     tr_info_t info;
    319 
    320     if( tr_metainfoParseHash( &info, h->tag, hashStr ) )
    321         return TR_EINVALID;
    322 
    323     return infoCanAdd( h, destination, &info );
    324 }
     325tr_torrentParseHash( const tr_handle_t  * h,
     326                     const char         * hashStr,
     327                     const char         * destination,
     328                     tr_info_t          * setme_info,
     329                     int                * setme_canAdd )
     330{
     331    int ret;
     332    tr_info_t tmp;
     333
     334    if( setme_info == NULL )
     335        setme_info = &tmp;
     336
     337    ret = tr_metainfoParseHash( setme_info, h->tag, hashStr );
     338
     339    if( setme_canAdd )
     340       *setme_canAdd = ret ? ret : infoCanAdd( h, destination, setme_info );
     341
     342    return ret;
     343}
     344
    325345
    326346tr_torrent_t *
    327 tr_torrentInitSaved( tr_handle_t  * h,
    328                      const char   * hashStr,
    329                      const char   * destination,
    330                      int            flags,
    331                      int          * error )
     347tr_torrentInitSaved( tr_handle_t    * h,
     348                     const char     * hashStr,
     349                     const char     * destination,
     350                     int              flags,
     351                     int            * error )
    332352{
    333353    int val;
     354    int err = 0;
    334355    tr_torrent_t * tor = NULL;
    335356
    336     if(( val = tr_torrentCanAddHash( h, destination, hashStr ) ))
     357    if(( val = tr_torrentParseHash( h, hashStr, destination, NULL, &err )))
    337358        *error = val;
     359    else if( err )
     360        *error = err;
    338361    else if(!(( tor = tr_new0( tr_torrent_t, 1 ))))
    339362        *error = TR_EOTHER;
     
    347370
    348371static int
    349 tr_torrentCanAddData( tr_handle_t  * h,
    350                       const char   * destination,
    351                       uint8_t      * data,
    352                       size_t         size )
    353 {
    354     tr_info_t info;
    355 
    356     if( tr_metainfoParseData( &info, h->tag, data, size, FALSE ) )
    357         return TR_EINVALID;
    358 
    359     return infoCanAdd( h, destination, &info );
     372tr_torrentParseData( const tr_handle_t  * h,
     373                     const uint8_t      * data,
     374                     size_t               size,
     375                     const char         * destination,
     376                     tr_info_t          * setme_info,
     377                     int                * setme_canAdd )
     378{
     379    int ret;
     380    tr_info_t tmp;
     381
     382    if( setme_info == NULL )
     383        setme_info = &tmp;
     384
     385    ret = tr_metainfoParseData( setme_info, h->tag, data, size, FALSE );
     386
     387    if( setme_canAdd )
     388       *setme_canAdd = ret ? ret : infoCanAdd( h, destination, setme_info );
     389
     390    return ret;
    360391}
    361392
    362393tr_torrent_t *
    363 tr_torrentInitData( tr_handle_t  * h,
    364                     uint8_t      * data,
    365                     size_t         size,
    366                     const char   * destination,
    367                     int            flags,
    368                     int          * error )
     394tr_torrentInitData( tr_handle_t    * h,
     395                    const uint8_t  * data,
     396                    size_t           size,
     397                    const char     * destination,
     398                    int              flags,
     399                    int            * error )
    369400{
    370401    int val;
     402    int err = 0;
    371403    tr_torrent_t * tor = NULL;
    372404
    373     if(( val = tr_torrentCanAddData( h, destination, data, size ) ))
     405    if(( val = tr_torrentParseData( h, data, size, destination, NULL, &err ) ))
    374406        *error = val;
     407    else if( err )
     408        *error = err;
    375409    else if(!(( tor = tr_new0( tr_torrent_t, 1 ))))
    376410        *error = TR_EOTHER;
  • trunk/libtransmission/transmission.h

    r2206 r2207  
    264264#define TR_EDUPLICATE   3
    265265#define TR_EOTHER       666
    266 tr_torrent_t * tr_torrentInit( tr_handle_t *,
    267                                const char * path,
    268                                const char * destination,
    269                                int flags, int * error );
     266tr_torrent_t * tr_torrentInit( tr_handle_t  * handle,
     267                               const char   * metainfo_filename,
     268                               const char   * destination,
     269                               int            flags,
     270                               int          * setme_error );
     271
     272typedef struct tr_info_s tr_info_t;
    270273
    271274/**
    272  * Checks to see if the specified torrent file could be
    273  * successfully added to Transmission.
    274  * returns TR_OK, TR_EDUPLICATE, TR_ERROR_IO_DUP_DOWNLOAD, or TR_EINVALID.
     275 * Parses the specified metainfo file.
     276 *
     277 * Returns TR_OK or TR_INVALID based on whether or not the
     278 * metainfo file is successfully parsed.
     279 *
     280 * If parsing succeeded and "setme_info" is non-NULL,
     281 * it will be populated with the file's information.
     282 *
     283 * If "setme_canAdd" is non-NULL, it will be set to TR_OK,
     284 * TR_EDUPLICATE, TR_ERROR_IO_DUP_DOWNLOAD, or TR_EINVALID.
     285 *
     286 * "destination" is used for the canAdd tests, so it can
     287 * be NULL if "setme_canAdd" is too.
    275288 */
    276 int tr_torrentCanAdd( const tr_handle_t  * handle,
    277                       const char         * destination,
    278                       const char         * path );
     289int tr_torrentParse( const tr_handle_t  * handle,
     290                     const char         * metainfo_filename,
     291                     const char         * destination,
     292                     tr_info_t          * setme_info,
     293                     int                * setme_canAdd );
    279294
    280295/***********************************************************************
     
    285300 **********************************************************************/
    286301tr_torrent_t * tr_torrentInitData( tr_handle_t *,
    287                                    uint8_t * data, size_t size,
     302                                   const uint8_t * data, size_t size,
    288303                                   const char * destination,
    289304                                   int flags, int * error );
     
    315330 * Return torrent metainfo.
    316331 **********************************************************************/
    317 typedef struct tr_info_s tr_info_t;
    318332tr_info_t * tr_torrentInfo( tr_torrent_t * );
    319333
Note: See TracChangeset for help on using the changeset viewer.