[gnome-software] Add gs_utils_rmtree convenience function



commit 16d8bdbb1986fd98f55be2e0ec989ba45377308c
Author: Joaquim Rocha <jrocha endlessm com>
Date:   Mon Jun 13 14:41:05 2016 +0200

    Add gs_utils_rmtree convenience function
    
    The implementation code for the function has been taken from the
    previously existent gs_test_rmtree.

 src/gs-self-test.c |   46 +-----------------------------------------
 src/gs-utils.c     |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gs-utils.h     |    3 +-
 3 files changed, 59 insertions(+), 46 deletions(-)
---
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index f31947c..04aa555 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -50,50 +50,6 @@ gs_test_get_filename (const gchar *filename)
        return g_strdup (full_tmp);
 }
 
-/**
- * gs_test_rmtree:
- **/
-static gboolean
-gs_test_rmtree (const gchar *directory, GError **error)
-{
-       const gchar *filename;
-       g_autoptr(GDir) dir = NULL;
-
-       /* try to open */
-       dir = g_dir_open (directory, 0, error);
-       if (dir == NULL)
-               return FALSE;
-
-       /* find each */
-       while ((filename = g_dir_read_name (dir))) {
-               g_autofree gchar *src = NULL;
-               src = g_build_filename (directory, filename, NULL);
-               if (g_file_test (src, G_FILE_TEST_IS_DIR) &&
-                   !g_file_test (src, G_FILE_TEST_IS_SYMLINK)) {
-                       if (!gs_test_rmtree (src, error))
-                               return FALSE;
-               } else {
-                       g_debug ("deleting %s", src);
-                       if (g_unlink (src) != 0) {
-                               g_set_error (error,
-                                            GS_PLUGIN_ERROR,
-                                            GS_PLUGIN_ERROR_FAILED,
-                                            "Failed to delete: %s", src);
-                               return FALSE;
-                       }
-               }
-       }
-       g_debug ("removing empty %s", directory);
-       if (g_rmdir (directory) != 0) {
-               g_set_error (error,
-                            GS_PLUGIN_ERROR,
-                            GS_PLUGIN_ERROR_FAILED,
-                            "Failed to remove: %s", directory);
-               return FALSE;
-       }
-       return TRUE;
-}
-
 static gboolean
 gs_app_list_filter_cb (GsApp *app, gpointer user_data)
 {
@@ -963,7 +919,7 @@ main (int argc, char **argv)
 
        /* ensure test root does not exist */
        if (g_file_test (tmp_root, G_FILE_TEST_EXISTS)) {
-               ret = gs_test_rmtree (tmp_root, &error);
+               ret = gs_utils_rmtree (tmp_root, &error);
                g_assert_no_error (error);
                g_assert (ret);
                g_assert (!g_file_test (tmp_root, G_FILE_TEST_EXISTS));
diff --git a/src/gs-utils.c b/src/gs-utils.c
index 08430f5..bf85654 100644
--- a/src/gs-utils.c
+++ b/src/gs-utils.c
@@ -357,4 +357,60 @@ gs_utils_unlink (const gchar *filename, GError **error)
        return TRUE;
 }
 
+static gboolean
+gs_utils_rmtree_real (const gchar *directory, GError **error)
+{
+       const gchar *filename;
+       g_autoptr(GDir) dir = NULL;
+
+       /* try to open */
+       dir = g_dir_open (directory, 0, error);
+       if (dir == NULL)
+               return FALSE;
+
+       /* find each */
+       while ((filename = g_dir_read_name (dir))) {
+               g_autofree gchar *src = NULL;
+               src = g_build_filename (directory, filename, NULL);
+               if (g_file_test (src, G_FILE_TEST_IS_DIR) &&
+                   !g_file_test (src, G_FILE_TEST_IS_SYMLINK)) {
+                       if (!gs_utils_rmtree_real (src, error))
+                               return FALSE;
+               } else {
+                       if (g_unlink (src) != 0) {
+                               g_set_error (error,
+                                            GS_PLUGIN_ERROR,
+                                            GS_PLUGIN_ERROR_FAILED,
+                                            "Failed to delete: %s", src);
+                               return FALSE;
+                       }
+               }
+       }
+
+       if (g_rmdir (directory) != 0) {
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_FAILED,
+                            "Failed to remove: %s", directory);
+               return FALSE;
+       }
+       return TRUE;
+}
+
+/**
+ * gs_utils_rmtree:
+ * @directory: A full directory pathname to delete
+ * @error: A #GError, or %NULL
+ *
+ * Deletes a directory from disk and all its contents.
+ *
+ * Returns: %TRUE for success
+ **/
+gboolean
+gs_utils_rmtree (const gchar *directory, GError **error)
+{
+       g_debug ("recursively removing directory '%s'", directory);
+       return gs_utils_rmtree_real (directory, error);
+}
+
 /* vim: set noexpandtab: */
diff --git a/src/gs-utils.h b/src/gs-utils.h
index 8fd1f3d..884ab91 100644
--- a/src/gs-utils.h
+++ b/src/gs-utils.h
@@ -63,7 +63,8 @@ GPermission   *gs_utils_get_permission        (const gchar    *id);
 gboolean        gs_utils_strv_fnmatch          (gchar          **strv,
                                                 const gchar    *str);
 GDesktopAppInfo *gs_utils_get_desktop_app_info (const gchar    *id);
-
+gboolean        gs_utils_rmtree                (const gchar    *directory,
+                                                GError         **error);
 G_END_DECLS
 
 #endif /* __GS_UTILS_H */


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