2009-12-07 03:57:55 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2009-12-07 03:57:55 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2009-12-07 03:57:55 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2008-08-21 14:57:59 +00:00
|
|
|
#include <string.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h> /* unlink, stat */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "bencode.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "crypto.h" /* tr_sha1 */
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "metainfo.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "platform.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-20 15:17:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-04-20 23:14:00 +00:00
|
|
|
char*
|
|
|
|
tr_metainfoGetBasename( const tr_info * inf )
|
|
|
|
{
|
|
|
|
char *ret, *pch, *name;
|
|
|
|
|
|
|
|
name = tr_strdup( inf->name );
|
|
|
|
for( pch=name; pch && *pch; ++pch )
|
|
|
|
if( *pch == '/' )
|
|
|
|
*pch = '_';
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2010-04-20 23:14:00 +00:00
|
|
|
ret = tr_strdup_printf( "%s.%16.16s", name, inf->hashString );
|
|
|
|
|
|
|
|
tr_free( name );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-20 19:00:52 +00:00
|
|
|
static char*
|
2008-12-14 11:21:11 +00:00
|
|
|
getTorrentFilename( const tr_session * session,
|
2008-09-23 19:11:04 +00:00
|
|
|
const tr_info * inf )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
2010-04-20 23:14:00 +00:00
|
|
|
char * base = tr_metainfoGetBasename( inf );
|
|
|
|
char * filename = tr_strdup_printf( "%s" TR_PATH_DELIMITER_STR "%s.torrent",
|
|
|
|
tr_getTorrentDir( session ), base );
|
|
|
|
tr_free( base );
|
|
|
|
return filename;
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 19:00:52 +00:00
|
|
|
static char*
|
2009-07-03 14:31:31 +00:00
|
|
|
getOldTorrentFilename( const tr_session * session, const tr_info * inf )
|
2007-12-25 01:23:07 +00:00
|
|
|
{
|
2009-07-03 14:31:31 +00:00
|
|
|
int i;
|
|
|
|
char * path;
|
|
|
|
struct stat sb;
|
2009-07-03 14:37:27 +00:00
|
|
|
const int tagCount = 5;
|
|
|
|
const char * tags[] = { "beos", "cli", "daemon", "macosx", "wx" };
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2009-07-03 14:37:27 +00:00
|
|
|
/* test the beos, cli, daemon, macosx, wx tags */
|
2009-07-03 14:31:31 +00:00
|
|
|
for( i=0; i<tagCount; ++i ) {
|
|
|
|
path = tr_strdup_printf( "%s%c%s-%s", tr_getTorrentDir( session ), '/', inf->hashString, tags[i] );
|
|
|
|
if( !stat( path, &sb ) && ( ( sb.st_mode & S_IFMT ) == S_IFREG ) )
|
|
|
|
return path;
|
|
|
|
tr_free( path );
|
|
|
|
}
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2009-07-03 14:37:27 +00:00
|
|
|
/* test a non-tagged file */
|
2009-07-03 14:31:31 +00:00
|
|
|
path = tr_buildPath( tr_getTorrentDir( session ), inf->hashString, NULL );
|
|
|
|
if( !stat( path, &sb ) && ( ( sb.st_mode & S_IFMT ) == S_IFREG ) )
|
|
|
|
return path;
|
|
|
|
tr_free( path );
|
|
|
|
|
2009-07-03 14:37:27 +00:00
|
|
|
/* return the -gtk form by default, since that's the most common case.
|
|
|
|
don't bother testing stat() on it since this is the last candidate
|
|
|
|
and we don't want to return NULL anyway */
|
|
|
|
return tr_strdup_printf( "%s%c%s-%s", tr_getTorrentDir( session ), '/', inf->hashString, "gtk" );
|
2007-12-25 01:23:07 +00:00
|
|
|
}
|
|
|
|
|
2009-05-21 19:54:59 +00:00
|
|
|
/* this is for really old versions of T and will probably be removed someday */
|
2008-04-14 11:52:50 +00:00
|
|
|
void
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_metainfoMigrate( tr_session * session,
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_info * inf )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
|
|
|
struct stat new_sb;
|
2008-12-14 11:21:11 +00:00
|
|
|
char * name = getTorrentFilename( session, inf );
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2008-08-21 16:12:17 +00:00
|
|
|
if( stat( name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
2008-12-14 11:21:11 +00:00
|
|
|
char * old_name = getOldTorrentFilename( session, inf );
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t contentLen;
|
2008-04-14 11:52:50 +00:00
|
|
|
uint8_t * content;
|
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_mkdirp( tr_getTorrentDir( session ), 0777 );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( content = tr_loadFile( old_name, &contentLen ) ) )
|
2008-04-14 11:52:50 +00:00
|
|
|
{
|
2008-05-19 14:06:44 +00:00
|
|
|
FILE * out;
|
|
|
|
errno = 0;
|
2008-08-21 16:12:17 +00:00
|
|
|
out = fopen( name, "wb+" );
|
2008-05-19 14:06:44 +00:00
|
|
|
if( !out )
|
2008-04-14 14:39:13 +00:00
|
|
|
{
|
2008-08-21 16:12:17 +00:00
|
|
|
tr_nerr( inf->name, _( "Couldn't create \"%1$s\": %2$s" ),
|
2008-09-23 19:11:04 +00:00
|
|
|
name, tr_strerror( errno ) );
|
2008-05-19 14:06:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-21 16:12:17 +00:00
|
|
|
if( fwrite( content, sizeof( uint8_t ), contentLen, out )
|
2008-09-23 19:11:04 +00:00
|
|
|
== contentLen )
|
2008-05-19 14:06:44 +00:00
|
|
|
{
|
|
|
|
tr_free( inf->torrent );
|
2008-08-21 16:12:17 +00:00
|
|
|
inf->torrent = tr_strdup( name );
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_sessionSetTorrentFile( session, inf->hashString, name );
|
2008-05-19 14:06:44 +00:00
|
|
|
unlink( old_name );
|
|
|
|
}
|
|
|
|
fclose( out );
|
2008-04-14 14:39:13 +00:00
|
|
|
}
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( content );
|
2008-08-20 19:00:52 +00:00
|
|
|
tr_free( old_name );
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
2008-08-20 19:00:52 +00:00
|
|
|
|
2008-08-21 16:12:17 +00:00
|
|
|
tr_free( name );
|
2008-04-14 11:52:50 +00:00
|
|
|
}
|
2007-12-25 01:23:07 +00:00
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-08-05 01:59:16 +00:00
|
|
|
static tr_bool
|
2009-12-28 00:35:29 +00:00
|
|
|
path_is_suspicious( const char * path )
|
|
|
|
{
|
|
|
|
return ( path == NULL )
|
|
|
|
|| ( strstr( path, "../" ) != NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_bool
|
|
|
|
getfile( char ** setme, const char * root, tr_benc * path )
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2009-08-05 01:59:16 +00:00
|
|
|
tr_bool success = FALSE;
|
2008-08-20 21:01:17 +00:00
|
|
|
|
2009-08-05 01:59:16 +00:00
|
|
|
if( tr_bencIsList( path ) )
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2009-12-28 00:35:29 +00:00
|
|
|
int i;
|
2010-12-20 02:07:51 +00:00
|
|
|
char * tmp;
|
2009-12-28 00:35:29 +00:00
|
|
|
const int n = tr_bencListSize( path );
|
2009-06-14 01:00:36 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2008-08-20 21:01:17 +00:00
|
|
|
|
|
|
|
evbuffer_add( buf, root, strlen( root ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
|
|
|
{
|
2008-08-20 21:01:17 +00:00
|
|
|
const char * str;
|
2009-12-28 00:35:29 +00:00
|
|
|
if( tr_bencGetStr( tr_bencListChild( path, i ), &str ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-08-20 21:01:17 +00:00
|
|
|
evbuffer_add( buf, TR_PATH_DELIMITER_STR, 1 );
|
|
|
|
evbuffer_add( buf, str, strlen( str ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
tmp = evbuffer_free_to_str( buf );
|
|
|
|
*setme = tr_utf8clean( tmp, -1 );
|
|
|
|
tr_free( tmp );
|
2008-08-20 21:01:17 +00:00
|
|
|
/* fprintf( stderr, "[%s]\n", *setme ); */
|
2009-08-05 01:59:16 +00:00
|
|
|
success = TRUE;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 00:35:29 +00:00
|
|
|
if( ( *setme != NULL ) && path_is_suspicious( *setme ) )
|
|
|
|
{
|
|
|
|
tr_free( *setme );
|
|
|
|
*setme = NULL;
|
|
|
|
success = FALSE;
|
|
|
|
}
|
|
|
|
|
2009-08-05 01:59:16 +00:00
|
|
|
return success;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
2009-12-28 00:35:29 +00:00
|
|
|
parseFiles( tr_info * inf, tr_benc * files, const tr_benc * length )
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2008-08-21 16:12:17 +00:00
|
|
|
int64_t len;
|
2008-08-20 21:01:17 +00:00
|
|
|
|
|
|
|
inf->totalSize = 0;
|
|
|
|
|
|
|
|
if( tr_bencIsList( files ) ) /* multi-file mode */
|
|
|
|
{
|
2008-08-21 16:12:17 +00:00
|
|
|
tr_file_index_t i;
|
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
inf->isMultifile = 1;
|
|
|
|
inf->fileCount = tr_bencListSize( files );
|
|
|
|
inf->files = tr_new0( tr_file, inf->fileCount );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < inf->fileCount; ++i )
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
|
|
|
tr_benc * file;
|
|
|
|
tr_benc * path;
|
|
|
|
|
|
|
|
file = tr_bencListChild( files, i );
|
|
|
|
if( !tr_bencIsDict( file ) )
|
|
|
|
return "files";
|
|
|
|
|
|
|
|
if( !tr_bencDictFindList( file, "path.utf-8", &path ) )
|
|
|
|
if( !tr_bencDictFindList( file, "path", &path ) )
|
|
|
|
return "path";
|
|
|
|
|
2009-08-05 01:59:16 +00:00
|
|
|
if( !getfile( &inf->files[i].name, inf->name, path ) )
|
2008-08-20 21:01:17 +00:00
|
|
|
return "path";
|
|
|
|
|
2009-01-01 18:38:49 +00:00
|
|
|
if( !tr_bencDictFindInt( file, "length", &len ) )
|
2008-08-20 21:01:17 +00:00
|
|
|
return "length";
|
|
|
|
|
2009-01-01 18:38:49 +00:00
|
|
|
inf->files[i].length = len;
|
|
|
|
inf->totalSize += len;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-21 16:12:17 +00:00
|
|
|
else if( tr_bencGetInt( length, &len ) ) /* single-file mode */
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2009-12-28 00:35:29 +00:00
|
|
|
if( path_is_suspicious( inf->name ) )
|
|
|
|
return "path";
|
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
inf->isMultifile = 0;
|
|
|
|
inf->fileCount = 1;
|
|
|
|
inf->files = tr_new0( tr_file, 1 );
|
|
|
|
inf->files[0].name = tr_strdup( inf->name );
|
2008-08-21 16:12:17 +00:00
|
|
|
inf->files[0].length = len;
|
|
|
|
inf->totalSize += len;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "length";
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-24 03:26:36 +00:00
|
|
|
static char *
|
2009-11-24 02:16:31 +00:00
|
|
|
tr_convertAnnounceToScrape( const char * announce )
|
2008-04-24 03:26:36 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * scrape = NULL;
|
2008-04-24 03:26:36 +00:00
|
|
|
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
|
2010-12-27 19:18:17 +00:00
|
|
|
* 'announce' to find the scrape page. */
|
2009-06-14 14:23:43 +00:00
|
|
|
if( ( ( s = strrchr( announce, '/' ) ) ) && !strncmp( ++s, "announce", 8 ) )
|
2008-04-24 03:26:36 +00:00
|
|
|
{
|
2009-06-14 14:23:43 +00:00
|
|
|
const char * prefix = announce;
|
|
|
|
const size_t prefix_len = s - announce;
|
|
|
|
const char * suffix = s + 8;
|
|
|
|
const size_t suffix_len = strlen( suffix );
|
|
|
|
const size_t alloc_len = prefix_len + 6 + suffix_len + 1;
|
|
|
|
char * walk = scrape = tr_new( char, alloc_len );
|
|
|
|
memcpy( walk, prefix, prefix_len ); walk += prefix_len;
|
|
|
|
memcpy( walk, "scrape", 6 ); walk += 6;
|
|
|
|
memcpy( walk, suffix, suffix_len ); walk += suffix_len;
|
|
|
|
*walk++ = '\0';
|
2009-06-14 14:39:51 +00:00
|
|
|
assert( walk - scrape == (int)alloc_len );
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return scrape;
|
|
|
|
}
|
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
static const char*
|
2009-11-25 05:13:58 +00:00
|
|
|
getannounce( tr_info * inf, tr_benc * meta )
|
2008-04-24 03:26:36 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * str;
|
2008-04-24 03:26:36 +00:00
|
|
|
tr_tracker_info * trackers = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
int trackerCount = 0;
|
|
|
|
tr_benc * tiers;
|
2008-04-24 03:26:36 +00:00
|
|
|
|
|
|
|
/* Announce-list */
|
|
|
|
if( tr_bencDictFindList( meta, "announce-list", &tiers ) )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int n;
|
2009-01-03 21:27:48 +00:00
|
|
|
int i, j, validTiers;
|
2008-08-21 16:12:17 +00:00
|
|
|
const int numTiers = tr_bencListSize( tiers );
|
2008-04-24 03:26:36 +00:00
|
|
|
|
|
|
|
n = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < numTiers; ++i )
|
2008-08-21 16:12:17 +00:00
|
|
|
n += tr_bencListSize( tr_bencListChild( tiers, i ) );
|
2008-04-24 03:26:36 +00:00
|
|
|
|
|
|
|
trackers = tr_new0( tr_tracker_info, n );
|
|
|
|
|
2009-01-03 21:27:48 +00:00
|
|
|
for( i = 0, validTiers = 0; i < numTiers; ++i )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-08-21 16:12:17 +00:00
|
|
|
tr_benc * tier = tr_bencListChild( tiers, i );
|
|
|
|
const int tierSize = tr_bencListSize( tier );
|
2009-01-03 21:27:48 +00:00
|
|
|
tr_bool anyAdded = FALSE;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( j = 0; j < tierSize; ++j )
|
|
|
|
{
|
|
|
|
if( tr_bencGetStr( tr_bencListChild( tier, j ), &str ) )
|
|
|
|
{
|
2008-09-05 19:11:30 +00:00
|
|
|
char * url = tr_strstrip( tr_strdup( str ) );
|
2010-02-20 15:57:05 +00:00
|
|
|
if( tr_urlIsValidTracker( url ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2010-01-10 02:34:00 +00:00
|
|
|
tr_tracker_info * t = trackers + trackerCount;
|
2009-01-03 21:27:48 +00:00
|
|
|
t->tier = validTiers;
|
2008-09-05 19:11:30 +00:00
|
|
|
t->announce = tr_strdup( url );
|
2009-11-24 02:16:31 +00:00
|
|
|
t->scrape = tr_convertAnnounceToScrape( url );
|
2010-01-13 22:40:07 +00:00
|
|
|
t->id = trackerCount;
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2009-01-03 21:27:48 +00:00
|
|
|
anyAdded = TRUE;
|
2010-01-10 02:34:00 +00:00
|
|
|
++trackerCount;
|
2008-09-05 19:11:30 +00:00
|
|
|
}
|
|
|
|
tr_free( url );
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2009-01-03 21:27:48 +00:00
|
|
|
if( anyAdded )
|
|
|
|
++validTiers;
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* did we use any of the tiers? */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !trackerCount )
|
|
|
|
{
|
2008-04-24 03:26:36 +00:00
|
|
|
tr_free( trackers );
|
|
|
|
trackers = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Regular announce value */
|
|
|
|
if( !trackerCount
|
2008-09-23 19:11:04 +00:00
|
|
|
&& tr_bencDictFindStr( meta, "announce", &str ) )
|
2008-04-24 03:26:36 +00:00
|
|
|
{
|
2008-09-05 19:11:30 +00:00
|
|
|
char * url = tr_strstrip( tr_strdup( str ) );
|
2010-02-20 15:57:05 +00:00
|
|
|
if( tr_urlIsValidTracker( url ) )
|
2008-09-05 19:11:30 +00:00
|
|
|
{
|
|
|
|
trackers = tr_new0( tr_tracker_info, 1 );
|
|
|
|
trackers[trackerCount].tier = 0;
|
|
|
|
trackers[trackerCount].announce = tr_strdup( url );
|
2010-01-10 02:34:00 +00:00
|
|
|
trackers[trackerCount].scrape = tr_convertAnnounceToScrape( url );
|
2010-01-13 22:40:07 +00:00
|
|
|
trackers[trackerCount].id = 0;
|
2010-01-10 02:34:00 +00:00
|
|
|
trackerCount++;
|
2008-09-05 19:11:30 +00:00
|
|
|
/*fprintf( stderr, "single announce: [%s]\n", url );*/
|
|
|
|
}
|
|
|
|
tr_free( url );
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inf->trackers = trackers;
|
|
|
|
inf->trackerCount = trackerCount;
|
|
|
|
|
2009-11-25 05:13:58 +00:00
|
|
|
return NULL;
|
2008-08-20 21:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
geturllist( tr_info * inf,
|
|
|
|
tr_benc * meta )
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2008-08-21 14:57:59 +00:00
|
|
|
tr_benc * urls;
|
2010-08-22 18:40:18 +00:00
|
|
|
const char * url;
|
2008-08-20 21:01:17 +00:00
|
|
|
|
|
|
|
if( tr_bencDictFindList( meta, "url-list", &urls ) )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
const int n = tr_bencListSize( urls );
|
2008-08-20 21:01:17 +00:00
|
|
|
|
|
|
|
inf->webseedCount = 0;
|
|
|
|
inf->webseeds = tr_new0( char*, n );
|
2008-05-29 00:38:31 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
2008-08-20 21:01:17 +00:00
|
|
|
if( tr_bencGetStr( tr_bencListChild( urls, i ), &url ) )
|
|
|
|
inf->webseeds[inf->webseedCount++] = tr_strdup( url );
|
|
|
|
}
|
2010-08-22 18:40:18 +00:00
|
|
|
else if( tr_bencDictFindStr( meta, "url-list", &url ) ) /* handle single items in webseeds */
|
|
|
|
{
|
|
|
|
inf->webseedCount = 1;
|
|
|
|
inf->webseeds = tr_new0( char*, 1 );
|
|
|
|
inf->webseeds[0] = tr_strdup( url );
|
|
|
|
}
|
2008-04-24 03:26:36 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static int
|
|
|
|
is_rfc2396_alnum( char ch )
|
|
|
|
{
|
|
|
|
return ( '0' <= ch && ch <= '9' )
|
|
|
|
|| ( 'A' <= ch && ch <= 'Z' )
|
2010-08-31 11:49:09 +00:00
|
|
|
|| ( 'a' <= ch && ch <= 'z' )
|
|
|
|
|| ch == '.'
|
|
|
|
|| ch == '-'
|
|
|
|
|| ch == '_'
|
|
|
|
|| ch == '~';
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
escape( char * out, const uint8_t * in, size_t in_len ) /* rfc2396 */
|
|
|
|
{
|
|
|
|
const uint8_t *end = in + in_len;
|
|
|
|
|
|
|
|
while( in != end )
|
|
|
|
if( is_rfc2396_alnum( *in ) )
|
|
|
|
*out++ = (char) *in++;
|
|
|
|
else
|
2010-08-31 11:49:09 +00:00
|
|
|
out += tr_snprintf( out, 4, "%%%02x", (unsigned int)*in++ );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
*out = '\0';
|
|
|
|
}
|
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
static const char*
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_metainfoParseImpl( const tr_session * session,
|
|
|
|
tr_info * inf,
|
|
|
|
tr_bool * hasInfoDict,
|
|
|
|
int * infoDictLength,
|
|
|
|
const tr_benc * meta_in )
|
2007-12-21 22:18:40 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int64_t i;
|
|
|
|
size_t raw_len;
|
|
|
|
const char * str;
|
2008-08-20 19:21:57 +00:00
|
|
|
const uint8_t * raw;
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_benc * d;
|
|
|
|
tr_benc * infoDict = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * meta = (tr_benc *) meta_in;
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bool b;
|
|
|
|
tr_bool isMagnet = FALSE;
|
2007-12-21 22:18:40 +00:00
|
|
|
|
|
|
|
/* info_hash: urlencoded 20-byte SHA1 hash of the value of the info key
|
2008-09-23 19:11:04 +00:00
|
|
|
* from the Metainfo file. Note that the value will be a bencoded
|
2007-12-21 22:18:40 +00:00
|
|
|
* dictionary, given the definition of the info key above. */
|
2010-02-02 22:45:22 +00:00
|
|
|
b = tr_bencDictFindDict( meta, "info", &infoDict );
|
|
|
|
if( hasInfoDict != NULL )
|
|
|
|
*hasInfoDict = b;
|
|
|
|
if( !b )
|
|
|
|
{
|
|
|
|
/* no info dictionary... is this a magnet link? */
|
|
|
|
if( tr_bencDictFindDict( meta, "magnet-info", &d ) )
|
|
|
|
{
|
|
|
|
isMagnet = TRUE;
|
|
|
|
|
|
|
|
/* get the info-hash */
|
|
|
|
if( !tr_bencDictFindRaw( d, "info_hash", &raw, &raw_len ) )
|
|
|
|
return "info_hash";
|
|
|
|
if( raw_len != SHA_DIGEST_LENGTH )
|
|
|
|
return "info_hash";
|
|
|
|
memcpy( inf->hash, raw, raw_len );
|
|
|
|
tr_sha1_to_hex( inf->hashString, inf->hash );
|
|
|
|
escape( inf->hashEscaped, inf->hash, SHA_DIGEST_LENGTH );
|
|
|
|
|
|
|
|
/* maybe get the display name */
|
|
|
|
if( tr_bencDictFindStr( d, "display-name", &str ) ) {
|
|
|
|
tr_free( inf->name );
|
|
|
|
inf->name = tr_strdup( str );
|
|
|
|
}
|
2010-08-03 03:16:21 +00:00
|
|
|
|
|
|
|
if( !inf->name )
|
|
|
|
inf->name = tr_strdup( inf->hashString );
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
|
|
|
else /* not a magnet link and has no info dict... */
|
|
|
|
{
|
|
|
|
return "info";
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2009-06-02 01:48:48 +00:00
|
|
|
int len;
|
2010-02-02 22:45:22 +00:00
|
|
|
char * bstr = tr_bencToStr( infoDict, TR_FMT_BENC, &len );
|
2009-01-01 18:38:49 +00:00
|
|
|
tr_sha1( inf->hash, bstr, len, NULL );
|
2008-08-20 19:21:57 +00:00
|
|
|
tr_sha1_to_hex( inf->hashString, inf->hash );
|
2009-09-25 21:05:59 +00:00
|
|
|
escape( inf->hashEscaped, inf->hash, SHA_DIGEST_LENGTH );
|
2009-11-24 02:16:31 +00:00
|
|
|
|
|
|
|
if( infoDictLength != NULL )
|
|
|
|
*infoDictLength = len;
|
|
|
|
|
2009-01-01 18:38:49 +00:00
|
|
|
tr_free( bstr );
|
2007-12-21 22:18:40 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-08-20 19:21:57 +00:00
|
|
|
/* name */
|
2010-02-02 22:45:22 +00:00
|
|
|
if( !isMagnet ) {
|
|
|
|
if( !tr_bencDictFindStr( infoDict, "name.utf-8", &str ) )
|
|
|
|
if( !tr_bencDictFindStr( infoDict, "name", &str ) )
|
|
|
|
str = "";
|
|
|
|
if( !str || !*str )
|
|
|
|
return "name";
|
|
|
|
tr_free( inf->name );
|
2010-07-07 16:48:23 +00:00
|
|
|
inf->name = tr_utf8clean( str, -1 );
|
2010-02-02 22:45:22 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-02-15 14:56:11 +00:00
|
|
|
/* comment */
|
2008-08-20 19:21:57 +00:00
|
|
|
if( !tr_bencDictFindStr( meta, "comment.utf-8", &str ) )
|
|
|
|
if( !tr_bencDictFindStr( meta, "comment", &str ) )
|
|
|
|
str = "";
|
2008-02-15 14:56:11 +00:00
|
|
|
tr_free( inf->comment );
|
2010-07-07 16:48:23 +00:00
|
|
|
inf->comment = tr_utf8clean( str, -1 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
/* created by */
|
2008-08-20 19:21:57 +00:00
|
|
|
if( !tr_bencDictFindStr( meta, "created by.utf-8", &str ) )
|
|
|
|
if( !tr_bencDictFindStr( meta, "created by", &str ) )
|
|
|
|
str = "";
|
2008-02-15 14:56:11 +00:00
|
|
|
tr_free( inf->creator );
|
2010-07-07 16:48:23 +00:00
|
|
|
inf->creator = tr_utf8clean( str, -1 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
/* creation date */
|
2008-08-20 19:21:57 +00:00
|
|
|
if( !tr_bencDictFindInt( meta, "creation date", &i ) )
|
|
|
|
i = 0;
|
|
|
|
inf->dateCreated = i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
/* private */
|
2010-02-02 22:45:22 +00:00
|
|
|
if( !tr_bencDictFindInt( infoDict, "private", &i ) )
|
2008-08-20 19:21:57 +00:00
|
|
|
if( !tr_bencDictFindInt( meta, "private", &i ) )
|
|
|
|
i = 0;
|
|
|
|
inf->isPrivate = i != 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
/* piece length */
|
2010-02-02 22:45:22 +00:00
|
|
|
if( !isMagnet ) {
|
|
|
|
if( !tr_bencDictFindInt( infoDict, "piece length", &i ) || ( i < 1 ) )
|
|
|
|
return "piece length";
|
|
|
|
inf->pieceSize = i;
|
|
|
|
}
|
2008-08-20 21:01:17 +00:00
|
|
|
|
|
|
|
/* pieces */
|
2010-02-02 22:45:22 +00:00
|
|
|
if( !isMagnet ) {
|
|
|
|
if( !tr_bencDictFindRaw( infoDict, "pieces", &raw, &raw_len ) )
|
|
|
|
return "pieces";
|
|
|
|
if( raw_len % SHA_DIGEST_LENGTH )
|
|
|
|
return "pieces";
|
|
|
|
inf->pieceCount = raw_len / SHA_DIGEST_LENGTH;
|
|
|
|
inf->pieces = tr_new0( tr_piece, inf->pieceCount );
|
|
|
|
for( i = 0; i < inf->pieceCount; ++i )
|
|
|
|
memcpy( inf->pieces[i].hash, &raw[i * SHA_DIGEST_LENGTH],
|
|
|
|
SHA_DIGEST_LENGTH );
|
|
|
|
}
|
2008-08-20 19:21:57 +00:00
|
|
|
|
|
|
|
/* files */
|
2010-02-02 22:45:22 +00:00
|
|
|
if( !isMagnet ) {
|
|
|
|
if( ( str = parseFiles( inf, tr_bencDictFind( infoDict, "files" ),
|
|
|
|
tr_bencDictFind( infoDict, "length" ) ) ) )
|
|
|
|
return str;
|
|
|
|
if( !inf->fileCount || !inf->totalSize )
|
|
|
|
return "files";
|
|
|
|
if( (uint64_t) inf->pieceCount !=
|
|
|
|
( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize )
|
|
|
|
return "files";
|
|
|
|
}
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
/* get announce or announce-list */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( str = getannounce( inf, meta ) ) )
|
2008-08-20 21:01:17 +00:00
|
|
|
return str;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
/* get the url-list */
|
|
|
|
geturllist( inf, meta );
|
|
|
|
|
2008-04-14 11:52:50 +00:00
|
|
|
/* filename of Transmission's copy */
|
|
|
|
tr_free( inf->torrent );
|
2009-04-02 17:30:29 +00:00
|
|
|
inf->torrent = session ? getTorrentFilename( session, inf ) : NULL;
|
2008-04-14 11:52:50 +00:00
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-05 00:07:48 +00:00
|
|
|
|
2009-08-05 01:59:16 +00:00
|
|
|
tr_bool
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_metainfoParse( const tr_session * session,
|
2010-02-02 22:45:22 +00:00
|
|
|
const tr_benc * meta_in,
|
2009-08-05 01:59:16 +00:00
|
|
|
tr_info * inf,
|
2010-02-02 22:45:22 +00:00
|
|
|
tr_bool * hasInfoDict,
|
|
|
|
int * infoDictLength )
|
2008-08-20 21:01:17 +00:00
|
|
|
{
|
2009-11-24 02:16:31 +00:00
|
|
|
const char * badTag = tr_metainfoParseImpl( session,
|
|
|
|
inf,
|
2010-02-02 22:45:22 +00:00
|
|
|
hasInfoDict,
|
2009-11-24 02:16:31 +00:00
|
|
|
infoDictLength,
|
|
|
|
meta_in );
|
2009-08-05 01:59:16 +00:00
|
|
|
const tr_bool success = badTag == NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
if( badTag )
|
|
|
|
{
|
|
|
|
tr_nerr( inf->name, _( "Invalid metadata entry \"%s\"" ), badTag );
|
|
|
|
tr_metainfoFree( inf );
|
|
|
|
}
|
2009-08-05 01:59:16 +00:00
|
|
|
|
|
|
|
return success;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 21:01:17 +00:00
|
|
|
void
|
|
|
|
tr_metainfoFree( tr_info * inf )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2009-11-24 02:16:31 +00:00
|
|
|
int i;
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_file_index_t ff;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < inf->webseedCount; ++i )
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_free( inf->webseeds[i] );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( ff = 0; ff < inf->fileCount; ++ff )
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_free( inf->files[ff].name );
|
2008-02-14 00:52:58 +00:00
|
|
|
|
2008-06-07 21:26:41 +00:00
|
|
|
tr_free( inf->webseeds );
|
2007-06-28 02:02:30 +00:00
|
|
|
tr_free( inf->pieces );
|
|
|
|
tr_free( inf->files );
|
2008-02-14 01:12:00 +00:00
|
|
|
tr_free( inf->comment );
|
|
|
|
tr_free( inf->creator );
|
2008-03-23 03:15:05 +00:00
|
|
|
tr_free( inf->torrent );
|
|
|
|
tr_free( inf->name );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
for( i = 0; i < inf->trackerCount; ++i )
|
|
|
|
{
|
2008-04-24 01:42:53 +00:00
|
|
|
tr_free( inf->trackers[i].announce );
|
|
|
|
tr_free( inf->trackers[i].scrape );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
tr_free( inf->trackers );
|
2007-07-25 17:19:29 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
memset( inf, '\0', sizeof( tr_info ) );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-12-25 01:23:07 +00:00
|
|
|
void
|
2008-12-14 11:21:11 +00:00
|
|
|
tr_metainfoRemoveSaved( const tr_session * session,
|
2008-09-23 19:11:04 +00:00
|
|
|
const tr_info * inf )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-08-20 19:00:52 +00:00
|
|
|
char * filename;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
filename = getTorrentFilename( session, inf );
|
2008-04-14 11:52:50 +00:00
|
|
|
unlink( filename );
|
2008-08-20 19:00:52 +00:00
|
|
|
tr_free( filename );
|
2007-03-13 06:56:50 +00:00
|
|
|
|
2008-12-14 11:21:11 +00:00
|
|
|
filename = getOldTorrentFilename( session, inf );
|
2008-04-14 11:52:50 +00:00
|
|
|
unlink( filename );
|
2008-08-20 19:00:52 +00:00
|
|
|
tr_free( filename );
|
2007-03-13 06:56:50 +00:00
|
|
|
}
|