consolidate reclaim attribute lib
[oweals/gnunet.git] / src / include / gnunet_configuration_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2008, 2009, 2018 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 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * Configuration API
26  *
27  * @defgroup configuration  Configuration library
28  * Configuration management
29  * @{
30  */
31 #ifndef GNUNET_CONFIGURATION_LIB_H
32 #define GNUNET_CONFIGURATION_LIB_H
33
34 #include "gnunet_time_lib.h"
35
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #if 0                           /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44 /**
45  * A configuration object.
46  */
47 struct GNUNET_CONFIGURATION_Handle;
48
49 /**
50  * Create a new configuration object.
51  * @return fresh configuration object
52  */
53 struct GNUNET_CONFIGURATION_Handle *
54 GNUNET_CONFIGURATION_create (void);
55
56
57 /**
58  * Duplicate an existing configuration object.
59  *
60  * @param cfg configuration to duplicate
61  * @return duplicate configuration
62  */
63 struct GNUNET_CONFIGURATION_Handle *
64 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg);
65
66
67 /**
68  * Destroy configuration object.
69  *
70  * @param cfg configuration to destroy
71  */
72 void
73 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg);
74
75
76 /**
77  * Load configuration.  This function will first parse the
78  * defaults and then parse the specific configuration file
79  * to overwrite the defaults.
80  *
81  * @param cfg configuration to update
82  * @param filename name of the configuration file, NULL to load defaults
83  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
84  */
85 int
86 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
87                            const char *filename);
88
89
90 /**
91  * Load default configuration.  This function will parse the
92  * defaults from the given @a defaults_d directory.
93  *
94  * @param cfg configuration to update
95  * @param defaults_d directory with the defaults
96  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
97  */
98 int
99 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
100                                 const char *defaults_d);
101
102
103 /**
104  * Parse a configuration file, add all of the options in the
105  * file to the configuration environment.
106  *
107  * @param cfg configuration to update
108  * @param filename name of the configuration file
109  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
110  */
111 int
112 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
113                             const char *filename);
114
115
116 /**
117  * Serializes the given configuration.
118  *
119  * @param cfg configuration to serialize
120  * @param size will be set to the size of the serialized memory block
121  * @return the memory block where the serialized configuration is
122  *           present. This memory should be freed by the caller
123  */
124 char *
125 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
126                                 size_t *size);
127
128
129 /**
130  * De-serializes configuration
131  *
132  * @param cfg configuration to update
133  * @param mem the memory block of serialized configuration
134  * @param size the size of the memory block
135  * @param allow_inline set to the base directory if we recursively load configuration
136  *          from inlined configurations; NULL if not and raise warnings
137  *          when we come across them
138  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
139  */
140 int
141 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
142                                   const char *mem,
143                                   size_t size,
144                                   const char *basedir);
145
146
147 /**
148  * Write configuration file.
149  *
150  * @param cfg configuration to write
151  * @param filename where to write the configuration
152  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
153  */
154 int
155 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
156                             const char *filename);
157
158
159 /**
160  * Write only configuration entries that have been changed to configuration file
161  * @param cfg_default default configuration
162  * @param cfg_new new configuration
163  * @param filename where to write the configuration diff between default and new
164  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
165  */
166 int
167 GNUNET_CONFIGURATION_write_diffs (const struct
168                                   GNUNET_CONFIGURATION_Handle *cfg_default,
169                                   const struct
170                                   GNUNET_CONFIGURATION_Handle *cfg_new,
171                                   const char *filename);
172
173
174 /**
175  * Compute configuration with only entries that have been changed
176  *
177  * @param cfg_default original configuration
178  * @param cfg_new new configuration
179  * @return configuration with only the differences, never NULL
180  */
181 struct GNUNET_CONFIGURATION_Handle *
182 GNUNET_CONFIGURATION_get_diff (const struct
183                                GNUNET_CONFIGURATION_Handle *cfg_default,
184                                const struct
185                                GNUNET_CONFIGURATION_Handle *cfg_new);
186
187
188 /**
189  * Test if there are configuration options that were
190  * changed since the last save.
191  *
192  * @param cfg configuration to inspect
193  * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
194  */
195 int
196 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg);
197
198
199 /**
200  * Signature of a function to be run with a configuration.
201  *
202  * @param cls closure
203  * @param cfg the configuration
204  * @return status code
205  */
206 typedef int
207 (*GNUNET_CONFIGURATION_Callback)(void *cls,
208                                  const struct GNUNET_CONFIGURATION_Handle *cfg);
209
210
211 /**
212  * Parse a configuration file @a filename and run the function
213  * @a cb with the resulting configuration object. Then free the
214  * configuration object and return the status value from @a cb.
215  *
216  * @param filename configuration to parse, NULL for "default"
217  * @param cb function to run
218  * @param cb_cls closure for @a cb
219  * @return #GNUNET_SYSERR if parsing the configuration failed,
220  *   otherwise return value from @a cb.
221  */
222 int
223 GNUNET_CONFIGURATION_parse_and_run (const char *filename,
224                                     GNUNET_CONFIGURATION_Callback cb,
225                                     void *cb_cls);
226
227
228 /**
229  * Function to iterate over options.
230  *
231  * @param cls closure
232  * @param section name of the section
233  * @param option name of the option
234  * @param value value of the option
235  */
236 typedef void
237 (*GNUNET_CONFIGURATION_Iterator) (void *cls,
238                                   const char *section,
239                                   const char *option,
240                                   const char *value);
241
242
243 /**
244  * Function to iterate over section.
245  *
246  * @param cls closure
247  * @param section name of the section
248  */
249 typedef void
250 (*GNUNET_CONFIGURATION_Section_Iterator) (void *cls,
251                                           const char *section);
252
253
254 /**
255  * Iterate over all options in the configuration.
256  *
257  * @param cfg configuration to inspect
258  * @param iter function to call on each option
259  * @param iter_cls closure for @a iter
260  */
261 void
262 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
263                               GNUNET_CONFIGURATION_Iterator iter,
264                               void *iter_cls);
265
266
267 /**
268  * Iterate over all sections in the configuration.
269  *
270  * @param cfg configuration to inspect
271  * @param iter function to call on each section
272  * @param iter_cls closure for @a iter
273  */
274 void
275 GNUNET_CONFIGURATION_iterate_sections (const struct
276                                        GNUNET_CONFIGURATION_Handle *cfg,
277                                        GNUNET_CONFIGURATION_Section_Iterator
278                                        iter,
279                                        void *iter_cls);
280
281
282 /**
283  * Remove the given section and all options in it.
284  *
285  * @param cfg configuration to inspect
286  * @param section name of the section to remove
287  */
288 void
289 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
290                                      const char *section);
291
292
293 /**
294  * Get a configuration value that should be a number.
295  *
296  * @param cfg configuration to inspect
297  * @param section section of interest
298  * @param option option of interest
299  * @param number where to store the numeric value of the option
300  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
301  */
302 int
303 GNUNET_CONFIGURATION_get_value_number (const struct
304                                        GNUNET_CONFIGURATION_Handle *cfg,
305                                        const char *section,
306                                        const char *option,
307                                        unsigned long long *number);
308
309
310 /**
311  * Get a configuration value that should be a floating point number.
312  *
313  * @param cfg configuration to inspect
314  * @param section section of interest
315  * @param option option of interest
316  * @param number where to store the floating value of the option
317  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
318  */
319 int
320 GNUNET_CONFIGURATION_get_value_float (const struct
321                                       GNUNET_CONFIGURATION_Handle *cfg,
322                                       const char *section,
323                                       const char *option,
324                                       float *number);
325
326
327 /**
328  * Get a configuration value that should be a relative time.
329  *
330  * @param cfg configuration to inspect
331  * @param section section of interest
332  * @param option option of interest
333  * @param time set to the time value stored in the configuration
334  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
335  */
336 int
337 GNUNET_CONFIGURATION_get_value_time (const struct
338                                      GNUNET_CONFIGURATION_Handle *cfg,
339                                      const char *section,
340                                      const char *option,
341                                      struct GNUNET_TIME_Relative *time);
342
343
344 /**
345  * Get a configuration value that should be a size in bytes.
346  *
347  * @param cfg configuration to inspect
348  * @param section section of interest
349  * @param option option of interest
350  * @param size set to the size in bytes as stored in the configuration
351  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
352  */
353 int
354 GNUNET_CONFIGURATION_get_value_size (const struct
355                                      GNUNET_CONFIGURATION_Handle *cfg,
356                                      const char *section,
357                                      const char *option,
358                                      unsigned long long *size);
359
360
361 /**
362  * Test if we have a value for a particular option
363  *
364  * @param cfg configuration to inspect
365  * @param section section of interest
366  * @param option option of interest
367  * @return #GNUNET_YES if so, #GNUNET_NO if not.
368  */
369 int
370 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
371                                  const char *section,
372                                  const char *option);
373
374
375 /**
376  * Get a configuration value that should be a string.
377  *
378  * @param cfg configuration to inspect
379  * @param section section of interest
380  * @param option option of interest
381  * @param value will be set to a freshly allocated configuration
382  *        value, or NULL if option is not specified
383  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
384  */
385 int
386 GNUNET_CONFIGURATION_get_value_string (const struct
387                                        GNUNET_CONFIGURATION_Handle *cfg,
388                                        const char *section,
389                                        const char *option,
390                                        char **value);
391
392
393 /**
394  * Get a configuration value that should be the name of a file
395  * or directory.
396  *
397  * @param cfg configuration to inspect
398  * @param section section of interest
399  * @param option option of interest
400  * @param value will be set to a freshly allocated configuration
401  *        value, or NULL if option is not specified
402  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
403  */
404 int
405 GNUNET_CONFIGURATION_get_value_filename (const struct
406                                          GNUNET_CONFIGURATION_Handle *cfg,
407                                          const char *section,
408                                          const char *option,
409                                          char **value);
410
411
412 /**
413  * Iterate over the set of filenames stored in a configuration value.
414  *
415  * @param cfg configuration to inspect
416  * @param section section of interest
417  * @param option option of interest
418  * @param cb function to call on each filename
419  * @param cb_cls closure for @a cb
420  * @return number of filenames iterated over, -1 on error
421  */
422 int
423 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
424                                               GNUNET_CONFIGURATION_Handle *cfg,
425                                               const char *section,
426                                               const char *option,
427                                               GNUNET_FileNameCallback cb,
428                                               void *cb_cls);
429
430 /**
431  * Iterate over values of a section in the configuration.
432  *
433  * @param cfg configuration to inspect
434  * @param section the section
435  * @param iter function to call on each option
436  * @param iter_cls closure for @a iter
437  */
438 void
439 GNUNET_CONFIGURATION_iterate_section_values (const struct
440                                              GNUNET_CONFIGURATION_Handle *cfg,
441                                              const char *section,
442                                              GNUNET_CONFIGURATION_Iterator iter,
443                                              void *iter_cls);
444
445 /**
446  * Get a configuration value that should be in a set of
447  * predefined strings
448  *
449  * @param cfg configuration to inspect
450  * @param section section of interest
451  * @param option option of interest
452  * @param choices NULL-terminated list of legal values
453  * @param value will be set to an entry in the legal list,
454  *        or NULL if option is not specified and no default given
455  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
456  */
457 int
458 GNUNET_CONFIGURATION_get_value_choice (const struct
459                                        GNUNET_CONFIGURATION_Handle *cfg,
460                                        const char *section,
461                                        const char *option,
462                                        const char *const *choices,
463                                        const char **value);
464
465 /**
466  * Get a configuration value that should be in a set of
467  * "YES" or "NO".
468  *
469  * @param cfg configuration to inspect
470  * @param section section of interest
471  * @param option option of interest
472  * @return #GNUNET_YES, #GNUNET_NO or if option has no valid value, #GNUNET_SYSERR
473  */
474 int
475 GNUNET_CONFIGURATION_get_value_yesno (const struct
476                                       GNUNET_CONFIGURATION_Handle *cfg,
477                                       const char *section,
478                                       const char *option);
479
480
481 /**
482  * Get Crockford32-encoded fixed-size binary data from a configuration.
483  *
484  * @param cfg configuration to access
485  * @param section section to access
486  * @param option option to access
487  * @param buf where to store the decoded binary result
488  * @param buf_size exact number of bytes to store in @a buf
489  * @return #GNUNET_OK on success
490  *         #GNUNET_NO is the value does not exist
491  *         #GNUNET_SYSERR on decoding error
492  */
493 int
494 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
495                                const char *section,
496                                const char *option,
497                                void *buf,
498                                size_t buf_size);
499
500
501 /**
502  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
503  * where either in the "PATHS" section or the environtment "FOO" is
504  * set to "DIRECTORY".  We also support default expansion,
505  * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
506  * set in PATHS or the environment, and otherwise to "default".  Note
507  * that "default" itself can also be a $-expression, thus
508  * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
509  * to VAR2.
510  *
511  * @param cfg configuration to use for path expansion
512  * @param orig string to $-expand (will be freed!)  Note that multiple
513  *          $-expressions can be present in this string.  They will all be
514  *          $-expanded.
515  * @return $-expanded string
516  */
517 char *
518 GNUNET_CONFIGURATION_expand_dollar (const struct
519                                     GNUNET_CONFIGURATION_Handle *cfg,
520                                     char *orig);
521
522
523 /**
524  * Set a configuration value that should be a number.
525  *
526  * @param cfg configuration to update
527  * @param section section of interest
528  * @param option option of interest
529  * @param number value to set
530  */
531 void
532 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
533                                        const char *section,
534                                        const char *option,
535                                        unsigned long long number);
536
537
538 /**
539  * Set a configuration value that should be a string.
540  *
541  * @param cfg configuration to update
542  * @param section section of interest
543  * @param option option of interest
544  * @param value value to set
545  */
546 void
547 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
548                                        const char *section,
549                                        const char *option,
550                                        const char *value);
551
552
553 /**
554  * Remove a filename from a configuration value that
555  * represents a list of filenames
556  *
557  * @param cfg configuration to update
558  * @param section section of interest
559  * @param option option of interest
560  * @param value filename to remove
561  * @return #GNUNET_OK on success,
562  *         #GNUNET_SYSERR if the filename is not in the list
563  */
564 int
565 GNUNET_CONFIGURATION_remove_value_filename (struct
566                                             GNUNET_CONFIGURATION_Handle *cfg,
567                                             const char *section,
568                                             const char *option,
569                                             const char *value);
570
571
572 /**
573  * Append a filename to a configuration value that
574  * represents a list of filenames
575  *
576  * @param cfg configuration to update
577  * @param section section of interest
578  * @param option option of interest
579  * @param value filename to append
580  * @return #GNUNET_OK on success,
581  *         #GNUNET_SYSERR if the filename already in the list
582  */
583 int
584 GNUNET_CONFIGURATION_append_value_filename (struct
585                                             GNUNET_CONFIGURATION_Handle *cfg,
586                                             const char *section,
587                                             const char *option,
588                                             const char *value);
589
590 #if 0                           /* keep Emacsens' auto-indent happy */
591 {
592 #endif
593 #ifdef __cplusplus
594 }
595 #endif
596
597 #endif
598
599 /** @} */ /* end of group configuration */