1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-30 19:46:56 +00:00
transmission/libtransmission/metainfo.c

636 lines
18 KiB
C
Raw Normal View History

2006-07-16 19:39:23 +00:00
/******************************************************************************
* $Id$
*
2008-01-01 17:20:20 +00:00
* Copyright (c) 2005-2008 Transmission authors and contributors
2006-07-16 19:39:23 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
2007-11-09 20:07:52 +00:00
#include <assert.h>
#include <ctype.h> /* isspace */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
2007-07-12 17:51:45 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> /* unlink, stat */
#include <event.h> /* struct evbuffer */
2006-07-16 19:39:23 +00:00
#include "transmission.h"
2007-07-09 20:10:42 +00:00
#include "bencode.h"
#include "crypto.h" /* tr_sha1 */
2007-07-09 20:10:42 +00:00
#include "metainfo.h"
#include "platform.h"
#include "utils.h"
2006-07-16 19:39:23 +00:00
/***********************************************************************
* Local prototypes
**********************************************************************/
static int parseFiles( tr_info * inf, tr_benc * name,
tr_benc * files, tr_benc * length );
2006-07-16 19:39:23 +00:00
2007-10-20 15:17:36 +00:00
/***
****
***/
#define WANTBYTES( want, got ) \
if( (want) > (got) ) { return; } else { (got) -= (want); }
static void
strlcat_utf8( void * dest, const void * src, size_t len, char skip )
{
char * s = dest;
const char * append = src;
const char * p;
/* don't overwrite the nul at the end */
len--;
/* Go to the end of the destination string */
while( s[0] )
{
s++;
len--;
}
/* Now start appending, converting on the fly if necessary */
for( p = append; p[0]; )
{
/* skip over the requested character */
if( skip == p[0] )
{
p++;
continue;
}
if( !( p[0] & 0x80 ) )
{
/* ASCII character */
WANTBYTES( 1, len );
*(s++) = *(p++);
continue;
}
if( ( p[0] & 0xE0 ) == 0xC0 && ( p[1] & 0xC0 ) == 0x80 )
{
/* 2-bytes UTF-8 character */
WANTBYTES( 2, len );
*(s++) = *(p++); *(s++) = *(p++);
continue;
}
if( ( p[0] & 0xF0 ) == 0xE0 && ( p[1] & 0xC0 ) == 0x80 &&
( p[2] & 0xC0 ) == 0x80 )
{
/* 3-bytes UTF-8 character */
WANTBYTES( 3, len );
*(s++) = *(p++); *(s++) = *(p++);
*(s++) = *(p++);
continue;
}
if( ( p[0] & 0xF8 ) == 0xF0 && ( p[1] & 0xC0 ) == 0x80 &&
( p[2] & 0xC0 ) == 0x80 && ( p[3] & 0xC0 ) == 0x80 )
{
/* 4-bytes UTF-8 character */
WANTBYTES( 4, len );
*(s++) = *(p++); *(s++) = *(p++);
*(s++) = *(p++); *(s++) = *(p++);
continue;
}
/* ISO 8859-1 -> UTF-8 conversion */
WANTBYTES( 2, len );
*(s++) = 0xC0 | ( ( *p & 0xFF ) >> 6 );
*(s++) = 0x80 | ( *(p++) & 0x3F );
}
}
static void
2008-04-14 11:52:50 +00:00
getTorrentFilename( const tr_handle * handle,
const tr_info * inf,
char * buf,
size_t buflen )
{
const char * dir = tr_getTorrentDir( handle );
char base[MAX_PATH_LENGTH];
snprintf( base, sizeof( base ), "%s.%16.16s.torrent", inf->name, inf->hashString );
tr_buildPath( buf, buflen, dir, base, NULL );
}
static void
getTorrentOldFilename( const tr_handle * handle,
const tr_info * info,
char * name,
size_t len )
{
const char * torDir = tr_getTorrentDir( handle );
if( !handle->tag )
{
2008-04-14 11:52:50 +00:00
tr_buildPath( name, len, torDir, info->hashString, NULL );
}
else
{
char base[1024];
2008-04-14 11:52:50 +00:00
snprintf( base, sizeof(base), "%s-%s", info->hashString, handle->tag );
tr_buildPath( name, len, torDir, base, NULL );
}
}
2008-04-14 11:52:50 +00:00
void
tr_metainfoMigrate( tr_handle * handle,
tr_info * inf )
2008-04-14 11:52:50 +00:00
{
struct stat new_sb;
char new_name[MAX_PATH_LENGTH];
getTorrentFilename( handle, inf, new_name, sizeof( new_name ) );
if( stat( new_name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) )
{
char old_name[MAX_PATH_LENGTH];
size_t contentLen;
uint8_t * content;
tr_mkdirp( tr_getTorrentDir( handle ), 0777 );
2008-04-14 11:52:50 +00:00
getTorrentOldFilename( handle, inf, old_name, sizeof( old_name ) );
if(( content = tr_loadFile( old_name, &contentLen )))
{
FILE * out;
errno = 0;
out = fopen( new_name, "wb+" );
if( !out )
{
tr_nerr( inf->name, _( "Couldn't create \"%1$s\": %2$s" ), new_name, tr_strerror( errno ) );
}
else
{
if( fwrite( content, sizeof( uint8_t ), contentLen, out ) == contentLen )
{
tr_free( inf->torrent );
inf->torrent = tr_strdup( new_name );
tr_sessionSetTorrentFile( handle, inf->hashString, new_name );
unlink( old_name );
}
fclose( out );
}
2008-04-14 11:52:50 +00:00
}
tr_free( content );
}
}
2008-04-24 03:26:36 +00:00
static char *
announceToScrape( const char * announce )
{
char * scrape = NULL;
const char * s;
/* To derive the scrape URL use the following steps:
* Begin with the announce URL. Find the last '/' in it.
* If the text immediately following that '/' isn't 'announce'
* it will be taken as a sign that that tracker doesn't support
* the scrape convention. If it does, substitute 'scrape' for
* 'announce' to find the scrape page. */
if((( s = strrchr( announce, '/' ))) && !strncmp( ++s, "announce", 8 ))
{
struct evbuffer * buf = evbuffer_new( );
evbuffer_add( buf, announce, s-announce );
evbuffer_add( buf, "scrape", 6 );
evbuffer_add_printf( buf, "%s", s+8 );
scrape = tr_strdup( ( char * ) EVBUFFER_DATA( buf ) );
evbuffer_free( buf );
}
return scrape;
}
static void
geturllist( tr_info * inf, tr_benc * meta )
{
benc_val_t * urls;
if( tr_bencDictFindList( meta, "url-list", &urls ) )
{
int i;
const char * url;
const int n = tr_bencListSize( urls );
inf->webseedCount = 0;
inf->webseeds = tr_new0( char*, n );
for( i=0; i<n; ++i )
if( tr_bencGetStr( tr_bencListChild( urls, i ), &url ) )
inf->webseeds[inf->webseedCount++] = tr_strdup( url );
}
}
2008-04-24 03:26:36 +00:00
static int
getannounce( tr_info * inf, tr_benc * meta )
{
const char * str;
tr_tracker_info * trackers = NULL;
int trackerCount = 0;
tr_benc * tiers;
/* Announce-list */
if( tr_bencDictFindList( meta, "announce-list", &tiers ) )
{
int n;
int i, j;
n = 0;
for( i=0; i<tiers->val.l.count; ++i )
n += tiers->val.l.vals[i].val.l.count;
trackers = tr_new0( tr_tracker_info, n );
trackerCount = 0;
for( i=0; i<tiers->val.l.count; ++i ) {
const tr_benc * tier = &tiers->val.l.vals[i];
for( j=0; tr_bencIsList(tier) && j<tier->val.l.count; ++j ) {
const tr_benc * a = &tier->val.l.vals[j];
if( tr_bencIsString( a ) && tr_httpIsValidURL( a->val.s.s ) ) {
tr_tracker_info * t = trackers + trackerCount++;
t->tier = i;
t->announce = tr_strndup( a->val.s.s, a->val.s.i );
t->scrape = announceToScrape( a->val.s.s );
/*fprintf( stderr, "tier %d: %s\n", i, a->val.s.s );*/
}
}
}
/* did we use any of the tiers? */
if( !trackerCount ) {
tr_nerr( inf->name, _( "Invalid metadata entry \"%s\"" ), "announce-list" );
2008-04-24 03:26:36 +00:00
tr_free( trackers );
trackers = NULL;
}
}
/* Regular announce value */
if( !trackerCount
&& tr_bencDictFindStr( meta, "announce", &str )
&& tr_httpIsValidURL( str ) )
{
trackers = tr_new0( tr_tracker_info, 1 );
trackers[trackerCount].tier = 0;
trackers[trackerCount].announce = tr_strdup( str );
trackers[trackerCount++].scrape = announceToScrape( str );
/*fprintf( stderr, "single announce: [%s]\n", str );*/
}
inf->trackers = trackers;
inf->trackerCount = trackerCount;
if( !inf->trackerCount )
tr_nerr( inf->name, _( "Invalid metadata entry \"%s\"" ), "announce" );
2008-04-24 03:26:36 +00:00
return inf->trackerCount ? TR_OK : TR_ERROR;
}
2007-04-02 20:38:23 +00:00
int
tr_metainfoParse( const tr_handle * handle,
tr_info * inf,
const tr_benc * meta_in )
2007-12-21 22:18:40 +00:00
{
tr_piece_index_t i;
tr_benc * beInfo, * val, * val2;
tr_benc * meta = (tr_benc *) meta_in;
char buf[4096];
2007-12-21 22:18:40 +00:00
/* info_hash: urlencoded 20-byte SHA1 hash of the value of the info key
* from the Metainfo file. Note that the value will be a bencoded
* dictionary, given the definition of the info key above. */
if(( beInfo = tr_bencDictFindType( meta, "info", TYPE_DICT )))
{
int len;
char * str = tr_bencSave( beInfo, &len );
tr_sha1( inf->hash, str, len, NULL );
tr_free( str );
}
else
{
tr_err( _( "Missing metadata entry \"%s\"" ), "info" );
2007-12-21 22:18:40 +00:00
return TR_EINVALID;
}
2008-02-25 20:21:22 +00:00
tr_sha1_to_hex( inf->hashString, inf->hash );
2006-07-16 19:39:23 +00:00
/* comment */
memset( buf, '\0', sizeof( buf ) );
2007-12-21 22:18:40 +00:00
val = tr_bencDictFindFirst( meta, "comment.utf-8", "comment", NULL );
2008-04-04 17:19:44 +00:00
if( tr_bencIsString( val ) )
strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 );
tr_free( inf->comment );
inf->comment = tr_strdup( buf );
/* creator */
memset( buf, '\0', sizeof( buf ) );
2007-12-21 22:18:40 +00:00
val = tr_bencDictFindFirst( meta, "created by.utf-8", "created by", NULL );
2008-04-04 17:19:44 +00:00
if( tr_bencIsString( val ) )
strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 );
tr_free( inf->creator );
inf->creator = tr_strdup( buf );
/* Date created */
inf->dateCreated = 0;
2007-12-21 22:18:40 +00:00
val = tr_bencDictFind( meta, "creation date" );
2008-04-04 17:19:44 +00:00
if( tr_bencIsInt( val ) )
inf->dateCreated = val->val.i;
/* Private torrent */
val = tr_bencDictFind( beInfo, "private" );
2007-12-21 22:18:40 +00:00
val2 = tr_bencDictFind( meta, "private" );
2008-04-04 17:19:44 +00:00
if( ( tr_bencIsInt(val) && val->val.i ) ||
( tr_bencIsInt(val2) && val2->val.i ) )
{
2007-10-15 20:58:39 +00:00
inf->isPrivate = 1;
}
2006-07-16 19:39:23 +00:00
/* Piece length */
val = tr_bencDictFind( beInfo, "piece length" );
2008-04-04 17:19:44 +00:00
if( !tr_bencIsInt( val ) )
2006-07-16 19:39:23 +00:00
{
if( val )
tr_nerr( inf->name, _( "Invalid metadata entry \"%s\"" ), "piece length" );
else
tr_nerr( inf->name, _( "Missing metadata entry \"%s\"" ), "piece length" );
goto fail;
2006-07-16 19:39:23 +00:00
}
inf->pieceSize = val->val.i;
/* Hashes */
val = tr_bencDictFind( beInfo, "pieces" );
2008-04-04 17:19:44 +00:00
if( !tr_bencIsString( val ) )
{
if( val )
tr_nerr( inf->name, _( "Invalid metadata entry \"%s\"" ), "pieces" );
else
tr_nerr( inf->name, _( "Missing metadata entry \"%s\"" ), "pieces" );
goto fail;
}
2006-07-16 19:39:23 +00:00
if( val->val.s.i % SHA_DIGEST_LENGTH )
{
tr_nerr( inf->name, _( "Invalid metadata entry \"%s\"" ), "pieces" );
goto fail;
2006-07-16 19:39:23 +00:00
}
inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH;
inf->pieces = calloc ( inf->pieceCount, sizeof(tr_piece) );
for ( i=0; i<inf->pieceCount; ++i )
{
memcpy (inf->pieces[i].hash, &val->val.s.s[i*SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH);
}
2006-07-16 19:39:23 +00:00
/* get file or top directory name */
val = tr_bencDictFindFirst( beInfo, "name.utf-8", "name", NULL );
if( parseFiles( inf, tr_bencDictFindFirst( beInfo,
"name.utf-8", "name", NULL ),
tr_bencDictFind( beInfo, "files" ),
tr_bencDictFind( beInfo, "length" ) ) )
{
goto fail;
}
2006-07-16 19:39:23 +00:00
if( !inf->fileCount || !inf->totalSize )
{
tr_nerr( inf->name, _( "Torrent is corrupt" ) ); /* the content is missing! */
2007-07-27 01:58:49 +00:00
goto fail;
}
/* TODO add more tests so we don't crash on weird files */
2006-07-16 19:39:23 +00:00
if( (uint64_t) inf->pieceCount !=
( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize )
{
tr_nerr( inf->name, _( "Torrent is corrupt" ) ); /* size of hashes and files don't match */
goto fail;
}
/* get announce or announce-list */
2007-12-21 22:18:40 +00:00
if( getannounce( inf, meta ) )
goto fail;
2006-07-16 19:39:23 +00:00
/* get the url-list */
geturllist( inf, meta );
2008-04-14 11:52:50 +00:00
/* filename of Transmission's copy */
getTorrentFilename( handle, inf, buf, sizeof( buf ) );
tr_free( inf->torrent );
inf->torrent = tr_strdup( buf );
return TR_OK;
fail:
tr_metainfoFree( inf );
return TR_EINVALID;
2006-07-16 19:39:23 +00:00
}
void tr_metainfoFree( tr_info * inf )
{
tr_file_index_t ff;
int i;
for( i=0; i<inf->webseedCount; ++i )
tr_free( inf->webseeds[i] );
for( ff=0; ff<inf->fileCount; ++ff )
tr_free( inf->files[ff].name );
tr_free( inf->webseeds );
tr_free( inf->pieces );
tr_free( inf->files );
tr_free( inf->comment );
tr_free( inf->creator );
tr_free( inf->torrent );
tr_free( inf->name );
for( i=0; i<inf->trackerCount; ++i ) {
tr_free( inf->trackers[i].announce );
tr_free( inf->trackers[i].scrape );
}
tr_free( inf->trackers );
memset( inf, '\0', sizeof(tr_info) );
}
static int
getfile( char ** setme, const char * prefix, tr_benc * name )
{
const char ** list;
int ii, jj;
char buf[4096];
2008-04-04 17:19:44 +00:00
if( !tr_bencIsList( name ) )
return TR_EINVALID;
list = calloc( name->val.l.count, sizeof( list[0] ) );
2008-04-04 17:19:44 +00:00
if( !list )
return TR_EINVALID;
for( ii = jj = 0; name->val.l.count > ii; ii++ )
{
2008-04-04 17:19:44 +00:00
tr_benc * dir = &name->val.l.vals[ii];
if( !tr_bencIsString( dir ) )
continue;
2008-04-04 17:19:44 +00:00
if( 0 == strcmp( "..", dir->val.s.s ) )
{
if( 0 < jj )
{
jj--;
}
}
else if( 0 != strcmp( ".", dir->val.s.s ) )
{
list[jj] = dir->val.s.s;
jj++;
}
}
if( 0 == jj )
{
free( list );
return TR_EINVALID;
}
memset( buf, 0, sizeof( buf ) );
strlcat_utf8( buf, prefix, sizeof(buf), 0 );
for( ii = 0; jj > ii; ii++ )
{
strlcat_utf8( buf, TR_PATH_DELIMITER_STR, sizeof(buf), 0 );
strlcat_utf8( buf, list[ii], sizeof(buf), TR_PATH_DELIMITER );
}
free( list );
tr_free( *setme );
*setme = tr_strdup( buf );
return TR_OK;
}
void
tr_metainfoRemoveSaved( const tr_handle * handle,
2008-04-14 11:52:50 +00:00
const tr_info * inf )
2006-07-16 19:39:23 +00:00
{
2008-04-14 11:52:50 +00:00
char filename[MAX_PATH_LENGTH];
2006-07-16 19:39:23 +00:00
2008-04-14 11:52:50 +00:00
getTorrentFilename( handle, inf, filename, sizeof( filename ) );
unlink( filename );
2008-04-14 11:52:50 +00:00
getTorrentOldFilename( handle, inf, filename, sizeof( filename ) );
unlink( filename );
}
static int
parseFiles( tr_info * inf, tr_benc * name,
tr_benc * files, tr_benc * length )
{
tr_benc * item, * path;
int ii;
char buf[4096];
2008-04-04 17:19:44 +00:00
if( !tr_bencIsString( name ) )
{
if( name )
2008-03-07 03:26:59 +00:00
tr_err( _( "Invalid metadata entry \"%s\"" ), "name" );
else
2008-03-07 03:26:59 +00:00
tr_err( _( "Missing metadata entry \"%s\"" ), "name" );
return TR_EINVALID;
}
memset( buf, 0, sizeof( buf ) );
strlcat_utf8( buf, name->val.s.s, sizeof( buf ), 0 );
tr_free( inf->name );
inf->name = tr_strdup( buf );
if( !inf->name || !*inf->name )
{
2008-03-07 03:26:59 +00:00
tr_err( _( "Invalid metadata entry \"%s\"" ), "name" );
return TR_EINVALID;
}
inf->totalSize = 0;
2008-04-04 17:19:44 +00:00
if( tr_bencIsList( files ) )
{
/* Multi-file mode */
2007-10-15 20:58:39 +00:00
inf->isMultifile = 1;
inf->fileCount = files->val.l.count;
inf->files = calloc( inf->fileCount, sizeof( inf->files[0] ) );
if( !inf->files )
return TR_EINVALID;
for( ii = 0; files->val.l.count > ii; ii++ )
{
item = &files->val.l.vals[ii];
path = tr_bencDictFindFirst( item, "path.utf-8", "path", NULL );
if( getfile( &inf->files[ii].name, inf->name, path ) )
{
if( path )
tr_nerr( inf->name, _( "Invalid metadata entry \"%s\"" ), "path" );
else
tr_nerr( inf->name, _( "Missing metadata entry \"%s\"" ), "path" );
return TR_EINVALID;
}
length = tr_bencDictFind( item, "length" );
2008-04-04 17:19:44 +00:00
if( !tr_bencIsInt( length ) )
{
if( length )
tr_nerr( inf->name, _( "Invalid metadata entry \"%s\"" ), "length" );
else
tr_nerr( inf->name, _( "Missing metadata entry \"%s\"" ), "length" );
return TR_EINVALID;
}
2007-03-31 19:10:32 +00:00
inf->files[ii].length = length->val.i;
inf->totalSize += length->val.i;
}
}
2008-04-04 17:19:44 +00:00
else if( tr_bencIsInt( length ) )
{
char buf[4096];
/* Single-file mode */
2007-10-15 20:58:39 +00:00
inf->isMultifile = 0;
inf->fileCount = 1;
inf->files = calloc( 1, sizeof( inf->files[0] ) );
if( !inf->files )
return TR_EINVALID;
memset( buf, 0, sizeof( buf ) );
strlcat_utf8( buf, name->val.s.s, sizeof(buf), TR_PATH_DELIMITER );
tr_free( inf->files[0].name );
inf->files[0].name = tr_strdup( buf );
inf->files[0].length = length->val.i;
inf->totalSize += length->val.i;
}
else
{
tr_nerr( inf->name, _( "Invalid or missing metadata entries \"length\" and \"files\"" ) );
}
return TR_OK;
}