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);
300 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n");
301 GNUNET_free_non_null (line_orig);
302 GNUNET_free (section);
303 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
309 * Parse a configuration file, add all of the options in the
310 * file to the configuration environment.
312 * @param cfg configuration to update
313 * @param filename name of the configuration file
314 * @return GNUNET_OK on success, GNUNET_SYSERR on error
317 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
318 const char *filename)
327 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", filename);
328 fn = GNUNET_STRINGS_filename_expand (filename);
329 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config file name expanded to `%s'\n", fn);
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", fn);
355 return GNUNET_SYSERR;
357 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
359 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
361 /* restore dirty flag - anything we set in the meantime
369 * Test if there are configuration options that were
370 * changed since the last save.
372 * @param cfg configuration to inspect
373 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
376 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
383 * Serializes the given configuration.
385 * @param cfg configuration to serialize
386 * @param size will be set to the size of the serialized memory block
387 * @return the memory block where the serialized configuration is
388 * present. This memory should be freed by the caller
391 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
394 struct ConfigSection *sec;
395 struct ConfigEntry *ent;
405 /* Pass1 : calculate the buffer size required */
410 /* For each section we need to add 3 charaters: {'[',']','\n'} */
411 m_size += strlen (sec->name) + 3;
415 if (NULL != ent->val)
417 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
419 while (NULL != (pos = strstr (pos, "\n")))
424 /* For each key = value pair we need to add 4 characters (2
425 spaces and 1 equal-to character and 1 new line) */
426 m_size += strlen (ent->key) + strlen (ent->val) + 4;
430 /* A new line after section end */
435 /* Pass2: Allocate memory and write the configuration to it */
436 mem = GNUNET_malloc (m_size);
442 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
443 GNUNET_assert (0 < len);
444 memcpy (mem + c_size, cbuf, len);
450 if (NULL != ent->val)
452 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
453 strcpy (val, ent->val);
454 while (NULL != (pos = strstr (val, "\n")))
456 memmove (&pos[2], &pos[1], strlen (&pos[1]));
460 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
462 memcpy (mem + c_size, cbuf, len);
468 memcpy (mem + c_size, "\n", 1);
472 GNUNET_assert (c_size == m_size);
479 * Write configuration file.
481 * @param cfg configuration to write
482 * @param filename where to write the configuration
483 * @return GNUNET_OK on success, GNUNET_SYSERR on error
486 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
487 const char *filename)
493 fn = GNUNET_STRINGS_filename_expand (filename);
495 return GNUNET_SYSERR;
496 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
499 return GNUNET_SYSERR;
501 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
502 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
503 GNUNET_DISK_PERM_USER_READ
504 | GNUNET_DISK_PERM_USER_WRITE
505 | GNUNET_DISK_PERM_GROUP_READ
506 | GNUNET_DISK_PERM_GROUP_WRITE))
509 GNUNET_free (cfg_buf);
510 LOG (GNUNET_ERROR_TYPE_WARNING,
511 "Writing configration to file: %s failed\n", filename);
512 cfg->dirty = GNUNET_SYSERR; /* last write failed */
513 return GNUNET_SYSERR;
516 GNUNET_free (cfg_buf);
517 cfg->dirty = GNUNET_NO; /* last write succeeded */
523 * Iterate over all options in the configuration.
525 * @param cfg configuration to inspect
526 * @param iter function to call on each option
527 * @param iter_cls closure for iter
530 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
531 GNUNET_CONFIGURATION_Iterator iter,
534 struct ConfigSection *spos;
535 struct ConfigEntry *epos;
537 spos = cfg->sections;
540 epos = spos->entries;
543 iter (iter_cls, spos->name, epos->key, epos->val);
552 * Iterate over values of a section in the configuration.
554 * @param cfg configuration to inspect
555 * @param section the section
556 * @param iter function to call on each option
557 * @param iter_cls closure for iter
560 GNUNET_CONFIGURATION_iterate_section_values (const struct
561 GNUNET_CONFIGURATION_Handle *cfg,
563 GNUNET_CONFIGURATION_Iterator iter,
566 struct ConfigSection *spos;
567 struct ConfigEntry *epos;
569 spos = cfg->sections;
570 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
576 epos = spos->entries;
579 iter (iter_cls, spos->name, epos->key, epos->val);
586 * Iterate over all sections in the configuration.
588 * @param cfg configuration to inspect
589 * @param iter function to call on each section
590 * @param iter_cls closure for iter
593 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
595 GNUNET_CONFIGURATION_Section_Iterator
596 iter, void *iter_cls)
598 struct ConfigSection *spos;
599 struct ConfigSection *next;
601 next = cfg->sections;
606 iter (iter_cls, spos->name);
611 * Remove the given section and all options in it.
613 * @param cfg configuration to inspect
614 * @param section name of the section to remove
617 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
620 struct ConfigSection *spos;
621 struct ConfigSection *prev;
622 struct ConfigEntry *ent;
625 spos = cfg->sections;
628 if (0 == strcasecmp (section, spos->name))
631 cfg->sections = spos->next;
633 prev->next = spos->next;
634 while (NULL != (ent = spos->entries))
636 spos->entries = ent->next;
637 GNUNET_free (ent->key);
638 GNUNET_free_non_null (ent->val);
640 cfg->dirty = GNUNET_YES;
642 GNUNET_free (spos->name);
653 * Copy a configuration value to the given target configuration.
654 * Overwrites existing entries.
656 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
657 * @param section section for the value
658 * @param option option name of the value
659 * @param value value to copy
662 copy_entry (void *cls, const char *section, const char *option,
665 struct GNUNET_CONFIGURATION_Handle *dst = cls;
667 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
672 * Duplicate an existing configuration object.
674 * @param cfg configuration to duplicate
675 * @return duplicate configuration
677 struct GNUNET_CONFIGURATION_Handle *
678 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
680 struct GNUNET_CONFIGURATION_Handle *ret;
682 ret = GNUNET_CONFIGURATION_create ();
683 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
692 * @param section FIXME
693 * @return matching entry, NULL if not found
695 static struct ConfigSection *
696 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
698 struct ConfigSection *pos;
701 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
708 * Find an entry from a configuration.
710 * @param cfg handle to the configuration
711 * @param section section the option is in
712 * @param key the option
713 * @return matching entry, NULL if not found
715 static struct ConfigEntry *
716 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
719 struct ConfigSection *sec;
720 struct ConfigEntry *pos;
722 sec = findSection (cfg, section);
726 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
733 * A callback function, compares entries from two configurations
734 * (default against a new configuration) and write the diffs in a
735 * diff-configuration object (the callback object).
737 * @param cls the diff configuration (struct DiffHandle*)
738 * @param section section for the value (of the default conf.)
739 * @param option option name of the value (of the default conf.)
740 * @param value value to copy (of the default conf.)
743 compareEntries (void *cls, const char *section, const char *option,
746 struct DiffHandle *dh = cls;
747 struct ConfigEntry *entNew;
749 entNew = findEntry (dh->cfgDefault, section, option);
750 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
752 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
757 * Write only configuration entries that have been changed to configuration file
758 * @param cfgDefault default configuration
759 * @param cfgNew new configuration
760 * @param filename where to write the configuration diff between default and new
761 * @return GNUNET_OK on success, GNUNET_SYSERR on error
764 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
766 const struct GNUNET_CONFIGURATION_Handle
767 *cfgNew, const char *filename)
770 struct DiffHandle diffHandle;
772 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
773 diffHandle.cfgDefault = cfgDefault;
774 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
775 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
776 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
782 * Set a configuration value that should be a string.
784 * @param cfg configuration to update
785 * @param section section of interest
786 * @param option option of interest
787 * @param value value to set
790 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
791 const char *section, const char *option,
794 struct ConfigSection *sec;
795 struct ConfigEntry *e;
798 e = findEntry (cfg, section, option);
801 nv = GNUNET_strdup (value);
802 GNUNET_free_non_null (e->val);
806 sec = findSection (cfg, section);
809 sec = GNUNET_malloc (sizeof (struct ConfigSection));
810 sec->name = GNUNET_strdup (section);
811 sec->next = cfg->sections;
814 e = GNUNET_malloc (sizeof (struct ConfigEntry));
815 e->key = GNUNET_strdup (option);
816 e->val = GNUNET_strdup (value);
817 e->next = sec->entries;
823 * Set a configuration value that should be a number.
825 * @param cfg configuration to update
826 * @param section section of interest
827 * @param option option of interest
828 * @param number value to set
831 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
832 const char *section, const char *option,
833 unsigned long long number)
837 GNUNET_snprintf (s, 64, "%llu", number);
838 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
843 * Get a configuration value that should be a number.
845 * @param cfg configuration to inspect
846 * @param section section of interest
847 * @param option option of interest
848 * @param number where to store the numeric value of the option
849 * @return GNUNET_OK on success, GNUNET_SYSERR on error
852 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
853 *cfg, const char *section,
855 unsigned long long *number)
857 struct ConfigEntry *e;
859 e = findEntry (cfg, section, option);
861 return GNUNET_SYSERR;
862 if (1 != SSCANF (e->val, "%llu", number))
863 return GNUNET_SYSERR;
869 * Get a configuration value that should be a relative time.
871 * @param cfg configuration to inspect
872 * @param section section of interest
873 * @param option option of interest
874 * @param time set to the time value stored in the configuration
875 * @return GNUNET_OK on success, GNUNET_SYSERR on error
878 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
879 *cfg, const char *section,
881 struct GNUNET_TIME_Relative *time)
883 struct ConfigEntry *e;
885 e = findEntry (cfg, section, option);
887 return GNUNET_SYSERR;
889 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
894 * Get a configuration value that should be a size in bytes.
896 * @param cfg configuration to inspect
897 * @param section section of interest
898 * @param option option of interest
899 * @param size set to the size in bytes as stored in the configuration
900 * @return GNUNET_OK on success, GNUNET_SYSERR on error
903 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
904 *cfg, const char *section,
906 unsigned long long *size)
908 struct ConfigEntry *e;
910 e = findEntry (cfg, section, option);
912 return GNUNET_SYSERR;
913 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
918 * Get a configuration value that should be a string.
920 * @param cfg configuration to inspect
921 * @param section section of interest
922 * @param option option of interest
923 * @param value will be set to a freshly allocated configuration
924 * value, or NULL if option is not specified
925 * @return GNUNET_OK on success, GNUNET_SYSERR on error
928 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
929 *cfg, const char *section,
930 const char *option, char **value)
932 struct ConfigEntry *e;
934 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section);
935 e = findEntry (cfg, section, option);
936 if ((e == NULL) || (e->val == NULL))
938 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve the string\n");
940 return GNUNET_SYSERR;
942 *value = GNUNET_strdup (e->val);
943 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved string `%s'\n", e->val);
949 * Get a configuration value that should be in a set of
952 * @param cfg configuration to inspect
953 * @param section section of interest
954 * @param option option of interest
955 * @param choices NULL-terminated list of legal values
956 * @param value will be set to an entry in the legal list,
957 * or NULL if option is not specified and no default given
958 * @return GNUNET_OK on success, GNUNET_SYSERR on error
961 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
962 *cfg, const char *section,
963 const char *option, const char **choices,
966 struct ConfigEntry *e;
969 e = findEntry (cfg, section, option);
971 return GNUNET_SYSERR;
973 while (choices[i] != NULL)
975 if (0 == strcasecmp (choices[i], e->val))
979 if (choices[i] == NULL)
981 LOG (GNUNET_ERROR_TYPE_ERROR,
982 _("Configuration value '%s' for '%s'"
983 " in section '%s' is not in set of legal choices\n"), e->val, option,
985 return GNUNET_SYSERR;
993 * Test if we have a value for a particular option
994 * @param cfg configuration to inspect
995 * @param section section of interest
996 * @param option option of interest
997 * @return GNUNET_YES if so, GNUNET_NO if not.
1000 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1001 const char *section, const char *option)
1003 struct ConfigEntry *e;
1005 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
1012 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1013 * where either in the "PATHS" section or the environtment
1014 * "FOO" is set to "DIRECTORY".
1016 * @param cfg configuration to use for path expansion
1017 * @param orig string to $-expand (will be freed!)
1018 * @return $-expanded string
1021 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
1030 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1034 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1038 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
1040 if (orig[i] == '\0')
1047 post = &orig[i + 1];
1049 LOG (GNUNET_ERROR_TYPE_DEBUG, "Split into `%s' and `%s'\n", orig, post);
1051 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
1053 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename for `%s' is not in PATHS config section\n", &orig[1]);
1054 if (NULL == (env = getenv (&orig[1])))
1056 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' is not an environment variable\n", &orig[1]);
1057 orig[i] = DIR_SEPARATOR;
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s' (returning orig)\n", orig);
1061 prefix = GNUNET_strdup (env);
1063 LOG (GNUNET_ERROR_TYPE_DEBUG, "Prefix is `%s'\n", prefix);
1064 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1065 strcpy (result, prefix);
1066 if ((strlen (prefix) == 0) ||
1067 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
1068 strcat (result, DIR_SEPARATOR_STR);
1069 strcat (result, post);
1070 GNUNET_free (prefix);
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s'\n", result);
1078 * Get a configuration value that should be a string.
1080 * @param cfg configuration to inspect
1081 * @param section section of interest
1082 * @param option option of interest
1083 * @param value will be set to a freshly allocated configuration
1084 * value, or NULL if option is not specified
1085 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1088 GNUNET_CONFIGURATION_get_value_filename (const struct
1089 GNUNET_CONFIGURATION_Handle *cfg,
1090 const char *section,
1091 const char *option, char **value)
1095 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve filename `%s' in section `%s'\n", option, section);
1097 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1099 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1101 return GNUNET_SYSERR;
1103 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1104 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1105 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1106 *value = GNUNET_STRINGS_filename_expand (tmp);
1108 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1110 return GNUNET_SYSERR;
1116 * Get a configuration value that should be in a set of
1117 * "GNUNET_YES" or "GNUNET_NO".
1119 * @param cfg configuration to inspect
1120 * @param section section of interest
1121 * @param option option of interest
1122 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1125 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1126 *cfg, const char *section,
1129 static const char *yesno[] = { "YES", "NO", NULL };
1134 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1135 if (ret == GNUNET_SYSERR)
1137 if (val == yesno[0])
1144 * Iterate over the set of filenames stored in a configuration value.
1146 * @param cfg configuration to inspect
1147 * @param section section of interest
1148 * @param option option of interest
1149 * @param cb function to call on each filename
1150 * @param cb_cls closure for cb
1151 * @return number of filenames iterated over, -1 on error
1154 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1155 GNUNET_CONFIGURATION_Handle *cfg,
1156 const char *section,
1158 GNUNET_FileNameCallback cb,
1168 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1170 GNUNET_assert (list != NULL);
1175 while (pos[0] == ' ')
1177 if (strlen (pos) == 0)
1180 while ((end[0] != ' ') && (end[0] != '\0'))
1188 memmove (end, &end[1], strlen (&end[1]) + 1);
1190 /* illegal, but just keep it */
1193 /* illegal, but just ignore that there was a '/' */
1201 if (strlen (pos) > 0)
1204 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1206 ret = GNUNET_SYSERR;
1222 * @param value FIXME
1226 escape_name (const char *value)
1232 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1233 memset (escaped, 0, strlen (value) * 2 + 1);
1236 while (rpos[0] != '\0')
1259 * @param cls string we compare with (const char*)
1260 * @param fn filename we are currently looking at
1261 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1264 test_match (void *cls, const char *fn)
1266 const char *of = cls;
1268 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1273 * Append a filename to a configuration value that
1274 * represents a list of filenames
1276 * @param cfg configuration to update
1277 * @param section section of interest
1278 * @param option option of interest
1279 * @param value filename to append
1280 * @return GNUNET_OK on success,
1281 * GNUNET_NO if the filename already in the list
1282 * GNUNET_SYSERR on error
1285 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1286 *cfg, const char *section,
1294 if (GNUNET_SYSERR ==
1295 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1298 return GNUNET_NO; /* already exists */
1300 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1301 old = GNUNET_strdup ("");
1302 escaped = escape_name (value);
1303 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1305 if (strlen (old) > 0)
1307 strcat (nw, escaped);
1308 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1311 GNUNET_free (escaped);
1317 * Remove a filename from a configuration value that
1318 * represents a list of filenames
1320 * @param cfg configuration to update
1321 * @param section section of interest
1322 * @param option option of interest
1323 * @param value filename to remove
1324 * @return GNUNET_OK on success,
1325 * GNUNET_NO if the filename is not in the list,
1326 * GNUNET_SYSERR on error
1329 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1330 *cfg, const char *section,
1341 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1343 match = escape_name (value);
1347 while (pos[0] == ' ')
1349 if (strlen (pos) == 0)
1352 while ((end[0] != ' ') && (end[0] != '\0'))
1363 /* illegal, but just keep it */
1366 /* illegal, but just ignore that there was a '/' */
1374 if (0 == strcmp (pos, match))
1377 memmove (pos, &end[1], strlen (&end[1]) + 1);
1385 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1387 GNUNET_free (match);
1396 GNUNET_free (match);
1402 * Wrapper around GNUNET_CONFIGURATION_parse.
1404 * @param cls the cfg
1405 * @param filename file to parse
1406 * @return GNUNET_OK on success
1409 parse_configuration_file (void *cls, const char *filename)
1411 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1415 /* Examine file extension */
1416 ext = strrchr (filename, '.');
1417 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1423 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1429 * Load default configuration. This function will parse the
1430 * defaults from the given defaults_d directory.
1432 * @param cfg configuration to update
1433 * @param defaults_d directory with the defaults
1434 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1437 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1438 const char *defaults_d)
1440 if (GNUNET_SYSERR ==
1441 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1442 return GNUNET_SYSERR; /* no configuration at all found */
1448 * Load configuration (starts with defaults, then loads
1449 * system-specific configuration).
1451 * @param cfg configuration to update
1452 * @param filename name of the configuration file, NULL to load defaults
1453 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1456 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1457 const char *filename)
1462 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1464 return GNUNET_SYSERR;
1466 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1467 GNUNET_free (ipath);
1468 if (GNUNET_SYSERR ==
1469 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1471 GNUNET_free (baseconfig);
1472 return GNUNET_SYSERR; /* no configuration at all found */
1474 GNUNET_free (baseconfig);
1475 if ((filename != NULL) &&
1476 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1478 /* specified configuration not found */
1479 return GNUNET_SYSERR;
1482 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1484 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1487 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1489 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1490 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1496 /* end of configuration.c */