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]);
289 LOG (GNUNET_ERROR_TYPE_WARNING,
290 _("Syntax error while deserializing in line %u\n"),
295 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n", tag);
296 GNUNET_free (section);
297 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
303 * Parse a configuration file, add all of the options in the
304 * file to the configuration environment.
306 * @param cfg configuration to update
307 * @param filename name of the configuration file
308 * @return GNUNET_OK on success, GNUNET_SYSERR on error
311 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
312 const char *filename)
321 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", filename);
322 fn = GNUNET_STRINGS_filename_expand (filename);
323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config file name expanded to `%s'\n", fn);
325 return GNUNET_SYSERR;
326 dirty = cfg->dirty; /* back up value! */
328 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
330 LOG (GNUNET_ERROR_TYPE_WARNING,
331 "Error while determining the file size of %s\n", fn);
333 return GNUNET_SYSERR;
337 GNUNET_break (0); /* File size is more than the heap size */
339 return GNUNET_SYSERR;
342 mem = GNUNET_malloc (fs);
343 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
345 LOG (GNUNET_ERROR_TYPE_WARNING,
346 "Error while reading file %s\n", fn);
349 return GNUNET_SYSERR;
351 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
353 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
355 /* restore dirty flag - anything we set in the meantime
363 * Test if there are configuration options that were
364 * changed since the last save.
366 * @param cfg configuration to inspect
367 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
370 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
377 * Serializes the given configuration.
379 * @param cfg configuration to serialize
380 * @param size will be set to the size of the serialized memory block
381 * @return the memory block where the serialized configuration is
382 * present. This memory should be freed by the caller
385 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
388 struct ConfigSection *sec;
389 struct ConfigEntry *ent;
399 /* Pass1 : calculate the buffer size required */
404 /* For each section we need to add 3 charaters: {'[',']','\n'} */
405 m_size += strlen (sec->name) + 3;
409 if (NULL != ent->val)
411 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
413 while (NULL != (pos = strstr (pos, "\n")))
418 /* For each key = value pair we need to add 4 characters (2
419 spaces and 1 equal-to character and 1 new line) */
420 m_size += strlen (ent->key) + strlen (ent->val) + 4;
424 /* A new line after section end */
429 /* Pass2: Allocate memory and write the configuration to it */
430 mem = GNUNET_malloc (m_size);
436 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
437 GNUNET_assert (0 < len);
438 memcpy (mem + c_size, cbuf, len);
444 if (NULL != ent->val)
446 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
447 strcpy (val, ent->val);
448 while (NULL != (pos = strstr (val, "\n")))
450 memmove (&pos[2], &pos[1], strlen (&pos[1]));
454 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
456 memcpy (mem + c_size, cbuf, len);
462 memcpy (mem + c_size, "\n", 1);
466 GNUNET_assert (c_size == m_size);
473 * Write configuration file.
475 * @param cfg configuration to write
476 * @param filename where to write the configuration
477 * @return GNUNET_OK on success, GNUNET_SYSERR on error
480 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
481 const char *filename)
487 fn = GNUNET_STRINGS_filename_expand (filename);
489 return GNUNET_SYSERR;
490 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
493 return GNUNET_SYSERR;
495 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
496 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
497 GNUNET_DISK_PERM_USER_READ
498 | GNUNET_DISK_PERM_USER_WRITE
499 | GNUNET_DISK_PERM_GROUP_READ
500 | GNUNET_DISK_PERM_GROUP_WRITE))
503 GNUNET_free (cfg_buf);
504 LOG (GNUNET_ERROR_TYPE_WARNING,
505 "Writing configration to file: %s failed\n", filename);
506 cfg->dirty = GNUNET_SYSERR; /* last write failed */
507 return GNUNET_SYSERR;
510 GNUNET_free (cfg_buf);
511 cfg->dirty = GNUNET_NO; /* last write succeeded */
517 * Iterate over all options in the configuration.
519 * @param cfg configuration to inspect
520 * @param iter function to call on each option
521 * @param iter_cls closure for iter
524 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
525 GNUNET_CONFIGURATION_Iterator iter,
528 struct ConfigSection *spos;
529 struct ConfigEntry *epos;
531 spos = cfg->sections;
534 epos = spos->entries;
537 iter (iter_cls, spos->name, epos->key, epos->val);
546 * Iterate over values of a section in the configuration.
548 * @param cfg configuration to inspect
549 * @param section the section
550 * @param iter function to call on each option
551 * @param iter_cls closure for iter
554 GNUNET_CONFIGURATION_iterate_section_values (const struct
555 GNUNET_CONFIGURATION_Handle *cfg,
557 GNUNET_CONFIGURATION_Iterator iter,
560 struct ConfigSection *spos;
561 struct ConfigEntry *epos;
563 spos = cfg->sections;
564 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
570 epos = spos->entries;
573 iter (iter_cls, spos->name, epos->key, epos->val);
580 * Iterate over all sections in the configuration.
582 * @param cfg configuration to inspect
583 * @param iter function to call on each section
584 * @param iter_cls closure for iter
587 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
589 GNUNET_CONFIGURATION_Section_Iterator
590 iter, void *iter_cls)
592 struct ConfigSection *spos;
593 struct ConfigSection *next;
595 next = cfg->sections;
600 iter (iter_cls, spos->name);
605 * Remove the given section and all options in it.
607 * @param cfg configuration to inspect
608 * @param section name of the section to remove
611 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
614 struct ConfigSection *spos;
615 struct ConfigSection *prev;
616 struct ConfigEntry *ent;
619 spos = cfg->sections;
622 if (0 == strcasecmp (section, spos->name))
625 cfg->sections = spos->next;
627 prev->next = spos->next;
628 while (NULL != (ent = spos->entries))
630 spos->entries = ent->next;
631 GNUNET_free (ent->key);
632 GNUNET_free_non_null (ent->val);
634 cfg->dirty = GNUNET_YES;
636 GNUNET_free (spos->name);
647 * Copy a configuration value to the given target configuration.
648 * Overwrites existing entries.
650 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
651 * @param section section for the value
652 * @param option option name of the value
653 * @param value value to copy
656 copy_entry (void *cls, const char *section, const char *option,
659 struct GNUNET_CONFIGURATION_Handle *dst = cls;
661 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
666 * Duplicate an existing configuration object.
668 * @param cfg configuration to duplicate
669 * @return duplicate configuration
671 struct GNUNET_CONFIGURATION_Handle *
672 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
674 struct GNUNET_CONFIGURATION_Handle *ret;
676 ret = GNUNET_CONFIGURATION_create ();
677 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
686 * @param section FIXME
687 * @return matching entry, NULL if not found
689 static struct ConfigSection *
690 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
692 struct ConfigSection *pos;
695 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
702 * Find an entry from a configuration.
704 * @param cfg handle to the configuration
705 * @param section section the option is in
706 * @param key the option
707 * @return matching entry, NULL if not found
709 static struct ConfigEntry *
710 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
713 struct ConfigSection *sec;
714 struct ConfigEntry *pos;
716 sec = findSection (cfg, section);
720 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
727 * A callback function, compares entries from two configurations
728 * (default against a new configuration) and write the diffs in a
729 * diff-configuration object (the callback object).
731 * @param cls the diff configuration (struct DiffHandle*)
732 * @param section section for the value (of the default conf.)
733 * @param option option name of the value (of the default conf.)
734 * @param value value to copy (of the default conf.)
737 compareEntries (void *cls, const char *section, const char *option,
740 struct DiffHandle *dh = cls;
741 struct ConfigEntry *entNew;
743 entNew = findEntry (dh->cfgDefault, section, option);
744 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
746 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
751 * Write only configuration entries that have been changed to configuration file
752 * @param cfgDefault default configuration
753 * @param cfgNew new configuration
754 * @param filename where to write the configuration diff between default and new
755 * @return GNUNET_OK on success, GNUNET_SYSERR on error
758 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
760 const struct GNUNET_CONFIGURATION_Handle
761 *cfgNew, const char *filename)
764 struct DiffHandle diffHandle;
766 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
767 diffHandle.cfgDefault = cfgDefault;
768 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
769 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
770 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
776 * Set a configuration value that should be a string.
778 * @param cfg configuration to update
779 * @param section section of interest
780 * @param option option of interest
781 * @param value value to set
784 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
785 const char *section, const char *option,
788 struct ConfigSection *sec;
789 struct ConfigEntry *e;
792 e = findEntry (cfg, section, option);
795 nv = GNUNET_strdup (value);
796 GNUNET_free_non_null (e->val);
800 sec = findSection (cfg, section);
803 sec = GNUNET_malloc (sizeof (struct ConfigSection));
804 sec->name = GNUNET_strdup (section);
805 sec->next = cfg->sections;
808 e = GNUNET_malloc (sizeof (struct ConfigEntry));
809 e->key = GNUNET_strdup (option);
810 e->val = GNUNET_strdup (value);
811 e->next = sec->entries;
817 * Set a configuration value that should be a number.
819 * @param cfg configuration to update
820 * @param section section of interest
821 * @param option option of interest
822 * @param number value to set
825 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
826 const char *section, const char *option,
827 unsigned long long number)
831 GNUNET_snprintf (s, 64, "%llu", number);
832 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
837 * Get a configuration value that should be a number.
839 * @param cfg configuration to inspect
840 * @param section section of interest
841 * @param option option of interest
842 * @param number where to store the numeric value of the option
843 * @return GNUNET_OK on success, GNUNET_SYSERR on error
846 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
847 *cfg, const char *section,
849 unsigned long long *number)
851 struct ConfigEntry *e;
853 e = findEntry (cfg, section, option);
855 return GNUNET_SYSERR;
856 if (1 != SSCANF (e->val, "%llu", number))
857 return GNUNET_SYSERR;
863 * Get a configuration value that should be a relative time.
865 * @param cfg configuration to inspect
866 * @param section section of interest
867 * @param option option of interest
868 * @param time set to the time value stored in the configuration
869 * @return GNUNET_OK on success, GNUNET_SYSERR on error
872 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
873 *cfg, const char *section,
875 struct GNUNET_TIME_Relative *time)
877 struct ConfigEntry *e;
879 e = findEntry (cfg, section, option);
881 return GNUNET_SYSERR;
883 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
888 * Get a configuration value that should be a size in bytes.
890 * @param cfg configuration to inspect
891 * @param section section of interest
892 * @param option option of interest
893 * @param size set to the size in bytes as stored in the configuration
894 * @return GNUNET_OK on success, GNUNET_SYSERR on error
897 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
898 *cfg, const char *section,
900 unsigned long long *size)
902 struct ConfigEntry *e;
904 e = findEntry (cfg, section, option);
906 return GNUNET_SYSERR;
907 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
912 * Get a configuration value that should be a string.
914 * @param cfg configuration to inspect
915 * @param section section of interest
916 * @param option option of interest
917 * @param value will be set to a freshly allocated configuration
918 * value, or NULL if option is not specified
919 * @return GNUNET_OK on success, GNUNET_SYSERR on error
922 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
923 *cfg, const char *section,
924 const char *option, char **value)
926 struct ConfigEntry *e;
928 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section);
929 e = findEntry (cfg, section, option);
930 if ((e == NULL) || (e->val == NULL))
932 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve the string\n");
934 return GNUNET_SYSERR;
936 *value = GNUNET_strdup (e->val);
937 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved string `%s'\n", e->val);
943 * Get a configuration value that should be in a set of
946 * @param cfg configuration to inspect
947 * @param section section of interest
948 * @param option option of interest
949 * @param choices NULL-terminated list of legal values
950 * @param value will be set to an entry in the legal list,
951 * or NULL if option is not specified and no default given
952 * @return GNUNET_OK on success, GNUNET_SYSERR on error
955 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
956 *cfg, const char *section,
957 const char *option, const char **choices,
960 struct ConfigEntry *e;
963 e = findEntry (cfg, section, option);
965 return GNUNET_SYSERR;
967 while (choices[i] != NULL)
969 if (0 == strcasecmp (choices[i], e->val))
973 if (choices[i] == NULL)
975 LOG (GNUNET_ERROR_TYPE_ERROR,
976 _("Configuration value '%s' for '%s'"
977 " in section '%s' is not in set of legal choices\n"), e->val, option,
979 return GNUNET_SYSERR;
987 * Test if we have a value for a particular option
988 * @param cfg configuration to inspect
989 * @param section section of interest
990 * @param option option of interest
991 * @return GNUNET_YES if so, GNUNET_NO if not.
994 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
995 const char *section, const char *option)
997 struct ConfigEntry *e;
999 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
1006 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1007 * where either in the "PATHS" section or the environtment
1008 * "FOO" is set to "DIRECTORY".
1010 * @param cfg configuration to use for path expansion
1011 * @param orig string to $-expand (will be freed!)
1012 * @return $-expanded string
1015 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
1024 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1028 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1032 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
1034 if (orig[i] == '\0')
1041 post = &orig[i + 1];
1043 LOG (GNUNET_ERROR_TYPE_DEBUG, "Split into `%s' and `%s'\n", orig, post);
1045 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
1047 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename for `%s' is not in PATHS config section\n", &orig[1]);
1048 if (NULL == (env = getenv (&orig[1])))
1050 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' is not an environment variable\n", &orig[1]);
1051 orig[i] = DIR_SEPARATOR;
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s' (returning orig)\n", orig);
1055 prefix = GNUNET_strdup (env);
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "Prefix is `%s'\n", prefix);
1058 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1059 strcpy (result, prefix);
1060 if ((strlen (prefix) == 0) ||
1061 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
1062 strcat (result, DIR_SEPARATOR_STR);
1063 strcat (result, post);
1064 GNUNET_free (prefix);
1066 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s'\n", result);
1072 * Get a configuration value that should be a string.
1074 * @param cfg configuration to inspect
1075 * @param section section of interest
1076 * @param option option of interest
1077 * @param value will be set to a freshly allocated configuration
1078 * value, or NULL if option is not specified
1079 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1082 GNUNET_CONFIGURATION_get_value_filename (const struct
1083 GNUNET_CONFIGURATION_Handle *cfg,
1084 const char *section,
1085 const char *option, char **value)
1089 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve filename `%s' in section `%s'\n", option, section);
1091 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1093 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1095 return GNUNET_SYSERR;
1097 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1098 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1099 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1100 *value = GNUNET_STRINGS_filename_expand (tmp);
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1104 return GNUNET_SYSERR;
1110 * Get a configuration value that should be in a set of
1111 * "GNUNET_YES" or "GNUNET_NO".
1113 * @param cfg configuration to inspect
1114 * @param section section of interest
1115 * @param option option of interest
1116 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1119 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1120 *cfg, const char *section,
1123 static const char *yesno[] = { "YES", "NO", NULL };
1128 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1129 if (ret == GNUNET_SYSERR)
1131 if (val == yesno[0])
1138 * Iterate over the set of filenames stored in a configuration value.
1140 * @param cfg configuration to inspect
1141 * @param section section of interest
1142 * @param option option of interest
1143 * @param cb function to call on each filename
1144 * @param cb_cls closure for cb
1145 * @return number of filenames iterated over, -1 on error
1148 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1149 GNUNET_CONFIGURATION_Handle *cfg,
1150 const char *section,
1152 GNUNET_FileNameCallback cb,
1162 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1164 GNUNET_assert (list != NULL);
1169 while (pos[0] == ' ')
1171 if (strlen (pos) == 0)
1174 while ((end[0] != ' ') && (end[0] != '\0'))
1182 memmove (end, &end[1], strlen (&end[1]) + 1);
1184 /* illegal, but just keep it */
1187 /* illegal, but just ignore that there was a '/' */
1195 if (strlen (pos) > 0)
1198 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1200 ret = GNUNET_SYSERR;
1216 * @param value FIXME
1220 escape_name (const char *value)
1226 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1227 memset (escaped, 0, strlen (value) * 2 + 1);
1230 while (rpos[0] != '\0')
1253 * @param cls string we compare with (const char*)
1254 * @param fn filename we are currently looking at
1255 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1258 test_match (void *cls, const char *fn)
1260 const char *of = cls;
1262 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1267 * Append a filename to a configuration value that
1268 * represents a list of filenames
1270 * @param cfg configuration to update
1271 * @param section section of interest
1272 * @param option option of interest
1273 * @param value filename to append
1274 * @return GNUNET_OK on success,
1275 * GNUNET_NO if the filename already in the list
1276 * GNUNET_SYSERR on error
1279 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1280 *cfg, const char *section,
1288 if (GNUNET_SYSERR ==
1289 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1292 return GNUNET_NO; /* already exists */
1294 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1295 old = GNUNET_strdup ("");
1296 escaped = escape_name (value);
1297 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1299 if (strlen (old) > 0)
1301 strcat (nw, escaped);
1302 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1305 GNUNET_free (escaped);
1311 * Remove a filename from a configuration value that
1312 * represents a list of filenames
1314 * @param cfg configuration to update
1315 * @param section section of interest
1316 * @param option option of interest
1317 * @param value filename to remove
1318 * @return GNUNET_OK on success,
1319 * GNUNET_NO if the filename is not in the list,
1320 * GNUNET_SYSERR on error
1323 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1324 *cfg, const char *section,
1335 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1337 match = escape_name (value);
1341 while (pos[0] == ' ')
1343 if (strlen (pos) == 0)
1346 while ((end[0] != ' ') && (end[0] != '\0'))
1357 /* illegal, but just keep it */
1360 /* illegal, but just ignore that there was a '/' */
1368 if (0 == strcmp (pos, match))
1371 memmove (pos, &end[1], strlen (&end[1]) + 1);
1379 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1381 GNUNET_free (match);
1390 GNUNET_free (match);
1396 * Wrapper around GNUNET_CONFIGURATION_parse.
1398 * @param cls the cfg
1399 * @param filename file to parse
1400 * @return GNUNET_OK on success
1403 parse_configuration_file (void *cls, const char *filename)
1405 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1409 /* Examine file extension */
1410 ext = strrchr (filename, '.');
1411 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1417 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1423 * Load default configuration. This function will parse the
1424 * defaults from the given defaults_d directory.
1426 * @param cfg configuration to update
1427 * @param defaults_d directory with the defaults
1428 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1431 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1432 const char *defaults_d)
1434 if (GNUNET_SYSERR ==
1435 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1436 return GNUNET_SYSERR; /* no configuration at all found */
1442 * Load configuration (starts with defaults, then loads
1443 * system-specific configuration).
1445 * @param cfg configuration to update
1446 * @param filename name of the configuration file, NULL to load defaults
1447 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1450 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1451 const char *filename)
1456 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1458 return GNUNET_SYSERR;
1460 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1461 GNUNET_free (ipath);
1462 if (GNUNET_SYSERR ==
1463 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1465 GNUNET_free (baseconfig);
1466 return GNUNET_SYSERR; /* no configuration at all found */
1468 GNUNET_free (baseconfig);
1469 if ((filename != NULL) &&
1470 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1472 /* specified configuration not found */
1473 return GNUNET_SYSERR;
1476 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1478 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1481 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1483 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1484 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1490 /* end of configuration.c */