challenger

OAuth 2.0-based authentication service that validates user can receive messages at a certain address
Log | Files | Refs | Submodules | README | LICENSE

challenger-dbinit.c (3945B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2023 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 challengerdb/challenger-dbinit.c
     18  * @brief Create tables for the challenger database.
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <gnunet/gnunet_util_lib.h>
     23 #include "challenger_util.h"
     24 #include "challenger_database_lib.h"
     25 #include "challenger-database/create_tables.h"
     26 #include "challenger-database/drop_tables.h"
     27 #include "challenger-database/gc.h"
     28 
     29 
     30 /**
     31  * Return value from main().
     32  */
     33 static int global_ret;
     34 
     35 /**
     36  * -r option: do full DB reset
     37  */
     38 static int reset_db;
     39 
     40 /**
     41  * -g option: do GC reset
     42  */
     43 static int gc_db;
     44 
     45 
     46 /**
     47  * Main function that will be run.
     48  *
     49  * @param cls closure
     50  * @param args remaining command-line arguments
     51  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
     52  * @param cfg configuration
     53  */
     54 static void
     55 run (void *cls,
     56      char *const *args,
     57      const char *cfgfile,
     58      const struct GNUNET_CONFIGURATION_Handle *cfg)
     59 {
     60   struct CHALLENGERDB_PostgresContext *db;
     61 
     62   (void) cls;
     63   (void) args;
     64   (void) cfgfile;
     65   if (NULL ==
     66       (db = CHALLENGERDB_connect_admin (cfg)))
     67   {
     68     fprintf (stderr,
     69              "Failed to initialize database connection.\n");
     70     global_ret = EXIT_NOTINSTALLED;
     71     return;
     72   }
     73   if (reset_db)
     74   {
     75     if (GNUNET_OK != CHALLENGERDB_drop_tables (db))
     76     {
     77       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     78                   "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");
     79     }
     80   }
     81   if (GNUNET_OK !=
     82       CHALLENGERDB_create_tables (db))
     83   {
     84     global_ret = EXIT_FAILURE;
     85     CHALLENGERDB_disconnect (db);
     86     return;
     87   }
     88   if (gc_db)
     89   {
     90     struct GNUNET_TIME_Absolute now;
     91 
     92     now = GNUNET_TIME_absolute_get ();
     93     if (0 >
     94         CHALLENGERDB_gc (db,
     95                          now))
     96     {
     97       fprintf (stderr,
     98                "Garbage collection failed!\n");
     99       global_ret = EXIT_FAILURE;
    100     }
    101   }
    102   CHALLENGERDB_disconnect (db);
    103 }
    104 
    105 
    106 /**
    107  * The main function of the database initialization tool.
    108  * Used to initialize the Challenger' database.
    109  *
    110  * @param argc number of arguments from the command line
    111  * @param argv command line arguments
    112  * @return 0 ok, non-zero on error
    113  */
    114 int
    115 main (int argc,
    116       char *const *argv)
    117 {
    118   struct GNUNET_GETOPT_CommandLineOption options[] = {
    119     GNUNET_GETOPT_option_flag ('r',
    120                                "reset",
    121                                "reset database (DANGEROUS: all existing data is lost!)",
    122                                &reset_db),
    123     GNUNET_GETOPT_option_flag ('g',
    124                                "garbagecollect",
    125                                "remove state data from database",
    126                                &gc_db),
    127     GNUNET_GETOPT_OPTION_END
    128   };
    129   enum GNUNET_GenericReturnValue ret;
    130 
    131   ret = GNUNET_PROGRAM_run (CHALLENGER_project_data (),
    132                             argc, argv,
    133                             "challenger-dbinit",
    134                             "Initialize challenger database",
    135                             options,
    136                             &run, NULL);
    137   if (GNUNET_SYSERR == ret)
    138     return EXIT_INVALIDARGUMENT;
    139   if (GNUNET_NO == ret)
    140     return EXIT_SUCCESS;
    141   return global_ret;
    142 }
    143 
    144 
    145 /* end of challenger-dbinit.c */