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 * Write configuration file.
276 * @param cfg configuration to write
277 * @param filename where to write the configuration
278 * @return GNUNET_OK on success, GNUNET_SYSERR on error
281 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
282 const char *filename)
284 struct ConfigSection *sec;
285 struct ConfigEntry *ent;
292 fn = GNUNET_STRINGS_filename_expand (filename);
294 return GNUNET_SYSERR;
295 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
298 return GNUNET_SYSERR;
300 if (NULL == (fp = FOPEN (fn, "w")))
302 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
304 return GNUNET_SYSERR;
311 if (0 > FPRINTF (fp, "[%s]\n", sec->name))
319 if (ent->val != NULL)
321 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
322 strcpy (val, ent->val);
323 while (NULL != (pos = strstr (val, "\n")))
325 memmove (&pos[2], &pos[1], strlen (&pos[1]));
329 if (0 > FPRINTF (fp, "%s = %s\n", ent->key, val))
341 if (0 > FPRINTF (fp, "%s\n", ""))
349 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
350 GNUNET_assert (0 == FCLOSE (fp));
353 cfg->dirty = GNUNET_SYSERR; /* last write failed */
354 return GNUNET_SYSERR;
356 cfg->dirty = GNUNET_NO; /* last write succeeded */
362 * Iterate over all options in the configuration.
364 * @param cfg configuration to inspect
365 * @param iter function to call on each option
366 * @param iter_cls closure for iter
369 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
370 GNUNET_CONFIGURATION_Iterator iter,
373 struct ConfigSection *spos;
374 struct ConfigEntry *epos;
376 spos = cfg->sections;
379 epos = spos->entries;
382 iter (iter_cls, spos->name, epos->key, epos->val);
391 * Iterate over values of a section in the configuration.
393 * @param cfg configuration to inspect
394 * @param section the section
395 * @param iter function to call on each option
396 * @param iter_cls closure for iter
399 GNUNET_CONFIGURATION_iterate_section_values (const struct
400 GNUNET_CONFIGURATION_Handle *cfg,
402 GNUNET_CONFIGURATION_Iterator iter,
405 struct ConfigSection *spos;
406 struct ConfigEntry *epos;
408 spos = cfg->sections;
409 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
415 epos = spos->entries;
418 iter (iter_cls, spos->name, epos->key, epos->val);
425 * Iterate over all sections in the configuration.
427 * @param cfg configuration to inspect
428 * @param iter function to call on each section
429 * @param iter_cls closure for iter
432 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
434 GNUNET_CONFIGURATION_Section_Iterator
435 iter, void *iter_cls)
437 struct ConfigSection *spos;
438 struct ConfigSection *next;
440 next = cfg->sections;
445 iter (iter_cls, spos->name);
450 * Remove the given section and all options in it.
452 * @param cfg configuration to inspect
453 * @param section name of the section to remove
456 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
459 struct ConfigSection *spos;
460 struct ConfigSection *prev;
461 struct ConfigEntry *ent;
464 spos = cfg->sections;
467 if (0 == strcasecmp (section, spos->name))
470 cfg->sections = spos->next;
472 prev->next = spos->next;
473 while (NULL != (ent = spos->entries))
475 spos->entries = ent->next;
476 GNUNET_free (ent->key);
477 GNUNET_free_non_null (ent->val);
479 cfg->dirty = GNUNET_YES;
481 GNUNET_free (spos->name);
492 * Copy a configuration value to the given target configuration.
493 * Overwrites existing entries.
495 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
496 * @param section section for the value
497 * @param option option name of the value
498 * @param value value to copy
501 copy_entry (void *cls, const char *section, const char *option,
504 struct GNUNET_CONFIGURATION_Handle *dst = cls;
506 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
511 * Duplicate an existing configuration object.
513 * @param cfg configuration to duplicate
514 * @return duplicate configuration
516 struct GNUNET_CONFIGURATION_Handle *
517 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
519 struct GNUNET_CONFIGURATION_Handle *ret;
521 ret = GNUNET_CONFIGURATION_create ();
522 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
531 * @param section FIXME
532 * @return matching entry, NULL if not found
534 static struct ConfigSection *
535 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
537 struct ConfigSection *pos;
540 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
547 * Find an entry from a configuration.
549 * @param cfg handle to the configuration
550 * @param section section the option is in
551 * @param key the option
552 * @return matching entry, NULL if not found
554 static struct ConfigEntry *
555 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
558 struct ConfigSection *sec;
559 struct ConfigEntry *pos;
561 sec = findSection (cfg, section);
565 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
572 * A callback function, compares entries from two configurations
573 * (default against a new configuration) and write the diffs in a
574 * diff-configuration object (the callback object).
576 * @param cls the diff configuration (struct DiffHandle*)
577 * @param section section for the value (of the default conf.)
578 * @param option option name of the value (of the default conf.)
579 * @param value value to copy (of the default conf.)
582 compareEntries (void *cls, const char *section, const char *option,
585 struct DiffHandle *dh = cls;
586 struct ConfigEntry *entNew;
588 entNew = findEntry (dh->cfgDefault, section, option);
589 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
591 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
596 * Write only configuration entries that have been changed to configuration file
597 * @param cfgDefault default configuration
598 * @param cfgNew new configuration
599 * @param filename where to write the configuration diff between default and new
600 * @return GNUNET_OK on success, GNUNET_SYSERR on error
603 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
605 const struct GNUNET_CONFIGURATION_Handle
606 *cfgNew, const char *filename)
609 struct DiffHandle diffHandle;
611 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
612 diffHandle.cfgDefault = cfgDefault;
613 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
614 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
615 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
621 * Set a configuration value that should be a string.
623 * @param cfg configuration to update
624 * @param section section of interest
625 * @param option option of interest
626 * @param value value to set
629 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
630 const char *section, const char *option,
633 struct ConfigSection *sec;
634 struct ConfigEntry *e;
636 e = findEntry (cfg, section, option);
639 GNUNET_free_non_null (e->val);
640 e->val = GNUNET_strdup (value);
643 sec = findSection (cfg, section);
646 sec = GNUNET_malloc (sizeof (struct ConfigSection));
647 sec->name = GNUNET_strdup (section);
648 sec->next = cfg->sections;
651 e = GNUNET_malloc (sizeof (struct ConfigEntry));
652 e->key = GNUNET_strdup (option);
653 e->val = GNUNET_strdup (value);
654 e->next = sec->entries;
660 * Set a configuration value that should be a number.
662 * @param cfg configuration to update
663 * @param section section of interest
664 * @param option option of interest
665 * @param number value to set
668 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
669 const char *section, const char *option,
670 unsigned long long number)
674 GNUNET_snprintf (s, 64, "%llu", number);
675 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
680 * Get a configuration value that should be a number.
682 * @param cfg configuration to inspect
683 * @param section section of interest
684 * @param option option of interest
685 * @param number where to store the numeric value of the option
686 * @return GNUNET_OK on success, GNUNET_SYSERR on error
689 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
690 *cfg, const char *section,
692 unsigned long long *number)
694 struct ConfigEntry *e;
696 e = findEntry (cfg, section, option);
698 return GNUNET_SYSERR;
699 if (1 != SSCANF (e->val, "%llu", number))
700 return GNUNET_SYSERR;
706 * Get a configuration value that should be a relative time.
708 * @param cfg configuration to inspect
709 * @param section section of interest
710 * @param option option of interest
711 * @param time set to the time value stored in the configuration
712 * @return GNUNET_OK on success, GNUNET_SYSERR on error
715 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
716 *cfg, const char *section,
718 struct GNUNET_TIME_Relative *time)
720 struct ConfigEntry *e;
722 e = findEntry (cfg, section, option);
724 return GNUNET_SYSERR;
726 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
731 * Get a configuration value that should be a size in bytes.
733 * @param cfg configuration to inspect
734 * @param section section of interest
735 * @param option option of interest
736 * @param size set to the size in bytes as stored in the configuration
737 * @return GNUNET_OK on success, GNUNET_SYSERR on error
740 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
741 *cfg, const char *section,
743 unsigned long long *size)
745 struct ConfigEntry *e;
747 e = findEntry (cfg, section, option);
749 return GNUNET_SYSERR;
750 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
755 * Get a configuration value that should be a string.
757 * @param cfg configuration to inspect
758 * @param section section of interest
759 * @param option option of interest
760 * @param value will be set to a freshly allocated configuration
761 * value, or NULL if option is not specified
762 * @return GNUNET_OK on success, GNUNET_SYSERR on error
765 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
766 *cfg, const char *section,
767 const char *option, char **value)
769 struct ConfigEntry *e;
771 e = findEntry (cfg, section, option);
772 if ((e == NULL) || (e->val == NULL))
775 return GNUNET_SYSERR;
777 *value = GNUNET_strdup (e->val);
783 * Get a configuration value that should be in a set of
786 * @param cfg configuration to inspect
787 * @param section section of interest
788 * @param option option of interest
789 * @param choices NULL-terminated list of legal values
790 * @param value will be set to an entry in the legal list,
791 * or NULL if option is not specified and no default given
792 * @return GNUNET_OK on success, GNUNET_SYSERR on error
795 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
796 *cfg, const char *section,
797 const char *option, const char **choices,
800 struct ConfigEntry *e;
803 e = findEntry (cfg, section, option);
805 return GNUNET_SYSERR;
807 while (choices[i] != NULL)
809 if (0 == strcasecmp (choices[i], e->val))
813 if (choices[i] == NULL)
815 LOG (GNUNET_ERROR_TYPE_ERROR,
816 _("Configuration value '%s' for '%s'"
817 " in section '%s' is not in set of legal choices\n"), e->val, option,
819 return GNUNET_SYSERR;
827 * Test if we have a value for a particular option
828 * @param cfg configuration to inspect
829 * @param section section of interest
830 * @param option option of interest
831 * @return GNUNET_YES if so, GNUNET_NO if not.
834 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
835 const char *section, const char *option)
837 struct ConfigEntry *e;
839 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
846 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
847 * where either in the "PATHS" section or the environtment
848 * "FOO" is set to "DIRECTORY".
850 * @param cfg configuration to use for path expansion
851 * @param orig string to $-expand (will be freed!)
852 * @return $-expanded string
855 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
867 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
879 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
881 if (NULL == (env = getenv (&orig[1])))
883 orig[i] = DIR_SEPARATOR;
886 prefix = GNUNET_strdup (env);
888 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
889 strcpy (result, prefix);
890 if ((strlen (prefix) == 0) ||
891 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
892 strcat (result, DIR_SEPARATOR_STR);
893 strcat (result, post);
894 GNUNET_free (prefix);
901 * Get a configuration value that should be a string.
903 * @param cfg configuration to inspect
904 * @param section section of interest
905 * @param option option of interest
906 * @param value will be set to a freshly allocated configuration
907 * value, or NULL if option is not specified
908 * @return GNUNET_OK on success, GNUNET_SYSERR on error
911 GNUNET_CONFIGURATION_get_value_filename (const struct
912 GNUNET_CONFIGURATION_Handle *cfg,
914 const char *option, char **value)
919 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
922 return GNUNET_SYSERR;
924 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
925 *value = GNUNET_STRINGS_filename_expand (tmp);
928 return GNUNET_SYSERR;
934 * Get a configuration value that should be in a set of
935 * "GNUNET_YES" or "GNUNET_NO".
937 * @param cfg configuration to inspect
938 * @param section section of interest
939 * @param option option of interest
940 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
943 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
944 *cfg, const char *section,
947 static const char *yesno[] = { "YES", "NO", NULL };
952 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
953 if (ret == GNUNET_SYSERR)
962 * Iterate over the set of filenames stored in a configuration value.
964 * @param cfg configuration to inspect
965 * @param section section of interest
966 * @param option option of interest
967 * @param cb function to call on each filename
968 * @param cb_cls closure for cb
969 * @return number of filenames iterated over, -1 on error
972 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
973 GNUNET_CONFIGURATION_Handle *cfg,
976 GNUNET_FileNameCallback cb,
986 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
988 GNUNET_assert (list != NULL);
993 while (pos[0] == ' ')
995 if (strlen (pos) == 0)
998 while ((end[0] != ' ') && (end[0] != '\0'))
1006 memmove (end, &end[1], strlen (&end[1]) + 1);
1008 /* illegal, but just keep it */
1011 /* illegal, but just ignore that there was a '/' */
1019 if (strlen (pos) > 0)
1022 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1024 ret = GNUNET_SYSERR;
1040 * @param value FIXME
1044 escape_name (const char *value)
1050 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1051 memset (escaped, 0, strlen (value) * 2 + 1);
1054 while (rpos[0] != '\0')
1077 * @param cls string we compare with (const char*)
1078 * @param fn filename we are currently looking at
1079 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1082 test_match (void *cls, const char *fn)
1084 const char *of = cls;
1086 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1091 * Append a filename to a configuration value that
1092 * represents a list of filenames
1094 * @param cfg configuration to update
1095 * @param section section of interest
1096 * @param option option of interest
1097 * @param value filename to append
1098 * @return GNUNET_OK on success,
1099 * GNUNET_NO if the filename already in the list
1100 * GNUNET_SYSERR on error
1103 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1104 *cfg, const char *section,
1112 if (GNUNET_SYSERR ==
1113 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1116 return GNUNET_NO; /* already exists */
1118 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1119 old = GNUNET_strdup ("");
1120 escaped = escape_name (value);
1121 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1123 if (strlen (old) > 0)
1125 strcat (nw, escaped);
1126 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1129 GNUNET_free (escaped);
1135 * Remove a filename from a configuration value that
1136 * represents a list of filenames
1138 * @param cfg configuration to update
1139 * @param section section of interest
1140 * @param option option of interest
1141 * @param value filename to remove
1142 * @return GNUNET_OK on success,
1143 * GNUNET_NO if the filename is not in the list,
1144 * GNUNET_SYSERR on error
1147 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1148 *cfg, const char *section,
1159 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1161 match = escape_name (value);
1165 while (pos[0] == ' ')
1167 if (strlen (pos) == 0)
1170 while ((end[0] != ' ') && (end[0] != '\0'))
1181 /* illegal, but just keep it */
1184 /* illegal, but just ignore that there was a '/' */
1192 if (0 == strcmp (pos, match))
1195 memmove (pos, &end[1], strlen (&end[1]) + 1);
1203 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1205 GNUNET_free (match);
1214 GNUNET_free (match);
1220 * Wrapper around GNUNET_CONFIGURATION_parse.
1222 * @param cls the cfg
1223 * @param filename file to parse
1224 * @return GNUNET_OK on success
1227 parse_configuration_file (void *cls, const char *filename)
1229 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1233 /* Examine file extension */
1234 ext = strrchr (filename, '.');
1235 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1241 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1247 * Load default configuration. This function will parse the
1248 * defaults from the given defaults_d directory.
1250 * @param cfg configuration to update
1251 * @param defaults_d directory with the defaults
1252 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1255 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1256 const char *defaults_d)
1258 if (GNUNET_SYSERR ==
1259 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1260 return GNUNET_SYSERR; /* no configuration at all found */
1266 * Load configuration (starts with defaults, then loads
1267 * system-specific configuration).
1269 * @param cfg configuration to update
1270 * @param filename name of the configuration file, NULL to load defaults
1271 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1274 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1275 const char *filename)
1280 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1282 return GNUNET_SYSERR;
1284 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1285 GNUNET_free (ipath);
1286 if (GNUNET_SYSERR ==
1287 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1289 GNUNET_free (baseconfig);
1290 return GNUNET_SYSERR; /* no configuration at all found */
1292 GNUNET_free (baseconfig);
1293 if ((filename != NULL) &&
1294 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1296 /* specified configuration not found */
1297 return GNUNET_SYSERR;
1300 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1302 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1305 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1307 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1308 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1314 /* end of configuration.c */