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 (4358B)


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