2 This file is part of GNUnet.
3 Copyright (C) 2006, 2007, 2008, 2009, 2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file src/util/configuration.c
23 * @brief configuration management
24 * @author Christian Grothoff
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_strings_lib.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_disk_lib.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
38 * @brief configuration entry
44 * This is a linked list.
46 struct ConfigEntry *next;
54 * current, commited value
61 * @brief configuration section
66 * This is a linked list.
68 struct ConfigSection *next;
71 * entries in the section
73 struct ConfigEntry *entries;
83 * @brief configuration data
85 struct GNUNET_CONFIGURATION_Handle
88 * Configuration sections.
90 struct ConfigSection *sections;
93 * Modification indication since last save
94 * #GNUNET_NO if clean, #GNUNET_YES if dirty,
95 * #GNUNET_SYSERR on error (i.e. last save failed)
103 * Used for diffing a configuration object against
108 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
110 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
115 * Create a GNUNET_CONFIGURATION_Handle.
117 * @return fresh configuration object
119 struct GNUNET_CONFIGURATION_Handle *
120 GNUNET_CONFIGURATION_create ()
122 return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
127 * Destroy configuration object.
129 * @param cfg configuration to destroy
132 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
134 struct ConfigSection *sec;
136 while (NULL != (sec = cfg->sections))
137 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
143 * Parse a configuration file @a filename and run the function
144 * @a cb with the resulting configuration object. Then free the
145 * configuration object and return the status value from @a cb.
147 * @param filename configuration to parse, NULL for "default"
148 * @param cb function to run
149 * @param cb_cls closure for @a cb
150 * @return #GNUNET_SYSERR if parsing the configuration failed,
151 * otherwise return value from @a cb.
154 GNUNET_CONFIGURATION_parse_and_run (const char *filename,
155 GNUNET_CONFIGURATION_Callback cb,
158 struct GNUNET_CONFIGURATION_Handle *cfg;
161 cfg = GNUNET_CONFIGURATION_create ();
163 GNUNET_CONFIGURATION_load (cfg,
167 GNUNET_CONFIGURATION_destroy (cfg);
168 return GNUNET_SYSERR;
172 GNUNET_CONFIGURATION_destroy (cfg);
178 * De-serializes configuration
180 * @param cfg configuration to update
181 * @param mem the memory block of serialized configuration
182 * @param size the size of the memory block
183 * @param basedir set to path from which we recursively load configuration
184 * from inlined configurations; NULL if not and raise warnings
185 * when we come across them
186 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
189 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
210 section = GNUNET_strdup ("");
214 while (r_bytes < size)
216 GNUNET_free_non_null (line_orig);
217 /* fgets-like behaviour on buffer */
218 to_read = size - r_bytes;
219 pos = memchr (&mem[r_bytes], '\n', to_read);
222 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
223 r_bytes += line_size;
227 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
228 r_bytes += line_size + 1;
231 /* increment line number */
233 /* tabs and '\r' are whitespace */
234 emptyline = GNUNET_YES;
235 for (i = 0; i < line_size; i++)
242 emptyline = GNUNET_NO;
244 /* ignore empty lines */
245 if (GNUNET_YES == emptyline)
248 /* remove tailing whitespace */
249 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
252 /* remove leading whitespace */
253 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
255 /* ignore comments */
256 if ( ('#' == line[0]) || ('%' == line[0]) )
259 /* handle special "@INLINE@" directive */
260 if (0 == strncasecmp (line,
262 strlen ("@INLINE@ ")))
265 value = &line[strlen ("@INLINE@ ")];
270 GNUNET_asprintf (&fn,
275 GNUNET_CONFIGURATION_parse (cfg,
279 ret = GNUNET_SYSERR; /* failed to parse included config */
286 LOG (GNUNET_ERROR_TYPE_DEBUG,
287 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
293 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
296 line[line_size - 1] = '\0';
298 GNUNET_free (section);
299 section = GNUNET_strdup (value);
302 if (NULL != (eq = strchr (line, '=')))
305 tag = GNUNET_strndup (line, eq - line);
306 /* remove tailing whitespace */
307 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
310 /* Strip whitespace */
312 while (isspace ((unsigned char) value[0]))
314 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
319 if ( ('"' == value[0]) &&
320 ('"' == value[strlen (value) - 1]) )
322 value[strlen (value) - 1] = '\0';
325 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
330 LOG (GNUNET_ERROR_TYPE_WARNING,
331 _("Syntax error while deserializing in line %u\n"),
336 GNUNET_free_non_null (line_orig);
337 GNUNET_free (section);
338 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
344 * Parse a configuration file, add all of the options in the
345 * file to the configuration environment.
347 * @param cfg configuration to update
348 * @param filename name of the configuration file
349 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
352 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
353 const char *filename)
364 fn = GNUNET_STRINGS_filename_expand (filename);
365 LOG (GNUNET_ERROR_TYPE_DEBUG,
366 "Asked to parse config file `%s'\n",
369 return GNUNET_SYSERR;
370 dirty = cfg->dirty; /* back up value! */
372 GNUNET_DISK_file_size (fn,
377 LOG (GNUNET_ERROR_TYPE_WARNING,
378 "Error while determining the file size of `%s'\n",
381 return GNUNET_SYSERR;
385 GNUNET_break (0); /* File size is more than the heap size */
387 return GNUNET_SYSERR;
390 mem = GNUNET_malloc (fs);
391 sret = GNUNET_DISK_fn_read (fn,
395 (fs != (size_t) sret) )
397 LOG (GNUNET_ERROR_TYPE_WARNING,
398 _("Error while reading file `%s'\n"),
402 return GNUNET_SYSERR;
404 LOG (GNUNET_ERROR_TYPE_DEBUG,
405 "Deserializing contents of file `%s'\n",
407 endsep = strrchr (fn, (int) '/');
410 ret = GNUNET_CONFIGURATION_deserialize (cfg,
416 /* restore dirty flag - anything we set in the meantime
424 * Test if there are configuration options that were
425 * changed since the last save.
427 * @param cfg configuration to inspect
428 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
431 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
438 * Serializes the given configuration.
440 * @param cfg configuration to serialize
441 * @param size will be set to the size of the serialized memory block
442 * @return the memory block where the serialized configuration is
443 * present. This memory should be freed by the caller
446 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
449 struct ConfigSection *sec;
450 struct ConfigEntry *ent;
459 /* Pass1 : calculate the buffer size required */
461 for (sec = cfg->sections; NULL != sec; sec = sec->next)
463 /* For each section we need to add 3 charaters: {'[',']','\n'} */
464 m_size += strlen (sec->name) + 3;
465 for (ent = sec->entries; NULL != ent; ent = ent->next)
467 if (NULL != ent->val)
469 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
471 while (NULL != (pos = strstr (pos, "\n")))
476 /* For each key = value pair we need to add 4 characters (2
477 spaces and 1 equal-to character and 1 new line) */
478 m_size += strlen (ent->key) + strlen (ent->val) + 4;
481 /* A new line after section end */
485 /* Pass2: Allocate memory and write the configuration to it */
486 mem = GNUNET_malloc (m_size);
492 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
493 GNUNET_assert (0 < len);
494 GNUNET_memcpy (mem + c_size, cbuf, len);
497 for (ent = sec->entries; NULL != ent; ent = ent->next)
499 if (NULL != ent->val)
501 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
502 strcpy (val, ent->val);
503 while (NULL != (pos = strstr (val, "\n")))
505 memmove (&pos[2], &pos[1], strlen (&pos[1]));
509 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
511 GNUNET_memcpy (mem + c_size, cbuf, len);
516 GNUNET_memcpy (mem + c_size, "\n", 1);
520 GNUNET_assert (c_size == m_size);
527 * Write configuration file.
529 * @param cfg configuration to write
530 * @param filename where to write the configuration
531 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
534 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
535 const char *filename)
542 fn = GNUNET_STRINGS_filename_expand (filename);
544 return GNUNET_SYSERR;
545 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
548 return GNUNET_SYSERR;
550 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
551 sret = GNUNET_DISK_fn_write (fn, cfg_buf, size,
552 GNUNET_DISK_PERM_USER_READ
553 | GNUNET_DISK_PERM_USER_WRITE
554 | GNUNET_DISK_PERM_GROUP_READ
555 | GNUNET_DISK_PERM_GROUP_WRITE);
557 (size != (size_t) sret) )
560 GNUNET_free (cfg_buf);
561 LOG (GNUNET_ERROR_TYPE_WARNING,
562 "Writing configuration to file `%s' failed\n",
564 cfg->dirty = GNUNET_SYSERR; /* last write failed */
565 return GNUNET_SYSERR;
568 GNUNET_free (cfg_buf);
569 cfg->dirty = GNUNET_NO; /* last write succeeded */
575 * Iterate over all options in the configuration.
577 * @param cfg configuration to inspect
578 * @param iter function to call on each option
579 * @param iter_cls closure for @a iter
582 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
583 GNUNET_CONFIGURATION_Iterator iter,
586 struct ConfigSection *spos;
587 struct ConfigEntry *epos;
589 for (spos = cfg->sections; NULL != spos; spos = spos->next)
590 for (epos = spos->entries; NULL != epos; epos = epos->next)
591 if (NULL != epos->val)
592 iter (iter_cls, spos->name, epos->key, epos->val);
597 * Iterate over values of a section in the configuration.
599 * @param cfg configuration to inspect
600 * @param section the section
601 * @param iter function to call on each option
602 * @param iter_cls closure for @a iter
605 GNUNET_CONFIGURATION_iterate_section_values (const struct
606 GNUNET_CONFIGURATION_Handle *cfg,
608 GNUNET_CONFIGURATION_Iterator iter,
611 struct ConfigSection *spos;
612 struct ConfigEntry *epos;
614 spos = cfg->sections;
615 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
619 for (epos = spos->entries; NULL != epos; epos = epos->next)
620 if (NULL != epos->val)
621 iter (iter_cls, spos->name, epos->key, epos->val);
626 * Iterate over all sections in the configuration.
628 * @param cfg configuration to inspect
629 * @param iter function to call on each section
630 * @param iter_cls closure for @a iter
633 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
634 GNUNET_CONFIGURATION_Section_Iterator iter,
637 struct ConfigSection *spos;
638 struct ConfigSection *next;
640 next = cfg->sections;
645 iter (iter_cls, spos->name);
651 * Remove the given section and all options in it.
653 * @param cfg configuration to inspect
654 * @param section name of the section to remove
657 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
660 struct ConfigSection *spos;
661 struct ConfigSection *prev;
662 struct ConfigEntry *ent;
665 spos = cfg->sections;
668 if (0 == strcasecmp (section, spos->name))
671 cfg->sections = spos->next;
673 prev->next = spos->next;
674 while (NULL != (ent = spos->entries))
676 spos->entries = ent->next;
677 GNUNET_free (ent->key);
678 GNUNET_free_non_null (ent->val);
680 cfg->dirty = GNUNET_YES;
682 GNUNET_free (spos->name);
693 * Copy a configuration value to the given target configuration.
694 * Overwrites existing entries.
696 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
697 * @param section section for the value
698 * @param option option name of the value
699 * @param value value to copy
702 copy_entry (void *cls,
707 struct GNUNET_CONFIGURATION_Handle *dst = cls;
709 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
714 * Duplicate an existing configuration object.
716 * @param cfg configuration to duplicate
717 * @return duplicate configuration
719 struct GNUNET_CONFIGURATION_Handle *
720 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
722 struct GNUNET_CONFIGURATION_Handle *ret;
724 ret = GNUNET_CONFIGURATION_create ();
725 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
731 * Find a section entry from a configuration.
733 * @param cfg configuration to search in
734 * @param section name of the section to look for
735 * @return matching entry, NULL if not found
737 static struct ConfigSection *
738 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
741 struct ConfigSection *pos;
744 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
751 * Find an entry from a configuration.
753 * @param cfg handle to the configuration
754 * @param section section the option is in
755 * @param key the option
756 * @return matching entry, NULL if not found
758 static struct ConfigEntry *
759 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
763 struct ConfigSection *sec;
764 struct ConfigEntry *pos;
766 if (NULL == (sec = find_section (cfg, section)))
769 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
776 * A callback function, compares entries from two configurations
777 * (default against a new configuration) and write the diffs in a
778 * diff-configuration object (the callback object).
780 * @param cls the diff configuration (`struct DiffHandle *`)
781 * @param section section for the value (of the default conf.)
782 * @param option option name of the value (of the default conf.)
783 * @param value value to copy (of the default conf.)
786 compare_entries (void *cls,
791 struct DiffHandle *dh = cls;
792 struct ConfigEntry *entNew;
794 entNew = find_entry (dh->cfg_default, section, option);
795 if ( (NULL != entNew) &&
796 (NULL != entNew->val) &&
797 (0 == strcmp (entNew->val, value)) )
799 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
804 * Compute configuration with only entries that have been changed
806 * @param cfg_default original configuration
807 * @param cfg_new new configuration
808 * @return configuration with only the differences, never NULL
810 struct GNUNET_CONFIGURATION_Handle *
811 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
812 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
814 struct DiffHandle diffHandle;
816 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
817 diffHandle.cfg_default = cfg_default;
818 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
819 return diffHandle.cfgDiff;
824 * Write only configuration entries that have been changed to configuration file
826 * @param cfg_default default configuration
827 * @param cfg_new new configuration
828 * @param filename where to write the configuration diff between default and new
829 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
832 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
834 const struct GNUNET_CONFIGURATION_Handle
835 *cfg_new, const char *filename)
838 struct GNUNET_CONFIGURATION_Handle *diff;
840 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
841 ret = GNUNET_CONFIGURATION_write (diff, filename);
842 GNUNET_CONFIGURATION_destroy (diff);
848 * Set a configuration value that should be a string.
850 * @param cfg configuration to update
851 * @param section section of interest
852 * @param option option of interest
853 * @param value value to set
856 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
857 const char *section, const char *option,
860 struct ConfigSection *sec;
861 struct ConfigEntry *e;
864 e = find_entry (cfg, section, option);
869 GNUNET_free_non_null (e->val);
874 nv = GNUNET_strdup (value);
875 GNUNET_free_non_null (e->val);
880 sec = find_section (cfg, section);
883 sec = GNUNET_new (struct ConfigSection);
884 sec->name = GNUNET_strdup (section);
885 sec->next = cfg->sections;
888 e = GNUNET_new (struct ConfigEntry);
889 e->key = GNUNET_strdup (option);
890 e->val = GNUNET_strdup (value);
891 e->next = sec->entries;
897 * Set a configuration value that should be a number.
899 * @param cfg configuration to update
900 * @param section section of interest
901 * @param option option of interest
902 * @param number value to set
905 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
908 unsigned long long number)
916 GNUNET_CONFIGURATION_set_value_string (cfg,
924 * Get a configuration value that should be a number.
926 * @param cfg configuration to inspect
927 * @param section section of interest
928 * @param option option of interest
929 * @param number where to store the numeric value of the option
930 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
933 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle *cfg,
936 unsigned long long *number)
938 struct ConfigEntry *e;
941 if (NULL == (e = find_entry (cfg, section, option)))
942 return GNUNET_SYSERR;
944 return GNUNET_SYSERR;
945 if (1 != SSCANF (e->val,
949 return GNUNET_SYSERR;
955 * Get a configuration value that should be a floating point number.
957 * @param cfg configuration to inspect
958 * @param section section of interest
959 * @param option option of interest
960 * @param number where to store the floating value of the option
961 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
964 GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle *cfg,
969 struct ConfigEntry *e;
972 if (NULL == (e = find_entry (cfg, section, option)))
973 return GNUNET_SYSERR;
975 return GNUNET_SYSERR;
976 if (1 != SSCANF (e->val,
980 return GNUNET_SYSERR;
987 * Get a configuration value that should be a relative time.
989 * @param cfg configuration to inspect
990 * @param section section of interest
991 * @param option option of interest
992 * @param time set to the time value stored in the configuration
993 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
996 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
999 struct GNUNET_TIME_Relative *time)
1001 struct ConfigEntry *e;
1004 if (NULL == (e = find_entry (cfg,
1007 return GNUNET_SYSERR;
1009 return GNUNET_SYSERR;
1010 ret = GNUNET_STRINGS_fancy_time_to_relative (e->val,
1012 if (GNUNET_OK != ret)
1013 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1016 _("Not a valid relative time specification"));
1022 * Get a configuration value that should be a size in bytes.
1024 * @param cfg configuration to inspect
1025 * @param section section of interest
1026 * @param option option of interest
1027 * @param size set to the size in bytes as stored in the configuration
1028 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1031 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
1032 const char *section,
1034 unsigned long long *size)
1036 struct ConfigEntry *e;
1038 if (NULL == (e = find_entry (cfg, section, option)))
1039 return GNUNET_SYSERR;
1041 return GNUNET_SYSERR;
1042 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
1047 * Get a configuration value that should be a string.
1049 * @param cfg configuration to inspect
1050 * @param section section of interest
1051 * @param option option of interest
1052 * @param value will be set to a freshly allocated configuration
1053 * value, or NULL if option is not specified
1054 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1057 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
1058 const char *section,
1062 struct ConfigEntry *e;
1064 if ( (NULL == (e = find_entry (cfg, section, option))) ||
1068 return GNUNET_SYSERR;
1070 *value = GNUNET_strdup (e->val);
1076 * Get a configuration value that should be in a set of
1077 * predefined strings
1079 * @param cfg configuration to inspect
1080 * @param section section of interest
1081 * @param option option of interest
1082 * @param choices NULL-terminated list of legal values
1083 * @param value will be set to an entry in the legal list,
1084 * or NULL if option is not specified and no default given
1085 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1088 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1089 const char *section,
1091 const char *const *choices,
1094 struct ConfigEntry *e;
1097 if (NULL == (e = find_entry (cfg, section, option)))
1098 return GNUNET_SYSERR;
1099 for (i = 0; NULL != choices[i]; i++)
1100 if (0 == strcasecmp (choices[i], e->val))
1102 if (NULL == choices[i])
1104 LOG (GNUNET_ERROR_TYPE_ERROR,
1105 _("Configuration value '%s' for '%s'"
1106 " in section '%s' is not in set of legal choices\n"),
1110 return GNUNET_SYSERR;
1112 *value = choices[i];
1118 * Get crockford32-encoded fixed-size binary data from a configuration.
1120 * @param cfg configuration to access
1121 * @param section section to access
1122 * @param option option to access
1123 * @param buf where to store the decoded binary result
1124 * @param buf_size exact number of bytes to store in @a buf
1125 * @return #GNUNET_OK on success
1126 * #GNUNET_NO is the value does not exist
1127 * #GNUNET_SYSERR on decoding error
1130 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1131 const char *section,
1141 (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1146 data_size = (strlen (enc) * 5) / 8;
1147 if (data_size != buf_size)
1150 return GNUNET_SYSERR;
1153 GNUNET_STRINGS_string_to_data (enc,
1158 return GNUNET_SYSERR;
1166 * Test if we have a value for a particular option
1168 * @param cfg configuration to inspect
1169 * @param section section of interest
1170 * @param option option of interest
1171 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1174 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1175 const char *section,
1178 struct ConfigEntry *e;
1180 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1187 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1188 * where either in the "PATHS" section or the environtment "FOO" is
1189 * set to "DIRECTORY". We also support default expansion,
1190 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1191 * set in PATHS or the environment, and otherwise to "default". Note
1192 * that "default" itself can also be a $-expression, thus
1193 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1196 * @param cfg configuration to use for path expansion
1197 * @param orig string to $-expand (will be freed!)
1198 * @param depth recursion depth, used to detect recursive expansions
1199 * @return $-expanded string
1202 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1223 LOG (GNUNET_ERROR_TYPE_WARNING,
1224 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1228 LOG (GNUNET_ERROR_TYPE_DEBUG,
1229 "Asked to $-expand %s\n",
1233 LOG (GNUNET_ERROR_TYPE_DEBUG,
1234 "Doesn't start with $ - not expanding\n");
1256 LOG (GNUNET_ERROR_TYPE_WARNING,
1257 _("Missing closing `%s' in option `%s'\n"),
1269 def = strchr (orig, ':');
1274 if ( ('-' == *def) ||
1277 def = GNUNET_strdup (def);
1285 while ( (orig[i] != '/') &&
1286 (orig[i] != '\\') &&
1287 (orig[i] != '\0') &&
1290 if (orig[i] == '\0')
1296 erased_char = orig[i];
1297 erased_pos = &orig[i];
1299 post = &orig[i + 1];
1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1303 "Split into `%s' and `%s' with default %s\n",
1308 GNUNET_CONFIGURATION_get_value_string (cfg,
1313 if (NULL == (env = getenv (start)))
1316 def = expand_dollar (cfg, def, depth + 1);
1321 start = GNUNET_strdup (start);
1323 *erased_pos = erased_char;
1324 LOG (GNUNET_ERROR_TYPE_WARNING,
1325 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1327 GNUNET_free (start);
1330 prefix = GNUNET_strdup (env);
1332 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1333 if ( (erased_pos) && ('}' != erased_char) )
1335 len = strlen (prefix) + 1;
1336 prefix = GNUNET_realloc (prefix, len + 1);
1337 prefix[len - 1] = erased_char;
1340 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1341 strcpy (result, prefix);
1342 strcat (result, post);
1343 GNUNET_free_non_null (def);
1344 GNUNET_free (prefix);
1351 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1352 * where either in the "PATHS" section or the environtment "FOO" is
1353 * set to "DIRECTORY". We also support default expansion,
1354 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1355 * set in PATHS or the environment, and otherwise to "default". Note
1356 * that "default" itself can also be a $-expression, thus
1357 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1360 * @param cfg configuration to use for path expansion
1361 * @param orig string to $-expand (will be freed!). Note that multiple
1362 * $-expressions can be present in this string. They will all be
1364 * @return $-expanded string
1367 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1374 for (i = 0; '\0' != orig[i]; i++)
1378 dup = GNUNET_strdup (orig + i);
1379 dup = expand_dollar (cfg, dup, 0);
1380 len = strlen (dup) + 1;
1381 orig = GNUNET_realloc (orig, i + len);
1382 GNUNET_memcpy (orig + i, dup, len);
1390 * Get a configuration value that should be a string.
1392 * @param cfg configuration to inspect
1393 * @param section section of interest
1394 * @param option option of interest
1395 * @param value will be set to a freshly allocated configuration
1396 * value, or NULL if option is not specified
1397 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1400 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1401 const char *section,
1408 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1410 LOG (GNUNET_ERROR_TYPE_DEBUG,
1411 "Failed to retrieve filename\n");
1413 return GNUNET_SYSERR;
1415 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1416 *value = GNUNET_STRINGS_filename_expand (tmp);
1419 return GNUNET_SYSERR;
1425 * Get a configuration value that should be in a set of
1428 * @param cfg configuration to inspect
1429 * @param section section of interest
1430 * @param option option of interest
1431 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1434 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1435 const char *section,
1438 static const char *yesno[] = { "YES", "NO", NULL };
1443 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1444 if (ret == GNUNET_SYSERR)
1446 if (val == yesno[0])
1453 * Iterate over the set of filenames stored in a configuration value.
1455 * @param cfg configuration to inspect
1456 * @param section section of interest
1457 * @param option option of interest
1458 * @param cb function to call on each filename
1459 * @param cb_cls closure for @a cb
1460 * @return number of filenames iterated over, -1 on error
1463 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1464 const char *section,
1466 GNUNET_FileNameCallback cb,
1476 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1478 GNUNET_assert (list != NULL);
1483 while (pos[0] == ' ')
1485 if (strlen (pos) == 0)
1488 while ((end[0] != ' ') && (end[0] != '\0'))
1496 memmove (end, &end[1], strlen (&end[1]) + 1);
1498 /* illegal, but just keep it */
1501 /* illegal, but just ignore that there was a '/' */
1509 if (strlen (pos) > 0)
1512 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1514 ret = GNUNET_SYSERR;
1530 * @param value FIXME
1534 escape_name (const char *value)
1540 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1541 memset (escaped, 0, strlen (value) * 2 + 1);
1544 while (rpos[0] != '\0')
1567 * @param cls string we compare with (const char*)
1568 * @param fn filename we are currently looking at
1569 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1572 test_match (void *cls, const char *fn)
1574 const char *of = cls;
1576 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1581 * Append a filename to a configuration value that
1582 * represents a list of filenames
1584 * @param cfg configuration to update
1585 * @param section section of interest
1586 * @param option option of interest
1587 * @param value filename to append
1588 * @return #GNUNET_OK on success,
1589 * #GNUNET_NO if the filename already in the list
1590 * #GNUNET_SYSERR on error
1593 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1594 const char *section,
1602 if (GNUNET_SYSERR ==
1603 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1606 return GNUNET_NO; /* already exists */
1608 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1609 old = GNUNET_strdup ("");
1610 escaped = escape_name (value);
1611 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1613 if (strlen (old) > 0)
1615 strcat (nw, escaped);
1616 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1619 GNUNET_free (escaped);
1625 * Remove a filename from a configuration value that
1626 * represents a list of filenames
1628 * @param cfg configuration to update
1629 * @param section section of interest
1630 * @param option option of interest
1631 * @param value filename to remove
1632 * @return #GNUNET_OK on success,
1633 * #GNUNET_NO if the filename is not in the list,
1634 * #GNUNET_SYSERR on error
1637 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1638 *cfg, const char *section,
1649 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1651 match = escape_name (value);
1655 while (pos[0] == ' ')
1657 if (strlen (pos) == 0)
1660 while ((end[0] != ' ') && (end[0] != '\0'))
1671 /* illegal, but just keep it */
1674 /* illegal, but just ignore that there was a '/' */
1682 if (0 == strcmp (pos, match))
1685 memmove (pos, &end[1], strlen (&end[1]) + 1);
1693 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1695 GNUNET_free (match);
1704 GNUNET_free (match);
1710 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1711 * file in a directory, we trigger parsing on those files that
1714 * @param cls the cfg
1715 * @param filename file to parse
1716 * @return #GNUNET_OK on success
1719 parse_configuration_file (void *cls, const char *filename)
1721 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1725 /* Examine file extension */
1726 ext = strrchr (filename, '.');
1727 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1729 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1730 "Skipping file `%s'\n",
1735 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1741 * Load default configuration. This function will parse the
1742 * defaults from the given defaults_d directory.
1744 * @param cfg configuration to update
1745 * @param defaults_d directory with the defaults
1746 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1749 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1750 const char *defaults_d)
1752 if (GNUNET_SYSERR ==
1753 GNUNET_DISK_directory_scan (defaults_d,
1754 &parse_configuration_file,
1756 return GNUNET_SYSERR; /* no configuration at all found */
1761 /* end of configuration.c */