From: Charles Kerr Date: Wed, 20 May 2009 16:02:12 +0000 (+0000) Subject: (trunk) #920 Add "Move Data" to libT so all clients can use it X-Git-Tag: 1.70~139 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7d844ec27950462c734d35dfccbead0364a8a1c7;p=transmission (trunk) #920 Add "Move Data" to libT so all clients can use it --- diff --git a/gtk/relocate.c b/gtk/relocate.c index 6b128cc69..261aa1ef6 100644 --- a/gtk/relocate.c +++ b/gtk/relocate.c @@ -36,7 +36,7 @@ onTimer( gpointer gdata ) struct UpdateData * data = gdata; const tr_bool done = data->done; - if( done ) + if( done != TR_LOC_MOVING ) { gtk_widget_destroy( GTK_WIDGET( data->dialog ) ); g_free( data ); @@ -75,7 +75,7 @@ onResponse( GtkDialog * dialog, int response, gpointer unused UNUSED ) updateData = g_new( struct UpdateData, 1 ); updateData->dialog = dialog; updateData->done = FALSE; - tr_torrentSetLocation( tor, location, do_move, &updateData->done ); + tr_torrentSetLocation( tor, location, do_move, NULL, &updateData->done ); gtr_timeout_add_seconds( 1, onTimer, updateData ); /* remember this location so that it can be the default next time */ diff --git a/libtransmission/rpcimpl.c b/libtransmission/rpcimpl.c index fc39309d2..4ce251a01 100644 --- a/libtransmission/rpcimpl.c +++ b/libtransmission/rpcimpl.c @@ -794,7 +794,7 @@ torrentSetLocation( tr_session * session, for( i=0; itor; const tr_bool do_move = data->move_from_old_location; const char * location = data->location; + double bytesHandled = 0; assert( tr_isTorrent( tor ) ); @@ -2313,37 +2316,51 @@ setLocation( void * vdata ) /* try to move the files. * FIXME: there are still all kinds of nasty cases, like what * if the target directory runs out of space halfway through... */ - for( i=0; iinfo.fileCount; ++i ) + for( i=0; !err && iinfo.fileCount; ++i ) { struct stat sb; - char * oldpath = tr_buildPath( tor->downloadDir, tor->info.files[i].name, NULL ); - char * newpath = tr_buildPath( location, tor->info.files[i].name, NULL ); - + const tr_file * f = &tor->info.files[i]; + char * oldpath = tr_buildPath( tor->downloadDir, f->name, NULL ); + char * newpath = tr_buildPath( location, f->name, NULL ); - if( do_move && !stat( oldpath, &sb ) ) + if( do_move ) { - tr_moveFile( oldpath, newpath ); + errno = 0; tr_torinf( tor, "moving \"%s\" to \"%s\"", oldpath, newpath ); + if( tr_moveFile( oldpath, newpath ) ) { + err = 1; + tr_torerr( tor, "error moving \"%s\" to \"%s\": %s", + oldpath, newpath, tr_strerror( errno ) ); + } } else if( !stat( newpath, &sb ) ) { tr_torinf( tor, "found \"%s\"", newpath ); } + if( data->setme_progress ) + { + bytesHandled += f->length; + *data->setme_progress = bytesHandled / tor->info.totalSize; + } + tr_free( newpath ); tr_free( oldpath ); } - /* blow away the leftover subdirectories in the old location */ - tr_torrentDeleteLocalData( tor, unlink ); + if( !err ) + { + /* blow away the leftover subdirectories in the old location */ + tr_torrentDeleteLocalData( tor, unlink ); - /* set the new location and reverify */ - tr_torrentSetDownloadDir( tor, location ); - tr_torrentVerify( tor ); + /* set the new location and reverify */ + tr_torrentSetDownloadDir( tor, location ); + tr_torrentVerify( tor ); + } } - if( data->setme_done ) - *data->setme_done = TRUE; + if( data->setme_state ) + *data->setme_state = err ? TR_LOC_ERROR : TR_LOC_DONE; /* cleanup */ tr_free( data->location ); @@ -2351,24 +2368,28 @@ setLocation( void * vdata ) } void -tr_torrentSetLocation( tr_torrent * tor, - const char * location, - tr_bool move_from_old_location, - tr_bool * setme_done ) +tr_torrentSetLocation( tr_torrent * tor, + const char * location, + tr_bool move_from_old_location, + double * setme_progress, + int * setme_state ) { struct LocationData * data; assert( tr_isTorrent( tor ) ); - if( setme_done ) - *setme_done = FALSE; + if( setme_state ) + *setme_state = TR_LOC_MOVING; + if( setme_progress ) + *setme_progress = 0; /* run this in the libtransmission thread */ data = tr_new( struct LocationData, 1 ); data->tor = tor; data->location = tr_strdup( location ); data->move_from_old_location = move_from_old_location; - data->setme_done = setme_done; + data->setme_state = setme_state; + data->setme_progress = setme_progress; tr_runInEventThread( tor->session, setLocation, data ); } diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 0cb6bf27e..d6dbb301e 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -937,11 +937,19 @@ void tr_torrentStop( tr_torrent * torrent ); typedef int tr_fileFunc( const char * filename ); +enum +{ + TR_LOC_MOVING, + TR_LOC_DONE, + TR_LOC_ERROR +}; + /** @brief Tell transmsision where to find this torrent's local data */ void tr_torrentSetLocation( tr_torrent * torrent, const char * location, tr_bool move_from_previous_location, - tr_bool * setme_done ); + double * setme_progress, + int * setme_state ); /** * @brief Deletes the torrent's local data.