LCOV - code coverage report
Current view: top level - syncdb - sync-dbinit.c (source / functions) Hit Total Coverage
Test: GNU Taler sync coverage report Lines: 19 26 73.1 %
Date: 2021-08-30 06:54:48 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2019 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 util/sync-dbinit.c
      18             :  * @brief Create tables for the sync database.
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include <gnunet/gnunet_util_lib.h>
      23             : #include "sync_util.h"
      24             : #include "sync_database_lib.h"
      25             : 
      26             : 
      27             : /**
      28             :  * Return value from main().
      29             :  */
      30             : static int global_ret;
      31             : 
      32             : /**
      33             :  * -r option: do full DB reset
      34             :  */
      35             : static int reset_db;
      36             : 
      37             : /**
      38             :  * -g option: do GC reset
      39             :  */
      40             : static int gc_db;
      41             : 
      42             : /**
      43             :  * Main function that will be run.
      44             :  *
      45             :  * @param cls closure
      46             :  * @param args remaining command-line arguments
      47             :  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
      48             :  * @param cfg configuration
      49             :  */
      50             : static void
      51           1 : run (void *cls,
      52             :      char *const *args,
      53             :      const char *cfgfile,
      54             :      const struct GNUNET_CONFIGURATION_Handle *cfg)
      55             : {
      56             :   struct SYNC_DatabasePlugin *plugin;
      57             : 
      58           1 :   if (NULL ==
      59           1 :       (plugin = SYNC_DB_plugin_load (cfg)))
      60             :   {
      61           0 :     fprintf (stderr,
      62             :              "Failed to initialize database plugin.\n");
      63           0 :     global_ret = 1;
      64           0 :     return;
      65             :   }
      66           1 :   if (reset_db)
      67             :   {
      68           1 :     (void) plugin->drop_tables (plugin->cls);
      69           1 :     SYNC_DB_plugin_unload (plugin);
      70           1 :     plugin = SYNC_DB_plugin_load (cfg);
      71             :   }
      72           1 :   if (gc_db)
      73             :   {
      74             :     struct GNUNET_TIME_Absolute now;
      75             :     struct GNUNET_TIME_Absolute ancient;
      76             : 
      77           0 :     now = GNUNET_TIME_absolute_get ();
      78           0 :     ancient = GNUNET_TIME_absolute_subtract (now,
      79             :                                              GNUNET_TIME_relative_multiply (
      80             :                                                GNUNET_TIME_UNIT_YEARS,
      81             :                                                6));
      82           0 :     plugin->gc (plugin->cls,
      83             :                 now,
      84             :                 ancient);
      85             :   }
      86           1 :   SYNC_DB_plugin_unload (plugin);
      87             : }
      88             : 
      89             : 
      90             : /**
      91             :  * The main function of the database initialization tool.
      92             :  * Used to initialize the Sync' database.
      93             :  *
      94             :  * @param argc number of arguments from the command line
      95             :  * @param argv command line arguments
      96             :  * @return 0 ok, 1 on error
      97             :  */
      98             : int
      99           1 : main (int argc,
     100             :       char *const *argv)
     101             : {
     102           1 :   struct GNUNET_GETOPT_CommandLineOption options[] = {
     103           1 :     GNUNET_GETOPT_option_flag ('r',
     104             :                                "reset",
     105             :                                "reset database (DANGEROUS: all existing data is lost!)",
     106             :                                &reset_db),
     107           1 :     GNUNET_GETOPT_option_flag ('g',
     108             :                                "garbagecollect",
     109             :                                "remove state data from database",
     110             :                                &gc_db),
     111             :     GNUNET_GETOPT_OPTION_END
     112             :   };
     113             : 
     114             :   /* FIRST get the libtalerutil initialization out
     115             :      of the way. Then throw that one away, and force
     116             :      the SYNC defaults to be used! */
     117           1 :   (void) TALER_project_data_default ();
     118           1 :   GNUNET_OS_init (SYNC_project_data_default ());
     119           1 :   GNUNET_assert (GNUNET_OK ==
     120             :                  GNUNET_log_setup ("sync-dbinit",
     121             :                                    "INFO",
     122             :                                    NULL));
     123           1 :   if (GNUNET_OK !=
     124           1 :       GNUNET_PROGRAM_run (argc, argv,
     125             :                           "sync-dbinit",
     126             :                           "Initialize sync database",
     127             :                           options,
     128             :                           &run, NULL))
     129           0 :     return 1;
     130           1 :   return global_ret;
     131             : }
     132             : 
     133             : 
     134             : /* end of sync-dbinit.c */

Generated by: LCOV version 1.14