1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-02-23 06:30:38 +00:00

save a copy of the torrent's metadata in tr_torrentInit(), not tr_torrentParse(). remove dead code from metainfo.c.

This commit is contained in:
Charles Kerr 2007-12-22 18:00:47 +00:00
parent c946168b1e
commit b13d8d1dd6
4 changed files with 28 additions and 187 deletions

View file

@ -67,15 +67,6 @@ tr_httpParseUrl( const char * url_in, int len,
/***********************************************************************
* Local prototypes
**********************************************************************/
static int realparse( tr_info * inf, const uint8_t * buf, size_t len );
static int realparse2( tr_info * inf, const benc_val_t * meta );
static void savedname( char * name, size_t len, const char * hash,
const char * tag );
static uint8_t * readtorrent( const char * path, size_t * len );
static int savetorrent( const char * hash, const char * tag,
const uint8_t * buf, size_t buflen );
static int getfile( char * buf, int size,
const char * prefix, benc_val_t * name );
static int getannounce( tr_info * inf, benc_val_t * meta );
static char * announceToScrape( const char * announce );
static int parseFiles( tr_info * inf, benc_val_t * name,
@ -157,161 +148,8 @@ strlcat_utf8( void * dest, const void * src, size_t len, char skip )
}
}
/***********************************************************************
* tr_metainfoParse
***********************************************************************
*
**********************************************************************/
int
tr_metainfoParseFile( tr_info * inf, const char * tag,
const char * path, int save )
{
uint8_t * buf;
size_t size;
/* read the torrent data */
buf = readtorrent( path, &size );
if( NULL == buf )
{
return TR_EINVALID;
}
if( realparse( inf, buf, size ) )
{
free( buf );
return TR_EINVALID;
}
if( save )
{
if( savetorrent( inf->hashString, tag, buf, size ) )
{
free( buf );
return TR_EINVALID;
}
savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );
}
else
{
snprintf( inf->torrent, sizeof inf->torrent, "%s", path );
}
free( buf );
return TR_OK;
}
int
tr_metainfoParseData( tr_info * inf, const char * tag,
const uint8_t * data, size_t size, int save )
{
if( realparse( inf, data, size ) )
{
return TR_EINVALID;
}
if( save )
{
if( savetorrent( inf->hashString, tag, data, size ) )
{
return TR_EINVALID;
}
savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );
}
return TR_OK;
}
int
tr_metainfoParseBenc( tr_info * inf, const char * tag, const benc_val_t * val, int save )
{
int err = 0;
if( !err && realparse2( inf, val ) )
err = TR_EINVALID;
if( !err && save ) {
int len;
uint8_t * text = (uint8_t *) tr_bencSave( val, &len );
err = savetorrent( inf->hashString, tag, text, len );
tr_free( text );
if( !err )
savedname( inf->torrent, sizeof( inf->torrent ), inf->hashString, tag );
}
return err;
}
int
tr_metainfoParseHash( tr_info * inf, const char * tag, const char * hash )
{
struct stat sb;
uint8_t * buf;
size_t size;
int save;
/* check it we should use an old file without a tag */
/* XXX this should go away at some point */
save = 0;
savedname( inf->torrent, sizeof inf->torrent, hash, tag );
if( 0 > stat( inf->torrent, &sb ) && ENOENT == errno )
{
savedname( inf->torrent, sizeof inf->torrent, hash, NULL );
if( 0 == stat( inf->torrent, &sb ))
{
save = 1;
}
}
buf = readtorrent( inf->torrent, &size );
if( NULL == buf )
{
return TR_EINVALID;
}
if( realparse( inf, buf, size ) )
{
free( buf );
return TR_EINVALID;
}
/* save a new tagged copy of the old untagged torrent */
if( save )
{
if( savetorrent( hash, tag, buf, size ) )
{
free( buf );
return TR_EINVALID;
}
savedname( inf->torrent, sizeof inf->torrent, hash, tag );
}
free( buf );
return TR_OK;
}
static int
realparse( tr_info * inf, const uint8_t * buf, size_t size )
{
int err = 0;
benc_val_t meta;
if( !err && tr_bencLoad( buf, size, &meta, NULL ) ) {
err = TR_EINVALID;
tr_err( "Error while parsing bencoded data [%*.*s]", (int)size, (int)size, (char*)buf );
}
if( !err ) {
err = realparse2( inf, &meta );
tr_bencFree( &meta );
}
return err;
}
static int
realparse2( tr_info * inf, const benc_val_t * meta_in )
tr_metainfoParse( tr_info * inf, const benc_val_t * meta_in )
{
int i;
benc_val_t * beInfo, * val, * val2;
@ -744,15 +582,9 @@ void tr_metainfoRemoveSaved( const char * hashString, const char * tag )
unlink( file );
}
static uint8_t *
readtorrent( const char * path, size_t * size )
{
return tr_loadFile( path, size );
}
/* Save a copy of the torrent file in the saved torrent directory */
static int
savetorrent( const char * hash, const char * tag,
int
tr_metainfoSave( const char * hash, const char * tag,
const uint8_t * buf, size_t buflen )
{
char path[MAX_PATH_LENGTH];

View file

@ -29,13 +29,10 @@
struct benc_val_t;
int tr_metainfoParseFile( tr_info *, const char * tag,
const char * path, int save );
int tr_metainfoParseData( tr_info *, const char * tag,
const uint8_t * data, size_t size, int save );
int tr_metainfoParseHash( tr_info *, const char * tag, const char * hash );
int tr_metainfoParseBenc( tr_info *, const char * tag, const struct benc_val_s *, int save );
int tr_metainfoParse( tr_info *, const struct benc_val_s * );
void tr_metainfoFree( tr_info * inf );
void tr_metainfoRemoveSaved( const char * hashString, const char * tag );
int tr_metainfoSave( const char *hashString, const char * tag, const uint8_t * metainfo, size_t len );
#endif

View file

@ -370,6 +370,19 @@ torrentRealInit( tr_handle * h,
tr_globalUnlock( h );
/* maybe save our own copy of the metainfo */
if( tr_ctorGetSave( ctor ) ) {
const benc_val_t * val;
if( !tr_ctorGetMetainfo( ctor, &val ) ) {
int len;
uint8_t * text = (uint8_t*) tr_bencSave( val, &len );
tr_metainfoSave( tor->info.hashString,
tor->handle->tag,
text, len );
tr_free( text );
}
}
if( doStart )
tr_torrentStart( tor );
}
@ -388,7 +401,7 @@ hashExists( const tr_handle * h,
}
int
tr_torrentParseFromCtor( const tr_handle * handle,
tr_torrentParse( const tr_handle * handle,
const tr_ctor * ctor,
tr_info * setmeInfo )
{
@ -396,7 +409,6 @@ tr_torrentParseFromCtor( const tr_handle * handle,
int doFree;
tr_info tmp;
const benc_val_t * metainfo;
const int doSave = tr_ctorGetSave( ctor );
if( setmeInfo == NULL )
setmeInfo = &tmp;
@ -405,7 +417,7 @@ tr_torrentParseFromCtor( const tr_handle * handle,
if( !err && tr_ctorGetMetainfo( ctor, &metainfo ) )
return TR_EINVALID;
err = tr_metainfoParseBenc( setmeInfo, handle->tag, metainfo, doSave );
err = tr_metainfoParse( setmeInfo, metainfo );
doFree = !err && ( setmeInfo == &tmp );
if( !err && hashExists( handle, setmeInfo->hash ) )
@ -426,7 +438,7 @@ tr_torrentNew( tr_handle * handle,
tr_info tmpInfo;
tr_torrent * tor = NULL;
err = tr_torrentParseFromCtor( handle, ctor, &tmpInfo );
err = tr_torrentParse( handle, ctor, &tmpInfo );
if( !err ) {
tor = tr_new0( tr_torrent, 1 );
tor->info = tmpInfo;

View file

@ -451,7 +451,7 @@ typedef struct tr_info tr_info;
* it will be filled with the metainfo's info. You'll need to
* call tr_metainfoFree( setme_info ) when done with it.
*/
int tr_torrentParseFromCtor( const tr_handle * handle,
int tr_torrentParse( const tr_handle * handle,
const tr_ctor * ctor,
tr_info * setme_info );