(trunk) clean up the return value of tr_torrentParse()
This commit is contained in:
parent
360f5c49ae
commit
1c3b53244d
|
@ -891,12 +891,12 @@ coreerr( TrCore * core UNUSED, guint code, const char * msg, struct cbdata * c )
|
|||
{
|
||||
switch( code )
|
||||
{
|
||||
case TR_EINVALID:
|
||||
case TR_PARSE_ERR:
|
||||
c->errqueue =
|
||||
g_slist_append( c->errqueue, g_path_get_basename( msg ) );
|
||||
break;
|
||||
|
||||
case TR_EDUPLICATE:
|
||||
case TR_PARSE_DUPLICATE:
|
||||
c->dupqueue = g_slist_append( c->dupqueue, g_strdup( msg ) );
|
||||
break;
|
||||
|
||||
|
|
|
@ -826,11 +826,10 @@ add_ctor( TrCore * core, tr_ctor * ctor, gboolean doPrompt, gboolean doNotify )
|
|||
|
||||
switch( err )
|
||||
{
|
||||
case TR_EINVALID:
|
||||
case TR_PARSE_ERR:
|
||||
break;
|
||||
|
||||
case TR_EDUPLICATE:
|
||||
g_message( "it's a duplicate" );
|
||||
case TR_PARSE_DUPLICATE:
|
||||
/* don't complain about .torrent files in the watch directory
|
||||
* that have already been added... that gets annoying and we
|
||||
* don't want to be nagging users to clean up their watch dirs */
|
||||
|
@ -910,8 +909,8 @@ add_filename( TrCore * core,
|
|||
tr_ctorSetMetainfoFromFile( ctor, filename );
|
||||
|
||||
err = add_ctor( core, ctor, doPrompt, doNotify );
|
||||
if( err == TR_EINVALID )
|
||||
tr_core_errsig( core, TR_EINVALID, filename );
|
||||
if( err == TR_PARSE_ERR )
|
||||
tr_core_errsig( core, TR_PARSE_ERR, filename );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -85,9 +85,9 @@ TrCoreClass;
|
|||
|
||||
enum tr_core_err
|
||||
{
|
||||
TR_CORE_ERR_ADD_TORRENT_ERR = TR_EINVALID,
|
||||
TR_CORE_ERR_ADD_TORRENT_DUP = TR_EDUPLICATE,
|
||||
TR_CORE_ERR_NO_MORE_TORRENTS /* finished adding a batch */
|
||||
TR_CORE_ERR_ADD_TORRENT_ERR = TR_PARSE_ERR,
|
||||
TR_CORE_ERR_ADD_TORRENT_DUP = TR_PARSE_DUPLICATE,
|
||||
TR_CORE_ERR_NO_MORE_TORRENTS = 1000 /* finished adding a batch */
|
||||
};
|
||||
|
||||
GType tr_core_get_type( void );
|
||||
|
|
14
gtk/util.c
14
gtk/util.c
|
@ -305,17 +305,9 @@ addTorrentErrorDialog( GtkWidget * child,
|
|||
|
||||
switch( err )
|
||||
{
|
||||
case TR_EINVALID:
|
||||
fmt = _( "The torrent file \"%s\" contains invalid data." );
|
||||
break;
|
||||
|
||||
case TR_EDUPLICATE:
|
||||
fmt = _( "The torrent file \"%s\" is already in use." ); break;
|
||||
|
||||
default:
|
||||
fmt = _(
|
||||
"The torrent file \"%s\" encountered an unknown error." );
|
||||
break;
|
||||
case TR_PARSE_ERR: fmt = _( "The torrent file \"%s\" contains invalid data." ); break;
|
||||
case TR_PARSE_DUPLICATE: fmt = _( "The torrent file \"%s\" is already in use." ); break;
|
||||
default: fmt = _( "The torrent file \"%s\" encountered an unknown error." ); break;
|
||||
}
|
||||
secondary = g_strdup_printf( fmt, filename );
|
||||
win = ( !child || GTK_IS_WINDOW( child ) )
|
||||
|
|
|
@ -135,18 +135,14 @@ tr_metainfoMigrate( tr_session * session,
|
|||
****
|
||||
***/
|
||||
|
||||
static int
|
||||
static tr_bool
|
||||
getfile( char ** setme,
|
||||
const char * root,
|
||||
tr_benc * path )
|
||||
{
|
||||
int err;
|
||||
tr_bool success = FALSE;
|
||||
|
||||
if( !tr_bencIsList( path ) )
|
||||
{
|
||||
err = TR_EINVALID;
|
||||
}
|
||||
else
|
||||
if( tr_bencIsList( path ) )
|
||||
{
|
||||
struct evbuffer * buf = evbuffer_new( );
|
||||
int n = tr_bencListSize( path );
|
||||
|
@ -167,10 +163,10 @@ getfile( char ** setme,
|
|||
*setme = tr_utf8clean( (char*)EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ), NULL );
|
||||
/* fprintf( stderr, "[%s]\n", *setme ); */
|
||||
evbuffer_free( buf );
|
||||
err = 0;
|
||||
success = TRUE;
|
||||
}
|
||||
|
||||
return err;
|
||||
return success;
|
||||
}
|
||||
|
||||
static const char*
|
||||
|
@ -203,7 +199,7 @@ parseFiles( tr_info * inf,
|
|||
if( !tr_bencDictFindList( file, "path", &path ) )
|
||||
return "path";
|
||||
|
||||
if( getfile( &inf->files[i].name, inf->name, path ) )
|
||||
if( !getfile( &inf->files[i].name, inf->name, path ) )
|
||||
return "path";
|
||||
|
||||
if( !tr_bencDictFindInt( file, "length", &len ) )
|
||||
|
@ -461,20 +457,21 @@ tr_metainfoParseImpl( const tr_session * session,
|
|||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
tr_bool
|
||||
tr_metainfoParse( const tr_session * session,
|
||||
tr_info * inf,
|
||||
const tr_benc * meta_in )
|
||||
tr_info * inf,
|
||||
const tr_benc * meta_in )
|
||||
{
|
||||
const char * badTag = tr_metainfoParseImpl( session, inf, meta_in );
|
||||
const tr_bool success = badTag == NULL;
|
||||
|
||||
if( badTag )
|
||||
{
|
||||
tr_nerr( inf->name, _( "Invalid metadata entry \"%s\"" ), badTag );
|
||||
tr_metainfoFree( inf );
|
||||
return TR_EINVALID;
|
||||
}
|
||||
return 0;
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -33,9 +33,9 @@
|
|||
|
||||
struct tr_benc;
|
||||
|
||||
int tr_metainfoParse( const tr_session * session,
|
||||
tr_info * info,
|
||||
const struct tr_benc * benc );
|
||||
tr_bool tr_metainfoParse( const tr_session * session,
|
||||
tr_info * info,
|
||||
const struct tr_benc * benc );
|
||||
|
||||
void tr_metainfoRemoveSaved( const tr_session * session,
|
||||
const tr_info * info );
|
||||
|
|
|
@ -926,11 +926,11 @@ addTorrentImpl( struct tr_rpc_idle_data * data, tr_ctor * ctor )
|
|||
notify( data->session, TR_RPC_TORRENT_ADDED, tor );
|
||||
tr_bencFree( &fields );
|
||||
}
|
||||
else if( err == TR_EDUPLICATE )
|
||||
else if( err == TR_PARSE_DUPLICATE )
|
||||
{
|
||||
result = "duplicate torrent";
|
||||
}
|
||||
else if( err == TR_EINVALID )
|
||||
else if( err == TR_PARSE_ERR )
|
||||
{
|
||||
result = "invalid or corrupt torrent file";
|
||||
}
|
||||
|
|
|
@ -670,12 +670,12 @@ torrentRealInit( tr_torrent * tor, const tr_ctor * ctor )
|
|||
torrentStart( tor, FALSE );
|
||||
}
|
||||
|
||||
int
|
||||
tr_torrentParse( const tr_ctor * ctor,
|
||||
tr_info * setmeInfo )
|
||||
tr_parse_result
|
||||
tr_torrentParse( const tr_ctor * ctor, tr_info * setmeInfo )
|
||||
{
|
||||
int err = 0;
|
||||
int doFree;
|
||||
tr_bool didParse;
|
||||
tr_parse_result result;
|
||||
tr_info tmp;
|
||||
const tr_benc * metainfo;
|
||||
tr_session * session = tr_ctorGetSession( ctor );
|
||||
|
@ -684,22 +684,22 @@ tr_torrentParse( const tr_ctor * ctor,
|
|||
setmeInfo = &tmp;
|
||||
memset( setmeInfo, 0, sizeof( tr_info ) );
|
||||
|
||||
if( !err && tr_ctorGetMetainfo( ctor, &metainfo ) )
|
||||
return TR_EINVALID;
|
||||
if( tr_ctorGetMetainfo( ctor, &metainfo ) )
|
||||
return TR_PARSE_ERR;
|
||||
|
||||
err = tr_metainfoParse( session, setmeInfo, metainfo );
|
||||
doFree = !err && ( setmeInfo == &tmp );
|
||||
didParse = tr_metainfoParse( session, setmeInfo, metainfo );
|
||||
doFree = didParse && ( setmeInfo == &tmp );
|
||||
|
||||
if( !err && !getBlockSize( setmeInfo->pieceSize ) )
|
||||
err = TR_EINVALID;
|
||||
if( didParse && !getBlockSize( setmeInfo->pieceSize ) )
|
||||
result = TR_PARSE_ERR;
|
||||
|
||||
if( !err && session && tr_torrentExists( session, setmeInfo->hash ) )
|
||||
err = TR_EDUPLICATE;
|
||||
if( didParse && session && tr_torrentExists( session, setmeInfo->hash ) )
|
||||
result = TR_PARSE_DUPLICATE;
|
||||
|
||||
if( doFree )
|
||||
tr_metainfoFree( setmeInfo );
|
||||
|
||||
return err;
|
||||
return result;
|
||||
}
|
||||
|
||||
tr_torrent *
|
||||
|
@ -1960,7 +1960,7 @@ tr_torrentSetAnnounceList( tr_torrent * tor,
|
|||
|
||||
/* try to parse it back again, to make sure it's good */
|
||||
memset( &tmpInfo, 0, sizeof( tr_info ) );
|
||||
if( !tr_metainfoParse( tor->session, &tmpInfo, &metainfo ) )
|
||||
if( tr_metainfoParse( tor->session, &tmpInfo, &metainfo ) )
|
||||
{
|
||||
/* it's good, so keep these new trackers and free the old ones */
|
||||
|
||||
|
|
|
@ -873,26 +873,36 @@ tr_session* tr_ctorGetSession( const tr_ctor * ctor );
|
|||
/* returns NULL if tr_ctorSetMetainfoFromFile() wasn't used */
|
||||
const char* tr_ctorGetSourceFile( const tr_ctor * ctor );
|
||||
|
||||
#define TR_EINVALID 1
|
||||
#define TR_EDUPLICATE 2
|
||||
typedef enum
|
||||
{
|
||||
TR_PARSE_OK,
|
||||
TR_PARSE_ERR,
|
||||
TR_PARSE_DUPLICATE
|
||||
}
|
||||
tr_parse_result;
|
||||
|
||||
/**
|
||||
* Parses the specified metainfo.
|
||||
* Returns 0 if it parsed successfully and can be added to Transmission.
|
||||
* Returns TR_EINVALID if it couldn't be parsed.
|
||||
* Returns TR_EDUPLICATE if it parsed but can't be added.
|
||||
* "download-dir" must be set to test for TR_EDUPLICATE.
|
||||
* @brief Parses the specified metainfo
|
||||
*
|
||||
* If setme_info is non-NULL and parsing is successful
|
||||
* (that is, if TR_EINVALID is not returned), then the parsed
|
||||
* metainfo is stored in setme_info and should be freed by the
|
||||
* caller via tr_metainfoFree().
|
||||
* @return TR_PARSE_ERR if parsing failed;
|
||||
* TR_PARSE_OK if parsing succeeded and it's not a duplicate;
|
||||
* TR_PARSE_DUPLICATE if parsing succeeded but it's a duplicate.
|
||||
*
|
||||
* If the constructor's session variable is NULL,
|
||||
* info.torrent will be NULL and the duplicate check will not be performed.
|
||||
* @param setme_info If parsing is successful and setme_info is non-NULL,
|
||||
* the parsed metainfo is stored there and sould be freed
|
||||
* by calling tr_metainfoFree() when no longer needed.
|
||||
*
|
||||
* Notes:
|
||||
*
|
||||
* 1. tr_torrentParse() won't be able to check for duplicates -- and therefore
|
||||
* won't return TR_PARSE_DUPLICATE -- unless ctor's "download-dir" and
|
||||
* session variable is set.
|
||||
*
|
||||
* 2. setme_info->torrent's value can't be set unless ctor's session variable
|
||||
* is set.
|
||||
*/
|
||||
int tr_torrentParse( const tr_ctor * ctor,
|
||||
tr_info * setme_info_or_NULL );
|
||||
tr_parse_result tr_torrentParse( const tr_ctor * ctor,
|
||||
tr_info * setme_info_or_NULL );
|
||||
|
||||
/** @brief free a metainfo
|
||||
@see tr_torrentParse */
|
||||
|
|
|
@ -799,9 +799,9 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy
|
|||
int result = tr_torrentParse(ctor, &info);
|
||||
if (result != TR_OK)
|
||||
{
|
||||
if (result == TR_EDUPLICATE)
|
||||
if (result == TR_PARSE_DUPLICATE)
|
||||
[self duplicateOpenAlert: [NSString stringWithUTF8String: info.name]];
|
||||
else if (result == TR_EINVALID)
|
||||
else if (result == TR_PARSE_ERR)
|
||||
{
|
||||
if (type != ADD_AUTO)
|
||||
[self invalidOpenAlert: [torrentPath lastPathComponent]];
|
||||
|
|
Loading…
Reference in New Issue