From c963f9eec8d72d6e17099d456330cd988294c0af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sat, 9 Feb 2019 00:00:00 +0000 Subject: bin: Add an option to ignore changes to locked keys during load If load command attempts to change one of non-writable keys, the whole operation fails with an error and no changes are made. Add an `-f` option to the load command that skips non-writable keys and proceeds with remaining changes. Closes issue #1. --- bin/dconf.c | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'bin') diff --git a/bin/dconf.c b/bin/dconf.c index 8e3f060..c91f82a 100644 --- a/bin/dconf.c +++ b/bin/dconf.c @@ -578,14 +578,29 @@ keyfile_foreach (GKeyFile *kf, return TRUE; } +typedef struct { + DConfClient *client; + DConfChangeset *changeset; + gboolean force; +} LoadContext; + static void changeset_set (const gchar *path, GVariant *value, gpointer user_data) { - DConfChangeset *changeset = user_data; + LoadContext *ctx = user_data; + + /* When force option is used, ignore changes made to non-writeable keys to + * avoid rejecting the whole changeset. + */ + if (ctx->force && !dconf_client_is_writable (ctx->client, path)) + { + g_fprintf (stderr, "warning: ignored non-writable key '%s'\n", path); + return; + } - dconf_changeset_set (changeset, path, value); + dconf_changeset_set (ctx->changeset, path, value); } static gboolean @@ -593,27 +608,39 @@ dconf_load (const gchar **argv, GError **error) { const gchar *dir; + gint index = 0; + gboolean force = FALSE; g_autoptr(GError) local_error = NULL; g_autoptr(GKeyFile) kf = NULL; g_autoptr(DConfChangeset) changeset = NULL; g_autoptr (DConfClient) client = NULL; - dir = argv[0]; + if (argv[index] != NULL && strcmp (argv[index], "-f") == 0) + { + force = TRUE; + index += 1; + } + + dir = argv[index]; if (!dconf_is_dir (dir, &local_error)) return option_error_propagate (error, &local_error); - if (argv[1] != NULL) + index += 1; + + if (argv[index] != NULL) return option_error_set (error, "too many arguments"); kf = keyfile_from_stdin (error); if (kf == NULL) return FALSE; + client = dconf_client_new (); changeset = dconf_changeset_new (); - if (!keyfile_foreach (kf, dir, changeset_set, changeset, error)) + + LoadContext ctx = { client, changeset, force }; + if (!keyfile_foreach (kf, dir, changeset_set, &ctx, error)) return FALSE; - client = dconf_client_new (); return dconf_client_change_sync (client, changeset, NULL, NULL, error); } @@ -1024,8 +1051,8 @@ static const Command commands[] = { }, { "load", dconf_load, - "Populate a subpath from stdin", - " DIR " + "Populate a subpath from stdin. -f ignore locked keys.", + " [-f] DIR " }, { "blame", dconf_blame, -- cgit v1.2.1