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 (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 memcpy (mem + c_size, cbuf, len);
408 if (NULL != ent->val)
410 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
411 strcpy (val, ent->val);
412 while (NULL != (pos = strstr (val, "\n")))
414 memmove (&pos[2], &pos[1], strlen (&pos[1]));
418 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
420 memcpy (mem + c_size, cbuf, len);
426 memcpy (mem + c_size, "\n", 1);
430 GNUNET_assert (c_size == m_size);
437 * Write configuration file.
439 * @param cfg configuration to write
440 * @param filename where to write the configuration
441 * @return GNUNET_OK on success, GNUNET_SYSERR on error
444 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
445 const char *filename)
451 fn = GNUNET_STRINGS_filename_expand (filename);
453 return GNUNET_SYSERR;
454 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
457 return GNUNET_SYSERR;
459 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
460 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
461 GNUNET_DISK_PERM_USER_READ
462 | GNUNET_DISK_PERM_USER_WRITE
463 | GNUNET_DISK_PERM_GROUP_READ
464 | GNUNET_DISK_PERM_GROUP_WRITE))
467 GNUNET_free (cfg_buf);
468 LOG (GNUNET_ERROR_TYPE_WARNING,
469 "Writing configration to file: %s failed\n", filename);
470 cfg->dirty = GNUNET_SYSERR; /* last write failed */
471 return GNUNET_SYSERR;
474 GNUNET_free (cfg_buf);
475 cfg->dirty = GNUNET_NO; /* last write succeeded */
481 * Iterate over all options in the configuration.
483 * @param cfg configuration to inspect
484 * @param iter function to call on each option
485 * @param iter_cls closure for iter
488 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
489 GNUNET_CONFIGURATION_Iterator iter,
492 struct ConfigSection *spos;
493 struct ConfigEntry *epos;
495 spos = cfg->sections;
498 epos = spos->entries;
501 iter (iter_cls, spos->name, epos->key, epos->val);
510 * Iterate over values of a section in the configuration.
512 * @param cfg configuration to inspect
513 * @param section the section
514 * @param iter function to call on each option
515 * @param iter_cls closure for iter
518 GNUNET_CONFIGURATION_iterate_section_values (const struct
519 GNUNET_CONFIGURATION_Handle *cfg,
521 GNUNET_CONFIGURATION_Iterator iter,
524 struct ConfigSection *spos;
525 struct ConfigEntry *epos;
527 spos = cfg->sections;
528 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
534 epos = spos->entries;
537 iter (iter_cls, spos->name, epos->key, epos->val);
544 * Iterate over all sections in the configuration.
546 * @param cfg configuration to inspect
547 * @param iter function to call on each section
548 * @param iter_cls closure for iter
551 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
553 GNUNET_CONFIGURATION_Section_Iterator
554 iter, void *iter_cls)
556 struct ConfigSection *spos;
557 struct ConfigSection *next;
559 next = cfg->sections;
564 iter (iter_cls, spos->name);
569 * Remove the given section and all options in it.
571 * @param cfg configuration to inspect
572 * @param section name of the section to remove
575 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
578 struct ConfigSection *spos;
579 struct ConfigSection *prev;
580 struct ConfigEntry *ent;
583 spos = cfg->sections;
586 if (0 == strcasecmp (section, spos->name))
589 cfg->sections = spos->next;
591 prev->next = spos->next;
592 while (NULL != (ent = spos->entries))
594 spos->entries = ent->next;
595 GNUNET_free (ent->key);
596 GNUNET_free_non_null (ent->val);
598 cfg->dirty = GNUNET_YES;
600 GNUNET_free (spos->name);
611 * Copy a configuration value to the given target configuration.
612 * Overwrites existing entries.
614 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
615 * @param section section for the value
616 * @param option option name of the value
617 * @param value value to copy
620 copy_entry (void *cls, const char *section, const char *option,
623 struct GNUNET_CONFIGURATION_Handle *dst = cls;
625 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
630 * Duplicate an existing configuration object.
632 * @param cfg configuration to duplicate
633 * @return duplicate configuration
635 struct GNUNET_CONFIGURATION_Handle *
636 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
638 struct GNUNET_CONFIGURATION_Handle *ret;
640 ret = GNUNET_CONFIGURATION_create ();
641 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
650 * @param section FIXME
651 * @return matching entry, NULL if not found
653 static struct ConfigSection *
654 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
656 struct ConfigSection *pos;
659 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
666 * Find an entry from a configuration.
668 * @param cfg handle to the configuration
669 * @param section section the option is in
670 * @param key the option
671 * @return matching entry, NULL if not found
673 static struct ConfigEntry *
674 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
677 struct ConfigSection *sec;
678 struct ConfigEntry *pos;
680 sec = findSection (cfg, section);
684 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
691 * A callback function, compares entries from two configurations
692 * (default against a new configuration) and write the diffs in a
693 * diff-configuration object (the callback object).
695 * @param cls the diff configuration (struct DiffHandle*)
696 * @param section section for the value (of the default conf.)
697 * @param option option name of the value (of the default conf.)
698 * @param value value to copy (of the default conf.)
701 compareEntries (void *cls, const char *section, const char *option,
704 struct DiffHandle *dh = cls;
705 struct ConfigEntry *entNew;
707 entNew = findEntry (dh->cfgDefault, section, option);
708 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
710 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
715 * Write only configuration entries that have been changed to configuration file
716 * @param cfgDefault default configuration
717 * @param cfgNew new configuration
718 * @param filename where to write the configuration diff between default and new
719 * @return GNUNET_OK on success, GNUNET_SYSERR on error
722 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
724 const struct GNUNET_CONFIGURATION_Handle
725 *cfgNew, const char *filename)
728 struct DiffHandle diffHandle;
730 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
731 diffHandle.cfgDefault = cfgDefault;
732 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
733 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
734 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
740 * Set a configuration value that should be a string.
742 * @param cfg configuration to update
743 * @param section section of interest
744 * @param option option of interest
745 * @param value value to set
748 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
749 const char *section, const char *option,
752 struct ConfigSection *sec;
753 struct ConfigEntry *e;
756 e = findEntry (cfg, section, option);
759 nv = GNUNET_strdup (value);
760 GNUNET_free_non_null (e->val);
764 sec = findSection (cfg, section);
767 sec = GNUNET_malloc (sizeof (struct ConfigSection));
768 sec->name = GNUNET_strdup (section);
769 sec->next = cfg->sections;
772 e = GNUNET_malloc (sizeof (struct ConfigEntry));
773 e->key = GNUNET_strdup (option);
774 e->val = GNUNET_strdup (value);
775 e->next = sec->entries;
781 * Set a configuration value that should be a number.
783 * @param cfg configuration to update
784 * @param section section of interest
785 * @param option option of interest
786 * @param number value to set
789 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
790 const char *section, const char *option,
791 unsigned long long number)
795 GNUNET_snprintf (s, 64, "%llu", number);
796 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
801 * Get a configuration value that should be a number.
803 * @param cfg configuration to inspect
804 * @param section section of interest
805 * @param option option of interest
806 * @param number where to store the numeric value of the option
807 * @return GNUNET_OK on success, GNUNET_SYSERR on error
810 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
811 *cfg, const char *section,
813 unsigned long long *number)
815 struct ConfigEntry *e;
817 e = findEntry (cfg, section, option);
819 return GNUNET_SYSERR;
820 if (1 != SSCANF (e->val, "%llu", number))
821 return GNUNET_SYSERR;
827 * Get a configuration value that should be a relative time.
829 * @param cfg configuration to inspect
830 * @param section section of interest
831 * @param option option of interest
832 * @param time set to the time value stored in the configuration
833 * @return GNUNET_OK on success, GNUNET_SYSERR on error
836 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
837 *cfg, const char *section,
839 struct GNUNET_TIME_Relative *time)
841 struct ConfigEntry *e;
843 e = findEntry (cfg, section, option);
845 return GNUNET_SYSERR;
847 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
852 * Get a configuration value that should be a size in bytes.
854 * @param cfg configuration to inspect
855 * @param section section of interest
856 * @param option option of interest
857 * @param size set to the size in bytes as stored in the configuration
858 * @return GNUNET_OK on success, GNUNET_SYSERR on error
861 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
862 *cfg, const char *section,
864 unsigned long long *size)
866 struct ConfigEntry *e;
868 e = findEntry (cfg, section, option);
870 return GNUNET_SYSERR;
871 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
876 * Get a configuration value that should be a string.
878 * @param cfg configuration to inspect
879 * @param section section of interest
880 * @param option option of interest
881 * @param value will be set to a freshly allocated configuration
882 * value, or NULL if option is not specified
883 * @return GNUNET_OK on success, GNUNET_SYSERR on error
886 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
887 *cfg, const char *section,
888 const char *option, char **value)
890 struct ConfigEntry *e;
892 e = findEntry (cfg, section, option);
893 if ((e == NULL) || (e->val == NULL))
896 return GNUNET_SYSERR;
898 *value = GNUNET_strdup (e->val);
904 * Get a configuration value that should be in a set of
907 * @param cfg configuration to inspect
908 * @param section section of interest
909 * @param option option of interest
910 * @param choices NULL-terminated list of legal values
911 * @param value will be set to an entry in the legal list,
912 * or NULL if option is not specified and no default given
913 * @return GNUNET_OK on success, GNUNET_SYSERR on error
916 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
917 *cfg, const char *section,
918 const char *option, const char **choices,
921 struct ConfigEntry *e;
924 e = findEntry (cfg, section, option);
926 return GNUNET_SYSERR;
928 while (choices[i] != NULL)
930 if (0 == strcasecmp (choices[i], e->val))
934 if (choices[i] == NULL)
936 LOG (GNUNET_ERROR_TYPE_ERROR,
937 _("Configuration value '%s' for '%s'"
938 " in section '%s' is not in set of legal choices\n"), e->val, option,
940 return GNUNET_SYSERR;
948 * Test if we have a value for a particular option
949 * @param cfg configuration to inspect
950 * @param section section of interest
951 * @param option option of interest
952 * @return GNUNET_YES if so, GNUNET_NO if not.
955 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
956 const char *section, const char *option)
958 struct ConfigEntry *e;
960 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
967 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
968 * where either in the "PATHS" section or the environtment
969 * "FOO" is set to "DIRECTORY".
971 * @param cfg configuration to use for path expansion
972 * @param orig string to $-expand (will be freed!)
973 * @return $-expanded string
976 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
988 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
1000 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
1002 if (NULL == (env = getenv (&orig[1])))
1004 orig[i] = DIR_SEPARATOR;
1007 prefix = GNUNET_strdup (env);
1009 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1010 strcpy (result, prefix);
1011 if ((strlen (prefix) == 0) ||
1012 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
1013 strcat (result, DIR_SEPARATOR_STR);
1014 strcat (result, post);
1015 GNUNET_free (prefix);
1022 * Get a configuration value that should be a string.
1024 * @param cfg configuration to inspect
1025 * @param section section of interest
1026 * @param option option of interest
1027 * @param value will be set to a freshly allocated configuration
1028 * value, or NULL if option is not specified
1029 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1032 GNUNET_CONFIGURATION_get_value_filename (const struct
1033 GNUNET_CONFIGURATION_Handle *cfg,
1034 const char *section,
1035 const char *option, char **value)
1040 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1043 return GNUNET_SYSERR;
1045 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1046 *value = GNUNET_STRINGS_filename_expand (tmp);
1049 return GNUNET_SYSERR;
1055 * Get a configuration value that should be in a set of
1056 * "GNUNET_YES" or "GNUNET_NO".
1058 * @param cfg configuration to inspect
1059 * @param section section of interest
1060 * @param option option of interest
1061 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1064 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1065 *cfg, const char *section,
1068 static const char *yesno[] = { "YES", "NO", NULL };
1073 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1074 if (ret == GNUNET_SYSERR)
1076 if (val == yesno[0])
1083 * Iterate over the set of filenames stored in a configuration value.
1085 * @param cfg configuration to inspect
1086 * @param section section of interest
1087 * @param option option of interest
1088 * @param cb function to call on each filename
1089 * @param cb_cls closure for cb
1090 * @return number of filenames iterated over, -1 on error
1093 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1094 GNUNET_CONFIGURATION_Handle *cfg,
1095 const char *section,
1097 GNUNET_FileNameCallback cb,
1107 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1109 GNUNET_assert (list != NULL);
1114 while (pos[0] == ' ')
1116 if (strlen (pos) == 0)
1119 while ((end[0] != ' ') && (end[0] != '\0'))
1127 memmove (end, &end[1], strlen (&end[1]) + 1);
1129 /* illegal, but just keep it */
1132 /* illegal, but just ignore that there was a '/' */
1140 if (strlen (pos) > 0)
1143 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1145 ret = GNUNET_SYSERR;
1161 * @param value FIXME
1165 escape_name (const char *value)
1171 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1172 memset (escaped, 0, strlen (value) * 2 + 1);
1175 while (rpos[0] != '\0')
1198 * @param cls string we compare with (const char*)
1199 * @param fn filename we are currently looking at
1200 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1203 test_match (void *cls, const char *fn)
1205 const char *of = cls;
1207 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1212 * Append a filename to a configuration value that
1213 * represents a list of filenames
1215 * @param cfg configuration to update
1216 * @param section section of interest
1217 * @param option option of interest
1218 * @param value filename to append
1219 * @return GNUNET_OK on success,
1220 * GNUNET_NO if the filename already in the list
1221 * GNUNET_SYSERR on error
1224 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1225 *cfg, const char *section,
1233 if (GNUNET_SYSERR ==
1234 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1237 return GNUNET_NO; /* already exists */
1239 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1240 old = GNUNET_strdup ("");
1241 escaped = escape_name (value);
1242 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1244 if (strlen (old) > 0)
1246 strcat (nw, escaped);
1247 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1250 GNUNET_free (escaped);
1256 * Remove a filename from a configuration value that
1257 * represents a list of filenames
1259 * @param cfg configuration to update
1260 * @param section section of interest
1261 * @param option option of interest
1262 * @param value filename to remove
1263 * @return GNUNET_OK on success,
1264 * GNUNET_NO if the filename is not in the list,
1265 * GNUNET_SYSERR on error
1268 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1269 *cfg, const char *section,
1280 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1282 match = escape_name (value);
1286 while (pos[0] == ' ')
1288 if (strlen (pos) == 0)
1291 while ((end[0] != ' ') && (end[0] != '\0'))
1302 /* illegal, but just keep it */
1305 /* illegal, but just ignore that there was a '/' */
1313 if (0 == strcmp (pos, match))
1316 memmove (pos, &end[1], strlen (&end[1]) + 1);
1324 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1326 GNUNET_free (match);
1335 GNUNET_free (match);
1341 * Wrapper around GNUNET_CONFIGURATION_parse.
1343 * @param cls the cfg
1344 * @param filename file to parse
1345 * @return GNUNET_OK on success
1348 parse_configuration_file (void *cls, const char *filename)
1350 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1354 /* Examine file extension */
1355 ext = strrchr (filename, '.');
1356 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1362 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1368 * Load default configuration. This function will parse the
1369 * defaults from the given defaults_d directory.
1371 * @param cfg configuration to update
1372 * @param defaults_d directory with the defaults
1373 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1376 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1377 const char *defaults_d)
1379 if (GNUNET_SYSERR ==
1380 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1381 return GNUNET_SYSERR; /* no configuration at all found */
1387 * Load configuration (starts with defaults, then loads
1388 * system-specific configuration).
1390 * @param cfg configuration to update
1391 * @param filename name of the configuration file, NULL to load defaults
1392 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1395 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1396 const char *filename)
1401 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1403 return GNUNET_SYSERR;
1405 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1406 GNUNET_free (ipath);
1407 if (GNUNET_SYSERR ==
1408 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1410 GNUNET_free (baseconfig);
1411 return GNUNET_SYSERR; /* no configuration at all found */
1413 GNUNET_free (baseconfig);
1414 if ((filename != NULL) &&
1415 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1417 /* specified configuration not found */
1418 return GNUNET_SYSERR;
1421 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1423 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1426 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1428 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1429 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1435 /* end of configuration.c */