donau-dbinit.c (3739B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2014-2022 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 donau-tools/donau-dbinit.c 18 * @brief Create tables for the donau database. 19 * @author Florian Dold 20 * @author Christian Grothoff 21 */ 22 #include "donau_config.h" 23 #include <gnunet/gnunet_util_lib.h> 24 #include "donaudb_lib.h" 25 #include "donau-database/drop_tables.h" 26 #include "donau-database/create_tables.h" 27 #include "donau_util.h" 28 29 30 /* LSB-style exit status codes */ 31 #ifndef EXIT_INVALIDARGUMENT 32 /** 33 * Command-line arguments are invalid. 34 * Restarting useless. 35 */ 36 #define EXIT_INVALIDARGUMENT 2 37 #endif 38 39 #ifndef EXIT_NOPERMISSION 40 /** 41 * Permissions needed to run are not available. 42 * Restarting useless. 43 */ 44 #define EXIT_NOPERMISSION 4 45 #endif 46 47 #ifndef EXIT_NOTINSTALLED 48 /** 49 * Key resources are not installed. 50 * Restarting useless. 51 */ 52 #define EXIT_NOTINSTALLED 5 53 #endif 54 55 56 /** 57 * Return value from main(). 58 */ 59 static int global_ret; 60 61 /** 62 * -r option: do full DB reset 63 */ 64 static int reset_db; 65 66 /** 67 * Main function that will be run. 68 * 69 * @param cls closure 70 * @param args remaining command-line arguments 71 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 72 * @param cfg configuration 73 */ 74 static void 75 run (void *cls, 76 char *const *args, 77 const char *cfgfile, 78 const struct GNUNET_CONFIGURATION_Handle *cfg) 79 { 80 struct DONAUDB_PostgresContext *ctx; 81 82 (void) cls; 83 (void) args; 84 (void) cfgfile; 85 if (NULL == 86 (ctx = DONAUDB_connect (cfg))) 87 { 88 fprintf (stderr, 89 "Failed to initialize database ctx.\n"); 90 global_ret = EXIT_NOTINSTALLED; 91 return; 92 } 93 94 if (reset_db) 95 { 96 if (GNUNET_OK != 97 DONAUDB_drop_tables (ctx)) 98 { 99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 100 "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"); 101 } 102 } 103 104 if (GNUNET_OK != 105 DONAUDB_create_tables (ctx)) 106 { 107 fprintf (stderr, 108 "Failed to initialize database.\n"); 109 DONAUDB_disconnect (ctx); 110 global_ret = EXIT_NOPERMISSION; 111 return; 112 } 113 114 DONAUDB_disconnect (ctx); 115 } 116 117 118 /** 119 * The main function of the database initialization tool. 120 * Used to initialize the Taler Donau's database. 121 * 122 * @param argc number of arguments from the command line 123 * @param argv command line arguments 124 * @return 0 ok, non-zero on error 125 */ 126 int 127 main (int argc, 128 char *const *argv) 129 { 130 const struct GNUNET_GETOPT_CommandLineOption options[] = { 131 GNUNET_GETOPT_option_flag ('r', 132 "reset", 133 "reset database (DANGEROUS: all existing data is lost!)", 134 &reset_db), 135 GNUNET_GETOPT_OPTION_END 136 }; 137 enum GNUNET_GenericReturnValue ret; 138 139 ret = GNUNET_PROGRAM_run ( 140 DONAU_project_data (), 141 argc, argv, 142 "donau-dbinit", 143 "Initialize Donau database", 144 options, 145 &run, NULL); 146 if (GNUNET_SYSERR == ret) 147 return EXIT_INVALIDARGUMENT; 148 if (GNUNET_NO == ret) 149 return EXIT_SUCCESS; 150 return global_ret; 151 } 152 153 154 /* end of donau-dbinit.c */