2 This file is part of GNUnet.
3 Copyright (C) 2006, 2007, 2008, 2009, 2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file src/util/configuration.c
23 * @brief configuration management
24 * @author Christian Grothoff
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_strings_lib.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_disk_lib.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
38 * @brief configuration entry
44 * This is a linked list.
46 struct ConfigEntry *next;
54 * current, commited value
61 * @brief configuration section
66 * This is a linked list.
68 struct ConfigSection *next;
71 * entries in the section
73 struct ConfigEntry *entries;
83 * @brief configuration data
85 struct GNUNET_CONFIGURATION_Handle
88 * Configuration sections.
90 struct ConfigSection *sections;
93 * Modification indication since last save
94 * #GNUNET_NO if clean, #GNUNET_YES if dirty,
95 * #GNUNET_SYSERR on error (i.e. last save failed)
103 * Used for diffing a configuration object against
108 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
110 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
115 * Create a GNUNET_CONFIGURATION_Handle.
117 * @return fresh configuration object
119 struct GNUNET_CONFIGURATION_Handle *
120 GNUNET_CONFIGURATION_create ()
122 return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
127 * Destroy configuration object.
129 * @param cfg configuration to destroy
132 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
134 struct ConfigSection *sec;
136 while (NULL != (sec = cfg->sections))
137 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
143 * De-serializes configuration
145 * @param cfg configuration to update
146 * @param mem the memory block of serialized configuration
147 * @param size the size of the memory block
148 * @param basedir set to path from which we recursively load configuration
149 * from inlined configurations; NULL if not and raise warnings
150 * when we come across them
151 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
154 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
175 section = GNUNET_strdup ("");
179 while (r_bytes < size)
181 GNUNET_free_non_null (line_orig);
182 /* fgets-like behaviour on buffer */
183 to_read = size - r_bytes;
184 pos = memchr (&mem[r_bytes], '\n', to_read);
187 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
188 r_bytes += line_size;
192 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
193 r_bytes += line_size + 1;
196 /* increment line number */
198 /* tabs and '\r' are whitespace */
199 emptyline = GNUNET_YES;
200 for (i = 0; i < line_size; i++)
207 emptyline = GNUNET_NO;
209 /* ignore empty lines */
210 if (GNUNET_YES == emptyline)
213 /* remove tailing whitespace */
214 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
217 /* remove leading whitespace */
218 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
220 /* ignore comments */
221 if ( ('#' == line[0]) || ('%' == line[0]) )
224 /* handle special "@INLINE@" directive */
225 if (0 == strncasecmp (line,
227 strlen ("@INLINE@ ")))
230 value = &line[strlen ("@INLINE@ ")];
235 GNUNET_asprintf (&fn,
240 GNUNET_CONFIGURATION_parse (cfg,
244 ret = GNUNET_SYSERR; /* failed to parse included config */
251 LOG (GNUNET_ERROR_TYPE_DEBUG,
252 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
258 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
261 line[line_size - 1] = '\0';
263 GNUNET_free (section);
264 section = GNUNET_strdup (value);
267 if (NULL != (eq = strchr (line, '=')))
270 tag = GNUNET_strndup (line, eq - line);
271 /* remove tailing whitespace */
272 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
275 /* Strip whitespace */
277 while (isspace ((unsigned char) value[0]))
279 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
284 if ( ('"' == value[0]) &&
285 ('"' == value[strlen (value) - 1]) )
287 value[strlen (value) - 1] = '\0';
290 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
295 LOG (GNUNET_ERROR_TYPE_WARNING,
296 _("Syntax error while deserializing in line %u\n"),
301 GNUNET_free_non_null (line_orig);
302 GNUNET_free (section);
303 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
309 * Parse a configuration file, add all of the options in the
310 * file to the configuration environment.
312 * @param cfg configuration to update
313 * @param filename name of the configuration file
314 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
317 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
318 const char *filename)
328 fn = GNUNET_STRINGS_filename_expand (filename);
329 LOG (GNUNET_ERROR_TYPE_DEBUG,
330 "Asked to parse config file `%s'\n",
333 return GNUNET_SYSERR;
334 dirty = cfg->dirty; /* back up value! */
336 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
338 LOG (GNUNET_ERROR_TYPE_WARNING,
339 "Error while determining the file size of `%s'\n",
342 return GNUNET_SYSERR;
346 GNUNET_break (0); /* File size is more than the heap size */
348 return GNUNET_SYSERR;
351 mem = GNUNET_malloc (fs);
352 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
354 LOG (GNUNET_ERROR_TYPE_WARNING,
355 _("Error while reading file `%s'\n"),
359 return GNUNET_SYSERR;
361 LOG (GNUNET_ERROR_TYPE_DEBUG,
362 "Deserializing contents of file `%s'\n",
364 endsep = strrchr (fn, (int) '/');
367 ret = GNUNET_CONFIGURATION_deserialize (cfg,
373 /* restore dirty flag - anything we set in the meantime
381 * Test if there are configuration options that were
382 * changed since the last save.
384 * @param cfg configuration to inspect
385 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
388 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
395 * Serializes the given configuration.
397 * @param cfg configuration to serialize
398 * @param size will be set to the size of the serialized memory block
399 * @return the memory block where the serialized configuration is
400 * present. This memory should be freed by the caller
403 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
406 struct ConfigSection *sec;
407 struct ConfigEntry *ent;
417 /* Pass1 : calculate the buffer size required */
419 for (sec = cfg->sections; NULL != sec; sec = sec->next)
421 /* For each section we need to add 3 charaters: {'[',']','\n'} */
422 m_size += strlen (sec->name) + 3;
423 for (ent = sec->entries; NULL != ent; ent = ent->next)
425 if (NULL != ent->val)
427 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
429 while (NULL != (pos = strstr (pos, "\n")))
434 /* For each key = value pair we need to add 4 characters (2
435 spaces and 1 equal-to character and 1 new line) */
436 m_size += strlen (ent->key) + strlen (ent->val) + 4;
439 /* A new line after section end */
443 /* Pass2: Allocate memory and write the configuration to it */
444 mem = GNUNET_malloc (m_size);
450 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
451 GNUNET_assert (0 < len);
452 GNUNET_memcpy (mem + c_size, cbuf, len);
455 for (ent = sec->entries; NULL != ent; ent = ent->next)
457 if (NULL != ent->val)
459 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
460 strcpy (val, ent->val);
461 while (NULL != (pos = strstr (val, "\n")))
463 memmove (&pos[2], &pos[1], strlen (&pos[1]));
467 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
469 GNUNET_memcpy (mem + c_size, cbuf, len);
474 GNUNET_memcpy (mem + c_size, "\n", 1);
478 GNUNET_assert (c_size == m_size);
485 * Write configuration file.
487 * @param cfg configuration to write
488 * @param filename where to write the configuration
489 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
492 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
493 const char *filename)
499 fn = GNUNET_STRINGS_filename_expand (filename);
501 return GNUNET_SYSERR;
502 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
505 return GNUNET_SYSERR;
507 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
508 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
509 GNUNET_DISK_PERM_USER_READ
510 | GNUNET_DISK_PERM_USER_WRITE
511 | GNUNET_DISK_PERM_GROUP_READ
512 | GNUNET_DISK_PERM_GROUP_WRITE))
515 GNUNET_free (cfg_buf);
516 LOG (GNUNET_ERROR_TYPE_WARNING,
517 "Writing configration to file `%s' failed\n",
519 cfg->dirty = GNUNET_SYSERR; /* last write failed */
520 return GNUNET_SYSERR;
523 GNUNET_free (cfg_buf);
524 cfg->dirty = GNUNET_NO; /* last write succeeded */
530 * Iterate over all options in the configuration.
532 * @param cfg configuration to inspect
533 * @param iter function to call on each option
534 * @param iter_cls closure for @a iter
537 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
538 GNUNET_CONFIGURATION_Iterator iter,
541 struct ConfigSection *spos;
542 struct ConfigEntry *epos;
544 for (spos = cfg->sections; NULL != spos; spos = spos->next)
545 for (epos = spos->entries; NULL != epos; epos = epos->next)
546 if (NULL != epos->val)
547 iter (iter_cls, spos->name, epos->key, epos->val);
552 * Iterate over values of a section in the configuration.
554 * @param cfg configuration to inspect
555 * @param section the section
556 * @param iter function to call on each option
557 * @param iter_cls closure for @a iter
560 GNUNET_CONFIGURATION_iterate_section_values (const struct
561 GNUNET_CONFIGURATION_Handle *cfg,
563 GNUNET_CONFIGURATION_Iterator iter,
566 struct ConfigSection *spos;
567 struct ConfigEntry *epos;
569 spos = cfg->sections;
570 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
574 for (epos = spos->entries; NULL != epos; epos = epos->next)
575 if (NULL != epos->val)
576 iter (iter_cls, spos->name, epos->key, epos->val);
581 * Iterate over all sections in the configuration.
583 * @param cfg configuration to inspect
584 * @param iter function to call on each section
585 * @param iter_cls closure for @a iter
588 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
589 GNUNET_CONFIGURATION_Section_Iterator iter,
592 struct ConfigSection *spos;
593 struct ConfigSection *next;
595 next = cfg->sections;
600 iter (iter_cls, spos->name);
606 * Remove the given section and all options in it.
608 * @param cfg configuration to inspect
609 * @param section name of the section to remove
612 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
615 struct ConfigSection *spos;
616 struct ConfigSection *prev;
617 struct ConfigEntry *ent;
620 spos = cfg->sections;
623 if (0 == strcasecmp (section, spos->name))
626 cfg->sections = spos->next;
628 prev->next = spos->next;
629 while (NULL != (ent = spos->entries))
631 spos->entries = ent->next;
632 GNUNET_free (ent->key);
633 GNUNET_free_non_null (ent->val);
635 cfg->dirty = GNUNET_YES;
637 GNUNET_free (spos->name);
648 * Copy a configuration value to the given target configuration.
649 * Overwrites existing entries.
651 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
652 * @param section section for the value
653 * @param option option name of the value
654 * @param value value to copy
657 copy_entry (void *cls,
662 struct GNUNET_CONFIGURATION_Handle *dst = cls;
664 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
669 * Duplicate an existing configuration object.
671 * @param cfg configuration to duplicate
672 * @return duplicate configuration
674 struct GNUNET_CONFIGURATION_Handle *
675 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
677 struct GNUNET_CONFIGURATION_Handle *ret;
679 ret = GNUNET_CONFIGURATION_create ();
680 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
686 * Find a section entry from a configuration.
688 * @param cfg configuration to search in
689 * @param section name of the section to look for
690 * @return matching entry, NULL if not found
692 static struct ConfigSection *
693 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
696 struct ConfigSection *pos;
699 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
706 * Find an entry from a configuration.
708 * @param cfg handle to the configuration
709 * @param section section the option is in
710 * @param key the option
711 * @return matching entry, NULL if not found
713 static struct ConfigEntry *
714 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
718 struct ConfigSection *sec;
719 struct ConfigEntry *pos;
721 if (NULL == (sec = find_section (cfg, section)))
724 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
731 * A callback function, compares entries from two configurations
732 * (default against a new configuration) and write the diffs in a
733 * diff-configuration object (the callback object).
735 * @param cls the diff configuration (`struct DiffHandle *`)
736 * @param section section for the value (of the default conf.)
737 * @param option option name of the value (of the default conf.)
738 * @param value value to copy (of the default conf.)
741 compare_entries (void *cls,
746 struct DiffHandle *dh = cls;
747 struct ConfigEntry *entNew;
749 entNew = find_entry (dh->cfg_default, section, option);
750 if ( (NULL != entNew) &&
751 (NULL != entNew->val) &&
752 (0 == strcmp (entNew->val, value)) )
754 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
759 * Compute configuration with only entries that have been changed
761 * @param cfg_default original configuration
762 * @param cfg_new new configuration
763 * @return configuration with only the differences, never NULL
765 struct GNUNET_CONFIGURATION_Handle *
766 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
767 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
769 struct DiffHandle diffHandle;
771 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
772 diffHandle.cfg_default = cfg_default;
773 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
774 return diffHandle.cfgDiff;
779 * Write only configuration entries that have been changed to configuration file
781 * @param cfg_default default configuration
782 * @param cfg_new new configuration
783 * @param filename where to write the configuration diff between default and new
784 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
787 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
789 const struct GNUNET_CONFIGURATION_Handle
790 *cfg_new, const char *filename)
793 struct GNUNET_CONFIGURATION_Handle *diff;
795 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
796 ret = GNUNET_CONFIGURATION_write (diff, filename);
797 GNUNET_CONFIGURATION_destroy (diff);
803 * Set a configuration value that should be a string.
805 * @param cfg configuration to update
806 * @param section section of interest
807 * @param option option of interest
808 * @param value value to set
811 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
812 const char *section, const char *option,
815 struct ConfigSection *sec;
816 struct ConfigEntry *e;
819 e = find_entry (cfg, section, option);
824 GNUNET_free_non_null (e->val);
829 nv = GNUNET_strdup (value);
830 GNUNET_free_non_null (e->val);
835 sec = find_section (cfg, section);
838 sec = GNUNET_new (struct ConfigSection);
839 sec->name = GNUNET_strdup (section);
840 sec->next = cfg->sections;
843 e = GNUNET_new (struct ConfigEntry);
844 e->key = GNUNET_strdup (option);
845 e->val = GNUNET_strdup (value);
846 e->next = sec->entries;
852 * Set a configuration value that should be a number.
854 * @param cfg configuration to update
855 * @param section section of interest
856 * @param option option of interest
857 * @param number value to set
860 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
861 const char *section, const char *option,
862 unsigned long long number)
866 GNUNET_snprintf (s, 64, "%llu", number);
867 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
872 * Get a configuration value that should be a number.
874 * @param cfg configuration to inspect
875 * @param section section of interest
876 * @param option option of interest
877 * @param number where to store the numeric value of the option
878 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
881 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
882 *cfg, const char *section,
884 unsigned long long *number)
886 struct ConfigEntry *e;
888 if (NULL == (e = find_entry (cfg, section, option)))
889 return GNUNET_SYSERR;
891 return GNUNET_SYSERR;
892 if (1 != SSCANF (e->val, "%llu", number))
893 return GNUNET_SYSERR;
898 * Get a configuration value that should be a floating point number.
900 * @param cfg configuration to inspect
901 * @param section section of interest
902 * @param option option of interest
903 * @param number where to store the floating value of the option
904 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
907 GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle
908 *cfg, const char *section,
912 struct ConfigEntry *e;
914 if (NULL == (e = find_entry (cfg, section, option)))
915 return GNUNET_SYSERR;
917 return GNUNET_SYSERR;
918 if (1 != SSCANF (e->val, "%f", number))
919 return GNUNET_SYSERR;
926 * Get a configuration value that should be a relative time.
928 * @param cfg configuration to inspect
929 * @param section section of interest
930 * @param option option of interest
931 * @param time set to the time value stored in the configuration
932 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
935 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
938 struct GNUNET_TIME_Relative *time)
940 struct ConfigEntry *e;
942 if (NULL == (e = find_entry (cfg, section, option)))
943 return GNUNET_SYSERR;
945 return GNUNET_SYSERR;
946 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
951 * Get a configuration value that should be a size in bytes.
953 * @param cfg configuration to inspect
954 * @param section section of interest
955 * @param option option of interest
956 * @param size set to the size in bytes as stored in the configuration
957 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
960 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
963 unsigned long long *size)
965 struct ConfigEntry *e;
967 if (NULL == (e = find_entry (cfg, section, option)))
968 return GNUNET_SYSERR;
970 return GNUNET_SYSERR;
971 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
976 * Get a configuration value that should be a string.
978 * @param cfg configuration to inspect
979 * @param section section of interest
980 * @param option option of interest
981 * @param value will be set to a freshly allocated configuration
982 * value, or NULL if option is not specified
983 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
986 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
991 struct ConfigEntry *e;
993 if ( (NULL == (e = find_entry (cfg, section, option))) ||
997 return GNUNET_SYSERR;
999 *value = GNUNET_strdup (e->val);
1005 * Get a configuration value that should be in a set of
1006 * predefined strings
1008 * @param cfg configuration to inspect
1009 * @param section section of interest
1010 * @param option option of interest
1011 * @param choices NULL-terminated list of legal values
1012 * @param value will be set to an entry in the legal list,
1013 * or NULL if option is not specified and no default given
1014 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1017 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1018 const char *section,
1020 const char *const *choices,
1023 struct ConfigEntry *e;
1026 if (NULL == (e = find_entry (cfg, section, option)))
1027 return GNUNET_SYSERR;
1028 for (i = 0; NULL != choices[i]; i++)
1029 if (0 == strcasecmp (choices[i], e->val))
1031 if (NULL == choices[i])
1033 LOG (GNUNET_ERROR_TYPE_ERROR,
1034 _("Configuration value '%s' for '%s'"
1035 " in section '%s' is not in set of legal choices\n"),
1039 return GNUNET_SYSERR;
1041 *value = choices[i];
1047 * Get crockford32-encoded fixed-size binary data from a configuration.
1049 * @param cfg configuration to access
1050 * @param section section to access
1051 * @param option option to access
1052 * @param buf where to store the decoded binary result
1053 * @param buf_size exact number of bytes to store in @a buf
1054 * @return #GNUNET_OK on success
1055 * #GNUNET_NO is the value does not exist
1056 * #GNUNET_SYSERR on decoding error
1059 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1060 const char *section,
1070 (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1075 data_size = (strlen (enc) * 5) / 8;
1076 if (data_size != buf_size)
1079 return GNUNET_SYSERR;
1082 GNUNET_STRINGS_string_to_data (enc,
1087 return GNUNET_SYSERR;
1095 * Test if we have a value for a particular option
1097 * @param cfg configuration to inspect
1098 * @param section section of interest
1099 * @param option option of interest
1100 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1103 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1104 const char *section,
1107 struct ConfigEntry *e;
1109 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1116 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1117 * where either in the "PATHS" section or the environtment "FOO" is
1118 * set to "DIRECTORY". We also support default expansion,
1119 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1120 * set in PATHS or the environment, and otherwise to "default". Note
1121 * that "default" itself can also be a $-expression, thus
1122 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1125 * @param cfg configuration to use for path expansion
1126 * @param orig string to $-expand (will be freed!)
1127 * @param depth recursion depth, used to detect recursive expansions
1128 * @return $-expanded string
1131 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1152 LOG (GNUNET_ERROR_TYPE_WARNING,
1153 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1157 LOG (GNUNET_ERROR_TYPE_DEBUG,
1158 "Asked to $-expand %s\n",
1162 LOG (GNUNET_ERROR_TYPE_DEBUG,
1163 "Doesn't start with $ - not expanding\n");
1185 LOG (GNUNET_ERROR_TYPE_WARNING,
1186 _("Missing closing `%s' in option `%s'\n"),
1198 def = strchr (orig, ':');
1203 if ( ('-' == *def) ||
1206 def = GNUNET_strdup (def);
1214 while ( (orig[i] != '/') &&
1215 (orig[i] != '\\') &&
1216 (orig[i] != '\0') &&
1219 if (orig[i] == '\0')
1225 erased_char = orig[i];
1226 erased_pos = &orig[i];
1228 post = &orig[i + 1];
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "Split into `%s' and `%s' with default %s\n",
1237 GNUNET_CONFIGURATION_get_value_string (cfg,
1242 if (NULL == (env = getenv (start)))
1245 def = expand_dollar (cfg, def, depth + 1);
1250 start = GNUNET_strdup (start);
1252 *erased_pos = erased_char;
1253 LOG (GNUNET_ERROR_TYPE_WARNING,
1254 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1256 GNUNET_free (start);
1259 prefix = GNUNET_strdup (env);
1261 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1262 if ( (erased_pos) && ('}' != erased_char) )
1264 len = strlen (prefix) + 1;
1265 prefix = GNUNET_realloc (prefix, len + 1);
1266 prefix[len - 1] = erased_char;
1269 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1270 strcpy (result, prefix);
1271 strcat (result, post);
1272 GNUNET_free_non_null (def);
1273 GNUNET_free (prefix);
1280 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1281 * where either in the "PATHS" section or the environtment "FOO" is
1282 * set to "DIRECTORY". We also support default expansion,
1283 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1284 * set in PATHS or the environment, and otherwise to "default". Note
1285 * that "default" itself can also be a $-expression, thus
1286 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1289 * @param cfg configuration to use for path expansion
1290 * @param orig string to $-expand (will be freed!). Note that multiple
1291 * $-expressions can be present in this string. They will all be
1293 * @return $-expanded string
1296 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1303 for (i = 0; '\0' != orig[i]; i++)
1307 dup = GNUNET_strdup (orig + i);
1308 dup = expand_dollar (cfg, dup, 0);
1309 len = strlen (dup) + 1;
1310 orig = GNUNET_realloc (orig, i + len);
1311 GNUNET_memcpy (orig + i, dup, len);
1319 * Get a configuration value that should be a string.
1321 * @param cfg configuration to inspect
1322 * @param section section of interest
1323 * @param option option of interest
1324 * @param value will be set to a freshly allocated configuration
1325 * value, or NULL if option is not specified
1326 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1329 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1330 const char *section,
1337 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1339 LOG (GNUNET_ERROR_TYPE_DEBUG,
1340 "Failed to retrieve filename\n");
1342 return GNUNET_SYSERR;
1344 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1345 *value = GNUNET_STRINGS_filename_expand (tmp);
1348 return GNUNET_SYSERR;
1354 * Get a configuration value that should be in a set of
1357 * @param cfg configuration to inspect
1358 * @param section section of interest
1359 * @param option option of interest
1360 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1363 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1364 const char *section,
1367 static const char *yesno[] = { "YES", "NO", NULL };
1372 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1373 if (ret == GNUNET_SYSERR)
1375 if (val == yesno[0])
1382 * Iterate over the set of filenames stored in a configuration value.
1384 * @param cfg configuration to inspect
1385 * @param section section of interest
1386 * @param option option of interest
1387 * @param cb function to call on each filename
1388 * @param cb_cls closure for @a cb
1389 * @return number of filenames iterated over, -1 on error
1392 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1393 const char *section,
1395 GNUNET_FileNameCallback cb,
1405 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1407 GNUNET_assert (list != NULL);
1412 while (pos[0] == ' ')
1414 if (strlen (pos) == 0)
1417 while ((end[0] != ' ') && (end[0] != '\0'))
1425 memmove (end, &end[1], strlen (&end[1]) + 1);
1427 /* illegal, but just keep it */
1430 /* illegal, but just ignore that there was a '/' */
1438 if (strlen (pos) > 0)
1441 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1443 ret = GNUNET_SYSERR;
1459 * @param value FIXME
1463 escape_name (const char *value)
1469 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1470 memset (escaped, 0, strlen (value) * 2 + 1);
1473 while (rpos[0] != '\0')
1496 * @param cls string we compare with (const char*)
1497 * @param fn filename we are currently looking at
1498 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1501 test_match (void *cls, const char *fn)
1503 const char *of = cls;
1505 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1510 * Append a filename to a configuration value that
1511 * represents a list of filenames
1513 * @param cfg configuration to update
1514 * @param section section of interest
1515 * @param option option of interest
1516 * @param value filename to append
1517 * @return #GNUNET_OK on success,
1518 * #GNUNET_NO if the filename already in the list
1519 * #GNUNET_SYSERR on error
1522 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1523 const char *section,
1531 if (GNUNET_SYSERR ==
1532 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1535 return GNUNET_NO; /* already exists */
1537 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1538 old = GNUNET_strdup ("");
1539 escaped = escape_name (value);
1540 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1542 if (strlen (old) > 0)
1544 strcat (nw, escaped);
1545 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1548 GNUNET_free (escaped);
1554 * Remove a filename from a configuration value that
1555 * represents a list of filenames
1557 * @param cfg configuration to update
1558 * @param section section of interest
1559 * @param option option of interest
1560 * @param value filename to remove
1561 * @return #GNUNET_OK on success,
1562 * #GNUNET_NO if the filename is not in the list,
1563 * #GNUNET_SYSERR on error
1566 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1567 *cfg, const char *section,
1578 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1580 match = escape_name (value);
1584 while (pos[0] == ' ')
1586 if (strlen (pos) == 0)
1589 while ((end[0] != ' ') && (end[0] != '\0'))
1600 /* illegal, but just keep it */
1603 /* illegal, but just ignore that there was a '/' */
1611 if (0 == strcmp (pos, match))
1614 memmove (pos, &end[1], strlen (&end[1]) + 1);
1622 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1624 GNUNET_free (match);
1633 GNUNET_free (match);
1639 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1640 * file in a directory, we trigger parsing on those files that
1643 * @param cls the cfg
1644 * @param filename file to parse
1645 * @return #GNUNET_OK on success
1648 parse_configuration_file (void *cls, const char *filename)
1650 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1654 /* Examine file extension */
1655 ext = strrchr (filename, '.');
1656 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1659 "Skipping file `%s'\n",
1664 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1670 * Load default configuration. This function will parse the
1671 * defaults from the given defaults_d directory.
1673 * @param cfg configuration to update
1674 * @param defaults_d directory with the defaults
1675 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1678 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1679 const char *defaults_d)
1681 if (GNUNET_SYSERR ==
1682 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1683 return GNUNET_SYSERR; /* no configuration at all found */
1688 /* end of configuration.c */