2007-06-18 03:40:41 +00:00
|
|
|
/*
|
2008-01-01 17:20:20 +00:00
|
|
|
* This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
|
2007-06-18 03:40:41 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* 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.
|
2007-08-18 17:19:49 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
2007-06-18 03:40:41 +00:00
|
|
|
*/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <errno.h>
|
2008-07-15 17:16:57 +00:00
|
|
|
#include <stdio.h> /* FILE, stderr */
|
2008-02-19 18:39:49 +00:00
|
|
|
#include <stdlib.h> /* qsort */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2007-10-30 20:11:23 +00:00
|
|
|
#include <libgen.h> /* dirname, basename */
|
2007-06-18 03:40:41 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "crypto.h" /* tr_sha1 */
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "bencode.h"
|
|
|
|
#include "makemeta.h"
|
|
|
|
#include "platform.h" /* threads, locks */
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h" /* buildpath */
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "version.h"
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
struct FileList
|
|
|
|
{
|
|
|
|
struct FileList * next;
|
2007-06-19 00:08:39 +00:00
|
|
|
uint64_t size;
|
|
|
|
char * filename;
|
2007-06-18 03:40:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct FileList*
|
|
|
|
getFiles( const char * dir,
|
|
|
|
const char * base,
|
|
|
|
struct FileList * list )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char buf[MAX_PATH_LENGTH];
|
|
|
|
struct stat sb;
|
|
|
|
DIR * odir = NULL;
|
|
|
|
sb.st_size = 0;
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
tr_buildPath( buf, sizeof(buf), dir, base, NULL );
|
2007-06-18 03:40:41 +00:00
|
|
|
i = stat( buf, &sb );
|
|
|
|
if( i ) {
|
2008-03-07 20:48:36 +00:00
|
|
|
tr_err( _( "Torrent Creator is skipping file \"%s\": %s" ), buf, tr_strerror(errno));
|
2007-06-18 03:40:41 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( S_ISDIR( sb.st_mode ) && (( odir = opendir ( buf ) )) )
|
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
for (d = readdir( odir ); d!=NULL; d=readdir( odir ) )
|
2007-06-19 01:49:56 +00:00
|
|
|
if( d->d_name && d->d_name[0]!='.' ) /* skip dotfiles, ., and .. */
|
2007-06-18 03:40:41 +00:00
|
|
|
list = getFiles( buf, d->d_name, list );
|
|
|
|
closedir( odir );
|
|
|
|
}
|
|
|
|
else if( S_ISREG( sb.st_mode ) )
|
|
|
|
{
|
2007-06-27 05:14:38 +00:00
|
|
|
struct FileList * node = tr_new( struct FileList, 1 );
|
2007-06-19 00:08:39 +00:00
|
|
|
node->size = sb.st_size;
|
2008-02-20 11:36:42 +00:00
|
|
|
if( ( buf[0]=='.' ) && ( buf[1]=='/' ) )
|
|
|
|
node->filename = tr_strdup( buf + 2 );
|
|
|
|
else
|
|
|
|
node->filename = tr_strdup( buf );
|
2007-06-18 03:40:41 +00:00
|
|
|
node->next = list;
|
|
|
|
list = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-07-01 14:04:18 +00:00
|
|
|
bestPieceSize( uint64_t totalSize )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-07-01 14:04:18 +00:00
|
|
|
static const uint64_t GiB = 1073741824;
|
|
|
|
static const uint64_t MiB = 1048576;
|
|
|
|
static const uint64_t KiB = 1024;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-08-07 19:25:19 +00:00
|
|
|
if( totalSize >= (2*GiB) ) return (2*MiB);
|
|
|
|
if( totalSize >= (1*GiB) ) return (1*MiB);
|
2007-08-13 13:57:12 +00:00
|
|
|
if( totalSize >= (512*MiB) ) return (512*KiB);
|
|
|
|
if( totalSize >= (350*MiB) ) return (256*KiB);
|
|
|
|
if( totalSize >= (150*MiB) ) return (128*KiB);
|
|
|
|
if( totalSize >= (50*MiB) ) return (64*KiB);
|
|
|
|
return (32*KiB); /* less than 50 meg */
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-06-19 00:08:39 +00:00
|
|
|
static int
|
|
|
|
builderFileCompare ( const void * va, const void * vb)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_metainfo_builder_file * a = va;
|
|
|
|
const tr_metainfo_builder_file * b = vb;
|
2007-06-19 00:08:39 +00:00
|
|
|
return strcmp( a->filename, b->filename );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_metainfo_builder*
|
|
|
|
tr_metaInfoBuilderCreate( tr_handle * handle, const char * topFile )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct FileList * files;
|
2007-06-19 00:08:39 +00:00
|
|
|
struct FileList * walk;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_metainfo_builder * ret = tr_new0( tr_metainfo_builder, 1 );
|
2007-06-18 03:40:41 +00:00
|
|
|
ret->top = tr_strdup( topFile );
|
2007-06-19 00:08:39 +00:00
|
|
|
ret->handle = handle;
|
2008-05-06 20:31:05 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
struct stat sb;
|
|
|
|
stat( topFile, &sb );
|
|
|
|
ret->isSingleFile = !S_ISDIR( sb.st_mode );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build a list of files containing topFile and,
|
|
|
|
if it's a directory, all of its children */
|
2008-05-06 20:31:05 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
char *dir, *base;
|
|
|
|
char dirbuf[MAX_PATH_LENGTH];
|
|
|
|
char basebuf[MAX_PATH_LENGTH];
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_strlcpy( dirbuf, topFile, sizeof( dirbuf ) );
|
|
|
|
tr_strlcpy( basebuf, topFile, sizeof( basebuf ) );
|
2007-06-18 03:40:41 +00:00
|
|
|
dir = dirname( dirbuf );
|
|
|
|
base = basename( basebuf );
|
|
|
|
files = getFiles( dir, base, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( walk=files; walk!=NULL; walk=walk->next )
|
|
|
|
++ret->fileCount;
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
ret->files = tr_new0( tr_metainfo_builder_file, ret->fileCount );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-06-19 00:08:39 +00:00
|
|
|
for( i=0, walk=files; walk!=NULL; ++i )
|
|
|
|
{
|
|
|
|
struct FileList * tmp = walk;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_metainfo_builder_file * file = &ret->files[i];
|
2007-06-19 00:08:39 +00:00
|
|
|
walk = walk->next;
|
|
|
|
file->filename = tmp->filename;
|
|
|
|
file->size = tmp->size;
|
|
|
|
ret->totalSize += tmp->size;
|
|
|
|
tr_free( tmp );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-06-19 00:08:39 +00:00
|
|
|
qsort( ret->files,
|
|
|
|
ret->fileCount,
|
2007-09-20 20:14:13 +00:00
|
|
|
sizeof(tr_metainfo_builder_file),
|
2007-06-19 00:08:39 +00:00
|
|
|
builderFileCompare );
|
|
|
|
|
2007-07-01 14:04:18 +00:00
|
|
|
ret->pieceSize = bestPieceSize( ret->totalSize );
|
2007-06-29 02:53:12 +00:00
|
|
|
ret->pieceCount = ret->pieceSize
|
|
|
|
? (int)( ret->totalSize / ret->pieceSize)
|
|
|
|
: 0;
|
2007-06-18 03:40:41 +00:00
|
|
|
if( ret->totalSize % ret->pieceSize )
|
|
|
|
++ret->pieceCount;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_metaInfoBuilderFree( tr_metainfo_builder * builder )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-04-17 19:54:22 +00:00
|
|
|
if( builder )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-06-01 05:36:23 +00:00
|
|
|
tr_file_index_t t;
|
|
|
|
int i;
|
|
|
|
for( t=0; t<builder->fileCount; ++t )
|
|
|
|
tr_free( builder->files[t].filename );
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_free( builder->files );
|
|
|
|
tr_free( builder->top );
|
|
|
|
tr_free( builder->comment );
|
2008-06-01 05:36:23 +00:00
|
|
|
for( i=0; i<builder->trackerCount; ++i )
|
|
|
|
tr_free( builder->trackers[i].announce );
|
|
|
|
tr_free( builder->trackers );
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_free( builder->outputFile );
|
|
|
|
tr_free( builder );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
static uint8_t*
|
2007-09-20 20:14:13 +00:00
|
|
|
getHashInfo ( tr_metainfo_builder * b )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-10-26 03:16:27 +00:00
|
|
|
uint32_t fileIndex = 0;
|
2007-07-06 18:24:54 +00:00
|
|
|
uint8_t *ret = tr_new0( uint8_t, SHA_DIGEST_LENGTH * b->pieceCount );
|
2007-06-18 03:40:41 +00:00
|
|
|
uint8_t *walk = ret;
|
2007-07-06 18:24:54 +00:00
|
|
|
uint8_t *buf;
|
2007-06-18 03:40:41 +00:00
|
|
|
uint64_t totalRemain;
|
|
|
|
uint64_t off = 0;
|
|
|
|
FILE * fp;
|
|
|
|
|
2007-07-06 18:24:54 +00:00
|
|
|
if( !b->totalSize )
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
buf = tr_new( uint8_t, b->pieceSize );
|
2007-06-18 03:40:41 +00:00
|
|
|
b->pieceIndex = 0;
|
|
|
|
totalRemain = b->totalSize;
|
2007-06-19 00:08:39 +00:00
|
|
|
fp = fopen( b->files[fileIndex].filename, "rb" );
|
2008-03-02 19:42:45 +00:00
|
|
|
if( !fp ) {
|
2008-03-24 17:18:08 +00:00
|
|
|
b->my_errno = errno;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( b->errfile, sizeof( b->errfile ), b->files[fileIndex].filename );
|
2008-03-24 17:18:08 +00:00
|
|
|
b->result = TR_MAKEMETA_IO_READ;
|
2008-08-11 17:00:23 +00:00
|
|
|
tr_free( buf );
|
2008-03-02 19:42:45 +00:00
|
|
|
tr_free( ret );
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
while ( totalRemain )
|
|
|
|
{
|
|
|
|
uint8_t *bufptr = buf;
|
2007-06-22 04:30:39 +00:00
|
|
|
const uint64_t thisPieceSize =
|
|
|
|
MIN( (uint32_t)b->pieceSize, totalRemain );
|
2007-06-18 03:40:41 +00:00
|
|
|
uint64_t pieceRemain = thisPieceSize;
|
|
|
|
|
|
|
|
assert( b->pieceIndex < b->pieceCount );
|
|
|
|
|
|
|
|
while( pieceRemain )
|
|
|
|
{
|
2007-06-22 04:30:39 +00:00
|
|
|
const uint64_t n_this_pass =
|
|
|
|
MIN( (b->files[fileIndex].size - off), pieceRemain );
|
2007-06-18 03:40:41 +00:00
|
|
|
fread( bufptr, 1, n_this_pass, fp );
|
|
|
|
bufptr += n_this_pass;
|
|
|
|
off += n_this_pass;
|
|
|
|
pieceRemain -= n_this_pass;
|
2007-06-19 00:08:39 +00:00
|
|
|
if( off == b->files[fileIndex].size ) {
|
2007-06-18 03:40:41 +00:00
|
|
|
off = 0;
|
|
|
|
fclose( fp );
|
|
|
|
fp = NULL;
|
|
|
|
if( ++fileIndex < b->fileCount ) {
|
2007-06-19 00:08:39 +00:00
|
|
|
fp = fopen( b->files[fileIndex].filename, "rb" );
|
2008-03-02 19:42:45 +00:00
|
|
|
if( !fp ) {
|
2008-03-24 17:18:08 +00:00
|
|
|
b->my_errno = errno;
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( b->errfile, sizeof( b->errfile ), b->files[fileIndex].filename );
|
2008-03-24 17:18:08 +00:00
|
|
|
b->result = TR_MAKEMETA_IO_READ;
|
2008-08-11 17:00:23 +00:00
|
|
|
tr_free( buf );
|
2008-03-02 19:42:45 +00:00
|
|
|
tr_free( ret );
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( bufptr-buf == (int)thisPieceSize );
|
|
|
|
assert( pieceRemain == 0 );
|
2007-10-14 03:26:13 +00:00
|
|
|
tr_sha1( walk, buf, thisPieceSize, NULL );
|
2007-06-18 03:40:41 +00:00
|
|
|
walk += SHA_DIGEST_LENGTH;
|
|
|
|
|
|
|
|
if( b->abortFlag ) {
|
2008-03-24 17:18:08 +00:00
|
|
|
b->result = TR_MAKEMETA_CANCELLED;
|
2007-06-18 03:40:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
totalRemain -= thisPieceSize;
|
|
|
|
++b->pieceIndex;
|
|
|
|
}
|
|
|
|
assert( b->abortFlag || (walk-ret == (int)(SHA_DIGEST_LENGTH*b->pieceCount)) );
|
|
|
|
assert( b->abortFlag || !totalRemain );
|
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
if( fp )
|
2007-06-18 03:40:41 +00:00
|
|
|
fclose( fp );
|
|
|
|
|
2007-06-19 00:08:39 +00:00
|
|
|
tr_free( buf );
|
2007-06-18 03:40:41 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-09-20 20:14:13 +00:00
|
|
|
getFileInfo( const char * topFile,
|
|
|
|
const tr_metainfo_builder_file * file,
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc * uninitialized_length,
|
|
|
|
tr_benc * uninitialized_path )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
const char *pch, *prev;
|
|
|
|
const size_t topLen = strlen(topFile) + 1; /* +1 for '/' */
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* get the file size */
|
2007-06-19 00:08:39 +00:00
|
|
|
tr_bencInitInt( uninitialized_length, file->size );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
/* the path list */
|
|
|
|
n = 1;
|
2007-06-19 00:08:39 +00:00
|
|
|
for( pch=file->filename+topLen; *pch; ++pch )
|
2007-06-18 03:40:41 +00:00
|
|
|
if (*pch == TR_PATH_DELIMITER)
|
|
|
|
++n;
|
2008-04-03 21:38:32 +00:00
|
|
|
tr_bencInitList( uninitialized_path, n );
|
2007-06-19 00:08:39 +00:00
|
|
|
for( prev=pch=file->filename+topLen; ; ++pch )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
char buf[MAX_PATH_LENGTH];
|
|
|
|
|
|
|
|
if (*pch && *pch!=TR_PATH_DELIMITER )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memcpy( buf, prev, pch-prev );
|
|
|
|
buf[pch-prev] = '\0';
|
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencListAddStr( uninitialized_path, buf );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
prev = pch + 1;
|
|
|
|
if (!*pch)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-02-26 21:58:58 +00:00
|
|
|
makeInfoDict ( tr_benc * dict,
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_metainfo_builder * builder )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
uint8_t * pch;
|
|
|
|
char base[MAX_PATH_LENGTH];
|
|
|
|
|
|
|
|
tr_bencDictReserve( dict, 5 );
|
2007-07-25 01:00:17 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
if ( builder->isSingleFile )
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencDictAddInt( dict, "length", builder->files[0].size );
|
|
|
|
else {
|
|
|
|
uint32_t i;
|
|
|
|
tr_benc * list = tr_bencDictAddList( dict, "files", builder->fileCount );
|
|
|
|
for( i=0; i<builder->fileCount; ++i ) {
|
|
|
|
tr_benc * dict = tr_bencListAddDict( list, 2 );
|
|
|
|
tr_benc * length = tr_bencDictAdd( dict, "length" );
|
|
|
|
tr_benc * pathVal = tr_bencDictAdd( dict, "path" );
|
|
|
|
getFileInfo( builder->top, &builder->files[i], length, pathVal );
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_strlcpy( base, builder->top, sizeof( base ) );
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencDictAddStr( dict, "name", basename( base ) );
|
2007-07-25 01:00:17 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencDictAddInt( dict, "piece length", builder->pieceSize );
|
2007-07-25 01:00:17 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
if(( pch = getHashInfo( builder ))) {
|
|
|
|
tr_bencDictAddRaw( dict, "pieces", pch, SHA_DIGEST_LENGTH * builder->pieceCount );
|
|
|
|
tr_free( pch );
|
2008-03-02 19:42:45 +00:00
|
|
|
}
|
2007-07-25 01:00:17 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencDictAddInt( dict, "private", builder->isPrivate ? 1 : 0 );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2008-03-24 15:58:06 +00:00
|
|
|
static void
|
|
|
|
tr_realMakeMetaInfo ( tr_metainfo_builder * builder )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-06-01 05:36:23 +00:00
|
|
|
int i;
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_benc top;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-06-01 05:36:23 +00:00
|
|
|
/* allow an empty set, but if URLs *are* listed, verify them. #814, #971 */
|
|
|
|
for( i=0; i<builder->trackerCount && !builder->result; ++i )
|
|
|
|
if( !tr_httpIsValidURL( builder->trackers[i].announce ) )
|
|
|
|
builder->result = TR_MAKEMETA_URL;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-06-01 05:36:23 +00:00
|
|
|
tr_bencInitDict( &top, 6 );
|
2008-04-18 16:23:59 +00:00
|
|
|
|
2008-06-01 05:36:23 +00:00
|
|
|
if( !builder->result && builder->trackerCount )
|
|
|
|
{
|
|
|
|
int prevTier = -1;
|
|
|
|
tr_benc * tier = NULL;
|
|
|
|
|
2008-08-11 17:29:53 +00:00
|
|
|
if( builder->trackerCount > 1 )
|
|
|
|
{
|
|
|
|
tr_benc * annList = tr_bencDictAddList( &top, "announce-list", 0 );
|
|
|
|
for( i=0; i<builder->trackerCount; ++i ) {
|
|
|
|
if( prevTier != builder->trackers[i].tier ) {
|
|
|
|
prevTier = builder->trackers[i].tier;
|
|
|
|
tier = tr_bencListAddList( annList, 0 );
|
|
|
|
}
|
|
|
|
tr_bencListAddStr( tier, builder->trackers[i].announce );
|
2008-06-01 05:36:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bencDictAddStr( &top, "announce", builder->trackers[0].announce );
|
|
|
|
}
|
2008-08-11 17:29:53 +00:00
|
|
|
|
2008-03-24 17:18:08 +00:00
|
|
|
if( !builder->result && !builder->abortFlag )
|
2008-03-24 15:58:06 +00:00
|
|
|
{
|
2008-04-18 16:23:59 +00:00
|
|
|
if( builder->comment && *builder->comment )
|
|
|
|
tr_bencDictAddStr( &top, "comment", builder->comment );
|
|
|
|
tr_bencDictAddStr( &top, "created by", TR_NAME "/" LONG_VERSION_STRING );
|
2008-08-11 19:05:02 +00:00
|
|
|
tr_bencDictAddInt( &top, "creation date", time(NULL) );
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencDictAddStr( &top, "encoding", "UTF-8" );
|
|
|
|
makeInfoDict( tr_bencDictAddDict( &top, "info", 666 ), builder );
|
2008-03-24 15:58:06 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
/* save the file */
|
2008-03-24 17:18:08 +00:00
|
|
|
if ( !builder->result && !builder->abortFlag ) {
|
2008-04-18 16:23:59 +00:00
|
|
|
if( tr_bencSaveFile( builder->outputFile, &top ) ) {
|
2008-03-24 17:18:08 +00:00
|
|
|
builder->my_errno = errno;
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_strlcpy( builder->errfile, builder->outputFile, sizeof( builder->errfile ) );
|
2008-03-24 17:18:08 +00:00
|
|
|
builder->result = TR_MAKEMETA_IO_WRITE;
|
2008-03-24 15:58:06 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2008-06-01 05:36:23 +00:00
|
|
|
tr_bencFree( &top );
|
2008-03-24 17:18:08 +00:00
|
|
|
if( builder->abortFlag )
|
|
|
|
builder->result = TR_MAKEMETA_CANCELLED;
|
2007-06-18 03:40:41 +00:00
|
|
|
builder->isDone = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** A threaded builder queue
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
static tr_metainfo_builder * queue = NULL;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_thread * workerThread = NULL;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
static tr_lock* getQueueLock( tr_handle * h )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_lock * lock = NULL;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalLock( h );
|
2007-07-30 15:27:52 +00:00
|
|
|
if( !lock )
|
|
|
|
lock = tr_lockNew( );
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalUnlock( h );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
2008-08-12 14:03:03 +00:00
|
|
|
static void makeMetaWorkerFunc( void * user_data )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_handle * handle = (tr_handle *) user_data;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_metainfo_builder * builder = NULL;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
/* find the next builder to process */
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lock * lock = getQueueLock ( handle );
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_lockLock( lock );
|
2008-08-01 16:43:22 +00:00
|
|
|
if( queue ) {
|
2007-06-18 03:40:41 +00:00
|
|
|
builder = queue;
|
|
|
|
queue = queue->nextBuilder;
|
|
|
|
}
|
|
|
|
tr_lockUnlock( lock );
|
|
|
|
|
|
|
|
/* if no builders, this worker thread is done */
|
|
|
|
if( builder == NULL )
|
|
|
|
break;
|
|
|
|
|
|
|
|
tr_realMakeMetaInfo ( builder );
|
|
|
|
}
|
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
workerThread = NULL;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-06-01 05:36:23 +00:00
|
|
|
tr_makeMetaInfo( tr_metainfo_builder * builder,
|
|
|
|
const char * outputFile,
|
|
|
|
const tr_tracker_info * trackers,
|
|
|
|
int trackerCount,
|
|
|
|
const char * comment,
|
|
|
|
int isPrivate )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-06-01 05:36:23 +00:00
|
|
|
int i;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lock * lock;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-04-17 19:54:22 +00:00
|
|
|
/* free any variables from a previous run */
|
2008-06-01 05:36:23 +00:00
|
|
|
for( i=0; i<builder->trackerCount; ++i )
|
|
|
|
tr_free( builder->trackers[i].announce );
|
|
|
|
tr_free( builder->trackers );
|
2008-04-17 19:54:22 +00:00
|
|
|
tr_free( builder->comment );
|
|
|
|
tr_free( builder->outputFile );
|
|
|
|
|
|
|
|
/* initialize the builder variables */
|
2007-06-18 03:40:41 +00:00
|
|
|
builder->abortFlag = 0;
|
|
|
|
builder->isDone = 0;
|
2008-06-01 05:36:23 +00:00
|
|
|
builder->trackerCount = trackerCount;
|
|
|
|
builder->trackers = tr_new0( tr_tracker_info, builder->trackerCount );
|
|
|
|
for( i=0; i<builder->trackerCount; ++i ) {
|
|
|
|
builder->trackers[i].tier = trackers[i].tier;
|
|
|
|
builder->trackers[i].announce = tr_strdup( trackers[i].announce );
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
builder->comment = tr_strdup( comment );
|
|
|
|
builder->isPrivate = isPrivate;
|
|
|
|
if( outputFile && *outputFile )
|
|
|
|
builder->outputFile = tr_strdup( outputFile );
|
|
|
|
else {
|
|
|
|
char out[MAX_PATH_LENGTH];
|
2008-07-15 17:16:57 +00:00
|
|
|
tr_snprintf( out, sizeof(out), "%s.torrent", builder->top);
|
2007-06-18 03:40:41 +00:00
|
|
|
builder->outputFile = tr_strdup( out );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enqueue the builder */
|
|
|
|
lock = getQueueLock ( builder->handle );
|
|
|
|
tr_lockLock( lock );
|
|
|
|
builder->nextBuilder = queue;
|
|
|
|
queue = builder;
|
2007-07-30 15:27:52 +00:00
|
|
|
if( !workerThread )
|
2008-08-12 14:03:03 +00:00
|
|
|
workerThread = tr_threadNew( makeMetaWorkerFunc, builder->handle );
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_lockUnlock( lock );
|
|
|
|
}
|
|
|
|
|