Owen W. Taylor 1b7d62e
From e3774822abcfd5be655cf7459c518beb3dc82b90 Mon Sep 17 00:00:00 2001
Owen W. Taylor 1b7d62e
From: "Owen W. Taylor" <otaylor@fishsoup.net>
Owen W. Taylor 1b7d62e
Date: Fri, 22 Oct 2021 14:20:56 -0400
Owen W. Taylor 1b7d62e
Subject: [PATCH] dnf: Update appstream xml files if dnf_sack_add_repos() does
Owen W. Taylor 1b7d62e
 the download
Owen W. Taylor 1b7d62e
Owen W. Taylor 1b7d62e
We were copying appstream xml files into /var/cache/app-info/xmls when we did
Owen W. Taylor 1b7d62e
the repository download directly by calling dnf_repo_update(), but
Owen W. Taylor 1b7d62e
dnf_repo_update() can also be implicitly called by dnf_sack_add_repos().
Owen W. Taylor 1b7d62e
Owen W. Taylor 1b7d62e
Check before calling dnf_sack_add_repos() which repos look out-of-date
Owen W. Taylor 1b7d62e
and copy over their appstream info afterwards.
Owen W. Taylor 1b7d62e
---
Owen W. Taylor 1b7d62e
 backends/dnf/pk-backend-dnf.c | 134 ++++++++++++++++++++++------------
Owen W. Taylor 1b7d62e
 1 file changed, 89 insertions(+), 45 deletions(-)
Owen W. Taylor 1b7d62e
Owen W. Taylor 1b7d62e
diff --git a/backends/dnf/pk-backend-dnf.c b/backends/dnf/pk-backend-dnf.c
Owen W. Taylor 1b7d62e
index e62f40ff4..42ae19602 100644
Owen W. Taylor 1b7d62e
--- a/backends/dnf/pk-backend-dnf.c
Owen W. Taylor 1b7d62e
+++ b/backends/dnf/pk-backend-dnf.c
Owen W. Taylor 1b7d62e
@@ -70,6 +70,12 @@ typedef struct {
Owen W. Taylor 1b7d62e
 	HyGoal		 goal;
Owen W. Taylor 1b7d62e
 } PkBackendDnfJobData;
Owen W. Taylor 1b7d62e
 
Owen W. Taylor 1b7d62e
+static GPtrArray * pk_backend_find_refresh_repos (PkBackendJob *job,
Owen W. Taylor 1b7d62e
+						  DnfState     *state,
Owen W. Taylor 1b7d62e
+						  GPtrArray    *repos,
Owen W. Taylor 1b7d62e
+						  gboolean      force,
Owen W. Taylor 1b7d62e
+						  GError      **error);
Owen W. Taylor 1b7d62e
+
Owen W. Taylor 1b7d62e
 const gchar *
Owen W. Taylor 1b7d62e
 pk_backend_get_description (PkBackend *backend)
