Skip to content

Commit

Permalink
update: Make autopruned refs automatically removed
Browse files Browse the repository at this point in the history
In order to maintain a system over time update automatically removes any EOL runtimes that are unused.

This extends it to also remove any autopruned refs. In practice this means removing no longer used driver versions as the system is updated.

Closes #5261
  • Loading branch information
TingPing committed Dec 20, 2023
1 parent 65bc369 commit e1d072c
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/flatpak-builtins-uninstall.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ flatpak_builtin_uninstall (int argc, char **argv, GCancellable *cancellable, GEr

udir = uninstall_dir_ensure (uninstall_dirs, dir);

unused = flatpak_dir_list_unused_refs (dir, opt_arch, NULL, NULL, NULL, FALSE, cancellable, error);
unused = flatpak_dir_list_unused_refs (dir, opt_arch, NULL, NULL, NULL, FALSE, FALSE, cancellable, error);
if (unused == NULL)
return FALSE;

Expand Down
1 change: 1 addition & 0 deletions common/flatpak-dir-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ char ** flatpak_dir_list_unused_refs (Fla
GHashTable *eol_injection,
const char * const *refs_to_exclude,
gboolean filter_by_eol,
gboolean filter_by_autoprune,
GCancellable *cancellable,
GError **error);

Expand Down
49 changes: 34 additions & 15 deletions common/flatpak-dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -16491,6 +16491,7 @@ find_used_refs (FlatpakDir *self,
GHashTable *metadata_injection,
GHashTable *refs_to_exclude,
GHashTable *used_refs, /* This is filled in */
GHashTable *autopruned_refs, /* This is filled in */
GCancellable *cancellable,
GError **error)
{
Expand Down Expand Up @@ -16662,6 +16663,10 @@ find_used_refs (FlatpakDir *self,
flatpak_decomposed_get_ref (ref_to_analyze), dir_name);
queue_ref_for_analysis (rel->ref, arch, analyzed_refs, refs_to_analyze);
}
else
{
g_hash_table_add (autopruned_refs, flatpak_decomposed_ref (rel->ref));
}
}
}

Expand All @@ -16678,10 +16683,12 @@ flatpak_dir_list_unused_refs (FlatpakDir *self,
GHashTable *eol_injection,
const char * const *refs_to_exclude,
gboolean filter_by_eol,
gboolean filter_by_autoprune,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GHashTable) used_refs = NULL;
g_autoptr(GHashTable) autoprune_refs = NULL;
g_autoptr(GHashTable) excluded_refs_ht = NULL;
g_autoptr(GPtrArray) refs = NULL;
g_autoptr(GPtrArray) runtime_refs = NULL;
Expand All @@ -16700,12 +16707,14 @@ flatpak_dir_list_unused_refs (FlatpakDir *self,
}

used_refs = g_hash_table_new_full ((GHashFunc)flatpak_decomposed_hash, (GEqualFunc)flatpak_decomposed_equal, (GDestroyNotify)flatpak_decomposed_unref, NULL);
autoprune_refs = g_hash_table_new_full ((GHashFunc)flatpak_decomposed_hash, (GEqualFunc)flatpak_decomposed_equal, (GDestroyNotify)flatpak_decomposed_unref, NULL);

g_info ("Checking installation ‘%s’ %s",
g_info ("Checking installation ‘%s’ %s %s",
flatpak_dir_get_name_cached (self),
filter_by_eol ? "for EOL unused refs" : "for unused refs");
filter_by_eol ? "for EOL unused refs" : "for unused refs",
filter_by_autoprune ? "and autoprunes" : "");
if (!find_used_refs (self, NULL, arch, metadata_injection, excluded_refs_ht,
used_refs, cancellable, error))
used_refs, autoprune_refs, cancellable, error))
return NULL;

/* If @self is a system installation, also check the per-user installation
Expand All @@ -16722,7 +16731,7 @@ flatpak_dir_list_unused_refs (FlatpakDir *self,
g_info ("Checking installation ‘%s’ by checking for dependent refs in ‘%s’",
flatpak_dir_get_name_cached (self), flatpak_dir_get_name_cached (user_dir));
if (!find_used_refs (self, user_dir, arch, metadata_injection, excluded_refs_ht,
used_refs, cancellable, &local_error))
used_refs, autoprune_refs, cancellable, &local_error))
{
/* We may get permission denied if the process is sandboxed with
* systemd's ProtectHome=
Expand Down Expand Up @@ -16752,9 +16761,13 @@ flatpak_dir_list_unused_refs (FlatpakDir *self,
if (arch != NULL && !flatpak_decomposed_is_arch (ref, arch))
continue;

if (filter_by_eol)
{
if (filter_by_autoprune || filter_by_eol)
{
gboolean is_autopruned = FALSE;
gboolean is_eol = FALSE;
gboolean include = TRUE;

is_autopruned = g_hash_table_contains (autoprune_refs, ref);

if (eol_injection && g_hash_table_contains (eol_injection, flatpak_decomposed_get_ref (ref)))
{
Expand All @@ -16766,20 +16779,26 @@ flatpak_dir_list_unused_refs (FlatpakDir *self,

/* deploy v4 guarantees eol/eolr info */
deploy_data = flatpak_dir_get_deploy_data (self, ref, 4,
cancellable, NULL);
cancellable, NULL);
is_eol = deploy_data != NULL &&
(flatpak_deploy_data_get_eol (deploy_data) != NULL ||
flatpak_deploy_data_get_eol_rebase (deploy_data));
flatpak_deploy_data_get_eol_rebase (deploy_data));
}

