From 642a5a81a180dc0bfb7d2e2b7759eb03d4b87e70 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 7 Aug 2017 14:58:24 +0100 Subject: [PATCH] lib/remote: Add getter/setter for collection IDs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This isn’t really used internally, but will be used by gnome-software for when it configures new flatpak remotes. This is new public API, but is only declared if compiling with --enable-p2p. Includes some basic smoketests. Signed-off-by: Philip Withnall --- common/flatpak-dir.c | 13 +++++++++++ common/flatpak-dir.h | 2 ++ lib/flatpak-remote.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ lib/flatpak-remote.h | 5 +++++ lib/test-lib.c | 9 +++++++- tests/testlibrary.c | 13 +++++++++++ 6 files changed, 93 insertions(+), 1 deletion(-) diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c index 5308f3fd..537f797b 100644 --- a/common/flatpak-dir.c +++ b/common/flatpak-dir.c @@ -7774,6 +7774,19 @@ get_group (const char *remote_name) return g_strdup_printf ("remote \"%s\"", remote_name); } +char * +flatpak_dir_get_remote_collection_id (FlatpakDir *self, + const char *remote_name) +{ + GKeyFile *config = ostree_repo_get_config (self->repo); + g_autofree char *group = get_group (remote_name); + + if (config) + return g_key_file_get_string (config, group, "collection-id", NULL); + + return NULL; +} + char * flatpak_dir_get_remote_title (FlatpakDir *self, const char *remote_name) diff --git a/common/flatpak-dir.h b/common/flatpak-dir.h index 7ddc1382..ff928c77 100644 --- a/common/flatpak-dir.h +++ b/common/flatpak-dir.h @@ -533,6 +533,8 @@ gboolean flatpak_dir_remove_remote (FlatpakDir *self, GError **error); char *flatpak_dir_get_remote_title (FlatpakDir *self, const char *remote_name); +char *flatpak_dir_get_remote_collection_id (FlatpakDir *self, + const char *remote_name); char *flatpak_dir_get_remote_main_ref (FlatpakDir *self, const char *remote_name); gboolean flatpak_dir_get_remote_oci (FlatpakDir *self, diff --git a/lib/flatpak-remote.c b/lib/flatpak-remote.c index 80f87722..db5e2b98 100644 --- a/lib/flatpak-remote.c +++ b/lib/flatpak-remote.c @@ -86,6 +86,7 @@ struct _FlatpakRemotePrivate FlatpakDir *dir; char *local_url; + char *local_collection_id; char *local_title; char *local_default_branch; gboolean local_gpg_verify; @@ -96,6 +97,7 @@ struct _FlatpakRemotePrivate FlatpakRemoteType type; guint local_url_set : 1; + guint local_collection_id_set : 1; guint local_title_set : 1; guint local_default_branch_set : 1; guint local_gpg_verify_set : 1; @@ -129,6 +131,7 @@ flatpak_remote_finalize (GObject *object) g_bytes_unref (priv->local_gpg_key); g_free (priv->local_url); + g_free (priv->local_collection_id); g_free (priv->local_title); g_free (priv->local_default_branch); @@ -343,6 +346,52 @@ flatpak_remote_set_url (FlatpakRemote *self, priv->local_url_set = TRUE; } +#ifdef FLATPAK_ENABLE_P2P +/** + * flatpak_remote_get_collection_id: + * @self: a #FlatpakRemote + * + * Returns the repository collection ID of this remote, if set. + * + * Returns: (transfer full) (nullable): the collection ID, or %NULL if unset + */ +char * +flatpak_remote_get_collection_id (FlatpakRemote *self) +{ + FlatpakRemotePrivate *priv = flatpak_remote_get_instance_private (self); + + if (priv->local_collection_id_set) + return g_strdup (priv->local_collection_id); + + if (priv->dir) + return flatpak_dir_get_remote_collection_id (priv->dir, priv->name); + + return NULL; +} + +/** + * flatpak_remote_set_collection_id: + * @self: a #FlatpakRemote + * @collection_id: The new collection ID + * + * Sets the repository collection ID of this remote. + * + * Note: This is a local modification of this object, you must commit changes + * using flatpak_installation_modify_remote() for the changes to take + * effect. + */ +void +flatpak_remote_set_collection_id (FlatpakRemote *self, + const char *collection_id) +{ + FlatpakRemotePrivate *priv = flatpak_remote_get_instance_private (self); + + g_free (priv->local_collection_id); + priv->local_collection_id = g_strdup (collection_id); + priv->local_collection_id_set = TRUE; +} +#endif /* FLATPAK_ENABLE_P2P */ + /** * flatpak_remote_get_title: * @self: a #FlatpakRemote @@ -763,6 +812,9 @@ flatpak_remote_commit (FlatpakRemote *self, if (priv->local_url_set) g_key_file_set_string (config, group, "url", priv->local_url); + if (priv->local_collection_id_set) + g_key_file_set_string (config, group, "collection-id", priv->local_collection_id); + if (priv->local_title_set) g_key_file_set_string (config, group, "xa.title", priv->local_title); diff --git a/lib/flatpak-remote.h b/lib/flatpak-remote.h index 07714a04..71afa719 100644 --- a/lib/flatpak-remote.h +++ b/lib/flatpak-remote.h @@ -74,6 +74,11 @@ FLATPAK_EXTERN GFile * flatpak_remote_get_appstream_timestamp (FlatpakRemo FLATPAK_EXTERN char * flatpak_remote_get_url (FlatpakRemote *self); FLATPAK_EXTERN void flatpak_remote_set_url (FlatpakRemote *self, const char *url); +#ifdef FLATPAK_ENABLE_P2P +FLATPAK_EXTERN char * flatpak_remote_get_collection_id (FlatpakRemote *self); +FLATPAK_EXTERN void flatpak_remote_set_collection_id (FlatpakRemote *self, + const char *collection_id); +#endif /* FLATPAK_ENABLE_P2P */ FLATPAK_EXTERN char * flatpak_remote_get_title (FlatpakRemote *self); FLATPAK_EXTERN void flatpak_remote_set_title (FlatpakRemote *self, const char *title); diff --git a/lib/test-lib.c b/lib/test-lib.c index 8a522960..84518fd2 100644 --- a/lib/test-lib.c +++ b/lib/test-lib.c @@ -357,10 +357,17 @@ main (int argc, char *argv[]) { FlatpakRemote *remote = g_ptr_array_index (remotes, i); g_autoptr(GPtrArray) refs = NULL; - g_print ("\nRemote: %s %d %s %s %s %d %d %s\n", + const char *collection_id = NULL; + +#ifdef FLATPAK_ENABLE_P2P + collection_id = flatpak_remote_get_collection_id (remote); +#endif /* !FLATPAK_ENABLE_P2P */ + + g_print ("\nRemote: %s %d %s %s %s %s %d %d %s\n", flatpak_remote_get_name (remote), flatpak_remote_get_prio (remote), flatpak_remote_get_url (remote), + collection_id, flatpak_remote_get_title (remote), flatpak_remote_get_default_branch (remote), flatpak_remote_get_gpg_verify (remote), diff --git a/tests/testlibrary.c b/tests/testlibrary.c index e8df0280..7901529c 100644 --- a/tests/testlibrary.c +++ b/tests/testlibrary.c @@ -12,6 +12,9 @@ static char *flatpak_installationsdir; static char *gpg_homedir; static char *gpg_args; static char *repo_url; +#ifdef FLATPAK_ENABLE_P2P +static char *repo_collection_id; +#endif /* FLATPAK_ENABLE_P2P */ int httpd_pid = -1; static const char *gpg_id = "7B0961FD"; @@ -251,6 +254,10 @@ test_remote_by_name (void) g_assert_false (flatpak_remote_get_disabled (remote)); g_assert_true (flatpak_remote_get_gpg_verify (remote)); g_assert_cmpint (flatpak_remote_get_prio (remote), ==, 1); + +#ifdef FLATPAK_ENABLE_P2P + g_assert_cmpstr (flatpak_remote_get_collection_id (remote), ==, repo_collection_id); +#endif /* FLATPAK_ENABLE_P2P */ } static void @@ -267,6 +274,12 @@ test_remote (void) remote = flatpak_installation_get_remote_by_name (inst, repo_name, NULL, &error); g_assert_no_error (error); +#ifdef FLATPAK_ENABLE_P2P + g_assert_cmpstr (flatpak_remote_get_collection_id (remote), ==, NULL); + flatpak_remote_set_collection_id (remote, "org.example.CollectionID"); + g_assert_cmpstr (flatpak_remote_get_collection_id (remote), ==, "org.example.CollectionID"); +#endif /* FLATPAK_ENABLE_P2P */ + g_assert_cmpstr (flatpak_remote_get_title (remote), ==, NULL); flatpak_remote_set_title (remote, "Test Repo"); g_assert_cmpstr (flatpak_remote_get_title (remote), ==, "Test Repo");