donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

donau-dbinit.c (3740B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2014-2022 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 donau-tools/donau-dbinit.c
     18  * @brief Create tables for the donau database.
     19  * @author Florian Dold
     20  * @author Christian Grothoff
     21  */
     22 #include "donau_config.h"
     23 #include <gnunet/gnunet_util_lib.h>
     24 #include "donaudb_lib.h"
     25 #include "donau_util.h"
     26 
     27 
     28 /* LSB-style exit status codes */
     29 #ifndef EXIT_INVALIDARGUMENT
     30 /**
     31  * Command-line arguments are invalid.
     32  * Restarting useless.
     33  */
     34 #define EXIT_INVALIDARGUMENT 2
     35 #endif
     36 
     37 #ifndef EXIT_NOPERMISSION
     38 /**
     39  * Permissions needed to run are not available.
     40  * Restarting useless.
     41  */
     42 #define EXIT_NOPERMISSION 4
     43 #endif
     44 
     45 #ifndef EXIT_NOTINSTALLED
     46 /**
     47  * Key resources are not installed.
     48  * Restarting useless.
     49  */
     50 #define EXIT_NOTINSTALLED 5
     51 #endif
     52 
     53 
     54 /**
     55  * Return value from main().
     56  */
     57 static int global_ret;
     58 
     59 /**
     60  * -r option: do full DB reset
     61  */
     62 static int reset_db;
     63 
     64 /**
     65  * Main function that will be run.
     66  *
     67  * @param cls closure
     68  * @param args remaining command-line arguments
     69  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
     70  * @param cfg configuration
     71  */
     72 static void
     73 run (void *cls,
     74      char *const *args,
     75      const char *cfgfile,
     76      const struct GNUNET_CONFIGURATION_Handle *cfg)
     77 {
     78   struct DONAUDB_Plugin *plugin;
     79 
     80   (void) cls;
     81   (void) args;
     82   (void) cfgfile;
     83   if (NULL ==
     84       (plugin = DONAUDB_plugin_load (cfg)))
     85   {
     86     fprintf (stderr,
     87              "Failed to initialize database plugin.\n");
     88     global_ret = EXIT_NOTINSTALLED;
     89     return;
     90   }
     91 
     92   if (reset_db)
     93   {
     94     if (GNUNET_OK !=
     95         plugin->drop_tables (plugin->cls))
     96     {
     97       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     98                   "Could not drop tables as requested. Either database was not yet initialized, or permission denied. Consult the logs. Will still try to create new tables.\n");
     99     }
    100   }
    101 
    102   if (GNUNET_OK !=
    103       plugin->create_tables (plugin->cls))
    104   {
    105     fprintf (stderr,
    106              "Failed to initialize database.\n");
    107     DONAUDB_plugin_unload (plugin);
    108     plugin = NULL;
    109     global_ret = EXIT_NOPERMISSION;
    110     return;
    111   }
    112 
    113   DONAUDB_plugin_unload (plugin);
    114   plugin = NULL;
    115 }
    116 
    117 
    118 /**
    119  * The main function of the database initialization tool.
    120  * Used to initialize the Taler Donau's database.
    121  *
    122  * @param argc number of arguments from the command line
    123  * @param argv command line arguments
    124  * @return 0 ok, non-zero on error
    125  */
    126 int
    127 main (int argc,
    128       char *const *argv)
    129 {
    130   const struct GNUNET_GETOPT_CommandLineOption options[] = {
    131     GNUNET_GETOPT_option_flag ('r',
    132                                "reset",
    133                                "reset database (DANGEROUS: all existing data is lost!)",
    134                                &reset_db),
    135     GNUNET_GETOPT_OPTION_END
    136   };
    137   enum GNUNET_GenericReturnValue ret;
    138 
    139   ret = GNUNET_PROGRAM_run (
    140     DONAU_project_data (),
    141     argc, argv,
    142     "donau-dbinit",
    143     gettext_noop ("Initialize Donau database"),
    144     options,
    145     &run, NULL);
    146   if (GNUNET_SYSERR == ret)
    147     return EXIT_INVALIDARGUMENT;
    148   if (GNUNET_NO == ret)
    149     return EXIT_SUCCESS;
    150   return global_ret;
    151 }
    152 
    153 
    154 /* end of donau-dbinit.c */