diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 13:37:38 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 13:37:38 +0200 |
commit | 9ef4abad615bea12d13be542b8ae5fbeb2dfee32 (patch) | |
tree | 8875a687e004d331c9ea6a1d511a328c72b88113 /src/util/configuration_helper.c | |
parent | e95236b3ed78cd597c15f34b89385295702b627f (diff) | |
download | gnunet-9ef4abad615bea12d13be542b8ae5fbeb2dfee32.tar.gz gnunet-9ef4abad615bea12d13be542b8ae5fbeb2dfee32.zip |
NEWS: Refactoring components under src/ into lib/, plugin/, cli/ and service/
This also includes a necessary API refactoring of crypto from IDENTITY
to UTIL.
Diffstat (limited to 'src/util/configuration_helper.c')
-rw-r--r-- | src/util/configuration_helper.c | 303 |
1 files changed, 0 insertions, 303 deletions
diff --git a/src/util/configuration_helper.c b/src/util/configuration_helper.c deleted file mode 100644 index d4d5fc732..000000000 --- a/src/util/configuration_helper.c +++ /dev/null | |||
@@ -1,303 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2006, 2007, 2008, 2009, 2013, 2020, 2021 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file src/util/configuration_helper.c | ||
22 | * @brief helper logic for gnunet-config | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | |||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | |||
29 | /** | ||
30 | * Print each option in a given section as a filename. | ||
31 | * | ||
32 | * @param cls closure | ||
33 | * @param section name of the section | ||
34 | * @param option name of the option | ||
35 | * @param value value of the option | ||
36 | */ | ||
37 | static void | ||
38 | print_filename_option (void *cls, | ||
39 | const char *section, | ||
40 | const char *option, | ||
41 | const char *value) | ||
42 | { | ||
43 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; | ||
44 | |||
45 | char *value_fn; | ||
46 | char *fn; | ||
47 | |||
48 | GNUNET_assert (GNUNET_OK == | ||
49 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
50 | section, | ||
51 | option, | ||
52 | &value_fn)); | ||
53 | fn = GNUNET_STRINGS_filename_expand (value_fn); | ||
54 | if (NULL == fn) | ||
55 | fn = value_fn; | ||
56 | else | ||
57 | GNUNET_free (value_fn); | ||
58 | fprintf (stdout, | ||
59 | "%s = %s\n", | ||
60 | option, | ||
61 | fn); | ||
62 | GNUNET_free (fn); | ||
63 | } | ||
64 | |||
65 | |||
66 | /** | ||
67 | * Print each option in a given section. | ||
68 | * | ||
69 | * @param cls closure | ||
70 | * @param section name of the section | ||
71 | * @param option name of the option | ||
72 | * @param value value of the option | ||
73 | */ | ||
74 | static void | ||
75 | print_option (void *cls, | ||
76 | const char *section, | ||
77 | const char *option, | ||
78 | const char *value) | ||
79 | { | ||
80 | (void) cls; | ||
81 | (void) section; | ||
82 | |||
83 | fprintf (stdout, | ||
84 | "%s = %s\n", | ||
85 | option, | ||
86 | value); | ||
87 | } | ||
88 | |||
89 | |||
90 | /** | ||
91 | * Print out given section name. | ||
92 | * | ||
93 | * @param cls unused | ||
94 | * @param section a section in the configuration file | ||
95 | */ | ||
96 | static void | ||
97 | print_section_name (void *cls, | ||
98 | const char *section) | ||
99 | { | ||
100 | (void) cls; | ||
101 | fprintf (stdout, | ||
102 | "%s\n", | ||
103 | section); | ||
104 | } | ||
105 | |||
106 | |||
107 | void | ||
108 | GNUNET_CONFIGURATION_config_tool_run ( | ||
109 | void *cls, | ||
110 | char *const *args, | ||
111 | const char *cfgfile, | ||
112 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
113 | { | ||
114 | struct GNUNET_CONFIGURATION_ConfigSettings *cs = cls; | ||
115 | struct GNUNET_CONFIGURATION_Handle *out = NULL; | ||
116 | struct GNUNET_CONFIGURATION_Handle *ncfg = NULL; | ||
117 | |||
118 | (void) args; | ||
119 | if (cs->diagnostics) | ||
120 | { | ||
121 | /* Re-parse the configuration with diagnostics enabled. */ | ||
122 | ncfg = GNUNET_CONFIGURATION_create (); | ||
123 | GNUNET_CONFIGURATION_enable_diagnostics (ncfg); | ||
124 | GNUNET_CONFIGURATION_load (ncfg, | ||
125 | cfgfile); | ||
126 | cfg = ncfg; | ||
127 | } | ||
128 | |||
129 | if (cs->full) | ||
130 | cs->rewrite = GNUNET_YES; | ||
131 | if (cs->list_sections) | ||
132 | { | ||
133 | fprintf (stderr, | ||
134 | _ ("The following sections are available:\n")); | ||
135 | GNUNET_CONFIGURATION_iterate_sections (cfg, | ||
136 | &print_section_name, | ||
137 | NULL); | ||
138 | return; | ||
139 | } | ||
140 | if ( (! cs->rewrite) && | ||
141 | (NULL == cs->section) ) | ||
142 | { | ||
143 | char *serialization; | ||
144 | |||
145 | if (! cs->diagnostics) | ||
146 | { | ||
147 | fprintf (stderr, | ||
148 | _ ("%s, %s or %s argument is required\n"), | ||
149 | "--section", | ||
150 | "--list-sections", | ||
151 | "--diagnostics"); | ||
152 | cs->global_ret = EXIT_INVALIDARGUMENT; | ||
153 | return; | ||
154 | } | ||
155 | serialization = GNUNET_CONFIGURATION_serialize_diagnostics (cfg); | ||
156 | fprintf (stdout, | ||
157 | "%s", | ||
158 | serialization); | ||
159 | GNUNET_free (serialization); | ||
160 | } | ||
161 | else if ( (NULL != cs->section) && | ||
162 | (NULL == cs->value) ) | ||
163 | { | ||
164 | if (NULL == cs->option) | ||
165 | { | ||
166 | GNUNET_CONFIGURATION_iterate_section_values ( | ||
167 | cfg, | ||
168 | cs->section, | ||
169 | cs->is_filename | ||
170 | ? &print_filename_option | ||
171 | : &print_option, | ||
172 | (void *) cfg); | ||
173 | } | ||
174 | else | ||
175 | { | ||
176 | char *value; | ||
177 | |||
178 | if (cs->is_filename) | ||
179 | { | ||
180 | if (GNUNET_OK != | ||
181 | GNUNET_CONFIGURATION_get_value_filename (cfg, | ||
182 | cs->section, | ||
183 | cs->option, | ||
184 | &value)) | ||
185 | { | ||
186 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
187 | cs->section, | ||
188 | cs->option); | ||
189 | cs->global_ret = EXIT_NOTCONFIGURED; | ||
190 | return; | ||
191 | } | ||
192 | } | ||
193 | else | ||
194 | { | ||
195 | if (GNUNET_OK != | ||
196 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
197 | cs->section, | ||
198 | cs->option, | ||
199 | &value)) | ||
200 | { | ||
201 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
202 | cs->section, | ||
203 | cs->option); | ||
204 | cs->global_ret = EXIT_NOTCONFIGURED; | ||
205 | return; | ||
206 | } | ||
207 | } | ||
208 | fprintf (stdout, | ||
209 | "%s\n", | ||
210 | value); | ||
211 | GNUNET_free (value); | ||
212 | } | ||
213 | } | ||
214 | else if (NULL != cs->section) | ||
215 | { | ||
216 | if (NULL == cs->option) | ||
217 | { | ||
218 | fprintf (stderr, | ||
219 | _ ("--option argument required to set value\n")); | ||
220 | cs->global_ret = EXIT_INVALIDARGUMENT; | ||
221 | return; | ||
222 | } | ||
223 | out = GNUNET_CONFIGURATION_dup (cfg); | ||
224 | GNUNET_CONFIGURATION_set_value_string (out, | ||
225 | cs->section, | ||
226 | cs->option, | ||
227 | cs->value); | ||
228 | cs->rewrite = GNUNET_YES; | ||
229 | } | ||
230 | if (cs->rewrite) | ||
231 | { | ||
232 | char *cfg_fn = NULL; | ||
233 | |||
234 | if (NULL == out) | ||
235 | out = GNUNET_CONFIGURATION_dup (cfg); | ||
236 | |||
237 | if (NULL == cfgfile) | ||
238 | { | ||
239 | const char *xdg = getenv ("XDG_CONFIG_HOME"); | ||
240 | |||
241 | if (NULL != xdg) | ||
242 | GNUNET_asprintf (&cfg_fn, | ||
243 | "%s%s%s", | ||
244 | xdg, | ||
245 | DIR_SEPARATOR_STR, | ||
246 | GNUNET_OS_project_data_get ()->config_file); | ||
247 | else | ||
248 | cfg_fn = GNUNET_strdup ( | ||
249 | GNUNET_OS_project_data_get ()->user_config_file); | ||
250 | cfgfile = cfg_fn; | ||
251 | } | ||
252 | |||
253 | if (! cs->full) | ||
254 | { | ||
255 | struct GNUNET_CONFIGURATION_Handle *def; | ||
256 | |||
257 | def = GNUNET_CONFIGURATION_create (); | ||
258 | if (GNUNET_OK != | ||
259 | GNUNET_CONFIGURATION_load (def, | ||
260 | NULL)) | ||
261 | { | ||
262 | fprintf (stderr, | ||
263 | _ ("failed to load configuration defaults")); | ||
264 | cs->global_ret = 1; | ||
265 | GNUNET_CONFIGURATION_destroy (def); | ||
266 | GNUNET_CONFIGURATION_destroy (out); | ||
267 | GNUNET_free (cfg_fn); | ||
268 | return; | ||
269 | } | ||
270 | if (GNUNET_OK != | ||
271 | GNUNET_CONFIGURATION_write_diffs (def, | ||
272 | out, | ||
273 | cfgfile)) | ||
274 | cs->global_ret = 2; | ||
275 | GNUNET_CONFIGURATION_destroy (def); | ||
276 | } | ||
277 | else | ||
278 | { | ||
279 | if (GNUNET_OK != | ||
280 | GNUNET_CONFIGURATION_write (out, | ||
281 | cfgfile)) | ||
282 | cs->global_ret = 2; | ||
283 | } | ||
284 | GNUNET_free (cfg_fn); | ||
285 | } | ||
286 | if (NULL != out) | ||
287 | GNUNET_CONFIGURATION_destroy (out); | ||
288 | if (NULL != ncfg) | ||
289 | GNUNET_CONFIGURATION_destroy (ncfg); | ||
290 | } | ||
291 | |||
292 | |||
293 | void | ||
294 | GNUNET_CONFIGURATION_config_settings_free ( | ||
295 | struct GNUNET_CONFIGURATION_ConfigSettings *cs) | ||
296 | { | ||
297 | GNUNET_free (cs->option); | ||
298 | GNUNET_free (cs->section); | ||
299 | GNUNET_free (cs->value); | ||
300 | } | ||
301 | |||
302 | |||
303 | /* end of configuration_helper.c */ | ||