From 9e4bb25b2c389263bc146f5393f62ad0e1348df2 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Wed, 11 May 2016 15:43:40 +0200 Subject: modify-remote: Move the actual config rewriting to FlatpakDir This is in preparation for doing this in the system helper. --- common/flatpak-dir.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ common/flatpak-dir.h | 6 ++++ 2 files changed, 87 insertions(+) (limited to 'common') diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c index f97e125..2c0e1f0 100644 --- a/common/flatpak-dir.c +++ b/common/flatpak-dir.c @@ -3901,6 +3901,87 @@ flatpak_dir_list_remotes (FlatpakDir *self, return res; } +gboolean +flatpak_dir_modify_remove (FlatpakDir *self, + const char *remote_name, + GKeyFile *config, + GBytes *gpg_data, + GCancellable *cancellable, + GError **error) +{ + g_autofree char *group = g_strdup_printf ("remote \"%s\"", remote_name); + g_autofree char *url = NULL; + g_autofree char *metalink = NULL; + g_autoptr(GKeyFile) new_config = NULL; + g_auto(GStrv) keys = NULL; + int i; + + if (strchr (remote_name, '/') != NULL) + return flatpak_fail (error, "Invalid character '/' in remote name: %s", + remote_name); + + + if (!g_key_file_has_group (config, group)) + return flatpak_fail (error, "No configuration for remote %s specified", + remote_name); + + metalink = g_key_file_get_string (config, group, "metalink", NULL); + if (metalink != NULL && *metalink != 0) + url = g_strconcat ("metalink=", metalink, NULL); + else + url = g_key_file_get_string (config, group, "url", NULL); + + if (url == NULL || *url == 0) + return flatpak_fail (error, "No url for remote %s specified", + remote_name); + + /* Add it if its not there yet */ + if (!ostree_repo_remote_change (self->repo, NULL, + OSTREE_REPO_REMOTE_CHANGE_ADD_IF_NOT_EXISTS, + remote_name, + url, NULL, cancellable, error)) + return FALSE; + + new_config = ostree_repo_copy_config (self->repo); + + g_key_file_remove_group (new_config, group, NULL); + + keys = g_key_file_get_keys (config, + group, + NULL, error); + if (keys == NULL) + return FALSE; + + for (i = 0; keys[i] != NULL; i++) + { + g_autofree gchar *value = g_key_file_get_value (config, group, keys[i], NULL); + if (value) + g_key_file_set_value (new_config, group, keys[i], value); + } + + if (!ostree_repo_write_config (self->repo, config, error)) + return FALSE; + + if (gpg_data != NULL) + { + g_autoptr(GInputStream) input_stream = g_memory_input_stream_new_from_bytes (gpg_data); + guint imported = 0; + + if (!ostree_repo_remote_gpg_import (self->repo, remote_name, input_stream, + NULL, &imported, cancellable, error)) + return FALSE; + + /* XXX If we ever add internationalization, use ngettext() here. */ + g_debug ("Imported %u GPG key%s to remote \"%s\"", + imported, (imported == 1) ? "" : "s", remote_name); + } + + if (!flatpak_dir_mark_changed (self, error)) + return FALSE; + + return TRUE; +} + static gboolean remove_unless_in_hash (gpointer key, gpointer value, diff --git a/common/flatpak-dir.h b/common/flatpak-dir.h index f40811f..7f76e2d 100644 --- a/common/flatpak-dir.h +++ b/common/flatpak-dir.h @@ -326,6 +326,12 @@ char *flatpak_dir_create_origin_remote (FlatpakDir *self, char **flatpak_dir_list_remotes (FlatpakDir *self, GCancellable *cancellable, GError **error); +gboolean flatpak_dir_modify_remove (FlatpakDir *self, + const char *remote_name, + GKeyFile *config, + GBytes *gpg_data, + GCancellable *cancellable, + GError **error); char *flatpak_dir_get_remote_title (FlatpakDir *self, const char *remote_name); int flatpak_dir_get_remote_prio (FlatpakDir *self, -- cgit v1.2.1