Owen W. Taylor 1b7d62e
 {
Owen W. Taylor 1b7d62e
@@ -555,11 +561,13 @@ dnf_utils_add_remote (PkBackendJob *job,
Owen W. Taylor 1b7d62e
 	gboolean ret;
Owen W. Taylor 1b7d62e
 	DnfState *state_local;
Owen W. Taylor 1b7d62e
 	g_autoptr(GPtrArray) repos = NULL;
Owen W. Taylor 1b7d62e
+	g_autoptr(GPtrArray) refresh_repos = NULL;
Owen W. Taylor 1b7d62e
 
Owen W. Taylor 1b7d62e
 	/* set state */
Owen W. Taylor 1b7d62e
 	ret = dnf_state_set_steps (state, error,
Owen W. Taylor 1b7d62e
 				   2, /* load files */
Owen W. Taylor 1b7d62e
-				   98, /* add repos */
Owen W. Taylor 1b7d62e
+				   1, /* count */
Owen W. Taylor 1b7d62e
+				   97, /* add repos */
Owen W. Taylor 1b7d62e
 				   -1);
Owen W. Taylor 1b7d62e
 	if (!ret)
Owen W. Taylor 1b7d62e
 		return FALSE;
Owen W. Taylor 1b7d62e
@@ -573,6 +581,20 @@ dnf_utils_add_remote (PkBackendJob *job,
Owen W. Taylor 1b7d62e
 	if (!dnf_state_done (state, error))
Owen W. Taylor 1b7d62e
 		return FALSE;
Owen W. Taylor 1b7d62e
 
Owen W. Taylor 1b7d62e
+	/* Find out what repositories potentially will get downloaded - we might need to update
Owen W. Taylor 1b7d62e
+	 * the appstream data for these repositories. Note that there is a small chance that the
Owen W. Taylor 1b7d62e
+	 * repo metadata could expire between the call to dnf_repo_check() at this point, and
Owen W. Taylor 1b7d62e
+	 * the call to dnf_repo_check() inside dnf_sack_add_repos() - in this case we'll end up
Owen W. Taylor 1b7d62e
+	 * with stale appstream data until the next metadata refresh.
Owen W. Taylor 1b7d62e
+	 */
Owen W. Taylor 1b7d62e
+	refresh_repos = pk_backend_find_refresh_repos (job,
Owen W. Taylor 1b7d62e
+						       state,
Owen W. Taylor 1b7d62e
+						       repos,
Owen W. Taylor 1b7d62e
+						       FALSE /* !force */,
Owen W. Taylor 1b7d62e
+						       error);
Owen W. Taylor 1b7d62e
+	if (refresh_repos == NULL)
Owen W. Taylor 1b7d62e
+		return FALSE;
Owen W. Taylor 1b7d62e
+
Owen W. Taylor 1b7d62e
 	/* add each repo */
Owen W. Taylor 1b7d62e
 	state_local = dnf_state_get_child (state);
Owen W. Taylor 1b7d62e
 	ret = dnf_sack_add_repos (sack,
Owen W. Taylor 1b7d62e
@@ -584,6 +606,12 @@ dnf_utils_add_remote (PkBackendJob *job,
Owen W. Taylor 1b7d62e
 	if (!ret)
Owen W. Taylor 1b7d62e
 		return FALSE;
Owen W. Taylor 1b7d62e
 
Owen W. Taylor 1b7d62e
+	for (guint i = 0; i < refresh_repos->len; i++) {
Owen W. Taylor 1b7d62e
+		DnfRepo *repo = g_ptr_array_index (refresh_repos, i);
Owen W. Taylor 1b7d62e
+		if (!dnf_utils_refresh_repo_appstream (repo, error))
Owen W. Taylor 1b7d62e
+			return FALSE;
Owen W. Taylor 1b7d62e
+	}
Owen W. Taylor 1b7d62e
+
Owen W. Taylor 1b7d62e
 	/* done */
Owen W. Taylor 1b7d62e
 	if (!dnf_state_done (state, error))
Owen W. Taylor 1b7d62e
 		return FALSE;
Owen W. Taylor 1b7d62e
@@ -1655,49 +1683,22 @@ pk_backend_refresh_subman (PkBackendJob *job)
Owen W. Taylor 1b7d62e
 	pk_backend_repo_list_changed (backend);
Owen W. Taylor 1b7d62e
 }
Owen W. Taylor 1b7d62e
 
Owen W. Taylor 1b7d62e
-static void
Owen W. Taylor 1b7d62e
-pk_backend_refresh_cache_thread (PkBackendJob *job,
Owen W. Taylor 1b7d62e
-				 GVariant *params,
Owen W. Taylor 1b7d62e
-				 gpointer user_data)
Owen W. Taylor 1b7d62e
+static GPtrArray *
Owen W. Taylor 1b7d62e
+pk_backend_find_refresh_repos (PkBackendJob *job,
Owen W. Taylor 1b7d62e
+			       DnfState     *state,
Owen W. Taylor 1b7d62e
+			       GPtrArray    *repos,
Owen W. Taylor 1b7d62e
+			       gboolean      force,
Owen W. Taylor 1b7d62e
+			       GError      **error)
Owen W. Taylor 1b7d62e
 {
Owen W. Taylor 1b7d62e
-	PkBackendDnfJobData *job_data = pk_backend_job_get_user_data (job);
Owen W. Taylor 1b7d62e
-	PkBackend *backend = pk_backend_job_get_backend (job);
Owen W. Taylor 1b7d62e
-	DnfRepo *repo;
Owen W. Taylor 1b7d62e
+	g_autoptr(GPtrArray) refresh_repos = NULL;
Owen W. Taylor 1b7d62e
 	DnfState *state_local;
Owen W. Taylor 1b7d62e
 	DnfState *state_loop;
Owen W. Taylor 1b7d62e
-	gboolean force;
Owen W. Taylor 1b7d62e
-	gboolean ret;
Owen W. Taylor 1b7d62e
 	guint cnt = 0;
Owen W. Taylor 1b7d62e
 	guint i;
Owen W. Taylor 1b7d62e
-	g_autoptr(DnfSack) sack = NULL;
Owen W. Taylor 1b7d62e
-	g_autoptr(GError) error = NULL;
Owen W. Taylor 1b7d62e
-	g_autoptr(GPtrArray) refresh_repos = NULL;
Owen W. Taylor 1b7d62e
-	g_autoptr(GPtrArray) repos = NULL;
Owen W. Taylor 1b7d62e
-
Owen W. Taylor 1b7d62e
-	/* set state */
Owen W. Taylor 1b7d62e
-	dnf_state_set_steps (job_data->state, NULL,
Owen W. Taylor 1b7d62e
-			     1, /* count */
Owen W. Taylor 1b7d62e
-			     95, /* download */
Owen W. Taylor 1b7d62e
-			     4, /* rebuild SAT */
Owen W. Taylor 1b7d62e
-			     -1);
Owen W. Taylor 1b7d62e
-
Owen W. Taylor 1b7d62e
-	g_variant_get (params, "(b)", &force);
Owen W. Taylor 1b7d62e
-
Owen W. Taylor 1b7d62e
-	/* kick subscription-manager if it exists */
Owen W. Taylor 1b7d62e
-	pk_backend_refresh_subman (job);
Owen W. Taylor 1b7d62e
-
Owen W. Taylor 1b7d62e
-	/* ask the context's repo loader for new repos, forcing it to reload them */
Owen W. Taylor 1b7d62e
-	repos = dnf_repo_loader_get_repos (dnf_context_get_repo_loader (job_data->context), &error);
Owen W. Taylor 1b7d62e
-	if (repos == NULL) {
Owen W. Taylor 1b7d62e
-		pk_backend_job_error_code (job,
Owen W. Taylor 1b7d62e
-		                           error->code,
Owen W. Taylor 1b7d62e
-		                           "failed to load repos: %s", error->message);
Owen W. Taylor 1b7d62e
-		return;
Owen W. Taylor 1b7d62e
-	}
Owen W. Taylor 1b7d62e
 
Owen W. Taylor 1b7d62e
 	/* count the enabled repos */
Owen W. Taylor 1b7d62e
 	for (i = 0; i < repos->len; i++) {
Owen W. Taylor 1b7d62e
-		repo = g_ptr_array_index (repos, i);
Owen W. Taylor 1b7d62e
+		DnfRepo *repo = g_ptr_array_index (repos, i);
Owen W. Taylor 1b7d62e
 		if (dnf_repo_get_enabled (repo) == DNF_REPO_ENABLED_NONE)
Owen W. Taylor 1b7d62e
 			continue;
Owen W. Taylor 1b7d62e
 		if (dnf_repo_get_kind (repo) == DNF_REPO_KIND_MEDIA)
Owen W. Taylor 1b7d62e
@@ -1709,12 +1710,12 @@ pk_backend_refresh_cache_thread (PkBackendJob *job,
Owen W. Taylor 1b7d62e
 
Owen W. Taylor 1b7d62e
 	/* figure out which repos need refreshing */
Owen W. Taylor 1b7d62e
 	refresh_repos = g_ptr_array_new ();
Owen W. Taylor 1b7d62e
-	state_local = dnf_state_get_child (job_data->state);
Owen W. Taylor 1b7d62e
+	state_local = dnf_state_get_child (state);
Owen W. Taylor 1b7d62e
 	dnf_state_set_number_steps (state_local, cnt);
Owen W. Taylor 1b7d62e
 	for (i = 0; i < repos->len; i++) {
Owen W. Taylor 1b7d62e
+		DnfRepo *repo = g_ptr_array_index (repos, i);
Owen W. Taylor 1b7d62e
 		gboolean repo_okay;
Owen W. Taylor 1b7d62e
 
Owen W. Taylor 1b7d62e
-		repo = g_ptr_array_index (repos, i);
Owen W. Taylor 1b7d62e
 		if (dnf_repo_get_enabled (repo) == DNF_REPO_ENABLED_NONE)
Owen W. Taylor 1b7d62e
 			continue;
Owen W. Taylor 1b7d62e
 		if (dnf_repo_get_kind (repo) == DNF_REPO_KIND_MEDIA)
Owen W. Taylor 1b7d62e
@@ -1733,16 +1734,59 @@ pk_backend_refresh_cache_thread (PkBackendJob *job,
Owen W. Taylor 1b7d62e
 			                 g_ptr_array_index (repos, i));
Owen W. Taylor 1b7d62e
 
Owen W. Taylor 1b7d62e
 		/* done */
Owen W. Taylor 1b7d62e
-		ret = dnf_state_done (state_local, &error);
Owen W. Taylor 1b7d62e
-		if (!ret) {
Owen W. Taylor 1b7d62e
-			pk_backend_job_error_code (job, error->code, "%s", error->message);
Owen W. Taylor 1b7d62e
-			return;
Owen W. Taylor 1b7d62e
-		}
Owen W. Taylor 1b7d62e
+		if (!dnf_state_done (state_local, error))
Owen W. Taylor 1b7d62e
+			return NULL;
Owen W. Taylor 1b7d62e
 	}
Owen W. Taylor 1b7d62e
 
Owen W. Taylor 1b7d62e
 	/* done */
Owen W. Taylor 1b7d62e
-	ret = dnf_state_done (job_data->state, &error);
Owen W. Taylor 1b7d62e
-	if (!ret) {
Owen W. Taylor 1b7d62e
+	if (!dnf_state_done (state, error))
Owen W. Taylor 1b7d62e
+		return NULL;
Owen W. Taylor 1b7d62e
+
Owen W. Taylor 1b7d62e
+	return g_steal_pointer (&refresh_repos);
Owen W. Taylor 1b7d62e
+}
Owen W. Taylor 1b7d62e
+
Owen W. Taylor 1b7d62e
+static void
Owen W. Taylor 1b7d62e
+pk_backend_refresh_cache_thread (PkBackendJob *job,
Owen W. Taylor 1b7d62e
+				 GVariant *params,
Owen W. Taylor 1b7d62e
+				 gpointer user_data)
Owen W. Taylor 1b7d62e
+{
Owen W. Taylor 1b7d62e
+	PkBackendDnfJobData *job_data = pk_backend_job_get_user_data (job);
Owen W. Taylor 1b7d62e
+	PkBackend *backend = pk_backend_job_get_backend (job);
Owen W. Taylor 1b7d62e
+	DnfRepo *repo;
Owen W. Taylor 1b7d62e
+	DnfState *state_local;
Owen W. Taylor 1b7d62e
+	DnfState *state_loop;
Owen W. Taylor 1b7d62e
+	gboolean force;
Owen W. Taylor 1b7d62e
+	gboolean ret;
Owen W. Taylor 1b7d62e
+	guint i;
Owen W. Taylor 1b7d62e
+	g_autoptr(DnfSack) sack = NULL;
Owen W. Taylor 1b7d62e
+	g_autoptr(GError) error = NULL;
Owen W. Taylor 1b7d62e
+	g_autoptr(GPtrArray) refresh_repos = NULL;
Owen W. Taylor 1b7d62e
+	g_autoptr(GPtrArray) repos = NULL;
Owen W. Taylor 1b7d62e
+
Owen W. Taylor 1b7d62e
+	/* set state */
Owen W. Taylor 1b7d62e
+	dnf_state_set_steps (job_data->state, NULL,
Owen W. Taylor 1b7d62e
+			     1, /* count */
Owen W. Taylor 1b7d62e
+			     95, /* download */
Owen W. Taylor 1b7d62e
+			     4, /* rebuild SAT */
Owen W. Taylor 1b7d62e
+			     -1);
Owen W. Taylor 1b7d62e
+
Owen W. Taylor 1b7d62e
+	g_variant_get (params, "(b)", &force);
Owen W. Taylor 1b7d62e
+
Owen W. Taylor 1b7d62e
+	/* kick subscription-manager if it exists */
Owen W. Taylor 1b7d62e
+	pk_backend_refresh_subman (job);
Owen W. Taylor 1b7d62e
+
Owen W. Taylor 1b7d62e
+	/* ask the context's repo loader for new repos, forcing it to reload them */
Owen W. Taylor 1b7d62e
+	repos = dnf_repo_loader_get_repos (dnf_context_get_repo_loader (job_data->context), &error);
Owen W. Taylor 1b7d62e
+	if (repos == NULL) {
Owen W. Taylor 1b7d62e
+		pk_backend_job_error_code (job,
Owen W. Taylor 1b7d62e
+		                           error->code,
Owen W. Taylor 1b7d62e
+		                           "failed to load repos: %s", error->message);
Owen W. Taylor 1b7d62e
+		return;
Owen W. Taylor 1b7d62e
+	}
Owen W. Taylor 1b7d62e
+
Owen W. Taylor 1b7d62e
+	/* figure out which repos need refreshing */
Owen W. Taylor 1b7d62e
+	refresh_repos = pk_backend_find_refresh_repos (job, job_data->state, repos, force, &error);
Owen W. Taylor 1b7d62e
+	if (refresh_repos == NULL) {
Owen W. Taylor 1b7d62e
 		pk_backend_job_error_code (job, error->code, "%s", error->message);
Owen W. Taylor 1b7d62e
 		return;
Owen W. Taylor 1b7d62e
 	}
Owen W. Taylor 1b7d62e
-- 
Owen W. Taylor 1b7d62e
2.29.2
Owen W. Taylor 1b7d62e