typos
[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 GNUNET_CONFIGURATION_Handle *cfg_default,
168                                   const struct GNUNET_CONFIGURATION_Handle *cfg_new,
169                                   const char *filename);
170
171
172 /**
173  * Compute configuration with only entries that have been changed
174  *
175  * @param cfg_default original configuration
176  * @param cfg_new new configuration
177  * @return configuration with only the differences, never NULL
178  */
179 struct GNUNET_CONFIGURATION_Handle *
180 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
181                                const struct GNUNET_CONFIGURATION_Handle *cfg_new);
182
183
184 /**
185  * Test if there are configuration options that were
186  * changed since the last save.
187  *
188  * @param cfg configuration to inspect
189  * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
190  */
191 int
192 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg);
193
194
195 /**
196  * Signature of a function to be run with a configuration.
197  *
198  * @param cls closure
199  * @param cfg the configuration
200  * @return status code
201  */
202 typedef int
203 (*GNUNET_CONFIGURATION_Callback)(void *cls,
204                                  const struct GNUNET_CONFIGURATION_Handle *cfg);
205
206
207 /**
208  * Parse a configuration file @a filename and run the function
209  * @a cb with the resulting configuration object. Then free the
210  * configuration object and return the status value from @a cb.
211  *
212  * @param filename configuration to parse, NULL for "default"
213  * @param cb function to run
214  * @param cb_cls closure for @a cb
215  * @return #GNUNET_SYSERR if parsing the configuration failed,
216  *   otherwise return value from @a cb.
217  */
218 int
219 GNUNET_CONFIGURATION_parse_and_run (const char *filename,
220                                     GNUNET_CONFIGURATION_Callback cb,
221                                     void *cb_cls);
222
223
224 /**
225  * Function to iterate over options.
226  *
227  * @param cls closure
228  * @param section name of the section
229  * @param option name of the option
230  * @param value value of the option
231  */
232 typedef void
233 (*GNUNET_CONFIGURATION_Iterator) (void *cls,
234                                   const char *section,
235                                   const char *option,
236                                   const char *value);
237
238
239 /**
240  * Function to iterate over section.
241  *
242  * @param cls closure
243  * @param section name of the section
244  */
245 typedef void
246 (*GNUNET_CONFIGURATION_Section_Iterator) (void *cls,
247                                           const char *section);
248
249
250 /**
251  * Iterate over all options in the configuration.
252  *
253  * @param cfg configuration to inspect
254  * @param iter function to call on each option
255  * @param iter_cls closure for @a iter
256  */
257 void
258 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
259                               GNUNET_CONFIGURATION_Iterator iter,
260                               void *iter_cls);
261
262
263 /**
264  * Iterate over all sections in the configuration.
265  *
266  * @param cfg configuration to inspect
267  * @param iter function to call on each section
268  * @param iter_cls closure for @a iter
269  */
270 void
271 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
272                                        GNUNET_CONFIGURATION_Section_Iterator iter,
273                                        void *iter_cls);
274
275
276 /**
277  * Remove the given section and all options in it.
278  *
279  * @param cfg configuration to inspect
280  * @param section name of the section to remove
281  */
282 void
283 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
284                                      const char *section);
285
286
287 /**
288  * Get a configuration value that should be a number.
289  *
290  * @param cfg configuration to inspect
291  * @param section section of interest
292  * @param option option of interest
293  * @param number where to store the numeric value of the option
294  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
295  */
296 int
297 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle *cfg,
298                                        const char *section,
299                                        const char *option,
300                                        unsigned long long *number);
301
302
303 /**
304  * Get a configuration value that should be a floating point number.
305  *
306  * @param cfg configuration to inspect
307  * @param section section of interest
308  * @param option option of interest
309  * @param number where to store the floating value of the option
310  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
311  */
312 int
313 GNUNET_CONFIGURATION_get_value_float  (const struct GNUNET_CONFIGURATION_Handle *cfg,
314                                        const char *section,
315                                        const char *option,
316                                        float *number);
317
318
319 /**
320  * Get a configuration value that should be a relative time.
321  *
322  * @param cfg configuration to inspect
323  * @param section section of interest
324  * @param option option of interest
325  * @param time set to the time value stored in the configuration
326  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
327  */
328 int
329 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
330                                      const char *section,
331                                      const char *option,
332                                      struct GNUNET_TIME_Relative *time);
333
334
335
336 /**
337  * Get a configuration value that should be a size in bytes.
338  *
339  * @param cfg configuration to inspect
340  * @param section section of interest
341  * @param option option of interest
342  * @param size set to the size in bytes as stored in the configuration
343  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
344  */
345 int
346 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
347                                      const char *section,
348                                      const char *option,
349                                      unsigned long long *size);
350
351
352 /**
353  * Test if we have a value for a particular option
354  *
355  * @param cfg configuration to inspect
356  * @param section section of interest
357  * @param option option of interest
358  * @return #GNUNET_YES if so, #GNUNET_NO if not.
359  */
360 int
361 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
362                                  const char *section,
363                                  const char *option);
364
365
366 /**
367  * Get a configuration value that should be a string.
368  *
369  * @param cfg configuration to inspect
370  * @param section section of interest
371  * @param option option of interest
372  * @param value will be set to a freshly allocated configuration
373  *        value, or NULL if option is not specified
374  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
375  */
376 int
377 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
378                                        const char *section,
379                                        const char *option,
380                                        char **value);
381
382
383 /**
384  * Get a configuration value that should be the name of a file
385  * or directory.
386  *
387  * @param cfg configuration to inspect
388  * @param section section of interest
389  * @param option option of interest
390  * @param value will be set to a freshly allocated configuration
391  *        value, or NULL if option is not specified
392  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
393  */
394 int
395 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
396                                          const char *section,
397                                          const char *option,
398                                          char **value);
399
400
401 /**
402  * Iterate over the set of filenames stored in a configuration value.
403  *
404  * @param cfg configuration to inspect
405  * @param section section of interest
406  * @param option option of interest
407  * @param cb function to call on each filename
408  * @param cb_cls closure for @a cb
409  * @return number of filenames iterated over, -1 on error
410  */
411 int
412 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
413                                               const char *section,
414                                               const char *option,
415                                               GNUNET_FileNameCallback cb,
416                                               void *cb_cls);
417
418 /**
419  * Iterate over values of a section in the configuration.
420  *
421  * @param cfg configuration to inspect
422  * @param section the section
423  * @param iter function to call on each option
424  * @param iter_cls closure for @a iter
425  */
426 void
427 GNUNET_CONFIGURATION_iterate_section_values (const struct GNUNET_CONFIGURATION_Handle *cfg,
428                                              const char *section,
429                                              GNUNET_CONFIGURATION_Iterator iter,
430                                              void *iter_cls);
431
432 /**
433  * Get a configuration value that should be in a set of
434  * predefined strings
435  *
436  * @param cfg configuration to inspect
437  * @param section section of interest
438  * @param option option of interest
439  * @param choices NULL-terminated list of legal values
440  * @param value will be set to an entry in the legal list,
441  *        or NULL if option is not specified and no default given
442  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
443  */
444 int
445 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
446                                        const char *section,
447                                        const char *option,
448                                        const char *const *choices,
449                                        const char **value);
450
451 /**
452  * Get a configuration value that should be in a set of
453  * "YES" or "NO".
454  *
455  * @param cfg configuration to inspect
456  * @param section section of interest
457  * @param option option of interest
458  * @return #GNUNET_YES, #GNUNET_NO or if option has no valid value, #GNUNET_SYSERR
459  */
460 int
461 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
462                                       const char *section,
463                                       const char *option);
464
465
466 /**
467  * Get Crockford32-encoded fixed-size binary data from a configuration.
468  *
469  * @param cfg configuration to access
470  * @param section section to access
471  * @param option option to access
472  * @param buf where to store the decoded binary result
473  * @param buf_size exact number of bytes to store in @a buf
474  * @return #GNUNET_OK on success
475  *         #GNUNET_NO is the value does not exist
476  *         #GNUNET_SYSERR on decoding error
477  */
478 int
479 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
480                                const char *section,
481                                const char *option,
482                                void *buf,
483                                size_t buf_size);
484
485
486 /**
487  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
488  * where either in the "PATHS" section or the environtment "FOO" is
489  * set to "DIRECTORY".  We also support default expansion,
490  * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
491  * set in PATHS or the environment, and otherwise to "default".  Note
492  * that "default" itself can also be a $-expression, thus
493  * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
494  * to VAR2.
495  *
496  * @param cfg configuration to use for path expansion
497  * @param orig string to $-expand (will be freed!)  Note that multiple
498  *          $-expressions can be present in this string.  They will all be
499  *          $-expanded.
500  * @return $-expanded string
501  */
502 char *
503 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
504                                     char *orig);
505
506
507 /**
508  * Set a configuration value that should be a number.
509  *
510  * @param cfg configuration to update
511  * @param section section of interest
512  * @param option option of interest
513  * @param number value to set
514  */
515 void
516 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
517                                        const char *section,
518                                        const char *option,
519                                        unsigned long long number);
520
521
522 /**
523  * Set a configuration value that should be a string.
524  *
525  * @param cfg configuration to update
526  * @param section section of interest
527  * @param option option of interest
528  * @param value value to set
529  */
530 void
531 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
532                                        const char *section,
533                                        const char *option,
534                                        const char *value);
535
536
537 /**
538  * Remove a filename from a configuration value that
539  * represents a list of filenames
540  *
541  * @param cfg configuration to update
542  * @param section section of interest
543  * @param option option of interest
544  * @param value filename to remove
545  * @return #GNUNET_OK on success,
546  *         #GNUNET_SYSERR if the filename is not in the list
547  */
548 int
549 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
550                                             const char *section,
551                                             const char *option,
552                                             const char *value);
553
554
555 /**
556  * Append a filename to a configuration value that
557  * represents a list of filenames
558  *
559  * @param cfg configuration to update
560  * @param section section of interest
561  * @param option option of interest
562  * @param value filename to append
563  * @return #GNUNET_OK on success,
564  *         #GNUNET_SYSERR if the filename already in the list
565  */
566 int
567 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
568                                             const char *section,
569                                             const char *option,
570                                             const char *value);
571
572 #if 0                           /* keep Emacsens' auto-indent happy */
573 {
574 #endif
575 #ifdef __cplusplus
576 }
577 #endif
578
579 #endif
580
581 /** @} */ /* end of group configuration */