Changeset 2455


Ignore:
Timestamp:
Jul 21, 2007, 6:46:54 PM (15 years ago)
Author:
charles
Message:

new utility function for app startup: tr_loadTorrents()

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/transmission.c

    r2444 r2455  
    2424
    2525#include <signal.h>
     26#include <sys/types.h> /* stat */
     27#include <sys/stat.h> /* stat */
     28#include <unistd.h> /* stat */
     29#include <dirent.h> /* opendir */
    2630#include "transmission.h"
    2731#include "fdlimit.h"
     32#include "list.h"
    2833#include "net.h"
    2934#include "shared.h"
     
    225230    tr_netResolveThreadClose();
    226231}
     232
     233tr_torrent_t **
     234tr_loadTorrents ( tr_handle_t   * h,
     235                  const char    * destination,
     236                  int             flags,
     237                  int          * setmeCount )
     238{
     239    int i, n = 0;
     240    struct stat sb;
     241    DIR * odir = NULL;
     242    const char * torrentDir = tr_getTorrentsDirectory( );
     243    tr_torrent_t ** torrents;
     244    tr_list_t *l=NULL, *list=NULL;
     245
     246    if( !stat( torrentDir, &sb )
     247        && S_ISDIR( sb.st_mode )
     248        && (( odir = opendir ( torrentDir ) )) )
     249    {
     250        struct dirent *d;
     251        for (d = readdir( odir ); d!=NULL; d=readdir( odir ) )
     252        {
     253            if( d->d_name && d->d_name[0]!='.' ) /* skip dotfiles, ., and .. */
     254            {
     255                tr_torrent_t * tor;
     256                char path[MAX_PATH_LENGTH];
     257                tr_buildPath( path, sizeof(path), torrentDir, d->d_name, NULL );
     258                tor = tr_torrentInit( h, path, destination, flags, NULL );
     259                if( tor != NULL ) {
     260                    list = tr_list_append( list, tor );
     261                    //fprintf (stderr, "#%d - %s\n", n, tor->info.name );
     262                    n++;
     263                }
     264            }
     265        }
     266        closedir( odir );
     267    }
     268
     269    torrents = tr_new( tr_torrent_t*, n );
     270    for( i=0, l=list; l!=NULL; l=l->next )
     271        torrents[i++] = (tr_torrent_t*) l->data;
     272    assert( i==n );
     273
     274    tr_list_free( list );
     275
     276    *setmeCount = n;
     277    tr_inf( "Loaded %d torrents from disk", *setmeCount );
     278    return torrents;
     279}
  • trunk/libtransmission/transmission.h

    r2444 r2455  
    285285void tr_close( tr_handle_t * );
    286286
     287
     288
     289/**
     290 *  Load all the torrents in tr_getTorrentsDirectory().
     291 *  This can be used at startup to kickstart all the torrents
     292 *  from the previous session.
     293 */
     294tr_torrent_t ** tr_loadTorrents ( tr_handle_t  * h,
     295                                  const char   * destination,
     296                                  int            flags,
     297                                  int          * setmeCount );
     298
     299
    287300/***********************************************************************
    288301 * tr_torrentInit
Note: See TracChangeset for help on using the changeset viewer.