taler-auditor-dbinit.c (4483B)
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 auditor/taler-auditor-dbinit.c 18 * @brief Create tables for the auditor database. 19 * @author Florian Dold 20 * @author Marcello Stanisci 21 */ 22 #include "platform.h" 23 #include <gnunet/gnunet_util_lib.h> 24 #include "taler/taler_util.h" 25 #include "auditordb_lib.h" 26 #include "auditor-database/gc.h" 27 #include "auditor-database/drop_tables.h" 28 #include "auditor-database/preflight.h" 29 #include "auditor-database/create_tables.h" 30 31 32 /** 33 * Return value from main(). 34 */ 35 static int global_ret; 36 37 /** 38 * -r option: do restart audits 39 */ 40 static int restart_db; 41 42 /** 43 * -R option: do full DB reset 44 */ 45 static int reset_db; 46 47 /** 48 * -g option: garbage collect DB reset 49 */ 50 static int gc_db; 51 52 53 /** 54 * Main function that will be run. 55 * 56 * @param cls closure 57 * @param args remaining command-line arguments 58 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 59 * @param cfg configuration 60 */ 61 static void 62 run (void *cls, 63 char *const *args, 64 const char *cfgfile, 65 const struct GNUNET_CONFIGURATION_Handle *cfg) 66 { 67 struct TALER_AUDITORDB_PostgresContext *pg; 68 69 (void) cls; 70 (void) args; 71 (void) cfgfile; 72 if (NULL == 73 (pg = TALER_AUDITORDB_connect (cfg))) 74 { 75 fprintf (stderr, 76 "Failed to initialize database connection.\n"); 77 global_ret = EXIT_NOTINSTALLED; 78 return; 79 } 80 if (reset_db || restart_db) 81 { 82 /* '-R' subsumes '-r': dropping all tables also drops the 83 recoverable audit state that '-r' removes. */ 84 if (GNUNET_OK != 85 TALER_AUDITORDB_drop_tables (pg, 86 0 != reset_db)) 87 { 88 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 89 reset_db 90 ? "Failed to reset database\n" 91 : "Failed to restart audits\n"); 92 TALER_AUDITORDB_disconnect (pg); 93 global_ret = EXIT_FAILURE; 94 return; 95 } 96 } 97 if (GNUNET_OK != 98 TALER_AUDITORDB_create_tables (pg, 99 false, 100 0)) 101 { 102 fprintf (stderr, 103 "Failed to initialize database.\n"); 104 TALER_AUDITORDB_disconnect (pg); 105 global_ret = EXIT_NOPERMISSION; 106 return; 107 } 108 if (gc_db) 109 { 110 if (GNUNET_SYSERR == TALER_AUDITORDB_gc (pg)) 111 { 112 fprintf (stderr, 113 "Garbage collection failed!\n"); 114 global_ret = EXIT_FAILURE; 115 } 116 } 117 TALER_AUDITORDB_disconnect (pg); 118 } 119 120 121 /** 122 * The main function of the database initialization tool. 123 * Used to initialize the Taler auditor's database. 124 * 125 * @param argc number of arguments from the command line 126 * @param argv command line arguments 127 * @return 0 ok, 1 on error 128 */ 129 int 130 main (int argc, 131 char *const *argv) 132 { 133 const struct GNUNET_GETOPT_CommandLineOption options[] = { 134 GNUNET_GETOPT_option_flag ('r', 135 "restart", 136 "restart audits (DANGEROUS: all audits resume from scratch)", 137 &restart_db), 138 GNUNET_GETOPT_option_flag ('R', 139 "reset", 140 "reset database (DANGEROUS: all existing data is lost!)", 141 &reset_db), 142 GNUNET_GETOPT_option_flag ('g', 143 "gc", 144 "garbage collect database", 145 &gc_db), 146 GNUNET_GETOPT_OPTION_END 147 }; 148 enum GNUNET_GenericReturnValue ret; 149 150 ret = GNUNET_PROGRAM_run ( 151 TALER_AUDITOR_project_data (), 152 argc, argv, 153 "taler-auditor-dbinit", 154 gettext_noop ("Initialize Taler auditor database"), 155 options, 156 &run, NULL); 157 if (GNUNET_SYSERR == ret) 158 return EXIT_INVALIDARGUMENT; 159 if (GNUNET_NO == ret) 160 return EXIT_SUCCESS; 161 return global_ret; 162 } 163 164 165 /* end of taler-auditor-dbinit.c */