aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/gnunet-namestore-dbtool.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namestore/gnunet-namestore-dbtool.c')
-rw-r--r--src/namestore/gnunet-namestore-dbtool.c189
1 files changed, 189 insertions, 0 deletions
diff --git a/src/namestore/gnunet-namestore-dbtool.c b/src/namestore/gnunet-namestore-dbtool.c
new file mode 100644
index 000000000..b0f7e2ab9
--- /dev/null
+++ b/src/namestore/gnunet-namestore-dbtool.c
@@ -0,0 +1,189 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014, 2019, 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file gnunet-namestore-dbtool.c
22 * @brief command line tool to manipulate the database backends for the namestore
23 * @author Martin Schanzenbach
24 *
25 */
26#include "platform.h"
27#include <gnunet_util_lib.h>
28#include <gnunet_namestore_plugin.h>
29
30/**
31 * Name of the plugin argument
32 */
33static char *pluginname;
34
35/**
36 * Reset argument
37 */
38static int reset;
39
40/**
41 * Initialize argument
42 */
43static int init;
44
45/**
46 * Return code
47 */
48static int ret = 0;
49
50/**
51 * Task run on shutdown. Cleans up everything.
52 *
53 * @param cls unused
54 */
55static void
56do_shutdown (void *cls)
57{
58 (void) cls;
59 if (NULL != pluginname)
60 GNUNET_free (pluginname);
61}
62/**
63 * Main function that will be run.
64 *
65 * @param cls closure
66 * @param args remaining command-line arguments
67 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
68 * @param cfg configuration
69 */
70static void
71run (void *cls,
72 char *const *args,
73 const char *cfgfile,
74 const struct GNUNET_CONFIGURATION_Handle *cfg)
75{
76 const char *pkey_str;
77 char *db_lib_name;
78 char *emsg;
79 struct GNUNET_NAMESTORE_PluginFunctions *plugin;
80
81 (void) cls;
82 (void) args;
83 (void) cfgfile;
84 if (NULL != args[0])
85 GNUNET_log (
86 GNUNET_ERROR_TYPE_WARNING,
87 _ ("Superfluous command line arguments (starting with `%s') ignored\n"),
88 args[0]);
89
90 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, (void *) cfg);
91 if (NULL == pluginname)
92 {
93 fprintf (stderr, "No plugin given!\n");
94 ret = 1;
95 GNUNET_SCHEDULER_shutdown ();
96 return;
97 }
98 GNUNET_asprintf (&db_lib_name, "libgnunet_plugin_namestore_%s", pluginname);
99 plugin = GNUNET_PLUGIN_load (db_lib_name, (void *) cfg);
100 if (NULL == plugin)
101 {
102 fprintf (stderr, "Failed to load %s!\n", db_lib_name);
103 ret = 1;
104 GNUNET_SCHEDULER_shutdown ();
105 GNUNET_free (db_lib_name);
106 return;
107 }
108 if (reset)
109 {
110 if (GNUNET_OK != plugin->reset_database (plugin->cls, &emsg))
111 {
112 // FIXME do we want to return a reason?
113 fprintf (stderr, "Failed to reset database: %s\n",
114 emsg);
115 ret = 1;
116 GNUNET_free (emsg);
117 GNUNET_free (db_lib_name);
118 GNUNET_SCHEDULER_shutdown ();
119 return;
120 }
121 }
122 else if (init)
123 {
124 if (GNUNET_OK != plugin->initialize_database (plugin->cls, &emsg))
125 {
126 // FIXME do we want to return a reason?
127 fprintf (stderr, "Failed to initialize database: %s\n",
128 emsg);
129 ret = 1;
130 GNUNET_free (emsg);
131 GNUNET_free (db_lib_name);
132 GNUNET_SCHEDULER_shutdown ();
133 return;
134 }
135 }
136 GNUNET_SCHEDULER_shutdown ();
137 GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, plugin));
138 GNUNET_free (db_lib_name);
139}
140
141
142/**
143 * The main function for gnunet-namestore-dbtool.
144 *
145 * @param argc number of arguments from the command line
146 * @param argv command line arguments
147 * @return 0 ok, 1 on error
148 */
149int
150main (int argc, char *const *argv)
151{
152 struct GNUNET_GETOPT_CommandLineOption options[] =
153 { GNUNET_GETOPT_option_flag ('i', "init", gettext_noop (
154 "initialize database"), &init),
155 GNUNET_GETOPT_option_flag ('r',
156 "reset",
157 gettext_noop (
158 "reset database (DANGEROUS: All existing data is lost!"),
159 &reset),
160 GNUNET_GETOPT_option_string (
161 'p',
162 "plugin",
163 "PLUGIN",
164 gettext_noop (
165 "the namestore plugin to work with, e.g. 'sqlite'"),
166 &pluginname),
167 GNUNET_GETOPT_OPTION_END };
168 int lret;
169
170 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
171 return 2;
172
173 GNUNET_log_setup ("gnunet-namestore-dbtool", "WARNING", NULL);
174 if (GNUNET_OK !=
175 (lret = GNUNET_PROGRAM_run (argc,
176 argv,
177 "gnunet-namestore-dbtool",
178 _ (
179 "GNUnet namestore database manipulation tool"),
180 options,
181 &run,
182 NULL)))
183 {
184 GNUNET_free_nz ((void *) argv);
185 return lret;
186 }
187 GNUNET_free_nz ((void *) argv);
188 return ret;
189}