Ignore:
Timestamp:
May 13, 2009, 3:54:04 PM (13 years ago)
Author:
charles
Message:

(trunk) #920: add "move data" to libT so all clients can use it

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/torrent.c

    r8257 r8389  
    240240            break;
    241241
    242         case TR_RATIOLIMIT_UNLIMITED:
     242        default: /* TR_RATIOLIMIT_UNLIMITED */
    243243            isLimited = FALSE;
    244244            break;
     
    735735
    736736void
    737 tr_torrentSetDownloadDir( tr_torrent * tor,
    738                           const char * path )
     737tr_torrentSetDownloadDir( tr_torrent * tor, const char * path )
    739738{
    740739    assert( tr_isTorrent( tor  ) );
     
    12361235
    12371236static void
    1238 torrentStart( tr_torrent * tor,
    1239               int          reloadProgress )
     1237torrentStart( tr_torrent * tor, int reloadProgress )
    12401238{
    12411239    assert( tr_isTorrent( tor ) );
     
    12731271    assert( tr_isTorrent( tor ) );
    12741272    tr_torrentRecheckCompleteness( tor );
     1273
     1274    if( tor->startAfterVerify )
     1275    {
     1276        tor->startAfterVerify = FALSE;
     1277        tr_torrentStart( tor );
     1278    }
    12751279}
    12761280
     
    12871291{
    12881292    assert( tr_isTorrent( tor ) );
    1289 
     1293    tr_globalLock( tor->session );
     1294
     1295    /* if the torrent's already being verified, stop it */
    12901296    tr_verifyRemove( tor );
    12911297
    1292     tr_globalLock( tor->session );
    1293 
     1298    /* if the torrent's running, stop it & set the restart-after-verify flag */
     1299    if( tor->isRunning ) {
     1300        tr_torrentStop( tor );
     1301        tor->startAfterVerify = TRUE;
     1302    }
     1303
     1304    /* add the torrent to the recheck queue */
    12941305    tr_torrentUncheck( tor );
    12951306    tr_verifyAdd( tor, torrentRecheckDoneCB );
     
    13161327    evbuffer_free( buf );
    13171328}
    1318 
    13191329
    13201330static void
     
    21992209***/
    22002210
     2211struct LocationData
     2212{
     2213    tr_bool move_from_old_location;
     2214    tr_bool * setme_done;
     2215    char * location;
     2216    tr_torrent * tor;
     2217};
     2218
     2219static void
     2220setLocation( void * vdata )
     2221{
     2222    struct LocationData * data = vdata;
     2223    tr_torrent * tor = data->tor;
     2224    const tr_bool do_move = data->move_from_old_location;
     2225    const char * location = data->location;
     2226
     2227    assert( tr_isTorrent( tor ) );
     2228
     2229    if( strcmp( location, tor->downloadDir ) )
     2230    {
     2231        tr_file_index_t i;
     2232
     2233        /* bad idea to move files while they're being verified... */
     2234        tr_verifyRemove( tor );
     2235
     2236        /* if the torrent is running, stop it and set a flag to
     2237         * restart after we're done */
     2238        if( tor->isRunning )
     2239        {
     2240            tr_torrentStop( tor );
     2241            tor->startAfterVerify = TRUE;
     2242        }
     2243
     2244        /* try to move the files.
     2245         * FIXME: there are still all kinds of nasty cases, like what
     2246         * if the target directory runs out of space halfway through... */
     2247        for( i=0; i<tor->info.fileCount; ++i )
     2248        {
     2249            struct stat sb;
     2250            char * oldpath = tr_buildPath( tor->downloadDir, tor->info.files[i].name, NULL );
     2251            char * newpath = tr_buildPath( location, tor->info.files[i].name, NULL );
     2252
     2253           
     2254            if( do_move && !stat( oldpath, &sb ) )
     2255            {
     2256                tr_moveFile( oldpath, newpath );
     2257                tr_torinf( tor, "moving \"%s\" to \"%s\"", oldpath, newpath );
     2258            }
     2259            else if( !stat( newpath, &sb ) )
     2260            {
     2261                tr_torinf( tor, "found \"%s\"", newpath );
     2262            }
     2263
     2264            tr_free( newpath );
     2265            tr_free( oldpath );
     2266        }
     2267
     2268        /* blow away the leftover subdirectories in the old location */
     2269        tr_torrentDeleteLocalData( tor, unlink );
     2270
     2271        /* set the new location and reverify */
     2272        tr_torrentSetDownloadDir( tor, location );
     2273        tr_torrentVerify( tor );
     2274    }
     2275
     2276    if( data->setme_done )
     2277        *data->setme_done = TRUE;
     2278
     2279    /* cleanup */
     2280    tr_free( data->location );
     2281    tr_free( data );
     2282}
     2283
     2284void
     2285tr_torrentSetLocation( tr_torrent * tor,
     2286                       const char * location,
     2287                       tr_bool      move_from_old_location,
     2288                       tr_bool    * setme_done )
     2289{
     2290    struct LocationData * data;
     2291
     2292    assert( tr_isTorrent( tor ) );
     2293
     2294    if( setme_done )
     2295        *setme_done = FALSE;
     2296
     2297    /* run this in the libtransmission thread */
     2298    data = tr_new( struct LocationData, 1 );
     2299    data->tor = tor;
     2300    data->location = tr_strdup( location );
     2301    data->move_from_old_location = move_from_old_location;
     2302    data->setme_done = setme_done;
     2303    tr_runInEventThread( tor->session, setLocation, data );
     2304}
     2305
     2306/***
     2307****
     2308***/
     2309
    22012310void
    22022311tr_torrentCheckSeedRatio( tr_torrent * tor )
Note: See TracChangeset for help on using the changeset viewer.