frosix-dbinit.c (4566B)
1 /* 2 This file is part of Frosix 3 Copyright (C) 2019-2021 SARL 4 5 Frosix is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Frosix 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 Frosix; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file frosixdb/frosix-dbinit.c 18 * @brief Create tables for the merchant database. 19 * @author Dennis Neufeld 20 * @author Dominik Meister 21 */ 22 #include "platform.h" 23 #include "frosix_database_lib.h" 24 #include "frosix_util_lib.h" 25 #include <gnunet/gnunet_time_lib.h> 26 #include <gnunet/gnunet_util_lib.h> 27 #include <taler/taler_util.h> 28 29 30 /** 31 * Return value from main(). 32 */ 33 static int global_ret; 34 35 /** 36 * -r option: do full DB reset 37 */ 38 static int reset_db; 39 40 /** 41 * -g option: do garbage collection 42 */ 43 static int gc_db; 44 45 /** 46 * Main function that will be run. 47 * 48 * @param cls closure 49 * @param args remaining command-line arguments 50 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 51 * @param cfg configuration 52 */ 53 static void 54 run (void *cls, 55 char *const *args, 56 const char *cfgfile, 57 const struct GNUNET_CONFIGURATION_Handle *cfg) 58 { 59 struct FROSIX_DatabasePlugin *plugin; 60 61 (void) cls; 62 (void) args; 63 (void) cfgfile; 64 65 if (NULL == 66 (plugin = FROSIX_DB_plugin_load (cfg))) 67 { 68 fprintf (stderr, 69 "Failed to initialize database plugin.\n"); 70 global_ret = EXIT_NOTINSTALLED; 71 return; 72 } 73 if (reset_db) 74 { 75 if (GNUNET_OK != plugin->drop_tables (plugin->cls)) 76 { 77 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 78 "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"); 79 } 80 } 81 if (GNUNET_OK != 82 plugin->create_tables (plugin->cls)) 83 { 84 global_ret = EXIT_FAILURE; 85 FROSIX_DB_plugin_unload (plugin); 86 return; 87 } 88 if (gc_db) 89 { 90 struct GNUNET_TIME_Absolute expire_backups; 91 struct GNUNET_TIME_Absolute expire_payments; 92 struct GNUNET_TIME_Absolute now; 93 94 now = GNUNET_TIME_absolute_get (); 95 expire_backups = GNUNET_TIME_absolute_subtract ( 96 now, 97 GNUNET_TIME_relative_multiply ( 98 GNUNET_TIME_UNIT_MONTHS, 99 6)); 100 expire_payments = GNUNET_TIME_absolute_subtract ( 101 now, 102 GNUNET_TIME_relative_multiply ( 103 GNUNET_TIME_UNIT_YEARS, 104 10)); 105 if (0 > plugin->gc (plugin->cls, 106 expire_backups, 107 expire_payments)) 108 { 109 fprintf (stderr, 110 "Garbage collection failed!\n"); 111 } 112 } 113 FROSIX_DB_plugin_unload (plugin); 114 } 115 116 117 /** 118 * The main function of the database initialization tool. 119 * Used to initialize the Anastasis' database. 120 * 121 * @param argc number of arguments from the command line 122 * @param argv command line arguments 123 * @return 0 ok, 1 on error 124 */ 125 int 126 main (int argc, 127 char *const *argv) 128 { 129 struct GNUNET_GETOPT_CommandLineOption options[] = { 130 GNUNET_GETOPT_option_flag ('g', 131 "garbagecollect", 132 "remove state data from database", 133 &gc_db), 134 GNUNET_GETOPT_option_flag ('r', 135 "reset", 136 "reset database (DANGEROUS: all existing data is lost!)", 137 &reset_db), 138 139 GNUNET_GETOPT_OPTION_END 140 }; 141 142 /* force linker to link against libtalerutil; if we do 143 not do this, the linker may "optimize" libtalerutil 144 away and skip #TALER_OS_init(), which we do need */ 145 (void) TALER_project_data_default (); 146 GNUNET_OS_init (FROSIX_project_data_default ()); 147 GNUNET_assert (GNUNET_OK == 148 GNUNET_log_setup ("frosix-dbinit", 149 "INFO", 150 NULL)); 151 if (GNUNET_OK != 152 GNUNET_PROGRAM_run (argc, argv, 153 "frosix-dbinit", 154 "Initialize frosix database", 155 options, 156 &run, NULL)) 157 return 1; 158 return global_ret; 159 } 160 161 162 /* end of frosix-dbinit.c */