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,
172 section = GNUNET_strdup ("");
175 memset (line, 0, 256);
176 while (r_bytes < size)
178 /* fgets-like behaviour on buffer. read size is 255 bytes */
179 to_read = size - r_bytes;
182 memcpy (line, mem + r_bytes, to_read);
183 line[to_read] = '\0';
184 if (NULL != (pos = strstr (line, "\n")))
187 r_bytes += (pos - line) + 1;
191 /* fgets-like behaviour end */
193 for (i = 0; i < 255; i++)
196 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' || line[0] == '\r')
199 for (i = 0; (i < 255 && line[i] != 0); i++)
200 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
204 /* remove tailing whitespace */
205 for (i = strlen (line) - 1; (i >= 0) && (isspace ((unsigned char) line[i]));
208 if (1 == SSCANF (line, "@INLINE@ %191[^\n]", value))
211 if (GNUNET_YES == allow_inline)
213 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
214 ret = GNUNET_SYSERR; /* failed to parse included config */
217 LOG (GNUNET_ERROR_TYPE_DEBUG,
218 "Ignoring parsing INLINE configurations as allow_inline is false\n");
220 else if (1 == SSCANF (line, "[%99[^]]]", value))
223 GNUNET_free (section);
224 section = GNUNET_strdup (value);
226 else if (2 == SSCANF (line, " %63[^= ] = %191[^\n]", tag, value))
230 i = strlen (value) - 1;
231 while ((i >= 0) && (isspace ((unsigned char) value[i])))
238 while ((value[i] != '\0') && (value[i] != '"'))
248 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
250 else if (1 == SSCANF (line, " %63[^= ] =[^\n]", tag))
253 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
258 LOG (GNUNET_ERROR_TYPE_WARNING,
259 _("Syntax error while deserializing operation at line %u\n"), nr);
264 GNUNET_free (section);
265 GNUNET_assert (r_bytes == size);
271 * Parse a configuration file, add all of the options in the
272 * file to the configuration environment.
274 * @param cfg configuration to update
275 * @param filename name of the configuration file
276 * @return GNUNET_OK on success, GNUNET_SYSERR on error
279 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
280 const char *filename)
289 fn = GNUNET_STRINGS_filename_expand (filename);
291 return GNUNET_SYSERR;
292 dirty = cfg->dirty; /* back up value! */
294 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
296 LOG (GNUNET_ERROR_TYPE_WARNING,
297 "Error while determining the file size of %s\n", fn);
299 return GNUNET_SYSERR;
303 GNUNET_break (0); /* File size is more than the heap size */
305 return GNUNET_SYSERR;
308 mem = GNUNET_malloc (fs);
309 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
311 LOG (GNUNET_ERROR_TYPE_WARNING,
312 "Error while reading file %s\n", fn);
315 return GNUNET_SYSERR;
318 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
320 /* restore dirty flag - anything we set in the meantime
328 * Test if there are configuration options that were
329 * changed since the last save.
331 * @param cfg configuration to inspect
332 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
335 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
342 * Serializes the given configuration.
344 * @param cfg configuration to serialize
345 * @param size will be set to the size of the serialized memory block
346 * @return the memory block where the serialized configuration is
347 * present. This memory should be freed by the caller
350 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
353 struct ConfigSection *sec;
354 struct ConfigEntry *ent;
364 /* Pass1 : calculate the buffer size required */
369 /* For each section we need to add 3 charaters: {'[',']','\n'} */
370 m_size += strlen (sec->name) + 3;
374 if (NULL != ent->val)
376 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
378 while (NULL != (pos = strstr (pos, "\n")))
383 /* For each key = value pair we need to add 4 characters (2
384 spaces and 1 equal-to character and 1 new line) */
385 m_size += strlen (ent->key) + strlen (ent->val) + 4;
389 /* A new line after section end */
394 /* Pass2: Allocate memory and write the configuration to it */
395 mem = GNUNET_malloc (m_size);
401 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
402 GNUNET_assert (0 < len);
403 memcpy (mem + c_size, cbuf, len);
409 if (NULL != ent->val)
411 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
412 strcpy (val, ent->val);
413 while (NULL != (pos = strstr (val, "\n")))
415 memmove (&pos[2], &pos[1], strlen (&pos[1]));
419 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
421 memcpy (mem + c_size, cbuf, len);
427 memcpy (mem + c_size, "\n", 1);
431 GNUNET_assert (c_size == m_size);
438 * Write configuration file.
440 * @param cfg configuration to write
441 * @param filename where to write the configuration
442 * @return GNUNET_OK on success, GNUNET_SYSERR on error
445 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
446 const char *filename)
452 fn = GNUNET_STRINGS_filename_expand (filename);
454 return GNUNET_SYSERR;
455 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
458 return GNUNET_SYSERR;
460 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
461 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
462 GNUNET_DISK_PERM_USER_READ
463 | GNUNET_DISK_PERM_USER_WRITE
464 | GNUNET_DISK_PERM_GROUP_READ
465 | GNUNET_DISK_PERM_GROUP_WRITE))
468 GNUNET_free (cfg_buf);
469 LOG (GNUNET_ERROR_TYPE_WARNING,
470 "Writing configration to file: %s failed\n", filename);
471 cfg->dirty = GNUNET_SYSERR; /* last write failed */
472 return GNUNET_SYSERR;
475 GNUNET_free (cfg_buf);
476 cfg->dirty = GNUNET_NO; /* last write succeeded */
482 * Iterate over all options in the configuration.
484 * @param cfg configuration to inspect
485 * @param iter function to call on each option
486 * @param iter_cls closure for iter
489 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
490 GNUNET_CONFIGURATION_Iterator iter,
493 struct ConfigSection *spos;
494 struct ConfigEntry *epos;
496 spos = cfg->sections;
499 epos = spos->entries;
502 iter (iter_cls, spos->name, epos->key, epos->val);
511 * Iterate over values of a section in the configuration.
513 * @param cfg configuration to inspect
514 * @param section the section
515 * @param iter function to call on each option
516 * @param iter_cls closure for iter
519 GNUNET_CONFIGURATION_iterate_section_values (const struct
520 GNUNET_CONFIGURATION_Handle *cfg,
522 GNUNET_CONFIGURATION_Iterator iter,
525 struct ConfigSection *spos;
526 struct ConfigEntry *epos;
528 spos = cfg->sections;
529 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
535 epos = spos->entries;
538 iter (iter_cls, spos->name, epos->key, epos->val);
545 * Iterate over all sections in the configuration.
547 * @param cfg configuration to inspect
548 * @param iter function to call on each section
549 * @param iter_cls closure for iter
552 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
554 GNUNET_CONFIGURATION_Section_Iterator
555 iter, void *iter_cls)
557 struct ConfigSection *spos;
558 struct ConfigSection *next;
560 next = cfg->sections;
565 iter (iter_cls, spos->name);
570 * Remove the given section and all options in it.
572 * @param cfg configuration to inspect
573 * @param section name of the section to remove
576 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
579 struct ConfigSection *spos;
580 struct ConfigSection *prev;
581 struct ConfigEntry *ent;
584 spos = cfg->sections;
587 if (0 == strcasecmp (section, spos->name))
590 cfg->sections = spos->next;
592 prev->next = spos->next;
593 while (NULL != (ent = spos->entries))
595 spos->entries = ent->next;
596 GNUNET_free (ent->key);
597 GNUNET_free_non_null (ent->val);
599 cfg->dirty = GNUNET_YES;
601 GNUNET_free (spos->name);
612 * Copy a configuration value to the given target configuration.
613 * Overwrites existing entries.
615 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
616 * @param section section for the value
617 * @param option option name of the value
618 * @param value value to copy
621 copy_entry (void *cls, const char *section, const char *option,
624 struct GNUNET_CONFIGURATION_Handle *dst = cls;
626 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
631 * Duplicate an existing configuration object.
633 * @param cfg configuration to duplicate
634 * @return duplicate configuration
636 struct GNUNET_CONFIGURATION_Handle *
637 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
639 struct GNUNET_CONFIGURATION_Handle *ret;
641 ret = GNUNET_CONFIGURATION_create ();
642 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
651 * @param section FIXME
652 * @return matching entry, NULL if not found
654 static struct ConfigSection *
655 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
657 struct ConfigSection *pos;
660 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
667 * Find an entry from a configuration.
669 * @param cfg handle to the configuration
670 * @param section section the option is in
671 * @param key the option
672 * @return matching entry, NULL if not found
674 static struct ConfigEntry *
675 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
678 struct ConfigSection *sec;
679 struct ConfigEntry *pos;
681 sec = findSection (cfg, section);
685 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
692 * A callback function, compares entries from two configurations
693 * (default against a new configuration) and write the diffs in a
694 * diff-configuration object (the callback object).
696 * @param cls the diff configuration (struct DiffHandle*)
697 * @param section section for the value (of the default conf.)
698 * @param option option name of the value (of the default conf.)
699 * @param value value to copy (of the default conf.)
702 compareEntries (void *cls, const char *section, const char *option,
705 struct DiffHandle *dh = cls;
706 struct ConfigEntry *entNew;
708 entNew = findEntry (dh->cfgDefault, section, option);
709 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
711 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
716 * Write only configuration entries that have been changed to configuration file
717 * @param cfgDefault default configuration
718 * @param cfgNew new configuration
719 * @param filename where to write the configuration diff between default and new
720 * @return GNUNET_OK on success, GNUNET_SYSERR on error
723 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
725 const struct GNUNET_CONFIGURATION_Handle
726 *cfgNew, const char *filename)
729 struct DiffHandle diffHandle;
731 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
732 diffHandle.cfgDefault = cfgDefault;
733 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
734 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
735 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
741 * Set a configuration value that should be a string.
743 * @param cfg configuration to update
744 * @param section section of interest
745 * @param option option of interest
746 * @param value value to set
749 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
750 const char *section, const char *option,
753 struct ConfigSection *sec;
754 struct ConfigEntry *e;
757 e = findEntry (cfg, section, option);
760 nv = GNUNET_strdup (value);
761 GNUNET_free_non_null (e->val);
765 sec = findSection (cfg, section);
768 sec = GNUNET_malloc (sizeof (struct ConfigSection));
769 sec->name = GNUNET_strdup (section);
770 sec->next = cfg->sections;
773 e = GNUNET_malloc (sizeof (struct ConfigEntry));
774 e->key = GNUNET_strdup (option);
775 e->val = GNUNET_strdup (value);
776 e->next = sec->entries;
782 * Set a configuration value that should be a number.
784 * @param cfg configuration to update
785 * @param section section of interest
786 * @param option option of interest
787 * @param number value to set
790 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
791 const char *section, const char *option,
792 unsigned long long number)
796 GNUNET_snprintf (s, 64, "%llu", number);
797 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
802 * Get a configuration value that should be a number.
804 * @param cfg configuration to inspect
805 * @param section section of interest
806 * @param option option of interest
807 * @param number where to store the numeric value of the option
808 * @return GNUNET_OK on success, GNUNET_SYSERR on error
811 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
812 *cfg, const char *section,
814 unsigned long long *number)
816 struct ConfigEntry *e;
818 e = findEntry (cfg, section, option);
820 return GNUNET_SYSERR;
821 if (1 != SSCANF (e->val, "%llu", number))
822 return GNUNET_SYSERR;
828 * Get a configuration value that should be a relative time.
830 * @param cfg configuration to inspect
831 * @param section section of interest
832 * @param option option of interest
833 * @param time set to the time value stored in the configuration
834 * @return GNUNET_OK on success, GNUNET_SYSERR on error
837 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
838 *cfg, const char *section,
840 struct GNUNET_TIME_Relative *time)
842 struct ConfigEntry *e;
844 e = findEntry (cfg, section, option);
846 return GNUNET_SYSERR;
848 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
853 * Get a configuration value that should be a size in bytes.
855 * @param cfg configuration to inspect
856 * @param section section of interest
857 * @param option option of interest
858 * @param size set to the size in bytes as stored in the configuration
859 * @return GNUNET_OK on success, GNUNET_SYSERR on error
862 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
863 *cfg, const char *section,
865 unsigned long long *size)
867 struct ConfigEntry *e;
869 e = findEntry (cfg, section, option);
871 return GNUNET_SYSERR;
872 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
877 * Get a configuration value that should be a string.
879 * @param cfg configuration to inspect
880 * @param section section of interest
881 * @param option option of interest
882 * @param value will be set to a freshly allocated configuration
883 * value, or NULL if option is not specified
884 * @return GNUNET_OK on success, GNUNET_SYSERR on error
887 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
888 *cfg, const char *section,
889 const char *option, char **value)
891 struct ConfigEntry *e;
893 e = findEntry (cfg, section, option);
894 if ((e == NULL) || (e->val == NULL))
897 return GNUNET_SYSERR;
899 *value = GNUNET_strdup (e->val);
905 * Get a configuration value that should be in a set of
908 * @param cfg configuration to inspect
909 * @param section section of interest
910 * @param option option of interest
911 * @param choices NULL-terminated list of legal values
912 * @param value will be set to an entry in the legal list,
913 * or NULL if option is not specified and no default given
914 * @return GNUNET_OK on success, GNUNET_SYSERR on error
917 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
918 *cfg, const char *section,
919 const char *option, const char **choices,
922 struct ConfigEntry *e;
925 e = findEntry (cfg, section, option);
927 return GNUNET_SYSERR;
929 while (choices[i] != NULL)
931 if (0 == strcasecmp (choices[i], e->val))
935 if (choices[i] == NULL)
937 LOG (GNUNET_ERROR_TYPE_ERROR,
938 _("Configuration value '%s' for '%s'"
939 " in section '%s' is not in set of legal choices\n"), e->val, option,
941 return GNUNET_SYSERR;
949 * Test if we have a value for a particular option
950 * @param cfg configuration to inspect
951 * @param section section of interest
952 * @param option option of interest
953 * @return GNUNET_YES if so, GNUNET_NO if not.
956 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
957 const char *section, const char *option)
959 struct ConfigEntry *e;
961 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
968 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
969 * where either in the "PATHS" section or the environtment
970 * "FOO" is set to "DIRECTORY".
972 * @param cfg configuration to use for path expansion
973 * @param orig string to $-expand (will be freed!)
974 * @return $-expanded string
977 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
989 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
1001 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
1003 if (NULL == (env = getenv (&orig[1])))
1005 orig[i] = DIR_SEPARATOR;
1008 prefix = GNUNET_strdup (env);
1010 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1011 strcpy (result, prefix);
1012 if ((strlen (prefix) == 0) ||
1013 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
1014 strcat (result, DIR_SEPARATOR_STR);
1015 strcat (result, post);
1016 GNUNET_free (prefix);
1023 * Get a configuration value that should be a string.
1025 * @param cfg configuration to inspect
1026 * @param section section of interest
1027 * @param option option of interest
1028 * @param value will be set to a freshly allocated configuration
1029 * value, or NULL if option is not specified
1030 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1033 GNUNET_CONFIGURATION_get_value_filename (const struct
1034 GNUNET_CONFIGURATION_Handle *cfg,
1035 const char *section,
1036 const char *option, char **value)
1041 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1044 return GNUNET_SYSERR;
1046 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1047 *value = GNUNET_STRINGS_filename_expand (tmp);
1050 return GNUNET_SYSERR;
1056 * Get a configuration value that should be in a set of
1057 * "GNUNET_YES" or "GNUNET_NO".
1059 * @param cfg configuration to inspect
1060 * @param section section of interest
1061 * @param option option of interest
1062 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1065 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1066 *cfg, const char *section,
1069 static const char *yesno[] = { "YES", "NO", NULL };
1074 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1075 if (ret == GNUNET_SYSERR)
1077 if (val == yesno[0])
1084 * Iterate over the set of filenames stored in a configuration value.
1086 * @param cfg configuration to inspect
1087 * @param section section of interest
1088 * @param option option of interest
1089 * @param cb function to call on each filename
1090 * @param cb_cls closure for cb
1091 * @return number of filenames iterated over, -1 on error
1094 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1095 GNUNET_CONFIGURATION_Handle *cfg,
1096 const char *section,
1098 GNUNET_FileNameCallback cb,
1108 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1110 GNUNET_assert (list != NULL);
1115 while (pos[0] == ' ')
1117 if (strlen (pos) == 0)
1120 while ((end[0] != ' ') && (end[0] != '\0'))
1128 memmove (end, &end[1], strlen (&end[1]) + 1);
1130 /* illegal, but just keep it */
1133 /* illegal, but just ignore that there was a '/' */
1141 if (strlen (pos) > 0)
1144 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1146 ret = GNUNET_SYSERR;
1162 * @param value FIXME
1166 escape_name (const char *value)
1172 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1173 memset (escaped, 0, strlen (value) * 2 + 1);
1176 while (rpos[0] != '\0')
1199 * @param cls string we compare with (const char*)
1200 * @param fn filename we are currently looking at
1201 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1204 test_match (void *cls, const char *fn)
1206 const char *of = cls;
1208 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1213 * Append a filename to a configuration value that
1214 * represents a list of filenames
1216 * @param cfg configuration to update
1217 * @param section section of interest
1218 * @param option option of interest
1219 * @param value filename to append
1220 * @return GNUNET_OK on success,
1221 * GNUNET_NO if the filename already in the list
1222 * GNUNET_SYSERR on error
1225 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1226 *cfg, const char *section,
1234 if (GNUNET_SYSERR ==
1235 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1238 return GNUNET_NO; /* already exists */
1240 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1241 old = GNUNET_strdup ("");
1242 escaped = escape_name (value);
1243 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1245 if (strlen (old) > 0)
1247 strcat (nw, escaped);
1248 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1251 GNUNET_free (escaped);
1257 * Remove a filename from a configuration value that
1258 * represents a list of filenames
1260 * @param cfg configuration to update
1261 * @param section section of interest
1262 * @param option option of interest
1263 * @param value filename to remove
1264 * @return GNUNET_OK on success,
1265 * GNUNET_NO if the filename is not in the list,
1266 * GNUNET_SYSERR on error
1269 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1270 *cfg, const char *section,
1281 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1283 match = escape_name (value);
1287 while (pos[0] == ' ')
1289 if (strlen (pos) == 0)
1292 while ((end[0] != ' ') && (end[0] != '\0'))
1303 /* illegal, but just keep it */
1306 /* illegal, but just ignore that there was a '/' */
1314 if (0 == strcmp (pos, match))
1317 memmove (pos, &end[1], strlen (&end[1]) + 1);
1325 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1327 GNUNET_free (match);
1336 GNUNET_free (match);
1342 * Wrapper around GNUNET_CONFIGURATION_parse.
1344 * @param cls the cfg
1345 * @param filename file to parse
1346 * @return GNUNET_OK on success
1349 parse_configuration_file (void *cls, const char *filename)
1351 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1355 /* Examine file extension */
1356 ext = strrchr (filename, '.');
1357 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1363 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1369 * Load default configuration. This function will parse the
1370 * defaults from the given defaults_d directory.
1372 * @param cfg configuration to update
1373 * @param defaults_d directory with the defaults
1374 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1377 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1378 const char *defaults_d)
1380 if (GNUNET_SYSERR ==
1381 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1382 return GNUNET_SYSERR; /* no configuration at all found */
1388 * Load configuration (starts with defaults, then loads
1389 * system-specific configuration).
1391 * @param cfg configuration to update
1392 * @param filename name of the configuration file, NULL to load defaults
1393 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1396 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1397 const char *filename)
1402 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1404 return GNUNET_SYSERR;
1406 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1407 GNUNET_free (ipath);
1408 if (GNUNET_SYSERR ==
1409 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1411 GNUNET_free (baseconfig);
1412 return GNUNET_SYSERR; /* no configuration at all found */
1414 GNUNET_free (baseconfig);
1415 if ((filename != NULL) &&
1416 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1418 /* specified configuration not found */
1419 return GNUNET_SYSERR;
1422 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1424 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1427 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1429 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1430 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1436 /* end of configuration.c */