global reindent, now with uncrustify hook enabled
[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 /**
346  * Get a configuration value that should be a size in bytes.
347  *
348  * @param cfg configuration to inspect
349  * @param section section of interest
350  * @param option option of interest
351  * @param size set to the size in bytes as stored in the configuration
352  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
353  */
354 int
355 GNUNET_CONFIGURATION_get_value_size (const struct
356                                      GNUNET_CONFIGURATION_Handle *cfg,
357                                      const char *section,
358                                      const char *option,
359                                      unsigned long long *size);
360
361
362 /**
363  * Test if we have a value for a particular option
364  *
365  * @param cfg configuration to inspect
366  * @param section section of interest
367  * @param option option of interest
368  * @return #GNUNET_YES if so, #GNUNET_NO if not.
369  */
370 int
371 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
372                                  const char *section,
373                                  const char *option);
374
375
376 /**
377  * Get a configuration value that should be a string.
378  *
379  * @param cfg configuration to inspect
380  * @param section section of interest
381  * @param option option of interest
382  * @param value will be set to a freshly allocated configuration
383  *        value, or NULL if option is not specified
384  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
385  */
386 int
387 GNUNET_CONFIGURATION_get_value_string (const struct
388                                        GNUNET_CONFIGURATION_Handle *cfg,
389                                        const char *section,
390                                        const char *option,
391                                        char **value);
392
393
394 /**
395  * Get a configuration value that should be the name of a file
396  * or directory.
397  *
398  * @param cfg configuration to inspect
399  * @param section section of interest
400  * @param option option of interest
401  * @param value will be set to a freshly allocated configuration
402  *        value, or NULL if option is not specified
403  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
404  */
405 int
406 GNUNET_CONFIGURATION_get_value_filename (const struct
407                                          GNUNET_CONFIGURATION_Handle *cfg,
408                                          const char *section,
409                                          const char *option,
410                                          char **value);
411
412
413 /**
414  * Iterate over the set of filenames stored in a configuration value.
415  *
416  * @param cfg configuration to inspect
417  * @param section section of interest
418  * @param option option of interest
419  * @param cb function to call on each filename
420  * @param cb_cls closure for @a cb
421  * @return number of filenames iterated over, -1 on error
422  */
423 int
424 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
425                                               GNUNET_CONFIGURATION_Handle *cfg,
426                                               const char *section,
427                                               const char *option,
428                                               GNUNET_FileNameCallback cb,
429                                               void *cb_cls);
430
431 /**
432  * Iterate over values of a section in the configuration.
433  *
434  * @param cfg configuration to inspect
435  * @param section the section
436  * @param iter function to call on each option
437  * @param iter_cls closure for @a iter
438  */
439 void
440 GNUNET_CONFIGURATION_iterate_section_values (const struct
441                                              GNUNET_CONFIGURATION_Handle *cfg,
442                                              const char *section,
443                                              GNUNET_CONFIGURATION_Iterator iter,
444                                              void *iter_cls);
445
446 /**
447  * Get a configuration value that should be in a set of
448  * predefined strings
449  *
450  * @param cfg configuration to inspect
451  * @param section section of interest
452  * @param option option of interest
453  * @param choices NULL-terminated list of legal values
454  * @param value will be set to an entry in the legal list,
455  *        or NULL if option is not specified and no default given
456  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
457  */
458 int
459 GNUNET_CONFIGURATION_get_value_choice (const struct
460                                        GNUNET_CONFIGURATION_Handle *cfg,
461                                        const char *section,
462                                        const char *option,
463                                        const char *const *choices,
464                                        const char **value);
465
466 /**
467  * Get a configuration value that should be in a set of
468  * "YES" or "NO".
469  *
470  * @param cfg configuration to inspect
471  * @param section section of interest
472  * @param option option of interest
473  * @return #GNUNET_YES, #GNUNET_NO or if option has no valid value, #GNUNET_SYSERR
474  */
475 int
476 GNUNET_CONFIGURATION_get_value_yesno (const struct
477                                       GNUNET_CONFIGURATION_Handle *cfg,
478                                       const char *section,
479                                       const char *option);
480
481
482 /**
483  * Get Crockford32-encoded fixed-size binary data from a configuration.
484  *
485  * @param cfg configuration to access
486  * @param section section to access
487  * @param option option to access
488  * @param buf where to store the decoded binary result
489  * @param buf_size exact number of bytes to store in @a buf
490  * @return #GNUNET_OK on success
491  *         #GNUNET_NO is the value does not exist
492  *         #GNUNET_SYSERR on decoding error
493  */
494 int
495 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
496                                const char *section,
497                                const char *option,
498                                void *buf,
499                                size_t buf_size);
500
501
502 /**
503  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
504  * where either in the "PATHS" section or the environtment "FOO" is
505  * set to "DIRECTORY".  We also support default expansion,
506  * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
507  * set in PATHS or the environment, and otherwise to "default".  Note
508  * that "default" itself can also be a $-expression, thus
509  * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
510  * to VAR2.
511  *
512  * @param cfg configuration to use for path expansion
513  * @param orig string to $-expand (will be freed!)  Note that multiple
514  *          $-expressions can be present in this string.  They will all be
515  *          $-expanded.
516  * @return $-expanded string
517  */
518 char *
519 GNUNET_CONFIGURATION_expand_dollar (const struct
520                                     GNUNET_CONFIGURATION_Handle *cfg,
521                                     char *orig);
522
523
524 /**
525  * Set a configuration value that should be a number.
526  *
527  * @param cfg configuration to update
528  * @param section section of interest
529  * @param option option of interest
530  * @param number value to set
531  */
532 void
533 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
534                                        const char *section,
535                                        const char *option,
536                                        unsigned long long number);
537
538
539 /**
540  * Set a configuration value that should be a string.
541  *
542  * @param cfg configuration to update
543  * @param section section of interest
544  * @param option option of interest
545  * @param value value to set
546  */
547 void
548 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
549                                        const char *section,
550                                        const char *option,
551                                        const char *value);
552
553
554 /**
555  * Remove a filename from a configuration value that
556  * represents a list of filenames
557  *
558  * @param cfg configuration to update
559  * @param section section of interest
560  * @param option option of interest
561  * @param value filename to remove
562  * @return #GNUNET_OK on success,
563  *         #GNUNET_SYSERR if the filename is not in the list
564  */
565 int
566 GNUNET_CONFIGURATION_remove_value_filename (struct
567                                             GNUNET_CONFIGURATION_Handle *cfg,
568                                             const char *section,
569                                             const char *option,
570                                             const char *value);
571
572
573 /**
574  * Append a filename to a configuration value that
575  * represents a list of filenames
576  *
577  * @param cfg configuration to update
578  * @param section section of interest
579  * @param option option of interest
580  * @param value filename to append
581  * @return #GNUNET_OK on success,
582  *         #GNUNET_SYSERR if the filename already in the list
583  */
584 int
585 GNUNET_CONFIGURATION_append_value_filename (struct
586                                             GNUNET_CONFIGURATION_Handle *cfg,
587                                             const char *section,
588                                             const char *option,
589                                             const char *value);
590
591 #if 0                           /* keep Emacsens' auto-indent happy */
592 {
593 #endif
594 #ifdef __cplusplus
595 }
596 #endif
597
598 #endif
599
600 /** @} */ /* end of group configuration */