if (!is_eol)
{
g_debug ("%s: Ref %s (%s) not end-of-life, so excluding from EOL unused refs",
G_STRFUNC, flatpak_decomposed_get_ref (ref),
flatpak_dir_get_name_cached (self));
if (is_autopruned && filter_by_autoprune)
include = TRUE;
if (is_eol && filter_by_eol)
include = TRUE;

if (!include)
{
g_debug ("%s: Ref %s (%s) not %s, so excluding from unused refs",
G_STRFUNC, flatpak_decomposed_get_ref (ref),
flatpak_dir_get_name_cached (self),
(!is_eol && filter_by_eol) ? "end-of-life" : "autopruned");
continue;
}
}
}
}

g_info ("%s: Ref %s (%s) is %s",
G_STRFUNC, flatpak_decomposed_get_ref (ref),
Expand Down
14 changes: 9 additions & 5 deletions common/flatpak-installation.c
Original file line number Diff line number Diff line change
Expand Up @@ -3071,10 +3071,11 @@ flatpak_installation_list_unused_refs (FlatpakInstallation *self,
*
* * exclude-refs (as): Act as if these refs are not installed even if they
* are when determining the set of unused refs
* * filter-by-eol (b): Only return refs as unused if they are End-Of-Life.
* Note that if this option is combined with other filters (of which there
* are none currently) non-EOL refs may also be returned.
*
* * filter-by-eol (b): Return refs as unused if they are End-Of-Life.
* Note that if this option is combined with other filters so non-EOL refs may also be returned.
* * filter-by-autoprune (b): Return refs as unused if they should be autopruned.
* Note that if this option is combined with other filters so non-autoprune refs may also be returned.
* Returns: (transfer container) (element-type FlatpakInstalledRef): a GPtrArray of
* #FlatpakInstalledRef instances
*
Expand All @@ -3093,19 +3094,22 @@ flatpak_installation_list_unused_refs_with_options (FlatpakInstallation *self,
g_auto(GStrv) refs_strv = NULL;
g_autofree char **refs_to_exclude = NULL;
gboolean filter_by_eol = FALSE;
gboolean filter_by_autoprune = FALSE;

if (options)
{
(void) g_variant_lookup (options, "exclude-refs", "^a&s", &refs_to_exclude);
(void) g_variant_lookup (options, "filter-by-eol", "b", &filter_by_eol);
(void) g_variant_lookup (options, "filter-by-autoprune", "b", &filter_by_autoprune);
}

dir = flatpak_installation_get_dir (self, error);
if (dir == NULL)
return NULL;

refs_strv = flatpak_dir_list_unused_refs (dir, arch, metadata_injection, NULL,
(const char * const *)refs_to_exclude, filter_by_eol,
(const char * const *)refs_to_exclude,
filter_by_eol, filter_by_autoprune,
cancellable, error);
if (refs_strv == NULL)
return NULL;
Expand Down
2 changes: 2 additions & 0 deletions common/flatpak-transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -5000,6 +5000,7 @@ add_uninstall_unused_ops (FlatpakTransaction *self,
NULL, /* eol_injection */
NULL, /* exclude_refs */
TRUE, /* filter_by_eol */
TRUE, /* filter_by_autoprune */
cancellable, error);
if (old_unused_refs == NULL)
return FALSE;
Expand Down Expand Up @@ -5057,6 +5058,7 @@ add_uninstall_unused_ops (FlatpakTransaction *self,
eol_injection,
to_be_excluded_strv,
TRUE, /* filter_by_eol */
TRUE, /* filter_by_autoprune */
cancellable, error);
if (unused_refs == NULL)
return FALSE;
Expand Down
8 changes: 4 additions & 4 deletions doc/flatpak-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -925,10 +925,10 @@
<term><option>autoprune-unless</option> (string)</term>
<listitem><para>
A condition that must be false for the extension to be considered unused when
pruning. For example, <command>flatpak uninstall --unused</command> uses
this information. The only currently recognized value is active-gl-driver,
which is true if the name of the active GL driver matches the extension
point basename. Available since 0.11.8.
pruning. For example, <command>flatpak uninstall --unused</command> and
<command>flatpak update</command> use this information. The only currently
recognized value is active-gl-driver, which is true if the name of the active
GL driver matches the extension point basename. Available since 0.11.8.
</para></listitem>
</varlistentry>
<varlistentry>
Expand Down
2 changes: 1 addition & 1 deletion tests/list-unused.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ main (int argc, char *argv[])
else
dir = flatpak_dir_get_system_default ();

refs = flatpak_dir_list_unused_refs (dir, NULL, NULL, NULL, (const char * const *)opt_exclude_refs, FALSE, NULL, &error);
refs = flatpak_dir_list_unused_refs (dir, NULL, NULL, NULL, (const char * const *)opt_exclude_refs, FALSE, FALSE, NULL, &error);
g_assert_nonnull (refs);
g_assert_no_error (error);

Expand Down

0 comments on commit e1d072c

Please sign in to comment.