LCOV - code coverage report
Current view: top level - merchant-tools - taler-merchant-dbinit.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 22 34 64.7 %
Date: 2025-06-23 16:22:09 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 merchant-tools/taler-merchant-dbinit.c
      18             :  * @brief Create tables for the merchant database.
      19             :  * @author Florian Dold
      20             :  * @author Marcello Stanisci
      21             :  */
      22             : #include "platform.h"
      23             : #include <taler/taler_util.h>
      24             : #include <gnunet/gnunet_util_lib.h>
      25             : #include "taler_merchant_util.h"
      26             : #include "taler_merchantdb_lib.h"
      27             : 
      28             : 
      29             : /**
      30             :  * Return value from main().
      31             :  */
      32             : static int global_ret;
      33             : 
      34             : /**
      35             :  * -r option: do full DB reset
      36             :  */
      37             : static int reset_db;
      38             : 
      39             : /**
      40             :  * -g option: garbage collect DB
      41             :  */
      42             : static int gc_db;
      43             : 
      44             : /**
      45             :  * Main function that will be run.
      46             :  *
      47             :  * @param cls closure
      48             :  * @param args remaining command-line arguments
      49             :  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
      50             :  * @param config configuration
      51             :  */
      52             : static void
      53          14 : run (void *cls,
      54             :      char *const *args,
      55             :      const char *cfgfile,
      56             :      const struct GNUNET_CONFIGURATION_Handle *config)
      57             : {
      58             :   struct TALER_MERCHANTDB_Plugin *plugin;
      59             :   struct GNUNET_CONFIGURATION_Handle *cfg;
      60             : 
      61          14 :   cfg = GNUNET_CONFIGURATION_dup (config);
      62          14 :   if (NULL ==
      63          14 :       (plugin = TALER_MERCHANTDB_plugin_load (cfg)))
      64             :   {
      65           0 :     fprintf (stderr,
      66             :              "Failed to initialize database plugin.\n");
      67           0 :     global_ret = 1;
      68           0 :     GNUNET_CONFIGURATION_destroy (cfg);
      69           0 :     return;
      70             :   }
      71          14 :   if (reset_db)
      72             :   {
      73          14 :     if (GNUNET_OK !=
      74          14 :         plugin->drop_tables (plugin->cls))
      75             :     {
      76           1 :       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
      77             :                   "Failed to reset the database\n");
      78             :     }
      79             :   }
      80          14 :   if (gc_db)
      81             :   {
      82           0 :     if (GNUNET_OK !=
      83           0 :         plugin->gc (plugin->cls))
      84             :     {
      85           0 :       global_ret = 1;
      86           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      87             :                   "Failed to garbage collect database\n");
      88             :     }
      89             :   }
      90             :   else
      91             :   {
      92          14 :     if (GNUNET_OK !=
      93          14 :         plugin->create_tables (plugin->cls))
      94             :     {
      95           0 :       global_ret = 1;
      96           0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      97             :                   "Failed to initialize tables\n");
      98             :     }
      99             :   }
     100          14 :   TALER_MERCHANTDB_plugin_unload (plugin);
     101          14 :   GNUNET_CONFIGURATION_destroy (cfg);
     102             : }
     103             : 
     104             : 
     105             : /**
     106             :  * The main function of the database initialization tool.
     107             :  * Used to initialize the Taler Exchange's database.
     108             :  *
     109             :  * @param argc number of arguments from the command line
     110             :  * @param argv command line arguments
     111             :  * @return 0 ok, 1 on error
     112             :  */
     113             : int
     114          14 : main (int argc,
     115             :       char *const *argv)
     116             : {
     117          14 :   struct GNUNET_GETOPT_CommandLineOption options[] = {
     118          14 :     GNUNET_GETOPT_option_flag ('g',
     119             :                                "gc",
     120             :                                "garbage collect database",
     121             :                                &gc_db),
     122          14 :     GNUNET_GETOPT_option_flag ('r',
     123             :                                "reset",
     124             :                                "reset database (DANGEROUS: all existing data is lost!)",
     125             :                                &reset_db),
     126             : 
     127          14 :     GNUNET_GETOPT_option_version (PACKAGE_VERSION "-" VCS_VERSION),
     128             :     GNUNET_GETOPT_OPTION_END
     129             :   };
     130             :   enum GNUNET_GenericReturnValue ret;
     131             : 
     132          14 :   ret = GNUNET_PROGRAM_run (
     133             :     TALER_MERCHANT_project_data (),
     134             :     argc, argv,
     135             :     "taler-merchant-dbinit",
     136             :     gettext_noop ("Initialize Taler merchant database"),
     137             :     options,
     138             :     &run, NULL);
     139          14 :   if (GNUNET_SYSERR == ret)
     140           0 :     return 3;
     141          14 :   if (GNUNET_NO == ret)
     142           0 :     return 0;
     143          14 :   return global_ret;
     144             : }
     145             : 
     146             : 
     147             : /* end of taler-merchant-dbinit.c */

Generated by: LCOV version 1.16