2007-06-18 03:40:41 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2007-2010 Mnemosyne LLC
|
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)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2007-06-18 03:40:41 +00:00
|
|
|
* 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 */
|
2010-01-25 05:06:54 +00:00
|
|
|
#include <string.h> /* strcmp, strlen, strcasecmp */
|
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>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2010-04-23 16:36:16 +00:00
|
|
|
#include "crypto.h" /* tr_sha1 */
|
2010-06-16 14:01:03 +00:00
|
|
|
#include "fdlimit.h" /* tr_open_file_for_scanning() */
|
2008-12-23 17:27:15 +00:00
|
|
|
#include "session.h"
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "bencode.h"
|
|
|
|
#include "makemeta.h"
|
2010-05-06 15:07:18 +00:00
|
|
|
#include "platform.h" /* threads, locks, TR_PATH_MAX */
|
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
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
struct FileList * next;
|
|
|
|
uint64_t size;
|
|
|
|
char * filename;
|
2007-06-18 03:40:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct FileList*
|
2008-09-23 19:11:04 +00:00
|
|
|
getFiles( const char * dir,
|
|
|
|
const char * base,
|
|
|
|
struct FileList * list )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
2008-10-14 03:03:29 +00:00
|
|
|
char * buf;
|
2007-06-18 03:40:41 +00:00
|
|
|
struct stat sb;
|
2008-09-23 19:11:04 +00:00
|
|
|
DIR * odir = NULL;
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
sb.st_size = 0;
|
|
|
|
|
2008-10-14 03:03:29 +00:00
|
|
|
buf = tr_buildPath( dir, base, NULL );
|
2007-06-18 03:40:41 +00:00
|
|
|
i = stat( buf, &sb );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( i )
|
|
|
|
{
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_err( _( "Torrent Creator is skipping file \"%s\": %s" ),
|
|
|
|
buf, tr_strerror( errno ) );
|
|
|
|
tr_free( buf );
|
2007-06-18 03:40:41 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( S_ISDIR( sb.st_mode ) && ( ( odir = opendir ( buf ) ) ) )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
struct dirent *d;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
|
2008-09-28 16:02:13 +00:00
|
|
|
if( d->d_name && d->d_name[0] != '.' ) /* skip dotfiles */
|
2007-06-18 03:40:41 +00:00
|
|
|
list = getFiles( buf, d->d_name, list );
|
|
|
|
closedir( odir );
|
|
|
|
}
|
2008-09-28 16:02:13 +00:00
|
|
|
else if( S_ISREG( sb.st_mode ) && ( sb.st_size > 0 ) )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
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-09-23 19:11:04 +00:00
|
|
|
if( ( buf[0] == '.' ) && ( buf[1] == '/' ) )
|
2008-02-20 11:36:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( buf );
|
2007-06-18 03:40:41 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2010-07-01 15:14:35 +00:00
|
|
|
static uint32_t
|
2007-07-01 14:04:18 +00:00
|
|
|
bestPieceSize( uint64_t totalSize )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2010-07-01 15:14:35 +00:00
|
|
|
const uint32_t GiB = 1073741824;
|
|
|
|
const uint32_t MiB = 1048576;
|
|
|
|
const uint32_t KiB = 1024;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( totalSize >= ( 2 * GiB ) ) return 2 * MiB;
|
|
|
|
if( totalSize >= ( 1 * GiB ) ) return 1 * MiB;
|
|
|
|
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
|
2008-09-23 19:11:04 +00:00
|
|
|
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;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-25 05:06:54 +00:00
|
|
|
return strcasecmp( a->filename, b->filename );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_metainfo_builder*
|
2009-04-08 18:47:48 +00:00
|
|
|
tr_metaInfoBuilderCreate( const char * topFile )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
struct FileList * files;
|
|
|
|
struct FileList * walk;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_metainfo_builder * ret = tr_new0( tr_metainfo_builder, 1 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
ret->top = tr_strdup( topFile );
|
2009-04-08 18:47:48 +00:00
|
|
|
|
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
|
|
|
{
|
2008-10-14 03:39:16 +00:00
|
|
|
char * dir = tr_dirname( topFile );
|
|
|
|
char * base = tr_basename( topFile );
|
2007-06-18 03:40:41 +00:00
|
|
|
files = getFiles( dir, base, NULL );
|
2008-10-14 03:39:16 +00:00
|
|
|
tr_free( base );
|
|
|
|
tr_free( dir );
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( walk = files; walk != NULL; walk = walk->next )
|
2007-06-18 03:40:41 +00:00
|
|
|
++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
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0, walk = files; walk != NULL; ++i )
|
2007-06-19 00:08:39 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
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,
|
2008-09-23 19:11:04 +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
|
2008-09-23 19:11:04 +00:00
|
|
|
? (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;
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
for( t = 0; t < builder->fileCount; ++t )
|
2008-06-01 05:36:23 +00:00
|
|
|
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-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < builder->trackerCount; ++i )
|
2008-06-01 05:36:23 +00:00
|
|
|
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*
|
2008-09-23 19:11:04 +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;
|
2010-06-16 14:01:03 +00:00
|
|
|
int fd;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-07-06 18:24:54 +00:00
|
|
|
if( !b->totalSize )
|
|
|
|
return ret;
|
|
|
|
|
2010-02-23 07:29:21 +00:00
|
|
|
buf = tr_valloc( b->pieceSize );
|
2007-06-18 03:40:41 +00:00
|
|
|
b->pieceIndex = 0;
|
|
|
|
totalRemain = b->totalSize;
|
2010-06-16 14:01:03 +00:00
|
|
|
fd = tr_open_file_for_scanning( b->files[fileIndex].filename );
|
|
|
|
if( fd < 0 )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-03-24 17:18:08 +00:00
|
|
|
b->my_errno = errno;
|
2008-10-22 17:14:50 +00:00
|
|
|
tr_strlcpy( b->errfile,
|
|
|
|
b->files[fileIndex].filename,
|
|
|
|
sizeof( b->errfile ) );
|
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;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
while( totalRemain )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2010-07-01 15:14:35 +00:00
|
|
|
uint8_t * bufptr = buf;
|
|
|
|
const uint32_t thisPieceSize = (uint32_t) MIN( b->pieceSize, totalRemain );
|
|
|
|
uint32_t leftInPiece = thisPieceSize;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
assert( b->pieceIndex < b->pieceCount );
|
|
|
|
|
2010-07-01 15:14:35 +00:00
|
|
|
while( leftInPiece )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2010-07-01 15:14:35 +00:00
|
|
|
const size_t n_this_pass = (size_t) MIN( ( b->files[fileIndex].size - off ), leftInPiece );
|
2010-06-16 14:01:03 +00:00
|
|
|
read( fd, bufptr, n_this_pass );
|
2007-06-18 03:40:41 +00:00
|
|
|
bufptr += n_this_pass;
|
|
|
|
off += n_this_pass;
|
2010-07-01 15:14:35 +00:00
|
|
|
leftInPiece -= n_this_pass;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( off == b->files[fileIndex].size )
|
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
off = 0;
|
2010-06-16 14:01:03 +00:00
|
|
|
tr_close_file( fd );
|
|
|
|
fd = -1;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ++fileIndex < b->fileCount )
|
|
|
|
{
|
2010-06-16 14:01:03 +00:00
|
|
|
fd = tr_open_file_for_scanning( b->files[fileIndex].filename );
|
|
|
|
if( fd < 0 )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-03-24 17:18:08 +00:00
|
|
|
b->my_errno = errno;
|
2008-10-22 17:14:50 +00:00
|
|
|
tr_strlcpy( b->errfile,
|
|
|
|
b->files[fileIndex].filename,
|
|
|
|
sizeof( b->errfile ) );
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( bufptr - buf == (int)thisPieceSize );
|
2010-07-01 15:14:35 +00:00
|
|
|
assert( leftInPiece == 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;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
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;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
assert( b->abortFlag
|
|
|
|
|| ( walk - ret == (int)( SHA_DIGEST_LENGTH * b->pieceCount ) ) );
|
2007-06-18 03:40:41 +00:00
|
|
|
assert( b->abortFlag || !totalRemain );
|
|
|
|
|
2010-06-16 14:01:03 +00:00
|
|
|
if( fd >= 0 )
|
|
|
|
tr_close_file( fd );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2007-06-19 00:08:39 +00:00
|
|
|
tr_free( buf );
|
2007-06-18 03:40:41 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
getFileInfo( const char * topFile,
|
|
|
|
const tr_metainfo_builder_file * file,
|
|
|
|
tr_benc * uninitialized_length,
|
|
|
|
tr_benc * uninitialized_path )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * pch, *prev;
|
2009-06-20 00:39:30 +00:00
|
|
|
size_t topLen;
|
2008-09-23 19:11:04 +00:00
|
|
|
int n;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
2009-06-20 00:39:30 +00:00
|
|
|
/* how much of file->filename to walk past */
|
|
|
|
topLen = strlen( topFile );
|
|
|
|
if( topLen>0 && topFile[topLen-1]!=TR_PATH_DELIMITER )
|
|
|
|
++topLen; /* +1 for the path delimiter */
|
|
|
|
|
|
|
|
/* build the path list */
|
2007-06-18 03:40:41 +00:00
|
|
|
n = 1;
|
2008-09-23 19:11:04 +00:00
|
|
|
for( pch = file->filename + topLen; *pch; ++pch )
|
|
|
|
if( *pch == TR_PATH_DELIMITER )
|
2007-06-18 03:40:41 +00:00
|
|
|
++n;
|
2008-04-03 21:38:32 +00:00
|
|
|
tr_bencInitList( uninitialized_path, n );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( prev = pch = file->filename + topLen; ; ++pch )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2010-05-06 17:02:31 +00:00
|
|
|
char buf[TR_PATH_MAX];
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( *pch && *pch != TR_PATH_DELIMITER )
|
2007-06-18 03:40:41 +00:00
|
|
|
continue;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
memcpy( buf, prev, pch - prev );
|
|
|
|
buf[pch - prev] = '\0';
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencListAddStr( uninitialized_path, buf );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
prev = pch + 1;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !*pch )
|
|
|
|
break;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
makeInfoDict( tr_benc * dict,
|
|
|
|
tr_metainfo_builder * builder )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
|
|
|
uint8_t * pch;
|
2008-10-14 03:39:16 +00:00
|
|
|
char * base;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
tr_bencDictReserve( dict, 5 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-10 02:40:22 +00:00
|
|
|
if( builder->fileCount == 1 )
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencDictAddInt( dict, "length", builder->files[0].size );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
tr_benc * list = tr_bencDictAddList( dict, "files",
|
|
|
|
builder->fileCount );
|
|
|
|
for( i = 0; i < builder->fileCount; ++i )
|
|
|
|
{
|
2009-01-01 18:38:49 +00:00
|
|
|
tr_benc * d = tr_bencListAddDict( list, 2 );
|
|
|
|
tr_benc * length = tr_bencDictAdd( d, "length" );
|
|
|
|
tr_benc * pathVal = tr_bencDictAdd( d, "path" );
|
2008-04-18 16:23:59 +00:00
|
|
|
getFileInfo( builder->top, &builder->files[i], length, pathVal );
|
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2008-10-14 03:39:16 +00:00
|
|
|
base = tr_basename( builder->top );
|
|
|
|
tr_bencDictAddStr( dict, "name", base );
|
|
|
|
tr_free( 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-09-23 19:11:04 +00:00
|
|
|
if( ( pch = getHashInfo( builder ) ) )
|
|
|
|
{
|
|
|
|
tr_bencDictAddRaw( dict, "pieces", pch,
|
|
|
|
SHA_DIGEST_LENGTH * builder->pieceCount );
|
2008-04-18 16:23:59 +00:00
|
|
|
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
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_realMakeMetaInfo( tr_metainfo_builder * builder )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +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 */
|
2009-09-17 01:18:59 +00:00
|
|
|
for( i = 0; i < builder->trackerCount && !builder->result; ++i ) {
|
2010-02-20 15:57:05 +00:00
|
|
|
if( !tr_urlIsValidTracker( builder->trackers[i].announce ) ) {
|
2009-09-17 01:18:59 +00:00
|
|
|
tr_strlcpy( builder->errfile, builder->trackers[i].announce,
|
|
|
|
sizeof( builder->errfile ) );
|
2008-06-01 05:36:23 +00:00
|
|
|
builder->result = TR_MAKEMETA_URL;
|
2009-09-17 01:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2010-07-07 22:50:18 +00:00
|
|
|
if( !builder->fileCount || !builder->totalSize ||
|
|
|
|
!builder->pieceSize || !builder->pieceCount )
|
|
|
|
{
|
|
|
|
builder->errfile[0] = '\0';
|
|
|
|
builder->my_errno = ENOENT;
|
|
|
|
builder->result = TR_MAKEMETA_IO_READ;
|
|
|
|
builder->isDone = TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-01 05:36:23 +00:00
|
|
|
if( !builder->result && builder->trackerCount )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int prevTier = -1;
|
2008-06-01 05:36:23 +00:00
|
|
|
tr_benc * tier = NULL;
|
|
|
|
|
2008-08-11 17:29:53 +00:00
|
|
|
if( builder->trackerCount > 1 )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc * annList = tr_bencDictAddList( &top, "announce-list",
|
|
|
|
0 );
|
|
|
|
for( i = 0; i < builder->trackerCount; ++i )
|
|
|
|
{
|
|
|
|
if( prevTier != builder->trackers[i].tier )
|
|
|
|
{
|
2008-08-11 17:29:53 +00:00
|
|
|
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 );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_bencDictAddStr( &top, "created by",
|
|
|
|
TR_NAME "/" LONG_VERSION_STRING );
|
|
|
|
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-09-23 19:11:04 +00:00
|
|
|
if( !builder->result && !builder->abortFlag )
|
|
|
|
{
|
2009-06-02 01:48:48 +00:00
|
|
|
if( tr_bencToFile( &top, TR_FMT_BENC, builder->outputFile ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-03-24 17:18:08 +00:00
|
|
|
builder->my_errno = errno;
|
2008-09-23 19:11:04 +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
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static tr_thread * workerThread = NULL;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static tr_lock*
|
2009-04-08 18:47:48 +00:00
|
|
|
getQueueLock( void )
|
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-07-30 15:27:52 +00:00
|
|
|
if( !lock )
|
2008-09-23 19:11:04 +00:00
|
|
|
lock = tr_lockNew( );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static void
|
2009-04-08 18:47:48 +00:00
|
|
|
makeMetaWorkerFunc( void * unused UNUSED )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2009-04-08 18:47:48 +00:00
|
|
|
for( ;; )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
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 */
|
2009-04-08 18:47:48 +00:00
|
|
|
tr_lock * lock = getQueueLock( );
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_lockLock( lock );
|
2008-09-23 19:11:04 +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 )
|
2008-09-23 19:11:04 +00:00
|
|
|
break;
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
tr_realMakeMetaInfo ( builder );
|
|
|
|
}
|
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
workerThread = NULL;
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +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-09-23 19:11:04 +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-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < builder->trackerCount; ++i )
|
2008-06-01 05:36:23 +00:00
|
|
|
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;
|
2009-09-17 01:18:59 +00:00
|
|
|
builder->result = 0;
|
2007-06-18 03:40:41 +00:00
|
|
|
builder->isDone = 0;
|
2009-09-17 01:18:59 +00:00
|
|
|
builder->pieceIndex = 0;
|
2008-06-01 05:36:23 +00:00
|
|
|
builder->trackerCount = trackerCount;
|
|
|
|
builder->trackers = tr_new0( tr_tracker_info, builder->trackerCount );
|
2010-07-07 22:50:18 +00:00
|
|
|
for( i = 0; i < builder->trackerCount; ++i ) {
|
2008-06-01 05:36:23 +00:00
|
|
|
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 );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
2008-10-14 03:39:16 +00:00
|
|
|
builder->outputFile = tr_strdup_printf( "%s.torrent", builder->top );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
/* enqueue the builder */
|
2009-04-08 18:47:48 +00:00
|
|
|
lock = getQueueLock ( );
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_lockLock( lock );
|
|
|
|
builder->nextBuilder = queue;
|
|
|
|
queue = builder;
|
2007-07-30 15:27:52 +00:00
|
|
|
if( !workerThread )
|
2009-04-08 18:47:48 +00:00
|
|
|
workerThread = tr_threadNew( makeMetaWorkerFunc, NULL );
|
2007-06-18 03:40:41 +00:00
|
|
|
tr_lockUnlock( lock );
|
|
|
|
}
|
|
|
|
|