LCOV - code coverage report
Current view: top level - auditor - taler-auditor-dbinit.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 23 35 65.7 %
Date: 2025-06-05 21:03:14 Functions: 2 2 100.0 %

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

Generated by: LCOV version 1.16