2 This file is part of GNUnet.
3 (C) 2006, 2007, 2008, 2009, 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file src/util/configuration.c
23 * @brief configuration management
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
35 * @brief configuration entry
41 * This is a linked list.
43 struct ConfigEntry *next;
51 * current, commited value
58 * @brief configuration section
63 * This is a linked list.
65 struct ConfigSection *next;
68 * entries in the section
70 struct ConfigEntry *entries;
80 * @brief configuration data
82 struct GNUNET_CONFIGURATION_Handle
85 * Configuration sections.
87 struct ConfigSection *sections;
90 * Modification indication since last save
91 * #GNUNET_NO if clean, #GNUNET_YES if dirty,
92 * #GNUNET_SYSERR on error (i.e. last save failed)
100 * Used for diffing a configuration object against
105 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
107 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
112 * Create a GNUNET_CONFIGURATION_Handle.
114 * @return fresh configuration object
116 struct GNUNET_CONFIGURATION_Handle *
117 GNUNET_CONFIGURATION_create ()
119 return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
124 * Destroy configuration object.
126 * @param cfg configuration to destroy
129 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
131 struct ConfigSection *sec;
133 while (NULL != (sec = cfg->sections))
134 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
140 * De-serializes configuration
142 * @param cfg configuration to update
143 * @param mem the memory block of serialized configuration
144 * @param size the size of the memory block
145 * @param allow_inline set to #GNUNET_YES if we recursively load configuration
146 * from inlined configurations; #GNUNET_NO if not and raise warnings
147 * when we come across them
148 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
151 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
171 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing config file\n");
173 section = GNUNET_strdup ("");
177 while (r_bytes < size)
179 GNUNET_free_non_null (line_orig);
180 /* fgets-like behaviour on buffer */
181 to_read = size - r_bytes;
182 pos = memchr (&mem[r_bytes], '\n', to_read);
185 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
186 r_bytes += line_size;
190 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
191 r_bytes += line_size + 1;
194 /* increment line number */
196 /* tabs and '\r' are whitespace */
197 emptyline = GNUNET_YES;
198 for (i = 0; i < line_size; i++)
205 emptyline = GNUNET_NO;
207 /* ignore empty lines */
208 if (GNUNET_YES == emptyline)
211 /* remove tailing whitespace */
212 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
215 /* remove leading whitespace */
216 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
218 /* ignore comments */
219 if ( ('#' == line[0]) || ('%' == line[0]) )
222 /* handle special "@INLINE@" directive */
223 if (0 == strncasecmp (line,
225 strlen ("@INLINE@ ")))
228 value = &line[strlen ("@INLINE@ ")];
229 if (GNUNET_YES == allow_inline)
231 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
233 ret = GNUNET_SYSERR; /* failed to parse included config */
239 LOG (GNUNET_ERROR_TYPE_DEBUG,
240 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
246 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
249 line[line_size - 1] = '\0';
251 GNUNET_free (section);
252 section = GNUNET_strdup (value);
253 LOG (GNUNET_ERROR_TYPE_DEBUG,
254 "Config section `%s'\n",
258 if (NULL != (eq = strchr (line, '=')))
261 tag = GNUNET_strndup (line, eq - line);
262 /* remove tailing whitespace */
263 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
266 /* Strip whitespace */
268 while (isspace ((unsigned char) value[0]))
270 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
275 if ( ('"' == value[0]) &&
276 ('"' == value[strlen (value) - 1]) )
278 value[strlen (value) - 1] = '\0';
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s=\"%s\"\n", tag, value);
282 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
287 LOG (GNUNET_ERROR_TYPE_WARNING,
288 _("Syntax error while deserializing in line %u\n"),
293 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n");
294 GNUNET_free_non_null (line_orig);
295 GNUNET_free (section);
296 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
302 * Parse a configuration file, add all of the options in the
303 * file to the configuration environment.
305 * @param cfg configuration to update
306 * @param filename name of the configuration file
307 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
310 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
311 const char *filename)
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
321 "Asked to parse config file `%s'\n",
323 fn = GNUNET_STRINGS_filename_expand (filename);
324 LOG (GNUNET_ERROR_TYPE_DEBUG,
325 "Config file name expanded to `%s'\n",
328 return GNUNET_SYSERR;
329 dirty = cfg->dirty; /* back up value! */
331 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
333 LOG (GNUNET_ERROR_TYPE_WARNING,
334 "Error while determining the file size of %s\n", fn);
336 return GNUNET_SYSERR;
340 GNUNET_break (0); /* File size is more than the heap size */
342 return GNUNET_SYSERR;
345 mem = GNUNET_malloc (fs);
346 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
348 LOG (GNUNET_ERROR_TYPE_WARNING,
349 "Error while reading file %s\n", fn);
352 return GNUNET_SYSERR;
354 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
356 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
358 /* restore dirty flag - anything we set in the meantime
366 * Test if there are configuration options that were
367 * changed since the last save.
369 * @param cfg configuration to inspect
370 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
373 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
380 * Serializes the given configuration.
382 * @param cfg configuration to serialize
383 * @param size will be set to the size of the serialized memory block
384 * @return the memory block where the serialized configuration is
385 * present. This memory should be freed by the caller
388 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
391 struct ConfigSection *sec;
392 struct ConfigEntry *ent;
402 /* Pass1 : calculate the buffer size required */
404 for (sec = cfg->sections; NULL != sec; sec = sec->next)
406 /* For each section we need to add 3 charaters: {'[',']','\n'} */
407 m_size += strlen (sec->name) + 3;
408 for (ent = sec->entries; NULL != ent; ent = ent->next)
410 if (NULL != ent->val)
412 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
414 while (NULL != (pos = strstr (pos, "\n")))
419 /* For each key = value pair we need to add 4 characters (2
420 spaces and 1 equal-to character and 1 new line) */
421 m_size += strlen (ent->key) + strlen (ent->val) + 4;
424 /* A new line after section end */
428 /* Pass2: Allocate memory and write the configuration to it */
429 mem = GNUNET_malloc (m_size);
435 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
436 GNUNET_assert (0 < len);
437 memcpy (mem + c_size, cbuf, len);
440 for (ent = sec->entries; NULL != ent; ent = ent->next)
442 if (NULL != ent->val)
444 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
445 strcpy (val, ent->val);
446 while (NULL != (pos = strstr (val, "\n")))
448 memmove (&pos[2], &pos[1], strlen (&pos[1]));
452 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
454 memcpy (mem + c_size, cbuf, len);
459 memcpy (mem + c_size, "\n", 1);
463 GNUNET_assert (c_size == m_size);
470 * Write configuration file.
472 * @param cfg configuration to write
473 * @param filename where to write the configuration
474 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
477 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
478 const char *filename)
484 fn = GNUNET_STRINGS_filename_expand (filename);
486 return GNUNET_SYSERR;
487 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
490 return GNUNET_SYSERR;
492 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
493 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
494 GNUNET_DISK_PERM_USER_READ
495 | GNUNET_DISK_PERM_USER_WRITE
496 | GNUNET_DISK_PERM_GROUP_READ
497 | GNUNET_DISK_PERM_GROUP_WRITE))
500 GNUNET_free (cfg_buf);
501 LOG (GNUNET_ERROR_TYPE_WARNING,
502 "Writing configration to file: %s failed\n", filename);
503 cfg->dirty = GNUNET_SYSERR; /* last write failed */
504 return GNUNET_SYSERR;
507 GNUNET_free (cfg_buf);
508 cfg->dirty = GNUNET_NO; /* last write succeeded */
514 * Iterate over all options in the configuration.
516 * @param cfg configuration to inspect
517 * @param iter function to call on each option
518 * @param iter_cls closure for @a iter
521 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
522 GNUNET_CONFIGURATION_Iterator iter,
525 struct ConfigSection *spos;
526 struct ConfigEntry *epos;
528 for (spos = cfg->sections; NULL != spos; spos = spos->next)
529 for (epos = spos->entries; NULL != epos; epos = epos->next)
530 if (NULL != epos->val)
531 iter (iter_cls, spos->name, epos->key, epos->val);
536 * Iterate over values of a section in the configuration.
538 * @param cfg configuration to inspect
539 * @param section the section
540 * @param iter function to call on each option
541 * @param iter_cls closure for @a iter
544 GNUNET_CONFIGURATION_iterate_section_values (const struct
545 GNUNET_CONFIGURATION_Handle *cfg,
547 GNUNET_CONFIGURATION_Iterator iter,
550 struct ConfigSection *spos;
551 struct ConfigEntry *epos;
553 spos = cfg->sections;
554 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
558 for (epos = spos->entries; NULL != epos; epos = epos->next)
559 if (NULL != epos->val)
560 iter (iter_cls, spos->name, epos->key, epos->val);
565 * Iterate over all sections in the configuration.
567 * @param cfg configuration to inspect
568 * @param iter function to call on each section
569 * @param iter_cls closure for @a iter
572 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
574 GNUNET_CONFIGURATION_Section_Iterator
575 iter, void *iter_cls)
577 struct ConfigSection *spos;
578 struct ConfigSection *next;
580 next = cfg->sections;
585 iter (iter_cls, spos->name);
591 * Remove the given section and all options in it.
593 * @param cfg configuration to inspect
594 * @param section name of the section to remove
597 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
600 struct ConfigSection *spos;
601 struct ConfigSection *prev;
602 struct ConfigEntry *ent;
605 spos = cfg->sections;
608 if (0 == strcasecmp (section, spos->name))
611 cfg->sections = spos->next;
613 prev->next = spos->next;
614 while (NULL != (ent = spos->entries))
616 spos->entries = ent->next;
617 GNUNET_free (ent->key);
618 GNUNET_free_non_null (ent->val);
620 cfg->dirty = GNUNET_YES;
622 GNUNET_free (spos->name);
633 * Copy a configuration value to the given target configuration.
634 * Overwrites existing entries.
636 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
637 * @param section section for the value
638 * @param option option name of the value
639 * @param value value to copy
642 copy_entry (void *cls,
647 struct GNUNET_CONFIGURATION_Handle *dst = cls;
649 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
654 * Duplicate an existing configuration object.
656 * @param cfg configuration to duplicate
657 * @return duplicate configuration
659 struct GNUNET_CONFIGURATION_Handle *
660 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
662 struct GNUNET_CONFIGURATION_Handle *ret;
664 ret = GNUNET_CONFIGURATION_create ();
665 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
671 * Find a section entry from a configuration.
673 * @param cfg configuration to search in
674 * @param section name of the section to look for
675 * @return matching entry, NULL if not found
677 static struct ConfigSection *
678 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
681 struct ConfigSection *pos;
684 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
691 * Find an entry from a configuration.
693 * @param cfg handle to the configuration
694 * @param section section the option is in
695 * @param key the option
696 * @return matching entry, NULL if not found
698 static struct ConfigEntry *
699 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
703 struct ConfigSection *sec;
704 struct ConfigEntry *pos;
706 if (NULL == (sec = find_section (cfg, section)))
709 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
716 * A callback function, compares entries from two configurations
717 * (default against a new configuration) and write the diffs in a
718 * diff-configuration object (the callback object).
720 * @param cls the diff configuration (`struct DiffHandle *`)
721 * @param section section for the value (of the default conf.)
722 * @param option option name of the value (of the default conf.)
723 * @param value value to copy (of the default conf.)
726 compare_entries (void *cls,
731 struct DiffHandle *dh = cls;
732 struct ConfigEntry *entNew;
734 entNew = find_entry (dh->cfg_default, section, option);
735 if ( (NULL != entNew) &&
736 (NULL != entNew->val) &&
737 (0 == strcmp (entNew->val, value)) )
739 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
744 * Compute configuration with only entries that have been changed
746 * @param cfg_default original configuration
747 * @param cfg_new new configuration
748 * @return configuration with only the differences, never NULL
750 struct GNUNET_CONFIGURATION_Handle *
751 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
752 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
754 struct DiffHandle diffHandle;
756 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
757 diffHandle.cfg_default = cfg_default;
758 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
759 return diffHandle.cfgDiff;
764 * Write only configuration entries that have been changed to configuration file
766 * @param cfg_default default configuration
767 * @param cfg_new new configuration
768 * @param filename where to write the configuration diff between default and new
769 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
772 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
774 const struct GNUNET_CONFIGURATION_Handle
775 *cfg_new, const char *filename)
778 struct GNUNET_CONFIGURATION_Handle *diff;
780 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
781 ret = GNUNET_CONFIGURATION_write (diff, filename);
782 GNUNET_CONFIGURATION_destroy (diff);
788 * Set a configuration value that should be a string.
790 * @param cfg configuration to update
791 * @param section section of interest
792 * @param option option of interest
793 * @param value value to set
796 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
797 const char *section, const char *option,
800 struct ConfigSection *sec;
801 struct ConfigEntry *e;
804 e = find_entry (cfg, section, option);
809 GNUNET_free_non_null (e->val);
814 nv = GNUNET_strdup (value);
815 GNUNET_free_non_null (e->val);
820 sec = find_section (cfg, section);
823 sec = GNUNET_new (struct ConfigSection);
824 sec->name = GNUNET_strdup (section);
825 sec->next = cfg->sections;
828 e = GNUNET_new (struct ConfigEntry);
829 e->key = GNUNET_strdup (option);
830 e->val = GNUNET_strdup (value);
831 e->next = sec->entries;
837 * Set a configuration value that should be a number.
839 * @param cfg configuration to update
840 * @param section section of interest
841 * @param option option of interest
842 * @param number value to set
845 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
846 const char *section, const char *option,
847 unsigned long long number)
851 GNUNET_snprintf (s, 64, "%llu", number);
852 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
857 * Get a configuration value that should be a number.
859 * @param cfg configuration to inspect
860 * @param section section of interest
861 * @param option option of interest
862 * @param number where to store the numeric value of the option
863 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
866 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
867 *cfg, const char *section,
869 unsigned long long *number)
871 struct ConfigEntry *e;
873 if (NULL == (e = find_entry (cfg, section, option)))
874 return GNUNET_SYSERR;
876 return GNUNET_SYSERR;
877 if (1 != SSCANF (e->val, "%llu", number))
878 return GNUNET_SYSERR;
883 * Get a configuration value that should be a floating point number.
885 * @param cfg configuration to inspect
886 * @param section section of interest
887 * @param option option of interest
888 * @param number where to store the floating value of the option
889 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
892 GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle
893 *cfg, const char *section,
897 struct ConfigEntry *e;
899 if (NULL == (e = find_entry (cfg, section, option)))
900 return GNUNET_SYSERR;
902 return GNUNET_SYSERR;
903 if (1 != SSCANF (e->val, "%f", number))
904 return GNUNET_SYSERR;
911 * Get a configuration value that should be a relative time.
913 * @param cfg configuration to inspect
914 * @param section section of interest
915 * @param option option of interest
916 * @param time set to the time value stored in the configuration
917 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
920 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
921 *cfg, const char *section,
923 struct GNUNET_TIME_Relative *time)
925 struct ConfigEntry *e;
927 if (NULL == (e = find_entry (cfg, section, option)))
928 return GNUNET_SYSERR;
930 return GNUNET_SYSERR;
931 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
936 * Get a configuration value that should be a size in bytes.
938 * @param cfg configuration to inspect
939 * @param section section of interest
940 * @param option option of interest
941 * @param size set to the size in bytes as stored in the configuration
942 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
945 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
948 unsigned long long *size)
950 struct ConfigEntry *e;
952 if (NULL == (e = find_entry (cfg, section, option)))
953 return GNUNET_SYSERR;
955 return GNUNET_SYSERR;
956 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
961 * Get a configuration value that should be a string.
963 * @param cfg configuration to inspect
964 * @param section section of interest
965 * @param option option of interest
966 * @param value will be set to a freshly allocated configuration
967 * value, or NULL if option is not specified
968 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
971 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
976 struct ConfigEntry *e;
978 LOG (GNUNET_ERROR_TYPE_DEBUG,
979 "Asked to retrieve string `%s' in section `%s'\n",
982 if ( (NULL == (e = find_entry (cfg, section, option))) ||
986 return GNUNET_SYSERR;
988 *value = GNUNET_strdup (e->val);
994 * Get a configuration value that should be in a set of
997 * @param cfg configuration to inspect
998 * @param section section of interest
999 * @param option option of interest
1000 * @param choices NULL-terminated list of legal values
1001 * @param value will be set to an entry in the legal list,
1002 * or NULL if option is not specified and no default given
1003 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1006 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1007 const char *section,
1009 const char *const *choices,
1012 struct ConfigEntry *e;
1015 if (NULL == (e = find_entry (cfg, section, option)))
1016 return GNUNET_SYSERR;
1017 for (i = 0; NULL != choices[i]; i++)
1018 if (0 == strcasecmp (choices[i], e->val))
1020 if (NULL == choices[i])
1022 LOG (GNUNET_ERROR_TYPE_ERROR,
1023 _("Configuration value '%s' for '%s'"
1024 " in section '%s' is not in set of legal choices\n"),
1028 return GNUNET_SYSERR;
1030 *value = choices[i];
1036 * Test if we have a value for a particular option
1038 * @param cfg configuration to inspect
1039 * @param section section of interest
1040 * @param option option of interest
1041 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1044 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1045 const char *section, const char *option)
1047 struct ConfigEntry *e;
1049 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1056 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1057 * where either in the "PATHS" section or the environtment "FOO" is
1058 * set to "DIRECTORY". We also support default expansion,
1059 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1060 * set in PATHS or the environment, and otherwise to "default". Note
1061 * that "default" itself can also be a $-expression, thus
1062 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1065 * @param cfg configuration to use for path expansion
1066 * @param orig string to $-expand (will be freed!)
1067 * @param depth recursion depth, used to detect recursive expansions
1068 * @return $-expanded string
1071 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1092 LOG (GNUNET_ERROR_TYPE_WARNING,
1093 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1097 LOG (GNUNET_ERROR_TYPE_DEBUG,
1098 "Asked to $-expand %s\n", orig);
1101 LOG (GNUNET_ERROR_TYPE_DEBUG,
1102 "Doesn't start with $ - not expanding\n");
1124 LOG (GNUNET_ERROR_TYPE_WARNING,
1125 _("Missing closing `%s' in option `%s'\n"),
1137 def = strchr (orig, ':');
1142 if ( ('-' == *def) ||
1145 def = GNUNET_strdup (def);
1153 while ( (orig[i] != '/') &&
1154 (orig[i] != '\\') &&
1155 (orig[i] != '\0') &&
1158 if (orig[i] == '\0')
1164 erased_char = orig[i];
1165 erased_pos = &orig[i];
1167 post = &orig[i + 1];
1170 LOG (GNUNET_ERROR_TYPE_DEBUG,
1171 "Split into `%s' and `%s' with default %s\n",
1176 GNUNET_CONFIGURATION_get_value_string (cfg,
1181 LOG (GNUNET_ERROR_TYPE_DEBUG,
1182 "Filename for `%s' is not in PATHS config section\n",
1184 if (NULL == (env = getenv (start)))
1186 LOG (GNUNET_ERROR_TYPE_DEBUG,
1187 "`%s' is not an environment variable\n",
1190 def = expand_dollar (cfg, def, depth + 1);
1195 start = GNUNET_strdup (start);
1197 *erased_pos = erased_char;
1198 LOG (GNUNET_ERROR_TYPE_WARNING,
1199 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1201 GNUNET_free (start);
1204 prefix = GNUNET_strdup (env);
1206 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1207 LOG (GNUNET_ERROR_TYPE_DEBUG,
1210 if ( (erased_pos) && ('}' != erased_char) )
1212 len = strlen (prefix) + 1;
1213 prefix = GNUNET_realloc (prefix, len + 1);
1214 prefix[len - 1] = erased_char;
1217 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1218 strcpy (result, prefix);
1219 strcat (result, post);
1220 GNUNET_free_non_null (def);
1221 GNUNET_free (prefix);
1223 LOG (GNUNET_ERROR_TYPE_DEBUG,
1224 "Expanded to `%s'\n",
1231 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1232 * where either in the "PATHS" section or the environtment "FOO" is
1233 * set to "DIRECTORY". We also support default expansion,
1234 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1235 * set in PATHS or the environment, and otherwise to "default". Note
1236 * that "default" itself can also be a $-expression, thus
1237 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1240 * @param cfg configuration to use for path expansion
1241 * @param orig string to $-expand (will be freed!). Note that multiple
1242 * $-expressions can be present in this string. They will all be
1244 * @return $-expanded string
1247 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1254 for (i = 0; '\0' != orig[i]; i++)
1258 dup = GNUNET_strdup (orig + i);
1259 dup = expand_dollar (cfg, dup, 0);
1260 len = strlen (dup) + 1;
1261 orig = GNUNET_realloc (orig, i + len);
1262 memcpy (orig + i, dup, len);
1270 * Get a configuration value that should be a string.
1272 * @param cfg configuration to inspect
1273 * @param section section of interest
1274 * @param option option of interest
1275 * @param value will be set to a freshly allocated configuration
1276 * value, or NULL if option is not specified
1277 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1280 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1281 const char *section,
1287 LOG (GNUNET_ERROR_TYPE_DEBUG,
1288 "Asked to retrieve filename `%s' in section `%s'\n",
1292 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "Failed to retrieve filename\n");
1297 return GNUNET_SYSERR;
1299 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1300 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1301 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1302 *value = GNUNET_STRINGS_filename_expand (tmp);
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1306 return GNUNET_SYSERR;
1312 * Get a configuration value that should be in a set of
1315 * @param cfg configuration to inspect
1316 * @param section section of interest
1317 * @param option option of interest
1318 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1321 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1322 const char *section,
1325 static const char *yesno[] = { "YES", "NO", NULL };
1330 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1331 if (ret == GNUNET_SYSERR)
1333 if (val == yesno[0])
1340 * Iterate over the set of filenames stored in a configuration value.
1342 * @param cfg configuration to inspect
1343 * @param section section of interest
1344 * @param option option of interest
1345 * @param cb function to call on each filename
1346 * @param cb_cls closure for @a cb
1347 * @return number of filenames iterated over, -1 on error
1350 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1351 const char *section,
1353 GNUNET_FileNameCallback cb,
1363 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1365 GNUNET_assert (list != NULL);
1370 while (pos[0] == ' ')
1372 if (strlen (pos) == 0)
1375 while ((end[0] != ' ') && (end[0] != '\0'))
1383 memmove (end, &end[1], strlen (&end[1]) + 1);
1385 /* illegal, but just keep it */
1388 /* illegal, but just ignore that there was a '/' */
1396 if (strlen (pos) > 0)
1399 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1401 ret = GNUNET_SYSERR;
1417 * @param value FIXME
1421 escape_name (const char *value)
1427 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1428 memset (escaped, 0, strlen (value) * 2 + 1);
1431 while (rpos[0] != '\0')
1454 * @param cls string we compare with (const char*)
1455 * @param fn filename we are currently looking at
1456 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1459 test_match (void *cls, const char *fn)
1461 const char *of = cls;
1463 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1468 * Append a filename to a configuration value that
1469 * represents a list of filenames
1471 * @param cfg configuration to update
1472 * @param section section of interest
1473 * @param option option of interest
1474 * @param value filename to append
1475 * @return #GNUNET_OK on success,
1476 * #GNUNET_NO if the filename already in the list
1477 * #GNUNET_SYSERR on error
1480 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1481 const char *section,
1489 if (GNUNET_SYSERR ==
1490 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1493 return GNUNET_NO; /* already exists */
1495 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1496 old = GNUNET_strdup ("");
1497 escaped = escape_name (value);
1498 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1500 if (strlen (old) > 0)
1502 strcat (nw, escaped);
1503 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1506 GNUNET_free (escaped);
1512 * Remove a filename from a configuration value that
1513 * represents a list of filenames
1515 * @param cfg configuration to update
1516 * @param section section of interest
1517 * @param option option of interest
1518 * @param value filename to remove
1519 * @return #GNUNET_OK on success,
1520 * #GNUNET_NO if the filename is not in the list,
1521 * #GNUNET_SYSERR on error
1524 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1525 *cfg, const char *section,
1536 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1538 match = escape_name (value);
1542 while (pos[0] == ' ')
1544 if (strlen (pos) == 0)
1547 while ((end[0] != ' ') && (end[0] != '\0'))
1558 /* illegal, but just keep it */
1561 /* illegal, but just ignore that there was a '/' */
1569 if (0 == strcmp (pos, match))
1572 memmove (pos, &end[1], strlen (&end[1]) + 1);
1580 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1582 GNUNET_free (match);
1591 GNUNET_free (match);
1597 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1598 * file in a directory, we trigger parsing on those files that
1601 * @param cls the cfg
1602 * @param filename file to parse
1603 * @return #GNUNET_OK on success
1606 parse_configuration_file (void *cls, const char *filename)
1608 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1612 /* Examine file extension */
1613 ext = strrchr (filename, '.');
1614 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1616 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1617 "Skipping file `%s'\n",
1622 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1628 * Load default configuration. This function will parse the
1629 * defaults from the given defaults_d directory.
1631 * @param cfg configuration to update
1632 * @param defaults_d directory with the defaults
1633 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1636 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1637 const char *defaults_d)
1639 if (GNUNET_SYSERR ==
1640 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1641 return GNUNET_SYSERR; /* no configuration at all found */
1647 * Load configuration (starts with defaults, then loads
1648 * system-specific configuration).
1650 * @param cfg configuration to update
1651 * @param filename name of the configuration file, NULL to load defaults
1652 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1655 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1656 const char *filename)
1661 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1663 return GNUNET_SYSERR;
1665 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1666 GNUNET_free (ipath);
1667 if (GNUNET_SYSERR ==
1668 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1670 GNUNET_free (baseconfig);
1671 return GNUNET_SYSERR; /* no configuration at all found */
1673 GNUNET_free (baseconfig);
1674 if ((NULL != filename) &&
1675 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1677 /* specified configuration not found */
1678 return GNUNET_SYSERR;
1681 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1683 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1689 /* end of configuration.c */