Clean up duplicated instance collection code

It was completely identical in flatpak-run.c and flatpak-instance.c.
This commit is contained in:
Ryan Gonzalez
2020-05-13 13:06:53 -05:00
committed by Alexander Larsson
parent ea1830e108
commit 1735d88f01
3 changed files with 31 additions and 59 deletions

View File

@@ -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__ */

View File

@@ -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);
}

View File

@@ -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 *