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/>.
20 * @file src/util/configuration.c
21 * @brief configuration management
22 * @author Christian Grothoff
26 #include "gnunet_crypto_lib.h"
27 #include "gnunet_strings_lib.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_disk_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
36 * @brief configuration entry
42 * This is a linked list.
44 struct ConfigEntry *next;
52 * current, commited value
59 * @brief configuration section
64 * This is a linked list.
66 struct ConfigSection *next;
69 * entries in the section
71 struct ConfigEntry *entries;
81 * @brief configuration data
83 struct GNUNET_CONFIGURATION_Handle
86 * Configuration sections.
88 struct ConfigSection *sections;
91 * Modification indication since last save
92 * #GNUNET_NO if clean, #GNUNET_YES if dirty,
93 * #GNUNET_SYSERR on error (i.e. last save failed)
101 * Used for diffing a configuration object against
106 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
108 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
113 * Create a GNUNET_CONFIGURATION_Handle.
115 * @return fresh configuration object
117 struct GNUNET_CONFIGURATION_Handle *
118 GNUNET_CONFIGURATION_create ()
120 return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
125 * Destroy configuration object.
127 * @param cfg configuration to destroy
130 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
132 struct ConfigSection *sec;
134 while (NULL != (sec = cfg->sections))
135 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
141 * Parse a configuration file @a filename and run the function
142 * @a cb with the resulting configuration object. Then free the
143 * configuration object and return the status value from @a cb.
145 * @param filename configuration to parse, NULL for "default"
146 * @param cb function to run
147 * @param cb_cls closure for @a cb
148 * @return #GNUNET_SYSERR if parsing the configuration failed,
149 * otherwise return value from @a cb.
152 GNUNET_CONFIGURATION_parse_and_run (const char *filename,
153 GNUNET_CONFIGURATION_Callback cb,
156 struct GNUNET_CONFIGURATION_Handle *cfg;
159 cfg = GNUNET_CONFIGURATION_create ();
161 GNUNET_CONFIGURATION_load (cfg,
165 GNUNET_CONFIGURATION_destroy (cfg);
166 return GNUNET_SYSERR;
170 GNUNET_CONFIGURATION_destroy (cfg);
176 * De-serializes configuration
178 * @param cfg configuration to update
179 * @param mem the memory block of serialized configuration
180 * @param size the size of the memory block
181 * @param basedir set to path from which we recursively load configuration
182 * from inlined configurations; NULL if not and raise warnings
183 * when we come across them
184 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
187 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
208 section = GNUNET_strdup ("");
212 while (r_bytes < size)
214 GNUNET_free_non_null (line_orig);
215 /* fgets-like behaviour on buffer */
216 to_read = size - r_bytes;
217 pos = memchr (&mem[r_bytes], '\n', to_read);
220 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
221 r_bytes += line_size;
225 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
226 r_bytes += line_size + 1;
229 /* increment line number */
231 /* tabs and '\r' are whitespace */
232 emptyline = GNUNET_YES;
233 for (i = 0; i < line_size; i++)
240 emptyline = GNUNET_NO;
242 /* ignore empty lines */
243 if (GNUNET_YES == emptyline)
246 /* remove tailing whitespace */
247 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
250 /* remove leading whitespace */
251 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
253 /* ignore comments */
254 if ( ('#' == line[0]) || ('%' == line[0]) )
257 /* handle special "@INLINE@" directive */
258 if (0 == strncasecmp (line,
260 strlen ("@INLINE@ ")))
263 value = &line[strlen ("@INLINE@ ")];
268 GNUNET_asprintf (&fn,
273 GNUNET_CONFIGURATION_parse (cfg,
277 ret = GNUNET_SYSERR; /* failed to parse included config */
284 LOG (GNUNET_ERROR_TYPE_DEBUG,
285 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
291 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
294 line[line_size - 1] = '\0';
296 GNUNET_free (section);
297 section = GNUNET_strdup (value);
300 if (NULL != (eq = strchr (line, '=')))
303 tag = GNUNET_strndup (line, eq - line);
304 /* remove tailing whitespace */
305 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
308 /* Strip whitespace */
310 while (isspace ((unsigned char) value[0]))
312 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
317 if ( ('"' == value[0]) &&
318 ('"' == value[strlen (value) - 1]) )
320 value[strlen (value) - 1] = '\0';
323 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
328 LOG (GNUNET_ERROR_TYPE_WARNING,
329 _("Syntax error while deserializing in line %u\n"),
334 GNUNET_free_non_null (line_orig);
335 GNUNET_free (section);
336 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
342 * Parse a configuration file, add all of the options in the
343 * file to the configuration environment.
345 * @param cfg configuration to update
346 * @param filename name of the configuration file
347 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
350 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
351 const char *filename)
362 fn = GNUNET_STRINGS_filename_expand (filename);
363 LOG (GNUNET_ERROR_TYPE_DEBUG,
364 "Asked to parse config file `%s'\n",
367 return GNUNET_SYSERR;
368 dirty = cfg->dirty; /* back up value! */
370 GNUNET_DISK_file_size (fn,
375 LOG (GNUNET_ERROR_TYPE_WARNING,
376 "Error while determining the file size of `%s'\n",
379 return GNUNET_SYSERR;
383 GNUNET_break (0); /* File size is more than the heap size */
385 return GNUNET_SYSERR;
388 mem = GNUNET_malloc (fs);
389 sret = GNUNET_DISK_fn_read (fn,
393 (fs != (size_t) sret) )
395 LOG (GNUNET_ERROR_TYPE_WARNING,
396 _("Error while reading file `%s'\n"),
400 return GNUNET_SYSERR;
402 LOG (GNUNET_ERROR_TYPE_DEBUG,
403 "Deserializing contents of file `%s'\n",
405 endsep = strrchr (fn, (int) '/');
408 ret = GNUNET_CONFIGURATION_deserialize (cfg,
414 /* restore dirty flag - anything we set in the meantime
422 * Test if there are configuration options that were
423 * changed since the last save.
425 * @param cfg configuration to inspect
426 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
429 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
436 * Serializes the given configuration.
438 * @param cfg configuration to serialize
439 * @param size will be set to the size of the serialized memory block
440 * @return the memory block where the serialized configuration is
441 * present. This memory should be freed by the caller
444 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
447 struct ConfigSection *sec;
448 struct ConfigEntry *ent;
457 /* Pass1 : calculate the buffer size required */
459 for (sec = cfg->sections; NULL != sec; sec = sec->next)
461 /* For each section we need to add 3 charaters: {'[',']','\n'} */
462 m_size += strlen (sec->name) + 3;
463 for (ent = sec->entries; NULL != ent; ent = ent->next)
465 if (NULL != ent->val)
467 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
469 while (NULL != (pos = strstr (pos, "\n")))
474 /* For each key = value pair we need to add 4 characters (2
475 spaces and 1 equal-to character and 1 new line) */
476 m_size += strlen (ent->key) + strlen (ent->val) + 4;
479 /* A new line after section end */
483 /* Pass2: Allocate memory and write the configuration to it */
484 mem = GNUNET_malloc (m_size);
490 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
491 GNUNET_assert (0 < len);
492 GNUNET_memcpy (mem + c_size, cbuf, len);
495 for (ent = sec->entries; NULL != ent; ent = ent->next)
497 if (NULL != ent->val)
499 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
500 strcpy (val, ent->val);
501 while (NULL != (pos = strstr (val, "\n")))
503 memmove (&pos[2], &pos[1], strlen (&pos[1]));
507 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
509 GNUNET_memcpy (mem + c_size, cbuf, len);
514 GNUNET_memcpy (mem + c_size, "\n", 1);
518 GNUNET_assert (c_size == m_size);
525 * Write configuration file.
527 * @param cfg configuration to write
528 * @param filename where to write the configuration
529 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
532 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
533 const char *filename)
540 fn = GNUNET_STRINGS_filename_expand (filename);
542 return GNUNET_SYSERR;
543 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
546 return GNUNET_SYSERR;
548 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
549 sret = GNUNET_DISK_fn_write (fn, cfg_buf, size,
550 GNUNET_DISK_PERM_USER_READ
551 | GNUNET_DISK_PERM_USER_WRITE
552 | GNUNET_DISK_PERM_GROUP_READ
553 | GNUNET_DISK_PERM_GROUP_WRITE);
555 (size != (size_t) sret) )
558 GNUNET_free (cfg_buf);
559 LOG (GNUNET_ERROR_TYPE_WARNING,
560 "Writing configuration to file `%s' failed\n",
562 cfg->dirty = GNUNET_SYSERR; /* last write failed */
563 return GNUNET_SYSERR;
566 GNUNET_free (cfg_buf);
567 cfg->dirty = GNUNET_NO; /* last write succeeded */
573 * Iterate over all options in the configuration.
575 * @param cfg configuration to inspect
576 * @param iter function to call on each option
577 * @param iter_cls closure for @a iter
580 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
581 GNUNET_CONFIGURATION_Iterator iter,
584 struct ConfigSection *spos;
585 struct ConfigEntry *epos;
587 for (spos = cfg->sections; NULL != spos; spos = spos->next)
588 for (epos = spos->entries; NULL != epos; epos = epos->next)
589 if (NULL != epos->val)
590 iter (iter_cls, spos->name, epos->key, epos->val);
595 * Iterate over values of a section in the configuration.
597 * @param cfg configuration to inspect
598 * @param section the section
599 * @param iter function to call on each option
600 * @param iter_cls closure for @a iter
603 GNUNET_CONFIGURATION_iterate_section_values (const struct
604 GNUNET_CONFIGURATION_Handle *cfg,
606 GNUNET_CONFIGURATION_Iterator iter,
609 struct ConfigSection *spos;
610 struct ConfigEntry *epos;
612 spos = cfg->sections;
613 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
617 for (epos = spos->entries; NULL != epos; epos = epos->next)
618 if (NULL != epos->val)
619 iter (iter_cls, spos->name, epos->key, epos->val);
624 * Iterate over all sections in the configuration.
626 * @param cfg configuration to inspect
627 * @param iter function to call on each section
628 * @param iter_cls closure for @a iter
631 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
632 GNUNET_CONFIGURATION_Section_Iterator iter,
635 struct ConfigSection *spos;
636 struct ConfigSection *next;
638 next = cfg->sections;
643 iter (iter_cls, spos->name);
649 * Remove the given section and all options in it.
651 * @param cfg configuration to inspect
652 * @param section name of the section to remove
655 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
658 struct ConfigSection *spos;
659 struct ConfigSection *prev;
660 struct ConfigEntry *ent;
663 spos = cfg->sections;
666 if (0 == strcasecmp (section, spos->name))
669 cfg->sections = spos->next;
671 prev->next = spos->next;
672 while (NULL != (ent = spos->entries))
674 spos->entries = ent->next;
675 GNUNET_free (ent->key);
676 GNUNET_free_non_null (ent->val);
678 cfg->dirty = GNUNET_YES;
680 GNUNET_free (spos->name);
691 * Copy a configuration value to the given target configuration.
692 * Overwrites existing entries.
694 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
695 * @param section section for the value
696 * @param option option name of the value
697 * @param value value to copy
700 copy_entry (void *cls,
705 struct GNUNET_CONFIGURATION_Handle *dst = cls;
707 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
712 * Duplicate an existing configuration object.
714 * @param cfg configuration to duplicate
715 * @return duplicate configuration
717 struct GNUNET_CONFIGURATION_Handle *
718 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
720 struct GNUNET_CONFIGURATION_Handle *ret;
722 ret = GNUNET_CONFIGURATION_create ();
723 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
729 * Find a section entry from a configuration.
731 * @param cfg configuration to search in
732 * @param section name of the section to look for
733 * @return matching entry, NULL if not found
735 static struct ConfigSection *
736 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
739 struct ConfigSection *pos;
742 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
749 * Find an entry from a configuration.
751 * @param cfg handle to the configuration
752 * @param section section the option is in
753 * @param key the option
754 * @return matching entry, NULL if not found
756 static struct ConfigEntry *
757 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
761 struct ConfigSection *sec;
762 struct ConfigEntry *pos;
764 if (NULL == (sec = find_section (cfg, section)))
767 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
774 * A callback function, compares entries from two configurations
775 * (default against a new configuration) and write the diffs in a
776 * diff-configuration object (the callback object).
778 * @param cls the diff configuration (`struct DiffHandle *`)
779 * @param section section for the value (of the default conf.)
780 * @param option option name of the value (of the default conf.)
781 * @param value value to copy (of the default conf.)
784 compare_entries (void *cls,
789 struct DiffHandle *dh = cls;
790 struct ConfigEntry *entNew;
792 entNew = find_entry (dh->cfg_default, section, option);
793 if ( (NULL != entNew) &&
794 (NULL != entNew->val) &&
795 (0 == strcmp (entNew->val, value)) )
797 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
802 * Compute configuration with only entries that have been changed
804 * @param cfg_default original configuration
805 * @param cfg_new new configuration
806 * @return configuration with only the differences, never NULL
808 struct GNUNET_CONFIGURATION_Handle *
809 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
810 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
812 struct DiffHandle diffHandle;
814 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
815 diffHandle.cfg_default = cfg_default;
816 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
817 return diffHandle.cfgDiff;
822 * Write only configuration entries that have been changed to configuration file
824 * @param cfg_default default configuration
825 * @param cfg_new new configuration
826 * @param filename where to write the configuration diff between default and new
827 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
830 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
832 const struct GNUNET_CONFIGURATION_Handle
833 *cfg_new, const char *filename)
836 struct GNUNET_CONFIGURATION_Handle *diff;
838 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
839 ret = GNUNET_CONFIGURATION_write (diff, filename);
840 GNUNET_CONFIGURATION_destroy (diff);
846 * Set a configuration value that should be a string.
848 * @param cfg configuration to update
849 * @param section section of interest
850 * @param option option of interest
851 * @param value value to set
854 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
855 const char *section, const char *option,
858 struct ConfigSection *sec;
859 struct ConfigEntry *e;
862 e = find_entry (cfg, section, option);
867 GNUNET_free_non_null (e->val);
872 nv = GNUNET_strdup (value);
873 GNUNET_free_non_null (e->val);
878 sec = find_section (cfg, section);
881 sec = GNUNET_new (struct ConfigSection);
882 sec->name = GNUNET_strdup (section);
883 sec->next = cfg->sections;
886 e = GNUNET_new (struct ConfigEntry);
887 e->key = GNUNET_strdup (option);
888 e->val = GNUNET_strdup (value);
889 e->next = sec->entries;
895 * Set a configuration value that should be a number.
897 * @param cfg configuration to update
898 * @param section section of interest
899 * @param option option of interest
900 * @param number value to set
903 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
906 unsigned long long number)
914 GNUNET_CONFIGURATION_set_value_string (cfg,
922 * Get a configuration value that should be a number.
924 * @param cfg configuration to inspect
925 * @param section section of interest
926 * @param option option of interest
927 * @param number where to store the numeric value of the option
928 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
931 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle *cfg,
934 unsigned long long *number)
936 struct ConfigEntry *e;
939 if (NULL == (e = find_entry (cfg, section, option)))
940 return GNUNET_SYSERR;
942 return GNUNET_SYSERR;
943 if (1 != SSCANF (e->val,
947 return GNUNET_SYSERR;
953 * Get a configuration value that should be a floating point number.
955 * @param cfg configuration to inspect
956 * @param section section of interest
957 * @param option option of interest
958 * @param number where to store the floating value of the option
959 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
962 GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle *cfg,
967 struct ConfigEntry *e;
970 if (NULL == (e = find_entry (cfg, section, option)))
971 return GNUNET_SYSERR;
973 return GNUNET_SYSERR;
974 if (1 != SSCANF (e->val,
978 return GNUNET_SYSERR;
985 * Get a configuration value that should be a relative time.
987 * @param cfg configuration to inspect
988 * @param section section of interest
989 * @param option option of interest
990 * @param time set to the time value stored in the configuration
991 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
994 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
997 struct GNUNET_TIME_Relative *time)
999 struct ConfigEntry *e;
1002 if (NULL == (e = find_entry (cfg,
1005 return GNUNET_SYSERR;
1007 return GNUNET_SYSERR;
1008 ret = GNUNET_STRINGS_fancy_time_to_relative (e->val,
1010 if (GNUNET_OK != ret)
1011 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1014 _("Not a valid relative time specification"));
1020 * Get a configuration value that should be a size in bytes.
1022 * @param cfg configuration to inspect
1023 * @param section section of interest
1024 * @param option option of interest
1025 * @param size set to the size in bytes as stored in the configuration
1026 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1029 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
1030 const char *section,
1032 unsigned long long *size)
1034 struct ConfigEntry *e;
1036 if (NULL == (e = find_entry (cfg, section, option)))
1037 return GNUNET_SYSERR;
1039 return GNUNET_SYSERR;
1040 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
1045 * Get a configuration value that should be a string.
1047 * @param cfg configuration to inspect
1048 * @param section section of interest
1049 * @param option option of interest
1050 * @param value will be set to a freshly allocated configuration
1051 * value, or NULL if option is not specified
1052 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1055 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
1056 const char *section,
1060 struct ConfigEntry *e;
1062 if ( (NULL == (e = find_entry (cfg, section, option))) ||
1066 return GNUNET_SYSERR;
1068 *value = GNUNET_strdup (e->val);
1074 * Get a configuration value that should be in a set of
1075 * predefined strings
1077 * @param cfg configuration to inspect
1078 * @param section section of interest
1079 * @param option option of interest
1080 * @param choices NULL-terminated list of legal values
1081 * @param value will be set to an entry in the legal list,
1082 * or NULL if option is not specified and no default given
1083 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1086 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1087 const char *section,
1089 const char *const *choices,
1092 struct ConfigEntry *e;
1095 if (NULL == (e = find_entry (cfg, section, option)))
1096 return GNUNET_SYSERR;
1097 for (i = 0; NULL != choices[i]; i++)
1098 if (0 == strcasecmp (choices[i], e->val))
1100 if (NULL == choices[i])
1102 LOG (GNUNET_ERROR_TYPE_ERROR,
1103 _("Configuration value '%s' for '%s'"
1104 " in section '%s' is not in set of legal choices\n"),
1108 return GNUNET_SYSERR;
1110 *value = choices[i];
1116 * Get crockford32-encoded fixed-size binary data from a configuration.
1118 * @param cfg configuration to access
1119 * @param section section to access
1120 * @param option option to access
1121 * @param buf where to store the decoded binary result
1122 * @param buf_size exact number of bytes to store in @a buf
1123 * @return #GNUNET_OK on success
1124 * #GNUNET_NO is the value does not exist
1125 * #GNUNET_SYSERR on decoding error
1128 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1129 const char *section,
1139 (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1144 data_size = (strlen (enc) * 5) / 8;
1145 if (data_size != buf_size)
1148 return GNUNET_SYSERR;
1151 GNUNET_STRINGS_string_to_data (enc,
1156 return GNUNET_SYSERR;
1164 * Test if we have a value for a particular option
1166 * @param cfg configuration to inspect
1167 * @param section section of interest
1168 * @param option option of interest
1169 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1172 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1173 const char *section,
1176 struct ConfigEntry *e;
1178 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1185 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1186 * where either in the "PATHS" section or the environtment "FOO" is
1187 * set to "DIRECTORY". We also support default expansion,
1188 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1189 * set in PATHS or the environment, and otherwise to "default". Note
1190 * that "default" itself can also be a $-expression, thus
1191 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1194 * @param cfg configuration to use for path expansion
1195 * @param orig string to $-expand (will be freed!)
1196 * @param depth recursion depth, used to detect recursive expansions
1197 * @return $-expanded string
1200 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1221 LOG (GNUNET_ERROR_TYPE_WARNING,
1222 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1226 LOG (GNUNET_ERROR_TYPE_DEBUG,
1227 "Asked to $-expand %s\n",
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "Doesn't start with $ - not expanding\n");
1254 LOG (GNUNET_ERROR_TYPE_WARNING,
1255 _("Missing closing `%s' in option `%s'\n"),
1267 def = strchr (orig, ':');
1272 if ( ('-' == *def) ||
1275 def = GNUNET_strdup (def);
1283 while ( (orig[i] != '/') &&
1284 (orig[i] != '\\') &&
1285 (orig[i] != '\0') &&
1288 if (orig[i] == '\0')
1294 erased_char = orig[i];
1295 erased_pos = &orig[i];
1297 post = &orig[i + 1];
1300 LOG (GNUNET_ERROR_TYPE_DEBUG,
1301 "Split into `%s' and `%s' with default %s\n",
1306 GNUNET_CONFIGURATION_get_value_string (cfg,
1311 if (NULL == (env = getenv (start)))
1314 def = expand_dollar (cfg, def, depth + 1);
1319 start = GNUNET_strdup (start);
1321 *erased_pos = erased_char;
1322 LOG (GNUNET_ERROR_TYPE_WARNING,
1323 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1325 GNUNET_free (start);
1328 prefix = GNUNET_strdup (env);
1330 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1331 if ( (erased_pos) && ('}' != erased_char) )
1333 len = strlen (prefix) + 1;
1334 prefix = GNUNET_realloc (prefix, len + 1);
1335 prefix[len - 1] = erased_char;
1338 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1339 strcpy (result, prefix);
1340 strcat (result, post);
1341 GNUNET_free_non_null (def);
1342 GNUNET_free (prefix);
1349 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1350 * where either in the "PATHS" section or the environtment "FOO" is
1351 * set to "DIRECTORY". We also support default expansion,
1352 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1353 * set in PATHS or the environment, and otherwise to "default". Note
1354 * that "default" itself can also be a $-expression, thus
1355 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1358 * @param cfg configuration to use for path expansion
1359 * @param orig string to $-expand (will be freed!). Note that multiple
1360 * $-expressions can be present in this string. They will all be
1362 * @return $-expanded string
1365 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1372 for (i = 0; '\0' != orig[i]; i++)
1376 dup = GNUNET_strdup (orig + i);
1377 dup = expand_dollar (cfg, dup, 0);
1378 len = strlen (dup) + 1;
1379 orig = GNUNET_realloc (orig, i + len);
1380 GNUNET_memcpy (orig + i, dup, len);
1388 * Get a configuration value that should be a string.
1390 * @param cfg configuration to inspect
1391 * @param section section of interest
1392 * @param option option of interest
1393 * @param value will be set to a freshly allocated configuration
1394 * value, or NULL if option is not specified
1395 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1398 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1399 const char *section,
1406 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1408 LOG (GNUNET_ERROR_TYPE_DEBUG,
1409 "Failed to retrieve filename\n");
1411 return GNUNET_SYSERR;
1413 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1414 *value = GNUNET_STRINGS_filename_expand (tmp);
1417 return GNUNET_SYSERR;
1423 * Get a configuration value that should be in a set of
1426 * @param cfg configuration to inspect
1427 * @param section section of interest
1428 * @param option option of interest
1429 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1432 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1433 const char *section,
1436 static const char *yesno[] = { "YES", "NO", NULL };
1441 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1442 if (ret == GNUNET_SYSERR)
1444 if (val == yesno[0])
1451 * Iterate over the set of filenames stored in a configuration value.
1453 * @param cfg configuration to inspect
1454 * @param section section of interest
1455 * @param option option of interest
1456 * @param cb function to call on each filename
1457 * @param cb_cls closure for @a cb
1458 * @return number of filenames iterated over, -1 on error
1461 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1462 const char *section,
1464 GNUNET_FileNameCallback cb,
1474 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1476 GNUNET_assert (list != NULL);
1481 while (pos[0] == ' ')
1483 if (strlen (pos) == 0)
1486 while ((end[0] != ' ') && (end[0] != '\0'))
1494 memmove (end, &end[1], strlen (&end[1]) + 1);
1496 /* illegal, but just keep it */
1499 /* illegal, but just ignore that there was a '/' */
1507 if (strlen (pos) > 0)
1510 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1512 ret = GNUNET_SYSERR;
1528 * @param value FIXME
1532 escape_name (const char *value)
1538 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1539 memset (escaped, 0, strlen (value) * 2 + 1);
1542 while (rpos[0] != '\0')
1565 * @param cls string we compare with (const char*)
1566 * @param fn filename we are currently looking at
1567 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1570 test_match (void *cls, const char *fn)
1572 const char *of = cls;
1574 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1579 * Append a filename to a configuration value that
1580 * represents a list of filenames
1582 * @param cfg configuration to update
1583 * @param section section of interest
1584 * @param option option of interest
1585 * @param value filename to append
1586 * @return #GNUNET_OK on success,
1587 * #GNUNET_NO if the filename already in the list
1588 * #GNUNET_SYSERR on error
1591 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1592 const char *section,
1600 if (GNUNET_SYSERR ==
1601 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1604 return GNUNET_NO; /* already exists */
1606 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1607 old = GNUNET_strdup ("");
1608 escaped = escape_name (value);
1609 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1611 if (strlen (old) > 0)
1613 strcat (nw, escaped);
1614 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1617 GNUNET_free (escaped);
1623 * Remove a filename from a configuration value that
1624 * represents a list of filenames
1626 * @param cfg configuration to update
1627 * @param section section of interest
1628 * @param option option of interest
1629 * @param value filename to remove
1630 * @return #GNUNET_OK on success,
1631 * #GNUNET_NO if the filename is not in the list,
1632 * #GNUNET_SYSERR on error
1635 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1636 *cfg, const char *section,
1647 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1649 match = escape_name (value);
1653 while (pos[0] == ' ')
1655 if (strlen (pos) == 0)
1658 while ((end[0] != ' ') && (end[0] != '\0'))
1669 /* illegal, but just keep it */
1672 /* illegal, but just ignore that there was a '/' */
1680 if (0 == strcmp (pos, match))
1683 memmove (pos, &end[1], strlen (&end[1]) + 1);
1691 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1693 GNUNET_free (match);
1702 GNUNET_free (match);
1708 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1709 * file in a directory, we trigger parsing on those files that
1712 * @param cls the cfg
1713 * @param filename file to parse
1714 * @return #GNUNET_OK on success
1717 parse_configuration_file (void *cls, const char *filename)
1719 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1723 /* Examine file extension */
1724 ext = strrchr (filename, '.');
1725 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1727 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1728 "Skipping file `%s'\n",
1733 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1739 * Load default configuration. This function will parse the
1740 * defaults from the given defaults_d directory.
1742 * @param cfg configuration to update
1743 * @param defaults_d directory with the defaults
1744 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1747 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1748 const char *defaults_d)
1750 if (GNUNET_SYSERR ==
1751 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1752 return GNUNET_SYSERR; /* no configuration at all found */
1757 /* end of configuration.c */