[epiphany] file-helpers: Fix ephy_file_delete_dir_recursively when an error is given



commit 331e681810d9b0af3ed66318f6c11e52e069616e
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Mon Dec 1 11:17:12 2014 +0100

    file-helpers: Fix ephy_file_delete_dir_recursively when an error is given
    
    The implementation is checking the given error pointer to check if an
    error ocurred, instead of checking the pointer pointed by the passed in
    error. This means that it is always failing when an error is passed, but
    it works in most of the cases when NULL is passed because the return value
    of functions is checked too. There's still a problem when
    g_file_enumerator_next_file() returns NULL, because in case of error we
    are considering it as end of enumeration. We can simplify the
    implementation and the caller code, by using the stdio API instead of
    GIO, since we know we are dealing with local files anyway and we are
    using the synchronous API.

 lib/ephy-file-helpers.c        |   92 ++++++++++++++++++++-------------------
 lib/ephy-file-helpers.h        |    2 +-
 lib/ephy-web-app-utils.c       |    7 +--
 tests/ephy-file-helpers-test.c |   17 ++-----
 4 files changed, 55 insertions(+), 63 deletions(-)
---
diff --git a/lib/ephy-file-helpers.c b/lib/ephy-file-helpers.c
index b06227d..baaa6a5 100644
--- a/lib/ephy-file-helpers.c
+++ b/lib/ephy-file-helpers.c
@@ -30,11 +30,13 @@
 #include "ephy-settings.h"
 #include "ephy-string.h"
 
+#include <errno.h>
 #include <gdk/gdk.h>
 #include <gio/gdesktopappinfo.h>
 #include <gio/gio.h>
 #include <glib.h>
 #include <glib/gi18n.h>
+#include <glib/gstdio.h>
 #include <gtk/gtk.h>
 #include <libxml/xmlreader.h>
 #include <stdlib.h>
