Changeset 5643 for trunk/libtransmission/makemeta.c
- Timestamp:
- Apr 18, 2008, 4:23:59 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/makemeta.c
r5636 r5643 275 275 tr_benc * uninitialized_path ) 276 276 { 277 tr_benc *sub;278 277 const char *pch, *prev; 279 278 const size_t topLen = strlen(topFile) + 1; /* +1 for '/' */ … … 299 298 buf[pch-prev] = '\0'; 300 299 301 sub = tr_bencListAdd( uninitialized_path ); 302 tr_bencInitStrDup( sub, buf ); 300 tr_bencListAddStr( uninitialized_path, buf ); 303 301 304 302 prev = pch + 1; … … 309 307 310 308 static void 311 makeFilesList( tr_benc * list,312 const tr_metainfo_builder * builder )313 {314 uint32_t i = 0;315 316 tr_bencListReserve( list, builder->fileCount );317 318 for( i=0; i<builder->fileCount; ++i )319 {320 tr_benc * dict = tr_bencListAdd( list );321 tr_benc *length, *pathVal;322 323 tr_bencInitDict( dict, 2 );324 length = tr_bencDictAdd( dict, "length" );325 pathVal = tr_bencDictAdd( dict, "path" );326 getFileInfo( builder->top, &builder->files[i], length, pathVal );327 }328 }329 330 static void331 309 makeInfoDict ( tr_benc * dict, 332 310 tr_metainfo_builder * builder ) 333 311 { 334 312 uint8_t * pch; 335 tr_benc * val;336 313 char base[MAX_PATH_LENGTH]; 337 314 … … 339 316 340 317 if ( builder->isSingleFile ) 341 {342 val = tr_bencDictAdd( dict, "length" );343 tr_bencInitInt( val, builder->files[0].size );344 }345 else346 {347 val = tr_bencDictAdd( dict, "files" );348 tr_bencInit( val, TYPE_LIST);349 makeFilesList( val, builder);350 }351 352 val = tr_bencDictAdd( dict, "name" ); 318 tr_bencDictAddInt( dict, "length", builder->files[0].size ); 319 else { 320 uint32_t i; 321 tr_benc * list = tr_bencDictAddList( dict, "files", builder->fileCount ); 322 for( i=0; i<builder->fileCount; ++i ) { 323 tr_benc * dict = tr_bencListAddDict( list, 2 ); 324 tr_benc * length = tr_bencDictAdd( dict, "length" ); 325 tr_benc * pathVal = tr_bencDictAdd( dict, "path" ); 326 getFileInfo( builder->top, &builder->files[i], length, pathVal ); 327 } 328 } 329 353 330 strlcpy( base, builder->top, sizeof( base ) ); 354 tr_bencInitStrDup ( val, basename( base ) ); 355 356 val = tr_bencDictAdd( dict, "piece length" ); 357 tr_bencInitInt( val, builder->pieceSize ); 358 359 if( ( pch = getHashInfo( builder ) ) ) { 360 val = tr_bencDictAdd( dict, "pieces" ); 361 tr_bencInitStr( val, pch, SHA_DIGEST_LENGTH * builder->pieceCount, 0 ); 362 } 363 364 val = tr_bencDictAdd( dict, "private" ); 365 tr_bencInitInt( val, builder->isPrivate ? 1 : 0 ); 331 tr_bencDictAddStr( dict, "name", basename( base ) ); 332 333 tr_bencDictAddInt( dict, "piece length", builder->pieceSize ); 334 335 if(( pch = getHashInfo( builder ))) { 336 tr_bencDictAddRaw( dict, "pieces", pch, SHA_DIGEST_LENGTH * builder->pieceCount ); 337 tr_free( pch ); 338 } 339 340 tr_bencDictAddInt( dict, "private", builder->isPrivate ? 1 : 0 ); 366 341 } 367 342 … … 370 345 { 371 346 int n = 5; 372 tr_benc top , *val;347 tr_benc top; 373 348 374 349 if ( builder->comment && *builder->comment ) ++n; 375 350 tr_bencInitDict( &top, n ); 376 351 377 val = tr_bencDictAdd( &top, "announce");378 tr_bencInitStrDup( val, builder->announce ); 352 tr_bencDictAddStr( &top, "announce", builder->announce ); 353 379 354 if( tr_httpParseURL( builder->announce, -1, NULL, NULL, NULL ) ) 380 {381 355 builder->result = TR_MAKEMETA_URL; 382 }383 356 384 357 if( !builder->result && !builder->abortFlag ) 385 358 { 386 if( builder->comment && *builder->comment ) { 387 val = tr_bencDictAdd( &top, "comment" ); 388 tr_bencInitStrDup( val, builder->comment ); 389 } 390 391 val = tr_bencDictAdd( &top, "created by" ); 392 tr_bencInitStrDup( val, TR_NAME "/" LONG_VERSION_STRING ); 393 394 val = tr_bencDictAdd( &top, "creation date" ); 395 tr_bencInitInt( val, time(0) ); 396 397 val = tr_bencDictAdd( &top, "encoding" ); 398 tr_bencInitStrDup( val, "UTF-8" ); 399 400 val = tr_bencDictAdd( &top, "info" ); 401 tr_bencInitDict( val, 666 ); 402 makeInfoDict( val, builder ); 359 if( builder->comment && *builder->comment ) 360 tr_bencDictAddStr( &top, "comment", builder->comment ); 361 tr_bencDictAddStr( &top, "created by", TR_NAME "/" LONG_VERSION_STRING ); 362 tr_bencDictAddInt( &top, "creation date", time(0) ); 363 tr_bencDictAddStr( &top, "encoding", "UTF-8" ); 364 makeInfoDict( tr_bencDictAddDict( &top, "info", 666 ), builder ); 403 365 } 404 366 405 367 /* save the file */ 406 368 if ( !builder->result && !builder->abortFlag ) { 407 char * pch = tr_bencSave( &top, &n ); 408 FILE * fp = fopen( builder->outputFile, "wb+" ); 409 size_t nmemb = n; 410 if( !fp || ( fwrite( pch, 1, nmemb, fp ) != nmemb ) ) { 369 if( tr_bencSaveFile( builder->outputFile, &top ) ) { 411 370 builder->my_errno = errno; 412 371 strlcpy( builder->errfile, builder->outputFile, sizeof( builder->errfile ) ); 413 372 builder->result = TR_MAKEMETA_IO_WRITE; 414 373 } 415 if( fp )416 fclose( fp );417 tr_free( pch );418 374 } 419 375
Note: See TracChangeset
for help on using the changeset viewer.