2007-12-21 16:51:42 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2007-2009 Charles Kerr <charles@transmissionbt.com>
|
2007-12-21 16:51:42 +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-12-21 16:51:42 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-02-26 21:58:58 +00:00
|
|
|
* $Id$
|
2007-12-21 16:51:42 +00:00
|
|
|
*/
|
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
#include <errno.h>
|
2007-12-21 16:51:42 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "bencode.h"
|
|
|
|
#include "platform.h"
|
2008-10-20 16:00:14 +00:00
|
|
|
#include "session.h" /* tr_sessionFindTorrentFile() */
|
|
|
|
#include "torrent.h" /* tr_ctorGetSave() */
|
2007-12-21 16:51:42 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
struct optional_args
|
|
|
|
{
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isSet_paused;
|
|
|
|
tr_bool isSet_connected;
|
|
|
|
tr_bool isSet_downloadDir;
|
2007-12-21 16:51:42 +00:00
|
|
|
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isPaused;
|
2008-09-23 19:11:04 +00:00
|
|
|
uint16_t peerLimit;
|
2008-10-14 03:54:57 +00:00
|
|
|
char * downloadDir;
|
2007-12-21 16:51:42 +00:00
|
|
|
};
|
|
|
|
|
2008-05-23 20:04:41 +00:00
|
|
|
/** Opaque class used when instantiating torrents.
|
2008-09-23 19:11:04 +00:00
|
|
|
* @ingroup tr_ctor */
|
2007-12-21 16:51:42 +00:00
|
|
|
struct tr_ctor
|
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
const tr_session * session;
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool saveInOurTorrentsDir;
|
|
|
|
tr_bool doDelete;
|
2007-12-21 16:51:42 +00:00
|
|
|
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isSet_metainfo;
|
|
|
|
tr_bool isSet_delete;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_benc metainfo;
|
|
|
|
char * sourceFile;
|
2007-12-21 16:51:42 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
struct optional_args optionalArgs[2];
|
2007-12-21 16:51:42 +00:00
|
|
|
};
|
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2007-12-21 16:51:42 +00:00
|
|
|
|
2008-02-13 02:24:12 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
setSourceFile( tr_ctor * ctor,
|
|
|
|
const char * sourceFile )
|
2008-02-13 02:24:12 +00:00
|
|
|
{
|
|
|
|
tr_free( ctor->sourceFile );
|
|
|
|
ctor->sourceFile = tr_strdup( sourceFile );
|
|
|
|
}
|
|
|
|
|
2007-12-21 16:51:42 +00:00
|
|
|
static void
|
2007-12-21 22:18:40 +00:00
|
|
|
clearMetainfo( tr_ctor * ctor )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ctor->isSet_metainfo )
|
|
|
|
{
|
2007-12-21 22:18:40 +00:00
|
|
|
ctor->isSet_metainfo = 0;
|
|
|
|
tr_bencFree( &ctor->metainfo );
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
2008-02-13 02:24:12 +00:00
|
|
|
|
|
|
|
setSourceFile( ctor, NULL );
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorSetMetainfo( tr_ctor * ctor,
|
|
|
|
const uint8_t * metainfo,
|
|
|
|
size_t len )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
|
|
|
int err;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
clearMetainfo( ctor );
|
|
|
|
err = tr_bencLoad( metainfo, len, &ctor->metainfo, NULL );
|
|
|
|
ctor->isSet_metainfo = !err;
|
2007-12-21 16:51:42 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-02-13 02:24:12 +00:00
|
|
|
const char*
|
|
|
|
tr_ctorGetSourceFile( const tr_ctor * ctor )
|
|
|
|
{
|
|
|
|
return ctor->sourceFile;
|
|
|
|
}
|
|
|
|
|
2007-12-21 16:51:42 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorSetMetainfoFromFile( tr_ctor * ctor,
|
|
|
|
const char * filename )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2007-12-21 22:18:40 +00:00
|
|
|
uint8_t * metainfo;
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t len;
|
|
|
|
int err;
|
2007-12-21 16:51:42 +00:00
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
metainfo = tr_loadFile( filename, &len );
|
|
|
|
if( metainfo && len )
|
|
|
|
err = tr_ctorSetMetainfo( ctor, metainfo, len );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2007-12-21 22:18:40 +00:00
|
|
|
clearMetainfo( ctor );
|
2007-12-21 16:51:42 +00:00
|
|
|
err = 1;
|
|
|
|
}
|
|
|
|
|
2008-02-13 02:24:12 +00:00
|
|
|
setSourceFile( ctor, filename );
|
|
|
|
|
2008-01-03 16:01:29 +00:00
|
|
|
/* if no `name' field was set, then set it from the filename */
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ctor->isSet_metainfo )
|
|
|
|
{
|
2008-08-20 18:42:45 +00:00
|
|
|
tr_benc * info;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_bencDictFindDict( &ctor->metainfo, "info", &info ) )
|
|
|
|
{
|
2008-08-21 14:57:59 +00:00
|
|
|
const char * name;
|
|
|
|
if( !tr_bencDictFindStr( info, "name.utf-8", &name ) )
|
|
|
|
if( !tr_bencDictFindStr( info, "name", &name ) )
|
|
|
|
name = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !name || !*name )
|
|
|
|
{
|
2008-10-14 03:39:16 +00:00
|
|
|
char * base = tr_basename( filename );
|
|
|
|
tr_bencDictAddStr( info, "name", base );
|
|
|
|
tr_free( base );
|
2008-01-03 16:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
tr_free( metainfo );
|
2007-12-21 16:51:42 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorSetMetainfoFromHash( tr_ctor * ctor,
|
|
|
|
const char * hashString )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err;
|
2008-04-14 14:39:13 +00:00
|
|
|
const char * filename;
|
2007-12-21 16:51:42 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
filename = tr_sessionFindTorrentFile( ctor->session, hashString );
|
2008-10-03 04:49:06 +00:00
|
|
|
if( !filename )
|
|
|
|
err = EINVAL;
|
2008-04-14 14:39:13 +00:00
|
|
|
else
|
2008-10-03 04:49:06 +00:00
|
|
|
err = tr_ctorSetMetainfoFromFile( ctor, filename );
|
2007-12-21 16:51:42 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-02-13 02:24:12 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorSetDeleteSource( tr_ctor * ctor,
|
2008-12-03 04:55:10 +00:00
|
|
|
tr_bool deleteSource )
|
2008-02-13 02:24:12 +00:00
|
|
|
{
|
2008-12-03 04:55:10 +00:00
|
|
|
ctor->doDelete = deleteSource != 0;
|
2008-02-13 02:24:12 +00:00
|
|
|
ctor->isSet_delete = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorGetDeleteSource( const tr_ctor * ctor,
|
|
|
|
uint8_t * setme )
|
2008-02-13 02:24:12 +00:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if( !ctor->isSet_delete )
|
|
|
|
err = 1;
|
|
|
|
else if( setme )
|
|
|
|
*setme = ctor->doDelete ? 1 : 0;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-12-22 17:30:31 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorSetSave( tr_ctor * ctor,
|
2008-12-03 04:55:10 +00:00
|
|
|
tr_bool saveInOurTorrentsDir )
|
2007-12-22 17:30:31 +00:00
|
|
|
{
|
2008-12-03 04:55:10 +00:00
|
|
|
ctor->saveInOurTorrentsDir = saveInOurTorrentsDir != 0;
|
2007-12-22 17:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_ctorGetSave( const tr_ctor * ctor )
|
|
|
|
{
|
|
|
|
return ctor && ctor->saveInOurTorrentsDir;
|
|
|
|
}
|
|
|
|
|
2007-12-21 16:51:42 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorSetPaused( tr_ctor * ctor,
|
|
|
|
tr_ctorMode mode,
|
2008-12-03 04:55:10 +00:00
|
|
|
tr_bool isPaused )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
|
|
|
struct optional_args * args = &ctor->optionalArgs[mode];
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-12-21 16:51:42 +00:00
|
|
|
args->isSet_paused = 1;
|
2007-12-21 22:18:40 +00:00
|
|
|
args->isPaused = isPaused ? 1 : 0;
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorSetPeerLimit( tr_ctor * ctor,
|
|
|
|
tr_ctorMode mode,
|
|
|
|
uint16_t peerLimit )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
|
|
|
struct optional_args * args = &ctor->optionalArgs[mode];
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-12-21 16:51:42 +00:00
|
|
|
args->isSet_connected = 1;
|
2008-05-12 16:33:17 +00:00
|
|
|
args->peerLimit = peerLimit;
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorSetDownloadDir( tr_ctor * ctor,
|
|
|
|
tr_ctorMode mode,
|
|
|
|
const char * directory )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
|
|
|
struct optional_args * args = &ctor->optionalArgs[mode];
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-14 03:54:57 +00:00
|
|
|
tr_free( args->downloadDir );
|
2008-10-14 17:57:44 +00:00
|
|
|
args->downloadDir = NULL;
|
|
|
|
args->isSet_downloadDir = 0;
|
2008-10-14 03:54:57 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( directory )
|
|
|
|
{
|
2008-06-11 23:40:27 +00:00
|
|
|
args->isSet_downloadDir = 1;
|
2008-10-14 03:54:57 +00:00
|
|
|
args->downloadDir = tr_strdup( directory );
|
2008-06-11 23:40:27 +00:00
|
|
|
}
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorGetPeerLimit( const tr_ctor * ctor,
|
|
|
|
tr_ctorMode mode,
|
|
|
|
uint16_t * setmeCount )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err = 0;
|
2007-12-21 16:51:42 +00:00
|
|
|
const struct optional_args * args = &ctor->optionalArgs[mode];
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2008-02-13 02:24:12 +00:00
|
|
|
if( !args->isSet_connected )
|
2007-12-21 22:18:40 +00:00
|
|
|
err = 1;
|
2008-02-13 02:24:12 +00:00
|
|
|
else if( setmeCount )
|
2008-05-12 16:33:17 +00:00
|
|
|
*setmeCount = args->peerLimit;
|
2007-12-21 22:18:40 +00:00
|
|
|
|
|
|
|
return err;
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorGetPaused( const tr_ctor * ctor,
|
|
|
|
tr_ctorMode mode,
|
|
|
|
uint8_t * setmeIsPaused )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err = 0;
|
2007-12-21 16:51:42 +00:00
|
|
|
const struct optional_args * args = &ctor->optionalArgs[mode];
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2008-02-13 02:24:12 +00:00
|
|
|
if( !args->isSet_paused )
|
2007-12-21 22:18:40 +00:00
|
|
|
err = 1;
|
2008-02-13 02:24:12 +00:00
|
|
|
else if( setmeIsPaused )
|
|
|
|
*setmeIsPaused = args->isPaused ? 1 : 0;
|
2007-12-21 22:18:40 +00:00
|
|
|
|
|
|
|
return err;
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorGetDownloadDir( const tr_ctor * ctor,
|
|
|
|
tr_ctorMode mode,
|
|
|
|
const char ** setmeDownloadDir )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int err = 0;
|
2007-12-21 16:51:42 +00:00
|
|
|
const struct optional_args * args = &ctor->optionalArgs[mode];
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
if( !args->isSet_downloadDir )
|
2007-12-21 22:18:40 +00:00
|
|
|
err = 1;
|
2008-05-18 16:44:30 +00:00
|
|
|
else if( setmeDownloadDir )
|
|
|
|
*setmeDownloadDir = args->downloadDir;
|
2007-12-21 22:18:40 +00:00
|
|
|
|
|
|
|
return err;
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctorGetMetainfo( const tr_ctor * ctor,
|
|
|
|
const tr_benc ** setme )
|
2007-12-21 16:51:42 +00:00
|
|
|
{
|
2007-12-21 22:18:40 +00:00
|
|
|
int err = 0;
|
|
|
|
|
2008-02-13 02:24:12 +00:00
|
|
|
if( !ctor->isSet_metainfo )
|
2007-12-21 22:18:40 +00:00
|
|
|
err = 1;
|
2008-02-13 02:24:12 +00:00
|
|
|
else if( setme )
|
|
|
|
*setme = &ctor->metainfo;
|
2007-12-21 22:18:40 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
tr_ctor*
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_ctorNew( const tr_session * session )
|
2007-12-21 22:18:40 +00:00
|
|
|
{
|
|
|
|
tr_ctor * ctor = tr_new0( struct tr_ctor, 1 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
ctor->session = session;
|
|
|
|
tr_ctorSetPeerLimit( ctor, TR_FALLBACK, session->peerLimitPerTorrent );
|
2007-12-21 22:18:40 +00:00
|
|
|
tr_ctorSetPaused( ctor, TR_FALLBACK, FALSE );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_ctorSetDownloadDir( ctor, TR_FALLBACK, session->downloadDir );
|
2007-12-22 17:30:31 +00:00
|
|
|
tr_ctorSetSave( ctor, TRUE );
|
2007-12-21 22:18:40 +00:00
|
|
|
return ctor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_ctorFree( tr_ctor * ctor )
|
|
|
|
{
|
|
|
|
clearMetainfo( ctor );
|
2008-10-14 03:54:57 +00:00
|
|
|
tr_free( ctor->optionalArgs[1].downloadDir );
|
|
|
|
tr_free( ctor->optionalArgs[0].downloadDir );
|
2007-12-21 22:18:40 +00:00
|
|
|
tr_free( ctor );
|
2007-12-21 16:51:42 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|