Debian Codegraph @

subprojects/gvdb/tools/gvdb-dump.c

raw
1 /*
2 * Copyright 2026 Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Matthias Clasen <[email protected]>
20 */
21
22 #include <glib.h>
23 #include "gvdb/gvdb-reader.h"
24
25 #define DEPTH 4
26
27 static GOptionContext *context;
28 static char **filenames;
29
30 const GOptionEntry entries[] = {
31 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, "The gvdb database file to dump", "FILE" },
32 { NULL }
33 };
34
35 static void
36 gvdb_table_dump (GvdbTable *table,
37 int depth)
38 {
39 gchar **names;
40 gsize length;
41
42 names = gvdb_table_get_names (table, &length);
43 if (!names)
44 return;
45
46 for (gsize i = 0; i < length; i++)
47 {
48 g_print ("%*c%s", depth, ' ', names[i]);
49
50 if (gvdb_table_has_value (table, names[i]))
51 {
52 GVariant *var;
53 GString *val;
54
55 var = gvdb_table_get_value (table, names[i]);
56 val = g_variant_print_string (var, NULL, FALSE);
57 g_print (": %s\n", val->str);
58 g_string_free (val, TRUE);
59 g_variant_unref (var);
60 }
61 else
62 {
63 GvdbTable *table2;
64
65 g_print ("\n");
66
67 table2 = gvdb_table_get_table (table, names[i]);
68 if (table2)
69 {
70 gvdb_table_dump (table2, depth + DEPTH);
71 gvdb_table_free (table2);
72 }
73 }
74 }
75
76 g_strfreev (names);
77 }
78
79 int
80 main (int argc, char *argv[])
81 {
82 GError *error = NULL;
83 GvdbTable *table;
84
85 context = g_option_context_new (NULL);
86 g_option_context_set_summary (context, "Dump the contents of a gvdb database");
87 g_option_context_add_main_entries (context, entries, NULL);
88
89 if (!g_option_context_parse (context, &argc, &argv, &error))
90 {
91 g_printerr ("%s\n", error->message);
92 g_error_free (error);
93 exit (1);
94 }
95
96 if (!filenames || g_strv_length (filenames) != 1)
97 {
98 char *help = g_option_context_get_help (context, TRUE, NULL);
99 g_printerr ("%s", help);
100 g_free (help);
101 exit (1);
102 }
103
104 table = gvdb_table_new (filenames[0], TRUE, &error);
105
106 if (!table)
107 {
108 g_printerr ("Can't read database: %s\n", error->message);
109 g_error_free (error);
110 exit (2);
111 }
112
113 gvdb_table_dump (table, 0);
114
115 gvdb_table_free (table);
116
117 return 0;
118 }