Changeset 1634


Ignore:
Timestamp:
Apr 2, 2007, 8:32:20 PM (16 years ago)
Author:
joshe
Message:

Add API function for adding a torrent from data, instead of a filename.

Location:
branches/daemon/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/daemon/libtransmission/metainfo.c

    r1613 r1634  
    3030 * Local prototypes
    3131 **********************************************************************/
     32static int realparse( tr_info_t * inf, uint8_t * buf, size_t len );
    3233static void savedname( char * name, size_t len, const char * hash,
    3334                       const char * tag );
    34 static char * readtorrent( const char * path, const char * hash,
    35                            const char * tag, size_t * len );
     35static char * readtorrent( const char * path, size_t * len );
    3636static int savetorrent( const char * hash, const char * tag,
    3737                        const uint8_t * buf, size_t buflen );
     
    4949 *
    5050 **********************************************************************/
    51 int tr_metainfoParse( tr_info_t * inf, const char * tag, const char * path,
    52                       const char * savedHash, int saveCopy )
    53 {
    54     char       * buf;
     51int
     52tr_metainfoParseFile( tr_info_t * inf, const char * tag,
     53                      const char * path, int save )
     54{
     55    uint8_t * buf;
     56    size_t    size;
     57
     58    /* read the torrent data */
     59    buf = readtorrent( path, &size );
     60    if( NULL == buf )
     61    {
     62        return 1;
     63    }
     64
     65    if( realparse( inf, buf, size ) )
     66    {
     67        free( buf );
     68        return 1;
     69    }
     70
     71    if( save )
     72    {
     73        if( savetorrent( inf->hashString, tag, buf, size ) )
     74        {
     75            free( buf );
     76            return 1;
     77        }
     78        savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );
     79    }
     80
     81    free( buf );
     82
     83    return 0;
     84}
     85
     86int
     87tr_metainfoParseData( tr_info_t * inf, const char * tag,
     88                      uint8_t * data, size_t size, int save )
     89{
     90    if( realparse( inf, data, size ) )
     91    {
     92        return 1;
     93    }
     94
     95    if( save )
     96    {
     97        if( savetorrent( inf->hashString, tag, data, size ) )
     98        {
     99            return 1;
     100        }
     101        savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );
     102    }
     103
     104    return 0;
     105}
     106
     107int
     108tr_metainfoParseHash( tr_info_t * inf, const char * tag, const char * hash )
     109{
     110    struct stat sb;
     111    uint8_t   * buf;
     112    size_t      size;
     113    int         save;
     114
     115    /* check it we should use an old file without a tag */
     116    /* XXX this should go away at some point */
     117    save = 0;
     118    savedname( inf->torrent, sizeof inf->torrent, hash, tag );
     119    if( 0 > stat( inf->torrent, &sb ) && ENOENT == errno )
     120    {
     121        savedname( inf->torrent, sizeof inf->torrent, hash, NULL );
     122        if( 0 == stat( inf->torrent, &sb ))
     123        {
     124            save = 1;
     125        }
     126    }
     127
     128    buf = readtorrent( inf->torrent, &size );
     129    if( NULL == buf )
     130    {
     131        return 1;
     132    }
     133
     134    if( realparse( inf, buf, size ) )
     135    {
     136        free( buf );
     137        return 1;
     138    }
     139
     140    /* save a new tagged copy of the old untagged torrent */
     141    if( save )
     142    {
     143        if( savetorrent( hash, tag, buf, size ) )
     144        {
     145            free( buf );
     146            return 1;
     147        }
     148        savedname( inf->torrent, sizeof inf->torrent, hash, tag );
     149    }
     150
     151    free( buf );
     152
     153    return 0;
     154}
     155
     156int
     157realparse( tr_info_t * inf, uint8_t * buf, size_t size )
     158{
    55159    benc_val_t   meta, * beInfo, * val, * val2;
    56160    int          i;
    57     size_t       len;
    58 
    59     assert( NULL == path || NULL == savedHash );
    60     /* if savedHash isn't null, saveCopy should be false */
    61     assert( NULL == savedHash || !saveCopy );
    62 
    63     /* read the torrent data */
    64     buf = readtorrent( path, savedHash, tag, &len );
    65     if( NULL == buf )
    66     {
    67         return 1;
    68     }
    69161
    70162    /* Parse bencoded infos */
    71     if( tr_bencLoad( buf, len, &meta, NULL ) )
     163    if( tr_bencLoad( buf, size, &meta, NULL ) )
    72164    {
    73165        tr_err( "Error while parsing bencoded data" );
    74         free( buf );
    75166        return 1;
    76167    }
     
    82173        tr_err( "%s \"info\" dictionary", ( beInfo ? "Invalid" : "Missing" ) );
    83174        tr_bencFree( &meta );
    84         free( buf );
    85175        return 1;
    86176    }
     
    91181        snprintf( inf->hashString + i * 2, sizeof( inf->hashString ) - i * 2,
    92182                  "%02x", inf->hash[i] );
    93     }
    94 
    95     /* Save a copy of the torrent file */
    96     if( saveCopy )
    97     {
    98         if( savetorrent( inf->hashString, tag, (uint8_t *) buf, len ) )
    99         {
    100             tr_bencFree( &meta );
    101             free( buf );
    102             return 1;
    103         }
    104     }
    105 
    106     /* We won't need this anymore */
    107     free( buf );
    108 
    109     /* Torrent file name */
    110     if( NULL == path || saveCopy )
    111     {
    112         savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );
    113     }
    114     else
    115     {
    116         snprintf( inf->torrent, MAX_PATH_LENGTH, "%s", path );
    117183    }
    118184
     
    469535}
    470536
    471 char * readtorrent( const char * path, const char * hash, const char * tag,
    472                     size_t * len )
    473 {
    474     char         hashpath[MAX_PATH_LENGTH], * buf;
     537char * readtorrent( const char * path, size_t * size )
     538{
     539    char       * buf;
    475540    struct stat  sb;
    476541    FILE       * file;
    477     int          lasterr, save;
    478 
    479     if( NULL != hash )
    480     {
    481         savedname( hashpath, sizeof hashpath, hash, tag );
    482         path = hashpath;
    483     }
    484542
    485543    /* try to stat the file */
    486     save = 0;
    487544    if( stat( path, &sb ) )
    488545    {
    489         if( ENOENT != errno || NULL == hash )
    490         {
    491             tr_err( "Could not stat file (%s)", path );
    492             return NULL;
    493         }
    494         /* it doesn't exist, check for an old file without a tag */
    495         /* XXX this should go away at some point */
    496         lasterr = errno;
    497         savedname( hashpath, sizeof hashpath, hash, NULL );
    498         if( stat( path, &sb ) )
    499         {
    500             savedname( hashpath, sizeof hashpath, hash, tag );
    501             errno = lasterr;
    502             tr_err( "Could not stat file (%s)", path );
    503             return NULL;
    504         }
    505         save = 1;
     546        tr_err( "Could not stat file (%s)", path );
     547        return NULL;
    506548    }
    507549
     
    513555    if( sb.st_size > TORRENT_MAX_SIZE )
    514556    {
    515         tr_err( "Torrent file is too big (%d bytes)", ( int )sb.st_size );
     557        tr_err( "Torrent file is too big (%"PRIu64" bytes)",
     558                ( uint64_t )sb.st_size );
    516559        return NULL;
    517560    }
     
    527570    if( NULL == buf )
    528571    {
    529         tr_err( "Could not allocate memory (%d bytes)", ( int )sb.st_size );
     572        tr_err( "Could not allocate memory (%"PRIu64" bytes)",
     573                ( uint64_t )sb.st_size );
    530574    }
    531575    fseek( file, 0, SEEK_SET );
     
    539583    fclose( file );
    540584
    541     /* save a new tagged copy of the old untagged torrent */
    542     if( save )
    543     {
    544         savetorrent( hash, tag, (uint8_t *) buf, sb.st_size );
    545     }
    546 
    547     *len = sb.st_size;
     585    *size = sb.st_size;
    548586
    549587    return buf;
  • branches/daemon/libtransmission/metainfo.h

    r1564 r1634  
    2828#include "transmission.h"
    2929
    30 int tr_metainfoParse( tr_info_t *, const char * tag, const char * path,
    31                       const char * savedHash, int saveCopy );
     30int tr_metainfoParseFile( tr_info_t *, const char * tag,
     31                          const char * path, int save );
     32int tr_metainfoParseData( tr_info_t *, const char * tag,
     33                          uint8_t * data, size_t size, int save );
     34int tr_metainfoParseHash( tr_info_t *, const char * tag, const char * hash );
    3235void tr_metainfoFree( tr_info_t * inf );
    3336void tr_metainfoRemoveSaved( const char * hashString, const char * tag );
  • branches/daemon/libtransmission/torrent.c

    r1614 r1634  
    5454}
    5555
    56 tr_torrent_t * tr_torrentInit( tr_handle_t * h, const char * path,
    57                                uint8_t * hash, int flags, int * error )
    58 {
    59     tr_torrent_t  * tor = calloc( sizeof( tr_torrent_t ), 1 );
    60     int             saveCopy = ( TR_FLAG_SAVE & flags );
     56tr_torrent_t *
     57tr_torrentInit( tr_handle_t * h, const char * path,
     58                uint8_t * hash, int flags, int * error )
     59{
     60    tr_torrent_t * tor;
     61
     62    tor  = calloc( 1, sizeof *tor );
     63    if( NULL == tor )
     64    {
     65        *error = TR_EOTHER;
     66        return NULL;
     67    }
    6168
    6269    /* Parse torrent file */
    63     if( tr_metainfoParse( &tor->info, h->tag, path, NULL, saveCopy ) )
     70    if( tr_metainfoParseFile( &tor->info, h->tag, path,
     71                              TR_FLAG_SAVE & flags ) )
    6472    {
    6573        *error = TR_EINVALID;
     
    7179}
    7280
    73 tr_torrent_t * tr_torrentInitSaved( tr_handle_t * h, const char * hashStr,
    74                                     int flags, int * error )
    75 {
    76     tr_torrent_t  * tor = calloc( sizeof( tr_torrent_t ), 1 );
     81tr_torrent_t *
     82tr_torrentInitData( tr_handle_t * h, uint8_t * data, size_t size,
     83                    uint8_t * hash, int flags, int * error )
     84{
     85    tr_torrent_t * tor;
     86
     87    tor  = calloc( 1, sizeof *tor );
     88    if( NULL == tor )
     89    {
     90        *error = TR_EOTHER;
     91        return NULL;
     92    }
    7793
    7894    /* Parse torrent file */
    79     if( tr_metainfoParse( &tor->info, h->tag, NULL, hashStr, 0 ) )
     95    if( tr_metainfoParseData( &tor->info, h->tag, data, size,
     96                              TR_FLAG_SAVE & flags ) )
     97    {
     98        *error = TR_EINVALID;
     99        free( tor );
     100        return NULL;
     101    }
     102
     103    return torrentRealInit( h, tor, hash, flags, error );
     104}
     105
     106tr_torrent_t *
     107tr_torrentInitSaved( tr_handle_t * h, const char * hashStr,
     108                     int flags, int * error )
     109{
     110    tr_torrent_t * tor;
     111
     112    tor  = calloc( 1, sizeof *tor );
     113    if( NULL == tor )
     114    {
     115        *error = TR_EOTHER;
     116        return NULL;
     117    }
     118
     119    /* Parse torrent file */
     120    if( tr_metainfoParseHash( &tor->info, h->tag, hashStr ) )
    80121    {
    81122        *error = TR_EINVALID;
  • branches/daemon/libtransmission/transmission.h

    r1614 r1634  
    215215
    216216/***********************************************************************
     217 * tr_torrentInitData
     218 ***********************************************************************
     219 * Like tr_torrentInit, except the actual torrent data is passed in
     220 * instead of the filename.
     221 **********************************************************************/
     222tr_torrent_t * tr_torrentInitData( tr_handle_t *, uint8_t * data,
     223                                   size_t size, uint8_t * hash,
     224                                   int flags, int * error );
     225
     226/***********************************************************************
    217227 * tr_torrentInitSaved
    218228 ***********************************************************************
Note: See TracChangeset for help on using the changeset viewer.