LCOV - code coverage report
Current view: top level - auditor - taler-auditor-dbinit.c (source / functions) Hit Total Coverage
Test: GNU Taler exchange coverage report Lines: 27 40 67.5 %
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, 2020 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 auditor/taler-auditor-dbinit.c
      18             :  * @brief Create tables for the auditor database.
      19             :  * @author Florian Dold
      20             :  * @author Marcello Stanisci
      21             :  */
      22             : #include "platform.h"
      23             : #include <gnunet/gnunet_util_lib.h>
      24             : #include "taler_auditordb_plugin.h"
      25             : 
      26             : 
      27             : /**
      28             :  * Return value from main().
      29             :  */
      30             : static int global_ret;
      31             : 
      32             : /**
      33             :  * -r option: do restart audits
      34             :  */
      35             : static int restart_db;
      36             : 
      37             : /**
      38             :  * -R option: do full DB reset
      39             :  */
      40             : static int reset_db;
      41             : 
      42             : /**
      43             :  * -g option: garbage collect DB reset
      44             :  */
      45             : static int gc_db;
      46             : 
      47             : 
      48             : /**
      49             :  * Main function that will be run.
      50             :  *
      51             :  * @param cls closure
      52             :  * @param args remaining command-line arguments
      53             :  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
      54             :  * @param cfg configuration
      55             :  */
      56             : static void
      57          51 : run (void *cls,
      58             :      char *const *args,
      59             :      const char *cfgfile,
      60             :      const struct GNUNET_CONFIGURATION_Handle *cfg)
      61             : {
      62             :   struct TALER_AUDITORDB_Plugin *plugin;
      63             : 
      64             :   (void) cls;
      65             :   (void) args;
      66             :   (void) cfgfile;
      67          51 :   if (NULL ==
      68          51 :       (plugin = TALER_AUDITORDB_plugin_load (cfg)))
      69             :   {
      70           0 :     fprintf (stderr,
      71             :              "Failed to initialize database plugin.\n");
      72           0 :     global_ret = EXIT_NOTINSTALLED;
      73           0 :     return;
      74             :   }
      75          51 :   if (reset_db)
      76             :   {
      77           9 :     if (GNUNET_OK !=
      78           9 :         plugin->drop_tables (plugin->cls,
      79             :                              GNUNET_YES))
      80           1 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      81             :                   "Failed to reset database\n");
      82             :   }
      83          42 :   else if (restart_db)
      84             :   {
      85          40 :     if (GNUNET_OK !=
      86          40 :         plugin->drop_tables (plugin->cls,
      87             :                              GNUNET_NO))
      88           0 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      89             :                   "Failed to restart audits\n");
      90             :   }
      91          51 :   if (GNUNET_OK !=
      92          51 :       plugin->create_tables (plugin->cls))
      93             :   {
      94           0 :     fprintf (stderr,
      95             :              "Failed to initialize database.\n");
      96           0 :     TALER_AUDITORDB_plugin_unload (plugin);
      97           0 :     global_ret = EXIT_NOPERMISSION;
      98           0 :     return;
      99             :   }
     100          51 :   if (gc_db)
     101             :   {
     102           0 :     if (GNUNET_SYSERR == plugin->gc (plugin->cls))
     103           0 :       fprintf (stderr,
     104             :                "Garbage collection failed!\n");
     105             :   }
     106          51 :   TALER_AUDITORDB_plugin_unload (plugin);
     107             : }
     108             : 
     109             : 
     110             : /**
     111             :  * The main function of the database initialization tool.
     112             :  * Used to initialize the Taler auditor's database.
     113             :  *
     114             :  * @param argc number of arguments from the command line
     115             :  * @param argv command line arguments
     116             :  * @return 0 ok, 1 on error
     117             :  */
     118             : int
     119          51 : main (int argc,
     120             :       char *const *argv)
     121             : {
     122          51 :   const struct GNUNET_GETOPT_CommandLineOption options[] = {
     123          51 :     GNUNET_GETOPT_option_flag ('r',
     124             :                                "restart",
     125             :                                "restart audits (DANGEROUS: all audits resume from scratch)",
     126             :                                &restart_db),
     127          51 :     GNUNET_GETOPT_option_flag ('R',
     128             :                                "reset",
     129             :                                "reset database (DANGEROUS: all existing data is lost!)",
     130             :                                &reset_db),
     131          51 :     GNUNET_GETOPT_option_flag ('g',
     132             :                                "gc",
     133             :                                "garbage collect database",
     134             :                                &gc_db),
     135             :     GNUNET_GETOPT_OPTION_END
     136             :   };
     137             :   enum GNUNET_GenericReturnValue ret;
     138             : 
     139          51 :   if (GNUNET_OK !=
     140          51 :       GNUNET_STRINGS_get_utf8_args (argc, argv,
     141             :                                     &argc, &argv))
     142           0 :     return EXIT_INVALIDARGUMENT;
     143             :   /* force linker to link against libtalerutil; if we do
     144             :      not do this, the linker may "optimize" libtalerutil
     145             :      away and skip #TALER_OS_init(), which we do need */
     146          51 :   TALER_OS_init ();
     147          51 :   ret = GNUNET_PROGRAM_run (
     148             :     argc, argv,
     149             :     "taler-auditor-dbinit",
     150             :     gettext_noop ("Initialize Taler auditor database"),
     151             :     options,
     152             :     &run, NULL);
     153          51 :   GNUNET_free_nz ((void *) argv);
     154          51 :   if (GNUNET_SYSERR == ret)
     155           0 :     return EXIT_INVALIDARGUMENT;
     156          51 :   if (GNUNET_NO == ret)
     157           0 :     return EXIT_SUCCESS;
     158          51 :   return global_ret;
     159             : }
     160             : 
     161             : 
     162             : /* end of taler-auditor-dbinit.c */

Generated by: LCOV version 1.14