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