LCOV - code coverage report
Current view: top level - exchange-tools - taler-exchange-dbinit.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 23 34 67.6 %
Date: 2021-08-30 06:43:37 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014, 2015 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU General Public License as published by the Free Software
       7             :   Foundation; either version 3, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU General Public License along with
      14             :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15             : */
      16             : /**
      17             :  * @file exchange-tools/taler-exchange-dbinit.c
      18             :  * @brief Create tables for the exchange database.
      19             :  * @author Florian Dold
      20             :  */
      21             : #include "platform.h"
      22             : #include <gnunet/gnunet_util_lib.h>
      23             : #include "taler_exchangedb_lib.h"
      24             : 
      25             : 
      26             : /**
      27             :  * Return value from main().
      28             :  */
      29             : static int global_ret;
      30             : 
      31             : /**
      32             :  * -r option: do full DB reset
      33             :  */
      34             : static int reset_db;
      35             : 
      36             : /**
      37             :  * -g option: garbage collect DB reset
      38             :  */
      39             : static int gc_db;
      40             : 
      41             : 
      42             : /**
      43             :  * Main function that will be run.
      44             :  *
      45             :  * @param cls closure
      46             :  * @param args remaining command-line arguments
      47             :  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
      48             :  * @param cfg configuration
      49             :  */
      50             : static void
      51          47 : run (void *cls,
      52             :      char *const *args,
      53             :      const char *cfgfile,
      54             :      const struct GNUNET_CONFIGURATION_Handle *cfg)
      55             : {
      56             :   struct TALER_EXCHANGEDB_Plugin *plugin;
      57             : 
      58             :   (void) cls;
      59             :   (void) args;
      60             :   (void) cfgfile;
      61          47 :   if (NULL ==
      62          47 :       (plugin = TALER_EXCHANGEDB_plugin_load (cfg)))
      63             :   {
      64           0 :     fprintf (stderr,
      65             :              "Failed to initialize database plugin.\n");
      66           0 :     global_ret = EXIT_NOTINSTALLED;
      67           0 :     return;
      68             :   }
      69          47 :   if (reset_db)
      70             :   {
      71           9 :     if (GNUNET_OK != plugin->drop_tables (plugin->cls))
      72             :     {
      73           1 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      74             :                   "Could not drop tables as requested. Either database was not yet initialized, or permission denied. Consult the logs. Will still try to create new tables.\n");
      75             :     }
      76             :   }
      77          47 :   if (GNUNET_OK !=
      78          47 :       plugin->create_tables (plugin->cls))
      79             :   {
      80           0 :     fprintf (stderr,
      81             :              "Failed to initialize database.\n");
      82           0 :     TALER_EXCHANGEDB_plugin_unload (plugin);
      83           0 :     global_ret = EXIT_NOPERMISSION;
      84           0 :     return;
      85             :   }
      86          47 :   if (gc_db)
      87             :   {
      88          34 :     if (GNUNET_SYSERR == plugin->gc (plugin->cls))
      89             :     {
      90           0 :       fprintf (stderr,
      91             :                "Garbage collection failed!\n");
      92             :     }
      93             :   }
      94          47 :   TALER_EXCHANGEDB_plugin_unload (plugin);
      95             : }
      96             : 
      97             : 
      98             : /**
      99             :  * The main function of the database initialization tool.
     100             :  * Used to initialize the Taler Exchange's database.
     101             :  *
     102             :  * @param argc number of arguments from the command line
     103             :  * @param argv command line arguments
     104             :  * @return 0 ok, 1 on error
     105             :  */
     106             : int
     107          47 : main (int argc,
     108             :       char *const *argv)
     109             : {
     110          47 :   const struct GNUNET_GETOPT_CommandLineOption options[] = {
     111          47 :     GNUNET_GETOPT_option_flag ('r',
     112             :                                "reset",
     113             :                                "reset database (DANGEROUS: all existing data is lost!)",
     114             :                                &reset_db),
     115          47 :     GNUNET_GETOPT_option_flag ('g',
     116             :                                "gc",
     117             :                                "garbage collect database",
     118             :                                &gc_db),
     119             :     GNUNET_GETOPT_OPTION_END
     120             :   };
     121             :   enum GNUNET_GenericReturnValue ret;
     122             : 
     123          47 :   if (GNUNET_OK !=
     124          47 :       GNUNET_STRINGS_get_utf8_args (argc, argv,
     125             :                                     &argc, &argv))
     126           0 :     return EXIT_INVALIDARGUMENT;
     127             :   /* force linker to link against libtalerutil; if we do
     128             :      not do this, the linker may "optimize" libtalerutil
     129             :      away and skip #TALER_OS_init(), which we do need */
     130          47 :   TALER_OS_init ();
     131          47 :   ret = GNUNET_PROGRAM_run (
     132             :     argc, argv,
     133             :     "taler-exchange-dbinit",
     134             :     gettext_noop ("Initialize Taler exchange database"),
     135             :     options,
     136             :     &run, NULL);
     137          47 :   GNUNET_free_nz ((void *) argv);
     138          47 :   if (GNUNET_SYSERR == ret)
     139           0 :     return EXIT_INVALIDARGUMENT;
     140          47 :   if (GNUNET_NO == ret)
     141           0 :     return EXIT_SUCCESS;
     142          47 :   return global_ret;
     143             : }
     144             : 
     145             : 
     146             : /* end of taler-exchange-dbinit.c */

Generated by: LCOV version 1.14