summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2014-10-17 11:47:01 -0400
committerColin Walters <walters@verbum.org>2014-10-17 19:09:42 -0400
commitcdfcf09316ed61964bde950b72092a96a221493d (patch)
tree3230ac8e07f813365da2ca63f0191f0d1d604964
parent49540f076fb71ba06341409e77be174d8596daf7 (diff)
downloadostree-cdfcf09316ed61964bde950b72092a96a221493d.tar.gz
Add "ostree remote delete" and corresponding API
For Anaconda, we have an ugly bootstrapping problem where we need to add the remote to the repository's config, then do a pull+deploy, then remove and re-add the config, because /etc/ostree/remotes.d doesn't exist yet in the target system. https://bugzilla.gnome.org/show_bug.cgi?id=738698
-rw-r--r--src/libostree/ostree-repo.c76
-rw-r--r--src/libostree/ostree-repo.h5
-rw-r--r--src/ostree/ot-builtin-remote.c5
-rwxr-xr-xtests/test-remote-add.sh10
4 files changed, 96 insertions, 0 deletions
diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c
index bddfbfb2..4887e8d1 100644
--- a/src/libostree/ostree-repo.c
+++ b/src/libostree/ostree-repo.c
@@ -474,6 +474,82 @@ ostree_repo_remote_add (OstreeRepo *self,
return ret;
}
+/**
+ * ostree_repo_remote_delete:
+ * @self: Repo
+ * @name: Name of remote
+ * @cancellable: Cancellable
+ * @error: Error
+ *
+ * Delete the remote named @name. It is an error if the provided
+ * remote does not exist.
+ *
+ */
+gboolean
+ostree_repo_remote_delete (OstreeRepo *self,
+ const char *name,
+ GCancellable *cancellable,
+ GError **error)
+{
+ gboolean ret = FALSE;
+ gs_unref_object GFile *etc_ostree_remotes_d = g_file_new_for_path (SYSCONFDIR "/ostree/remotes.d");
+ local_cleanup_keyfile GKeyFile *target_keyfile = NULL;
+ gs_free char *section = NULL;
+ gs_unref_object GFile *target_conf = NULL;
+ gboolean is_system;
+
+ g_return_val_if_fail (name != NULL, FALSE);
+
+ if (strchr (name, '/') != NULL)
+ {
+ g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "Invalid character '/' in remote name: %s",
+ name);
+ goto out;
+ }
+
+ section = g_strdup_printf ("remote \"%s\"", name);
+
+ /* Note we prefer deleting from the config if it exists there */
+ if (g_key_file_has_group (self->config, section))
+ is_system = FALSE;
+ else
+ is_system = ostree_repo_is_system (self);
+
+ if (is_system)
+ {
+ gs_free char *target_name = NULL;
+
+ target_name = g_strconcat (name, ".conf", NULL);
+ target_conf = g_file_get_child (etc_ostree_remotes_d, target_name);
+
+ if (!gs_file_unlink (target_conf, cancellable, error))
+ goto out;
+ }
+ else
+ {
+ gsize len;
+ gs_free char *data = NULL;
+
+ target_conf = g_object_ref (self->config_file);
+
+ target_keyfile = ostree_repo_copy_config (self);
+
+ if (!g_key_file_remove_group (target_keyfile, section, error))
+ goto out;
+
+ data = g_key_file_to_data (target_keyfile, &len, NULL);
+ if (!g_file_replace_contents (target_conf, data, len,
+ NULL, FALSE, 0, NULL,
+ cancellable, error))
+ goto out;
+ }
+
+ ret = TRUE;
+ out:
+ return ret;
+}
+
static gboolean
ostree_repo_mode_to_string (OstreeRepoMode mode,
const char **out_mode,
diff --git a/src/libostree/ostree-repo.h b/src/libostree/ostree-repo.h
index 5ca9b3fe..d924904e 100644
--- a/src/libostree/ostree-repo.h
+++ b/src/libostree/ostree-repo.h
@@ -74,6 +74,11 @@ gboolean ostree_repo_remote_add (OstreeRepo *self,
GCancellable *cancellable,
GError **error);
+gboolean ostree_repo_remote_delete (OstreeRepo *self,
+ const char *name,
+ GCancellable *cancellable,
+ GError **error);
+
OstreeRepo * ostree_repo_get_parent (OstreeRepo *self);
gboolean ostree_repo_write_config (OstreeRepo *self,
diff --git a/src/ostree/ot-builtin-remote.c b/src/ostree/ot-builtin-remote.c
index 3a9c3b74..6e63a489 100644
--- a/src/ostree/ot-builtin-remote.c
+++ b/src/ostree/ot-builtin-remote.c
@@ -160,6 +160,11 @@ ostree_builtin_remote (int argc, char **argv, OstreeRepo *repo, GCancellable *ca
g_print ("%s\n", url);
}
+ else if (!strcmp (op, "delete"))
+ {
+ if (!ostree_repo_remote_delete (repo, remote_name, cancellable, error))
+ goto out;
+ }
else
{
usage_error (context, "Unknown operation", error);
diff --git a/tests/test-remote-add.sh b/tests/test-remote-add.sh
index 7cd88124..65efdc1e 100755
--- a/tests/test-remote-add.sh
+++ b/tests/test-remote-add.sh
@@ -32,3 +32,13 @@ echo "ok config"
$OSTREE remote add --no-gpg-verify another http://another.com/repo
assert_file_has_content $test_tmpdir/repo/config "gpg-verify=false"
echo "ok remote no gpg-verify"
+
+$OSTREE remote delete another
+echo "ok remote delete"
+
+if $OSTREE remote delete nosuchremote 2>err.txt; then
+ assert_not_reached "Deleting remote unexpectedly succeeded"
+fi
+assert_file_has_content err.txt "error: "
+
+