Changeset 281


Ignore:
Timestamp:
Jun 7, 2006, 4:05:15 AM (16 years ago)
Author:
joshe
Message:

Add libtransmission support for saving private copies of torrent files.

Location:
branches/save_torrent_files/libtransmission
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/save_torrent_files/libtransmission/fastresume.h

    r261 r281  
    6464static char * fastResumeFileName( tr_io_t * io )
    6565{
    66     char * ret, * p;
    67     int i;
    68 
    69     asprintf( &ret, "%s/resume.%40d", tr_getPrefsDirectory(), 0 );
    70 
    71     p = &ret[ strlen( ret ) - 2 * SHA_DIGEST_LENGTH ];
    72     for( i = 0; i < SHA_DIGEST_LENGTH; i++ )
    73     {
    74         sprintf( p, "%02x", io->tor->info.hash[i] );
    75         p += 2;
    76     }
     66    char * ret;
     67
     68    asprintf( &ret, "%s/resume.%s", tr_getPrefsDirectory(),
     69              io->tor->info.hashString );
    7770
    7871    return ret;
  • branches/save_torrent_files/libtransmission/internal.h

    r261 r281  
    4646#include <netinet/in.h>
    4747#include <fcntl.h>
     48#include <assert.h>
    4849#ifdef BEOS_NETSERVER
    4950#  define in_port_t uint16_t
  • branches/save_torrent_files/libtransmission/metainfo.c

    r261 r281  
    3535 *
    3636 **********************************************************************/
    37 int tr_metainfoParse( tr_info_t * inf, const char * path )
     37int tr_metainfoParse( tr_info_t * inf, const char * path,
     38                      const char * savedHash, int saveCopy )
    3839{
    3940    FILE       * file;
     
    4445    struct stat sb;
    4546
    46     snprintf( inf->torrent, MAX_PATH_LENGTH, "%s", path );
     47    assert( NULL == path || NULL == savedHash );
     48    /* if savedHash isn't null, saveCopy should be false */
     49    assert( NULL == savedHash || !saveCopy );
     50
     51    if ( NULL != savedHash )
     52    {
     53        snprintf( inf->torrent, MAX_PATH_LENGTH, "%s/torrents/%s",
     54                  tr_getPrefsDirectory(), savedHash );
     55        path = inf->torrent;
     56    }
    4757
    4858    if( stat( path, &sb ) )
     
    98108    SHA1( (uint8_t *) beInfo->begin,
    99109          (long) beInfo->end - (long) beInfo->begin, inf->hash );
    100 
    101     /* No that we got the hash, we won't need this anymore */
     110    for( i = 0; i < SHA_DIGEST_LENGTH; i++ )
     111    {
     112        sprintf( inf->hashString + i * 2, "%02x", inf->hash[i] );
     113    }
     114
     115    if( saveCopy )
     116    {
     117        /* Create ~/.transmission/torrents/ directory */
     118        snprintf( inf->torrent, MAX_PATH_LENGTH, "%s/torrents/%s",
     119                  tr_getPrefsDirectory(), inf->hashString );
     120        s = strrchr( inf->torrent, '/' );
     121        *s = '\0';
     122        if( mkdir( inf->torrent, 0777 ) && EEXIST != errno )
     123        {
     124            fprintf( stderr, "Could not create directory (%s)\n", inf->torrent );
     125            tr_bencFree( &meta );
     126            free( buf );
     127            return 1;
     128        }
     129        *s = '/';
     130
     131        /* Save a copy of the torrent file in ~/.transmission/torrents */
     132        file = fopen( inf->torrent, "wb" );
     133        if( !file )
     134        {
     135            fprintf( stderr, "Could not open file (%s)\n", inf->torrent );
     136            tr_bencFree( &meta );
     137            free( buf );
     138            return 1;
     139        }
     140        fseek( file, 0, SEEK_SET );
     141        if( fwrite( buf, sb.st_size, 1, file ) != 1 )
     142        {
     143            fprintf( stderr, "Write error (%s)\n", inf->torrent );
     144            tr_bencFree( &meta );
     145            free( buf );
     146            fclose( file );
     147            return 1;
     148        }
     149        fclose( file );
     150    }
     151    else
     152    {
     153        snprintf( inf->torrent, MAX_PATH_LENGTH, "%s", path );
     154    }
     155
     156    /* We won't need this anymore */
    102157    free( buf );
    103158
     
    225280}
    226281
     282void tr_metainfoRemoveSaved( const char * hashString )
     283{
     284    char file[MAX_PATH_LENGTH];
     285
     286    snprintf( file, MAX_PATH_LENGTH, "%s/torrents/%s",
     287              tr_getPrefsDirectory(), hashString );
     288    unlink(file);
     289}
     290
    227291/***********************************************************************
    228292 * strcatUTF8
  • branches/save_torrent_files/libtransmission/metainfo.h

    r261 r281  
    2626#define TR_METAINFO_H 1
    2727
    28 int tr_metainfoParse( tr_info_t *, const char * );
     28int tr_metainfoParse( tr_info_t *, const char * path,
     29                      const char * savedHash, int saveCopy );
    2930
     31void tr_metainfoRemoveSaved( const char * hashString );
    3032#endif
  • branches/save_torrent_files/libtransmission/transmission.c

    r261 r281  
    2828 * Local prototypes
    2929 **********************************************************************/
     30static tr_torrent_t * torrentRealInit( tr_handle_t *, tr_torrent_t * tor,
     31                                       int flags, int * error );
    3032static void torrentReallyStop( tr_torrent_t * );
    3133static void  downloadLoop( void * );
     
    175177}
    176178
     179tr_torrent_t * tr_torrentInit( tr_handle_t * h, const char * path,
     180                               int flags, int * error )
     181{
     182    tr_torrent_t  * tor = calloc( sizeof( tr_torrent_t ), 1 );
     183    int             saveCopy = ( TR_FSAVEPRIVATE & flags );
     184
     185    /* Parse torrent file */
     186    if( tr_metainfoParse( &tor->info, path, NULL, saveCopy ) )
     187    {
     188        *error = TR_EINVALID;
     189        free( tor );
     190        return NULL;
     191    }
     192
     193    return torrentRealInit( h, tor, flags, error );
     194}
     195
     196tr_torrent_t * tr_torrentInitSaved( tr_handle_t * h, const char * hashStr,
     197                                    int flags, int * error )
     198{
     199    tr_torrent_t  * tor = calloc( sizeof( tr_torrent_t ), 1 );
     200
     201    /* Parse torrent file */
     202    if( tr_metainfoParse( &tor->info, NULL, hashStr, 0 ) )
     203    {
     204        *error = TR_EINVALID;
     205        free( tor );
     206        return NULL;
     207    }
     208
     209    return torrentRealInit( h, tor, ( TR_FSAVEPRIVATE | flags ), error );
     210}
     211
    177212/***********************************************************************
    178213 * tr_torrentInit
     
    181216 * to fill it.
    182217 **********************************************************************/
    183 tr_torrent_t * tr_torrentInit( tr_handle_t * h, const char * path,
    184                                int * error )
    185 {
    186     tr_torrent_t  * tor, * tor_tmp;
     218static tr_torrent_t * torrentRealInit( tr_handle_t * h, tr_torrent_t * tor,
     219                                       int flags, int * error )
     220{
     221    tr_torrent_t  * tor_tmp;
    187222    tr_info_t     * inf;
    188223    int             i;
    189224    char          * s1, * s2;
    190225
    191     tor = calloc( sizeof( tr_torrent_t ), 1 );
    192     inf = &tor->info;
    193 
    194     /* Parse torrent file */
    195     if( tr_metainfoParse( inf, path ) )
    196     {
    197         *error = TR_EINVALID;
    198         free( tor );
    199         return NULL;
    200     }
     226    inf        = &tor->info;
     227    inf->flags = flags;
    201228
    202229    /* Make sure this torrent is not already open */
     
    480507    }
    481508    tr_lockUnlock( &tor->lock );
     509}
     510
     511void tr_torrentRemoveSaved( tr_torrent_t * tor ) {
     512    tr_metainfoRemoveSaved( tor->info.hashString );
    482513}
    483514
  • branches/save_torrent_files/libtransmission/transmission.h

    r264 r281  
    124124 * valid torrent file, returns an handle and adds it to the list of
    125125 * torrents (but doesn't start it). Returns NULL and sets *error
    126  * otherwise.
     126 * otherwise.  If the TR_FSAVEPRIVATE flag is passed then a private copy
     127 * of the torrent file will be saved.
    127128 **********************************************************************/
    128129#define TR_EINVALID     1
     
    131132#define TR_EOTHER       666
    132133tr_torrent_t * tr_torrentInit( tr_handle_t *, const char * path,
    133                                int * error );
     134                               int flags, int * error );
     135
     136/***********************************************************************
     137 * tr_torrentInitSaved
     138 ***********************************************************************
     139 * Opens and parses a torrent file as with tr_torrentInit, only taking
     140 * the hash string of a saved torrent file instead of a filename.  There
     141 * are currently no valid flags for this function.
     142 **********************************************************************/
     143tr_torrent_t * tr_torrentInitSaved( tr_handle_t *, const char * hashStr,
     144                                    int flags, int * error );
    134145
    135146typedef struct tr_info_s tr_info_t;
     
    202213
    203214/***********************************************************************
     215 * tr_torrentRemoveSaved
     216 ***********************************************************************
     217 * Removes the private saved copy of a torrent file for torrents which
     218 * the TR_FSAVEPRIVATE flag is set.
     219 **********************************************************************/
     220void tr_torrentRemoveSaved( tr_torrent_t * );
     221
     222/***********************************************************************
    204223 * tr_torrentClose
    205224 ***********************************************************************
     
    208227 **********************************************************************/
    209228void tr_torrentClose( tr_handle_t *, tr_torrent_t * );
    210 
    211229
    212230/***********************************************************************
     
    226244    /* General info */
    227245    uint8_t     hash[SHA_DIGEST_LENGTH];
     246    char        hashString[2*SHA_DIGEST_LENGTH+1];
    228247    char        name[MAX_PATH_LENGTH];
     248
     249    /* Flags */
     250#define TR_FSAVEPRIVATE 0x01    /* save a private copy of the torrent */
     251    int         flags;
    229252
    230253    /* Tracker info */
Note: See TracChangeset for help on using the changeset viewer.