anastasis-dbinit.c (4095B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2019-2021, 2026 Anastasis SARL 4 5 Anastasis 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 Anastasis 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 Anastasis; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file stasis/anastasis-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 "anastasis_util_lib.h" 24 #include "anastasis_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 garbate collection 39 */ 40 static int gc_db; 41 42 /** 43 * Main function that will be run. 44 * 45 * @param cls closure 46 * @param args remaining command-line arguments 47 * @param cfgfile name of the configuration file used (for saving, can be NULL!) 48 * @param cfg configuration 49 */ 50 static void 51 run (void *cls, 52 char *const *args, 53 const char *cfgfile, 54 const struct GNUNET_CONFIGURATION_Handle *cfg) 55 { 56 if (GNUNET_OK != 57 ANASTASIS_DB_init_admin (cfg)) 58 { 59 fprintf (stderr, 60 "Failed to initialize database.\n"); 61 global_ret = EXIT_NOTINSTALLED; 62 return; 63 } 64 if (reset_db) 65 { 66 if (GNUNET_OK != 67 ANASTASIS_DB_drop_tables ()) 68 { 69 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 70 "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"); 71 } 72 } 73 if (GNUNET_OK != 74 ANASTASIS_DB_create_tables ()) 75 { 76 global_ret = EXIT_FAILURE; 77 ANASTASIS_DB_fini (); 78 return; 79 } 80 if (gc_db) 81 { 82 struct GNUNET_TIME_Absolute expire_backups; 83 struct GNUNET_TIME_Absolute expire_payments; 84 struct GNUNET_TIME_Absolute now; 85 86 now = GNUNET_TIME_absolute_get (); 87 expire_backups = GNUNET_TIME_absolute_subtract ( 88 now, 89 GNUNET_TIME_relative_multiply ( 90 GNUNET_TIME_UNIT_MONTHS, 91 6)); 92 expire_payments = GNUNET_TIME_absolute_subtract ( 93 now, 94 GNUNET_TIME_relative_multiply ( 95 GNUNET_TIME_UNIT_YEARS, 96 10)); 97 if (0 > ANASTASIS_DB_gc ( 98 expire_backups, 99 expire_payments)) 100 { 101 fprintf (stderr, 102 "Garbage collection failed!\n"); 103 } 104 } 105 ANASTASIS_DB_fini (); 106 } 107 108 109 /** 110 * The main function of the database initialization tool. 111 * Used to initialize the Anastasis' database. 112 * 113 * @param argc number of arguments from the command line 114 * @param argv command line arguments 115 * @return 0 ok, 1 on error 116 */ 117 int 118 main (int argc, 119 char *const *argv) 120 { 121 struct GNUNET_GETOPT_CommandLineOption options[] = { 122 GNUNET_GETOPT_option_flag ('g', 123 "garbagecollect", 124 "remove state data from database", 125 &gc_db), 126 GNUNET_GETOPT_option_flag ('r', 127 "reset", 128 "reset database (DANGEROUS: all existing data is lost!)", 129 &reset_db), 130 131 GNUNET_GETOPT_OPTION_END 132 }; 133 enum GNUNET_GenericReturnValue ret; 134 135 ret = GNUNET_PROGRAM_run (ANASTASIS_project_data (), 136 argc, argv, 137 "anastasis-dbinit", 138 "Initialize anastasis database", 139 options, 140 &run, NULL); 141 if (GNUNET_SYSERR == ret) 142 return EXIT_INVALIDARGUMENT; 143 if (GNUNET_NO == ret) 144 return EXIT_SUCCESS; 145 return global_ret; 146 } 147 148 149 /* end of anastasis-dbinit.c */