From 6cdbf400b2e4e02c5ab9c395517aef6a3a1fea26 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Tue, 15 Dec 2015 14:36:17 +0100 Subject: [PATCH] lib: Add xdg_app_dir_undeploy_all and xdg_app_dir_remove_ref helpers --- app/xdg-app-builtins-uninstall.c | 111 ++----------------------------- common/xdg-app-dir.c | 79 ++++++++++++++++++++++ common/xdg-app-dir.h | 11 +++ 3 files changed, 94 insertions(+), 107 deletions(-) diff --git a/app/xdg-app-builtins-uninstall.c b/app/xdg-app-builtins-uninstall.c index cfe20bbd..46f096a7 100644 --- a/app/xdg-app-builtins-uninstall.c +++ b/app/xdg-app-builtins-uninstall.c @@ -47,18 +47,11 @@ xdg_app_builtin_uninstall_runtime (int argc, char **argv, GCancellable *cancella { g_autoptr(GOptionContext) context = NULL; g_autoptr(XdgAppDir) dir = NULL; - g_autoptr(GFile) deploy_base = NULL; - g_autoptr(GFile) arch_dir = NULL; - g_autoptr(GFile) top_dir = NULL; - g_autoptr(OstreeRepo) repo = NULL; const char *name; const char *arch; const char *branch; g_autofree char *ref = NULL; g_autofree char *repository = NULL; - g_auto(GStrv) deployed = NULL; - int i; - GError *temp_error = NULL; gboolean was_deployed; context = g_option_context_new ("RUNTIME [BRANCH] - Uninstall a runtime"); @@ -95,55 +88,12 @@ xdg_app_builtin_uninstall_runtime (int argc, char **argv, GCancellable *cancella if (!xdg_app_dir_set_active (dir, ref, NULL, cancellable, error)) return FALSE; - if (!xdg_app_dir_list_deployed (dir, ref, &deployed, cancellable, error)) + if (!xdg_app_dir_undeploy_all (dir, ref, opt_force_remove, &was_deployed, cancellable, error)) return FALSE; - for (i = 0; deployed[i] != NULL; i++) - { - g_debug ("undeploying %s", deployed[i]); - if (!xdg_app_dir_undeploy (dir, ref, deployed[i], opt_force_remove, cancellable, error)) - return FALSE; - } - - deploy_base = xdg_app_dir_get_deploy_dir (dir, ref); - was_deployed = g_file_query_exists (deploy_base, cancellable); - if (was_deployed) - { - g_debug ("removing deploy base"); - if (!gs_shutil_rm_rf (deploy_base, cancellable, error)) - return FALSE; - } - - g_debug ("cleaning up empty directories"); - arch_dir = g_file_get_parent (deploy_base); - if (g_file_query_exists (arch_dir, cancellable) && - !g_file_delete (arch_dir, cancellable, &temp_error)) - { - if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY)) - { - g_propagate_error (error, temp_error); - return FALSE; - } - g_clear_error (&temp_error); - } - - top_dir = g_file_get_parent (arch_dir); - if (g_file_query_exists (top_dir, cancellable) && - !g_file_delete (top_dir, cancellable, &temp_error)) - { - if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY)) - { - g_propagate_error (error, temp_error); - return FALSE; - } - g_clear_error (&temp_error); - } - if (!opt_keep_ref) { - repo = xdg_app_dir_get_repo (dir); - - if (!ostree_repo_set_ref_immediate (repo, repository, ref, NULL, cancellable, error)) + if (!xdg_app_dir_remove_ref (dir, repository, ref, cancellable, error)) return FALSE; if (!xdg_app_dir_prune (dir, cancellable, error)) @@ -163,19 +113,12 @@ xdg_app_builtin_uninstall_app (int argc, char **argv, GCancellable *cancellable, { g_autoptr(GOptionContext) context = NULL; g_autoptr(XdgAppDir) dir = NULL; - g_autoptr(GFile) deploy_base = NULL; - g_autoptr(GFile) arch_dir = NULL; - g_autoptr(GFile) top_dir = NULL; - g_autoptr(OstreeRepo) repo = NULL; const char *name; const char *arch; const char *branch; g_autofree char *ref = NULL; g_autofree char *repository = NULL; g_autofree char *current_ref = NULL; - g_auto(GStrv) deployed = NULL; - int i; - GError *temp_error = NULL; gboolean was_deployed; context = g_option_context_new ("APP [BRANCH] - Uninstall an application"); @@ -218,58 +161,12 @@ xdg_app_builtin_uninstall_app (int argc, char **argv, GCancellable *cancellable, return FALSE; } - if (!xdg_app_dir_list_deployed (dir, ref, &deployed, cancellable, error)) + if (!xdg_app_dir_undeploy_all (dir, ref, opt_force_remove, &was_deployed, cancellable, error)) return FALSE; - for (i = 0; deployed[i] != NULL; i++) - { - g_debug ("undeploying %s", deployed[i]); - if (!xdg_app_dir_undeploy (dir, ref, deployed[i], opt_force_remove, cancellable, error)) - return FALSE; - } - - deploy_base = xdg_app_dir_get_deploy_dir (dir, ref); - was_deployed = g_file_query_exists (deploy_base, cancellable); - if (was_deployed) - { - g_debug ("removing deploy base"); - if (!gs_shutil_rm_rf (deploy_base, cancellable, error)) - return FALSE; - } - - if (!xdg_app_dir_update_exports (dir, name, cancellable, error)) - return FALSE; - - g_debug ("cleaning up empty directories"); - arch_dir = g_file_get_parent (deploy_base); - if (g_file_query_exists (arch_dir, cancellable) && - !g_file_delete (arch_dir, cancellable, &temp_error)) - { - if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY)) - { - g_propagate_error (error, temp_error); - return FALSE; - } - g_clear_error (&temp_error); - } - - top_dir = g_file_get_parent (arch_dir); - if (g_file_query_exists (top_dir, cancellable) && - !g_file_delete (top_dir, cancellable, &temp_error)) - { - if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY)) - { - g_propagate_error (error, temp_error); - return FALSE; - } - g_clear_error (&temp_error); - } - if (!opt_keep_ref) { - repo = xdg_app_dir_get_repo (dir); - - if (!ostree_repo_set_ref_immediate (repo, repository, ref, NULL, cancellable, error)) + if (!xdg_app_dir_remove_ref (dir, repository, ref, cancellable, error)) return FALSE; if (!xdg_app_dir_prune (dir, cancellable, error)) diff --git a/common/xdg-app-dir.c b/common/xdg-app-dir.c index 725c33e4..7a72cc8c 100644 --- a/common/xdg-app-dir.c +++ b/common/xdg-app-dir.c @@ -1881,6 +1881,85 @@ xdg_app_dir_undeploy (XdgAppDir *self, return ret; } +gboolean +xdg_app_dir_undeploy_all (XdgAppDir *self, + const char *ref, + gboolean force_remove, + gboolean *was_deployed_out, + GCancellable *cancellable, + GError **error) +{ + g_auto(GStrv) deployed = NULL; + g_autoptr(GFile) deploy_base = NULL; + g_autoptr(GFile) arch_dir = NULL; + g_autoptr(GFile) top_dir = NULL; + GError *temp_error = NULL; + int i; + gboolean was_deployed; + + if (!xdg_app_dir_list_deployed (self, ref, &deployed, cancellable, error)) + return FALSE; + + for (i = 0; deployed[i] != NULL; i++) + { + g_debug ("undeploying %s", deployed[i]); + if (!xdg_app_dir_undeploy (self, ref, deployed[i], force_remove, cancellable, error)) + return FALSE; + } + + deploy_base = xdg_app_dir_get_deploy_dir (self, ref); + was_deployed = g_file_query_exists (deploy_base, cancellable); + if (was_deployed) + { + g_debug ("removing deploy base"); + if (!gs_shutil_rm_rf (deploy_base, cancellable, error)) + return FALSE; + } + + g_debug ("cleaning up empty directories"); + arch_dir = g_file_get_parent (deploy_base); + if (g_file_query_exists (arch_dir, cancellable) && + !g_file_delete (arch_dir, cancellable, &temp_error)) + { + if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY)) + { + g_propagate_error (error, temp_error); + return FALSE; + } + g_clear_error (&temp_error); + } + + top_dir = g_file_get_parent (arch_dir); + if (g_file_query_exists (top_dir, cancellable) && + !g_file_delete (top_dir, cancellable, &temp_error)) + { + if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY)) + { + g_propagate_error (error, temp_error); + return FALSE; + } + g_clear_error (&temp_error); + } + + if (was_deployed_out) + *was_deployed_out = was_deployed; + + return TRUE; +} + +gboolean +xdg_app_dir_remove_ref (XdgAppDir *self, + const char *remote_name, + const char *ref, + GCancellable *cancellable, + GError **error) +{ + if (!ostree_repo_set_ref_immediate (self->repo, remote_name, ref, NULL, cancellable, error)) + return FALSE; + + return TRUE; +} + gboolean xdg_app_dir_cleanup_removed (XdgAppDir *self, GCancellable *cancellable, diff --git a/common/xdg-app-dir.h b/common/xdg-app-dir.h index 1feb69f5..07244b3f 100644 --- a/common/xdg-app-dir.h +++ b/common/xdg-app-dir.h @@ -161,6 +161,17 @@ gboolean xdg_app_dir_undeploy (XdgAppDir *self, gboolean force_remove, GCancellable *cancellable, GError **error); +gboolean xdg_app_dir_undeploy_all (XdgAppDir *self, + const char *ref, + gboolean force_remove, + gboolean *was_deployed_out, + GCancellable *cancellable, + GError **error); +gboolean xdg_app_dir_remove_ref (XdgAppDir *self, + const char *remote_name, + const char *ref, + GCancellable *cancellable, + GError **error); gboolean xdg_app_dir_update_exports (XdgAppDir *self, const char *app, GCancellable *cancellable,