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