2 This file is part of GNUnet.
3 (C) 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file src/util/configuration.c
23 * @brief configuration management
24 * @author Christian Grothoff
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_strings_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 *cfgDefault;
109 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_malloc (sizeof (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 * Parse a configuration file, add all of the options in the
144 * file to the configuration environment.
146 * @param cfg configuration to update
147 * @param filename name of the configuration file
148 * @return GNUNET_OK on success, GNUNET_SYSERR on error
151 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
152 const char *filename)
166 fn = GNUNET_STRINGS_filename_expand (filename);
168 return GNUNET_SYSERR;
169 dirty = cfg->dirty; /* back up value! */
170 if (NULL == (fp = FOPEN (fn, "r")))
172 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
174 return GNUNET_SYSERR;
178 section = GNUNET_strdup ("");
179 memset (line, 0, 256);
181 while (NULL != fgets (line, 255, fp))
184 for (i = 0; i < 255; i++)
187 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' || line[0] == '\r')
190 for (i = 0; (i < 255 && line[i] != 0); i++)
191 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
195 /* remove tailing whitespace */
196 for (i = strlen (line) - 1; (i >= 0) && (isspace ((unsigned char) line[i]));
199 if (1 == SSCANF (line, "@INLINE@ %191[^\n]", value))
202 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
203 ret = GNUNET_SYSERR; /* failed to parse included config */
205 else if (1 == SSCANF (line, "[%99[^]]]", value))
208 GNUNET_free (section);
209 section = GNUNET_strdup (value);
211 else if (2 == SSCANF (line, " %63[^= ] = %191[^\n]", tag, value))
215 i = strlen (value) - 1;
216 while ((i >= 0) && (isspace ((unsigned char) value[i])))
223 while ((value[i] != '\0') && (value[i] != '"'))
233 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
235 else if (1 == SSCANF (line, " %63[^= ] =[^\n]", tag))
238 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
243 LOG (GNUNET_ERROR_TYPE_WARNING,
244 _("Syntax error in configuration file `%s' at line %u.\n"), filename,
250 GNUNET_assert (0 == FCLOSE (fp));
251 /* restore dirty flag - anything we set in the meantime
254 GNUNET_free (section);
260 * Test if there are configuration options that were
261 * changed since the last save.
263 * @param cfg configuration to inspect
264 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
267 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
274 * Serializes the given configuration.
276 * @param cfg configuration to serialize
277 * @param size will be set to the size of the serialized memory block
278 * @return the memory block where the serialized configuration is
279 * present. This memory should be freed by the caller
282 GNUNET_CONFIGURATION_serialize (struct GNUNET_CONFIGURATION_Handle *cfg,
285 struct ConfigSection *sec;
286 struct ConfigEntry *ent;
296 /* Pass1 : calculate the buffer size required */
301 /* For each section we need to add 3 charaters: {'[',']','\n'} */
302 m_size += strlen (sec->name) + 3;
306 if (NULL != ent->val)
308 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
310 while (NULL != (pos = strstr (pos, "\n")))
315 /* For each key = value pair we need to add 4 characters (2
316 spaces and 1 equal-to character and 1 new line) */
317 m_size += strlen (ent->key) + strlen (ent->val) + 4;
321 /* A new line after section end */
326 /* Pass2: Allocate memory and write the configuration to it */
327 mem = GNUNET_malloc (m_size);
333 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
334 memcpy (mem + c_size, cbuf, len);
340 if (NULL != ent->val)
342 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
343 strcpy (val, ent->val);
344 while (NULL != (pos = strstr (val, "\n")))
346 memmove (&pos[2], &pos[1], strlen (&pos[1]));
350 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
352 memcpy (mem + c_size, cbuf, len);
358 memcpy (mem + c_size, "\n", 1);
362 GNUNET_assert (c_size == m_size);
369 * Write configuration file.
371 * @param cfg configuration to write
372 * @param filename where to write the configuration
373 * @return GNUNET_OK on success, GNUNET_SYSERR on error
376 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
377 const char *filename)
383 fn = GNUNET_STRINGS_filename_expand (filename);
385 return GNUNET_SYSERR;
386 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
389 return GNUNET_SYSERR;
391 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
392 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
393 GNUNET_DISK_PERM_USER_READ
394 | GNUNET_DISK_PERM_USER_WRITE
395 | GNUNET_DISK_PERM_GROUP_READ
396 | GNUNET_DISK_PERM_GROUP_WRITE))
399 GNUNET_free (cfg_buf);
400 LOG (GNUNET_ERROR_TYPE_WARNING,
401 "Writing configration to file: %s failed\n", filename);
402 cfg->dirty = GNUNET_SYSERR; /* last write failed */
403 return GNUNET_SYSERR;
406 GNUNET_free (cfg_buf);
407 cfg->dirty = GNUNET_NO; /* last write succeeded */
413 * Iterate over all options in the configuration.
415 * @param cfg configuration to inspect
416 * @param iter function to call on each option
417 * @param iter_cls closure for iter
420 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
421 GNUNET_CONFIGURATION_Iterator iter,
424 struct ConfigSection *spos;
425 struct ConfigEntry *epos;
427 spos = cfg->sections;
430 epos = spos->entries;
433 iter (iter_cls, spos->name, epos->key, epos->val);
442 * Iterate over values of a section in the configuration.
444 * @param cfg configuration to inspect
445 * @param section the section
446 * @param iter function to call on each option
447 * @param iter_cls closure for iter
450 GNUNET_CONFIGURATION_iterate_section_values (const struct
451 GNUNET_CONFIGURATION_Handle *cfg,
453 GNUNET_CONFIGURATION_Iterator iter,
456 struct ConfigSection *spos;
457 struct ConfigEntry *epos;
459 spos = cfg->sections;
460 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
466 epos = spos->entries;
469 iter (iter_cls, spos->name, epos->key, epos->val);
476 * Iterate over all sections in the configuration.
478 * @param cfg configuration to inspect
479 * @param iter function to call on each section
480 * @param iter_cls closure for iter
483 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
485 GNUNET_CONFIGURATION_Section_Iterator
486 iter, void *iter_cls)
488 struct ConfigSection *spos;
489 struct ConfigSection *next;
491 next = cfg->sections;
496 iter (iter_cls, spos->name);
501 * Remove the given section and all options in it.
503 * @param cfg configuration to inspect
504 * @param section name of the section to remove
507 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
510 struct ConfigSection *spos;
511 struct ConfigSection *prev;
512 struct ConfigEntry *ent;
515 spos = cfg->sections;
518 if (0 == strcasecmp (section, spos->name))
521 cfg->sections = spos->next;
523 prev->next = spos->next;
524 while (NULL != (ent = spos->entries))
526 spos->entries = ent->next;
527 GNUNET_free (ent->key);
528 GNUNET_free_non_null (ent->val);
530 cfg->dirty = GNUNET_YES;
532 GNUNET_free (spos->name);
543 * Copy a configuration value to the given target configuration.
544 * Overwrites existing entries.
546 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
547 * @param section section for the value
548 * @param option option name of the value
549 * @param value value to copy
552 copy_entry (void *cls, const char *section, const char *option,
555 struct GNUNET_CONFIGURATION_Handle *dst = cls;
557 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
562 * Duplicate an existing configuration object.
564 * @param cfg configuration to duplicate
565 * @return duplicate configuration
567 struct GNUNET_CONFIGURATION_Handle *
568 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
570 struct GNUNET_CONFIGURATION_Handle *ret;
572 ret = GNUNET_CONFIGURATION_create ();
573 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
582 * @param section FIXME
583 * @return matching entry, NULL if not found
585 static struct ConfigSection *
586 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
588 struct ConfigSection *pos;
591 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
598 * Find an entry from a configuration.
600 * @param cfg handle to the configuration
601 * @param section section the option is in
602 * @param key the option
603 * @return matching entry, NULL if not found
605 static struct ConfigEntry *
606 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
609 struct ConfigSection *sec;
610 struct ConfigEntry *pos;
612 sec = findSection (cfg, section);
616 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
623 * A callback function, compares entries from two configurations
624 * (default against a new configuration) and write the diffs in a
625 * diff-configuration object (the callback object).
627 * @param cls the diff configuration (struct DiffHandle*)
628 * @param section section for the value (of the default conf.)
629 * @param option option name of the value (of the default conf.)
630 * @param value value to copy (of the default conf.)
633 compareEntries (void *cls, const char *section, const char *option,
636 struct DiffHandle *dh = cls;
637 struct ConfigEntry *entNew;
639 entNew = findEntry (dh->cfgDefault, section, option);
640 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
642 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
647 * Write only configuration entries that have been changed to configuration file
648 * @param cfgDefault default configuration
649 * @param cfgNew new configuration
650 * @param filename where to write the configuration diff between default and new
651 * @return GNUNET_OK on success, GNUNET_SYSERR on error
654 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
656 const struct GNUNET_CONFIGURATION_Handle
657 *cfgNew, const char *filename)
660 struct DiffHandle diffHandle;
662 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
663 diffHandle.cfgDefault = cfgDefault;
664 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
665 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
666 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
672 * Set a configuration value that should be a string.
674 * @param cfg configuration to update
675 * @param section section of interest
676 * @param option option of interest
677 * @param value value to set
680 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
681 const char *section, const char *option,
684 struct ConfigSection *sec;
685 struct ConfigEntry *e;
688 e = findEntry (cfg, section, option);
691 nv = GNUNET_strdup (value);
692 GNUNET_free_non_null (e->val);
696 sec = findSection (cfg, section);
699 sec = GNUNET_malloc (sizeof (struct ConfigSection));
700 sec->name = GNUNET_strdup (section);
701 sec->next = cfg->sections;
704 e = GNUNET_malloc (sizeof (struct ConfigEntry));
705 e->key = GNUNET_strdup (option);
706 e->val = GNUNET_strdup (value);
707 e->next = sec->entries;
713 * Set a configuration value that should be a number.
715 * @param cfg configuration to update
716 * @param section section of interest
717 * @param option option of interest
718 * @param number value to set
721 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
722 const char *section, const char *option,
723 unsigned long long number)
727 GNUNET_snprintf (s, 64, "%llu", number);
728 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
733 * Get a configuration value that should be a number.
735 * @param cfg configuration to inspect
736 * @param section section of interest
737 * @param option option of interest
738 * @param number where to store the numeric value of the option
739 * @return GNUNET_OK on success, GNUNET_SYSERR on error
742 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
743 *cfg, const char *section,
745 unsigned long long *number)
747 struct ConfigEntry *e;
749 e = findEntry (cfg, section, option);
751 return GNUNET_SYSERR;
752 if (1 != SSCANF (e->val, "%llu", number))
753 return GNUNET_SYSERR;
759 * Get a configuration value that should be a relative time.
761 * @param cfg configuration to inspect
762 * @param section section of interest
763 * @param option option of interest
764 * @param time set to the time value stored in the configuration
765 * @return GNUNET_OK on success, GNUNET_SYSERR on error
768 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
769 *cfg, const char *section,
771 struct GNUNET_TIME_Relative *time)
773 struct ConfigEntry *e;
775 e = findEntry (cfg, section, option);
777 return GNUNET_SYSERR;
779 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
784 * Get a configuration value that should be a size in bytes.
786 * @param cfg configuration to inspect
787 * @param section section of interest
788 * @param option option of interest
789 * @param size set to the size in bytes as stored in the configuration
790 * @return GNUNET_OK on success, GNUNET_SYSERR on error
793 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
794 *cfg, const char *section,
796 unsigned long long *size)
798 struct ConfigEntry *e;
800 e = findEntry (cfg, section, option);
802 return GNUNET_SYSERR;
803 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
808 * Get a configuration value that should be a string.
810 * @param cfg configuration to inspect
811 * @param section section of interest
812 * @param option option of interest
813 * @param value will be set to a freshly allocated configuration
814 * value, or NULL if option is not specified
815 * @return GNUNET_OK on success, GNUNET_SYSERR on error
818 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
819 *cfg, const char *section,
820 const char *option, char **value)
822 struct ConfigEntry *e;
824 e = findEntry (cfg, section, option);
825 if ((e == NULL) || (e->val == NULL))
828 return GNUNET_SYSERR;
830 *value = GNUNET_strdup (e->val);
836 * Get a configuration value that should be in a set of
839 * @param cfg configuration to inspect
840 * @param section section of interest
841 * @param option option of interest
842 * @param choices NULL-terminated list of legal values
843 * @param value will be set to an entry in the legal list,
844 * or NULL if option is not specified and no default given
845 * @return GNUNET_OK on success, GNUNET_SYSERR on error
848 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
849 *cfg, const char *section,
850 const char *option, const char **choices,
853 struct ConfigEntry *e;
856 e = findEntry (cfg, section, option);
858 return GNUNET_SYSERR;
860 while (choices[i] != NULL)
862 if (0 == strcasecmp (choices[i], e->val))
866 if (choices[i] == NULL)
868 LOG (GNUNET_ERROR_TYPE_ERROR,
869 _("Configuration value '%s' for '%s'"
870 " in section '%s' is not in set of legal choices\n"), e->val, option,
872 return GNUNET_SYSERR;
880 * Test if we have a value for a particular option
881 * @param cfg configuration to inspect
882 * @param section section of interest
883 * @param option option of interest
884 * @return GNUNET_YES if so, GNUNET_NO if not.
887 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
888 const char *section, const char *option)
890 struct ConfigEntry *e;
892 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
899 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
900 * where either in the "PATHS" section or the environtment
901 * "FOO" is set to "DIRECTORY".
903 * @param cfg configuration to use for path expansion
904 * @param orig string to $-expand (will be freed!)
905 * @return $-expanded string
908 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
920 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
932 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
934 if (NULL == (env = getenv (&orig[1])))
936 orig[i] = DIR_SEPARATOR;
939 prefix = GNUNET_strdup (env);
941 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
942 strcpy (result, prefix);
943 if ((strlen (prefix) == 0) ||
944 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
945 strcat (result, DIR_SEPARATOR_STR);
946 strcat (result, post);
947 GNUNET_free (prefix);
954 * Get a configuration value that should be a string.
956 * @param cfg configuration to inspect
957 * @param section section of interest
958 * @param option option of interest
959 * @param value will be set to a freshly allocated configuration
960 * value, or NULL if option is not specified
961 * @return GNUNET_OK on success, GNUNET_SYSERR on error
964 GNUNET_CONFIGURATION_get_value_filename (const struct
965 GNUNET_CONFIGURATION_Handle *cfg,
967 const char *option, char **value)
972 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
975 return GNUNET_SYSERR;
977 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
978 *value = GNUNET_STRINGS_filename_expand (tmp);
981 return GNUNET_SYSERR;
987 * Get a configuration value that should be in a set of
988 * "GNUNET_YES" or "GNUNET_NO".
990 * @param cfg configuration to inspect
991 * @param section section of interest
992 * @param option option of interest
993 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
996 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
997 *cfg, const char *section,
1000 static const char *yesno[] = { "YES", "NO", NULL };
1005 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1006 if (ret == GNUNET_SYSERR)
1008 if (val == yesno[0])
1015 * Iterate over the set of filenames stored in a configuration value.
1017 * @param cfg configuration to inspect
1018 * @param section section of interest
1019 * @param option option of interest
1020 * @param cb function to call on each filename
1021 * @param cb_cls closure for cb
1022 * @return number of filenames iterated over, -1 on error
1025 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1026 GNUNET_CONFIGURATION_Handle *cfg,
1027 const char *section,
1029 GNUNET_FileNameCallback cb,
1039 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1041 GNUNET_assert (list != NULL);
1046 while (pos[0] == ' ')
1048 if (strlen (pos) == 0)
1051 while ((end[0] != ' ') && (end[0] != '\0'))
1059 memmove (end, &end[1], strlen (&end[1]) + 1);
1061 /* illegal, but just keep it */
1064 /* illegal, but just ignore that there was a '/' */
1072 if (strlen (pos) > 0)
1075 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1077 ret = GNUNET_SYSERR;
1093 * @param value FIXME
1097 escape_name (const char *value)
1103 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1104 memset (escaped, 0, strlen (value) * 2 + 1);
1107 while (rpos[0] != '\0')
1130 * @param cls string we compare with (const char*)
1131 * @param fn filename we are currently looking at
1132 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1135 test_match (void *cls, const char *fn)
1137 const char *of = cls;
1139 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1144 * Append a filename to a configuration value that
1145 * represents a list of filenames
1147 * @param cfg configuration to update
1148 * @param section section of interest
1149 * @param option option of interest
1150 * @param value filename to append
1151 * @return GNUNET_OK on success,
1152 * GNUNET_NO if the filename already in the list
1153 * GNUNET_SYSERR on error
1156 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1157 *cfg, const char *section,
1165 if (GNUNET_SYSERR ==
1166 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1169 return GNUNET_NO; /* already exists */
1171 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1172 old = GNUNET_strdup ("");
1173 escaped = escape_name (value);
1174 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1176 if (strlen (old) > 0)
1178 strcat (nw, escaped);
1179 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1182 GNUNET_free (escaped);
1188 * Remove a filename from a configuration value that
1189 * represents a list of filenames
1191 * @param cfg configuration to update
1192 * @param section section of interest
1193 * @param option option of interest
1194 * @param value filename to remove
1195 * @return GNUNET_OK on success,
1196 * GNUNET_NO if the filename is not in the list,
1197 * GNUNET_SYSERR on error
1200 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1201 *cfg, const char *section,
1212 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1214 match = escape_name (value);
1218 while (pos[0] == ' ')
1220 if (strlen (pos) == 0)
1223 while ((end[0] != ' ') && (end[0] != '\0'))
1234 /* illegal, but just keep it */
1237 /* illegal, but just ignore that there was a '/' */
1245 if (0 == strcmp (pos, match))
1248 memmove (pos, &end[1], strlen (&end[1]) + 1);
1256 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1258 GNUNET_free (match);
1267 GNUNET_free (match);
1273 * Wrapper around GNUNET_CONFIGURATION_parse.
1275 * @param cls the cfg
1276 * @param filename file to parse
1277 * @return GNUNET_OK on success
1280 parse_configuration_file (void *cls, const char *filename)
1282 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1286 /* Examine file extension */
1287 ext = strrchr (filename, '.');
1288 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1290 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1294 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1300 * Load default configuration. This function will parse the
1301 * defaults from the given defaults_d directory.
1303 * @param cfg configuration to update
1304 * @param defaults_d directory with the defaults
1305 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1308 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1309 const char *defaults_d)
1311 if (GNUNET_SYSERR ==
1312 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1313 return GNUNET_SYSERR; /* no configuration at all found */
1319 * Load configuration (starts with defaults, then loads
1320 * system-specific configuration).
1322 * @param cfg configuration to update
1323 * @param filename name of the configuration file, NULL to load defaults
1324 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1327 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1328 const char *filename)
1333 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1335 return GNUNET_SYSERR;
1337 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1338 GNUNET_free (ipath);
1339 if (GNUNET_SYSERR ==
1340 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1342 GNUNET_free (baseconfig);
1343 return GNUNET_SYSERR; /* no configuration at all found */
1345 GNUNET_free (baseconfig);
1346 if ((filename != NULL) &&
1347 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1349 /* specified configuration not found */
1350 return GNUNET_SYSERR;
1353 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1355 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1358 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1360 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1361 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1367 /* end of configuration.c */