gvfs r2133 - in trunk: . daemon daemon/trashlib



Author: ryanl
Date: Fri Dec 12 07:42:32 2008
New Revision: 2133
URL: http://svn.gnome.org/viewvc/gvfs?rev=2133&view=rev

Log:
2008-12-11  Ryan Lortie  <desrt desrt ca>

        Implement pull support on trash backend.

        * daemon/trashlib/trashitem.[ch]: add support for restoring items
        * daemon/gvfsbackendtrash.c: implement pull



Modified:
   trunk/ChangeLog
   trunk/daemon/gvfsbackendtrash.c
   trunk/daemon/trashlib/trashitem.c
   trunk/daemon/trashlib/trashitem.h

Modified: trunk/daemon/gvfsbackendtrash.c
==============================================================================
--- trunk/daemon/gvfsbackendtrash.c	(original)
+++ trunk/daemon/gvfsbackendtrash.c	Fri Dec 12 07:42:32 2008
@@ -367,6 +367,70 @@
   return TRUE;
 }
 
+static gboolean
+trash_backend_pull (GVfsBackend           *vfs_backend,
+                    GVfsJobPull           *job,
+                    const gchar           *source,
+                    const gchar           *local_path,
+                    GFileCopyFlags         flags,
+                    gboolean               remove_source,
+                    GFileProgressCallback  progress_callback,
+                    gpointer               progress_callback_data)
+{
+  GVfsBackendTrash *backend = G_VFS_BACKEND_TRASH (vfs_backend);
+  GError *error = NULL;
+
+  if (source[1] == '\0')
+    g_set_error (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+                 _("Can't pull trash"));
+  else
+    {
+      gboolean is_toplevel;
+      TrashItem *item;
+      GFile *real;
+
+      real = trash_backend_get_file (backend, source, &item,
+                                     &is_toplevel, &error);
+
+      if (real)
+        {
+          if (remove_source && !is_toplevel)
+            g_set_error (&error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED,
+                         _("Items in the trash may not be modified"));
+
+          else
+            {
+              GFile *destination;
+              gboolean it_worked;
+
+              destination = g_file_new_for_path (local_path);
+
+              if (remove_source)
+                it_worked = trash_item_restore (item, destination, &error);
+              else
+                it_worked = g_file_copy (real, destination, flags,
+                                         NULL, NULL, NULL, &error);
+
+              g_object_unref (destination);
+
+              if (it_worked)
+                {
+                  g_vfs_job_succeeded (G_VFS_JOB (job));
+
+                  return TRUE;
+                }
+            }
+
+          trash_item_unref (item);
+        }
+ 
+    }
+
+  g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
+  
+  return TRUE;
+}
+
 static void
 trash_backend_add_info (TrashItem *item,
                         GFileInfo *info,
@@ -717,6 +781,7 @@
   backend_class->try_query_fs_info = trash_backend_query_fs_info;
   backend_class->try_enumerate = trash_backend_enumerate;
   backend_class->try_delete = trash_backend_delete;
+  backend_class->try_pull = trash_backend_pull;
   backend_class->try_create_dir_monitor = trash_backend_create_dir_monitor;
   backend_class->try_create_file_monitor = trash_backend_create_file_monitor;
 }

Modified: trunk/daemon/trashlib/trashitem.c
==============================================================================
--- trunk/daemon/trashlib/trashitem.c	(original)
+++ trunk/daemon/trashlib/trashitem.c	Fri Dec 12 07:42:32 2008
@@ -479,30 +479,9 @@
       g_sprintf (buffer, "%u", unique + i);
       temp_name = g_file_get_child (expunged, buffer);
 
-      if (g_file_move (item->file, temp_name,
-                       G_FILE_COPY_OVERWRITE |
-                       G_FILE_COPY_NOFOLLOW_SYMLINKS |
-                       G_FILE_COPY_NO_FALLBACK_FOR_MOVE,
-                       NULL, NULL, NULL, NULL))
+      /* "restore" the item into the expunged folder */
+      if (trash_item_restore (item, temp_name, NULL))
         {
-          g_static_rw_lock_writer_lock (&item->root->lock);
-          g_hash_table_remove (item->root->item_table, item->escaped_name);
-          g_static_rw_lock_writer_unlock (&item->root->lock);
-
-          {
-            GFile *trashinfo;
-            gchar *basename;
-            gchar *relname;
-
-            basename = g_file_get_basename (item->file);
-            relname = g_strdup_printf ("../../info/%s.trashinfo", basename);
-            trashinfo = g_file_resolve_relative_path (item->file, relname);
-            g_free (basename);
-            g_free (relname);
-
-            g_file_delete (trashinfo, NULL, NULL);
-          }
-
           trash_expunge (expunged);
           success = TRUE;
         }
@@ -520,3 +499,38 @@
 
   return success;
 }
+
+gboolean
+trash_item_restore (TrashItem  *item,
+                    GFile      *dest,
+                    GError    **error)
+{
+  if (g_file_move (item->file, dest,
+                   G_FILE_COPY_OVERWRITE |
+                   G_FILE_COPY_NOFOLLOW_SYMLINKS |
+                   G_FILE_COPY_NO_FALLBACK_FOR_MOVE,
+                   NULL, NULL, NULL, NULL))
+    {
+      g_static_rw_lock_writer_lock (&item->root->lock);
+      g_hash_table_remove (item->root->item_table, item->escaped_name);
+      g_static_rw_lock_writer_unlock (&item->root->lock);
+
+      {
+        GFile *trashinfo;
+        gchar *basename;
+        gchar *relname;
+
+        basename = g_file_get_basename (item->file);
+        relname = g_strdup_printf ("../../info/%s.trashinfo", basename);
+        trashinfo = g_file_resolve_relative_path (item->file, relname);
+        g_free (basename);
+        g_free (relname);
+
+        g_file_delete (trashinfo, NULL, NULL);
+      }
+
+      return TRUE;
+    }
+
+  return FALSE;
+}

Modified: trunk/daemon/trashlib/trashitem.h
==============================================================================
--- trunk/daemon/trashlib/trashitem.h	(original)
+++ trunk/daemon/trashlib/trashitem.h	Fri Dec 12 07:42:32 2008
@@ -52,5 +52,8 @@
 /* delete a trash item (safe while holding a reference to it) */
 gboolean        trash_item_delete            (TrashItem          *item,
                                               GError            **error);
+gboolean        trash_item_restore           (TrashItem          *item,
+                                              GFile              *dest,
+                                              GError            **error);
 
 #endif /* _trashitem_h_ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]