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 allow_inline set to #GNUNET_YES if we recursively load configuration
149 * from inlined configurations; #GNUNET_NO 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,
174 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing config file\n");
176 section = GNUNET_strdup ("");
180 while (r_bytes < size)
182 GNUNET_free_non_null (line_orig);
183 /* fgets-like behaviour on buffer */
184 to_read = size - r_bytes;
185 pos = memchr (&mem[r_bytes], '\n', to_read);
188 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
189 r_bytes += line_size;
193 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
194 r_bytes += line_size + 1;
197 /* increment line number */
199 /* tabs and '\r' are whitespace */
200 emptyline = GNUNET_YES;
201 for (i = 0; i < line_size; i++)
208 emptyline = GNUNET_NO;
210 /* ignore empty lines */
211 if (GNUNET_YES == emptyline)
214 /* remove tailing whitespace */
215 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
218 /* remove leading whitespace */
219 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
221 /* ignore comments */
222 if ( ('#' == line[0]) || ('%' == line[0]) )
225 /* handle special "@INLINE@" directive */
226 if (0 == strncasecmp (line,
228 strlen ("@INLINE@ ")))
231 value = &line[strlen ("@INLINE@ ")];
232 if (GNUNET_YES == allow_inline)
234 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
236 ret = GNUNET_SYSERR; /* failed to parse included config */
242 LOG (GNUNET_ERROR_TYPE_DEBUG,
243 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
249 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
252 line[line_size - 1] = '\0';
254 GNUNET_free (section);
255 section = GNUNET_strdup (value);
256 LOG (GNUNET_ERROR_TYPE_DEBUG,
257 "Config section `%s'\n",
261 if (NULL != (eq = strchr (line, '=')))
264 tag = GNUNET_strndup (line, eq - line);
265 /* remove tailing whitespace */
266 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
269 /* Strip whitespace */
271 while (isspace ((unsigned char) value[0]))
273 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
278 if ( ('"' == value[0]) &&
279 ('"' == value[strlen (value) - 1]) )
281 value[strlen (value) - 1] = '\0';
284 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s=\"%s\"\n", tag, value);
285 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
290 LOG (GNUNET_ERROR_TYPE_WARNING,
291 _("Syntax error while deserializing in line %u\n"),
296 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n");
297 GNUNET_free_non_null (line_orig);
298 GNUNET_free (section);
299 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
305 * Parse a configuration file, add all of the options in the
306 * file to the configuration environment.
308 * @param cfg configuration to update
309 * @param filename name of the configuration file
310 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
313 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
314 const char *filename)
323 LOG (GNUNET_ERROR_TYPE_DEBUG,
324 "Asked to parse config file `%s'\n",
326 fn = GNUNET_STRINGS_filename_expand (filename);
327 LOG (GNUNET_ERROR_TYPE_DEBUG,
328 "Config file name expanded to `%s'\n",
331 return GNUNET_SYSERR;
332 dirty = cfg->dirty; /* back up value! */
334 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
336 LOG (GNUNET_ERROR_TYPE_WARNING,
337 "Error while determining the file size of %s\n", fn);
339 return GNUNET_SYSERR;
343 GNUNET_break (0); /* File size is more than the heap size */
345 return GNUNET_SYSERR;
348 mem = GNUNET_malloc (fs);
349 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
351 LOG (GNUNET_ERROR_TYPE_WARNING,
352 "Error while reading file %s\n",
356 return GNUNET_SYSERR;
358 LOG (GNUNET_ERROR_TYPE_DEBUG,
359 "Deserializing contents of file `%s'\n",
362 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
364 /* restore dirty flag - anything we set in the meantime
372 * Test if there are configuration options that were
373 * changed since the last save.
375 * @param cfg configuration to inspect
376 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
379 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
386 * Serializes the given configuration.
388 * @param cfg configuration to serialize
389 * @param size will be set to the size of the serialized memory block
390 * @return the memory block where the serialized configuration is
391 * present. This memory should be freed by the caller
394 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
397 struct ConfigSection *sec;
398 struct ConfigEntry *ent;
408 /* Pass1 : calculate the buffer size required */
410 for (sec = cfg->sections; NULL != sec; sec = sec->next)
412 /* For each section we need to add 3 charaters: {'[',']','\n'} */
413 m_size += strlen (sec->name) + 3;
414 for (ent = sec->entries; NULL != ent; ent = ent->next)
416 if (NULL != ent->val)
418 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
420 while (NULL != (pos = strstr (pos, "\n")))
425 /* For each key = value pair we need to add 4 characters (2
426 spaces and 1 equal-to character and 1 new line) */
427 m_size += strlen (ent->key) + strlen (ent->val) + 4;
430 /* A new line after section end */
434 /* Pass2: Allocate memory and write the configuration to it */
435 mem = GNUNET_malloc (m_size);
441 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
442 GNUNET_assert (0 < len);
443 memcpy (mem + c_size, cbuf, len);
446 for (ent = sec->entries; NULL != ent; ent = ent->next)
448 if (NULL != ent->val)
450 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
451 strcpy (val, ent->val);
452 while (NULL != (pos = strstr (val, "\n")))
454 memmove (&pos[2], &pos[1], strlen (&pos[1]));
458 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
460 memcpy (mem + c_size, cbuf, len);
465 memcpy (mem + c_size, "\n", 1);
469 GNUNET_assert (c_size == m_size);
476 * Write configuration file.
478 * @param cfg configuration to write
479 * @param filename where to write the configuration
480 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
483 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
484 const char *filename)
490 fn = GNUNET_STRINGS_filename_expand (filename);
492 return GNUNET_SYSERR;
493 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
496 return GNUNET_SYSERR;
498 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
499 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
500 GNUNET_DISK_PERM_USER_READ
501 | GNUNET_DISK_PERM_USER_WRITE
502 | GNUNET_DISK_PERM_GROUP_READ
503 | GNUNET_DISK_PERM_GROUP_WRITE))
506 GNUNET_free (cfg_buf);
507 LOG (GNUNET_ERROR_TYPE_WARNING,
508 "Writing configration to file: %s failed\n", filename);
509 cfg->dirty = GNUNET_SYSERR; /* last write failed */
510 return GNUNET_SYSERR;
513 GNUNET_free (cfg_buf);
514 cfg->dirty = GNUNET_NO; /* last write succeeded */
520 * Iterate over all options in the configuration.
522 * @param cfg configuration to inspect
523 * @param iter function to call on each option
524 * @param iter_cls closure for @a iter
527 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
528 GNUNET_CONFIGURATION_Iterator iter,
531 struct ConfigSection *spos;
532 struct ConfigEntry *epos;
534 for (spos = cfg->sections; NULL != spos; spos = spos->next)
535 for (epos = spos->entries; NULL != epos; epos = epos->next)
536 if (NULL != epos->val)
537 iter (iter_cls, spos->name, epos->key, epos->val);
542 * Iterate over values of a section in the configuration.
544 * @param cfg configuration to inspect
545 * @param section the section
546 * @param iter function to call on each option
547 * @param iter_cls closure for @a iter
550 GNUNET_CONFIGURATION_iterate_section_values (const struct
551 GNUNET_CONFIGURATION_Handle *cfg,
553 GNUNET_CONFIGURATION_Iterator iter,
556 struct ConfigSection *spos;
557 struct ConfigEntry *epos;
559 spos = cfg->sections;
560 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
564 for (epos = spos->entries; NULL != epos; epos = epos->next)
565 if (NULL != epos->val)
566 iter (iter_cls, spos->name, epos->key, epos->val);
571 * Iterate over all sections in the configuration.
573 * @param cfg configuration to inspect
574 * @param iter function to call on each section
575 * @param iter_cls closure for @a iter
578 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
579 GNUNET_CONFIGURATION_Section_Iterator iter,
582 struct ConfigSection *spos;
583 struct ConfigSection *next;
585 next = cfg->sections;
590 iter (iter_cls, spos->name);
596 * Remove the given section and all options in it.
598 * @param cfg configuration to inspect
599 * @param section name of the section to remove
602 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
605 struct ConfigSection *spos;
606 struct ConfigSection *prev;
607 struct ConfigEntry *ent;
610 spos = cfg->sections;
613 if (0 == strcasecmp (section, spos->name))
616 cfg->sections = spos->next;
618 prev->next = spos->next;
619 while (NULL != (ent = spos->entries))
621 spos->entries = ent->next;
622 GNUNET_free (ent->key);
623 GNUNET_free_non_null (ent->val);
625 cfg->dirty = GNUNET_YES;
627 GNUNET_free (spos->name);
638 * Copy a configuration value to the given target configuration.
639 * Overwrites existing entries.
641 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
642 * @param section section for the value
643 * @param option option name of the value
644 * @param value value to copy
647 copy_entry (void *cls,
652 struct GNUNET_CONFIGURATION_Handle *dst = cls;
654 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
659 * Duplicate an existing configuration object.
661 * @param cfg configuration to duplicate
662 * @return duplicate configuration
664 struct GNUNET_CONFIGURATION_Handle *
665 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
667 struct GNUNET_CONFIGURATION_Handle *ret;
669 ret = GNUNET_CONFIGURATION_create ();
670 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
676 * Find a section entry from a configuration.
678 * @param cfg configuration to search in
679 * @param section name of the section to look for
680 * @return matching entry, NULL if not found
682 static struct ConfigSection *
683 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
686 struct ConfigSection *pos;
689 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
696 * Find an entry from a configuration.
698 * @param cfg handle to the configuration
699 * @param section section the option is in
700 * @param key the option
701 * @return matching entry, NULL if not found
703 static struct ConfigEntry *
704 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
708 struct ConfigSection *sec;
709 struct ConfigEntry *pos;
711 if (NULL == (sec = find_section (cfg, section)))
714 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
721 * A callback function, compares entries from two configurations
722 * (default against a new configuration) and write the diffs in a
723 * diff-configuration object (the callback object).
725 * @param cls the diff configuration (`struct DiffHandle *`)
726 * @param section section for the value (of the default conf.)
727 * @param option option name of the value (of the default conf.)
728 * @param value value to copy (of the default conf.)
731 compare_entries (void *cls,
736 struct DiffHandle *dh = cls;
737 struct ConfigEntry *entNew;
739 entNew = find_entry (dh->cfg_default, section, option);
740 if ( (NULL != entNew) &&
741 (NULL != entNew->val) &&
742 (0 == strcmp (entNew->val, value)) )
744 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
749 * Compute configuration with only entries that have been changed
751 * @param cfg_default original configuration
752 * @param cfg_new new configuration
753 * @return configuration with only the differences, never NULL
755 struct GNUNET_CONFIGURATION_Handle *
756 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
757 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
759 struct DiffHandle diffHandle;
761 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
762 diffHandle.cfg_default = cfg_default;
763 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
764 return diffHandle.cfgDiff;
769 * Write only configuration entries that have been changed to configuration file
771 * @param cfg_default default configuration
772 * @param cfg_new new configuration
773 * @param filename where to write the configuration diff between default and new
774 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
777 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
779 const struct GNUNET_CONFIGURATION_Handle
780 *cfg_new, const char *filename)
783 struct GNUNET_CONFIGURATION_Handle *diff;
785 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
786 ret = GNUNET_CONFIGURATION_write (diff, filename);
787 GNUNET_CONFIGURATION_destroy (diff);
793 * Set a configuration value that should be a string.
795 * @param cfg configuration to update
796 * @param section section of interest
797 * @param option option of interest
798 * @param value value to set
801 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
802 const char *section, const char *option,
805 struct ConfigSection *sec;
806 struct ConfigEntry *e;
809 e = find_entry (cfg, section, option);
814 GNUNET_free_non_null (e->val);
819 nv = GNUNET_strdup (value);
820 GNUNET_free_non_null (e->val);
825 sec = find_section (cfg, section);
828 sec = GNUNET_new (struct ConfigSection);
829 sec->name = GNUNET_strdup (section);
830 sec->next = cfg->sections;
833 e = GNUNET_new (struct ConfigEntry);
834 e->key = GNUNET_strdup (option);
835 e->val = GNUNET_strdup (value);
836 e->next = sec->entries;
842 * Set a configuration value that should be a number.
844 * @param cfg configuration to update
845 * @param section section of interest
846 * @param option option of interest
847 * @param number value to set
850 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
851 const char *section, const char *option,
852 unsigned long long number)
856 GNUNET_snprintf (s, 64, "%llu", number);
857 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
862 * Get a configuration value that should be a number.
864 * @param cfg configuration to inspect
865 * @param section section of interest
866 * @param option option of interest
867 * @param number where to store the numeric value of the option
868 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
871 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
872 *cfg, const char *section,
874 unsigned long long *number)
876 struct ConfigEntry *e;
878 if (NULL == (e = find_entry (cfg, section, option)))
879 return GNUNET_SYSERR;
881 return GNUNET_SYSERR;
882 if (1 != SSCANF (e->val, "%llu", number))
883 return GNUNET_SYSERR;
888 * Get a configuration value that should be a floating point number.
890 * @param cfg configuration to inspect
891 * @param section section of interest
892 * @param option option of interest
893 * @param number where to store the floating value of the option
894 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
897 GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle
898 *cfg, const char *section,
902 struct ConfigEntry *e;
904 if (NULL == (e = find_entry (cfg, section, option)))
905 return GNUNET_SYSERR;
907 return GNUNET_SYSERR;
908 if (1 != SSCANF (e->val, "%f", number))
909 return GNUNET_SYSERR;
916 * Get a configuration value that should be a relative time.
918 * @param cfg configuration to inspect
919 * @param section section of interest
920 * @param option option of interest
921 * @param time set to the time value stored in the configuration
922 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
925 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
928 struct GNUNET_TIME_Relative *time)
930 struct ConfigEntry *e;
932 if (NULL == (e = find_entry (cfg, section, option)))
933 return GNUNET_SYSERR;
935 return GNUNET_SYSERR;
936 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
941 * Get a configuration value that should be a size in bytes.
943 * @param cfg configuration to inspect
944 * @param section section of interest
945 * @param option option of interest
946 * @param size set to the size in bytes as stored in the configuration
947 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
950 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
953 unsigned long long *size)
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_size_to_bytes (e->val, size);
966 * Get a configuration value that should be a string.
968 * @param cfg configuration to inspect
969 * @param section section of interest
970 * @param option option of interest
971 * @param value will be set to a freshly allocated configuration
972 * value, or NULL if option is not specified
973 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
976 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
981 struct ConfigEntry *e;
983 LOG (GNUNET_ERROR_TYPE_DEBUG,
984 "Asked to retrieve string `%s' in section `%s'\n",
987 if ( (NULL == (e = find_entry (cfg, section, option))) ||
991 return GNUNET_SYSERR;
993 *value = GNUNET_strdup (e->val);
999 * Get a configuration value that should be in a set of
1000 * predefined strings
1002 * @param cfg configuration to inspect
1003 * @param section section of interest
1004 * @param option option of interest
1005 * @param choices NULL-terminated list of legal values
1006 * @param value will be set to an entry in the legal list,
1007 * or NULL if option is not specified and no default given
1008 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1011 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1012 const char *section,
1014 const char *const *choices,
1017 struct ConfigEntry *e;
1020 if (NULL == (e = find_entry (cfg, section, option)))
1021 return GNUNET_SYSERR;
1022 for (i = 0; NULL != choices[i]; i++)
1023 if (0 == strcasecmp (choices[i], e->val))
1025 if (NULL == choices[i])
1027 LOG (GNUNET_ERROR_TYPE_ERROR,
1028 _("Configuration value '%s' for '%s'"
1029 " in section '%s' is not in set of legal choices\n"),
1033 return GNUNET_SYSERR;
1035 *value = choices[i];
1041 * Get crockford32-encoded fixed-size binary data from a configuration.
1043 * @param cfg configuration to access
1044 * @param section section to access
1045 * @param option option to access
1046 * @param buf where to store the decoded binary result
1047 * @param buf_size exact number of bytes to store in @a buf
1048 * @return #GNUNET_OK on success
1049 * #GNUNET_NO is the value does not exist
1050 * #GNUNET_SYSERR on decoding error
1053 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1054 const char *section,
1064 (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1069 data_size = (strlen (enc) * 5) / 8;
1070 if (data_size != buf_size)
1073 return GNUNET_SYSERR;
1076 GNUNET_STRINGS_string_to_data (enc,
1081 return GNUNET_SYSERR;
1089 * Test if we have a value for a particular option
1091 * @param cfg configuration to inspect
1092 * @param section section of interest
1093 * @param option option of interest
1094 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1097 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1098 const char *section,
1101 struct ConfigEntry *e;
1103 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1110 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1111 * where either in the "PATHS" section or the environtment "FOO" is
1112 * set to "DIRECTORY". We also support default expansion,
1113 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1114 * set in PATHS or the environment, and otherwise to "default". Note
1115 * that "default" itself can also be a $-expression, thus
1116 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1119 * @param cfg configuration to use for path expansion
1120 * @param orig string to $-expand (will be freed!)
1121 * @param depth recursion depth, used to detect recursive expansions
1122 * @return $-expanded string
1125 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1146 LOG (GNUNET_ERROR_TYPE_WARNING,
1147 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "Asked to $-expand %s\n", orig);
1155 LOG (GNUNET_ERROR_TYPE_DEBUG,
1156 "Doesn't start with $ - not expanding\n");
1178 LOG (GNUNET_ERROR_TYPE_WARNING,
1179 _("Missing closing `%s' in option `%s'\n"),
1191 def = strchr (orig, ':');
1196 if ( ('-' == *def) ||
1199 def = GNUNET_strdup (def);
1207 while ( (orig[i] != '/') &&
1208 (orig[i] != '\\') &&
1209 (orig[i] != '\0') &&
1212 if (orig[i] == '\0')
1218 erased_char = orig[i];
1219 erased_pos = &orig[i];
1221 post = &orig[i + 1];
1224 LOG (GNUNET_ERROR_TYPE_DEBUG,
1225 "Split into `%s' and `%s' with default %s\n",
1230 GNUNET_CONFIGURATION_get_value_string (cfg,
1235 LOG (GNUNET_ERROR_TYPE_DEBUG,
1236 "Filename for `%s' is not in PATHS config section\n",
1238 if (NULL == (env = getenv (start)))
1240 LOG (GNUNET_ERROR_TYPE_DEBUG,
1241 "`%s' is not an environment variable\n",
1244 def = expand_dollar (cfg, def, depth + 1);
1249 start = GNUNET_strdup (start);
1251 *erased_pos = erased_char;
1252 LOG (GNUNET_ERROR_TYPE_WARNING,
1253 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1255 GNUNET_free (start);
1258 prefix = GNUNET_strdup (env);
1260 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1261 LOG (GNUNET_ERROR_TYPE_DEBUG,
1264 if ( (erased_pos) && ('}' != erased_char) )
1266 len = strlen (prefix) + 1;
1267 prefix = GNUNET_realloc (prefix, len + 1);
1268 prefix[len - 1] = erased_char;
1271 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1272 strcpy (result, prefix);
1273 strcat (result, post);
1274 GNUNET_free_non_null (def);
1275 GNUNET_free (prefix);
1277 LOG (GNUNET_ERROR_TYPE_DEBUG,
1278 "Expanded to `%s'\n",
1285 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1286 * where either in the "PATHS" section or the environtment "FOO" is
1287 * set to "DIRECTORY". We also support default expansion,
1288 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1289 * set in PATHS or the environment, and otherwise to "default". Note
1290 * that "default" itself can also be a $-expression, thus
1291 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1294 * @param cfg configuration to use for path expansion
1295 * @param orig string to $-expand (will be freed!). Note that multiple
1296 * $-expressions can be present in this string. They will all be
1298 * @return $-expanded string
1301 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1308 for (i = 0; '\0' != orig[i]; i++)
1312 dup = GNUNET_strdup (orig + i);
1313 dup = expand_dollar (cfg, dup, 0);
1314 len = strlen (dup) + 1;
1315 orig = GNUNET_realloc (orig, i + len);
1316 memcpy (orig + i, dup, len);
1324 * Get a configuration value that should be a string.
1326 * @param cfg configuration to inspect
1327 * @param section section of interest
1328 * @param option option of interest
1329 * @param value will be set to a freshly allocated configuration
1330 * value, or NULL if option is not specified
1331 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1334 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1335 const char *section,
1341 LOG (GNUNET_ERROR_TYPE_DEBUG,
1342 "Asked to retrieve filename `%s' in section `%s'\n",
1346 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1348 LOG (GNUNET_ERROR_TYPE_DEBUG,
1349 "Failed to retrieve filename\n");
1351 return GNUNET_SYSERR;
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1354 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1355 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1356 *value = GNUNET_STRINGS_filename_expand (tmp);
1358 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1360 return GNUNET_SYSERR;
1366 * Get a configuration value that should be in a set of
1369 * @param cfg configuration to inspect
1370 * @param section section of interest
1371 * @param option option of interest
1372 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1375 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1376 const char *section,
1379 static const char *yesno[] = { "YES", "NO", NULL };
1384 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1385 if (ret == GNUNET_SYSERR)
1387 if (val == yesno[0])
1394 * Iterate over the set of filenames stored in a configuration value.
1396 * @param cfg configuration to inspect
1397 * @param section section of interest
1398 * @param option option of interest
1399 * @param cb function to call on each filename
1400 * @param cb_cls closure for @a cb
1401 * @return number of filenames iterated over, -1 on error
1404 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1405 const char *section,
1407 GNUNET_FileNameCallback cb,
1417 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1419 GNUNET_assert (list != NULL);
1424 while (pos[0] == ' ')
1426 if (strlen (pos) == 0)
1429 while ((end[0] != ' ') && (end[0] != '\0'))
1437 memmove (end, &end[1], strlen (&end[1]) + 1);
1439 /* illegal, but just keep it */
1442 /* illegal, but just ignore that there was a '/' */
1450 if (strlen (pos) > 0)
1453 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1455 ret = GNUNET_SYSERR;
1471 * @param value FIXME
1475 escape_name (const char *value)
1481 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1482 memset (escaped, 0, strlen (value) * 2 + 1);
1485 while (rpos[0] != '\0')
1508 * @param cls string we compare with (const char*)
1509 * @param fn filename we are currently looking at
1510 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1513 test_match (void *cls, const char *fn)
1515 const char *of = cls;
1517 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1522 * Append a filename to a configuration value that
1523 * represents a list of filenames
1525 * @param cfg configuration to update
1526 * @param section section of interest
1527 * @param option option of interest
1528 * @param value filename to append
1529 * @return #GNUNET_OK on success,
1530 * #GNUNET_NO if the filename already in the list
1531 * #GNUNET_SYSERR on error
1534 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1535 const char *section,
1543 if (GNUNET_SYSERR ==
1544 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1547 return GNUNET_NO; /* already exists */
1549 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1550 old = GNUNET_strdup ("");
1551 escaped = escape_name (value);
1552 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1554 if (strlen (old) > 0)
1556 strcat (nw, escaped);
1557 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1560 GNUNET_free (escaped);
1566 * Remove a filename from a configuration value that
1567 * represents a list of filenames
1569 * @param cfg configuration to update
1570 * @param section section of interest
1571 * @param option option of interest
1572 * @param value filename to remove
1573 * @return #GNUNET_OK on success,
1574 * #GNUNET_NO if the filename is not in the list,
1575 * #GNUNET_SYSERR on error
1578 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1579 *cfg, const char *section,
1590 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1592 match = escape_name (value);
1596 while (pos[0] == ' ')
1598 if (strlen (pos) == 0)
1601 while ((end[0] != ' ') && (end[0] != '\0'))
1612 /* illegal, but just keep it */
1615 /* illegal, but just ignore that there was a '/' */
1623 if (0 == strcmp (pos, match))
1626 memmove (pos, &end[1], strlen (&end[1]) + 1);
1634 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1636 GNUNET_free (match);
1645 GNUNET_free (match);
1651 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1652 * file in a directory, we trigger parsing on those files that
1655 * @param cls the cfg
1656 * @param filename file to parse
1657 * @return #GNUNET_OK on success
1660 parse_configuration_file (void *cls, const char *filename)
1662 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1666 /* Examine file extension */
1667 ext = strrchr (filename, '.');
1668 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1670 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1671 "Skipping file `%s'\n",
1676 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1682 * Load default configuration. This function will parse the
1683 * defaults from the given defaults_d directory.
1685 * @param cfg configuration to update
1686 * @param defaults_d directory with the defaults
1687 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1690 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1691 const char *defaults_d)
1693 if (GNUNET_SYSERR ==
1694 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1695 return GNUNET_SYSERR; /* no configuration at all found */
1700 /* end of configuration.c */