LCOV - code coverage report
Current view: top level - challengerdb - challenger-dbinit.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 58.1 % 31 18
Test Date: 2026-06-09 21:00:27 Functions: 100.0 % 2 2

            Line data    Source code
       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            3 : 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            3 :   if (NULL ==
      66            3 :       (db = CHALLENGERDB_connect (cfg,
      67              :                                   true)))
      68              :   {
      69            0 :     fprintf (stderr,
      70              :              "Failed to initialize database connection.\n");
      71            0 :     global_ret = EXIT_NOTINSTALLED;
      72            0 :     return;
      73              :   }
      74            3 :   if (reset_db)
      75              :   {
      76            3 :     if (GNUNET_OK != CHALLENGERDB_drop_tables (db))
      77              :     {
      78            1 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
      79              :                   "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");
      80              :     }
      81              :   }
      82            3 :   if (GNUNET_OK !=
      83            3 :       CHALLENGERDB_create_tables (db))
      84              :   {
      85            0 :     global_ret = EXIT_FAILURE;
      86            0 :     CHALLENGERDB_disconnect (db);
      87            0 :     return;
      88              :   }
      89            3 :   if (gc_db)
      90              :   {
      91              :     struct GNUNET_TIME_Absolute now;
      92              : 
      93            0 :     now = GNUNET_TIME_absolute_get ();
      94            0 :     if (0 >
      95            0 :         CHALLENGERDB_gc (db,
      96              :                          now))
      97              :     {
      98            0 :       fprintf (stderr,
      99              :                "Garbage collection failed!\n");
     100            0 :       global_ret = EXIT_FAILURE;
     101              :     }
     102              :   }
     103            3 :   CHALLENGERDB_disconnect (db);
     104              : }
     105              : 
     106              : 
     107              : /**
     108              :  * The main function of the database initialization tool.
     109              :  * Used to initialize the Challenger' database.
     110              :  *
     111              :  * @param argc number of arguments from the command line
     112              :  * @param argv command line arguments
     113              :  * @return 0 ok, non-zero on error
     114              :  */
     115              : int
     116            3 : main (int argc,
     117              :       char *const *argv)
     118              : {
     119            3 :   struct GNUNET_GETOPT_CommandLineOption options[] = {
     120            3 :     GNUNET_GETOPT_option_flag ('r',
     121              :                                "reset",
     122              :                                "reset database (DANGEROUS: all existing data is lost!)",
     123              :                                &reset_db),
     124            3 :     GNUNET_GETOPT_option_flag ('g',
     125              :                                "garbagecollect",
     126              :                                "remove state data from database",
     127              :                                &gc_db),
     128              :     GNUNET_GETOPT_OPTION_END
     129              :   };
     130              :   enum GNUNET_GenericReturnValue ret;
     131              : 
     132            3 :   ret = GNUNET_PROGRAM_run (CHALLENGER_project_data (),
     133              :                             argc, argv,
     134              :                             "challenger-dbinit",
     135              :                             "Initialize challenger database",
     136              :                             options,
     137              :                             &run, NULL);
     138            3 :   if (GNUNET_SYSERR == ret)
     139            0 :     return EXIT_INVALIDARGUMENT;
     140            3 :   if (GNUNET_NO == ret)
     141            0 :     return EXIT_SUCCESS;
     142            3 :   return global_ret;
     143              : }
     144              : 
     145              : 
     146              : /* end of challenger-dbinit.c */
        

Generated by: LCOV version 2.0-1