Changeset 1564


Ignore:
Timestamp:
Mar 13, 2007, 6:56:50 AM (15 years ago)
Author:
joshe
Message:

Append a frontend-specific tag to saved torrents.
This prevents one frontend from deleting a saved torrent used by another.

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/beos/TRWindow.cpp

    r1530 r1564  
    8383       
    8484        // Bring up the Transmission Engine
    85         engine = tr_init();
     85        engine = tr_init( "beos" );
    8686        LoadSettings();
    8787       
  • trunk/cli/transmissioncli.c

    r1562 r1564  
    124124
    125125    /* Initialize libtransmission */
    126     h = tr_init();
     126    h = tr_init( "cli" );
    127127
    128128    /* Open and parse torrent file */
  • trunk/gtk/tr_backend.c

    r1504 r1564  
    105105  TrBackend *self = (TrBackend *)instance;
    106106
    107   self->handle = tr_init();
     107  self->handle = tr_init( "gtk" );
    108108  self->disposed = FALSE;
    109109}
  • trunk/libtransmission/internal.h

    r1534 r1564  
    213213    tr_torrent_t * torrentList;
    214214
     215    char         * tag;
    215216    int            isPortSet;
    216217    int            uploadLimit;
  • trunk/libtransmission/metainfo.c

    r1562 r1564  
    3030 * Local prototypes
    3131 **********************************************************************/
     32static void savedname( char * name, size_t len, const char * hash,
     33                       const char * tag );
     34static char * readtorrent( const char * path, const char * hash,
     35                           const char * tag, size_t * len );
     36static int savetorrent( const char * hash, const char * tag,
     37                        const uint8_t * buf, size_t buflen );
    3238static int getfile( char * buf, int size,
    3339                    const char * prefix, const benc_val_t * name );
     
    4147 *
    4248 **********************************************************************/
    43 int tr_metainfoParse( tr_info_t * inf, const char * path,
     49int tr_metainfoParse( tr_info_t * inf, const char * tag, const char * path,
    4450                      const char * savedHash, int saveCopy )
    4551{
    46     FILE       * file;
    4752    char       * buf;
    4853    benc_val_t   meta, * beInfo, * list, * val;
    4954    int          i;
    50     struct stat  sb;
     55    size_t       len;
    5156
    5257    assert( NULL == path || NULL == savedHash );
     
    5459    assert( NULL == savedHash || !saveCopy );
    5560
    56     if( NULL != savedHash )
    57     {
    58         snprintf( inf->torrent, MAX_PATH_LENGTH, "%s/%s",
    59                   tr_getTorrentsDirectory(), savedHash );
    60         path = inf->torrent;
    61     }
    62 
    63     if( stat( path, &sb ) )
    64     {
    65         tr_err( "Could not stat file (%s)", path );
    66         return 1;
    67     }
    68     if( ( sb.st_mode & S_IFMT ) != S_IFREG )
    69     {
    70         tr_err( "Not a regular file (%s)", path );
    71         return 1;
    72     }
    73     if( sb.st_size > TORRENT_MAX_SIZE )
    74     {
    75         tr_err( "Torrent file is too big (%d bytes)", (int)sb.st_size );
    76         return 1;
    77     }
    78 
    79     /* Load the torrent file into our buffer */
    80     file = fopen( path, "rb" );
    81     if( !file )
    82     {
    83         tr_err( "Could not open file (%s)", path );
    84         return 1;
    85     }
    86     buf = malloc( sb.st_size );
    87     fseek( file, 0, SEEK_SET );
    88     if( fread( buf, sb.st_size, 1, file ) != 1 )
    89     {
    90         tr_err( "Read error (%s)", path );
    91         free( buf );
    92         fclose( file );
    93         return 1;
    94     }
    95     fclose( file );
     61    /* read the torrent data */
     62    buf = readtorrent( path, savedHash, tag, &len );
     63    if( NULL == buf )
     64    {
     65        return 1;
     66    }
    9667
    9768    /* Parse bencoded infos */
    98     if( tr_bencLoad( buf, sb.st_size, &meta, NULL ) )
     69    if( tr_bencLoad( buf, len, &meta, NULL ) )
    9970    {
    10071        tr_err( "Error while parsing bencoded data" );
     
    12091    }
    12192
     93    /* Save a copy of the torrent file */
    12294    if( saveCopy )
    12395    {
    124         /* Save a copy of the torrent file in the private torrent directory */
    125         snprintf( inf->torrent, MAX_PATH_LENGTH, "%s/%s",
    126                   tr_getTorrentsDirectory(), inf->hashString );
    127         file = fopen( inf->torrent, "wb" );
    128         if( !file )
    129         {
    130             tr_err( "Could not open file (%s) (%s)",
    131                     inf->torrent, strerror(errno) );
     96        if( savetorrent( inf->hashString, tag, buf, len ) )
     97        {
    13298            tr_bencFree( &meta );
    13399            free( buf );
    134100            return 1;
    135101        }
    136         fseek( file, 0, SEEK_SET );
    137         if( fwrite( buf, sb.st_size, 1, file ) != 1 )
    138         {
    139             tr_err( "Write error (%s)", inf->torrent );
    140             tr_bencFree( &meta );
    141             free( buf );
    142             fclose( file );
    143             return 1;
    144         }
    145         fclose( file );
    146     }
    147     else
    148     {
    149         snprintf( inf->torrent, MAX_PATH_LENGTH, "%s", path );
    150102    }
    151103
    152104    /* We won't need this anymore */
    153105    free( buf );
    154        
     106
     107    /* Torrent file name */
     108    if( NULL == path || saveCopy )
     109    {
     110        savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag );
     111    }
     112    else
     113    {
     114        snprintf( inf->torrent, MAX_PATH_LENGTH, "%s", path );
     115    }
     116
    155117    /* Comment info */
    156118    val = tr_bencDictFindFirst( &meta, "comment.utf-8", "comment", NULL );
     
    531493}
    532494
    533 void tr_metainfoRemoveSaved( const char * hashString )
     495void savedname( char * name, size_t len, const char * hash, const char * tag )
     496{
     497    if( NULL == tag )
     498    {
     499        snprintf( name, len, "%s/%s", tr_getTorrentsDirectory(), hash );
     500    }
     501    else
     502    {
     503        snprintf( name, len, "%s/%s-%s",
     504                  tr_getTorrentsDirectory(), hash, tag );
     505    }
     506}
     507
     508void tr_metainfoRemoveSaved( const char * hashString, const char * tag )
    534509{
    535510    char file[MAX_PATH_LENGTH];
    536511
    537     snprintf( file, MAX_PATH_LENGTH, "%s/%s",
    538               tr_getTorrentsDirectory(), hashString );
     512    savedname( file, sizeof file, hashString, tag );
    539513    unlink(file);
     514}
     515
     516char * readtorrent( const char * path, const char * hash, const char * tag,
     517                    size_t * len )
     518{
     519    char         hashpath[MAX_PATH_LENGTH], * buf;
     520    struct stat  sb;
     521    FILE       * file;
     522    int          lasterr, save;
     523
     524    if( NULL != hash )
     525    {
     526        savedname( hashpath, sizeof hashpath, hash, tag );
     527        path = hashpath;
     528    }
     529
     530    /* try to stat the file */
     531    save = 0;
     532    if( stat( path, &sb ) )
     533    {
     534        if( ENOENT != errno || NULL == hash )
     535        {
     536            tr_err( "Could not stat file (%s)", path );
     537            return NULL;
     538        }
     539        /* it doesn't exist, check for an old file without a tag */
     540        /* XXX this should go away at some point */
     541        lasterr = errno;
     542        savedname( hashpath, sizeof hashpath, hash, NULL );
     543        if( stat( path, &sb ) )
     544        {
     545            savedname( hashpath, sizeof hashpath, hash, tag );
     546            errno = lasterr;
     547            tr_err( "Could not stat file (%s)", path );
     548            return NULL;
     549        }
     550        save = 1;
     551    }
     552
     553    if( ( sb.st_mode & S_IFMT ) != S_IFREG )
     554    {
     555        tr_err( "Not a regular file (%s)", path );
     556        return NULL;
     557    }
     558    if( sb.st_size > TORRENT_MAX_SIZE )
     559    {
     560        tr_err( "Torrent file is too big (%d bytes)", ( int )sb.st_size );
     561        return NULL;
     562    }
     563
     564    /* Load the torrent file into our buffer */
     565    file = fopen( path, "rb" );
     566    if( !file )
     567    {
     568        tr_err( "Could not open file (%s)", path );
     569        return NULL;
     570    }
     571    buf = malloc( sb.st_size );
     572    if( NULL == buf )
     573    {
     574        tr_err( "Could not allocate memory (%d bytes)", ( int )sb.st_size );
     575    }
     576    fseek( file, 0, SEEK_SET );
     577    if( fread( buf, sb.st_size, 1, file ) != 1 )
     578    {
     579        tr_err( "Read error (%s)", path );
     580        free( buf );
     581        fclose( file );
     582        return NULL;
     583    }
     584    fclose( file );
     585
     586    /* save a new tagged copy of the old untagged torrent */
     587    if( save )
     588    {
     589        savetorrent( hash, tag, buf, sb.st_size );
     590    }
     591
     592    *len = sb.st_size;
     593
     594    return buf;
     595}
     596
     597/* Save a copy of the torrent file in the saved torrent directory */
     598int savetorrent( const char * hash, const char * tag,
     599                 const uint8_t * buf, size_t buflen )
     600{
     601    char   path[MAX_PATH_LENGTH];
     602    FILE * file;
     603
     604    savedname( path, sizeof path, hash, tag );
     605    file = fopen( path, "wb" );
     606    if( !file )
     607    {
     608        tr_err( "Could not open file (%s) (%s)", path, strerror( errno ) );
     609        return 1;
     610    }
     611    fseek( file, 0, SEEK_SET );
     612    if( fwrite( buf, 1, buflen, file ) != buflen )
     613    {
     614        tr_err( "Could not write file (%s) (%s)", path, strerror( errno ) );
     615        fclose( file );
     616        return 1;
     617    }
     618    fclose( file );
     619
     620    return 0;
    540621}
    541622
  • trunk/libtransmission/metainfo.h

    r1403 r1564  
    2828#include "transmission.h"
    2929
    30 int tr_metainfoParse( tr_info_t *, const char * path,
     30int tr_metainfoParse( tr_info_t *, const char * tag, const char * path,
    3131                      const char * savedHash, int saveCopy );
    3232void tr_metainfoFree( tr_info_t * inf );
    33 void tr_metainfoRemoveSaved( const char * hashString );
     33void tr_metainfoRemoveSaved( const char * hashString, const char * tag );
    3434
    3535#endif
  • trunk/libtransmission/torrent.c

    r1562 r1564  
    6161
    6262    /* Parse torrent file */
    63     if( tr_metainfoParse( &tor->info, path, NULL, saveCopy ) )
     63    if( tr_metainfoParse( &tor->info, h->tag, path, NULL, saveCopy ) )
    6464    {
    6565        *error = TR_EINVALID;
     
    7777
    7878    /* Parse torrent file */
    79     if( tr_metainfoParse( &tor->info, NULL, hashStr, 0 ) )
     79    if( tr_metainfoParse( &tor->info, h->tag, NULL, hashStr, 0 ) )
    8080    {
    8181        *error = TR_EINVALID;
     
    558558void tr_torrentRemoveSaved( tr_torrent_t * tor )
    559559{
    560     tr_metainfoRemoveSaved( tor->info.hashString );
     560    tr_metainfoRemoveSaved( tor->info.hashString, tor->handle->tag );
    561561}
    562562
  • trunk/libtransmission/transmission.c

    r1562 r1564  
    3131 * Allocates a tr_handle_t structure and initializes a few things
    3232 **********************************************************************/
    33 tr_handle_t * tr_init()
     33tr_handle_t * tr_init( const char * tag )
    3434{
    3535    tr_handle_t * h;
     
    4040
    4141    h = calloc( sizeof( tr_handle_t ), 1 );
     42    if( NULL == h )
     43    {
     44        return NULL;
     45    }
     46
     47    h->tag = strdup( tag );
     48    if( NULL == h->tag )
     49    {
     50        free( h );
     51        return NULL;
     52    }
    4253
    4354    /* Generate a peer id : "-TRxxyy-" + 12 random alphanumeric
  • trunk/libtransmission/transmission.h

    r1517 r1564  
    7373 ***********************************************************************
    7474 * Initializes a libtransmission instance. Returns a obscure handle to
    75  * be passed to all functions below.
     75 * be passed to all functions below. The tag argument is a short string
     76 * unique to the program invoking tr_init(), it is currently used as
     77 * part of saved torrent files' names to prevent one frontend from
     78 * deleting a torrent used by another. The following tags are used:
     79 *   beos cli daemon gtk macosx
    7680 **********************************************************************/
    7781typedef struct tr_handle_s tr_handle_t;
    78 tr_handle_t * tr_init();
     82tr_handle_t * tr_init( const char * tag );
    7983
    8084typedef struct tr_tracker_info_s tr_tracker_info_t;
  • trunk/macosx/Controller.m

    r1503 r1564  
    137137    if ((self = [super init]))
    138138    {
    139         fLib = tr_init();
     139        fLib = tr_init( "macosx" );
    140140       
    141141        fTorrents = [[NSMutableArray alloc] initWithCapacity: 10];
Note: See TracChangeset for help on using the changeset viewer.