Changeset 2207
- Timestamp:
- Jun 27, 2007, 2:54:31 PM (16 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/choking.c
r2202 r2207 30 30 #endif 31 31 32 /* We may try to allocate and free tables of size 0.33 Quick and dirty way to handle it... */34 #define malloc tr_malloc35 #define free tr_free36 37 32 struct tr_choking_s 38 33 { … … 46 41 tr_choking_t * c; 47 42 48 c = calloc( 1, sizeof( tr_choking_t ));43 c = tr_new0( tr_choking_t, 1 ); 49 44 c->h = h; 50 45 c->slots = 4242; … … 93 88 if( *zeroCount && ( shuffle = tr_rand( *zeroCount ) ) ) 94 89 { 95 tr_peer_t ** bak; 96 bak = malloc( shuffle * sizeof( tr_peer_t * ) ); 90 tr_peer_t ** bak = tr_new( tr_peer_t*, shuffle );; 97 91 memcpy( bak, zero, shuffle * sizeof( tr_peer_t * ) ); 98 92 memmove( zero, &zero[shuffle], … … 100 94 memcpy( &zero[*zeroCount - shuffle], bak, 101 95 shuffle * sizeof( tr_peer_t * ) ); 102 free( bak );96 tr_free( bak ); 103 97 } 104 98 … … 150 144 } 151 145 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 ); 154 148 canChokeCount = 0; 155 149 canUnchokeCount = 0; … … 213 207 } 214 208 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 ); 219 213 220 214 sortPeersDescending( canChoke, canChokeCount, … … 225 219 canUnchokeNonZero, &canUnchokeNonZeroCount); 226 220 227 free( canChoke );228 free( canUnchoke );221 tr_free( canChoke ); 222 tr_free( canUnchoke ); 229 223 230 224 if( mustOptimistic ) … … 304 298 } 305 299 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 ); 310 304 311 305 /* Unlock all torrents */ … … 319 313 { 320 314 tr_lockClose( &c->lock ); 321 free( c );322 } 315 tr_free( c ); 316 } -
trunk/libtransmission/completion.c
r2187 r2207 52 52 tr_completion_t * cp; 53 53 54 cp = malloc( sizeof( tr_completion_t ));54 cp = tr_new( tr_completion_t, 1 ); 55 55 cp->tor = tor; 56 56 cp->blockBitfield = tr_bitfieldNew( tor->blockCount ); 57 cp->blockDownloaders = malloc(tor->blockCount );57 cp->blockDownloaders = tr_new( uint8_t, tor->blockCount ); 58 58 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 ); 61 61 62 62 cp->nBlocksInLastPiece = tr_pieceCountBlocks ( tor->info.pieceCount - 1 ); … … 71 71 void tr_cpClose( tr_completion_t * cp ) 72 72 { 73 tr_free( cp->completeBlocks ); 74 tr_free( cp->missingBlocks ); 75 tr_bitfieldFree( cp->pieceBitfield ); 76 tr_free( cp->blockDownloaders ); 73 77 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 ); 79 79 } 80 80 … … 264 264 end = start + count; 265 265 266 pool = malloc( count * sizeof( int ));266 pool = tr_new( int, count ); 267 267 poolSize = 0; 268 268 min = 255; … … 295 295 } 296 296 297 free( pool );297 tr_free( pool ); 298 298 return ret; 299 299 } -
trunk/libtransmission/metainfo.c
r2157 r2207 30 30 * Local prototypes 31 31 **********************************************************************/ 32 static int realparse( tr_info_t * inf, uint8_t * buf, size_t len );32 static int realparse( tr_info_t * inf, const uint8_t * buf, size_t len ); 33 33 static void savedname( char * name, size_t len, const char * hash, 34 34 const char * tag ); … … 60 60 if( NULL == buf ) 61 61 { 62 return 1;62 return TR_EINVALID; 63 63 } 64 64 … … 66 66 { 67 67 free( buf ); 68 return 1;68 return TR_EINVALID; 69 69 } 70 70 … … 74 74 { 75 75 free( buf ); 76 return 1;76 return TR_EINVALID; 77 77 } 78 78 savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag ); … … 85 85 free( buf ); 86 86 87 return 0;87 return TR_OK; 88 88 } 89 89 90 90 int 91 91 tr_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 ) 93 93 { 94 94 if( realparse( inf, data, size ) ) 95 95 { 96 return 1;96 return TR_EINVALID; 97 97 } 98 98 … … 101 101 if( savetorrent( inf->hashString, tag, data, size ) ) 102 102 { 103 return 1;103 return TR_EINVALID; 104 104 } 105 105 savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag ); 106 106 } 107 107 108 return 0;108 return TR_OK; 109 109 } 110 110 … … 133 133 if( NULL == buf ) 134 134 { 135 return 1;135 return TR_EINVALID; 136 136 } 137 137 … … 139 139 { 140 140 free( buf ); 141 return 1;141 return TR_EINVALID; 142 142 } 143 143 … … 148 148 { 149 149 free( buf ); 150 return 1;150 return TR_EINVALID; 151 151 } 152 152 savedname( inf->torrent, sizeof inf->torrent, hash, tag ); … … 155 155 free( buf ); 156 156 157 return 0;157 return TR_OK; 158 158 } 159 159 160 160 int 161 realparse( tr_info_t * inf, uint8_t * buf, size_t size )161 realparse( tr_info_t * inf, const uint8_t * buf, size_t size ) 162 162 { 163 163 benc_val_t meta, * beInfo, * val, * val2; … … 168 168 { 169 169 tr_err( "Error while parsing bencoded data" ); 170 return 1;170 return TR_EINVALID; 171 171 } 172 172 … … 177 177 tr_err( "%s \"info\" dictionary", ( beInfo ? "Invalid" : "Missing" ) ); 178 178 tr_bencFree( &meta ); 179 return 1;179 return TR_EINVALID; 180 180 } 181 181 SHA1( (uint8_t *) beInfo->begin, … … 274 274 275 275 tr_bencFree( &meta ); 276 return 0;276 return TR_OK; 277 277 278 278 fail: 279 279 tr_metainfoFree( inf ); 280 280 tr_bencFree( &meta ); 281 return 1;281 return TR_EINVALID; 282 282 } 283 283 … … 311 311 if( TYPE_LIST != name->type ) 312 312 { 313 return 1;313 return TR_EINVALID; 314 314 } 315 315 … … 317 317 if( NULL == list ) 318 318 { 319 return 1;319 return TR_EINVALID; 320 320 } 321 321 … … 343 343 if( 0 == jj ) 344 344 { 345 return 1;345 return TR_EINVALID; 346 346 } 347 347 … … 354 354 free( list ); 355 355 356 return 0;356 return TR_OK; 357 357 } 358 358 … … 458 458 { 459 459 tr_err( "No \"announce\" entry" ); 460 return 1;460 return TR_EINVALID; 461 461 } 462 462 … … 465 465 { 466 466 tr_err( "Invalid announce URL (%s)", val->val.s.s ); 467 return 1;467 return TR_EINVALID; 468 468 } 469 469 … … 479 479 } 480 480 481 return 0;481 return TR_OK; 482 482 } 483 483 … … 615 615 { 616 616 tr_err( "Could not open file (%s) (%s)", path, strerror( errno ) ); 617 return 1;617 return TR_EINVALID; 618 618 } 619 619 fseek( file, 0, SEEK_SET ); … … 622 622 tr_err( "Could not write file (%s) (%s)", path, strerror( errno ) ); 623 623 fclose( file ); 624 return 1;624 return TR_EINVALID; 625 625 } 626 626 fclose( file ); 627 627 628 return 0;628 return TR_OK; 629 629 } 630 630 … … 639 639 { 640 640 tr_err( "%s \"name\" string", ( name ? "Invalid" : "Missing" ) ); 641 return 1;641 return TR_EINVALID; 642 642 } 643 643 … … 646 646 { 647 647 tr_err( "Invalid \"name\" string" ); 648 return 1;648 return TR_EINVALID; 649 649 } 650 650 inf->totalSize = 0; … … 659 659 if( NULL == inf->files ) 660 660 { 661 return 1;661 return TR_EINVALID; 662 662 } 663 663 … … 671 671 tr_err( "%s \"path\" entry", 672 672 ( path ? "Invalid" : "Missing" ) ); 673 return 1;673 return TR_EINVALID; 674 674 } 675 675 length = tr_bencDictFind( item, "length" ); … … 678 678 tr_err( "%s \"length\" entry", 679 679 ( length ? "Invalid" : "Missing" ) ); 680 return 1;680 return TR_EINVALID; 681 681 } 682 682 inf->files[ii].length = length->val.i; … … 693 693 if( NULL == inf->files ) 694 694 { 695 return 1;695 return TR_EINVALID; 696 696 } 697 697 … … 709 709 } 710 710 711 return 0;711 return TR_OK; 712 712 } 713 713 -
trunk/libtransmission/metainfo.h
r1636 r2207 31 31 const char * path, int save ); 32 32 int 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 ); 34 34 int tr_metainfoParseHash( tr_info_t *, const char * tag, const char * hash ); 35 35 void tr_metainfoFree( tr_info_t * inf ); -
trunk/libtransmission/torrent.c
r2206 r2207 277 277 278 278 int 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 ); 279 tr_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; 289 297 } 290 298 … … 297 305 { 298 306 int val; 307 int err = 0; 299 308 tr_torrent_t * tor = NULL; 300 309 301 if(( val = tr_torrent CanAdd( h, destination, path )))310 if(( val = tr_torrentParse( h, destination, path, NULL, &err ))) 302 311 *error = val; 312 else if( err ) 313 *error = err; 303 314 else if(!(( tor = tr_new0( tr_torrent_t, 1 )))) 304 315 *error = TR_EOTHER; … … 312 323 313 324 static 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 } 325 tr_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 325 345 326 346 tr_torrent_t * 327 tr_torrentInitSaved( tr_handle_t * h,328 const char * hashStr,329 const char * destination,330 int flags,331 int * error )347 tr_torrentInitSaved( tr_handle_t * h, 348 const char * hashStr, 349 const char * destination, 350 int flags, 351 int * error ) 332 352 { 333 353 int val; 354 int err = 0; 334 355 tr_torrent_t * tor = NULL; 335 356 336 if(( val = tr_torrent CanAddHash( h, destination, hashStr )))357 if(( val = tr_torrentParseHash( h, hashStr, destination, NULL, &err ))) 337 358 *error = val; 359 else if( err ) 360 *error = err; 338 361 else if(!(( tor = tr_new0( tr_torrent_t, 1 )))) 339 362 *error = TR_EOTHER; … … 347 370 348 371 static 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 ); 372 tr_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; 360 391 } 361 392 362 393 tr_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 )394 tr_torrentInitData( tr_handle_t * h, 395 const uint8_t * data, 396 size_t size, 397 const char * destination, 398 int flags, 399 int * error ) 369 400 { 370 401 int val; 402 int err = 0; 371 403 tr_torrent_t * tor = NULL; 372 404 373 if(( val = tr_torrent CanAddData( h, destination, data, size) ))405 if(( val = tr_torrentParseData( h, data, size, destination, NULL, &err ) )) 374 406 *error = val; 407 else if( err ) 408 *error = err; 375 409 else if(!(( tor = tr_new0( tr_torrent_t, 1 )))) 376 410 *error = TR_EOTHER; -
trunk/libtransmission/transmission.h
r2206 r2207 264 264 #define TR_EDUPLICATE 3 265 265 #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 ); 266 tr_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 272 typedef struct tr_info_s tr_info_t; 270 273 271 274 /** 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. 275 288 */ 276 int tr_torrentCanAdd( const tr_handle_t * handle, 277 const char * destination, 278 const char * path ); 289 int 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 ); 279 294 280 295 /*********************************************************************** … … 285 300 **********************************************************************/ 286 301 tr_torrent_t * tr_torrentInitData( tr_handle_t *, 287 uint8_t * data, size_t size,302 const uint8_t * data, size_t size, 288 303 const char * destination, 289 304 int flags, int * error ); … … 315 330 * Return torrent metainfo. 316 331 **********************************************************************/ 317 typedef struct tr_info_s tr_info_t;318 332 tr_info_t * tr_torrentInfo( tr_torrent_t * ); 319 333
Note: See TracChangeset
for help on using the changeset viewer.