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