2 This file is part of GNUnet.
3 Copyright (C) 2006, 2007, 2008, 2009, 2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file src/util/configuration.c
21 * @brief configuration management
22 * @author Christian Grothoff
26 #include "gnunet_crypto_lib.h"
27 #include "gnunet_strings_lib.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_disk_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
36 * @brief configuration entry
42 * This is a linked list.
44 struct ConfigEntry *next;
52 * current, commited value
59 * @brief configuration section
64 * This is a linked list.
66 struct ConfigSection *next;
69 * entries in the section
71 struct ConfigEntry *entries;
81 * @brief configuration data
83 struct GNUNET_CONFIGURATION_Handle
86 * Configuration sections.
88 struct ConfigSection *sections;
91 * Modification indication since last save
92 * #GNUNET_NO if clean, #GNUNET_YES if dirty,
93 * #GNUNET_SYSERR on error (i.e. last save failed)
101 * Used for diffing a configuration object against
106 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
108 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
113 * Create a GNUNET_CONFIGURATION_Handle.
115 * @return fresh configuration object
117 struct GNUNET_CONFIGURATION_Handle *
118 GNUNET_CONFIGURATION_create ()
120 return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
125 * Destroy configuration object.
127 * @param cfg configuration to destroy
130 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
132 struct ConfigSection *sec;
134 while (NULL != (sec = cfg->sections))
135 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
141 * De-serializes configuration
143 * @param cfg configuration to update
144 * @param mem the memory block of serialized configuration
145 * @param size the size of the memory block
146 * @param basedir set to path from which we recursively load configuration
147 * from inlined configurations; NULL if not and raise warnings
148 * when we come across them
149 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
152 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
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@ ")];
233 GNUNET_asprintf (&fn,
238 GNUNET_CONFIGURATION_parse (cfg,
242 ret = GNUNET_SYSERR; /* failed to parse included config */
249 LOG (GNUNET_ERROR_TYPE_DEBUG,
250 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
256 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
259 line[line_size - 1] = '\0';
261 GNUNET_free (section);
262 section = GNUNET_strdup (value);
265 if (NULL != (eq = strchr (line, '=')))
268 tag = GNUNET_strndup (line, eq - line);
269 /* remove tailing whitespace */
270 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
273 /* Strip whitespace */
275 while (isspace ((unsigned char) value[0]))
277 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
282 if ( ('"' == value[0]) &&
283 ('"' == value[strlen (value) - 1]) )
285 value[strlen (value) - 1] = '\0';
288 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
293 LOG (GNUNET_ERROR_TYPE_WARNING,
294 _("Syntax error while deserializing in line %u\n"),
299 GNUNET_free_non_null (line_orig);
300 GNUNET_free (section);
301 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
307 * Parse a configuration file, add all of the options in the
308 * file to the configuration environment.
310 * @param cfg configuration to update
311 * @param filename name of the configuration file
312 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
315 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
316 const char *filename)
327 fn = GNUNET_STRINGS_filename_expand (filename);
328 LOG (GNUNET_ERROR_TYPE_DEBUG,
329 "Asked to parse config file `%s'\n",
332 return GNUNET_SYSERR;
333 dirty = cfg->dirty; /* back up value! */
335 GNUNET_DISK_file_size (fn,
340 LOG (GNUNET_ERROR_TYPE_WARNING,
341 "Error while determining the file size of `%s'\n",
344 return GNUNET_SYSERR;
348 GNUNET_break (0); /* File size is more than the heap size */
350 return GNUNET_SYSERR;
353 mem = GNUNET_malloc (fs);
354 sret = GNUNET_DISK_fn_read (fn,
358 (fs != (size_t) sret) )
360 LOG (GNUNET_ERROR_TYPE_WARNING,
361 _("Error while reading file `%s'\n"),
365 return GNUNET_SYSERR;
367 LOG (GNUNET_ERROR_TYPE_DEBUG,
368 "Deserializing contents of file `%s'\n",
370 endsep = strrchr (fn, (int) '/');
373 ret = GNUNET_CONFIGURATION_deserialize (cfg,
379 /* restore dirty flag - anything we set in the meantime
387 * Test if there are configuration options that were
388 * changed since the last save.
390 * @param cfg configuration to inspect
391 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
394 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
401 * Serializes the given configuration.
403 * @param cfg configuration to serialize
404 * @param size will be set to the size of the serialized memory block
405 * @return the memory block where the serialized configuration is
406 * present. This memory should be freed by the caller
409 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
412 struct ConfigSection *sec;
413 struct ConfigEntry *ent;
422 /* Pass1 : calculate the buffer size required */
424 for (sec = cfg->sections; NULL != sec; sec = sec->next)
426 /* For each section we need to add 3 charaters: {'[',']','\n'} */
427 m_size += strlen (sec->name) + 3;
428 for (ent = sec->entries; NULL != ent; ent = ent->next)
430 if (NULL != ent->val)
432 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
434 while (NULL != (pos = strstr (pos, "\n")))
439 /* For each key = value pair we need to add 4 characters (2
440 spaces and 1 equal-to character and 1 new line) */
441 m_size += strlen (ent->key) + strlen (ent->val) + 4;
444 /* A new line after section end */
448 /* Pass2: Allocate memory and write the configuration to it */
449 mem = GNUNET_malloc (m_size);
455 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
456 GNUNET_assert (0 < len);
457 GNUNET_memcpy (mem + c_size, cbuf, len);
460 for (ent = sec->entries; NULL != ent; ent = ent->next)
462 if (NULL != ent->val)
464 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
465 strcpy (val, ent->val);
466 while (NULL != (pos = strstr (val, "\n")))
468 memmove (&pos[2], &pos[1], strlen (&pos[1]));
472 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
474 GNUNET_memcpy (mem + c_size, cbuf, len);
479 GNUNET_memcpy (mem + c_size, "\n", 1);
483 GNUNET_assert (c_size == m_size);
490 * Write configuration file.
492 * @param cfg configuration to write
493 * @param filename where to write the configuration
494 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
497 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
498 const char *filename)
505 fn = GNUNET_STRINGS_filename_expand (filename);
507 return GNUNET_SYSERR;
508 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
511 return GNUNET_SYSERR;
513 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
514 sret = GNUNET_DISK_fn_write (fn, cfg_buf, size,
515 GNUNET_DISK_PERM_USER_READ
516 | GNUNET_DISK_PERM_USER_WRITE
517 | GNUNET_DISK_PERM_GROUP_READ
518 | GNUNET_DISK_PERM_GROUP_WRITE);
520 (size != (size_t) sret) )
523 GNUNET_free (cfg_buf);
524 LOG (GNUNET_ERROR_TYPE_WARNING,
525 "Writing configration to file `%s' failed\n",
527 cfg->dirty = GNUNET_SYSERR; /* last write failed */
528 return GNUNET_SYSERR;
531 GNUNET_free (cfg_buf);
532 cfg->dirty = GNUNET_NO; /* last write succeeded */
538 * Iterate over all options in the configuration.
540 * @param cfg configuration to inspect
541 * @param iter function to call on each option
542 * @param iter_cls closure for @a iter
545 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
546 GNUNET_CONFIGURATION_Iterator iter,
549 struct ConfigSection *spos;
550 struct ConfigEntry *epos;
552 for (spos = cfg->sections; NULL != spos; spos = spos->next)
553 for (epos = spos->entries; NULL != epos; epos = epos->next)
554 if (NULL != epos->val)
555 iter (iter_cls, spos->name, epos->key, epos->val);
560 * Iterate over values of a section in the configuration.
562 * @param cfg configuration to inspect
563 * @param section the section
564 * @param iter function to call on each option
565 * @param iter_cls closure for @a iter
568 GNUNET_CONFIGURATION_iterate_section_values (const struct
569 GNUNET_CONFIGURATION_Handle *cfg,
571 GNUNET_CONFIGURATION_Iterator iter,
574 struct ConfigSection *spos;
575 struct ConfigEntry *epos;
577 spos = cfg->sections;
578 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
582 for (epos = spos->entries; NULL != epos; epos = epos->next)
583 if (NULL != epos->val)
584 iter (iter_cls, spos->name, epos->key, epos->val);
589 * Iterate over all sections in the configuration.
591 * @param cfg configuration to inspect
592 * @param iter function to call on each section
593 * @param iter_cls closure for @a iter
596 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
597 GNUNET_CONFIGURATION_Section_Iterator iter,
600 struct ConfigSection *spos;
601 struct ConfigSection *next;
603 next = cfg->sections;
608 iter (iter_cls, spos->name);
614 * Remove the given section and all options in it.
616 * @param cfg configuration to inspect
617 * @param section name of the section to remove
620 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
623 struct ConfigSection *spos;
624 struct ConfigSection *prev;
625 struct ConfigEntry *ent;
628 spos = cfg->sections;
631 if (0 == strcasecmp (section, spos->name))
634 cfg->sections = spos->next;
636 prev->next = spos->next;
637 while (NULL != (ent = spos->entries))
639 spos->entries = ent->next;
640 GNUNET_free (ent->key);
641 GNUNET_free_non_null (ent->val);
643 cfg->dirty = GNUNET_YES;
645 GNUNET_free (spos->name);
656 * Copy a configuration value to the given target configuration.
657 * Overwrites existing entries.
659 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
660 * @param section section for the value
661 * @param option option name of the value
662 * @param value value to copy
665 copy_entry (void *cls,
670 struct GNUNET_CONFIGURATION_Handle *dst = cls;
672 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
677 * Duplicate an existing configuration object.
679 * @param cfg configuration to duplicate
680 * @return duplicate configuration
682 struct GNUNET_CONFIGURATION_Handle *
683 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
685 struct GNUNET_CONFIGURATION_Handle *ret;
687 ret = GNUNET_CONFIGURATION_create ();
688 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
694 * Find a section entry from a configuration.
696 * @param cfg configuration to search in
697 * @param section name of the section to look for
698 * @return matching entry, NULL if not found
700 static struct ConfigSection *
701 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
704 struct ConfigSection *pos;
707 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
714 * Find an entry from a configuration.
716 * @param cfg handle to the configuration
717 * @param section section the option is in
718 * @param key the option
719 * @return matching entry, NULL if not found
721 static struct ConfigEntry *
722 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
726 struct ConfigSection *sec;
727 struct ConfigEntry *pos;
729 if (NULL == (sec = find_section (cfg, section)))
732 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
739 * A callback function, compares entries from two configurations
740 * (default against a new configuration) and write the diffs in a
741 * diff-configuration object (the callback object).
743 * @param cls the diff configuration (`struct DiffHandle *`)
744 * @param section section for the value (of the default conf.)
745 * @param option option name of the value (of the default conf.)
746 * @param value value to copy (of the default conf.)
749 compare_entries (void *cls,
754 struct DiffHandle *dh = cls;
755 struct ConfigEntry *entNew;
757 entNew = find_entry (dh->cfg_default, section, option);
758 if ( (NULL != entNew) &&
759 (NULL != entNew->val) &&
760 (0 == strcmp (entNew->val, value)) )
762 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
767 * Compute configuration with only entries that have been changed
769 * @param cfg_default original configuration
770 * @param cfg_new new configuration
771 * @return configuration with only the differences, never NULL
773 struct GNUNET_CONFIGURATION_Handle *
774 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
775 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
777 struct DiffHandle diffHandle;
779 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
780 diffHandle.cfg_default = cfg_default;
781 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
782 return diffHandle.cfgDiff;
787 * Write only configuration entries that have been changed to configuration file
789 * @param cfg_default default configuration
790 * @param cfg_new new configuration
791 * @param filename where to write the configuration diff between default and new
792 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
795 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
797 const struct GNUNET_CONFIGURATION_Handle
798 *cfg_new, const char *filename)
801 struct GNUNET_CONFIGURATION_Handle *diff;
803 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
804 ret = GNUNET_CONFIGURATION_write (diff, filename);
805 GNUNET_CONFIGURATION_destroy (diff);
811 * Set a configuration value that should be a string.
813 * @param cfg configuration to update
814 * @param section section of interest
815 * @param option option of interest
816 * @param value value to set
819 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
820 const char *section, const char *option,
823 struct ConfigSection *sec;
824 struct ConfigEntry *e;
827 e = find_entry (cfg, section, option);
832 GNUNET_free_non_null (e->val);
837 nv = GNUNET_strdup (value);
838 GNUNET_free_non_null (e->val);
843 sec = find_section (cfg, section);
846 sec = GNUNET_new (struct ConfigSection);
847 sec->name = GNUNET_strdup (section);
848 sec->next = cfg->sections;
851 e = GNUNET_new (struct ConfigEntry);
852 e->key = GNUNET_strdup (option);
853 e->val = GNUNET_strdup (value);
854 e->next = sec->entries;
860 * Set a configuration value that should be a number.
862 * @param cfg configuration to update
863 * @param section section of interest
864 * @param option option of interest
865 * @param number value to set
868 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
871 unsigned long long number)
879 GNUNET_CONFIGURATION_set_value_string (cfg,
887 * Get a configuration value that should be a number.
889 * @param cfg configuration to inspect
890 * @param section section of interest
891 * @param option option of interest
892 * @param number where to store the numeric value of the option
893 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
896 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
897 *cfg, const char *section,
899 unsigned long long *number)
901 struct ConfigEntry *e;
903 if (NULL == (e = find_entry (cfg, section, option)))
904 return GNUNET_SYSERR;
906 return GNUNET_SYSERR;
907 if (1 != SSCANF (e->val, "%llu", number))
908 return GNUNET_SYSERR;
913 * Get a configuration value that should be a floating point number.
915 * @param cfg configuration to inspect
916 * @param section section of interest
917 * @param option option of interest
918 * @param number where to store the floating value of the option
919 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
922 GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle
923 *cfg, const char *section,
927 struct ConfigEntry *e;
929 if (NULL == (e = find_entry (cfg, section, option)))
930 return GNUNET_SYSERR;
932 return GNUNET_SYSERR;
933 if (1 != SSCANF (e->val, "%f", number))
934 return GNUNET_SYSERR;
941 * Get a configuration value that should be a relative time.
943 * @param cfg configuration to inspect
944 * @param section section of interest
945 * @param option option of interest
946 * @param time set to the time value stored in the configuration
947 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
950 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
953 struct GNUNET_TIME_Relative *time)
955 struct ConfigEntry *e;
957 if (NULL == (e = find_entry (cfg, section, option)))
958 return GNUNET_SYSERR;
960 return GNUNET_SYSERR;
961 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
966 * Get a configuration value that should be a size in bytes.
968 * @param cfg configuration to inspect
969 * @param section section of interest
970 * @param option option of interest
971 * @param size set to the size in bytes as stored in the configuration
972 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
975 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
978 unsigned long long *size)
980 struct ConfigEntry *e;
982 if (NULL == (e = find_entry (cfg, section, option)))
983 return GNUNET_SYSERR;
985 return GNUNET_SYSERR;
986 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
991 * Get a configuration value that should be a string.
993 * @param cfg configuration to inspect
994 * @param section section of interest
995 * @param option option of interest
996 * @param value will be set to a freshly allocated configuration
997 * value, or NULL if option is not specified
998 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1001 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
1002 const char *section,
1006 struct ConfigEntry *e;
1008 if ( (NULL == (e = find_entry (cfg, section, option))) ||
1012 return GNUNET_SYSERR;
1014 *value = GNUNET_strdup (e->val);
1020 * Get a configuration value that should be in a set of
1021 * predefined strings
1023 * @param cfg configuration to inspect
1024 * @param section section of interest
1025 * @param option option of interest
1026 * @param choices NULL-terminated list of legal values
1027 * @param value will be set to an entry in the legal list,
1028 * or NULL if option is not specified and no default given
1029 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1032 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1033 const char *section,
1035 const char *const *choices,
1038 struct ConfigEntry *e;
1041 if (NULL == (e = find_entry (cfg, section, option)))
1042 return GNUNET_SYSERR;
1043 for (i = 0; NULL != choices[i]; i++)
1044 if (0 == strcasecmp (choices[i], e->val))
1046 if (NULL == choices[i])
1048 LOG (GNUNET_ERROR_TYPE_ERROR,
1049 _("Configuration value '%s' for '%s'"
1050 " in section '%s' is not in set of legal choices\n"),
1054 return GNUNET_SYSERR;
1056 *value = choices[i];
1062 * Get crockford32-encoded fixed-size binary data from a configuration.
1064 * @param cfg configuration to access
1065 * @param section section to access
1066 * @param option option to access
1067 * @param buf where to store the decoded binary result
1068 * @param buf_size exact number of bytes to store in @a buf
1069 * @return #GNUNET_OK on success
1070 * #GNUNET_NO is the value does not exist
1071 * #GNUNET_SYSERR on decoding error
1074 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1075 const char *section,
1085 (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1090 data_size = (strlen (enc) * 5) / 8;
1091 if (data_size != buf_size)
1094 return GNUNET_SYSERR;
1097 GNUNET_STRINGS_string_to_data (enc,
1102 return GNUNET_SYSERR;
1110 * Test if we have a value for a particular option
1112 * @param cfg configuration to inspect
1113 * @param section section of interest
1114 * @param option option of interest
1115 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1118 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1119 const char *section,
1122 struct ConfigEntry *e;
1124 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1131 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1132 * where either in the "PATHS" section or the environtment "FOO" is
1133 * set to "DIRECTORY". We also support default expansion,
1134 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1135 * set in PATHS or the environment, and otherwise to "default". Note
1136 * that "default" itself can also be a $-expression, thus
1137 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1140 * @param cfg configuration to use for path expansion
1141 * @param orig string to $-expand (will be freed!)
1142 * @param depth recursion depth, used to detect recursive expansions
1143 * @return $-expanded string
1146 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1167 LOG (GNUNET_ERROR_TYPE_WARNING,
1168 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1172 LOG (GNUNET_ERROR_TYPE_DEBUG,
1173 "Asked to $-expand %s\n",
1177 LOG (GNUNET_ERROR_TYPE_DEBUG,
1178 "Doesn't start with $ - not expanding\n");
1200 LOG (GNUNET_ERROR_TYPE_WARNING,
1201 _("Missing closing `%s' in option `%s'\n"),
1213 def = strchr (orig, ':');
1218 if ( ('-' == *def) ||
1221 def = GNUNET_strdup (def);
1229 while ( (orig[i] != '/') &&
1230 (orig[i] != '\\') &&
1231 (orig[i] != '\0') &&
1234 if (orig[i] == '\0')
1240 erased_char = orig[i];
1241 erased_pos = &orig[i];
1243 post = &orig[i + 1];
1246 LOG (GNUNET_ERROR_TYPE_DEBUG,
1247 "Split into `%s' and `%s' with default %s\n",
1252 GNUNET_CONFIGURATION_get_value_string (cfg,
1257 if (NULL == (env = getenv (start)))
1260 def = expand_dollar (cfg, def, depth + 1);
1265 start = GNUNET_strdup (start);
1267 *erased_pos = erased_char;
1268 LOG (GNUNET_ERROR_TYPE_WARNING,
1269 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1271 GNUNET_free (start);
1274 prefix = GNUNET_strdup (env);
1276 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1277 if ( (erased_pos) && ('}' != erased_char) )
1279 len = strlen (prefix) + 1;
1280 prefix = GNUNET_realloc (prefix, len + 1);
1281 prefix[len - 1] = erased_char;
1284 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1285 strcpy (result, prefix);
1286 strcat (result, post);
1287 GNUNET_free_non_null (def);
1288 GNUNET_free (prefix);
1295 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1296 * where either in the "PATHS" section or the environtment "FOO" is
1297 * set to "DIRECTORY". We also support default expansion,
1298 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1299 * set in PATHS or the environment, and otherwise to "default". Note
1300 * that "default" itself can also be a $-expression, thus
1301 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1304 * @param cfg configuration to use for path expansion
1305 * @param orig string to $-expand (will be freed!). Note that multiple
1306 * $-expressions can be present in this string. They will all be
1308 * @return $-expanded string
1311 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1318 for (i = 0; '\0' != orig[i]; i++)
1322 dup = GNUNET_strdup (orig + i);
1323 dup = expand_dollar (cfg, dup, 0);
1324 len = strlen (dup) + 1;
1325 orig = GNUNET_realloc (orig, i + len);
1326 GNUNET_memcpy (orig + i, dup, len);
1334 * Get a configuration value that should be a string.
1336 * @param cfg configuration to inspect
1337 * @param section section of interest
1338 * @param option option of interest
1339 * @param value will be set to a freshly allocated configuration
1340 * value, or NULL if option is not specified
1341 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1344 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1345 const char *section,
1352 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1354 LOG (GNUNET_ERROR_TYPE_DEBUG,
1355 "Failed to retrieve filename\n");
1357 return GNUNET_SYSERR;
1359 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1360 *value = GNUNET_STRINGS_filename_expand (tmp);
1363 return GNUNET_SYSERR;
1369 * Get a configuration value that should be in a set of
1372 * @param cfg configuration to inspect
1373 * @param section section of interest
1374 * @param option option of interest
1375 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1378 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1379 const char *section,
1382 static const char *yesno[] = { "YES", "NO", NULL };
1387 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1388 if (ret == GNUNET_SYSERR)
1390 if (val == yesno[0])
1397 * Iterate over the set of filenames stored in a configuration value.
1399 * @param cfg configuration to inspect
1400 * @param section section of interest
1401 * @param option option of interest
1402 * @param cb function to call on each filename
1403 * @param cb_cls closure for @a cb
1404 * @return number of filenames iterated over, -1 on error
1407 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1408 const char *section,
1410 GNUNET_FileNameCallback cb,
1420 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1422 GNUNET_assert (list != NULL);
1427 while (pos[0] == ' ')
1429 if (strlen (pos) == 0)
1432 while ((end[0] != ' ') && (end[0] != '\0'))
1440 memmove (end, &end[1], strlen (&end[1]) + 1);
1442 /* illegal, but just keep it */
1445 /* illegal, but just ignore that there was a '/' */
1453 if (strlen (pos) > 0)
1456 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1458 ret = GNUNET_SYSERR;
1474 * @param value FIXME
1478 escape_name (const char *value)
1484 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1485 memset (escaped, 0, strlen (value) * 2 + 1);
1488 while (rpos[0] != '\0')
1511 * @param cls string we compare with (const char*)
1512 * @param fn filename we are currently looking at
1513 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1516 test_match (void *cls, const char *fn)
1518 const char *of = cls;
1520 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1525 * Append a filename to a configuration value that
1526 * represents a list of filenames
1528 * @param cfg configuration to update
1529 * @param section section of interest
1530 * @param option option of interest
1531 * @param value filename to append
1532 * @return #GNUNET_OK on success,
1533 * #GNUNET_NO if the filename already in the list
1534 * #GNUNET_SYSERR on error
1537 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1538 const char *section,
1546 if (GNUNET_SYSERR ==
1547 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1550 return GNUNET_NO; /* already exists */
1552 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1553 old = GNUNET_strdup ("");
1554 escaped = escape_name (value);
1555 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1557 if (strlen (old) > 0)
1559 strcat (nw, escaped);
1560 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1563 GNUNET_free (escaped);
1569 * Remove a filename from a configuration value that
1570 * represents a list of filenames
1572 * @param cfg configuration to update
1573 * @param section section of interest
1574 * @param option option of interest
1575 * @param value filename to remove
1576 * @return #GNUNET_OK on success,
1577 * #GNUNET_NO if the filename is not in the list,
1578 * #GNUNET_SYSERR on error
1581 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1582 *cfg, const char *section,
1593 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1595 match = escape_name (value);
1599 while (pos[0] == ' ')
1601 if (strlen (pos) == 0)
1604 while ((end[0] != ' ') && (end[0] != '\0'))
1615 /* illegal, but just keep it */
1618 /* illegal, but just ignore that there was a '/' */
1626 if (0 == strcmp (pos, match))
1629 memmove (pos, &end[1], strlen (&end[1]) + 1);
1637 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1639 GNUNET_free (match);
1648 GNUNET_free (match);
1654 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1655 * file in a directory, we trigger parsing on those files that
1658 * @param cls the cfg
1659 * @param filename file to parse
1660 * @return #GNUNET_OK on success
1663 parse_configuration_file (void *cls, const char *filename)
1665 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1669 /* Examine file extension */
1670 ext = strrchr (filename, '.');
1671 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1673 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1674 "Skipping file `%s'\n",
1679 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1685 * Load default configuration. This function will parse the
1686 * defaults from the given defaults_d directory.
1688 * @param cfg configuration to update
1689 * @param defaults_d directory with the defaults
1690 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1693 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1694 const char *defaults_d)
1696 if (GNUNET_SYSERR ==
1697 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1698 return GNUNET_SYSERR; /* no configuration at all found */
1703 /* end of configuration.c */