Changeset 2206
- Timestamp:
- Jun 27, 2007, 5:14:38 AM (15 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cli/transmissioncli.c
r2202 r2206 155 155 156 156 /* Open and parse torrent file */ 157 if( !( tor = tr_torrentInit( h, torrentPath, ".", NULL,0, &error ) ) )157 if( !( tor = tr_torrentInit( h, torrentPath, ".", 0, &error ) ) ) 158 158 { 159 159 printf( "Failed opening torrent file `%s'\n", torrentPath ); -
trunk/daemon/torrents.c
r2204 r2206 503 503 { 504 504 tor->tor = tr_torrentInit( gl_handle, path, dir, 505 tor->hash,506 505 TR_FLAG_SAVE, &errcode ); 507 506 } … … 513 512 { 514 513 tor->tor = tr_torrentInitData( gl_handle, data, size, dir, 515 tor->hash,TR_FLAG_SAVE, &errcode );514 TR_FLAG_SAVE, &errcode ); 516 515 } 517 516 -
trunk/gtk/tr_torrent.c
r2202 r2206 313 313 flags |= TR_FLAG_PAUSED; 314 314 315 handle = tr_torrentInit( back, torrent, dir, NULL,flags, &errcode );315 handle = tr_torrentInit( back, torrent, dir, flags, &errcode ); 316 316 317 317 if(NULL == handle) { … … 355 355 356 356 errcode = -1; 357 handle = tr_torrentInitData( back, data, size, dir, NULL,flags, &errcode );357 handle = tr_torrentInitData( back, data, size, dir, flags, &errcode ); 358 358 359 359 if( NULL == handle ) … … 434 434 handle = tr_torrentInitSaved(back, hash, dir, flags, &errcode); 435 435 else 436 handle = tr_torrentInit(back, torrent, dir, NULL,flags, &errcode);436 handle = tr_torrentInit(back, torrent, dir, flags, &errcode); 437 437 438 438 if(NULL == handle) { -
trunk/libtransmission/makemeta.c
r2184 r2206 64 64 else if( S_ISREG( sb.st_mode ) ) 65 65 { 66 struct FileList * node = tr_ malloc( sizeof( struct FileList ));66 struct FileList * node = tr_new( struct FileList, 1 ); 67 67 node->size = sb.st_size; 68 68 node->filename = tr_strdup( buf ); … … 118 118 struct FileList * files; 119 119 struct FileList * walk; 120 tr_metainfo_builder_t * ret = tr_ calloc( 1, sizeof(tr_metainfo_builder_t));120 tr_metainfo_builder_t * ret = tr_new0( tr_metainfo_builder_t, 1 ); 121 121 ret->top = tr_strdup( topFile ); 122 122 ret->handle = handle; … … 143 143 ++ret->fileCount; 144 144 145 ret->files = tr_ calloc(ret->fileCount, sizeof(tr_metainfo_builder_file_t));145 ret->files = tr_new0( tr_metainfo_builder_file_t, ret->fileCount ); 146 146 147 147 for( i=0, walk=files; walk!=NULL; ++i ) … … 194 194 { 195 195 int fileIndex = 0; 196 uint8_t *ret = (uint8_t*) tr_malloc (SHA_DIGEST_LENGTH * b->pieceCount );196 uint8_t *ret = tr_new( uint8_t, SHA_DIGEST_LENGTH * b->pieceCount ); 197 197 uint8_t *walk = ret; 198 uint8_t *buf = tr_ malloc(b->pieceSize );198 uint8_t *buf = tr_new( uint8_t, b->pieceSize ); 199 199 uint64_t totalRemain; 200 200 uint64_t off = 0; … … 423 423 if( lock == NULL ) 424 424 { 425 lock = tr_ calloc( 1, sizeof( tr_lock_t ));425 lock = tr_new0( tr_lock_t, 1 ); 426 426 tr_lockInit( lock ); 427 427 } -
trunk/libtransmission/torrent.c
r2202 r2206 98 98 99 99 static int 100 tr_torrentDuplicateDownload( tr_torrent_t * tor )101 {102 tr_torrent_t * current;103 104 /* Check if a torrent with the same name and destination is already active */105 for( current = tor->handle->torrentList; current; current = current->next )106 {107 if( current != tor108 && !strcmp( tor->destination, current->destination )109 && !strcmp( tor->info.name, current->info.name ) )110 {111 return TRUE;112 }113 }114 return FALSE;115 }116 117 static int118 100 getBytePiece( const tr_info_t * info, uint64_t byteOffset ) 119 101 { … … 160 142 } 161 143 162 void144 static void 163 145 tr_torrentInitFilePieces( tr_torrent_t * tor ) 164 146 { … … 180 162 static void torrentThreadLoop( void * ); 181 163 182 static tr_torrent_t *164 static void 183 165 torrentRealInit( tr_handle_t * h, 184 166 tr_torrent_t * tor, 185 167 const char * destination, 186 uint8_t * hash, 187 int flags, 188 int * error ) 189 { 168 int flags ) 169 { 170 int i; 190 171 char name[512]; 191 tr_torrent_t * tor_tmp;192 tr_info_t * inf;193 int i;194 172 195 inf = &tor->info; 196 inf->flags |= flags; 173 tor->info.flags = flags; 197 174 198 175 tr_sharedLock( h->shared ); 199 176 200 177 tor->destination = tr_strdup( destination ); 201 202 /* Make sure this torrent is not already open */203 for( tor_tmp = h->torrentList; tor_tmp; tor_tmp = tor_tmp->next )204 {205 if( !memcmp( tor->info.hash, tor_tmp->info.hash,206 SHA_DIGEST_LENGTH ) )207 {208 if( NULL != hash )209 {210 memcpy( hash, tor->info.hash, SHA_DIGEST_LENGTH );211 }212 *error = TR_EDUPLICATE;213 tr_metainfoFree( &tor->info );214 free( tor );215 tr_sharedUnlock( h->shared );216 return NULL;217 }218 }219 178 220 179 tr_torrentInitFilePieces( tor ); … … 226 185 tor->hasChangedState = -1; 227 186 228 /* Don't start if a torrent with the same name229 and destination is already active */230 if( tr_torrentDuplicateDownload( tor ) )231 {232 *error = TR_ERROR_IO_DUP_DOWNLOAD;233 tr_metainfoFree( &tor->info );234 free( tor );235 tr_sharedUnlock( h->shared );236 return NULL;237 }238 239 187 /* Escaped info hash for HTTP queries */ 240 188 for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) … … 242 190 snprintf( &tor->escapedHashString[3*i], 243 191 sizeof( tor->escapedHashString ) - 3 * i, 244 "%%%02x", inf->hash[i] );192 "%%%02x", tor->info.hash[i] ); 245 193 } 246 194 … … 248 196 249 197 /* Block size: usually 16 ko, or less if we have to */ 250 tor->blockSize = MIN( inf->pieceSize, 1 << 14 );251 tor->blockCount = ( inf->totalSize + tor->blockSize - 1 ) /198 tor->blockSize = MIN( tor->info.pieceSize, 1 << 14 ); 199 tor->blockCount = ( tor->info.totalSize + tor->blockSize - 1 ) / 252 200 tor->blockSize; 253 201 tor->completion = tr_cpInit( tor ); … … 284 232 snprintf( name, sizeof( name ), "torrent %p (%s)", tor, tor->info.name ); 285 233 tr_threadCreate( &tor->thread, torrentThreadLoop, tor, name ); 286 287 return tor; 288 } 289 234 } 235 236 static int 237 pathIsInUse ( const tr_handle_t * h, 238 const char * destination, 239 const char * name ) 240 { 241 const tr_torrent_t * tor; 242 243 for( tor=h->torrentList; tor; tor=tor->next ) 244 if( !strcmp( destination, tor->destination ) 245 && !strcmp( name, tor->info.name ) ) 246 return TRUE; 247 248 return FALSE; 249 } 250 251 static int 252 hashExists( const tr_handle_t * h, 253 const uint8_t * hash ) 254 { 255 const tr_torrent_t * tor; 256 257 for( tor=h->torrentList; tor; tor=tor->next ) 258 if( !memcmp( hash, tor->info.hash, SHA_DIGEST_LENGTH ) ) 259 return TRUE; 260 261 return FALSE; 262 } 263 264 static int 265 infoCanAdd( const tr_handle_t * h, 266 const char * destination, 267 const tr_info_t * info ) 268 { 269 if( hashExists( h, info->hash ) ) 270 return TR_EDUPLICATE; 271 272 if( pathIsInUse( h, destination, info->name ) ) 273 return TR_ERROR_IO_DUP_DOWNLOAD; 274 275 return TR_OK; 276 } 277 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 ); 289 } 290 290 291 tr_torrent_t * 291 292 tr_torrentInit( tr_handle_t * h, 292 293 const char * path, 293 294 const char * destination, 294 uint8_t * hash,295 295 int flags, 296 296 int * error ) 297 297 { 298 tr_torrent_t * tor = tr_calloc( 1, sizeof *tor ); 299 if( NULL == tor ) 300 { 298 int val; 299 tr_torrent_t * tor = NULL; 300 301 if(( val = tr_torrentCanAdd( h, destination, path ) )) 302 *error = val; 303 else if(!(( tor = tr_new0( tr_torrent_t, 1 )))) 301 304 *error = TR_EOTHER; 302 return NULL; 303 } 304 305 /* Parse torrent file */ 306 if( tr_metainfoParseFile( &tor->info, h->tag, path, 307 TR_FLAG_SAVE & flags ) ) 308 { 309 *error = TR_EINVALID; 310 free( tor ); 311 return NULL; 312 } 313 314 return torrentRealInit( h, tor, destination, hash, flags, error ); 315 } 316 317 tr_torrent_t * 318 tr_torrentInitData( tr_handle_t * h, 319 uint8_t * data, 320 size_t size, 321 const char * destination, 322 uint8_t * hash, 323 int flags, 324 int * error ) 325 { 326 tr_torrent_t * tor = tr_calloc( 1, sizeof *tor ); 327 if( NULL == tor ) 328 { 329 *error = TR_EOTHER; 330 return NULL; 331 } 332 333 /* Parse torrent file */ 334 if( tr_metainfoParseData( &tor->info, h->tag, data, size, 335 TR_FLAG_SAVE & flags ) ) 336 { 337 *error = TR_EINVALID; 338 free( tor ); 339 return NULL; 340 } 341 342 return torrentRealInit( h, tor, destination, hash, flags, error ); 305 else { 306 tr_metainfoParseFile( &tor->info, h->tag, path, TR_FLAG_SAVE & flags ); 307 torrentRealInit( h, tor, destination, flags ); 308 } 309 310 return tor; 311 } 312 313 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 ); 343 324 } 344 325 … … 350 331 int * error ) 351 332 { 352 tr_torrent_t * tor = calloc( 1, sizeof *tor ); 353 if( NULL == tor ) 354 { 333 int val; 334 tr_torrent_t * tor = NULL; 335 336 if(( val = tr_torrentCanAddHash( h, destination, hashStr ) )) 337 *error = val; 338 else if(!(( tor = tr_new0( tr_torrent_t, 1 )))) 355 339 *error = TR_EOTHER; 356 return NULL; 357 } 358 359 /* Parse torrent file */ 360 if( tr_metainfoParseHash( &tor->info, h->tag, hashStr ) ) 361 { 362 *error = TR_EINVALID; 363 free( tor ); 364 return NULL; 365 } 366 367 return torrentRealInit( h, tor, destination, NULL, (TR_FLAG_SAVE|flags), error ); 340 else { 341 tr_metainfoParseHash( &tor->info, h->tag, hashStr ); 342 torrentRealInit( h, tor, destination, (TR_FLAG_SAVE|flags) ); 343 } 344 345 return tor; 346 } 347 348 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 ); 360 } 361 362 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 ) 369 { 370 int val; 371 tr_torrent_t * tor = NULL; 372 373 if(( val = tr_torrentCanAddData( h, destination, data, size ) )) 374 *error = val; 375 else if(!(( tor = tr_new0( tr_torrent_t, 1 )))) 376 *error = TR_EOTHER; 377 else { 378 tr_metainfoParseData( &tor->info, h->tag, data, size, TR_FLAG_SAVE & flags ); 379 torrentRealInit( h, tor, destination, flags ); 380 } 381 382 return tor; 368 383 } 369 384 … … 452 467 uint8_t * peerCompact; 453 468 454 if( !( tor->status & TR_STATUS_ACTIVE ))469 if( tor->status != TR_RUN_RUNNING ) 455 470 return; 456 471 … … 486 501 tc = tor->tracker; 487 502 s->cannotConnect = tr_trackerCannotConnect( tc ); 488 s->tracker = ( tc ? tr_trackerGet( tc ) : &tor->info.trackerList[0].list[0] ); 503 s->tracker = tc 504 ? tr_trackerGet( tc ) 505 : &tor->info.trackerList[0].list[0]; 489 506 490 507 /* peers... */ … … 568 585 569 586 *peerCount = tor->peerCount; 570 571 peers = (tr_peer_stat_t *) calloc( tor->peerCount, sizeof( tr_peer_stat_t ) );587 588 peers = tr_new0( tr_peer_stat_t, tor->peerCount ); 572 589 if (peers != NULL) 573 590 { … … 700 717 701 718 float* 702 tr_torrentCompletion( tr_torrent_t * tor )719 tr_torrentCompletion( const tr_torrent_t * tor ) 703 720 { 704 721 int i; … … 706 723 tr_torrentReaderLock( tor ); 707 724 708 f = calloc ( tor->info.fileCount, sizeof( float ));725 f = tr_new0( float, tor->info.fileCount ); 709 726 for( i=0; i<tor->info.fileCount; ++i ) 710 727 f[i] = tr_torrentFileCompletion ( tor, i ); … … 891 908 /* create the check-files mutex */ 892 909 if( !checkFilesLockInited ) { 893 tr_lockInit( &checkFilesLock );894 checkFilesLockInited = TRUE;910 checkFilesLockInited = TRUE; 911 tr_lockInit( &checkFilesLock ); 895 912 } 896 913 … … 971 988 /* starting to run... */ 972 989 if( tor->io == NULL ) { 990 *tor->errorString = '\0'; 973 991 tr_torrentResetTransferStats( tor ); 974 992 tor->io = tr_ioInitFast( tor ); … … 986 1004 if( cpStatus != tor->cpStatus ) { 987 1005 tor->hasChangedState = tor->cpStatus = cpStatus; 988 if( (cpStatus == TR_CP_COMPLETE) && tor->tracker!=NULL ) 989 tr_trackerCompleted( tor->tracker ); 1006 if( (cpStatus == TR_CP_COMPLETE) /* if we're complete */ 1007 && tor->tracker!=NULL /* and we have a tracker */ 1008 && tor->downloadedCur ) /* and it just happened */ 1009 tr_trackerCompleted( tor->tracker ); /* tell the tracker */ 990 1010 tr_ioSync( tor->io ); 991 1011 } … … 1103 1123 1104 1124 tr_torrentReaderLock( tor ); 1105 p = tr_ malloc( tor->info.fileCount * sizeof(tr_priority_t));1125 p = tr_new0( tr_priority_t, tor->info.fileCount ); 1106 1126 for( i=0; i<tor->info.fileCount; ++i ) 1107 1127 p[i] = tor->info.files[i].priority; -
trunk/libtransmission/transmission.h
r2202 r2206 267 267 const char * path, 268 268 const char * destination, 269 uint8_t * hash, int flags, int * error ); 269 int flags, int * error ); 270 271 /** 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 */ 276 int tr_torrentCanAdd( const tr_handle_t * handle, 277 const char * destination, 278 const char * path ); 270 279 271 280 /*********************************************************************** … … 278 287 uint8_t * data, size_t size, 279 288 const char * destination, 280 uint8_t * hash,int flags, int * error );289 int flags, int * error ); 281 290 282 291 /*********************************************************************** … … 401 410 * array when done. 402 411 **********************************************************************/ 403 float * tr_torrentCompletion( tr_torrent_t * );412 float * tr_torrentCompletion( const tr_torrent_t * ); 404 413 405 414 float tr_torrentFileCompletion( const tr_torrent_t *, int fileIndex ); … … 506 515 there's nothing we want right now */ 507 516 TR_STATUS_SEED = (1<<4), /* Seeding */ 508 TR_STATUS_STOPPING = (1<<5), /* Sending 'stopped' to the tracker */ 509 TR_STATUS_STOPPED = (1<<6) /* Sent 'stopped' but thread still 510 running (for internal use only) */ 517 TR_STATUS_STOPPING = (1<<5), /* Stopping -- closing connections, etc. */ 518 TR_STATUS_STOPPED = (1<<6) /* Torrent is stopped */ 511 519 } 512 520 torrent_status_t; -
trunk/libtransmission/utils.c
r2154 r2206 412 412 } 413 413 414 void* 415 tr_malloc0( size_t size ) 416 { 417 void * ret = tr_malloc( size ); 418 memset( ret, 0, size ); 419 return ret; 420 } 421 414 422 void tr_free( void * p ) 415 423 { -
trunk/libtransmission/utils.h
r2154 r2206 177 177 ***/ 178 178 179 #define tr_new(struct_type, n_structs) \ 180 ((struct_type *) tr_malloc (((size_t) sizeof (struct_type)) * ((size_t) (n_structs)))) 181 #define tr_new0(struct_type, n_structs) \ 182 ((struct_type *) tr_malloc0 (((size_t) sizeof (struct_type)) * ((size_t) (n_structs)))) 183 184 void* tr_malloc ( size_t ); 185 void* tr_malloc0 ( size_t ); 186 void* tr_calloc ( size_t nmemb, size_t size ); 187 void tr_free ( void* ); 188 179 189 char* tr_strdup( const char * str ); 180 190 char* tr_strndup( const char * str, int len ); 181 void* tr_malloc( size_t );182 void* tr_calloc( size_t nmemb, size_t size );183 void tr_free( void* );184 191 185 192 /***
Note: See TracChangeset
for help on using the changeset viewer.