preparations for fixing #4065
[oweals/gnunet.git] / src / util / configuration.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2007, 2008, 2009, 2013 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      option) any later version.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file src/util/configuration.c
23  * @brief configuration management
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_strings_lib.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_disk_lib.h"
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
36
37 /**
38  * @brief configuration entry
39  */
40 struct ConfigEntry
41 {
42
43   /**
44    * This is a linked list.
45    */
46   struct ConfigEntry *next;
47
48   /**
49    * key for this entry
50    */
51   char *key;
52
53   /**
54    * current, commited value
55    */
56   char *val;
57 };
58
59
60 /**
61  * @brief configuration section
62  */
63 struct ConfigSection
64 {
65   /**
66    * This is a linked list.
67    */
68   struct ConfigSection *next;
69
70   /**
71    * entries in the section
72    */
73   struct ConfigEntry *entries;
74
75   /**
76    * name of the section
77    */
78   char *name;
79 };
80
81
82 /**
83  * @brief configuration data
84  */
85 struct GNUNET_CONFIGURATION_Handle
86 {
87   /**
88    * Configuration sections.
89    */
90   struct ConfigSection *sections;
91
92   /**
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)
96    */
97   int dirty;
98
99 };
100
101
102 /**
103  * Used for diffing a configuration object against
104  * the default one
105  */
106 struct DiffHandle
107 {
108   const struct GNUNET_CONFIGURATION_Handle *cfg_default;
109
110   struct GNUNET_CONFIGURATION_Handle *cfgDiff;
111 };
112
113
114 /**
115  * Create a GNUNET_CONFIGURATION_Handle.
116  *
117  * @return fresh configuration object
118  */
119 struct GNUNET_CONFIGURATION_Handle *
120 GNUNET_CONFIGURATION_create ()
121 {
122   return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
123 }
124
125
126 /**
127  * Destroy configuration object.
128  *
129  * @param cfg configuration to destroy
130  */
131 void
132 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
133 {
134   struct ConfigSection *sec;
135
136   while (NULL != (sec = cfg->sections))
137     GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
138   GNUNET_free (cfg);
139 }
140
141
142 /**
143  * De-serializes configuration
144  *
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_SYSERR on error
152  */
153 int
154 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
155                                   const char *mem,
156                                   const size_t size,
157                                   int allow_inline)
158 {
159   char *line;
160   char *line_orig;
161   size_t line_size;
162   char *pos;
163   unsigned int nr;
164   size_t r_bytes;
165   size_t to_read;
166   size_t i;
167   int emptyline;
168   int ret;
169   char *section;
170   char *eq;
171   char *tag;
172   char *value;
173
174   LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing config file\n");
175   ret = GNUNET_OK;
176   section = GNUNET_strdup ("");
177   nr = 0;
178   r_bytes = 0;
179   line_orig = NULL;
180   while (r_bytes < size)
181   {
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);
186     if (NULL == pos)
187     {
188       line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
189       r_bytes += line_size;
190     }
191     else
192     {
193       line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
194       r_bytes += line_size + 1;
195     }
196     line = line_orig;
197     /* increment line number */
198     nr++;
199     /* tabs and '\r' are whitespace */
200     emptyline = GNUNET_YES;
201     for (i = 0; i < line_size; i++)
202     {
203       if (line[i] == '\t')
204         line[i] = ' ';
205       if (line[i] == '\r')
206         line[i] = ' ';
207       if (' ' != line[i])
208         emptyline = GNUNET_NO;
209     }
210     /* ignore empty lines */
211     if (GNUNET_YES == emptyline)
212       continue;
213
214     /* remove tailing whitespace */
215     for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
216       line[i] = '\0';
217
218     /* remove leading whitespace */
219     for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
220
221     /* ignore comments */
222     if ( ('#' == line[0]) || ('%' == line[0]) )
223       continue;
224
225     /* handle special "@INLINE@" directive */
226     if (0 == strncasecmp (line,
227                           "@INLINE@ ",
228                           strlen ("@INLINE@ ")))
229     {
230       /* @INLINE@ value */
231       value = &line[strlen ("@INLINE@ ")];
232       if (GNUNET_YES == allow_inline)
233       {
234         if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
235         {
236           ret = GNUNET_SYSERR;    /* failed to parse included config */
237           break;
238         }
239       }
240       else
241       {
242         LOG (GNUNET_ERROR_TYPE_DEBUG,
243              "Ignoring parsing @INLINE@ configurations, not allowed!\n");
244         ret = GNUNET_SYSERR;
245         break;
246       }
247       continue;
248     }
249     if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
250     {
251       /* [value] */
252       line[line_size - 1] = '\0';
253       value = &line[1];
254       GNUNET_free (section);
255       section = GNUNET_strdup (value);
256       LOG (GNUNET_ERROR_TYPE_DEBUG,
257            "Config section `%s'\n",
258            section);
259       continue;
260     }
261     if (NULL != (eq = strchr (line, '=')))
262     {
263       /* tag = value */
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--)
267         tag[i] = '\0';
268
269       /* Strip whitespace */
270       value = eq + 1;
271       while (isspace ((unsigned char) value[0]))
272         value++;
273       for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
274         value[i] = '\0';
275
276       /* remove quotes */
277       i = 0;
278       if ( ('"' == value[0]) &&
279            ('"' == value[strlen (value) - 1]) )
280       {
281         value[strlen (value) - 1] = '\0';
282         value++;
283       }
284       LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s=\"%s\"\n", tag, value);
285       GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
286       GNUNET_free (tag);
287       continue;
288     }
289     /* parse error */
290     LOG (GNUNET_ERROR_TYPE_WARNING,
291          _("Syntax error while deserializing in line %u\n"),
292          nr);
293     ret = GNUNET_SYSERR;
294     break;
295   }
296   LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n");
297   GNUNET_free_non_null (line_orig);
298   GNUNET_free (section);
299   GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
300   return ret;
301 }
302
303
304 /**
305  * Parse a configuration file, add all of the options in the
306  * file to the configuration environment.
307  *
308  * @param cfg configuration to update
309  * @param filename name of the configuration file
310  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
311  */
312 int
313 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
314                             const char *filename)
315 {
316   uint64_t fs64;
317   size_t fs;
318   char *fn;
319   char *mem;
320   int dirty;
321   int ret;
322
323   LOG (GNUNET_ERROR_TYPE_DEBUG,
324        "Asked to parse config file `%s'\n",
325        filename);
326   fn = GNUNET_STRINGS_filename_expand (filename);
327   LOG (GNUNET_ERROR_TYPE_DEBUG,
328        "Config file name expanded to `%s'\n",
329        fn);
330   if (NULL == fn)
331     return GNUNET_SYSERR;
332   dirty = cfg->dirty;           /* back up value! */
333   if (GNUNET_SYSERR ==
334       GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
335   {
336     LOG (GNUNET_ERROR_TYPE_WARNING,
337          "Error while determining the file size of %s\n", fn);
338     GNUNET_free (fn);
339     return GNUNET_SYSERR;
340   }
341   if (fs64 > SIZE_MAX)
342   {
343     GNUNET_break (0);           /* File size is more than the heap size */
344     GNUNET_free (fn);
345     return GNUNET_SYSERR;
346   }
347   fs = fs64;
348   mem = GNUNET_malloc (fs);
349   if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
350   {
351     LOG (GNUNET_ERROR_TYPE_WARNING,
352          "Error while reading file %s\n",
353          fn);
354     GNUNET_free (fn);
355     GNUNET_free (mem);
356     return GNUNET_SYSERR;
357   }
358   LOG (GNUNET_ERROR_TYPE_DEBUG,
359        "Deserializing contents of file `%s'\n",
360        fn);
361   GNUNET_free (fn);
362   ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
363   GNUNET_free (mem);
364   /* restore dirty flag - anything we set in the meantime
365    * came from disk */
366   cfg->dirty = dirty;
367   return ret;
368 }
369
370
371 /**
372  * Test if there are configuration options that were
373  * changed since the last save.
374  *
375  * @param cfg configuration to inspect
376  * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
377  */
378 int
379 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
380 {
381   return cfg->dirty;
382 }
383
384
385 /**
386  * Serializes the given configuration.
387  *
388  * @param cfg configuration to serialize
389  * @param size will be set to the size of the serialized memory block
390  * @return the memory block where the serialized configuration is
391  *           present. This memory should be freed by the caller
392  */
393 char *
394 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
395                                 size_t *size)
396 {
397   struct ConfigSection *sec;
398   struct ConfigEntry *ent;
399   char *mem;
400   char *cbuf;
401   char *val;
402   char *pos;
403   int len;
404   size_t m_size;
405   size_t c_size;
406
407
408   /* Pass1 : calculate the buffer size required */
409   m_size = 0;
410   for (sec = cfg->sections; NULL != sec; sec = sec->next)
411   {
412     /* For each section we need to add 3 charaters: {'[',']','\n'} */
413     m_size += strlen (sec->name) + 3;
414     for (ent = sec->entries; NULL != ent; ent = ent->next)
415     {
416       if (NULL != ent->val)
417       {
418         /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
419         pos = ent->val;
420         while (NULL != (pos = strstr (pos, "\n")))
421         {
422           m_size++;
423           pos++;
424         }
425         /* For each key = value pair we need to add 4 characters (2
426            spaces and 1 equal-to character and 1 new line) */
427         m_size += strlen (ent->key) + strlen (ent->val) + 4;
428       }
429     }
430     /* A new line after section end */
431     m_size++;
432   }
433
434   /* Pass2: Allocate memory and write the configuration to it */
435   mem = GNUNET_malloc (m_size);
436   sec = cfg->sections;
437   c_size = 0;
438   *size = c_size;
439   while (NULL != sec)
440   {
441     len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
442     GNUNET_assert (0 < len);
443     memcpy (mem + c_size, cbuf, len);
444     c_size += len;
445     GNUNET_free (cbuf);
446     for (ent = sec->entries; NULL != ent; ent = ent->next)
447     {
448       if (NULL != ent->val)
449       {
450         val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
451         strcpy (val, ent->val);
452         while (NULL != (pos = strstr (val, "\n")))
453         {
454           memmove (&pos[2], &pos[1], strlen (&pos[1]));
455           pos[0] = '\\';
456           pos[1] = 'n';
457         }
458         len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
459         GNUNET_free (val);
460         memcpy (mem + c_size, cbuf, len);
461         c_size += len;
462         GNUNET_free (cbuf);
463       }
464     }
465     memcpy (mem + c_size, "\n", 1);
466     c_size ++;
467     sec = sec->next;
468   }
469   GNUNET_assert (c_size == m_size);
470   *size = c_size;
471   return mem;
472 }
473
474
475 /**
476  * Write configuration file.
477  *
478  * @param cfg configuration to write
479  * @param filename where to write the configuration
480  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
481  */
482 int
483 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
484                             const char *filename)
485 {
486   char *fn;
487   char *cfg_buf;
488   size_t size;
489
490   fn = GNUNET_STRINGS_filename_expand (filename);
491   if (fn == NULL)
492     return GNUNET_SYSERR;
493   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
494   {
495     GNUNET_free (fn);
496     return GNUNET_SYSERR;
497   }
498   cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
499   if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
500                                     GNUNET_DISK_PERM_USER_READ
501                                     | GNUNET_DISK_PERM_USER_WRITE
502                                     | GNUNET_DISK_PERM_GROUP_READ
503                                     | GNUNET_DISK_PERM_GROUP_WRITE))
504   {
505     GNUNET_free (fn);
506     GNUNET_free (cfg_buf);
507     LOG (GNUNET_ERROR_TYPE_WARNING,
508          "Writing configration to file: %s failed\n", filename);
509     cfg->dirty = GNUNET_SYSERR; /* last write failed */
510     return GNUNET_SYSERR;
511   }
512   GNUNET_free (fn);
513   GNUNET_free (cfg_buf);
514   cfg->dirty = GNUNET_NO;       /* last write succeeded */
515   return GNUNET_OK;
516 }
517
518
519 /**
520  * Iterate over all options in the configuration.
521  *
522  * @param cfg configuration to inspect
523  * @param iter function to call on each option
524  * @param iter_cls closure for @a iter
525  */
526 void
527 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
528                               GNUNET_CONFIGURATION_Iterator iter,
529                               void *iter_cls)
530 {
531   struct ConfigSection *spos;
532   struct ConfigEntry *epos;
533
534   for (spos = cfg->sections; NULL != spos; spos = spos->next)
535     for (epos = spos->entries; NULL != epos; epos = epos->next)
536       if (NULL != epos->val)
537         iter (iter_cls, spos->name, epos->key, epos->val);
538 }
539
540
541 /**
542  * Iterate over values of a section in the configuration.
543  *
544  * @param cfg configuration to inspect
545  * @param section the section
546  * @param iter function to call on each option
547  * @param iter_cls closure for @a iter
548  */
549 void
550 GNUNET_CONFIGURATION_iterate_section_values (const struct
551                                              GNUNET_CONFIGURATION_Handle *cfg,
552                                              const char *section,
553                                              GNUNET_CONFIGURATION_Iterator iter,
554                                              void *iter_cls)
555 {
556   struct ConfigSection *spos;
557   struct ConfigEntry *epos;
558
559   spos = cfg->sections;
560   while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
561     spos = spos->next;
562   if (NULL == spos)
563     return;
564   for (epos = spos->entries; NULL != epos; epos = epos->next)
565     if (NULL != epos->val)
566       iter (iter_cls, spos->name, epos->key, epos->val);
567 }
568
569
570 /**
571  * Iterate over all sections in the configuration.
572  *
573  * @param cfg configuration to inspect
574  * @param iter function to call on each section
575  * @param iter_cls closure for @a iter
576  */
577 void
578 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
579                                        GNUNET_CONFIGURATION_Section_Iterator iter,
580                                        void *iter_cls)
581 {
582   struct ConfigSection *spos;
583   struct ConfigSection *next;
584
585   next = cfg->sections;
586   while (next != NULL)
587   {
588     spos = next;
589     next = spos->next;
590     iter (iter_cls, spos->name);
591   }
592 }
593
594
595 /**
596  * Remove the given section and all options in it.
597  *
598  * @param cfg configuration to inspect
599  * @param section name of the section to remove
600  */
601 void
602 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
603                                      const char *section)
604 {
605   struct ConfigSection *spos;
606   struct ConfigSection *prev;
607   struct ConfigEntry *ent;
608
609   prev = NULL;
610   spos = cfg->sections;
611   while (NULL != spos)
612   {
613     if (0 == strcasecmp (section, spos->name))
614     {
615       if (NULL == prev)
616         cfg->sections = spos->next;
617       else
618         prev->next = spos->next;
619       while (NULL != (ent = spos->entries))
620       {
621         spos->entries = ent->next;
622         GNUNET_free (ent->key);
623         GNUNET_free_non_null (ent->val);
624         GNUNET_free (ent);
625         cfg->dirty = GNUNET_YES;
626       }
627       GNUNET_free (spos->name);
628       GNUNET_free (spos);
629       return;
630     }
631     prev = spos;
632     spos = spos->next;
633   }
634 }
635
636
637 /**
638  * Copy a configuration value to the given target configuration.
639  * Overwrites existing entries.
640  *
641  * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
642  * @param section section for the value
643  * @param option option name of the value
644  * @param value value to copy
645  */
646 static void
647 copy_entry (void *cls,
648             const char *section,
649             const char *option,
650             const char *value)
651 {
652   struct GNUNET_CONFIGURATION_Handle *dst = cls;
653
654   GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
655 }
656
657
658 /**
659  * Duplicate an existing configuration object.
660  *
661  * @param cfg configuration to duplicate
662  * @return duplicate configuration
663  */
664 struct GNUNET_CONFIGURATION_Handle *
665 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
666 {
667   struct GNUNET_CONFIGURATION_Handle *ret;
668
669   ret = GNUNET_CONFIGURATION_create ();
670   GNUNET_CONFIGURATION_iterate (cfg, &copy_entry, ret);
671   return ret;
672 }
673
674
675 /**
676  * Find a section entry from a configuration.
677  *
678  * @param cfg configuration to search in
679  * @param section name of the section to look for
680  * @return matching entry, NULL if not found
681  */
682 static struct ConfigSection *
683 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
684              const char *section)
685 {
686   struct ConfigSection *pos;
687
688   pos = cfg->sections;
689   while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
690     pos = pos->next;
691   return pos;
692 }
693
694
695 /**
696  * Find an entry from a configuration.
697  *
698  * @param cfg handle to the configuration
699  * @param section section the option is in
700  * @param key the option
701  * @return matching entry, NULL if not found
702  */
703 static struct ConfigEntry *
704 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
705            const char *section,
706            const char *key)
707 {
708   struct ConfigSection *sec;
709   struct ConfigEntry *pos;
710
711   if (NULL == (sec = find_section (cfg, section)))
712     return NULL;
713   pos = sec->entries;
714   while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
715     pos = pos->next;
716   return pos;
717 }
718
719
720 /**
721  * A callback function, compares entries from two configurations
722  * (default against a new configuration) and write the diffs in a
723  * diff-configuration object (the callback object).
724  *
725  * @param cls the diff configuration (`struct DiffHandle *`)
726  * @param section section for the value (of the default conf.)
727  * @param option option name of the value (of the default conf.)
728  * @param value value to copy (of the default conf.)
729  */
730 static void
731 compare_entries (void *cls,
732                  const char *section,
733                  const char *option,
734                  const char *value)
735 {
736   struct DiffHandle *dh = cls;
737   struct ConfigEntry *entNew;
738
739   entNew = find_entry (dh->cfg_default, section, option);
740   if ( (NULL != entNew) &&
741        (NULL != entNew->val) &&
742        (0 == strcmp (entNew->val, value)) )
743     return;
744   GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
745 }
746
747
748 /**
749  * Compute configuration with only entries that have been changed
750  *
751  * @param cfg_default original configuration
752  * @param cfg_new new configuration
753  * @return configuration with only the differences, never NULL
754  */
755 struct GNUNET_CONFIGURATION_Handle *
756 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
757                                const struct GNUNET_CONFIGURATION_Handle *cfg_new)
758 {
759   struct DiffHandle diffHandle;
760
761   diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
762   diffHandle.cfg_default = cfg_default;
763   GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
764   return diffHandle.cfgDiff;
765 }
766
767
768 /**
769  * Write only configuration entries that have been changed to configuration file
770  *
771  * @param cfg_default default configuration
772  * @param cfg_new new configuration
773  * @param filename where to write the configuration diff between default and new
774  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
775  */
776 int
777 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
778                                   *cfg_default,
779                                   const struct GNUNET_CONFIGURATION_Handle
780                                   *cfg_new, const char *filename)
781 {
782   int ret;
783   struct GNUNET_CONFIGURATION_Handle *diff;
784
785   diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
786   ret = GNUNET_CONFIGURATION_write (diff, filename);
787   GNUNET_CONFIGURATION_destroy (diff);
788   return ret;
789 }
790
791
792 /**
793  * Set a configuration value that should be a string.
794  *
795  * @param cfg configuration to update
796  * @param section section of interest
797  * @param option option of interest
798  * @param value value to set
799  */
800 void
801 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
802                                        const char *section, const char *option,
803                                        const char *value)
804 {
805   struct ConfigSection *sec;
806   struct ConfigEntry *e;
807   char *nv;
808
809   e = find_entry (cfg, section, option);
810   if (NULL != e)
811   {
812     if (NULL == value)
813     {
814       GNUNET_free_non_null (e->val);
815       e->val = NULL;
816     }
817     else
818     {
819       nv = GNUNET_strdup (value);
820       GNUNET_free_non_null (e->val);
821       e->val = nv;
822     }
823     return;
824   }
825   sec = find_section (cfg, section);
826   if (sec == NULL)
827   {
828     sec = GNUNET_new (struct ConfigSection);
829     sec->name = GNUNET_strdup (section);
830     sec->next = cfg->sections;
831     cfg->sections = sec;
832   }
833   e = GNUNET_new (struct ConfigEntry);
834   e->key = GNUNET_strdup (option);
835   e->val = GNUNET_strdup (value);
836   e->next = sec->entries;
837   sec->entries = e;
838 }
839
840
841 /**
842  * Set a configuration value that should be a number.
843  *
844  * @param cfg configuration to update
845  * @param section section of interest
846  * @param option option of interest
847  * @param number value to set
848  */
849 void
850 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
851                                        const char *section, const char *option,
852                                        unsigned long long number)
853 {
854   char s[64];
855
856   GNUNET_snprintf (s, 64, "%llu", number);
857   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
858 }
859
860
861 /**
862  * Get a configuration value that should be a number.
863  *
864  * @param cfg configuration to inspect
865  * @param section section of interest
866  * @param option option of interest
867  * @param number where to store the numeric value of the option
868  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
869  */
870 int
871 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
872                                        *cfg, const char *section,
873                                        const char *option,
874                                        unsigned long long *number)
875 {
876   struct ConfigEntry *e;
877
878   if (NULL == (e = find_entry (cfg, section, option)))
879     return GNUNET_SYSERR;
880   if (NULL == e->val)
881     return GNUNET_SYSERR;
882   if (1 != SSCANF (e->val, "%llu", number))
883     return GNUNET_SYSERR;
884   return GNUNET_OK;
885 }
886
887 /**
888  * Get a configuration value that should be a floating point number.
889  *
890  * @param cfg configuration to inspect
891  * @param section section of interest
892  * @param option option of interest
893  * @param number where to store the floating value of the option
894  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
895  */
896 int
897 GNUNET_CONFIGURATION_get_value_float  (const struct GNUNET_CONFIGURATION_Handle
898                                        *cfg, const char *section,
899                                        const char *option,
900                                        float *number)
901 {
902   struct ConfigEntry *e;
903
904   if (NULL == (e = find_entry (cfg, section, option)))
905     return GNUNET_SYSERR;
906   if (NULL == e->val)
907     return GNUNET_SYSERR;
908   if (1 != SSCANF (e->val, "%f", number))
909     return GNUNET_SYSERR;
910   return GNUNET_OK;
911 }
912
913
914
915 /**
916  * Get a configuration value that should be a relative time.
917  *
918  * @param cfg configuration to inspect
919  * @param section section of interest
920  * @param option option of interest
921  * @param time set to the time value stored in the configuration
922  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
923  */
924 int
925 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
926                                      const char *section,
927                                      const char *option,
928                                      struct GNUNET_TIME_Relative *time)
929 {
930   struct ConfigEntry *e;
931
932   if (NULL == (e = find_entry (cfg, section, option)))
933     return GNUNET_SYSERR;
934   if (NULL == e->val)
935     return GNUNET_SYSERR;
936   return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
937 }
938
939
940 /**
941  * Get a configuration value that should be a size in bytes.
942  *
943  * @param cfg configuration to inspect
944  * @param section section of interest
945  * @param option option of interest
946  * @param size set to the size in bytes as stored in the configuration
947  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
948  */
949 int
950 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
951                                      const char *section,
952                                      const char *option,
953                                      unsigned long long *size)
954 {
955   struct ConfigEntry *e;
956
957   if (NULL == (e = find_entry (cfg, section, option)))
958     return GNUNET_SYSERR;
959   if (NULL == e->val)
960     return GNUNET_SYSERR;
961   return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
962 }
963
964
965 /**
966  * Get a configuration value that should be a string.
967  *
968  * @param cfg configuration to inspect
969  * @param section section of interest
970  * @param option option of interest
971  * @param value will be set to a freshly allocated configuration
972  *        value, or NULL if option is not specified
973  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
974  */
975 int
976 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
977                                        const char *section,
978                                        const char *option,
979                                        char **value)
980 {
981   struct ConfigEntry *e;
982
983   LOG (GNUNET_ERROR_TYPE_DEBUG,
984        "Asked to retrieve string `%s' in section `%s'\n",
985        option,
986        section);
987   if ( (NULL == (e = find_entry (cfg, section, option))) ||
988        (NULL == e->val) )
989   {
990     *value = NULL;
991     return GNUNET_SYSERR;
992   }
993   *value = GNUNET_strdup (e->val);
994   return GNUNET_OK;
995 }
996
997
998 /**
999  * Get a configuration value that should be in a set of
1000  * predefined strings
1001  *
1002  * @param cfg configuration to inspect
1003  * @param section section of interest
1004  * @param option option of interest
1005  * @param choices NULL-terminated list of legal values
1006  * @param value will be set to an entry in the legal list,
1007  *        or NULL if option is not specified and no default given
1008  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1009  */
1010 int
1011 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1012                                        const char *section,
1013                                        const char *option,
1014                                        const char *const *choices,
1015                                        const char **value)
1016 {
1017   struct ConfigEntry *e;
1018   unsigned int i;
1019
1020   if (NULL == (e = find_entry (cfg, section, option)))
1021     return GNUNET_SYSERR;
1022   for (i = 0; NULL != choices[i]; i++)
1023     if (0 == strcasecmp (choices[i], e->val))
1024       break;
1025   if (NULL == choices[i])
1026   {
1027     LOG (GNUNET_ERROR_TYPE_ERROR,
1028          _("Configuration value '%s' for '%s'"
1029            " in section '%s' is not in set of legal choices\n"),
1030          e->val,
1031          option,
1032          section);
1033     return GNUNET_SYSERR;
1034   }
1035   *value = choices[i];
1036   return GNUNET_OK;
1037 }
1038
1039
1040 /**
1041  * Get crockford32-encoded fixed-size binary data from a configuration.
1042  *
1043  * @param cfg configuration to access
1044  * @param section section to access
1045  * @param option option to access
1046  * @param buf where to store the decoded binary result
1047  * @param buf_size exact number of bytes to store in @a buf
1048  * @return #GNUNET_OK on success
1049  *         #GNUNET_NO is the value does not exist
1050  *         #GNUNET_SYSERR on decoding error
1051  */
1052 int
1053 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1054                                const char *section,
1055                                const char *option,
1056                                void *buf,
1057                                size_t buf_size)
1058 {
1059   char *enc;
1060   int res;
1061   size_t data_size;
1062
1063   if (GNUNET_OK !=
1064       (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1065                                                     section,
1066                                                     option,
1067                                                     &enc)))
1068     return res;
1069   data_size = (strlen (enc) * 5) / 8;
1070   if (data_size != buf_size)
1071   {
1072     GNUNET_free (enc);
1073     return GNUNET_SYSERR;
1074   }
1075   if (GNUNET_OK !=
1076       GNUNET_STRINGS_string_to_data (enc,
1077                                      strlen (enc),
1078                                      buf, buf_size))
1079   {
1080     GNUNET_free (enc);
1081     return GNUNET_SYSERR;
1082   }
1083   GNUNET_free (enc);
1084   return GNUNET_OK;
1085 }
1086
1087
1088 /**
1089  * Test if we have a value for a particular option
1090  *
1091  * @param cfg configuration to inspect
1092  * @param section section of interest
1093  * @param option option of interest
1094  * @return #GNUNET_YES if so, #GNUNET_NO if not.
1095  */
1096 int
1097 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1098                                  const char *section,
1099                                  const char *option)
1100 {
1101   struct ConfigEntry *e;
1102
1103   if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1104     return GNUNET_NO;
1105   return GNUNET_YES;
1106 }
1107
1108
1109 /**
1110  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1111  * where either in the "PATHS" section or the environtment "FOO" is
1112  * set to "DIRECTORY".  We also support default expansion,
1113  * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1114  * set in PATHS or the environment, and otherwise to "default".  Note
1115  * that "default" itself can also be a $-expression, thus
1116  * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1117  * to VAR2.
1118  *
1119  * @param cfg configuration to use for path expansion
1120  * @param orig string to $-expand (will be freed!)
1121  * @param depth recursion depth, used to detect recursive expansions
1122  * @return $-expanded string
1123  */
1124 static char *
1125 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1126                char *orig,
1127                unsigned int depth)
1128 {
1129   int i;
1130   char *prefix;
1131   char *result;
1132   char *start;
1133   const char *post;
1134   const char *env;
1135   char *def;
1136   char *end;
1137   unsigned int lopen;
1138   char erased_char;
1139   char *erased_pos;
1140   size_t len;
1141
1142   if (NULL == orig)
1143     return NULL;
1144   if (depth > 128)
1145   {
1146     LOG (GNUNET_ERROR_TYPE_WARNING,
1147          _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1148          orig);
1149     return orig;
1150   }
1151   LOG (GNUNET_ERROR_TYPE_DEBUG,
1152        "Asked to $-expand %s\n", orig);
1153   if ('$' != orig[0])
1154   {
1155     LOG (GNUNET_ERROR_TYPE_DEBUG,
1156          "Doesn't start with $ - not expanding\n");
1157     return orig;
1158   }
1159   erased_char = 0;
1160   erased_pos = NULL;
1161   if ('{' == orig[1])
1162   {
1163     start = &orig[2];
1164     lopen = 1;
1165     end = &orig[1];
1166     while (lopen > 0)
1167     {
1168       end++;
1169       switch (*end)
1170       {
1171       case '}':
1172         lopen--;
1173         break;
1174       case '{':
1175         lopen++;
1176         break;
1177       case '\0':
1178         LOG (GNUNET_ERROR_TYPE_WARNING,
1179              _("Missing closing `%s' in option `%s'\n"),
1180              "}",
1181              orig);
1182         return orig;
1183       default:
1184         break;
1185       }
1186     }
1187     erased_char = *end;
1188     erased_pos = end;
1189     *end = '\0';
1190     post = end + 1;
1191     def = strchr (orig, ':');
1192     if (NULL != def)
1193     {
1194       *def = '\0';
1195       def++;
1196       if ( ('-' == *def) ||
1197            ('=' == *def) )
1198         def++;
1199       def = GNUNET_strdup (def);
1200     }
1201   }
1202   else
1203   {
1204     start = &orig[1];
1205     def = NULL;
1206     i = 0;
1207     while ( (orig[i] != '/') &&
1208             (orig[i] != '\\') &&
1209             (orig[i] != '\0')  &&
1210             (orig[i] != ' ') )
1211       i++;
1212     if (orig[i] == '\0')
1213     {
1214       post = "";
1215     }
1216     else
1217     {
1218       erased_char = orig[i];
1219       erased_pos = &orig[i];
1220       orig[i] = '\0';
1221       post = &orig[i + 1];
1222     }
1223   }
1224   LOG (GNUNET_ERROR_TYPE_DEBUG,
1225        "Split into `%s' and `%s' with default %s\n",
1226        start,
1227        post,
1228        def);
1229   if (GNUNET_OK !=
1230       GNUNET_CONFIGURATION_get_value_string (cfg,
1231                                              "PATHS",
1232                                              start,
1233                                              &prefix))
1234   {
1235     LOG (GNUNET_ERROR_TYPE_DEBUG,
1236          "Filename for `%s' is not in PATHS config section\n",
1237          start);
1238     if (NULL == (env = getenv (start)))
1239     {
1240       LOG (GNUNET_ERROR_TYPE_DEBUG,
1241            "`%s' is not an environment variable\n",
1242            start);
1243       /* try default */
1244       def = expand_dollar (cfg, def, depth + 1);
1245       env = def;
1246     }
1247     if (NULL == env)
1248     {
1249       start = GNUNET_strdup (start);
1250       if (erased_pos)
1251         *erased_pos = erased_char;
1252       LOG (GNUNET_ERROR_TYPE_WARNING,
1253            _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1254            start, orig);
1255       GNUNET_free (start);
1256       return orig;
1257     }
1258     prefix = GNUNET_strdup (env);
1259   }
1260   prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1261   LOG (GNUNET_ERROR_TYPE_DEBUG,
1262        "Prefix is `%s'\n",
1263        prefix);
1264   if ( (erased_pos) && ('}' != erased_char) )
1265   {
1266     len = strlen (prefix) + 1;
1267     prefix = GNUNET_realloc (prefix, len + 1);
1268     prefix[len - 1] = erased_char;
1269     prefix[len] = '\0';
1270   }
1271   result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1272   strcpy (result, prefix);
1273   strcat (result, post);
1274   GNUNET_free_non_null (def);
1275   GNUNET_free (prefix);
1276   GNUNET_free (orig);
1277   LOG (GNUNET_ERROR_TYPE_DEBUG,
1278        "Expanded to `%s'\n",
1279        result);
1280   return result;
1281 }
1282
1283
1284 /**
1285  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1286  * where either in the "PATHS" section or the environtment "FOO" is
1287  * set to "DIRECTORY".  We also support default expansion,
1288  * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1289  * set in PATHS or the environment, and otherwise to "default".  Note
1290  * that "default" itself can also be a $-expression, thus
1291  * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1292  * to VAR2.
1293  *
1294  * @param cfg configuration to use for path expansion
1295  * @param orig string to $-expand (will be freed!).  Note that multiple
1296  *          $-expressions can be present in this string.  They will all be
1297  *          $-expanded.
1298  * @return $-expanded string
1299  */
1300 char *
1301 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1302                                     char *orig)
1303 {
1304   char *dup;
1305   size_t i;
1306   size_t len;
1307
1308   for (i = 0; '\0' != orig[i]; i++)
1309   {
1310     if ('$' != orig[i])
1311       continue;
1312     dup = GNUNET_strdup (orig + i);
1313     dup = expand_dollar (cfg, dup, 0);
1314     len = strlen (dup) + 1;
1315     orig = GNUNET_realloc (orig, i + len);
1316     memcpy (orig + i, dup, len);
1317     GNUNET_free (dup);
1318   }
1319   return orig;
1320 }
1321
1322
1323 /**
1324  * Get a configuration value that should be a string.
1325  *
1326  * @param cfg configuration to inspect
1327  * @param section section of interest
1328  * @param option option of interest
1329  * @param value will be set to a freshly allocated configuration
1330  *        value, or NULL if option is not specified
1331  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1332  */
1333 int
1334 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1335                                          const char *section,
1336                                          const char *option,
1337                                          char **value)
1338 {
1339   char *tmp;
1340
1341   LOG (GNUNET_ERROR_TYPE_DEBUG,
1342        "Asked to retrieve filename `%s' in section `%s'\n",
1343        option,
1344        section);
1345   if (GNUNET_OK !=
1346       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1347   {
1348     LOG (GNUNET_ERROR_TYPE_DEBUG,
1349          "Failed to retrieve filename\n");
1350     *value = NULL;
1351     return GNUNET_SYSERR;
1352   }
1353   LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1354   tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1355   LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1356   *value = GNUNET_STRINGS_filename_expand (tmp);
1357   GNUNET_free (tmp);
1358   LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1359   if (*value == NULL)
1360     return GNUNET_SYSERR;
1361   return GNUNET_OK;
1362 }
1363
1364
1365 /**
1366  * Get a configuration value that should be in a set of
1367  * "YES" or "NO".
1368  *
1369  * @param cfg configuration to inspect
1370  * @param section section of interest
1371  * @param option option of interest
1372  * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1373  */
1374 int
1375 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1376                                       const char *section,
1377                                       const char *option)
1378 {
1379   static const char *yesno[] = { "YES", "NO", NULL };
1380   const char *val;
1381   int ret;
1382
1383   ret =
1384       GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1385   if (ret == GNUNET_SYSERR)
1386     return ret;
1387   if (val == yesno[0])
1388     return GNUNET_YES;
1389   return GNUNET_NO;
1390 }
1391
1392
1393 /**
1394  * Iterate over the set of filenames stored in a configuration value.
1395  *
1396  * @param cfg configuration to inspect
1397  * @param section section of interest
1398  * @param option option of interest
1399  * @param cb function to call on each filename
1400  * @param cb_cls closure for @a cb
1401  * @return number of filenames iterated over, -1 on error
1402  */
1403 int
1404 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1405                                               const char *section,
1406                                               const char *option,
1407                                               GNUNET_FileNameCallback cb,
1408                                               void *cb_cls)
1409 {
1410   char *list;
1411   char *pos;
1412   char *end;
1413   char old;
1414   int ret;
1415
1416   if (GNUNET_OK !=
1417       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1418     return 0;
1419   GNUNET_assert (list != NULL);
1420   ret = 0;
1421   pos = list;
1422   while (1)
1423   {
1424     while (pos[0] == ' ')
1425       pos++;
1426     if (strlen (pos) == 0)
1427       break;
1428     end = pos + 1;
1429     while ((end[0] != ' ') && (end[0] != '\0'))
1430     {
1431       if (end[0] == '\\')
1432       {
1433         switch (end[1])
1434         {
1435         case '\\':
1436         case ' ':
1437           memmove (end, &end[1], strlen (&end[1]) + 1);
1438         case '\0':
1439           /* illegal, but just keep it */
1440           break;
1441         default:
1442           /* illegal, but just ignore that there was a '/' */
1443           break;
1444         }
1445       }
1446       end++;
1447     }
1448     old = end[0];
1449     end[0] = '\0';
1450     if (strlen (pos) > 0)
1451     {
1452       ret++;
1453       if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1454       {
1455         ret = GNUNET_SYSERR;
1456         break;
1457       }
1458     }
1459     if (old == '\0')
1460       break;
1461     pos = end + 1;
1462   }
1463   GNUNET_free (list);
1464   return ret;
1465 }
1466
1467
1468 /**
1469  * FIXME.
1470  *
1471  * @param value FIXME
1472  * @return FIXME
1473  */
1474 static char *
1475 escape_name (const char *value)
1476 {
1477   char *escaped;
1478   const char *rpos;
1479   char *wpos;
1480
1481   escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1482   memset (escaped, 0, strlen (value) * 2 + 1);
1483   rpos = value;
1484   wpos = escaped;
1485   while (rpos[0] != '\0')
1486   {
1487     switch (rpos[0])
1488     {
1489     case '\\':
1490     case ' ':
1491       wpos[0] = '\\';
1492       wpos[1] = rpos[0];
1493       wpos += 2;
1494       break;
1495     default:
1496       wpos[0] = rpos[0];
1497       wpos++;
1498     }
1499     rpos++;
1500   }
1501   return escaped;
1502 }
1503
1504
1505 /**
1506  * FIXME.
1507  *
1508  * @param cls string we compare with (const char*)
1509  * @param fn filename we are currently looking at
1510  * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1511  */
1512 static int
1513 test_match (void *cls, const char *fn)
1514 {
1515   const char *of = cls;
1516
1517   return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1518 }
1519
1520
1521 /**
1522  * Append a filename to a configuration value that
1523  * represents a list of filenames
1524  *
1525  * @param cfg configuration to update
1526  * @param section section of interest
1527  * @param option option of interest
1528  * @param value filename to append
1529  * @return #GNUNET_OK on success,
1530  *         #GNUNET_NO if the filename already in the list
1531  *         #GNUNET_SYSERR on error
1532  */
1533 int
1534 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1535                                             const char *section,
1536                                             const char *option,
1537                                             const char *value)
1538 {
1539   char *escaped;
1540   char *old;
1541   char *nw;
1542
1543   if (GNUNET_SYSERR ==
1544       GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1545                                                     &test_match,
1546                                                     (void *) value))
1547     return GNUNET_NO;           /* already exists */
1548   if (GNUNET_OK !=
1549       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1550     old = GNUNET_strdup ("");
1551   escaped = escape_name (value);
1552   nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1553   strcpy (nw, old);
1554   if (strlen (old) > 0)
1555     strcat (nw, " ");
1556   strcat (nw, escaped);
1557   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1558   GNUNET_free (old);
1559   GNUNET_free (nw);
1560   GNUNET_free (escaped);
1561   return GNUNET_OK;
1562 }
1563
1564
1565 /**
1566  * Remove a filename from a configuration value that
1567  * represents a list of filenames
1568  *
1569  * @param cfg configuration to update
1570  * @param section section of interest
1571  * @param option option of interest
1572  * @param value filename to remove
1573  * @return #GNUNET_OK on success,
1574  *         #GNUNET_NO if the filename is not in the list,
1575  *         #GNUNET_SYSERR on error
1576  */
1577 int
1578 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1579                                             *cfg, const char *section,
1580                                             const char *option,
1581                                             const char *value)
1582 {
1583   char *list;
1584   char *pos;
1585   char *end;
1586   char *match;
1587   char old;
1588
1589   if (GNUNET_OK !=
1590       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1591     return GNUNET_NO;
1592   match = escape_name (value);
1593   pos = list;
1594   while (1)
1595   {
1596     while (pos[0] == ' ')
1597       pos++;
1598     if (strlen (pos) == 0)
1599       break;
1600     end = pos + 1;
1601     while ((end[0] != ' ') && (end[0] != '\0'))
1602     {
1603       if (end[0] == '\\')
1604       {
1605         switch (end[1])
1606         {
1607         case '\\':
1608         case ' ':
1609           end++;
1610           break;
1611         case '\0':
1612           /* illegal, but just keep it */
1613           break;
1614         default:
1615           /* illegal, but just ignore that there was a '/' */
1616           break;
1617         }
1618       }
1619       end++;
1620     }
1621     old = end[0];
1622     end[0] = '\0';
1623     if (0 == strcmp (pos, match))
1624     {
1625       if (old != '\0')
1626         memmove (pos, &end[1], strlen (&end[1]) + 1);
1627       else
1628       {
1629         if (pos != list)
1630           pos[-1] = '\0';
1631         else
1632           pos[0] = '\0';
1633       }
1634       GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1635       GNUNET_free (list);
1636       GNUNET_free (match);
1637       return GNUNET_OK;
1638     }
1639     if (old == '\0')
1640       break;
1641     end[0] = old;
1642     pos = end + 1;
1643   }
1644   GNUNET_free (list);
1645   GNUNET_free (match);
1646   return GNUNET_NO;
1647 }
1648
1649
1650 /**
1651  * Wrapper around #GNUNET_CONFIGURATION_parse.  Called on each
1652  * file in a directory, we trigger parsing on those files that
1653  * end with ".conf".
1654  *
1655  * @param cls the cfg
1656  * @param filename file to parse
1657  * @return #GNUNET_OK on success
1658  */
1659 static int
1660 parse_configuration_file (void *cls, const char *filename)
1661 {
1662   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1663   char * ext;
1664   int ret;
1665
1666   /* Examine file extension */
1667   ext = strrchr (filename, '.');
1668   if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1669   {
1670     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1671                 "Skipping file `%s'\n",
1672                 filename);
1673     return GNUNET_OK;
1674   }
1675
1676   ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1677   return ret;
1678 }
1679
1680
1681 /**
1682  * Load default configuration.  This function will parse the
1683  * defaults from the given defaults_d directory.
1684  *
1685  * @param cfg configuration to update
1686  * @param defaults_d directory with the defaults
1687  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1688  */
1689 int
1690 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1691                                 const char *defaults_d)
1692 {
1693   if (GNUNET_SYSERR ==
1694       GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1695     return GNUNET_SYSERR;       /* no configuration at all found */
1696   return GNUNET_OK;
1697 }
1698
1699
1700 /* end of configuration.c */