@@ -429,15 +431,10 @@ ephy_file_helpers_shutdown (void)
        {
                if (!keep_directory)
                {
-                       GFile *tmp_dir_file;
-                       tmp_dir_file = g_file_new_for_path (tmp_dir);
-
                        /* recursively delete the contents and the
                         * directory */
                        LOG ("shutdown: delete tmp_dir %s", tmp_dir);
-                       ephy_file_delete_dir_recursively (tmp_dir_file,
-                                                         NULL);
-                       g_object_unref (tmp_dir_file);
+                       ephy_file_delete_dir_recursively (tmp_dir, NULL);
                }
 
                g_free (tmp_dir);
@@ -878,56 +875,61 @@ ephy_file_browse_to (GFile *file,
  * Returns: %TRUE if delete succeeded
  **/
 gboolean
-ephy_file_delete_dir_recursively (GFile *directory, GError **error)
+ephy_file_delete_dir_recursively (const char *directory, GError **error)
 {
-       GFileEnumerator *children = NULL;
-       GFileInfo *info;
-       gboolean ret = FALSE;
-
-       children = g_file_enumerate_children (directory,
-                                             "standard::name,standard::type",
-                                             0, NULL, error);
-       if (children == NULL || error)
-               goto out;
-
-       info = g_file_enumerator_next_file (children, NULL, error);
-       while (info || error) {
-               GFile *child;
-               const char *name;
-               GFileType type;
-
-               if (error)
-                       goto out;
+       GDir* dir;
+       const char* file_name;
+       gboolean failed = FALSE;
 
-               name = g_file_info_get_name (info);
-               child = g_file_get_child (directory, name);
-               type = g_file_info_get_file_type (info);
+       dir = g_dir_open (directory, 0, error);
+       if (!dir)
+               return FALSE;
 
-               LOG ("ephy-file-delete-dir: delete child %s", name);
-               if (type == G_FILE_TYPE_DIRECTORY)
-                       ret = ephy_file_delete_dir_recursively (child, error);
-               else if (type == G_FILE_TYPE_REGULAR)
-                       ret = g_file_delete (child, NULL, error);
+       file_name = g_dir_read_name (dir);
+       while (file_name && !failed) {
+               char *file_path;
 
-               g_object_unref (info);
-               g_object_unref (child);
+               file_path = g_build_filename (directory, file_name, NULL);
+               if (g_file_test (file_path, G_FILE_TEST_IS_DIR))
+               {
+                       failed = !ephy_file_delete_dir_recursively (file_path, error);
+               }
+               else
+               {
+                       int result = g_unlink (file_path);
 
-               if (!ret)
-                       goto out;
+                       if (result == -1)
+                       {
+                               int errsv = errno;
 
-               info = g_file_enumerator_next_file (children, NULL, error);
+                               g_set_error (error, G_IO_ERROR,
+                                            g_io_error_from_errno (errsv),
+                                            "Error removing file %s: %s",
+                                            file_path, g_strerror (errsv));
+                               failed = TRUE;
+                       }
+               }
+               g_free (file_path);
        }
+       g_dir_close (dir);
 
-       ret = TRUE;
+       if (!failed)
+       {
+               int result = g_rmdir (directory);
 
-       LOG ("ephy-file-delete-dir: delete successful");
-       g_file_delete (directory, NULL, error);
+               if (result == -1)
+               {
+                       int errsv = errno;
 
-out:
-       if (children)
-               g_object_unref (children);
+                       g_set_error (error, G_IO_ERROR,
+                                    g_io_error_from_errno (errsv),
+                                    "Error removing directory %s: %s",
+                                    directory, g_strerror (errsv));
+                       failed = TRUE;
+               }
+       }
 
-       return ret;
+       return !failed;
 }
 
 /**
diff --git a/lib/ephy-file-helpers.h b/lib/ephy-file-helpers.h
index 6220b4e..5ffb5f2 100644
--- a/lib/ephy-file-helpers.h
+++ b/lib/ephy-file-helpers.h
@@ -86,7 +86,7 @@ gboolean           ephy_file_open_uri_in_default_browser    (const char
                                                              GdkScreen             *screen);
 gboolean           ephy_file_browse_to                      (GFile                 *file,
                                                              guint32                user_time);
-gboolean           ephy_file_delete_dir_recursively         (GFile                 *file,
+gboolean           ephy_file_delete_dir_recursively         (const char            *directory,
                                                              GError               **error);
 void               ephy_file_delete_uri                     (const char            *uri);
 gboolean           ephy_file_move_uri                       (const char            *source_uri,
diff --git a/lib/ephy-web-app-utils.c b/lib/ephy-web-app-utils.c
index 165e83f..aeac51d 100644
--- a/lib/ephy-web-app-utils.c
+++ b/lib/ephy-web-app-utils.c
@@ -120,7 +120,7 @@ ephy_web_application_delete (const char *name)
   char *profile_dir = NULL;
   char *desktop_file = NULL, *desktop_path = NULL;
   char *wm_class;
-  GFile *profile = NULL, *launcher = NULL;
+  GFile *launcher = NULL;
   gboolean return_value = FALSE;
 
   g_return_val_if_fail (name, FALSE);
@@ -136,8 +136,7 @@ ephy_web_application_delete (const char *name)
     goto out;
   }
 
-  profile = g_file_new_for_path (profile_dir);
-  if (!ephy_file_delete_dir_recursively (profile, NULL))
+  if (!ephy_file_delete_dir_recursively (profile_dir, NULL))
     goto out;
   LOG ("Deleted application profile.\n");
 
@@ -156,8 +155,6 @@ ephy_web_application_delete (const char *name)
 
 out:
 
-  if (profile)
-    g_object_unref (profile);
   g_free (profile_dir);
 
   if (launcher)
diff --git a/tests/ephy-file-helpers-test.c b/tests/ephy-file-helpers-test.c
index c33c1da..43d0009 100644
--- a/tests/ephy-file-helpers-test.c
+++ b/tests/ephy-file-helpers-test.c
@@ -89,17 +89,12 @@ test_ephy_file_helpers_init (void)
 
     /* Cleanup dir left behind. */
     if (keep_dir) {
-      GFile *file;
-
-      file = g_file_new_for_path (tmp_dir);
       /* As a safety measure, only try recursive delete on paths
        * prefixed with /tmp. */
       if (g_str_has_prefix (tmp_dir, "/tmp"))
-        g_assert (ephy_file_delete_dir_recursively (file, NULL));
+        g_assert (ephy_file_delete_dir_recursively (tmp_dir, NULL));
       else
         g_warning ("INIT: dangerous path returned as tmp_dir: %s", tmp_dir);
-
-      g_object_unref (file);
     }
 
     g_free (tmp_dir);
@@ -184,8 +179,8 @@ test_ephy_file_create_delete_dir (void)
   ephy_file_helpers_init (NULL, EPHY_FILE_HELPERS_PRIVATE_PROFILE, NULL);
 
   for (i = 0; i < G_N_ELEMENTS (dir_tests); i++) {
-    GFile *file = NULL;
     DirTest test;
+    GError *error = NULL;
 
     test = dir_tests[i];
 
@@ -195,14 +190,12 @@ test_ephy_file_create_delete_dir (void)
     g_assert (ephy_ensure_dir_exists (test.dir, NULL) == (test.exists || test.can_create));
     g_assert (g_file_test (test.dir, G_FILE_TEST_EXISTS) == (test.exists || test.can_create));
 
-    file = g_file_new_for_path (test.dir);
-
-    g_assert (ephy_file_delete_dir_recursively (file, NULL) == test.can_delete);
+    g_assert (ephy_file_delete_dir_recursively (test.dir, &error) == test.can_delete);
+    if (error)
+            g_error_free (error);
 
     if (test.exists)
       g_assert (g_file_test (test.dir, G_FILE_TEST_EXISTS) != test.can_delete);
-
-    g_object_unref (file);
   }
 
   ephy_file_helpers_shutdown ();


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