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 * 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_ERROR 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 */
237 GNUNET_free_non_null (line_orig);
243 LOG (GNUNET_ERROR_TYPE_DEBUG,
244 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
246 GNUNET_free_non_null (line_orig);
251 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
254 line[line_size - 1] = '\0';
256 GNUNET_free (section);
257 section = GNUNET_strdup (value);
258 LOG (GNUNET_ERROR_TYPE_DEBUG,
259 "Config section `%s'\n",
263 if (NULL != (eq = strchr (line, '=')))
266 tag = GNUNET_strndup (line, eq - line);
267 /* remove tailing whitespace */
268 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
271 /* Strip whitespace */
273 while (isspace ((unsigned char) value[0]))
275 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
280 if ( ('"' == value[0]) &&
281 ('"' == value[strlen (value) - 1]) )
283 value[strlen (value) - 1] = '\0';
286 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s=\"%s\"\n", tag, value);
287 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
292 LOG (GNUNET_ERROR_TYPE_WARNING,
293 _("Syntax error while deserializing in line %u\n"),
296 GNUNET_free_non_null (line_orig);
299 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n", tag);
300 GNUNET_free_non_null (line_orig);
301 GNUNET_free (section);
302 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
308 * Parse a configuration file, add all of the options in the
309 * file to the configuration environment.
311 * @param cfg configuration to update
312 * @param filename name of the configuration file
313 * @return GNUNET_OK on success, GNUNET_SYSERR on error
316 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
317 const char *filename)
326 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", filename);
327 fn = GNUNET_STRINGS_filename_expand (filename);
328 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config file name expanded to `%s'\n", fn);
330 return GNUNET_SYSERR;
331 dirty = cfg->dirty; /* back up value! */
333 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
335 LOG (GNUNET_ERROR_TYPE_WARNING,
336 "Error while determining the file size of %s\n", fn);
338 return GNUNET_SYSERR;
342 GNUNET_break (0); /* File size is more than the heap size */
344 return GNUNET_SYSERR;
347 mem = GNUNET_malloc (fs);
348 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
350 LOG (GNUNET_ERROR_TYPE_WARNING,
351 "Error while reading file %s\n", fn);
354 return GNUNET_SYSERR;
356 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
358 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
360 /* restore dirty flag - anything we set in the meantime
368 * Test if there are configuration options that were
369 * changed since the last save.
371 * @param cfg configuration to inspect
372 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
375 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
382 * Serializes the given configuration.
384 * @param cfg configuration to serialize
385 * @param size will be set to the size of the serialized memory block
386 * @return the memory block where the serialized configuration is
387 * present. This memory should be freed by the caller
390 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
393 struct ConfigSection *sec;
394 struct ConfigEntry *ent;
404 /* Pass1 : calculate the buffer size required */
409 /* For each section we need to add 3 charaters: {'[',']','\n'} */
410 m_size += strlen (sec->name) + 3;
414 if (NULL != ent->val)
416 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
418 while (NULL != (pos = strstr (pos, "\n")))
423 /* For each key = value pair we need to add 4 characters (2
424 spaces and 1 equal-to character and 1 new line) */
425 m_size += strlen (ent->key) + strlen (ent->val) + 4;
429 /* 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);
449 if (NULL != ent->val)
451 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
452 strcpy (val, ent->val);
453 while (NULL != (pos = strstr (val, "\n")))
455 memmove (&pos[2], &pos[1], strlen (&pos[1]));
459 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
461 memcpy (mem + c_size, cbuf, len);
467 memcpy (mem + c_size, "\n", 1);
471 GNUNET_assert (c_size == m_size);
478 * Write configuration file.
480 * @param cfg configuration to write
481 * @param filename where to write the configuration
482 * @return GNUNET_OK on success, GNUNET_SYSERR on error
485 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
486 const char *filename)
492 fn = GNUNET_STRINGS_filename_expand (filename);
494 return GNUNET_SYSERR;
495 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
498 return GNUNET_SYSERR;
500 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
501 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
502 GNUNET_DISK_PERM_USER_READ
503 | GNUNET_DISK_PERM_USER_WRITE
504 | GNUNET_DISK_PERM_GROUP_READ
505 | GNUNET_DISK_PERM_GROUP_WRITE))
508 GNUNET_free (cfg_buf);
509 LOG (GNUNET_ERROR_TYPE_WARNING,
510 "Writing configration to file: %s failed\n", filename);
511 cfg->dirty = GNUNET_SYSERR; /* last write failed */
512 return GNUNET_SYSERR;
515 GNUNET_free (cfg_buf);
516 cfg->dirty = GNUNET_NO; /* last write succeeded */
522 * Iterate over all options in the configuration.
524 * @param cfg configuration to inspect
525 * @param iter function to call on each option
526 * @param iter_cls closure for iter
529 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
530 GNUNET_CONFIGURATION_Iterator iter,
533 struct ConfigSection *spos;
534 struct ConfigEntry *epos;
536 spos = cfg->sections;
539 epos = spos->entries;
542 iter (iter_cls, spos->name, epos->key, epos->val);
551 * Iterate over values of a section in the configuration.
553 * @param cfg configuration to inspect
554 * @param section the section
555 * @param iter function to call on each option
556 * @param iter_cls closure for iter
559 GNUNET_CONFIGURATION_iterate_section_values (const struct
560 GNUNET_CONFIGURATION_Handle *cfg,
562 GNUNET_CONFIGURATION_Iterator iter,
565 struct ConfigSection *spos;
566 struct ConfigEntry *epos;
568 spos = cfg->sections;
569 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
575 epos = spos->entries;
578 iter (iter_cls, spos->name, epos->key, epos->val);
585 * Iterate over all sections in the configuration.
587 * @param cfg configuration to inspect
588 * @param iter function to call on each section
589 * @param iter_cls closure for iter
592 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
594 GNUNET_CONFIGURATION_Section_Iterator
595 iter, void *iter_cls)
597 struct ConfigSection *spos;
598 struct ConfigSection *next;
600 next = cfg->sections;
605 iter (iter_cls, spos->name);
610 * Remove the given section and all options in it.
612 * @param cfg configuration to inspect
613 * @param section name of the section to remove
616 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
619 struct ConfigSection *spos;
620 struct ConfigSection *prev;
621 struct ConfigEntry *ent;
624 spos = cfg->sections;
627 if (0 == strcasecmp (section, spos->name))
630 cfg->sections = spos->next;
632 prev->next = spos->next;
633 while (NULL != (ent = spos->entries))
635 spos->entries = ent->next;
636 GNUNET_free (ent->key);
637 GNUNET_free_non_null (ent->val);
639 cfg->dirty = GNUNET_YES;
641 GNUNET_free (spos->name);
652 * Copy a configuration value to the given target configuration.
653 * Overwrites existing entries.
655 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
656 * @param section section for the value
657 * @param option option name of the value
658 * @param value value to copy
661 copy_entry (void *cls, const char *section, const char *option,
664 struct GNUNET_CONFIGURATION_Handle *dst = cls;
666 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
671 * Duplicate an existing configuration object.
673 * @param cfg configuration to duplicate
674 * @return duplicate configuration
676 struct GNUNET_CONFIGURATION_Handle *
677 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
679 struct GNUNET_CONFIGURATION_Handle *ret;
681 ret = GNUNET_CONFIGURATION_create ();
682 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
691 * @param section FIXME
692 * @return matching entry, NULL if not found
694 static struct ConfigSection *
695 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
697 struct ConfigSection *pos;
700 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
707 * Find an entry from a configuration.
709 * @param cfg handle to the configuration
710 * @param section section the option is in
711 * @param key the option
712 * @return matching entry, NULL if not found
714 static struct ConfigEntry *
715 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
718 struct ConfigSection *sec;
719 struct ConfigEntry *pos;
721 sec = findSection (cfg, section);
725 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
732 * A callback function, compares entries from two configurations
733 * (default against a new configuration) and write the diffs in a
734 * diff-configuration object (the callback object).
736 * @param cls the diff configuration (struct DiffHandle*)
737 * @param section section for the value (of the default conf.)
738 * @param option option name of the value (of the default conf.)
739 * @param value value to copy (of the default conf.)
742 compareEntries (void *cls, const char *section, const char *option,
745 struct DiffHandle *dh = cls;
746 struct ConfigEntry *entNew;
748 entNew = findEntry (dh->cfgDefault, section, option);
749 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
751 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
756 * Write only configuration entries that have been changed to configuration file
757 * @param cfgDefault default configuration
758 * @param cfgNew new configuration
759 * @param filename where to write the configuration diff between default and new
760 * @return GNUNET_OK on success, GNUNET_SYSERR on error
763 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
765 const struct GNUNET_CONFIGURATION_Handle
766 *cfgNew, const char *filename)
769 struct DiffHandle diffHandle;
771 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
772 diffHandle.cfgDefault = cfgDefault;
773 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
774 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
775 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
781 * Set a configuration value that should be a string.
783 * @param cfg configuration to update
784 * @param section section of interest
785 * @param option option of interest
786 * @param value value to set
789 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
790 const char *section, const char *option,
793 struct ConfigSection *sec;
794 struct ConfigEntry *e;
797 e = findEntry (cfg, section, option);
800 nv = GNUNET_strdup (value);
801 GNUNET_free_non_null (e->val);
805 sec = findSection (cfg, section);
808 sec = GNUNET_malloc (sizeof (struct ConfigSection));
809 sec->name = GNUNET_strdup (section);
810 sec->next = cfg->sections;
813 e = GNUNET_malloc (sizeof (struct ConfigEntry));
814 e->key = GNUNET_strdup (option);
815 e->val = GNUNET_strdup (value);
816 e->next = sec->entries;
822 * Set a configuration value that should be a number.
824 * @param cfg configuration to update
825 * @param section section of interest
826 * @param option option of interest
827 * @param number value to set
830 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
831 const char *section, const char *option,
832 unsigned long long number)
836 GNUNET_snprintf (s, 64, "%llu", number);
837 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
842 * Get a configuration value that should be a number.
844 * @param cfg configuration to inspect
845 * @param section section of interest
846 * @param option option of interest
847 * @param number where to store the numeric value of the option
848 * @return GNUNET_OK on success, GNUNET_SYSERR on error
851 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
852 *cfg, const char *section,
854 unsigned long long *number)
856 struct ConfigEntry *e;
858 e = findEntry (cfg, section, option);
860 return GNUNET_SYSERR;
861 if (1 != SSCANF (e->val, "%llu", number))
862 return GNUNET_SYSERR;
868 * Get a configuration value that should be a relative time.
870 * @param cfg configuration to inspect
871 * @param section section of interest
872 * @param option option of interest
873 * @param time set to the time value stored in the configuration
874 * @return GNUNET_OK on success, GNUNET_SYSERR on error
877 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
878 *cfg, const char *section,
880 struct GNUNET_TIME_Relative *time)
882 struct ConfigEntry *e;
884 e = findEntry (cfg, section, option);
886 return GNUNET_SYSERR;
888 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
893 * Get a configuration value that should be a size in bytes.
895 * @param cfg configuration to inspect
896 * @param section section of interest
897 * @param option option of interest
898 * @param size set to the size in bytes as stored in the configuration
899 * @return GNUNET_OK on success, GNUNET_SYSERR on error
902 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
903 *cfg, const char *section,
905 unsigned long long *size)
907 struct ConfigEntry *e;
909 e = findEntry (cfg, section, option);
911 return GNUNET_SYSERR;
912 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
917 * Get a configuration value that should be a string.
919 * @param cfg configuration to inspect
920 * @param section section of interest
921 * @param option option of interest
922 * @param value will be set to a freshly allocated configuration
923 * value, or NULL if option is not specified
924 * @return GNUNET_OK on success, GNUNET_SYSERR on error
927 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
928 *cfg, const char *section,
929 const char *option, char **value)
931 struct ConfigEntry *e;
933 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section);
934 e = findEntry (cfg, section, option);
935 if ((e == NULL) || (e->val == NULL))
937 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve the string\n");
939 return GNUNET_SYSERR;
941 *value = GNUNET_strdup (e->val);
942 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved string `%s'\n", e->val);
948 * Get a configuration value that should be in a set of
951 * @param cfg configuration to inspect
952 * @param section section of interest
953 * @param option option of interest
954 * @param choices NULL-terminated list of legal values
955 * @param value will be set to an entry in the legal list,
956 * or NULL if option is not specified and no default given
957 * @return GNUNET_OK on success, GNUNET_SYSERR on error
960 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
961 *cfg, const char *section,
962 const char *option, const char **choices,
965 struct ConfigEntry *e;
968 e = findEntry (cfg, section, option);
970 return GNUNET_SYSERR;
972 while (choices[i] != NULL)
974 if (0 == strcasecmp (choices[i], e->val))
978 if (choices[i] == NULL)
980 LOG (GNUNET_ERROR_TYPE_ERROR,
981 _("Configuration value '%s' for '%s'"
982 " in section '%s' is not in set of legal choices\n"), e->val, option,
984 return GNUNET_SYSERR;
992 * Test if we have a value for a particular option
993 * @param cfg configuration to inspect
994 * @param section section of interest
995 * @param option option of interest
996 * @return GNUNET_YES if so, GNUNET_NO if not.
999 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1000 const char *section, const char *option)
1002 struct ConfigEntry *e;
1004 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
1011 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1012 * where either in the "PATHS" section or the environtment
1013 * "FOO" is set to "DIRECTORY".
1015 * @param cfg configuration to use for path expansion
1016 * @param orig string to $-expand (will be freed!)
1017 * @return $-expanded string
1020 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
1029 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1033 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1037 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
1039 if (orig[i] == '\0')
1046 post = &orig[i + 1];
1048 LOG (GNUNET_ERROR_TYPE_DEBUG, "Split into `%s' and `%s'\n", orig, post);
1050 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename for `%s' is not in PATHS config section\n", &orig[1]);
1053 if (NULL == (env = getenv (&orig[1])))
1055 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' is not an environment variable\n", &orig[1]);
1056 orig[i] = DIR_SEPARATOR;
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s' (returning orig)\n", orig);
1060 prefix = GNUNET_strdup (env);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, "Prefix is `%s'\n", prefix);
1063 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1064 strcpy (result, prefix);
1065 if ((strlen (prefix) == 0) ||
1066 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
1067 strcat (result, DIR_SEPARATOR_STR);
1068 strcat (result, post);
1069 GNUNET_free (prefix);
1071 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s'\n", result);
1077 * Get a configuration value that should be a string.
1079 * @param cfg configuration to inspect
1080 * @param section section of interest
1081 * @param option option of interest
1082 * @param value will be set to a freshly allocated configuration
1083 * value, or NULL if option is not specified
1084 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1087 GNUNET_CONFIGURATION_get_value_filename (const struct
1088 GNUNET_CONFIGURATION_Handle *cfg,
1089 const char *section,
1090 const char *option, char **value)
1094 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve filename `%s' in section `%s'\n", option, section);
1096 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1100 return GNUNET_SYSERR;
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1103 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1105 *value = GNUNET_STRINGS_filename_expand (tmp);
1107 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1109 return GNUNET_SYSERR;
1115 * Get a configuration value that should be in a set of
1116 * "GNUNET_YES" or "GNUNET_NO".
1118 * @param cfg configuration to inspect
1119 * @param section section of interest
1120 * @param option option of interest
1121 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1124 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1125 *cfg, const char *section,
1128 static const char *yesno[] = { "YES", "NO", NULL };
1133 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1134 if (ret == GNUNET_SYSERR)
1136 if (val == yesno[0])
1143 * Iterate over the set of filenames stored in a configuration value.
1145 * @param cfg configuration to inspect
1146 * @param section section of interest
1147 * @param option option of interest
1148 * @param cb function to call on each filename
1149 * @param cb_cls closure for cb
1150 * @return number of filenames iterated over, -1 on error
1153 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1154 GNUNET_CONFIGURATION_Handle *cfg,
1155 const char *section,
1157 GNUNET_FileNameCallback cb,
1167 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1169 GNUNET_assert (list != NULL);
1174 while (pos[0] == ' ')
1176 if (strlen (pos) == 0)
1179 while ((end[0] != ' ') && (end[0] != '\0'))
1187 memmove (end, &end[1], strlen (&end[1]) + 1);
1189 /* illegal, but just keep it */
1192 /* illegal, but just ignore that there was a '/' */
1200 if (strlen (pos) > 0)
1203 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1205 ret = GNUNET_SYSERR;
1221 * @param value FIXME
1225 escape_name (const char *value)
1231 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1232 memset (escaped, 0, strlen (value) * 2 + 1);
1235 while (rpos[0] != '\0')
1258 * @param cls string we compare with (const char*)
1259 * @param fn filename we are currently looking at
1260 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1263 test_match (void *cls, const char *fn)
1265 const char *of = cls;
1267 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1272 * Append a filename to a configuration value that
1273 * represents a list of filenames
1275 * @param cfg configuration to update
1276 * @param section section of interest
1277 * @param option option of interest
1278 * @param value filename to append
1279 * @return GNUNET_OK on success,
1280 * GNUNET_NO if the filename already in the list
1281 * GNUNET_SYSERR on error
1284 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1285 *cfg, const char *section,
1293 if (GNUNET_SYSERR ==
1294 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1297 return GNUNET_NO; /* already exists */
1299 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1300 old = GNUNET_strdup ("");
1301 escaped = escape_name (value);
1302 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1304 if (strlen (old) > 0)
1306 strcat (nw, escaped);
1307 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1310 GNUNET_free (escaped);
1316 * Remove a filename from a configuration value that
1317 * represents a list of filenames
1319 * @param cfg configuration to update
1320 * @param section section of interest
1321 * @param option option of interest
1322 * @param value filename to remove
1323 * @return GNUNET_OK on success,
1324 * GNUNET_NO if the filename is not in the list,
1325 * GNUNET_SYSERR on error
1328 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1329 *cfg, const char *section,
1340 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1342 match = escape_name (value);
1346 while (pos[0] == ' ')
1348 if (strlen (pos) == 0)
1351 while ((end[0] != ' ') && (end[0] != '\0'))
1362 /* illegal, but just keep it */
1365 /* illegal, but just ignore that there was a '/' */
1373 if (0 == strcmp (pos, match))
1376 memmove (pos, &end[1], strlen (&end[1]) + 1);
1384 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1386 GNUNET_free (match);
1395 GNUNET_free (match);
1401 * Wrapper around GNUNET_CONFIGURATION_parse.
1403 * @param cls the cfg
1404 * @param filename file to parse
1405 * @return GNUNET_OK on success
1408 parse_configuration_file (void *cls, const char *filename)
1410 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1414 /* Examine file extension */
1415 ext = strrchr (filename, '.');
1416 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1422 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1428 * Load default configuration. This function will parse the
1429 * defaults from the given defaults_d directory.
1431 * @param cfg configuration to update
1432 * @param defaults_d directory with the defaults
1433 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1436 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1437 const char *defaults_d)
1439 if (GNUNET_SYSERR ==
1440 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1441 return GNUNET_SYSERR; /* no configuration at all found */
1447 * Load configuration (starts with defaults, then loads
1448 * system-specific configuration).
1450 * @param cfg configuration to update
1451 * @param filename name of the configuration file, NULL to load defaults
1452 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1455 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1456 const char *filename)
1461 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1463 return GNUNET_SYSERR;
1465 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1466 GNUNET_free (ipath);
1467 if (GNUNET_SYSERR ==
1468 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1470 GNUNET_free (baseconfig);
1471 return GNUNET_SYSERR; /* no configuration at all found */
1473 GNUNET_free (baseconfig);
1474 if ((filename != NULL) &&
1475 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1477 /* specified configuration not found */
1478 return GNUNET_SYSERR;
1481 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1483 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1486 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1488 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1489 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1495 /* end of configuration.c */