taler-merchant-dbinit.c (3828B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2014, 2015, 2020 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 merchant-tools/taler-merchant-dbinit.c 18 * @brief Create tables for the merchant database. 19 * @author Florian Dold 20 * @author Marcello Stanisci 21 */ 22 #include "platform.h" 23 #include <taler/taler_util.h> 24 #include <gnunet/gnunet_util_lib.h> 25 #include "taler_merchant_util.h" 26 #include "taler_merchantdb_lib.h" 27 28 29 /** 30 * Return value from main(). 31 */ 32 static int global_ret; 33 34 /** 35 * -r option: do full DB reset 36 */ 37 static int reset_db; 38 39 /** 40 * -g option: garbage collect DB 41 */ 42 static int gc_db; 43 44 /** 45 * Main function that will be run. 46 * 47 * @param cls closure 48 * @param args remaining command-line arguments 49 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 50 * @param config configuration 51 */ 52 static void 53 run (void *cls, 54 char *const *args, 55 const char *cfgfile, 56 const struct GNUNET_CONFIGURATION_Handle *config) 57 { 58 struct TALER_MERCHANTDB_Plugin *plugin; 59 struct GNUNET_CONFIGURATION_Handle *cfg; 60 61 cfg = GNUNET_CONFIGURATION_dup (config); 62 if (NULL == 63 (plugin = TALER_MERCHANTDB_plugin_load (cfg))) 64 { 65 fprintf (stderr, 66 "Failed to initialize database plugin.\n"); 67 global_ret = 1; 68 GNUNET_CONFIGURATION_destroy (cfg); 69 return; 70 } 71 if (reset_db) 72 { 73 if (GNUNET_OK != 74 plugin->drop_tables (plugin->cls)) 75 { 76 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 77 "Failed to reset the database\n"); 78 } 79 } 80 if (gc_db) 81 { 82 if (GNUNET_OK != 83 plugin->gc (plugin->cls)) 84 { 85 global_ret = 1; 86 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 87 "Failed to garbage collect database\n"); 88 } 89 } 90 else 91 { 92 if (GNUNET_OK != 93 plugin->create_tables (plugin->cls)) 94 { 95 global_ret = 1; 96 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 97 "Failed to initialize tables\n"); 98 } 99 } 100 TALER_MERCHANTDB_plugin_unload (plugin); 101 GNUNET_CONFIGURATION_destroy (cfg); 102 } 103 104 105 /** 106 * The main function of the database initialization tool. 107 * Used to initialize the Taler Exchange's database. 108 * 109 * @param argc number of arguments from the command line 110 * @param argv command line arguments 111 * @return 0 ok, 1 on error 112 */ 113 int 114 main (int argc, 115 char *const *argv) 116 { 117 struct GNUNET_GETOPT_CommandLineOption options[] = { 118 GNUNET_GETOPT_option_flag ('g', 119 "gc", 120 "garbage collect database", 121 &gc_db), 122 GNUNET_GETOPT_option_flag ('r', 123 "reset", 124 "reset database (DANGEROUS: all existing data is lost!)", 125 &reset_db), 126 127 GNUNET_GETOPT_option_version (PACKAGE_VERSION "-" VCS_VERSION), 128 GNUNET_GETOPT_OPTION_END 129 }; 130 enum GNUNET_GenericReturnValue ret; 131 132 ret = GNUNET_PROGRAM_run ( 133 TALER_MERCHANT_project_data (), 134 argc, argv, 135 "taler-merchant-dbinit", 136 gettext_noop ("Initialize Taler merchant database"), 137 options, 138 &run, NULL); 139 if (GNUNET_SYSERR == ret) 140 return 3; 141 if (GNUNET_NO == ret) 142 return 0; 143 return global_ret; 144 } 145 146 147 /* end of taler-merchant-dbinit.c */