From 0eea5370ba5b94508e560f84d5636519f2c09833 Mon Sep 17 00:00:00 2001
From: Alexander Larsson <alexl@redhat.com>
Date: Wed, 18 Sep 2019 10:28:10 +0200
Subject: [PATCH] Handle 'versions' key when finding local related ref

We were only handling the old single-value 'version' key, even though
we handled the 'versions' key when finding remote related refs.

The result of this was that some extensions, such as the 19.08 opengl
default one was installed by default (as it was found as remote related)
yet still removed with --unused (as it was not locally related).

Fixes https://github.com/flatpak/flatpak/issues/3004

Closes: #3093
Approved by: mwleed

(manually cherry-picked from commit aa7b62a0bf5fc325d0178f350bfb58bf655370e8)

Closes: #3115
Approved by: alexlarsson
---
 common/flatpak-dir.c | 88 ++++++++++++++++++++++++++------------------
 1 file changed, 52 insertions(+), 36 deletions(-)

diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
index e32af9cfa..d64775d85 100644
--- a/common/flatpak-dir.c
+++ b/common/flatpak-dir.c
@@ -12625,6 +12625,9 @@ flatpak_dir_find_local_related_for_metadata (FlatpakDir   *self,
           g_autofree char *extension = NULL;
           g_autofree char *version = g_key_file_get_string (metakey, groups[i],
                                                             FLATPAK_METADATA_KEY_VERSION, NULL);
+          g_auto(GStrv) versions = g_key_file_get_string_list (metakey, groups[i],
+                                                               FLATPAK_METADATA_KEY_VERSIONS,
+                                                               NULL, NULL);
           gboolean subdirectories = g_key_file_get_boolean (metakey, groups[i],
                                                             FLATPAK_METADATA_KEY_SUBDIRECTORIES, NULL);
           gboolean no_autodownload = g_key_file_get_boolean (metakey, groups[i],
@@ -12637,19 +12640,24 @@ flatpak_dir_find_local_related_for_metadata (FlatpakDir   *self,
                                                         FLATPAK_METADATA_KEY_AUTODELETE, NULL);
           gboolean locale_subset = g_key_file_get_boolean (metakey, groups[i],
                                                            FLATPAK_METADATA_KEY_LOCALE_SUBSET, NULL);
-          const char *branch;
-          g_autofree char *extension_ref = NULL;
-          g_autofree char *prefixed_extension_ref = NULL;
-          g_autofree char *checksum = NULL;
           g_autofree char *extension_collection_id = NULL;
+          const char *default_branches[] = { NULL, NULL};
+          const char **branches;
+          int branch_i;
 
           /* Parse actual extension name */
           flatpak_parse_extension_with_tag (tagged_extension, &extension, NULL);
 
-          if (version)
-            branch = version;
+          if (versions)
+            branches = (const char **) versions;
           else
-            branch = parts[3];
+            {
+              if (version)
+                default_branches[0] = version;
+              else
+                default_branches[0] = parts[3];
+              branches = default_branches;
+            }
 
           extension_collection_id = g_key_file_get_string (metakey, groups[i],
                                                            FLATPAK_METADATA_KEY_COLLECTION_ID, NULL);
@@ -12669,38 +12677,46 @@ flatpak_dir_find_local_related_for_metadata (FlatpakDir   *self,
           g_clear_pointer (&extension_collection_id, g_free);
           extension_collection_id = g_strdup (collection_id);
 
-          extension_ref = g_build_filename ("runtime", extension, parts[2], branch, NULL);
-          prefixed_extension_ref = g_strdup_printf ("%s:%s", remote_name, extension_ref);
-          if (ostree_repo_resolve_rev (self->repo,
-                                       prefixed_extension_ref,
-                                       FALSE,
-                                       &checksum,
-                                       NULL))
-            {
-              add_related (self, related, extension, extension_collection_id, extension_ref,
-                           checksum, no_autodownload, download_if, autoprune_unless, autodelete, locale_subset);
-            }
-          else if (subdirectories)
+          for (branch_i = 0; branches[branch_i] != NULL; branch_i++)
             {
-              g_autoptr(GPtrArray) matches = local_match_prefix (self, extension_ref, remote_name);
-              int j;
-              for (j = 0; j < matches->len; j++)
-                {
-                  const char *match = g_ptr_array_index (matches, j);
-                  g_autofree char *prefixed_match = NULL;
-                  g_autofree char *match_checksum = NULL;
-
-                  prefixed_match = g_strdup_printf ("%s:%s", remote_name, match);
+              g_autofree char *extension_ref = NULL;
+              g_autofree char *checksum = NULL;
+              g_autofree char *prefixed_extension_ref = NULL;
+              const char *branch = branches[branch_i];
 
-                  if (ostree_repo_resolve_rev (self->repo,
-                                               prefixed_match,
-                                               FALSE,
-                                               &match_checksum,
-                                               NULL))
+              extension_ref = g_build_filename ("runtime", extension, parts[2], branch, NULL);
+              prefixed_extension_ref = g_strdup_printf ("%s:%s", remote_name, extension_ref);
+              if (ostree_repo_resolve_rev (self->repo,
+                                           prefixed_extension_ref,
+                                           FALSE,
+                                           &checksum,
+                                           NULL))
+                {
+                  add_related (self, related, extension, extension_collection_id, extension_ref,
+                               checksum, no_autodownload, download_if, autoprune_unless, autodelete, locale_subset);
+                }
+              else if (subdirectories)
+                {
+                  g_autoptr(GPtrArray) matches = local_match_prefix (self, extension_ref, remote_name);
+                  int j;
+                  for (j = 0; j < matches->len; j++)
                     {
-                      add_related (self, related, extension,
-                                   extension_collection_id, match, match_checksum,
-                                   no_autodownload, download_if, autoprune_unless, autodelete, locale_subset);
+                      const char *match = g_ptr_array_index (matches, j);
+                      g_autofree char *prefixed_match = NULL;
+                      g_autofree char *match_checksum = NULL;
+
+                      prefixed_match = g_strdup_printf ("%s:%s", remote_name, match);
+
+                      if (ostree_repo_resolve_rev (self->repo,
+                                                   prefixed_match,
+                                                   FALSE,
+                                                   &match_checksum,
+                                                   NULL))
+                        {
+                          add_related (self, related, extension,
+                                       extension_collection_id, match, match_checksum,
+                                       no_autodownload, download_if, autoprune_unless, autodelete, locale_subset);
+                        }
                     }
                 }
             }
