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 */
242 LOG (GNUNET_ERROR_TYPE_DEBUG,
243 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
249 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
252 line[line_size - 1] = '\0';
254 GNUNET_free (section);
255 section = GNUNET_strdup (value);
256 LOG (GNUNET_ERROR_TYPE_DEBUG,
257 "Config section `%s'\n",
261 if (NULL != (eq = strchr (line, '=')))
264 tag = GNUNET_strndup (line, eq - line);
265 /* remove tailing whitespace */
266 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
269 /* Strip whitespace */
271 while (isspace ((unsigned char) value[0]))
273 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
278 if ( ('"' == value[0]) &&
279 ('"' == value[strlen (value) - 1]) )
281 value[strlen (value) - 1] = '\0';
284 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s=\"%s\"\n", tag, value);
285 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
290 LOG (GNUNET_ERROR_TYPE_WARNING,
291 _("Syntax error while deserializing in line %u\n"),
296 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n");
297 GNUNET_free_non_null (line_orig);
298 GNUNET_free (section);
299 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
305 * Parse a configuration file, add all of the options in the
306 * file to the configuration environment.
308 * @param cfg configuration to update
309 * @param filename name of the configuration file
310 * @return GNUNET_OK on success, GNUNET_SYSERR on error
313 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
314 const char *filename)
323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", filename);
324 fn = GNUNET_STRINGS_filename_expand (filename);
325 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config file name expanded to `%s'\n", fn);
327 return GNUNET_SYSERR;
328 dirty = cfg->dirty; /* back up value! */
330 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
332 LOG (GNUNET_ERROR_TYPE_WARNING,
333 "Error while determining the file size of %s\n", fn);
335 return GNUNET_SYSERR;
339 GNUNET_break (0); /* File size is more than the heap size */
341 return GNUNET_SYSERR;
344 mem = GNUNET_malloc (fs);
345 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
347 LOG (GNUNET_ERROR_TYPE_WARNING,
348 "Error while reading file %s\n", fn);
351 return GNUNET_SYSERR;
353 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
355 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
357 /* restore dirty flag - anything we set in the meantime
365 * Test if there are configuration options that were
366 * changed since the last save.
368 * @param cfg configuration to inspect
369 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
372 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
379 * Serializes the given configuration.
381 * @param cfg configuration to serialize
382 * @param size will be set to the size of the serialized memory block
383 * @return the memory block where the serialized configuration is
384 * present. This memory should be freed by the caller
387 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
390 struct ConfigSection *sec;
391 struct ConfigEntry *ent;
401 /* Pass1 : calculate the buffer size required */
406 /* For each section we need to add 3 charaters: {'[',']','\n'} */
407 m_size += strlen (sec->name) + 3;
411 if (NULL != ent->val)
413 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
415 while (NULL != (pos = strstr (pos, "\n")))
420 /* For each key = value pair we need to add 4 characters (2
421 spaces and 1 equal-to character and 1 new line) */
422 m_size += strlen (ent->key) + strlen (ent->val) + 4;
426 /* A new line after section end */
431 /* Pass2: Allocate memory and write the configuration to it */
432 mem = GNUNET_malloc (m_size);
438 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
439 GNUNET_assert (0 < len);
440 memcpy (mem + c_size, cbuf, len);
446 if (NULL != ent->val)
448 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
449 strcpy (val, ent->val);
450 while (NULL != (pos = strstr (val, "\n")))
452 memmove (&pos[2], &pos[1], strlen (&pos[1]));
456 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
458 memcpy (mem + c_size, cbuf, len);
464 memcpy (mem + c_size, "\n", 1);
468 GNUNET_assert (c_size == m_size);
475 * Write configuration file.
477 * @param cfg configuration to write
478 * @param filename where to write the configuration
479 * @return GNUNET_OK on success, GNUNET_SYSERR on error
482 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
483 const char *filename)
489 fn = GNUNET_STRINGS_filename_expand (filename);
491 return GNUNET_SYSERR;
492 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
495 return GNUNET_SYSERR;
497 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
498 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
499 GNUNET_DISK_PERM_USER_READ
500 | GNUNET_DISK_PERM_USER_WRITE
501 | GNUNET_DISK_PERM_GROUP_READ
502 | GNUNET_DISK_PERM_GROUP_WRITE))
505 GNUNET_free (cfg_buf);
506 LOG (GNUNET_ERROR_TYPE_WARNING,
507 "Writing configration to file: %s failed\n", filename);
508 cfg->dirty = GNUNET_SYSERR; /* last write failed */
509 return GNUNET_SYSERR;
512 GNUNET_free (cfg_buf);
513 cfg->dirty = GNUNET_NO; /* last write succeeded */
519 * Iterate over all options in the configuration.
521 * @param cfg configuration to inspect
522 * @param iter function to call on each option
523 * @param iter_cls closure for iter
526 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
527 GNUNET_CONFIGURATION_Iterator iter,
530 struct ConfigSection *spos;
531 struct ConfigEntry *epos;
533 spos = cfg->sections;
536 epos = spos->entries;
539 iter (iter_cls, spos->name, epos->key, epos->val);
548 * Iterate over values of a section in the configuration.
550 * @param cfg configuration to inspect
551 * @param section the section
552 * @param iter function to call on each option
553 * @param iter_cls closure for iter
556 GNUNET_CONFIGURATION_iterate_section_values (const struct
557 GNUNET_CONFIGURATION_Handle *cfg,
559 GNUNET_CONFIGURATION_Iterator iter,
562 struct ConfigSection *spos;
563 struct ConfigEntry *epos;
565 spos = cfg->sections;
566 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
572 epos = spos->entries;
575 iter (iter_cls, spos->name, epos->key, epos->val);
582 * Iterate over all sections in the configuration.
584 * @param cfg configuration to inspect
585 * @param iter function to call on each section
586 * @param iter_cls closure for iter
589 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
591 GNUNET_CONFIGURATION_Section_Iterator
592 iter, void *iter_cls)
594 struct ConfigSection *spos;
595 struct ConfigSection *next;
597 next = cfg->sections;
602 iter (iter_cls, spos->name);
607 * Remove the given section and all options in it.
609 * @param cfg configuration to inspect
610 * @param section name of the section to remove
613 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
616 struct ConfigSection *spos;
617 struct ConfigSection *prev;
618 struct ConfigEntry *ent;
621 spos = cfg->sections;
624 if (0 == strcasecmp (section, spos->name))
627 cfg->sections = spos->next;
629 prev->next = spos->next;
630 while (NULL != (ent = spos->entries))
632 spos->entries = ent->next;
633 GNUNET_free (ent->key);
634 GNUNET_free_non_null (ent->val);
636 cfg->dirty = GNUNET_YES;
638 GNUNET_free (spos->name);
649 * Copy a configuration value to the given target configuration.
650 * Overwrites existing entries.
652 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
653 * @param section section for the value
654 * @param option option name of the value
655 * @param value value to copy
658 copy_entry (void *cls, const char *section, const char *option,
661 struct GNUNET_CONFIGURATION_Handle *dst = cls;
663 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
668 * Duplicate an existing configuration object.
670 * @param cfg configuration to duplicate
671 * @return duplicate configuration
673 struct GNUNET_CONFIGURATION_Handle *
674 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
676 struct GNUNET_CONFIGURATION_Handle *ret;
678 ret = GNUNET_CONFIGURATION_create ();
679 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
688 * @param section FIXME
689 * @return matching entry, NULL if not found
691 static struct ConfigSection *
692 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
694 struct ConfigSection *pos;
697 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
704 * Find an entry from a configuration.
706 * @param cfg handle to the configuration
707 * @param section section the option is in
708 * @param key the option
709 * @return matching entry, NULL if not found
711 static struct ConfigEntry *
712 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
715 struct ConfigSection *sec;
716 struct ConfigEntry *pos;
718 sec = findSection (cfg, section);
722 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
729 * A callback function, compares entries from two configurations
730 * (default against a new configuration) and write the diffs in a
731 * diff-configuration object (the callback object).
733 * @param cls the diff configuration (struct DiffHandle*)
734 * @param section section for the value (of the default conf.)
735 * @param option option name of the value (of the default conf.)
736 * @param value value to copy (of the default conf.)
739 compare_entries (void *cls, const char *section, const char *option,
742 struct DiffHandle *dh = cls;
743 struct ConfigEntry *entNew;
745 entNew = findEntry (dh->cfgDefault, section, option);
746 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
748 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
753 * Compute configuration with only entries that have been changed
755 * @param cfgDefault original configuration
756 * @param cfgNew new configuration
757 * @return configuration with only the differences, never NULL
759 struct GNUNET_CONFIGURATION_Handle *
760 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle
762 const struct GNUNET_CONFIGURATION_Handle
765 struct DiffHandle diffHandle;
767 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
768 diffHandle.cfgDefault = cfgDefault;
769 GNUNET_CONFIGURATION_iterate (cfgNew, &compare_entries, &diffHandle);
770 return diffHandle.cfgDiff;
775 * Write only configuration entries that have been changed to configuration file
776 * @param cfgDefault default configuration
777 * @param cfgNew new configuration
778 * @param filename where to write the configuration diff between default and new
779 * @return GNUNET_OK on success, GNUNET_SYSERR on error
782 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
784 const struct GNUNET_CONFIGURATION_Handle
785 *cfgNew, const char *filename)
788 struct GNUNET_CONFIGURATION_Handle *diff;
790 diff = GNUNET_CONFIGURATION_get_diff (cfgDefault, cfgNew);
791 ret = GNUNET_CONFIGURATION_write (diff, filename);
792 GNUNET_CONFIGURATION_destroy (diff);
798 * Set a configuration value that should be a string.
800 * @param cfg configuration to update
801 * @param section section of interest
802 * @param option option of interest
803 * @param value value to set
806 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
807 const char *section, const char *option,
810 struct ConfigSection *sec;
811 struct ConfigEntry *e;
814 e = findEntry (cfg, section, option);
817 nv = GNUNET_strdup (value);
818 GNUNET_free_non_null (e->val);
822 sec = findSection (cfg, section);
825 sec = GNUNET_malloc (sizeof (struct ConfigSection));
826 sec->name = GNUNET_strdup (section);
827 sec->next = cfg->sections;
830 e = GNUNET_malloc (sizeof (struct ConfigEntry));
831 e->key = GNUNET_strdup (option);
832 e->val = GNUNET_strdup (value);
833 e->next = sec->entries;
839 * Set a configuration value that should be a number.
841 * @param cfg configuration to update
842 * @param section section of interest
843 * @param option option of interest
844 * @param number value to set
847 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
848 const char *section, const char *option,
849 unsigned long long number)
853 GNUNET_snprintf (s, 64, "%llu", number);
854 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
859 * Get a configuration value that should be a number.
861 * @param cfg configuration to inspect
862 * @param section section of interest
863 * @param option option of interest
864 * @param number where to store the numeric value of the option
865 * @return GNUNET_OK on success, GNUNET_SYSERR on error
868 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
869 *cfg, const char *section,
871 unsigned long long *number)
873 struct ConfigEntry *e;
875 e = findEntry (cfg, section, option);
877 return GNUNET_SYSERR;
878 if (1 != SSCANF (e->val, "%llu", number))
879 return GNUNET_SYSERR;
885 * Get a configuration value that should be a relative time.
887 * @param cfg configuration to inspect
888 * @param section section of interest
889 * @param option option of interest
890 * @param time set to the time value stored in the configuration
891 * @return GNUNET_OK on success, GNUNET_SYSERR on error
894 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
895 *cfg, const char *section,
897 struct GNUNET_TIME_Relative *time)
899 struct ConfigEntry *e;
901 e = findEntry (cfg, section, option);
903 return GNUNET_SYSERR;
905 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
910 * Get a configuration value that should be a size in bytes.
912 * @param cfg configuration to inspect
913 * @param section section of interest
914 * @param option option of interest
915 * @param size set to the size in bytes as stored in the configuration
916 * @return GNUNET_OK on success, GNUNET_SYSERR on error
919 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
920 *cfg, const char *section,
922 unsigned long long *size)
924 struct ConfigEntry *e;
926 e = findEntry (cfg, section, option);
928 return GNUNET_SYSERR;
929 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
934 * Get a configuration value that should be a string.
936 * @param cfg configuration to inspect
937 * @param section section of interest
938 * @param option option of interest
939 * @param value will be set to a freshly allocated configuration
940 * value, or NULL if option is not specified
941 * @return GNUNET_OK on success, GNUNET_SYSERR on error
944 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
945 *cfg, const char *section,
946 const char *option, char **value)
948 struct ConfigEntry *e;
950 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section);
951 e = findEntry (cfg, section, option);
952 if ((e == NULL) || (e->val == NULL))
954 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve the string\n");
956 return GNUNET_SYSERR;
958 *value = GNUNET_strdup (e->val);
959 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved string `%s'\n", e->val);
965 * Get a configuration value that should be in a set of
968 * @param cfg configuration to inspect
969 * @param section section of interest
970 * @param option option of interest
971 * @param choices NULL-terminated list of legal values
972 * @param value will be set to an entry in the legal list,
973 * or NULL if option is not specified and no default given
974 * @return GNUNET_OK on success, GNUNET_SYSERR on error
977 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
978 *cfg, const char *section,
979 const char *option, const char **choices,
982 struct ConfigEntry *e;
985 e = findEntry (cfg, section, option);
987 return GNUNET_SYSERR;
989 while (choices[i] != NULL)
991 if (0 == strcasecmp (choices[i], e->val))
995 if (choices[i] == NULL)
997 LOG (GNUNET_ERROR_TYPE_ERROR,
998 _("Configuration value '%s' for '%s'"
999 " in section '%s' is not in set of legal choices\n"), e->val, option,
1001 return GNUNET_SYSERR;
1003 *value = choices[i];
1009 * Test if we have a value for a particular option
1010 * @param cfg configuration to inspect
1011 * @param section section of interest
1012 * @param option option of interest
1013 * @return GNUNET_YES if so, GNUNET_NO if not.
1016 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1017 const char *section, const char *option)
1019 struct ConfigEntry *e;
1021 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
1028 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1029 * where either in the "PATHS" section or the environtment
1030 * "FOO" is set to "DIRECTORY".
1032 * @param cfg configuration to use for path expansion
1033 * @param orig string to $-expand (will be freed!)
1034 * @return $-expanded string
1037 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
1046 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1054 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
1056 if (orig[i] == '\0')
1063 post = &orig[i + 1];
1065 LOG (GNUNET_ERROR_TYPE_DEBUG, "Split into `%s' and `%s'\n", orig, post);
1067 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
1069 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename for `%s' is not in PATHS config section\n", &orig[1]);
1070 if (NULL == (env = getenv (&orig[1])))
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' is not an environment variable\n", &orig[1]);
1073 orig[i] = DIR_SEPARATOR;
1074 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s' (returning orig)\n", orig);
1077 prefix = GNUNET_strdup (env);
1079 LOG (GNUNET_ERROR_TYPE_DEBUG, "Prefix is `%s'\n", prefix);
1080 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1081 strcpy (result, prefix);
1082 if ((strlen (prefix) == 0) ||
1083 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
1084 strcat (result, DIR_SEPARATOR_STR);
1085 strcat (result, post);
1086 GNUNET_free (prefix);
1088 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s'\n", result);
1094 * Get a configuration value that should be a string.
1096 * @param cfg configuration to inspect
1097 * @param section section of interest
1098 * @param option option of interest
1099 * @param value will be set to a freshly allocated configuration
1100 * value, or NULL if option is not specified
1101 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1104 GNUNET_CONFIGURATION_get_value_filename (const struct
1105 GNUNET_CONFIGURATION_Handle *cfg,
1106 const char *section,
1107 const char *option, char **value)
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve filename `%s' in section `%s'\n", option, section);
1113 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1115 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1117 return GNUNET_SYSERR;
1119 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1120 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1121 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1122 *value = GNUNET_STRINGS_filename_expand (tmp);
1124 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1126 return GNUNET_SYSERR;
1132 * Get a configuration value that should be in a set of
1133 * "GNUNET_YES" or "GNUNET_NO".
1135 * @param cfg configuration to inspect
1136 * @param section section of interest
1137 * @param option option of interest
1138 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1141 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1142 *cfg, const char *section,
1145 static const char *yesno[] = { "YES", "NO", NULL };
1150 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1151 if (ret == GNUNET_SYSERR)
1153 if (val == yesno[0])
1160 * Iterate over the set of filenames stored in a configuration value.
1162 * @param cfg configuration to inspect
1163 * @param section section of interest
1164 * @param option option of interest
1165 * @param cb function to call on each filename
1166 * @param cb_cls closure for cb
1167 * @return number of filenames iterated over, -1 on error
1170 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1171 GNUNET_CONFIGURATION_Handle *cfg,
1172 const char *section,
1174 GNUNET_FileNameCallback cb,
1184 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1186 GNUNET_assert (list != NULL);
1191 while (pos[0] == ' ')
1193 if (strlen (pos) == 0)
1196 while ((end[0] != ' ') && (end[0] != '\0'))
1204 memmove (end, &end[1], strlen (&end[1]) + 1);
1206 /* illegal, but just keep it */
1209 /* illegal, but just ignore that there was a '/' */
1217 if (strlen (pos) > 0)
1220 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1222 ret = GNUNET_SYSERR;
1238 * @param value FIXME
1242 escape_name (const char *value)
1248 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1249 memset (escaped, 0, strlen (value) * 2 + 1);
1252 while (rpos[0] != '\0')
1275 * @param cls string we compare with (const char*)
1276 * @param fn filename we are currently looking at
1277 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1280 test_match (void *cls, const char *fn)
1282 const char *of = cls;
1284 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1289 * Append a filename to a configuration value that
1290 * represents a list of filenames
1292 * @param cfg configuration to update
1293 * @param section section of interest
1294 * @param option option of interest
1295 * @param value filename to append
1296 * @return GNUNET_OK on success,
1297 * GNUNET_NO if the filename already in the list
1298 * GNUNET_SYSERR on error
1301 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1302 *cfg, const char *section,
1310 if (GNUNET_SYSERR ==
1311 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1314 return GNUNET_NO; /* already exists */
1316 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1317 old = GNUNET_strdup ("");
1318 escaped = escape_name (value);
1319 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1321 if (strlen (old) > 0)
1323 strcat (nw, escaped);
1324 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1327 GNUNET_free (escaped);
1333 * Remove a filename from a configuration value that
1334 * represents a list of filenames
1336 * @param cfg configuration to update
1337 * @param section section of interest
1338 * @param option option of interest
1339 * @param value filename to remove
1340 * @return GNUNET_OK on success,
1341 * GNUNET_NO if the filename is not in the list,
1342 * GNUNET_SYSERR on error
1345 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1346 *cfg, const char *section,
1357 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1359 match = escape_name (value);
1363 while (pos[0] == ' ')
1365 if (strlen (pos) == 0)
1368 while ((end[0] != ' ') && (end[0] != '\0'))
1379 /* illegal, but just keep it */
1382 /* illegal, but just ignore that there was a '/' */
1390 if (0 == strcmp (pos, match))
1393 memmove (pos, &end[1], strlen (&end[1]) + 1);
1401 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1403 GNUNET_free (match);
1412 GNUNET_free (match);
1418 * Wrapper around GNUNET_CONFIGURATION_parse.
1420 * @param cls the cfg
1421 * @param filename file to parse
1422 * @return GNUNET_OK on success
1425 parse_configuration_file (void *cls, const char *filename)
1427 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1431 /* Examine file extension */
1432 ext = strrchr (filename, '.');
1433 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1439 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1445 * Load default configuration. This function will parse the
1446 * defaults from the given defaults_d directory.
1448 * @param cfg configuration to update
1449 * @param defaults_d directory with the defaults
1450 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1453 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1454 const char *defaults_d)
1456 if (GNUNET_SYSERR ==
1457 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1458 return GNUNET_SYSERR; /* no configuration at all found */
1464 * Load configuration (starts with defaults, then loads
1465 * system-specific configuration).
1467 * @param cfg configuration to update
1468 * @param filename name of the configuration file, NULL to load defaults
1469 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1472 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1473 const char *filename)
1478 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1480 return GNUNET_SYSERR;
1482 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1483 GNUNET_free (ipath);
1484 if (GNUNET_SYSERR ==
1485 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1487 GNUNET_free (baseconfig);
1488 return GNUNET_SYSERR; /* no configuration at all found */
1490 GNUNET_free (baseconfig);
1491 if ((filename != NULL) &&
1492 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1494 /* specified configuration not found */
1495 return GNUNET_SYSERR;
1498 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1500 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1507 /* end of configuration.c */