From 1735d88f011e174a96aa2e6b345d2e8cf762ccc9 Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Wed, 13 May 2020 13:06:53 -0500 Subject: [PATCH] Clean up duplicated instance collection code It was completely identical in flatpak-run.c and flatpak-instance.c. --- common/flatpak-instance-private.h | 2 ++ common/flatpak-instance.c | 46 ++++++++++++++++++------------- common/flatpak-run.c | 42 ++-------------------------- 3 files changed, 31 insertions(+), 59 deletions(-) diff --git a/common/flatpak-instance-private.h b/common/flatpak-instance-private.h index bd2998d5..61a334a7 100644 --- a/common/flatpak-instance-private.h +++ b/common/flatpak-instance-private.h @@ -26,4 +26,6 @@ FlatpakInstance *flatpak_instance_new (const char *dir); FlatpakInstance *flatpak_instance_new_for_id (const char *id); +void flatpak_instance_iterate_all_and_gc (GPtrArray *out_instances); + #endif /* __FLATPAK_INSTANCE_PRIVATE_H__ */ diff --git a/common/flatpak-instance.c b/common/flatpak-instance.c index 75e89b82..7281212b 100644 --- a/common/flatpak-instance.c +++ b/common/flatpak-instance.c @@ -429,29 +429,17 @@ flatpak_instance_new_for_id (const char *id) return flatpak_instance_new (dir); } -/** - * flatpak_instance_get_all: - * - * Gets FlatpakInstance objects for all running sandboxes in the current session. - * - * Returns: (transfer full) (element-type FlatpakInstance): a #GPtrArray of - * #FlatpakInstance objects - * - * Since: 1.1 - */ -GPtrArray * -flatpak_instance_get_all (void) +void +flatpak_instance_iterate_all_and_gc (GPtrArray *out_instances) { - g_autoptr(GPtrArray) instances = NULL; - g_autofree char *base_dir = NULL; + + g_autofree char *base_dir = g_build_filename (g_get_user_runtime_dir (), ".flatpak", NULL); g_auto(GLnxDirFdIterator) iter = { 0 }; struct dirent *dent; - instances = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); - base_dir = g_build_filename (g_get_user_runtime_dir (), ".flatpak", NULL); - + /* Clean up unused instances */ if (!glnx_dirfd_iterator_init_at (AT_FDCWD, base_dir, FALSE, &iter, NULL)) - return g_steal_pointer (&instances); + return; while (TRUE) { @@ -485,9 +473,29 @@ flatpak_instance_get_all (void) continue; } - g_ptr_array_add (instances, flatpak_instance_new_for_id (dent->d_name)); + if (out_instances != NULL) + g_ptr_array_add (out_instances, flatpak_instance_new_for_id (dent->d_name)); } } +} + +/** + * flatpak_instance_get_all: + * + * Gets FlatpakInstance objects for all running sandboxes in the current session. + * + * Returns: (transfer full) (element-type FlatpakInstance): a #GPtrArray of + * #FlatpakInstance objects + * + * Since: 1.1 + */ +GPtrArray * +flatpak_instance_get_all (void) +{ + g_autoptr(GPtrArray) instances = NULL; + + instances = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + flatpak_instance_iterate_all_and_gc (instances); return g_steal_pointer (&instances); } diff --git a/common/flatpak-run.c b/common/flatpak-run.c index ef51ee3f..2951f2f0 100644 --- a/common/flatpak-run.c +++ b/common/flatpak-run.c @@ -58,6 +58,7 @@ #include "flatpak-proxy.h" #include "flatpak-utils-base-private.h" #include "flatpak-dir-private.h" +#include "flatpak-instance-private.h" #include "flatpak-systemd-dbus-generated.h" #include "flatpak-document-dbus-generated.h" #include "flatpak-error.h" @@ -1892,46 +1893,7 @@ flatpak_app_compute_permissions (GKeyFile *app_metadata, static void flatpak_run_gc_ids (void) { - g_autofree char *base_dir = g_build_filename (g_get_user_runtime_dir (), ".flatpak", NULL); - g_auto(GLnxDirFdIterator) iter = { 0 }; - struct dirent *dent; - - /* Clean up unused instances */ - if (!glnx_dirfd_iterator_init_at (AT_FDCWD, base_dir, FALSE, &iter, NULL)) - return; - - while (TRUE) - { - if (!glnx_dirfd_iterator_next_dent_ensure_dtype (&iter, &dent, NULL, NULL)) - break; - - if (dent == NULL) - break; - - if (dent->d_type == DT_DIR) - { - g_autofree char *ref_file = g_strconcat (dent->d_name, "/.ref", NULL); - struct stat statbuf; - struct flock l = { - .l_type = F_WRLCK, - .l_whence = SEEK_SET, - .l_start = 0, - .l_len = 0 - }; - glnx_autofd int lock_fd = openat (iter.fd, ref_file, O_RDWR | O_CLOEXEC); - if (lock_fd != -1 && - fstat (lock_fd, &statbuf) == 0 && - /* Only gc if created at least 3 secs ago, to work around race mentioned in flatpak_run_allocate_id() */ - statbuf.st_mtime + 3 < time (NULL) && - fcntl (lock_fd, F_GETLK, &l) == 0 && - l.l_type == F_UNLCK) - { - /* The instance is not used, remove it */ - g_debug ("Cleaning up unused container id %s", dent->d_name); - glnx_shutil_rm_rf_at (iter.fd, dent->d_name, NULL, NULL); - } - } - } + flatpak_instance_iterate_all_and_gc (NULL); } static char *