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,
171 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing config file\n");
173 section = GNUNET_strdup ("");
176 memset (line, 0, 256);
177 while (r_bytes < size)
179 /* fgets-like behaviour on buffer. read size is 255 bytes */
180 to_read = size - r_bytes;
183 memcpy (line, mem + r_bytes, to_read);
184 line[to_read] = '\0';
185 if (NULL != (pos = strstr (line, "\n")))
188 r_bytes += (pos - line) + 1;
192 /* fgets-like behaviour end */
194 for (i = 0; i < 255; i++)
197 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' || line[0] == '\r')
200 for (i = 0; (i < 255 && line[i] != 0); i++)
201 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
205 /* remove tailing whitespace */
206 for (i = strlen (line) - 1; (i >= 0) && (isspace ((unsigned char) line[i]));
209 if (1 == SSCANF (line, "@INLINE@ %191[^\n]", value))
212 if (GNUNET_YES == allow_inline)
214 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
215 ret = GNUNET_SYSERR; /* failed to parse included config */
218 LOG (GNUNET_ERROR_TYPE_DEBUG,
219 "Ignoring parsing INLINE configurations as allow_inline is false\n");
221 else if (1 == SSCANF (line, "[%99[^]]]", value))
224 GNUNET_free (section);
225 section = GNUNET_strdup (value);
226 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config section `%s'\n", section);
228 else if (2 == SSCANF (line, " %63[^= ] = %191[^\n]", tag, value))
232 i = strlen (value) - 1;
233 while ((i >= 0) && (isspace ((unsigned char) value[i])))
240 while ((value[i] != '\0') && (value[i] != '"'))
250 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s=%s\n", tag, value);
251 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
253 else if (1 == SSCANF (line, " %63[^= ] =[^\n]", tag))
256 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s is empty\n", tag);
257 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
262 LOG (GNUNET_ERROR_TYPE_WARNING,
263 _("Syntax error while deserializing operation at line %u\n"), nr);
268 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n", tag);
269 GNUNET_free (section);
270 GNUNET_assert (r_bytes == size);
276 * Parse a configuration file, add all of the options in the
277 * file to the configuration environment.
279 * @param cfg configuration to update
280 * @param filename name of the configuration file
281 * @return GNUNET_OK on success, GNUNET_SYSERR on error
284 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
285 const char *filename)
294 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", filename);
295 fn = GNUNET_STRINGS_filename_expand (filename);
296 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config file name expanded to `%s'\n", fn);
298 return GNUNET_SYSERR;
299 dirty = cfg->dirty; /* back up value! */
301 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
303 LOG (GNUNET_ERROR_TYPE_WARNING,
304 "Error while determining the file size of %s\n", fn);
306 return GNUNET_SYSERR;
310 GNUNET_break (0); /* File size is more than the heap size */
312 return GNUNET_SYSERR;
315 mem = GNUNET_malloc (fs);
316 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
318 LOG (GNUNET_ERROR_TYPE_WARNING,
319 "Error while reading file %s\n", fn);
322 return GNUNET_SYSERR;
324 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
326 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
328 /* restore dirty flag - anything we set in the meantime
336 * Test if there are configuration options that were
337 * changed since the last save.
339 * @param cfg configuration to inspect
340 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
343 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
350 * Serializes the given configuration.
352 * @param cfg configuration to serialize
353 * @param size will be set to the size of the serialized memory block
354 * @return the memory block where the serialized configuration is
355 * present. This memory should be freed by the caller
358 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
361 struct ConfigSection *sec;
362 struct ConfigEntry *ent;
372 /* Pass1 : calculate the buffer size required */
377 /* For each section we need to add 3 charaters: {'[',']','\n'} */
378 m_size += strlen (sec->name) + 3;
382 if (NULL != ent->val)
384 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
386 while (NULL != (pos = strstr (pos, "\n")))
391 /* For each key = value pair we need to add 4 characters (2
392 spaces and 1 equal-to character and 1 new line) */
393 m_size += strlen (ent->key) + strlen (ent->val) + 4;
397 /* A new line after section end */
402 /* Pass2: Allocate memory and write the configuration to it */
403 mem = GNUNET_malloc (m_size);
409 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
410 GNUNET_assert (0 < len);
411 memcpy (mem + c_size, cbuf, len);
417 if (NULL != ent->val)
419 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
420 strcpy (val, ent->val);
421 while (NULL != (pos = strstr (val, "\n")))
423 memmove (&pos[2], &pos[1], strlen (&pos[1]));
427 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
429 memcpy (mem + c_size, cbuf, len);
435 memcpy (mem + c_size, "\n", 1);
439 GNUNET_assert (c_size == m_size);
446 * Write configuration file.
448 * @param cfg configuration to write
449 * @param filename where to write the configuration
450 * @return GNUNET_OK on success, GNUNET_SYSERR on error
453 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
454 const char *filename)
460 fn = GNUNET_STRINGS_filename_expand (filename);
462 return GNUNET_SYSERR;
463 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
466 return GNUNET_SYSERR;
468 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
469 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
470 GNUNET_DISK_PERM_USER_READ
471 | GNUNET_DISK_PERM_USER_WRITE
472 | GNUNET_DISK_PERM_GROUP_READ
473 | GNUNET_DISK_PERM_GROUP_WRITE))
476 GNUNET_free (cfg_buf);
477 LOG (GNUNET_ERROR_TYPE_WARNING,
478 "Writing configration to file: %s failed\n", filename);
479 cfg->dirty = GNUNET_SYSERR; /* last write failed */
480 return GNUNET_SYSERR;
483 GNUNET_free (cfg_buf);
484 cfg->dirty = GNUNET_NO; /* last write succeeded */
490 * Iterate over all options in the configuration.
492 * @param cfg configuration to inspect
493 * @param iter function to call on each option
494 * @param iter_cls closure for iter
497 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
498 GNUNET_CONFIGURATION_Iterator iter,
501 struct ConfigSection *spos;
502 struct ConfigEntry *epos;
504 spos = cfg->sections;
507 epos = spos->entries;
510 iter (iter_cls, spos->name, epos->key, epos->val);
519 * Iterate over values of a section in the configuration.
521 * @param cfg configuration to inspect
522 * @param section the section
523 * @param iter function to call on each option
524 * @param iter_cls closure for iter
527 GNUNET_CONFIGURATION_iterate_section_values (const struct
528 GNUNET_CONFIGURATION_Handle *cfg,
530 GNUNET_CONFIGURATION_Iterator iter,
533 struct ConfigSection *spos;
534 struct ConfigEntry *epos;
536 spos = cfg->sections;
537 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
543 epos = spos->entries;
546 iter (iter_cls, spos->name, epos->key, epos->val);
553 * Iterate over all sections in the configuration.
555 * @param cfg configuration to inspect
556 * @param iter function to call on each section
557 * @param iter_cls closure for iter
560 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
562 GNUNET_CONFIGURATION_Section_Iterator
563 iter, void *iter_cls)
565 struct ConfigSection *spos;
566 struct ConfigSection *next;
568 next = cfg->sections;
573 iter (iter_cls, spos->name);
578 * Remove the given section and all options in it.
580 * @param cfg configuration to inspect
581 * @param section name of the section to remove
584 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
587 struct ConfigSection *spos;
588 struct ConfigSection *prev;
589 struct ConfigEntry *ent;
592 spos = cfg->sections;
595 if (0 == strcasecmp (section, spos->name))
598 cfg->sections = spos->next;
600 prev->next = spos->next;
601 while (NULL != (ent = spos->entries))
603 spos->entries = ent->next;
604 GNUNET_free (ent->key);
605 GNUNET_free_non_null (ent->val);
607 cfg->dirty = GNUNET_YES;
609 GNUNET_free (spos->name);
620 * Copy a configuration value to the given target configuration.
621 * Overwrites existing entries.
623 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
624 * @param section section for the value
625 * @param option option name of the value
626 * @param value value to copy
629 copy_entry (void *cls, const char *section, const char *option,
632 struct GNUNET_CONFIGURATION_Handle *dst = cls;
634 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
639 * Duplicate an existing configuration object.
641 * @param cfg configuration to duplicate
642 * @return duplicate configuration
644 struct GNUNET_CONFIGURATION_Handle *
645 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
647 struct GNUNET_CONFIGURATION_Handle *ret;
649 ret = GNUNET_CONFIGURATION_create ();
650 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
659 * @param section FIXME
660 * @return matching entry, NULL if not found
662 static struct ConfigSection *
663 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
665 struct ConfigSection *pos;
668 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
675 * Find an entry from a configuration.
677 * @param cfg handle to the configuration
678 * @param section section the option is in
679 * @param key the option
680 * @return matching entry, NULL if not found
682 static struct ConfigEntry *
683 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
686 struct ConfigSection *sec;
687 struct ConfigEntry *pos;
689 sec = findSection (cfg, section);
693 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
700 * A callback function, compares entries from two configurations
701 * (default against a new configuration) and write the diffs in a
702 * diff-configuration object (the callback object).
704 * @param cls the diff configuration (struct DiffHandle*)
705 * @param section section for the value (of the default conf.)
706 * @param option option name of the value (of the default conf.)
707 * @param value value to copy (of the default conf.)
710 compareEntries (void *cls, const char *section, const char *option,
713 struct DiffHandle *dh = cls;
714 struct ConfigEntry *entNew;
716 entNew = findEntry (dh->cfgDefault, section, option);
717 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
719 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
724 * Write only configuration entries that have been changed to configuration file
725 * @param cfgDefault default configuration
726 * @param cfgNew new configuration
727 * @param filename where to write the configuration diff between default and new
728 * @return GNUNET_OK on success, GNUNET_SYSERR on error
731 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
733 const struct GNUNET_CONFIGURATION_Handle
734 *cfgNew, const char *filename)
737 struct DiffHandle diffHandle;
739 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
740 diffHandle.cfgDefault = cfgDefault;
741 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
742 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
743 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
749 * Set a configuration value that should be a string.
751 * @param cfg configuration to update
752 * @param section section of interest
753 * @param option option of interest
754 * @param value value to set
757 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
758 const char *section, const char *option,
761 struct ConfigSection *sec;
762 struct ConfigEntry *e;
765 e = findEntry (cfg, section, option);
768 nv = GNUNET_strdup (value);
769 GNUNET_free_non_null (e->val);
773 sec = findSection (cfg, section);
776 sec = GNUNET_malloc (sizeof (struct ConfigSection));
777 sec->name = GNUNET_strdup (section);
778 sec->next = cfg->sections;
781 e = GNUNET_malloc (sizeof (struct ConfigEntry));
782 e->key = GNUNET_strdup (option);
783 e->val = GNUNET_strdup (value);
784 e->next = sec->entries;
790 * Set a configuration value that should be a number.
792 * @param cfg configuration to update
793 * @param section section of interest
794 * @param option option of interest
795 * @param number value to set
798 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
799 const char *section, const char *option,
800 unsigned long long number)
804 GNUNET_snprintf (s, 64, "%llu", number);
805 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
810 * Get a configuration value that should be a number.
812 * @param cfg configuration to inspect
813 * @param section section of interest
814 * @param option option of interest
815 * @param number where to store the numeric value of the option
816 * @return GNUNET_OK on success, GNUNET_SYSERR on error
819 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
820 *cfg, const char *section,
822 unsigned long long *number)
824 struct ConfigEntry *e;
826 e = findEntry (cfg, section, option);
828 return GNUNET_SYSERR;
829 if (1 != SSCANF (e->val, "%llu", number))
830 return GNUNET_SYSERR;
836 * Get a configuration value that should be a relative time.
838 * @param cfg configuration to inspect
839 * @param section section of interest
840 * @param option option of interest
841 * @param time set to the time value stored in the configuration
842 * @return GNUNET_OK on success, GNUNET_SYSERR on error
845 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
846 *cfg, const char *section,
848 struct GNUNET_TIME_Relative *time)
850 struct ConfigEntry *e;
852 e = findEntry (cfg, section, option);
854 return GNUNET_SYSERR;
856 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
861 * Get a configuration value that should be a size in bytes.
863 * @param cfg configuration to inspect
864 * @param section section of interest
865 * @param option option of interest
866 * @param size set to the size in bytes as stored in the configuration
867 * @return GNUNET_OK on success, GNUNET_SYSERR on error
870 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
871 *cfg, const char *section,
873 unsigned long long *size)
875 struct ConfigEntry *e;
877 e = findEntry (cfg, section, option);
879 return GNUNET_SYSERR;
880 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
885 * Get a configuration value that should be a string.
887 * @param cfg configuration to inspect
888 * @param section section of interest
889 * @param option option of interest
890 * @param value will be set to a freshly allocated configuration
891 * value, or NULL if option is not specified
892 * @return GNUNET_OK on success, GNUNET_SYSERR on error
895 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
896 *cfg, const char *section,
897 const char *option, char **value)
899 struct ConfigEntry *e;
901 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section);
902 e = findEntry (cfg, section, option);
903 if ((e == NULL) || (e->val == NULL))
905 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve the string\n");
907 return GNUNET_SYSERR;
909 *value = GNUNET_strdup (e->val);
910 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved string `%s'\n", e->val);
916 * Get a configuration value that should be in a set of
919 * @param cfg configuration to inspect
920 * @param section section of interest
921 * @param option option of interest
922 * @param choices NULL-terminated list of legal values
923 * @param value will be set to an entry in the legal list,
924 * or NULL if option is not specified and no default given
925 * @return GNUNET_OK on success, GNUNET_SYSERR on error
928 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
929 *cfg, const char *section,
930 const char *option, const char **choices,
933 struct ConfigEntry *e;
936 e = findEntry (cfg, section, option);
938 return GNUNET_SYSERR;
940 while (choices[i] != NULL)
942 if (0 == strcasecmp (choices[i], e->val))
946 if (choices[i] == NULL)
948 LOG (GNUNET_ERROR_TYPE_ERROR,
949 _("Configuration value '%s' for '%s'"
950 " in section '%s' is not in set of legal choices\n"), e->val, option,
952 return GNUNET_SYSERR;
960 * Test if we have a value for a particular option
961 * @param cfg configuration to inspect
962 * @param section section of interest
963 * @param option option of interest
964 * @return GNUNET_YES if so, GNUNET_NO if not.
967 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
968 const char *section, const char *option)
970 struct ConfigEntry *e;
972 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
979 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
980 * where either in the "PATHS" section or the environtment
981 * "FOO" is set to "DIRECTORY".
983 * @param cfg configuration to use for path expansion
984 * @param orig string to $-expand (will be freed!)
985 * @return $-expanded string
988 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
997 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1001 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1005 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
1007 if (orig[i] == '\0')
1014 post = &orig[i + 1];
1016 LOG (GNUNET_ERROR_TYPE_DEBUG, "Split into `%s' and `%s'\n", orig, post);
1018 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename for `%s' is not in PATHS config section\n", &orig[1]);
1021 if (NULL == (env = getenv (&orig[1])))
1023 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' is not an environment variable\n", &orig[1]);
1024 orig[i] = DIR_SEPARATOR;
1025 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s' (returning orig)\n", orig);
1028 prefix = GNUNET_strdup (env);
1030 LOG (GNUNET_ERROR_TYPE_DEBUG, "Prefix is `%s'\n", prefix);
1031 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1032 strcpy (result, prefix);
1033 if ((strlen (prefix) == 0) ||
1034 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
1035 strcat (result, DIR_SEPARATOR_STR);
1036 strcat (result, post);
1037 GNUNET_free (prefix);
1039 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s'\n", result);
1045 * Get a configuration value that should be a string.
1047 * @param cfg configuration to inspect
1048 * @param section section of interest
1049 * @param option option of interest
1050 * @param value will be set to a freshly allocated configuration
1051 * value, or NULL if option is not specified
1052 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1055 GNUNET_CONFIGURATION_get_value_filename (const struct
1056 GNUNET_CONFIGURATION_Handle *cfg,
1057 const char *section,
1058 const char *option, char **value)
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve filename `%s' in section `%s'\n", option, section);
1064 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1066 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1068 return GNUNET_SYSERR;
1070 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1071 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1073 *value = GNUNET_STRINGS_filename_expand (tmp);
1075 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1077 return GNUNET_SYSERR;
1083 * Get a configuration value that should be in a set of
1084 * "GNUNET_YES" or "GNUNET_NO".
1086 * @param cfg configuration to inspect
1087 * @param section section of interest
1088 * @param option option of interest
1089 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1092 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1093 *cfg, const char *section,
1096 static const char *yesno[] = { "YES", "NO", NULL };
1101 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1102 if (ret == GNUNET_SYSERR)
1104 if (val == yesno[0])
1111 * Iterate over the set of filenames stored in a configuration value.
1113 * @param cfg configuration to inspect
1114 * @param section section of interest
1115 * @param option option of interest
1116 * @param cb function to call on each filename
1117 * @param cb_cls closure for cb
1118 * @return number of filenames iterated over, -1 on error
1121 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1122 GNUNET_CONFIGURATION_Handle *cfg,
1123 const char *section,
1125 GNUNET_FileNameCallback cb,
1135 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1137 GNUNET_assert (list != NULL);
1142 while (pos[0] == ' ')
1144 if (strlen (pos) == 0)
1147 while ((end[0] != ' ') && (end[0] != '\0'))
1155 memmove (end, &end[1], strlen (&end[1]) + 1);
1157 /* illegal, but just keep it */
1160 /* illegal, but just ignore that there was a '/' */
1168 if (strlen (pos) > 0)
1171 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1173 ret = GNUNET_SYSERR;
1189 * @param value FIXME
1193 escape_name (const char *value)
1199 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1200 memset (escaped, 0, strlen (value) * 2 + 1);
1203 while (rpos[0] != '\0')
1226 * @param cls string we compare with (const char*)
1227 * @param fn filename we are currently looking at
1228 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1231 test_match (void *cls, const char *fn)
1233 const char *of = cls;
1235 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1240 * Append a filename to a configuration value that
1241 * represents a list of filenames
1243 * @param cfg configuration to update
1244 * @param section section of interest
1245 * @param option option of interest
1246 * @param value filename to append
1247 * @return GNUNET_OK on success,
1248 * GNUNET_NO if the filename already in the list
1249 * GNUNET_SYSERR on error
1252 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1253 *cfg, const char *section,
1261 if (GNUNET_SYSERR ==
1262 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1265 return GNUNET_NO; /* already exists */
1267 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1268 old = GNUNET_strdup ("");
1269 escaped = escape_name (value);
1270 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1272 if (strlen (old) > 0)
1274 strcat (nw, escaped);
1275 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1278 GNUNET_free (escaped);
1284 * Remove a filename from a configuration value that
1285 * represents a list of filenames
1287 * @param cfg configuration to update
1288 * @param section section of interest
1289 * @param option option of interest
1290 * @param value filename to remove
1291 * @return GNUNET_OK on success,
1292 * GNUNET_NO if the filename is not in the list,
1293 * GNUNET_SYSERR on error
1296 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1297 *cfg, const char *section,
1308 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1310 match = escape_name (value);
1314 while (pos[0] == ' ')
1316 if (strlen (pos) == 0)
1319 while ((end[0] != ' ') && (end[0] != '\0'))
1330 /* illegal, but just keep it */
1333 /* illegal, but just ignore that there was a '/' */
1341 if (0 == strcmp (pos, match))
1344 memmove (pos, &end[1], strlen (&end[1]) + 1);
1352 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1354 GNUNET_free (match);
1363 GNUNET_free (match);
1369 * Wrapper around GNUNET_CONFIGURATION_parse.
1371 * @param cls the cfg
1372 * @param filename file to parse
1373 * @return GNUNET_OK on success
1376 parse_configuration_file (void *cls, const char *filename)
1378 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1382 /* Examine file extension */
1383 ext = strrchr (filename, '.');
1384 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1386 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1390 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1396 * Load default configuration. This function will parse the
1397 * defaults from the given defaults_d directory.
1399 * @param cfg configuration to update
1400 * @param defaults_d directory with the defaults
1401 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1404 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1405 const char *defaults_d)
1407 if (GNUNET_SYSERR ==
1408 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1409 return GNUNET_SYSERR; /* no configuration at all found */
1415 * Load configuration (starts with defaults, then loads
1416 * system-specific configuration).
1418 * @param cfg configuration to update
1419 * @param filename name of the configuration file, NULL to load defaults
1420 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1423 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1424 const char *filename)
1429 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1431 return GNUNET_SYSERR;
1433 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1434 GNUNET_free (ipath);
1435 if (GNUNET_SYSERR ==
1436 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1438 GNUNET_free (baseconfig);
1439 return GNUNET_SYSERR; /* no configuration at all found */
1441 GNUNET_free (baseconfig);
1442 if ((filename != NULL) &&
1443 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1445 /* specified configuration not found */
1446 return GNUNET_SYSERR;
1449 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1451 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1454 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1456 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1457 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1463 /* end of configuration.c */