Changeset 1634
- Timestamp:
- Apr 2, 2007, 8:32:20 PM (16 years ago)
- Location:
- branches/daemon/libtransmission
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/daemon/libtransmission/metainfo.c
r1613 r1634 30 30 * Local prototypes 31 31 **********************************************************************/ 32 static int realparse( tr_info_t * inf, uint8_t * buf, size_t len ); 32 33 static void savedname( char * name, size_t len, const char * hash, 33 34 const char * tag ); 34 static char * readtorrent( const char * path, const char * hash, 35 const char * tag, size_t * len ); 35 static char * readtorrent( const char * path, size_t * len ); 36 36 static int savetorrent( const char * hash, const char * tag, 37 37 const uint8_t * buf, size_t buflen ); … … 49 49 * 50 50 **********************************************************************/ 51 int tr_metainfoParse( tr_info_t * inf, const char * tag, const char * path, 52 const char * savedHash, int saveCopy ) 53 { 54 char * buf; 51 int 52 tr_metainfoParseFile( tr_info_t * inf, const char * tag, 53 const char * path, int save ) 54 { 55 uint8_t * buf; 56 size_t size; 57 58 /* read the torrent data */ 59 buf = readtorrent( path, &size ); 60 if( NULL == buf ) 61 { 62 return 1; 63 } 64 65 if( realparse( inf, buf, size ) ) 66 { 67 free( buf ); 68 return 1; 69 } 70 71 if( save ) 72 { 73 if( savetorrent( inf->hashString, tag, buf, size ) ) 74 { 75 free( buf ); 76 return 1; 77 } 78 savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag ); 79 } 80 81 free( buf ); 82 83 return 0; 84 } 85 86 int 87 tr_metainfoParseData( tr_info_t * inf, const char * tag, 88 uint8_t * data, size_t size, int save ) 89 { 90 if( realparse( inf, data, size ) ) 91 { 92 return 1; 93 } 94 95 if( save ) 96 { 97 if( savetorrent( inf->hashString, tag, data, size ) ) 98 { 99 return 1; 100 } 101 savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag ); 102 } 103 104 return 0; 105 } 106 107 int 108 tr_metainfoParseHash( tr_info_t * inf, const char * tag, const char * hash ) 109 { 110 struct stat sb; 111 uint8_t * buf; 112 size_t size; 113 int save; 114 115 /* check it we should use an old file without a tag */ 116 /* XXX this should go away at some point */ 117 save = 0; 118 savedname( inf->torrent, sizeof inf->torrent, hash, tag ); 119 if( 0 > stat( inf->torrent, &sb ) && ENOENT == errno ) 120 { 121 savedname( inf->torrent, sizeof inf->torrent, hash, NULL ); 122 if( 0 == stat( inf->torrent, &sb )) 123 { 124 save = 1; 125 } 126 } 127 128 buf = readtorrent( inf->torrent, &size ); 129 if( NULL == buf ) 130 { 131 return 1; 132 } 133 134 if( realparse( inf, buf, size ) ) 135 { 136 free( buf ); 137 return 1; 138 } 139 140 /* save a new tagged copy of the old untagged torrent */ 141 if( save ) 142 { 143 if( savetorrent( hash, tag, buf, size ) ) 144 { 145 free( buf ); 146 return 1; 147 } 148 savedname( inf->torrent, sizeof inf->torrent, hash, tag ); 149 } 150 151 free( buf ); 152 153 return 0; 154 } 155 156 int 157 realparse( tr_info_t * inf, uint8_t * buf, size_t size ) 158 { 55 159 benc_val_t meta, * beInfo, * val, * val2; 56 160 int i; 57 size_t len;58 59 assert( NULL == path || NULL == savedHash );60 /* if savedHash isn't null, saveCopy should be false */61 assert( NULL == savedHash || !saveCopy );62 63 /* read the torrent data */64 buf = readtorrent( path, savedHash, tag, &len );65 if( NULL == buf )66 {67 return 1;68 }69 161 70 162 /* Parse bencoded infos */ 71 if( tr_bencLoad( buf, len, &meta, NULL ) )163 if( tr_bencLoad( buf, size, &meta, NULL ) ) 72 164 { 73 165 tr_err( "Error while parsing bencoded data" ); 74 free( buf );75 166 return 1; 76 167 } … … 82 173 tr_err( "%s \"info\" dictionary", ( beInfo ? "Invalid" : "Missing" ) ); 83 174 tr_bencFree( &meta ); 84 free( buf );85 175 return 1; 86 176 } … … 91 181 snprintf( inf->hashString + i * 2, sizeof( inf->hashString ) - i * 2, 92 182 "%02x", inf->hash[i] ); 93 }94 95 /* Save a copy of the torrent file */96 if( saveCopy )97 {98 if( savetorrent( inf->hashString, tag, (uint8_t *) buf, len ) )99 {100 tr_bencFree( &meta );101 free( buf );102 return 1;103 }104 }105 106 /* We won't need this anymore */107 free( buf );108 109 /* Torrent file name */110 if( NULL == path || saveCopy )111 {112 savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );113 }114 else115 {116 snprintf( inf->torrent, MAX_PATH_LENGTH, "%s", path );117 183 } 118 184 … … 469 535 } 470 536 471 char * readtorrent( const char * path, const char * hash, const char * tag, 472 size_t * len ) 473 { 474 char hashpath[MAX_PATH_LENGTH], * buf; 537 char * readtorrent( const char * path, size_t * size ) 538 { 539 char * buf; 475 540 struct stat sb; 476 541 FILE * file; 477 int lasterr, save;478 479 if( NULL != hash )480 {481 savedname( hashpath, sizeof hashpath, hash, tag );482 path = hashpath;483 }484 542 485 543 /* try to stat the file */ 486 save = 0;487 544 if( stat( path, &sb ) ) 488 545 { 489 if( ENOENT != errno || NULL == hash ) 490 { 491 tr_err( "Could not stat file (%s)", path ); 492 return NULL; 493 } 494 /* it doesn't exist, check for an old file without a tag */ 495 /* XXX this should go away at some point */ 496 lasterr = errno; 497 savedname( hashpath, sizeof hashpath, hash, NULL ); 498 if( stat( path, &sb ) ) 499 { 500 savedname( hashpath, sizeof hashpath, hash, tag ); 501 errno = lasterr; 502 tr_err( "Could not stat file (%s)", path ); 503 return NULL; 504 } 505 save = 1; 546 tr_err( "Could not stat file (%s)", path ); 547 return NULL; 506 548 } 507 549 … … 513 555 if( sb.st_size > TORRENT_MAX_SIZE ) 514 556 { 515 tr_err( "Torrent file is too big (%d bytes)", ( int )sb.st_size ); 557 tr_err( "Torrent file is too big (%"PRIu64" bytes)", 558 ( uint64_t )sb.st_size ); 516 559 return NULL; 517 560 } … … 527 570 if( NULL == buf ) 528 571 { 529 tr_err( "Could not allocate memory (%d bytes)", ( int )sb.st_size ); 572 tr_err( "Could not allocate memory (%"PRIu64" bytes)", 573 ( uint64_t )sb.st_size ); 530 574 } 531 575 fseek( file, 0, SEEK_SET ); … … 539 583 fclose( file ); 540 584 541 /* save a new tagged copy of the old untagged torrent */ 542 if( save ) 543 { 544 savetorrent( hash, tag, (uint8_t *) buf, sb.st_size ); 545 } 546 547 *len = sb.st_size; 585 *size = sb.st_size; 548 586 549 587 return buf; -
branches/daemon/libtransmission/metainfo.h
r1564 r1634 28 28 #include "transmission.h" 29 29 30 int tr_metainfoParse( tr_info_t *, const char * tag, const char * path, 31 const char * savedHash, int saveCopy ); 30 int tr_metainfoParseFile( tr_info_t *, const char * tag, 31 const char * path, int save ); 32 int tr_metainfoParseData( tr_info_t *, const char * tag, 33 uint8_t * data, size_t size, int save ); 34 int tr_metainfoParseHash( tr_info_t *, const char * tag, const char * hash ); 32 35 void tr_metainfoFree( tr_info_t * inf ); 33 36 void tr_metainfoRemoveSaved( const char * hashString, const char * tag ); -
branches/daemon/libtransmission/torrent.c
r1614 r1634 54 54 } 55 55 56 tr_torrent_t * tr_torrentInit( tr_handle_t * h, const char * path, 57 uint8_t * hash, int flags, int * error ) 58 { 59 tr_torrent_t * tor = calloc( sizeof( tr_torrent_t ), 1 ); 60 int saveCopy = ( TR_FLAG_SAVE & flags ); 56 tr_torrent_t * 57 tr_torrentInit( tr_handle_t * h, const char * path, 58 uint8_t * hash, int flags, int * error ) 59 { 60 tr_torrent_t * tor; 61 62 tor = calloc( 1, sizeof *tor ); 63 if( NULL == tor ) 64 { 65 *error = TR_EOTHER; 66 return NULL; 67 } 61 68 62 69 /* Parse torrent file */ 63 if( tr_metainfoParse( &tor->info, h->tag, path, NULL, saveCopy ) ) 70 if( tr_metainfoParseFile( &tor->info, h->tag, path, 71 TR_FLAG_SAVE & flags ) ) 64 72 { 65 73 *error = TR_EINVALID; … … 71 79 } 72 80 73 tr_torrent_t * tr_torrentInitSaved( tr_handle_t * h, const char * hashStr, 74 int flags, int * error ) 75 { 76 tr_torrent_t * tor = calloc( sizeof( tr_torrent_t ), 1 ); 81 tr_torrent_t * 82 tr_torrentInitData( tr_handle_t * h, uint8_t * data, size_t size, 83 uint8_t * hash, int flags, int * error ) 84 { 85 tr_torrent_t * tor; 86 87 tor = calloc( 1, sizeof *tor ); 88 if( NULL == tor ) 89 { 90 *error = TR_EOTHER; 91 return NULL; 92 } 77 93 78 94 /* Parse torrent file */ 79 if( tr_metainfoParse( &tor->info, h->tag, NULL, hashStr, 0 ) ) 95 if( tr_metainfoParseData( &tor->info, h->tag, data, size, 96 TR_FLAG_SAVE & flags ) ) 97 { 98 *error = TR_EINVALID; 99 free( tor ); 100 return NULL; 101 } 102 103 return torrentRealInit( h, tor, hash, flags, error ); 104 } 105 106 tr_torrent_t * 107 tr_torrentInitSaved( tr_handle_t * h, const char * hashStr, 108 int flags, int * error ) 109 { 110 tr_torrent_t * tor; 111 112 tor = calloc( 1, sizeof *tor ); 113 if( NULL == tor ) 114 { 115 *error = TR_EOTHER; 116 return NULL; 117 } 118 119 /* Parse torrent file */ 120 if( tr_metainfoParseHash( &tor->info, h->tag, hashStr ) ) 80 121 { 81 122 *error = TR_EINVALID; -
branches/daemon/libtransmission/transmission.h
r1614 r1634 215 215 216 216 /*********************************************************************** 217 * tr_torrentInitData 218 *********************************************************************** 219 * Like tr_torrentInit, except the actual torrent data is passed in 220 * instead of the filename. 221 **********************************************************************/ 222 tr_torrent_t * tr_torrentInitData( tr_handle_t *, uint8_t * data, 223 size_t size, uint8_t * hash, 224 int flags, int * error ); 225 226 /*********************************************************************** 217 227 * tr_torrentInitSaved 218 228 ***********************************************************************
Note: See TracChangeset
for help on using the changeset viewer.