GNUnet  0.19.2
gnunet-namestore-dbtool.c
Go to the documentation of this file.
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  */
26 #include "platform.h"
27 #include <gnunet_util_lib.h>
29 
33 static char *pluginname;
34 
38 static int reset;
39 
43 static int init;
44 
48 static int ret = 0;
49 
55 static void
56 do_shutdown (void *cls)
57 {
58  (void) cls;
59  if (NULL != pluginname)
61 }
62 
63 
72 static void
73 run (void *cls,
74  char *const *args,
75  const char *cfgfile,
76  const struct GNUNET_CONFIGURATION_Handle *cfg)
77 {
78  char *db_lib_name;
80 
81  (void) cls;
82  (void) args;
83  (void) cfgfile;
84  if (NULL != args[0])
85  GNUNET_log (
87  _ ("Superfluous command line arguments (starting with `%s') ignored\n"),
88  args[0]);
89 
91  (void *) cfg);
92  if (NULL == pluginname)
93  {
94  fprintf (stderr, "No plugin given!\n");
95  ret = 1;
97  return;
98  }
100  "libgnunet_plugin_namestore_%s",
101  pluginname);
103  if (NULL == plugin)
104  {
105  fprintf (stderr,
106  "Failed to load %s!\n",
107  db_lib_name);
108  ret = 1;
111  return;
112  }
113  if (reset)
114  {
115  if (GNUNET_OK !=
116  plugin->drop_tables (plugin->cls))
117  {
118  fprintf (stderr,
119  "Failed to reset database\n");
120  ret = 1;
123  return;
124  }
125  }
126  if (init || reset)
127  {
128  if (GNUNET_OK !=
129  plugin->create_tables (plugin->cls))
130  {
131  fprintf (stderr,
132  "Failed to initialize database\n");
133  ret = 1;
136  return;
137  }
138  }
141  plugin));
143 }
144 
145 
153 int
154 main (int argc, char *const *argv)
155 {
157  GNUNET_GETOPT_option_flag ('i', "init",
158  gettext_noop ("initialize database"),
159  &init),
161  "reset",
162  gettext_noop (
163  "reset database (DANGEROUS: All existing data is lost!"),
164  &reset),
166  'p',
167  "plugin",
168  "PLUGIN",
169  gettext_noop (
170  "the namestore plugin to work with, e.g. 'sqlite'"),
171  &pluginname),
173  };
174  int lret;
175 
176  if (GNUNET_OK !=
177  GNUNET_STRINGS_get_utf8_args (argc, argv,
178  &argc, &argv))
179  return 2;
180 
181  GNUNET_log_setup ("gnunet-namestore-dbtool",
182  "WARNING",
183  NULL);
184  if (GNUNET_OK !=
185  (lret = GNUNET_PROGRAM_run (argc,
186  argv,
187  "gnunet-namestore-dbtool",
188  _ (
189  "GNUnet namestore database manipulation tool"),
190  options,
191  &run,
192  NULL)))
193  {
194  GNUNET_free_nz ((void *) argv);
195  return lret;
196  }
197  GNUNET_free_nz ((void *) argv);
198  return ret;
199 }
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:70
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
struct TestcasePlugin * plugin
The process handle to the testbed service.
static char * pluginname
Name of the plugin argument.
static int ret
Return code.
static void do_shutdown(void *cls)
Task run on shutdown.
static int init
Initialize argument.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run.
int main(int argc, char *const *argv)
The main function for gnunet-namestore-dbtool.
static int reset
Reset argument.
static char * db_lib_name
Name of the database plugin.
Plugin API for the namestore database backend.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
#define GNUNET_log(kind,...)
@ GNUNET_OK
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_WARNING
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:242
void * GNUNET_PLUGIN_load(const char *library_name, void *arg)
Setup plugin (runs the "init" callback and returns whatever "init" returned).
Definition: plugin.c:198
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:400
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:534
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1303
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1222
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
Definition of a command line option.
struct returned by the initialization function of the plugin
void * cls
Closure to pass to all plugin functions.