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", tag);
297 GNUNET_free (section);
298 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
304 * Parse a configuration file, add all of the options in the
305 * file to the configuration environment.
307 * @param cfg configuration to update
308 * @param filename name of the configuration file
309 * @return GNUNET_OK on success, GNUNET_SYSERR on error
312 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
313 const char *filename)
322 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", filename);
323 fn = GNUNET_STRINGS_filename_expand (filename);
324 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config file name expanded to `%s'\n", fn);
326 return GNUNET_SYSERR;
327 dirty = cfg->dirty; /* back up value! */
329 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
331 LOG (GNUNET_ERROR_TYPE_WARNING,
332 "Error while determining the file size of %s\n", fn);
334 return GNUNET_SYSERR;
338 GNUNET_break (0); /* File size is more than the heap size */
340 return GNUNET_SYSERR;
343 mem = GNUNET_malloc (fs);
344 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
346 LOG (GNUNET_ERROR_TYPE_WARNING,
347 "Error while reading file %s\n", fn);
350 return GNUNET_SYSERR;
352 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
354 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
356 /* restore dirty flag - anything we set in the meantime
364 * Test if there are configuration options that were
365 * changed since the last save.
367 * @param cfg configuration to inspect
368 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
371 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
378 * Serializes the given configuration.
380 * @param cfg configuration to serialize
381 * @param size will be set to the size of the serialized memory block
382 * @return the memory block where the serialized configuration is
383 * present. This memory should be freed by the caller
386 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
389 struct ConfigSection *sec;
390 struct ConfigEntry *ent;
400 /* Pass1 : calculate the buffer size required */
405 /* For each section we need to add 3 charaters: {'[',']','\n'} */
406 m_size += strlen (sec->name) + 3;
410 if (NULL != ent->val)
412 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
414 while (NULL != (pos = strstr (pos, "\n")))
419 /* For each key = value pair we need to add 4 characters (2
420 spaces and 1 equal-to character and 1 new line) */
421 m_size += strlen (ent->key) + strlen (ent->val) + 4;
425 /* A new line after section end */
430 /* Pass2: Allocate memory and write the configuration to it */
431 mem = GNUNET_malloc (m_size);
437 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
438 GNUNET_assert (0 < len);
439 memcpy (mem + c_size, cbuf, len);
445 if (NULL != ent->val)
447 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
448 strcpy (val, ent->val);
449 while (NULL != (pos = strstr (val, "\n")))
451 memmove (&pos[2], &pos[1], strlen (&pos[1]));
455 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
457 memcpy (mem + c_size, cbuf, len);
463 memcpy (mem + c_size, "\n", 1);
467 GNUNET_assert (c_size == m_size);
474 * Write configuration file.
476 * @param cfg configuration to write
477 * @param filename where to write the configuration
478 * @return GNUNET_OK on success, GNUNET_SYSERR on error
481 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
482 const char *filename)
488 fn = GNUNET_STRINGS_filename_expand (filename);
490 return GNUNET_SYSERR;
491 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
494 return GNUNET_SYSERR;
496 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
497 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
498 GNUNET_DISK_PERM_USER_READ
499 | GNUNET_DISK_PERM_USER_WRITE
500 | GNUNET_DISK_PERM_GROUP_READ
501 | GNUNET_DISK_PERM_GROUP_WRITE))
504 GNUNET_free (cfg_buf);
505 LOG (GNUNET_ERROR_TYPE_WARNING,
506 "Writing configration to file: %s failed\n", filename);
507 cfg->dirty = GNUNET_SYSERR; /* last write failed */
508 return GNUNET_SYSERR;
511 GNUNET_free (cfg_buf);
512 cfg->dirty = GNUNET_NO; /* last write succeeded */
518 * Iterate over all options in the configuration.
520 * @param cfg configuration to inspect
521 * @param iter function to call on each option
522 * @param iter_cls closure for iter
525 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
526 GNUNET_CONFIGURATION_Iterator iter,
529 struct ConfigSection *spos;
530 struct ConfigEntry *epos;
532 spos = cfg->sections;
535 epos = spos->entries;
538 iter (iter_cls, spos->name, epos->key, epos->val);
547 * Iterate over values of a section in the configuration.
549 * @param cfg configuration to inspect
550 * @param section the section
551 * @param iter function to call on each option
552 * @param iter_cls closure for iter
555 GNUNET_CONFIGURATION_iterate_section_values (const struct
556 GNUNET_CONFIGURATION_Handle *cfg,
558 GNUNET_CONFIGURATION_Iterator iter,
561 struct ConfigSection *spos;
562 struct ConfigEntry *epos;
564 spos = cfg->sections;
565 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
571 epos = spos->entries;
574 iter (iter_cls, spos->name, epos->key, epos->val);
581 * Iterate over all sections in the configuration.
583 * @param cfg configuration to inspect
584 * @param iter function to call on each section
585 * @param iter_cls closure for iter
588 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
590 GNUNET_CONFIGURATION_Section_Iterator
591 iter, void *iter_cls)
593 struct ConfigSection *spos;
594 struct ConfigSection *next;
596 next = cfg->sections;
601 iter (iter_cls, spos->name);
606 * Remove the given section and all options in it.
608 * @param cfg configuration to inspect
609 * @param section name of the section to remove
612 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
615 struct ConfigSection *spos;
616 struct ConfigSection *prev;
617 struct ConfigEntry *ent;
620 spos = cfg->sections;
623 if (0 == strcasecmp (section, spos->name))
626 cfg->sections = spos->next;
628 prev->next = spos->next;
629 while (NULL != (ent = spos->entries))
631 spos->entries = ent->next;
632 GNUNET_free (ent->key);
633 GNUNET_free_non_null (ent->val);
635 cfg->dirty = GNUNET_YES;
637 GNUNET_free (spos->name);
648 * Copy a configuration value to the given target configuration.
649 * Overwrites existing entries.
651 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
652 * @param section section for the value
653 * @param option option name of the value
654 * @param value value to copy
657 copy_entry (void *cls, const char *section, const char *option,
660 struct GNUNET_CONFIGURATION_Handle *dst = cls;
662 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
667 * Duplicate an existing configuration object.
669 * @param cfg configuration to duplicate
670 * @return duplicate configuration
672 struct GNUNET_CONFIGURATION_Handle *
673 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
675 struct GNUNET_CONFIGURATION_Handle *ret;
677 ret = GNUNET_CONFIGURATION_create ();
678 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
687 * @param section FIXME
688 * @return matching entry, NULL if not found
690 static struct ConfigSection *
691 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
693 struct ConfigSection *pos;
696 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
703 * Find an entry from a configuration.
705 * @param cfg handle to the configuration
706 * @param section section the option is in
707 * @param key the option
708 * @return matching entry, NULL if not found
710 static struct ConfigEntry *
711 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
714 struct ConfigSection *sec;
715 struct ConfigEntry *pos;
717 sec = findSection (cfg, section);
721 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
728 * A callback function, compares entries from two configurations
729 * (default against a new configuration) and write the diffs in a
730 * diff-configuration object (the callback object).
732 * @param cls the diff configuration (struct DiffHandle*)
733 * @param section section for the value (of the default conf.)
734 * @param option option name of the value (of the default conf.)
735 * @param value value to copy (of the default conf.)
738 compareEntries (void *cls, const char *section, const char *option,
741 struct DiffHandle *dh = cls;
742 struct ConfigEntry *entNew;
744 entNew = findEntry (dh->cfgDefault, section, option);
745 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
747 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
752 * Write only configuration entries that have been changed to configuration file
753 * @param cfgDefault default configuration
754 * @param cfgNew new configuration
755 * @param filename where to write the configuration diff between default and new
756 * @return GNUNET_OK on success, GNUNET_SYSERR on error
759 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
761 const struct GNUNET_CONFIGURATION_Handle
762 *cfgNew, const char *filename)
765 struct DiffHandle diffHandle;
767 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
768 diffHandle.cfgDefault = cfgDefault;
769 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
770 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
771 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
777 * Set a configuration value that should be a string.
779 * @param cfg configuration to update
780 * @param section section of interest
781 * @param option option of interest
782 * @param value value to set
785 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
786 const char *section, const char *option,
789 struct ConfigSection *sec;
790 struct ConfigEntry *e;
793 e = findEntry (cfg, section, option);
796 nv = GNUNET_strdup (value);
797 GNUNET_free_non_null (e->val);
801 sec = findSection (cfg, section);
804 sec = GNUNET_malloc (sizeof (struct ConfigSection));
805 sec->name = GNUNET_strdup (section);
806 sec->next = cfg->sections;
809 e = GNUNET_malloc (sizeof (struct ConfigEntry));
810 e->key = GNUNET_strdup (option);
811 e->val = GNUNET_strdup (value);
812 e->next = sec->entries;
818 * Set a configuration value that should be a number.
820 * @param cfg configuration to update
821 * @param section section of interest
822 * @param option option of interest
823 * @param number value to set
826 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
827 const char *section, const char *option,
828 unsigned long long number)
832 GNUNET_snprintf (s, 64, "%llu", number);
833 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
838 * Get a configuration value that should be a number.
840 * @param cfg configuration to inspect
841 * @param section section of interest
842 * @param option option of interest
843 * @param number where to store the numeric value of the option
844 * @return GNUNET_OK on success, GNUNET_SYSERR on error
847 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
848 *cfg, const char *section,
850 unsigned long long *number)
852 struct ConfigEntry *e;
854 e = findEntry (cfg, section, option);
856 return GNUNET_SYSERR;
857 if (1 != SSCANF (e->val, "%llu", number))
858 return GNUNET_SYSERR;
864 * Get a configuration value that should be a relative time.
866 * @param cfg configuration to inspect
867 * @param section section of interest
868 * @param option option of interest
869 * @param time set to the time value stored in the configuration
870 * @return GNUNET_OK on success, GNUNET_SYSERR on error
873 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
874 *cfg, const char *section,
876 struct GNUNET_TIME_Relative *time)
878 struct ConfigEntry *e;
880 e = findEntry (cfg, section, option);
882 return GNUNET_SYSERR;
884 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
889 * Get a configuration value that should be a size in bytes.
891 * @param cfg configuration to inspect
892 * @param section section of interest
893 * @param option option of interest
894 * @param size set to the size in bytes as stored in the configuration
895 * @return GNUNET_OK on success, GNUNET_SYSERR on error
898 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
899 *cfg, const char *section,
901 unsigned long long *size)
903 struct ConfigEntry *e;
905 e = findEntry (cfg, section, option);
907 return GNUNET_SYSERR;
908 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
913 * Get a configuration value that should be a string.
915 * @param cfg configuration to inspect
916 * @param section section of interest
917 * @param option option of interest
918 * @param value will be set to a freshly allocated configuration
919 * value, or NULL if option is not specified
920 * @return GNUNET_OK on success, GNUNET_SYSERR on error
923 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
924 *cfg, const char *section,
925 const char *option, char **value)
927 struct ConfigEntry *e;
929 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section);
930 e = findEntry (cfg, section, option);
931 if ((e == NULL) || (e->val == NULL))
933 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve the string\n");
935 return GNUNET_SYSERR;
937 *value = GNUNET_strdup (e->val);
938 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved string `%s'\n", e->val);
944 * Get a configuration value that should be in a set of
947 * @param cfg configuration to inspect
948 * @param section section of interest
949 * @param option option of interest
950 * @param choices NULL-terminated list of legal values
951 * @param value will be set to an entry in the legal list,
952 * or NULL if option is not specified and no default given
953 * @return GNUNET_OK on success, GNUNET_SYSERR on error
956 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
957 *cfg, const char *section,
958 const char *option, const char **choices,
961 struct ConfigEntry *e;
964 e = findEntry (cfg, section, option);
966 return GNUNET_SYSERR;
968 while (choices[i] != NULL)
970 if (0 == strcasecmp (choices[i], e->val))
974 if (choices[i] == NULL)
976 LOG (GNUNET_ERROR_TYPE_ERROR,
977 _("Configuration value '%s' for '%s'"
978 " in section '%s' is not in set of legal choices\n"), e->val, option,
980 return GNUNET_SYSERR;
988 * Test if we have a value for a particular option
989 * @param cfg configuration to inspect
990 * @param section section of interest
991 * @param option option of interest
992 * @return GNUNET_YES if so, GNUNET_NO if not.
995 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
996 const char *section, const char *option)
998 struct ConfigEntry *e;
1000 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
1007 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1008 * where either in the "PATHS" section or the environtment
1009 * "FOO" is set to "DIRECTORY".
1011 * @param cfg configuration to use for path expansion
1012 * @param orig string to $-expand (will be freed!)
1013 * @return $-expanded string
1016 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
1025 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1029 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1033 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
1035 if (orig[i] == '\0')
1042 post = &orig[i + 1];
1044 LOG (GNUNET_ERROR_TYPE_DEBUG, "Split into `%s' and `%s'\n", orig, post);
1046 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
1048 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename for `%s' is not in PATHS config section\n", &orig[1]);
1049 if (NULL == (env = getenv (&orig[1])))
1051 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' is not an environment variable\n", &orig[1]);
1052 orig[i] = DIR_SEPARATOR;
1053 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s' (returning orig)\n", orig);
1056 prefix = GNUNET_strdup (env);
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, "Prefix is `%s'\n", prefix);
1059 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1060 strcpy (result, prefix);
1061 if ((strlen (prefix) == 0) ||
1062 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
1063 strcat (result, DIR_SEPARATOR_STR);
1064 strcat (result, post);
1065 GNUNET_free (prefix);
1067 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s'\n", result);
1073 * Get a configuration value that should be a string.
1075 * @param cfg configuration to inspect
1076 * @param section section of interest
1077 * @param option option of interest
1078 * @param value will be set to a freshly allocated configuration
1079 * value, or NULL if option is not specified
1080 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1083 GNUNET_CONFIGURATION_get_value_filename (const struct
1084 GNUNET_CONFIGURATION_Handle *cfg,
1085 const char *section,
1086 const char *option, char **value)
1090 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve filename `%s' in section `%s'\n", option, section);
1092 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1094 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1096 return GNUNET_SYSERR;
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1099 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1100 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1101 *value = GNUNET_STRINGS_filename_expand (tmp);
1103 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1105 return GNUNET_SYSERR;
1111 * Get a configuration value that should be in a set of
1112 * "GNUNET_YES" or "GNUNET_NO".
1114 * @param cfg configuration to inspect
1115 * @param section section of interest
1116 * @param option option of interest
1117 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1120 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1121 *cfg, const char *section,
1124 static const char *yesno[] = { "YES", "NO", NULL };
1129 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1130 if (ret == GNUNET_SYSERR)
1132 if (val == yesno[0])
1139 * Iterate over the set of filenames stored in a configuration value.
1141 * @param cfg configuration to inspect
1142 * @param section section of interest
1143 * @param option option of interest
1144 * @param cb function to call on each filename
1145 * @param cb_cls closure for cb
1146 * @return number of filenames iterated over, -1 on error
1149 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1150 GNUNET_CONFIGURATION_Handle *cfg,
1151 const char *section,
1153 GNUNET_FileNameCallback cb,
1163 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1165 GNUNET_assert (list != NULL);
1170 while (pos[0] == ' ')
1172 if (strlen (pos) == 0)
1175 while ((end[0] != ' ') && (end[0] != '\0'))
1183 memmove (end, &end[1], strlen (&end[1]) + 1);
1185 /* illegal, but just keep it */
1188 /* illegal, but just ignore that there was a '/' */
1196 if (strlen (pos) > 0)
1199 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1201 ret = GNUNET_SYSERR;
1217 * @param value FIXME
1221 escape_name (const char *value)
1227 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1228 memset (escaped, 0, strlen (value) * 2 + 1);
1231 while (rpos[0] != '\0')
1254 * @param cls string we compare with (const char*)
1255 * @param fn filename we are currently looking at
1256 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1259 test_match (void *cls, const char *fn)
1261 const char *of = cls;
1263 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1268 * Append a filename to a configuration value that
1269 * represents a list of filenames
1271 * @param cfg configuration to update
1272 * @param section section of interest
1273 * @param option option of interest
1274 * @param value filename to append
1275 * @return GNUNET_OK on success,
1276 * GNUNET_NO if the filename already in the list
1277 * GNUNET_SYSERR on error
1280 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1281 *cfg, const char *section,
1289 if (GNUNET_SYSERR ==
1290 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1293 return GNUNET_NO; /* already exists */
1295 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1296 old = GNUNET_strdup ("");
1297 escaped = escape_name (value);
1298 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1300 if (strlen (old) > 0)
1302 strcat (nw, escaped);
1303 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1306 GNUNET_free (escaped);
1312 * Remove a filename from a configuration value that
1313 * represents a list of filenames
1315 * @param cfg configuration to update
1316 * @param section section of interest
1317 * @param option option of interest
1318 * @param value filename to remove
1319 * @return GNUNET_OK on success,
1320 * GNUNET_NO if the filename is not in the list,
1321 * GNUNET_SYSERR on error
1324 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1325 *cfg, const char *section,
1336 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1338 match = escape_name (value);
1342 while (pos[0] == ' ')
1344 if (strlen (pos) == 0)
1347 while ((end[0] != ' ') && (end[0] != '\0'))
1358 /* illegal, but just keep it */
1361 /* illegal, but just ignore that there was a '/' */
1369 if (0 == strcmp (pos, match))
1372 memmove (pos, &end[1], strlen (&end[1]) + 1);
1380 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1382 GNUNET_free (match);
1391 GNUNET_free (match);
1397 * Wrapper around GNUNET_CONFIGURATION_parse.
1399 * @param cls the cfg
1400 * @param filename file to parse
1401 * @return GNUNET_OK on success
1404 parse_configuration_file (void *cls, const char *filename)
1406 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1410 /* Examine file extension */
1411 ext = strrchr (filename, '.');
1412 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1414 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1418 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1424 * Load default configuration. This function will parse the
1425 * defaults from the given defaults_d directory.
1427 * @param cfg configuration to update
1428 * @param defaults_d directory with the defaults
1429 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1432 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1433 const char *defaults_d)
1435 if (GNUNET_SYSERR ==
1436 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1437 return GNUNET_SYSERR; /* no configuration at all found */
1443 * Load configuration (starts with defaults, then loads
1444 * system-specific configuration).
1446 * @param cfg configuration to update
1447 * @param filename name of the configuration file, NULL to load defaults
1448 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1451 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1452 const char *filename)
1457 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1459 return GNUNET_SYSERR;
1461 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1462 GNUNET_free (ipath);
1463 if (GNUNET_SYSERR ==
1464 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1466 GNUNET_free (baseconfig);
1467 return GNUNET_SYSERR; /* no configuration at all found */
1469 GNUNET_free (baseconfig);
1470 if ((filename != NULL) &&
1471 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1473 /* specified configuration not found */
1474 return GNUNET_SYSERR;
1477 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1479 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1482 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1484 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1485 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1491 /* end of configuration.c */