exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

taler-auditor-dbinit.c (4266B)


      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 "taler/platform.h"
     23 #include <gnunet/gnunet_util_lib.h>
     24 #include "taler/taler_util.h"
     25 #include "taler/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 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   if (NULL ==
     69       (plugin = TALER_AUDITORDB_plugin_load (cfg,
     70                                              true)))
     71   {
     72     fprintf (stderr,
     73              "Failed to initialize database plugin.\n");
     74     global_ret = EXIT_NOTINSTALLED;
     75     return;
     76   }
     77   if (reset_db)
     78   {
     79     if (GNUNET_OK !=
     80         plugin->drop_tables (plugin->cls,
     81                              GNUNET_YES))
     82       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     83                   "Failed to reset database\n");
     84   }
     85   else if (restart_db)
     86   {
     87     if (GNUNET_OK !=
     88         plugin->drop_tables (plugin->cls,
     89                              GNUNET_NO))
     90       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     91                   "Failed to restart audits\n");
     92   }
     93   if (GNUNET_OK !=
     94       plugin->create_tables (plugin->cls,
     95                              false,
     96                              0))
     97   {
     98     fprintf (stderr,
     99              "Failed to initialize database.\n");
    100     TALER_AUDITORDB_plugin_unload (plugin);
    101     global_ret = EXIT_NOPERMISSION;
    102     return;
    103   }
    104   if (gc_db)
    105   {
    106     if (GNUNET_SYSERR == plugin->gc (plugin->cls))
    107       fprintf (stderr,
    108                "Garbage collection failed!\n");
    109   }
    110   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 main (int argc,
    124       char *const *argv)
    125 {
    126   const struct GNUNET_GETOPT_CommandLineOption options[] = {
    127     GNUNET_GETOPT_option_flag ('r',
    128                                "restart",
    129                                "restart audits (DANGEROUS: all audits resume from scratch)",
    130                                &restart_db),
    131     GNUNET_GETOPT_option_flag ('R',
    132                                "reset",
    133                                "reset database (DANGEROUS: all existing data is lost!)",
    134                                &reset_db),
    135     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   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   if (GNUNET_SYSERR == ret)
    151     return EXIT_INVALIDARGUMENT;
    152   if (GNUNET_NO == ret)
    153     return EXIT_SUCCESS;
    154   return global_ret;
    155 }
    156 
    157 
    158 /* end of taler-auditor-dbinit.c */