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,
355 "Deserializing contents of file `%s'\n",
358 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
360 /* restore dirty flag - anything we set in the meantime
368 * Test if there are configuration options that were
369 * changed since the last save.
371 * @param cfg configuration to inspect
372 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
375 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
382 * Serializes the given configuration.
384 * @param cfg configuration to serialize
385 * @param size will be set to the size of the serialized memory block
386 * @return the memory block where the serialized configuration is
387 * present. This memory should be freed by the caller
390 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
393 struct ConfigSection *sec;
394 struct ConfigEntry *ent;
404 /* Pass1 : calculate the buffer size required */
406 for (sec = cfg->sections; NULL != sec; sec = sec->next)
408 /* For each section we need to add 3 charaters: {'[',']','\n'} */
409 m_size += strlen (sec->name) + 3;
410 for (ent = sec->entries; NULL != ent; ent = ent->next)
412 if (NULL != ent->val)
414 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
416 while (NULL != (pos = strstr (pos, "\n")))
421 /* For each key = value pair we need to add 4 characters (2
422 spaces and 1 equal-to character and 1 new line) */
423 m_size += strlen (ent->key) + strlen (ent->val) + 4;
426 /* A new line after section end */
430 /* Pass2: Allocate memory and write the configuration to it */
431 mem = GNUNET_malloc (m_size);
437 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
438 GNUNET_assert (0 < len);
439 memcpy (mem + c_size, cbuf, len);
442 for (ent = sec->entries; NULL != ent; ent = ent->next)
444 if (NULL != ent->val)
446 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
447 strcpy (val, ent->val);
448 while (NULL != (pos = strstr (val, "\n")))
450 memmove (&pos[2], &pos[1], strlen (&pos[1]));
454 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
456 memcpy (mem + c_size, cbuf, len);
461 memcpy (mem + c_size, "\n", 1);
465 GNUNET_assert (c_size == m_size);
472 * Write configuration file.
474 * @param cfg configuration to write
475 * @param filename where to write the configuration
476 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
479 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
480 const char *filename)
486 fn = GNUNET_STRINGS_filename_expand (filename);
488 return GNUNET_SYSERR;
489 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
492 return GNUNET_SYSERR;
494 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
495 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
496 GNUNET_DISK_PERM_USER_READ
497 | GNUNET_DISK_PERM_USER_WRITE
498 | GNUNET_DISK_PERM_GROUP_READ
499 | GNUNET_DISK_PERM_GROUP_WRITE))
502 GNUNET_free (cfg_buf);
503 LOG (GNUNET_ERROR_TYPE_WARNING,
504 "Writing configration to file: %s failed\n", filename);
505 cfg->dirty = GNUNET_SYSERR; /* last write failed */
506 return GNUNET_SYSERR;
509 GNUNET_free (cfg_buf);
510 cfg->dirty = GNUNET_NO; /* last write succeeded */
516 * Iterate over all options in the configuration.
518 * @param cfg configuration to inspect
519 * @param iter function to call on each option
520 * @param iter_cls closure for @a iter
523 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
524 GNUNET_CONFIGURATION_Iterator iter,
527 struct ConfigSection *spos;
528 struct ConfigEntry *epos;
530 for (spos = cfg->sections; NULL != spos; spos = spos->next)
531 for (epos = spos->entries; NULL != epos; epos = epos->next)
532 if (NULL != epos->val)
533 iter (iter_cls, spos->name, epos->key, epos->val);
538 * Iterate over values of a section in the configuration.
540 * @param cfg configuration to inspect
541 * @param section the section
542 * @param iter function to call on each option
543 * @param iter_cls closure for @a iter
546 GNUNET_CONFIGURATION_iterate_section_values (const struct
547 GNUNET_CONFIGURATION_Handle *cfg,
549 GNUNET_CONFIGURATION_Iterator iter,
552 struct ConfigSection *spos;
553 struct ConfigEntry *epos;
555 spos = cfg->sections;
556 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
560 for (epos = spos->entries; NULL != epos; epos = epos->next)
561 if (NULL != epos->val)
562 iter (iter_cls, spos->name, epos->key, epos->val);
567 * Iterate over all sections in the configuration.
569 * @param cfg configuration to inspect
570 * @param iter function to call on each section
571 * @param iter_cls closure for @a iter
574 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
576 GNUNET_CONFIGURATION_Section_Iterator
577 iter, void *iter_cls)
579 struct ConfigSection *spos;
580 struct ConfigSection *next;
582 next = cfg->sections;
587 iter (iter_cls, spos->name);
593 * Remove the given section and all options in it.
595 * @param cfg configuration to inspect
596 * @param section name of the section to remove
599 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
602 struct ConfigSection *spos;
603 struct ConfigSection *prev;
604 struct ConfigEntry *ent;
607 spos = cfg->sections;
610 if (0 == strcasecmp (section, spos->name))
613 cfg->sections = spos->next;
615 prev->next = spos->next;
616 while (NULL != (ent = spos->entries))
618 spos->entries = ent->next;
619 GNUNET_free (ent->key);
620 GNUNET_free_non_null (ent->val);
622 cfg->dirty = GNUNET_YES;
624 GNUNET_free (spos->name);
635 * Copy a configuration value to the given target configuration.
636 * Overwrites existing entries.
638 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
639 * @param section section for the value
640 * @param option option name of the value
641 * @param value value to copy
644 copy_entry (void *cls,
649 struct GNUNET_CONFIGURATION_Handle *dst = cls;
651 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
656 * Duplicate an existing configuration object.
658 * @param cfg configuration to duplicate
659 * @return duplicate configuration
661 struct GNUNET_CONFIGURATION_Handle *
662 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
664 struct GNUNET_CONFIGURATION_Handle *ret;
666 ret = GNUNET_CONFIGURATION_create ();
667 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
673 * Find a section entry from a configuration.
675 * @param cfg configuration to search in
676 * @param section name of the section to look for
677 * @return matching entry, NULL if not found
679 static struct ConfigSection *
680 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
683 struct ConfigSection *pos;
686 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
693 * Find an entry from a configuration.
695 * @param cfg handle to the configuration
696 * @param section section the option is in
697 * @param key the option
698 * @return matching entry, NULL if not found
700 static struct ConfigEntry *
701 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
705 struct ConfigSection *sec;
706 struct ConfigEntry *pos;
708 if (NULL == (sec = find_section (cfg, section)))
711 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
718 * A callback function, compares entries from two configurations
719 * (default against a new configuration) and write the diffs in a
720 * diff-configuration object (the callback object).
722 * @param cls the diff configuration (`struct DiffHandle *`)
723 * @param section section for the value (of the default conf.)
724 * @param option option name of the value (of the default conf.)
725 * @param value value to copy (of the default conf.)
728 compare_entries (void *cls,
733 struct DiffHandle *dh = cls;
734 struct ConfigEntry *entNew;
736 entNew = find_entry (dh->cfg_default, section, option);
737 if ( (NULL != entNew) &&
738 (NULL != entNew->val) &&
739 (0 == strcmp (entNew->val, value)) )
741 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
746 * Compute configuration with only entries that have been changed
748 * @param cfg_default original configuration
749 * @param cfg_new new configuration
750 * @return configuration with only the differences, never NULL
752 struct GNUNET_CONFIGURATION_Handle *
753 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
754 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
756 struct DiffHandle diffHandle;
758 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
759 diffHandle.cfg_default = cfg_default;
760 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
761 return diffHandle.cfgDiff;
766 * Write only configuration entries that have been changed to configuration file
768 * @param cfg_default default configuration
769 * @param cfg_new new configuration
770 * @param filename where to write the configuration diff between default and new
771 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
774 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
776 const struct GNUNET_CONFIGURATION_Handle
777 *cfg_new, const char *filename)
780 struct GNUNET_CONFIGURATION_Handle *diff;
782 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
783 ret = GNUNET_CONFIGURATION_write (diff, filename);
784 GNUNET_CONFIGURATION_destroy (diff);
790 * Set a configuration value that should be a string.
792 * @param cfg configuration to update
793 * @param section section of interest
794 * @param option option of interest
795 * @param value value to set
798 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
799 const char *section, const char *option,
802 struct ConfigSection *sec;
803 struct ConfigEntry *e;
806 e = find_entry (cfg, section, option);
811 GNUNET_free_non_null (e->val);
816 nv = GNUNET_strdup (value);
817 GNUNET_free_non_null (e->val);
822 sec = find_section (cfg, section);
825 sec = GNUNET_new (struct ConfigSection);
826 sec->name = GNUNET_strdup (section);
827 sec->next = cfg->sections;
830 e = GNUNET_new (struct ConfigEntry);
831 e->key = GNUNET_strdup (option);
832 e->val = GNUNET_strdup (value);
833 e->next = sec->entries;
839 * Set a configuration value that should be a number.
841 * @param cfg configuration to update
842 * @param section section of interest
843 * @param option option of interest
844 * @param number value to set
847 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
848 const char *section, const char *option,
849 unsigned long long number)
853 GNUNET_snprintf (s, 64, "%llu", number);
854 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
859 * Get a configuration value that should be a number.
861 * @param cfg configuration to inspect
862 * @param section section of interest
863 * @param option option of interest
864 * @param number where to store the numeric value of the option
865 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
868 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
869 *cfg, const char *section,
871 unsigned long long *number)
873 struct ConfigEntry *e;
875 if (NULL == (e = find_entry (cfg, section, option)))
876 return GNUNET_SYSERR;
878 return GNUNET_SYSERR;
879 if (1 != SSCANF (e->val, "%llu", number))
880 return GNUNET_SYSERR;
885 * Get a configuration value that should be a floating point number.
887 * @param cfg configuration to inspect
888 * @param section section of interest
889 * @param option option of interest
890 * @param number where to store the floating value of the option
891 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
894 GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle
895 *cfg, const char *section,
899 struct ConfigEntry *e;
901 if (NULL == (e = find_entry (cfg, section, option)))
902 return GNUNET_SYSERR;
904 return GNUNET_SYSERR;
905 if (1 != SSCANF (e->val, "%f", number))
906 return GNUNET_SYSERR;
913 * Get a configuration value that should be a relative time.
915 * @param cfg configuration to inspect
916 * @param section section of interest
917 * @param option option of interest
918 * @param time set to the time value stored in the configuration
919 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
922 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
923 *cfg, const char *section,
925 struct GNUNET_TIME_Relative *time)
927 struct ConfigEntry *e;
929 if (NULL == (e = find_entry (cfg, section, option)))
930 return GNUNET_SYSERR;
932 return GNUNET_SYSERR;
933 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
938 * Get a configuration value that should be a size in bytes.
940 * @param cfg configuration to inspect
941 * @param section section of interest
942 * @param option option of interest
943 * @param size set to the size in bytes as stored in the configuration
944 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
947 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
950 unsigned long long *size)
952 struct ConfigEntry *e;
954 if (NULL == (e = find_entry (cfg, section, option)))
955 return GNUNET_SYSERR;
957 return GNUNET_SYSERR;
958 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
963 * Get a configuration value that should be a string.
965 * @param cfg configuration to inspect
966 * @param section section of interest
967 * @param option option of interest
968 * @param value will be set to a freshly allocated configuration
969 * value, or NULL if option is not specified
970 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
973 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
978 struct ConfigEntry *e;
980 LOG (GNUNET_ERROR_TYPE_DEBUG,
981 "Asked to retrieve string `%s' in section `%s'\n",
984 if ( (NULL == (e = find_entry (cfg, section, option))) ||
988 return GNUNET_SYSERR;
990 *value = GNUNET_strdup (e->val);
996 * Get a configuration value that should be in a set of
999 * @param cfg configuration to inspect
1000 * @param section section of interest
1001 * @param option option of interest
1002 * @param choices NULL-terminated list of legal values
1003 * @param value will be set to an entry in the legal list,
1004 * or NULL if option is not specified and no default given
1005 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1008 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1009 const char *section,
1011 const char *const *choices,
1014 struct ConfigEntry *e;
1017 if (NULL == (e = find_entry (cfg, section, option)))
1018 return GNUNET_SYSERR;
1019 for (i = 0; NULL != choices[i]; i++)
1020 if (0 == strcasecmp (choices[i], e->val))
1022 if (NULL == choices[i])
1024 LOG (GNUNET_ERROR_TYPE_ERROR,
1025 _("Configuration value '%s' for '%s'"
1026 " in section '%s' is not in set of legal choices\n"),
1030 return GNUNET_SYSERR;
1032 *value = choices[i];
1038 * Test if we have a value for a particular option
1040 * @param cfg configuration to inspect
1041 * @param section section of interest
1042 * @param option option of interest
1043 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1046 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1047 const char *section, const char *option)
1049 struct ConfigEntry *e;
1051 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1058 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1059 * where either in the "PATHS" section or the environtment "FOO" is
1060 * set to "DIRECTORY". We also support default expansion,
1061 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1062 * set in PATHS or the environment, and otherwise to "default". Note
1063 * that "default" itself can also be a $-expression, thus
1064 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1067 * @param cfg configuration to use for path expansion
1068 * @param orig string to $-expand (will be freed!)
1069 * @param depth recursion depth, used to detect recursive expansions
1070 * @return $-expanded string
1073 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1094 LOG (GNUNET_ERROR_TYPE_WARNING,
1095 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1099 LOG (GNUNET_ERROR_TYPE_DEBUG,
1100 "Asked to $-expand %s\n", orig);
1103 LOG (GNUNET_ERROR_TYPE_DEBUG,
1104 "Doesn't start with $ - not expanding\n");
1126 LOG (GNUNET_ERROR_TYPE_WARNING,
1127 _("Missing closing `%s' in option `%s'\n"),
1139 def = strchr (orig, ':');
1144 if ( ('-' == *def) ||
1147 def = GNUNET_strdup (def);
1155 while ( (orig[i] != '/') &&
1156 (orig[i] != '\\') &&
1157 (orig[i] != '\0') &&
1160 if (orig[i] == '\0')
1166 erased_char = orig[i];
1167 erased_pos = &orig[i];
1169 post = &orig[i + 1];
1172 LOG (GNUNET_ERROR_TYPE_DEBUG,
1173 "Split into `%s' and `%s' with default %s\n",
1178 GNUNET_CONFIGURATION_get_value_string (cfg,
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "Filename for `%s' is not in PATHS config section\n",
1186 if (NULL == (env = getenv (start)))
1188 LOG (GNUNET_ERROR_TYPE_DEBUG,
1189 "`%s' is not an environment variable\n",
1192 def = expand_dollar (cfg, def, depth + 1);
1197 start = GNUNET_strdup (start);
1199 *erased_pos = erased_char;
1200 LOG (GNUNET_ERROR_TYPE_WARNING,
1201 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1203 GNUNET_free (start);
1206 prefix = GNUNET_strdup (env);
1208 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1209 LOG (GNUNET_ERROR_TYPE_DEBUG,
1212 if ( (erased_pos) && ('}' != erased_char) )
1214 len = strlen (prefix) + 1;
1215 prefix = GNUNET_realloc (prefix, len + 1);
1216 prefix[len - 1] = erased_char;
1219 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1220 strcpy (result, prefix);
1221 strcat (result, post);
1222 GNUNET_free_non_null (def);
1223 GNUNET_free (prefix);
1225 LOG (GNUNET_ERROR_TYPE_DEBUG,
1226 "Expanded to `%s'\n",
1233 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1234 * where either in the "PATHS" section or the environtment "FOO" is
1235 * set to "DIRECTORY". We also support default expansion,
1236 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1237 * set in PATHS or the environment, and otherwise to "default". Note
1238 * that "default" itself can also be a $-expression, thus
1239 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1242 * @param cfg configuration to use for path expansion
1243 * @param orig string to $-expand (will be freed!). Note that multiple
1244 * $-expressions can be present in this string. They will all be
1246 * @return $-expanded string
1249 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1256 for (i = 0; '\0' != orig[i]; i++)
1260 dup = GNUNET_strdup (orig + i);
1261 dup = expand_dollar (cfg, dup, 0);
1262 len = strlen (dup) + 1;
1263 orig = GNUNET_realloc (orig, i + len);
1264 memcpy (orig + i, dup, len);
1272 * Get a configuration value that should be a string.
1274 * @param cfg configuration to inspect
1275 * @param section section of interest
1276 * @param option option of interest
1277 * @param value will be set to a freshly allocated configuration
1278 * value, or NULL if option is not specified
1279 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1282 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1283 const char *section,
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 "Asked to retrieve filename `%s' in section `%s'\n",
1294 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1296 LOG (GNUNET_ERROR_TYPE_DEBUG,
1297 "Failed to retrieve filename\n");
1299 return GNUNET_SYSERR;
1301 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1302 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1303 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1304 *value = GNUNET_STRINGS_filename_expand (tmp);
1306 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1308 return GNUNET_SYSERR;
1314 * Get a configuration value that should be in a set of
1317 * @param cfg configuration to inspect
1318 * @param section section of interest
1319 * @param option option of interest
1320 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1323 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1324 const char *section,
1327 static const char *yesno[] = { "YES", "NO", NULL };
1332 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1333 if (ret == GNUNET_SYSERR)
1335 if (val == yesno[0])
1342 * Iterate over the set of filenames stored in a configuration value.
1344 * @param cfg configuration to inspect
1345 * @param section section of interest
1346 * @param option option of interest
1347 * @param cb function to call on each filename
1348 * @param cb_cls closure for @a cb
1349 * @return number of filenames iterated over, -1 on error
1352 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1353 const char *section,
1355 GNUNET_FileNameCallback cb,
1365 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1367 GNUNET_assert (list != NULL);
1372 while (pos[0] == ' ')
1374 if (strlen (pos) == 0)
1377 while ((end[0] != ' ') && (end[0] != '\0'))
1385 memmove (end, &end[1], strlen (&end[1]) + 1);
1387 /* illegal, but just keep it */
1390 /* illegal, but just ignore that there was a '/' */
1398 if (strlen (pos) > 0)
1401 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1403 ret = GNUNET_SYSERR;
1419 * @param value FIXME
1423 escape_name (const char *value)
1429 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1430 memset (escaped, 0, strlen (value) * 2 + 1);
1433 while (rpos[0] != '\0')
1456 * @param cls string we compare with (const char*)
1457 * @param fn filename we are currently looking at
1458 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1461 test_match (void *cls, const char *fn)
1463 const char *of = cls;
1465 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1470 * Append a filename to a configuration value that
1471 * represents a list of filenames
1473 * @param cfg configuration to update
1474 * @param section section of interest
1475 * @param option option of interest
1476 * @param value filename to append
1477 * @return #GNUNET_OK on success,
1478 * #GNUNET_NO if the filename already in the list
1479 * #GNUNET_SYSERR on error
1482 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1483 const char *section,
1491 if (GNUNET_SYSERR ==
1492 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1495 return GNUNET_NO; /* already exists */
1497 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1498 old = GNUNET_strdup ("");
1499 escaped = escape_name (value);
1500 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1502 if (strlen (old) > 0)
1504 strcat (nw, escaped);
1505 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1508 GNUNET_free (escaped);
1514 * Remove a filename from a configuration value that
1515 * represents a list of filenames
1517 * @param cfg configuration to update
1518 * @param section section of interest
1519 * @param option option of interest
1520 * @param value filename to remove
1521 * @return #GNUNET_OK on success,
1522 * #GNUNET_NO if the filename is not in the list,
1523 * #GNUNET_SYSERR on error
1526 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1527 *cfg, const char *section,
1538 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1540 match = escape_name (value);
1544 while (pos[0] == ' ')
1546 if (strlen (pos) == 0)
1549 while ((end[0] != ' ') && (end[0] != '\0'))
1560 /* illegal, but just keep it */
1563 /* illegal, but just ignore that there was a '/' */
1571 if (0 == strcmp (pos, match))
1574 memmove (pos, &end[1], strlen (&end[1]) + 1);
1582 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1584 GNUNET_free (match);
1593 GNUNET_free (match);
1599 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1600 * file in a directory, we trigger parsing on those files that
1603 * @param cls the cfg
1604 * @param filename file to parse
1605 * @return #GNUNET_OK on success
1608 parse_configuration_file (void *cls, const char *filename)
1610 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1614 /* Examine file extension */
1615 ext = strrchr (filename, '.');
1616 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1618 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1619 "Skipping file `%s'\n",
1624 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1630 * Load default configuration. This function will parse the
1631 * defaults from the given defaults_d directory.
1633 * @param cfg configuration to update
1634 * @param defaults_d directory with the defaults
1635 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1638 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1639 const char *defaults_d)
1641 if (GNUNET_SYSERR ==
1642 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1643 return GNUNET_SYSERR; /* no configuration at all found */
1649 * Load configuration (starts with defaults, then loads
1650 * system-specific configuration).
1652 * @param cfg configuration to update
1653 * @param filename name of the configuration file, NULL to load defaults
1654 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1657 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1658 const char *filename)
1663 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1665 return GNUNET_SYSERR;
1667 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1668 GNUNET_free (ipath);
1669 if (GNUNET_SYSERR ==
1670 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1672 GNUNET_free (baseconfig);
1673 return GNUNET_SYSERR; /* no configuration at all found */
1675 GNUNET_free (baseconfig);
1676 if ((NULL != filename) &&
1677 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1679 /* specified configuration not found */
1680 return GNUNET_SYSERR;
1683 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1685 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1691 /* end of configuration.c */