]> granicus.if.org Git - transmission/commitdiff
(trunk) #920 Add "Move Data" to libT so all clients can use it
authorCharles Kerr <charles@transmissionbt.com>
Wed, 20 May 2009 16:02:12 +0000 (16:02 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Wed, 20 May 2009 16:02:12 +0000 (16:02 +0000)
gtk/relocate.c
libtransmission/rpcimpl.c
libtransmission/torrent.c
libtransmission/transmission.h

index 6b128cc69dca7ee232ded38f43887af9f1528417..261aa1ef6c3c19a0fe0d1541c0211f3c64c676d9 100644 (file)
@@ -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 */
index fc39309d2526d35840377019760cf810789c6c16..4ce251a017b7b043f50939f1f74184b04ed1edb4 100644 (file)
@@ -794,7 +794,7 @@ torrentSetLocation( tr_session               * session,
         for( i=0; i<torrentCount; ++i )
         {
             tr_torrent * tor = torrents[i];
-            tr_torrentSetLocation( tor, location, move, NULL );
+            tr_torrentSetLocation( tor, location, move, NULL, NULL );
             notify( session, TR_RPC_TORRENT_CHANGED, tor );
         }
 
index a48b184d7966d59f97899ea3e09465b0aa8747b5..758951c881376a7e2ffd801512f0ac8e0b79cae8 100644 (file)
@@ -2280,7 +2280,8 @@ tr_torrentDeleteLocalData( tr_torrent * tor, tr_fileFunc fileFunc )
 struct LocationData
 {
     tr_bool move_from_old_location;
-    tr_bool * setme_done;
+    int * setme_state;
+    double * setme_progress;
     char * location;
     tr_torrent * tor;
 };
@@ -2288,10 +2289,12 @@ struct LocationData
 static void
 setLocation( void * vdata )
 {
+    int err = 0;
     struct LocationData * data = vdata;
     tr_torrent * tor = data->tor;
     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; i<tor->info.fileCount; ++i )
+        for( i=0; !err && i<tor->info.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 );
 }
 
index 0cb6bf27e2e800ed906c5f1005c4273cc687f3ef..d6dbb301e1a6f8b5b03f1baaaa8b02f04884f4cf 100644 (file)
@@ -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.