2 This file is part of GNUnet.
3 Copyright (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",
353 return GNUNET_SYSERR;
355 LOG (GNUNET_ERROR_TYPE_DEBUG,
356 "Deserializing contents of file `%s'\n",
359 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
361 /* restore dirty flag - anything we set in the meantime
369 * Test if there are configuration options that were
370 * changed since the last save.
372 * @param cfg configuration to inspect
373 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
376 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
383 * Serializes the given configuration.
385 * @param cfg configuration to serialize
386 * @param size will be set to the size of the serialized memory block
387 * @return the memory block where the serialized configuration is
388 * present. This memory should be freed by the caller
391 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
394 struct ConfigSection *sec;
395 struct ConfigEntry *ent;
405 /* Pass1 : calculate the buffer size required */
407 for (sec = cfg->sections; NULL != sec; sec = sec->next)
409 /* For each section we need to add 3 charaters: {'[',']','\n'} */
410 m_size += strlen (sec->name) + 3;
411 for (ent = sec->entries; NULL != ent; ent = ent->next)
413 if (NULL != ent->val)
415 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
417 while (NULL != (pos = strstr (pos, "\n")))
422 /* For each key = value pair we need to add 4 characters (2
423 spaces and 1 equal-to character and 1 new line) */
424 m_size += strlen (ent->key) + strlen (ent->val) + 4;
427 /* A new line after section end */
431 /* Pass2: Allocate memory and write the configuration to it */
432 mem = GNUNET_malloc (m_size);
438 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
439 GNUNET_assert (0 < len);
440 memcpy (mem + c_size, cbuf, len);
443 for (ent = sec->entries; NULL != ent; ent = ent->next)
445 if (NULL != ent->val)
447 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
448 strcpy (val, ent->val);
449 while (NULL != (pos = strstr (val, "\n")))
451 memmove (&pos[2], &pos[1], strlen (&pos[1]));
455 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
457 memcpy (mem + c_size, cbuf, len);
462 memcpy (mem + c_size, "\n", 1);
466 GNUNET_assert (c_size == m_size);
473 * Write configuration file.
475 * @param cfg configuration to write
476 * @param filename where to write the configuration
477 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
480 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
481 const char *filename)
487 fn = GNUNET_STRINGS_filename_expand (filename);
489 return GNUNET_SYSERR;
490 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
493 return GNUNET_SYSERR;
495 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
496 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
497 GNUNET_DISK_PERM_USER_READ
498 | GNUNET_DISK_PERM_USER_WRITE
499 | GNUNET_DISK_PERM_GROUP_READ
500 | GNUNET_DISK_PERM_GROUP_WRITE))
503 GNUNET_free (cfg_buf);
504 LOG (GNUNET_ERROR_TYPE_WARNING,
505 "Writing configration to file: %s failed\n", filename);
506 cfg->dirty = GNUNET_SYSERR; /* last write failed */
507 return GNUNET_SYSERR;
510 GNUNET_free (cfg_buf);
511 cfg->dirty = GNUNET_NO; /* last write succeeded */
517 * Iterate over all options in the configuration.
519 * @param cfg configuration to inspect
520 * @param iter function to call on each option
521 * @param iter_cls closure for @a iter
524 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
525 GNUNET_CONFIGURATION_Iterator iter,
528 struct ConfigSection *spos;
529 struct ConfigEntry *epos;
531 for (spos = cfg->sections; NULL != spos; spos = spos->next)
532 for (epos = spos->entries; NULL != epos; epos = epos->next)
533 if (NULL != epos->val)
534 iter (iter_cls, spos->name, epos->key, epos->val);
539 * Iterate over values of a section in the configuration.
541 * @param cfg configuration to inspect
542 * @param section the section
543 * @param iter function to call on each option
544 * @param iter_cls closure for @a iter
547 GNUNET_CONFIGURATION_iterate_section_values (const struct
548 GNUNET_CONFIGURATION_Handle *cfg,
550 GNUNET_CONFIGURATION_Iterator iter,
553 struct ConfigSection *spos;
554 struct ConfigEntry *epos;
556 spos = cfg->sections;
557 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
561 for (epos = spos->entries; NULL != epos; epos = epos->next)
562 if (NULL != epos->val)
563 iter (iter_cls, spos->name, epos->key, epos->val);
568 * Iterate over all sections in the configuration.
570 * @param cfg configuration to inspect
571 * @param iter function to call on each section
572 * @param iter_cls closure for @a iter
575 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
576 GNUNET_CONFIGURATION_Section_Iterator iter,
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 *cfg,
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 * Get crockford32-encoded fixed-size binary data from a configuration.
1040 * @param cfg configuration to access
1041 * @param section section to access
1042 * @param option option to access
1043 * @param buf where to store the decoded binary result
1044 * @param buf_size exact number of bytes to store in @a buf
1045 * @return #GNUNET_OK on success
1046 * #GNUNET_NO is the value does not exist
1047 * #GNUNET_SYSERR on decoding error
1050 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1051 const char *section,
1061 (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1066 data_size = (strlen (enc) * 5) / 8;
1067 if (data_size != buf_size)
1070 return GNUNET_SYSERR;
1073 GNUNET_STRINGS_string_to_data (enc,
1078 return GNUNET_SYSERR;
1086 * Test if we have a value for a particular option
1088 * @param cfg configuration to inspect
1089 * @param section section of interest
1090 * @param option option of interest
1091 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1094 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1095 const char *section,
1098 struct ConfigEntry *e;
1100 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1107 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1108 * where either in the "PATHS" section or the environtment "FOO" is
1109 * set to "DIRECTORY". We also support default expansion,
1110 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1111 * set in PATHS or the environment, and otherwise to "default". Note
1112 * that "default" itself can also be a $-expression, thus
1113 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1116 * @param cfg configuration to use for path expansion
1117 * @param orig string to $-expand (will be freed!)
1118 * @param depth recursion depth, used to detect recursive expansions
1119 * @return $-expanded string
1122 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1143 LOG (GNUNET_ERROR_TYPE_WARNING,
1144 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1148 LOG (GNUNET_ERROR_TYPE_DEBUG,
1149 "Asked to $-expand %s\n", orig);
1152 LOG (GNUNET_ERROR_TYPE_DEBUG,
1153 "Doesn't start with $ - not expanding\n");
1175 LOG (GNUNET_ERROR_TYPE_WARNING,
1176 _("Missing closing `%s' in option `%s'\n"),
1188 def = strchr (orig, ':');
1193 if ( ('-' == *def) ||
1196 def = GNUNET_strdup (def);
1204 while ( (orig[i] != '/') &&
1205 (orig[i] != '\\') &&
1206 (orig[i] != '\0') &&
1209 if (orig[i] == '\0')
1215 erased_char = orig[i];
1216 erased_pos = &orig[i];
1218 post = &orig[i + 1];
1221 LOG (GNUNET_ERROR_TYPE_DEBUG,
1222 "Split into `%s' and `%s' with default %s\n",
1227 GNUNET_CONFIGURATION_get_value_string (cfg,
1232 LOG (GNUNET_ERROR_TYPE_DEBUG,
1233 "Filename for `%s' is not in PATHS config section\n",
1235 if (NULL == (env = getenv (start)))
1237 LOG (GNUNET_ERROR_TYPE_DEBUG,
1238 "`%s' is not an environment variable\n",
1241 def = expand_dollar (cfg, def, depth + 1);
1246 start = GNUNET_strdup (start);
1248 *erased_pos = erased_char;
1249 LOG (GNUNET_ERROR_TYPE_WARNING,
1250 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1252 GNUNET_free (start);
1255 prefix = GNUNET_strdup (env);
1257 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1258 LOG (GNUNET_ERROR_TYPE_DEBUG,
1261 if ( (erased_pos) && ('}' != erased_char) )
1263 len = strlen (prefix) + 1;
1264 prefix = GNUNET_realloc (prefix, len + 1);
1265 prefix[len - 1] = erased_char;
1268 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1269 strcpy (result, prefix);
1270 strcat (result, post);
1271 GNUNET_free_non_null (def);
1272 GNUNET_free (prefix);
1274 LOG (GNUNET_ERROR_TYPE_DEBUG,
1275 "Expanded to `%s'\n",
1282 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1283 * where either in the "PATHS" section or the environtment "FOO" is
1284 * set to "DIRECTORY". We also support default expansion,
1285 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1286 * set in PATHS or the environment, and otherwise to "default". Note
1287 * that "default" itself can also be a $-expression, thus
1288 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1291 * @param cfg configuration to use for path expansion
1292 * @param orig string to $-expand (will be freed!). Note that multiple
1293 * $-expressions can be present in this string. They will all be
1295 * @return $-expanded string
1298 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1305 for (i = 0; '\0' != orig[i]; i++)
1309 dup = GNUNET_strdup (orig + i);
1310 dup = expand_dollar (cfg, dup, 0);
1311 len = strlen (dup) + 1;
1312 orig = GNUNET_realloc (orig, i + len);
1313 memcpy (orig + i, dup, len);
1321 * Get a configuration value that should be a string.
1323 * @param cfg configuration to inspect
1324 * @param section section of interest
1325 * @param option option of interest
1326 * @param value will be set to a freshly allocated configuration
1327 * value, or NULL if option is not specified
1328 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1331 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1332 const char *section,
1338 LOG (GNUNET_ERROR_TYPE_DEBUG,
1339 "Asked to retrieve filename `%s' in section `%s'\n",
1343 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1345 LOG (GNUNET_ERROR_TYPE_DEBUG,
1346 "Failed to retrieve filename\n");
1348 return GNUNET_SYSERR;
1350 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1351 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1352 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1353 *value = GNUNET_STRINGS_filename_expand (tmp);
1355 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1357 return GNUNET_SYSERR;
1363 * Get a configuration value that should be in a set of
1366 * @param cfg configuration to inspect
1367 * @param section section of interest
1368 * @param option option of interest
1369 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1372 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1373 const char *section,
1376 static const char *yesno[] = { "YES", "NO", NULL };
1381 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1382 if (ret == GNUNET_SYSERR)
1384 if (val == yesno[0])
1391 * Iterate over the set of filenames stored in a configuration value.
1393 * @param cfg configuration to inspect
1394 * @param section section of interest
1395 * @param option option of interest
1396 * @param cb function to call on each filename
1397 * @param cb_cls closure for @a cb
1398 * @return number of filenames iterated over, -1 on error
1401 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1402 const char *section,
1404 GNUNET_FileNameCallback cb,
1414 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1416 GNUNET_assert (list != NULL);
1421 while (pos[0] == ' ')
1423 if (strlen (pos) == 0)
1426 while ((end[0] != ' ') && (end[0] != '\0'))
1434 memmove (end, &end[1], strlen (&end[1]) + 1);
1436 /* illegal, but just keep it */
1439 /* illegal, but just ignore that there was a '/' */
1447 if (strlen (pos) > 0)
1450 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1452 ret = GNUNET_SYSERR;
1468 * @param value FIXME
1472 escape_name (const char *value)
1478 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1479 memset (escaped, 0, strlen (value) * 2 + 1);
1482 while (rpos[0] != '\0')
1505 * @param cls string we compare with (const char*)
1506 * @param fn filename we are currently looking at
1507 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1510 test_match (void *cls, const char *fn)
1512 const char *of = cls;
1514 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1519 * Append a filename to a configuration value that
1520 * represents a list of filenames
1522 * @param cfg configuration to update
1523 * @param section section of interest
1524 * @param option option of interest
1525 * @param value filename to append
1526 * @return #GNUNET_OK on success,
1527 * #GNUNET_NO if the filename already in the list
1528 * #GNUNET_SYSERR on error
1531 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1532 const char *section,
1540 if (GNUNET_SYSERR ==
1541 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1544 return GNUNET_NO; /* already exists */
1546 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1547 old = GNUNET_strdup ("");
1548 escaped = escape_name (value);
1549 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1551 if (strlen (old) > 0)
1553 strcat (nw, escaped);
1554 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1557 GNUNET_free (escaped);
1563 * Remove a filename from a configuration value that
1564 * represents a list of filenames
1566 * @param cfg configuration to update
1567 * @param section section of interest
1568 * @param option option of interest
1569 * @param value filename to remove
1570 * @return #GNUNET_OK on success,
1571 * #GNUNET_NO if the filename is not in the list,
1572 * #GNUNET_SYSERR on error
1575 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1576 *cfg, const char *section,
1587 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1589 match = escape_name (value);
1593 while (pos[0] == ' ')
1595 if (strlen (pos) == 0)
1598 while ((end[0] != ' ') && (end[0] != '\0'))
1609 /* illegal, but just keep it */
1612 /* illegal, but just ignore that there was a '/' */
1620 if (0 == strcmp (pos, match))
1623 memmove (pos, &end[1], strlen (&end[1]) + 1);
1631 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1633 GNUNET_free (match);
1642 GNUNET_free (match);
1648 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1649 * file in a directory, we trigger parsing on those files that
1652 * @param cls the cfg
1653 * @param filename file to parse
1654 * @return #GNUNET_OK on success
1657 parse_configuration_file (void *cls, const char *filename)
1659 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1663 /* Examine file extension */
1664 ext = strrchr (filename, '.');
1665 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1667 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1668 "Skipping file `%s'\n",
1673 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1679 * Load default configuration. This function will parse the
1680 * defaults from the given defaults_d directory.
1682 * @param cfg configuration to update
1683 * @param defaults_d directory with the defaults
1684 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1687 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1688 const char *defaults_d)
1690 if (GNUNET_SYSERR ==
1691 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1692 return GNUNET_SYSERR; /* no configuration at all found */
1698 * Load configuration (starts with defaults, then loads
1699 * system-specific configuration).
1701 * @param cfg configuration to update
1702 * @param filename name of the configuration file, NULL to load defaults
1703 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1706 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1707 const char *filename)
1712 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1714 return GNUNET_SYSERR;
1716 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1717 GNUNET_free (ipath);
1718 if (GNUNET_SYSERR ==
1719 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1721 GNUNET_free (baseconfig);
1722 return GNUNET_SYSERR; /* no configuration at all found */
1724 GNUNET_free (baseconfig);
1725 if ((NULL != filename) &&
1726 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1728 /* specified configuration not found */
1729 return GNUNET_SYSERR;
1732 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1734 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1740 /* end of configuration.c */