challenger-dbinit.c (3889B)
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 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 /** 44 * Main function that will be run. 45 * 46 * @param cls closure 47 * @param args remaining command-line arguments 48 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 49 * @param cfg configuration 50 */ 51 static void 52 run (void *cls, 53 char *const *args, 54 const char *cfgfile, 55 const struct GNUNET_CONFIGURATION_Handle *cfg) 56 { 57 struct CHALLENGER_DatabasePlugin *plugin; 58 59 (void) cls; 60 (void) args; 61 (void) cfgfile; 62 if (NULL == 63 (plugin = CHALLENGER_DB_plugin_load (cfg, 64 true))) 65 { 66 fprintf (stderr, 67 "Failed to initialize database plugin.\n"); 68 global_ret = EXIT_NOTINSTALLED; 69 return; 70 } 71 if (reset_db) 72 { 73 if (GNUNET_OK != plugin->drop_tables (plugin->cls)) 74 { 75 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 76 "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"); 77 } 78 } 79 if (GNUNET_OK != 80 plugin->create_tables (plugin->cls)) 81 { 82 global_ret = EXIT_FAILURE; 83 CHALLENGER_DB_plugin_unload (plugin); 84 return; 85 } 86 if (gc_db) 87 { 88 struct GNUNET_TIME_Absolute now; 89 90 now = GNUNET_TIME_absolute_get (); 91 if (0 > 92 plugin->gc (plugin->cls, 93 now)) 94 { 95 fprintf (stderr, 96 "Garbage collection failed!\n"); 97 global_ret = EXIT_FAILURE; 98 } 99 } 100 CHALLENGER_DB_plugin_unload (plugin); 101 } 102 103 104 /** 105 * The main function of the database initialization tool. 106 * Used to initialize the Challenger' database. 107 * 108 * @param argc number of arguments from the command line 109 * @param argv command line arguments 110 * @return 0 ok, non-zero on error 111 */ 112 int 113 main (int argc, 114 char *const *argv) 115 { 116 struct GNUNET_GETOPT_CommandLineOption options[] = { 117 GNUNET_GETOPT_option_flag ('r', 118 "reset", 119 "reset database (DANGEROUS: all existing data is lost!)", 120 &reset_db), 121 GNUNET_GETOPT_option_flag ('g', 122 "garbagecollect", 123 "remove state data from database", 124 &gc_db), 125 GNUNET_GETOPT_OPTION_END 126 }; 127 enum GNUNET_GenericReturnValue ret; 128 129 ret = GNUNET_PROGRAM_run (CHALLENGER_project_data (), 130 argc, argv, 131 "challenger-dbinit", 132 "Initialize challenger database", 133 options, 134 &run, NULL); 135 if (GNUNET_SYSERR == ret) 136 return EXIT_INVALIDARGUMENT; 137 if (GNUNET_NO == ret) 138 return EXIT_SUCCESS; 139 return global_ret; 140 } 141 142 143 /* end of challenger-dbinit.c */