diff options
author | Colin Walters <walters@verbum.org> | 2012-04-16 21:21:50 -0400 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2012-04-18 23:12:34 -0400 |
commit | 5947b5b1453e2514e4c483b836e872c585cb816c (patch) | |
tree | ef2a179a3d8004541c04a0e204d2bf788e68cfc0 /src/ostree/ot-builtin-config.c | |
parent | 2ecc0cdef141a760a3b744fa884f7fc52b86e53d (diff) | |
download | ostree-5947b5b1453e2514e4c483b836e872c585cb816c.tar.gz |
core: Add ability for repositories to have a "parent"
This will be useful for ostbuild; a user can create their own archive
mode repository which transparently inherits objects from the
root-owned one in /ostree.
Diffstat (limited to 'src/ostree/ot-builtin-config.c')
-rw-r--r-- | src/ostree/ot-builtin-config.c | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/src/ostree/ot-builtin-config.c b/src/ostree/ot-builtin-config.c new file mode 100644 index 00000000..8ad6a17c --- /dev/null +++ b/src/ostree/ot-builtin-config.c @@ -0,0 +1,144 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters <walters@verbum.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Colin Walters <walters@verbum.org> + */ + +#include "config.h" + +#include "ot-builtins.h" +#include "ostree.h" + +#include <glib/gi18n.h> + +static GOptionEntry options[] = { + { NULL } +}; + +static gboolean +split_key_string (const char *k, + char **out_section, + char **out_value, + GError **error) +{ + const char *dot = strchr (k, '.'); + + if (!dot) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, + "Key must be of the form \"sectionname.keyname\""); + return FALSE; + } + + *out_section = g_strndup (k, dot - k); + *out_value = g_strdup (dot + 1); + + return TRUE; +} + +gboolean +ostree_builtin_config (int argc, char **argv, GFile *repo_path, GError **error) +{ + GOptionContext *context = NULL; + gboolean ret = FALSE; + const char *op; + const char *section_key; + const char *value; + ot_lobj OstreeRepo *repo = NULL; + ot_lfree char *section = NULL; + ot_lfree char *key = NULL; + GKeyFile *config = NULL; + + context = g_option_context_new ("- Change configuration settings"); + g_option_context_add_main_entries (context, options, NULL); + + if (!g_option_context_parse (context, &argc, &argv, error)) + goto out; + + repo = ostree_repo_new (repo_path); + if (!ostree_repo_check (repo, error)) + goto out; + + if (argc < 2) + { + ot_util_usage_error (context, "OPERATION must be specified", error); + goto out; + } + + op = argv[1]; + + if (!strcmp (op, "set")) + { + if (argc < 4) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, + "KEY and VALUE must be specified"); + goto out; + } + + section_key = argv[2]; + value = argv[3]; + + if (!split_key_string (section_key, §ion, &key, error)) + goto out; + + config = ostree_repo_copy_config (repo); + g_key_file_set_string (config, section, key, value); + + if (!ostree_repo_write_config (repo, config, error)) + goto out; + } + else if (!strcmp (op, "set")) + { + ot_lfree char *value = NULL; + if (argc < 3) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, + "KEY must be specified"); + goto out; + } + + section_key = argv[2]; + + if (!split_key_string (section_key, §ion, &key, error)) + goto out; + + config = g_key_file_ref (ostree_repo_get_config (repo)); + + value = g_key_file_get_string (config, section, key, error); + if (value == NULL) + goto out; + + g_print ("%s\n", value); + } + else + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, + "Unknown operation %s", op); + goto out; + } + + ret = TRUE; + out: + if (config) + g_key_file_free (config); + if (context) + g_option_context_free (context); + return ret; +} |